Author Topic: Copy Last object to list of coordinates in CSV file  (Read 10046 times)

0 Members and 1 Guest are viewing this topic.

mjfarrell

  • Seagull
  • Posts: 14444
  • Every Student their own Lesson
Copy Last object to list of coordinates in CSV file
« on: July 15, 2011, 01:01:12 PM »
Hi all, I'm trying to help with a process, and I think lisp, or a script might solve part of it.
Granted I don't really do lisp, however this is what I think we need or want to do.

Copy the last object created by the user to a list of coordinate pairs in an external CSV, or delimited text file.

Thanks in advance for the help.
Be your Best


Michael Farrell
http://primeservicesglobal.com/

BlackBox

  • King Gator
  • Posts: 3770
Re: Copy Last object to list of coordinates in CSV file
« Reply #1 on: July 15, 2011, 01:27:45 PM »
Could you clarify the desired "list of coordinate pairs" part... are you wanting to copy all of the DXF "Grouped Pairs" to recreate/log the object? I'm not sure what you mean by this.
"How we think determines what we do, and what we do determines what we get."

mjfarrell

  • Seagull
  • Posts: 14444
  • Every Student their own Lesson
Re: Copy Last object to list of coordinates in CSV file
« Reply #2 on: July 15, 2011, 01:56:58 PM »
Could you clarify the desired "list of coordinate pairs" part... are you wanting to copy all of the DXF "Grouped Pairs" to recreate/log the object? I'm not sure what you mean by this.
The user will have a file (csv, or TXT) could be space or comma delimitted, in that file a list of X,Y pairs.

as in

1,5
5,7
9,7
4,8
3,13

etc, only they may actually be carried out to a couple of decimal places; my guess is the precision of the data isn't important to the process as long as all items in the list are processed.
It's just a list of points, in a file from where I'm sitting.
Be your Best


Michael Farrell
http://primeservicesglobal.com/

jbuzbee

  • Swamp Rat
  • Posts: 851
Re: Copy Last object to list of coordinates in CSV file
« Reply #3 on: July 15, 2011, 02:06:51 PM »
But if the points saved in the file aren't associated with the object what's the point?
James Buzbee
Windows 8

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: Copy Last object to list of coordinates in CSV file
« Reply #4 on: July 15, 2011, 02:07:15 PM »
If I understand correctly Michael, would something like this work for you?

