Author Topic: Copy Last object to list of coordinates in CSV file  (Read 10045 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/

mjfarrell

  • Seagull
  • Posts: 14444
  • Every Student their own Lesson
Re: Copy Last object to list of coordinates in CSV file
« Reply #15 on: July 15, 2011, 04:04:54 PM »
Lee, the user reports the following:


** Error: no function definition: VLAX-GET-ACAD-OBJECT **


I am not sure if the error is in the code, or that they did not select the file with the coordinate pairs in it; or both.
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 #16 on: July 15, 2011, 04:05:56 PM »
Add:

Code: [Select]
(vl-load-com)

Edit: FWIW - Most users add this to acaddoc.lsp, so it is not needed for each routine which uses Visual LISP (ActiveX).
"How we think determines what we do, and what we do determines what we get."

jbuzbee

  • Swamp Rat
  • Posts: 851
Re: Copy Last object to list of coordinates in CSV file
« Reply #17 on: July 15, 2011, 04:20:54 PM »
Quote
See, and I'm trying to decide between a Vodka Tonic, or a dram of Glenlivet (18yr)... decisions, decisions.

Well I'm in Florida and it's HOT, so Margaritas are the obvious choice. 

Oh wait, I could muddle some fresh mint and make Mojitos next!  Course after that I probably should post anymore.

 :wink:
James Buzbee
Windows 8

BlackBox

  • King Gator
  • Posts: 3770
Re: Copy Last object to list of coordinates in CSV file
« Reply #18 on: July 15, 2011, 04:24:46 PM »
Quote
See, and I'm trying to decide between a Vodka Tonic, or a dram of Glenlivet (18yr)... decisions, decisions.

Well I'm in Florida and it's HOT, so Margaritas are the obvious choice. 

Oh wait, I could muddle some fresh mint and make Mojitos next!  Course after that I probably should post anymore.

 :wink:

Tell me about it!

Not sure where you are, but here in SW Florida it's been 90+ since about 10:00 AM... I just keep my AC at 72 and below LoL  ;-)

I also enjoy margaritas, and mojitos alike... especially when we hang out by the pool all day (weekends).
"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 #19 on: July 15, 2011, 05:07:21 PM »
mmmmmmmmmm mo mo mo Mojito pro favor!

although it's so hot in AZ one dare not drink until well after the sun goes down.
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 #20 on: July 15, 2011, 05:31:42 PM »
Did your victim user ever get back to you after the (vl-load-com) business, or is it looking more like Monday to find 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 #21 on: July 15, 2011, 06:28:17 PM »
my guess is after he got loaded it all went to plan
Be your Best


Michael Farrell
http://primeservicesglobal.com/

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: Copy Last object to list of coordinates in CSV file
« Reply #22 on: July 16, 2011, 07:45:17 AM »
Lee, the user reports the following:

** Error: no function definition: VLAX-GET-ACAD-OBJECT **

I am not sure if the error is in the code, or that they did not select the file with the coordinate pairs in it; or both.

I sometimes forget the (vl-load-com) because I too include it in my ACADDOC.lsp, so sometimes don't notice when its missing...I shall update the earlier code to save anyone else the confusion.

Edit: FWIW - Most users add this to acaddoc.lsp, so it is not needed for each routine which uses Visual LISP (ActiveX).

mjfarrell

  • Seagull
  • Posts: 14444
  • Every Student their own Lesson
Re: Copy Last object to list of coordinates in CSV file
« Reply #23 on: July 16, 2011, 10:22:59 AM »

I sometimes forget the (vl-load-com) because I too include it in my ACADDOC.lsp, so sometimes don't notice when its missing...I shall update the earlier code to save anyone else the confusion.

Edit: FWIW - Most users add this to acaddoc.lsp, so it is not needed for each routine which uses Visual LISP (ActiveX).
would this 'error' not be similar in nature to the 'nils' error I was puzzling over?
Be your Best


Michael Farrell
http://primeservicesglobal.com/

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: Copy Last object to list of coordinates in CSV file
« Reply #24 on: July 16, 2011, 10:46:08 AM »
would this 'error' not be similar in nature to the 'nils' error I was puzzling over?

nils error? Are you referring to the Lynn Allen thread?

If so, then no.
Since (vl-load-com) only needs to be called once per drawing session to load the Visual LISP extensions, it makes sense to include it in the ACADDOC.lsp and not in every program (in which every subsequent call to the function is redundant). However, this more efficient approach may confuse beginners who don't include it in the ACADDOC.lsp or have not experienced errors similar to that described above.

The 'nils' returned by a program is not an error, but merely bad practice when writing a program called from the command line. Note that some functions may intentionally return nil to serve a particular purpose.

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
Re: Copy Last object to list of coordinates in CSV file
« Reply #25 on: July 16, 2011, 11:51:12 AM »
Since (vl-load-com) only needs to be called once per drawing session to load the Visual LISP extensions, it makes sense to include it in the ACADDOC.lsp and not in every program (in which every subsequent call to the function is redundant).
This is not quite correct, Lee. (vl-load-com) need be called only once per Autocad session, so it really only needs to be placed in acad.lsp which is normally only called when Acad starts (unless ACADLSPASDOC is set to true when it, too, loads with every drawing)

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: Copy Last object to list of coordinates in CSV file
« Reply #26 on: July 16, 2011, 01:09:00 PM »
Since (vl-load-com) only needs to be called once per drawing session to load the Visual LISP extensions, it makes sense to include it in the ACADDOC.lsp and not in every program (in which every subsequent call to the function is redundant).
This is not quite correct, Lee. (vl-load-com) need be called only once per Autocad session, so it really only needs to be placed in acad.lsp which is normally only called when Acad starts (unless ACADLSPASDOC is set to true when it, too, loads with every drawing)

Thanks for the clarification Jeff  :-)

mjfarrell

  • Seagull
  • Posts: 14444
  • Every Student their own Lesson
Re: Copy Last object to list of coordinates in CSV file
« Reply #27 on: July 18, 2011, 08:15:07 AM »
To all involved; thanks for the quick reply, the effective code, and the clear explanation of concepts involved.

TheSwamp crew is the greatest thing on the Internet!
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 #28 on: July 18, 2011, 09:42:15 AM »
Since (vl-load-com) only needs to be called once per drawing session to load the Visual LISP extensions, it makes sense to include it in the ACADDOC.lsp and not in every program (in which every subsequent call to the function is redundant).
This is not quite correct, Lee. (vl-load-com) need be called only once per Autocad session, so it really only needs to be placed in acad.lsp which is normally only called when Acad starts (unless ACADLSPASDOC is set to true when it, too, loads with every drawing)

Thanks for the clarification Jeff  :-)

1+ ... I learn something new everyday.
"How we think determines what we do, and what we do determines what we get."

jbuzbee

  • Swamp Rat
  • Posts: 851
Re: Copy Last object to list of coordinates in CSV file
« Reply #29 on: July 18, 2011, 10:15:20 AM »
Wait, what?  It's Monday already?

What happened to the weekend - oh, never mind.
James Buzbee
Windows 8