Code: [Select]
(defun c:test ( / *error* _csv->lst _csv->var acdoc acsel data file pt )
  ;; © Lee Mac 2011

  (defun *error* ( msg )
    (if acdoc (_EndUndo acdoc))
    (if (and file (eq 'FILE (type file))) (close file))
    (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
        (princ (strcat "\n** Error: " msg " **")))
    (princ)
  )

  (defun _StartUndo ( doc ) (_EndUndo doc)
    (vla-StartUndoMark doc)
  )

  (defun _EndUndo ( doc )
    (if (= 8 (logand 8 (getvar 'UNDOCTL)))
      (vla-EndUndoMark doc)
    )
  )

  (defun _csv->lst ( str / pos )
    (if (setq pos (vl-string-position 44 str))
      (cons (substr str 1 pos) (_csv->lst (substr str (+ pos 2))))
      (list str)
    )
  )

  (defun _csv->var ( file / ln lst )
    (if (setq file (open file "r"))
      (progn
        (while (setq ln (read-line file))
          (if (apply 'and (setq ln (mapcar 'distof (_csv->lst ln))))
            (setq lst (cons (vlax-3D-point ln) lst))
          )
        )
        (setq file (close file))
      )
    )
    (reverse lst)
  )

  (setq acdoc (vla-get-activedocument (vlax-get-acad-object)))
  (cond
    ( (not (setq file (getfiled "Select Data File" "" "csv;txt" 16)))
      (princ "\n*Cancel*")
    )
    ( (not (setq data (_csv->var file)))
      (princ "\n--> No Point Data Found in Selected File.")
    )
    ( (and (ssget) (setq pt (getpoint "\nSpecify Base Point: ")))

      (_StartUndo acdoc)
      (setq pt (vlax-3D-point (trans pt 1 0)))
      (vlax-for obj (setq acsel (vla-get-activeselectionset acdoc))
        (foreach p1 data (vla-move (vla-copy obj) pt p1))
      )
      (vla-delete acsel)
      (_EndUndo acdoc)
    )
  )
  (princ)
)
(vl-load-com) (princ)

« Edited to Add (vl-load-com) »
« Last Edit: July 16, 2011, 07:46:33 AM by Lee Mac »

BlackBox

  • King Gator
  • Posts: 3770
Re: Copy Last object to list of coordinates in CSV file
« Reply #5 on: July 15, 2011, 02:07:47 PM »
Creating an external CSV is no biggie, nor is processing lists of coordinates to said CSV.

Perhaps I'm just not understanding how the desired coordinate pairs correlate to the "last object created."

As you well know, there's a multitude of coordinates that can be extracted from different object (entity) types, be they extracted by AutoLISP DXF code (or even Polar?), or Visual LISP properties.

For example - A line has Startpoint, Endpoint (and perhaps Mid?), whereas a Circle, or Arc has quadrants, and even a center. Simplest of all a Point entity has only the insertion point itself.

"How we think determines what we do, and what we do determines what we get."

mjfarrell

  • Seagull
  • Posts: 14444
  • Every Student their own Lesson
Re: Copy Last object to list of coordinates in CSV file
« Reply #6 on: July 15, 2011, 02:08:55 PM »
But if the points saved in the file aren't associated with the object what's the point?
we are making NEW objects at the point location(s)...otherwsie there would be no point, game, set, or match.   :wink:
Be your Best


Michael Farrell
http://primeservicesglobal.com/

mjfarrell

  • Seagull
  • Posts: 14444
  • Every Student their own Lesson
Re: Copy Last object to list of coordinates in CSV file
« Reply #7 on: July 15, 2011, 02:11:14 PM »
If I understand correctly Michael, would something like this work for you?

Code: [Select]
(defun c:test ( / *error* _csv->lst _csv->var acdoc acsel data file pt )
  ;; © Lee Mac 2011

  (defun *error* ( msg )
    (if acdoc (_EndUndo acdoc))
    (if (and file (eq 'FILE (type file))) (close file))
    (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
        (princ (strcat "\n** Error: " msg " **")))
    (princ)
  )

  (defun _StartUndo ( doc ) (_EndUndo doc)
    (vla-StartUndoMark doc)
  )

  (defun _EndUndo ( doc )
    (if (= 8 (logand 8 (getvar 'UNDOCTL)))
      (vla-EndUndoMark doc)
    )
  )

  (defun _csv->lst ( str / pos )
    (if (setq pos (vl-string-position 44 str))
      (cons (substr str 1 pos) (_csv->lst (substr str (+ pos 2))))
      (list str)
    )
  )

  (defun _csv->var ( file / ln lst )
    (if (setq file (open file "r"))
      (progn
        (while (setq ln (read-line file))
          (if (apply 'and (setq ln (mapcar 'distof (_csv->lst ln))))
            (setq lst (cons (vlax-3D-point ln) lst))
          )
        )
        (setq file (close file))
      )
    )
    (reverse lst)
  )

  (setq acdoc (vla-get-activedocument (vlax-get-acad-object)))
  (cond
    ( (not (setq file (getfiled "Select Data File" "" "csv;txt" 16)))
      (princ "\n*Cancel*")
    )
    ( (not (setq data (_csv->var file)))
      (princ "\n--> No Point Data Found in Selected File.")
    )
    ( (and (ssget) (setq pt (getpoint "\nSpecify Base Point: ")))

      (_StartUndo acdoc)
      (setq pt (vlax-3D-point (trans pt 1 0)))
      (vlax-for obj (setq acsel (vla-get-activeselectionset acdoc))
        (foreach p1 data (vla-move (vla-copy obj) pt p1))
      )
      (vla-delete acsel)
      (_EndUndo acdoc)
    )
  )
  (princ)
)
Thanks Lee I will have them test...

FTF, the last object has an insertion point....we (they) just want to copy that object and use it's ins point to copy it to a buch of other locations from the paired list.
We are not attempting to create the points list, we already have that.
Be your Best


Michael Farrell
http://primeservicesglobal.com/

BlackBox

  • King Gator
  • Posts: 3770
Re: Copy Last object to list of coordinates in CSV file
« Reply #8 on: July 15, 2011, 02:13:52 PM »
Quote
But if the points saved in the file aren't associated with the object what's the point?
we are making NEW objects at the point location(s)...otherwsie there would be no point, game, set, or match.   :wink:

FTF, the last object has an insertion point....we (they) just want to copy that object and use it's ins point to copy it to a buch of other locations from the paired list.
We are not attempting to create the points list, we already have that.

This corrects my confusion... You're wanting to copy the last object created to each of the list of coordinates.

For some reason, I was (mis-)understanding you to mean you were "extracting" the coordinate(s) from the last object created to be written to the CSV.

SoB - What a Friday!? (Self inflicted face-palm) LoL
"How we think determines what we do, and what we do determines what we get."

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: Copy Last object to list of coordinates in CSV file
« Reply #9 on: July 15, 2011, 02:17:33 PM »
FTF, the last object has an insertion point....we (they) just want to copy that object and use it's ins point to copy it to a buch of other locations from the paired list.

Cool, just select the insertion point when my code prompts for a basepoint.

jbuzbee

  • Swamp Rat
  • Posts: 851
Re: Copy Last object to list of coordinates in CSV file
« Reply #10 on: July 15, 2011, 02:17:56 PM »
I'm obviously missing something very basic here: back to the margaritas . . . carry on.  :wink:
James Buzbee
Windows 8

BlackBox

  • King Gator
  • Posts: 3770
Re: Copy Last object to list of coordinates in CSV file
« Reply #11 on: July 15, 2011, 02:21:42 PM »
I'm obviously missing something very basic here: back to the margaritas . . . carry on.  :wink:

See, and I'm trying to decide between a Vodka Tonic, or a dram of Glenlivet (18yr)... decisions, decisions.  :-D
"How we think determines what we do, and what we do determines what we get."

mjfarrell

  • Seagull
  • Posts: 14444
  • Every Student their own Lesson
Re: Copy Last object to list of coordinates in CSV file
« Reply #12 on: July 15, 2011, 02:47:59 PM »
Will report back when the code as posted is tested by the victim user in question.

Being part Irish the decision is easy; Go with the Whiskey my good man!
Be your Best


Michael Farrell
http://primeservicesglobal.com/

BlackBox

  • King Gator
  • Posts: 3770
Re: Copy Last object to list of coordinates in CSV file
« Reply #13 on: July 15, 2011, 02:52:15 PM »
Being part Irish the decision is easy; Go with the Whiskey my good man!

The Glenlivet it is then, Sláinte!



Edit: Also, I'm glad Lee was able to help you out.
"How we think determines what we do, and what we do determines what we get."

mjfarrell

  • Seagull
  • Posts: 14444
  • Every Student their own Lesson
Re: Copy Last object to list of coordinates in CSV file
« Reply #14 on: July 15, 2011, 02:57:55 PM »
I would be interested in any other solutions as well....as their is usually more than one way to lisp a cat.

Be your Best


Michael Farrell
http://primeservicesglobal.com/