Visual LISP, AutoLISP and General Customization

Visual LISP, AutoLISP and General Customization

Reply
Contributor
Cadastrophe1911
Posts: 13
Registered: ‎06-08-2012
Message 1 of 25 (1,026 Views)
Accepted Solution

Help with exporting point locations to text file

1026 Views, 24 Replies
06-08-2012 11:10 AM

Hi all... Im trying to come up with a lisp that will export x and y coords to a txt or cvs file. However, I need the lisp to export in this format

 

Units       Inch

WPT      1     0     0     ( X=0 Y=0)

WPT      2     1     1     (X=1  Y=1)

WPT      3     2     2

 

 

I need to be able to set my zero with move origin and the export the points.

 

This is for a post for a CNC Hole Popper

 

 


Cadastrophe1911 wrote:

Yes, I want to be able to put a point in a circle and have it list the point locations in that format. I can either copy it to a text file or have it put in a text file or cvs file


Minimally tested, but this seems to do that:

 

(setq
  ptfile (open "C:/TEMP/PTFILE.TXT" "w"); <---- edit file path & name
  inc 0
  ptlist
    (mapcar
      '(lambda (x) (cdr (assoc 10 (entget x)))); Point locations
      (mapcar 'cadr (ssnamex (ssget "_X" '((0 . "POINT"))))); all Point entities in drawing
    ); mapcar & ptlist
); setq
(write-line "Units\tInch" ptfile)
(foreach x ptlist
  (write-line
    (strcat
      "WPT\t"
      (itoa (setq inc (1+ inc))) "\t"
      (rtos (car x) 2 2) "\t" ; X coordinate <--- edit precision, or use (itoa) instead
      (rtos (cadr x) 2 2); Y coordinate <--- edit precision, or use (itoa) instead
    ); strcat
    ptfile
  )
)
(close ptfile)

 

With this result from a group of Point entities I placed in a drawing [tab-delimited in the .txt file, even though it doesn't come through that way in pasting it in here]:

 

Units Inch
WPT 1 -0.33 0.12
WPT 2 -0.01 0.27
WPT 3 0.36 0.07
WPT 4 0.30 -0.24
WPT 5 -0.24 -0.23

 

EDIT:  If the only Circles in the drawing are ones you want to put Points in, and if you want to put Points in all Circles in the drawing, you could do the same without the middle-men of the Point entities, if you don't also need them for some other reason.  Just replace "POINT" above with "CIRCLE" -- a Circle's center is the same (assoc 10) designation as a Point's location.


Cadastrophe1911 wrote:

(defun C:drill20 (/ ptfile inc ptlist);
....

    (mapcar
      '(lambda (x) (cdr (assoc 10 (entget x)))); Point locations
....

      (rtos (car (trans x 0 1)) 2 4) "\t" ; X coordinate
      (rtos (cadr (trans x 0 1)) 2 4); Y coordinate
....

(princ))

  (close ptfile)

); end defun 

 

That works.. do i have my PRINC in the right place and format???? it still gives the nil


The (princ) should be the last thing before the final closing right parenthesis.

 

It also occurred to me that it could be streamlined just slightly by translating each point's location from World to Current CS only once, instead of separately for each of the X & Y coordinates.  Try this:
 

(defun C:drill20 (/ ptfile inc ptlist);
  (setq
    ptfile (open "C:/TEMP/PTFILE.TXT" "w")
    inc 0
    ptlist
    (mapcar
      '(lambda (x) (trans (cdr (assoc 10 (entget x))) 0 1)); locations translated to current CS
      (mapcar 'cadr (ssnamex (ssget "_X" '((0 . "POINT"))))); all Point entities in drawing
    ); end mapcar & ptlist
  ); end setq
  (write-line "Units\tInch" ptfile)
  (foreach x ptlist
    (write-line
      (strcat
        "WPT\t" ; WPT + tab
        (itoa (setq inc (1+ inc))) "\t" ; sequence number + tab
        (rtos (car x) 2 4) "\t" ; X coordinate + tab
        (rtos (cadr x) 2 4); Y coordinate
      ); end strcat
      ptfile
    ); end write-line
  ); end foreach

  (close ptfile)

  (princ)

); end defun

*Expert Elite*
Kent1Cooper
Posts: 5,606
Registered: ‎09-13-2004
Message 2 of 25 (1,023 Views)

Re: Help with exporting point locations to text file

06-08-2012 12:04 PM in reply to: Cadastrophe1911

Doesn't look too difficult, with a series of (write-line) functions including (strcat) functions in them.  But first:

 

In what form are the points whose coordinates you want to export?  A list of point lists?  If so, do they include Z coordinates?  A selection set of Point entities?  A series of User-selected locations?  Another text file?  Something else?

Kent Cooper
Contributor
Cadastrophe1911
Posts: 13
Registered: ‎06-08-2012
Message 3 of 25 (1,018 Views)

Re: Help with exporting point locations to text file

06-08-2012 12:16 PM in reply to: Kent1Cooper

a list of point locations that do not include a z dim. each point will be a work point or WPT. The points will also be named as in WPT    1. I need the Lisp to count the points and give them each a corrisponding number. They have to be seperatied by tabs also. I would like to be able to enter a drawing and put points in the hole locations i need selected, then be able to export those point locations in the above format.

*Expert Elite*
Kent1Cooper
Posts: 5,606
Registered: ‎09-13-2004
Message 4 of 25 (1,011 Views)

Re: Help with exporting point locations to text file

06-08-2012 12:54 PM in reply to: Cadastrophe1911

Cadastrophe1911 wrote:

a list of point locations that do not include a z dim. each point will be a work point or WPT. The points will also be named as in WPT    1. I need the Lisp to count the points and give them each a corrisponding number. They have to be seperatied by tabs also. I would like to be able to enter a drawing and put points in the hole locations i need selected, then be able to export those point locations in the above format.


Partial solution:

 

Given a list that looks like this:

 

(setq ptlist '((0 0) (1 1) (2 2)))

 

this seems to have the desired result in the format of the external text file:

 

(setq
  ptfile (open "C:/TEMP/PTFILE.TXT" "W")
  inc 0
)
(write-line "Units\tInch" ptfile)
(foreach x ptlist
  (write-line
    (strcat "WPT\t" (itoa (setq inc (1+ inc))) "\t" (itoa (car x)) "\t" (itoa (last x)))
    ptfile
  )
)
(close ptfile)

 

That's assuming the X & Y values are all integers as in your original sample, but (rtos) could be used instead of (itoa) if they need to be real numbers.

 

Are you talking about putting Point entities in the hole locations, or Blocks, or something else?

Kent Cooper
Contributor
Cadastrophe1911
Posts: 13
Registered: ‎06-08-2012
Message 5 of 25 (1,009 Views)

Re: Help with exporting point locations to text file

06-08-2012 01:11 PM in reply to: Kent1Cooper

Yes, I want to be able to put a point in a circle and have it list the point locations in that format. I can either copy it to a text file or have it put in a text file or cvs file

*Expert Elite*
Kent1Cooper
Posts: 5,606
Registered: ‎09-13-2004
Message 6 of 25 (1,002 Views)

Re: Help with exporting point locations to text file

06-08-2012 01:41 PM in reply to: Cadastrophe1911

Cadastrophe1911 wrote:

Yes, I want to be able to put a point in a circle and have it list the point locations in that format. I can either copy it to a text file or have it put in a text file or cvs file


Minimally tested, but this seems to do that:

 

(setq
  ptfile (open "C:/TEMP/PTFILE.TXT" "w"); <---- edit file path & name
  inc 0
  ptlist
    (mapcar
      '(lambda (x) (cdr (assoc 10 (entget x)))); Point locations
      (mapcar 'cadr (ssnamex (ssget "_X" '((0 . "POINT"))))); all Point entities in drawing
    ); mapcar & ptlist
); setq
(write-line "Units\tInch" ptfile)
(foreach x ptlist
  (write-line
    (strcat
      "WPT\t"
      (itoa (setq inc (1+ inc))) "\t"
      (rtos (car x) 2 2) "\t" ; X coordinate <--- edit precision, or use (itoa) instead
      (rtos (cadr x) 2 2); Y coordinate <--- edit precision, or use (itoa) instead
    ); strcat
    ptfile
  )
)
(close ptfile)

 

With this result from a group of Point entities I placed in a drawing [tab-delimited in the .txt file, even though it doesn't come through that way in pasting it in here]:

 

Units Inch
WPT 1 -0.33 0.12
WPT 2 -0.01 0.27
WPT 3 0.36 0.07
WPT 4 0.30 -0.24
WPT 5 -0.24 -0.23

 

EDIT:  If the only Circles in the drawing are ones you want to put Points in, and if you want to put Points in all Circles in the drawing, you could do the same without the middle-men of the Point entities, if you don't also need them for some other reason.  Just replace "POINT" above with "CIRCLE" -- a Circle's center is the same (assoc 10) designation as a Point's location.

Kent Cooper
Contributor
Cadastrophe1911
Posts: 13
Registered: ‎06-08-2012
Message 7 of 25 (995 Views)

Re: Help with exporting point locations to text file

06-08-2012 02:13 PM in reply to: Kent1Cooper

Tried to load it and got this

 

Command: _appload drill20.LSP successfully loaded.


Command: ; error: bad argument type: lselsetp nil

 

I dont have much experience with lisp files.

*Expert Elite*
Kent1Cooper
Posts: 5,606
Registered: ‎09-13-2004
Message 8 of 25 (977 Views)

Re: Help with exporting point locations to text file

06-09-2012 04:21 AM in reply to: Cadastrophe1911

Cadastrophe1911 wrote:

Tried to load it and got this

 

Command: _appload drill20.LSP successfully loaded.


Command: ; error: bad argument type: lselsetp nil

....


That looks as though it found no Points.  They would need to be actual Point entities [drawn with the Point command, or perhaps Measure or Divide], not Blocks or something.  Or Circle entities if you switched to that.  If they're actually Blocks or some other kind of thing, the (ssget) filter list would need to be different.

 

The .lsp file should contain something like:

 

(defun C:drill20 (/ ptfile inc ptlist); <--- or whatever command name

  (setq
  ....the stuff in between from above....

  (close ptfile)

); end defun

 

[And it can have other stuff, like a (princ) line just before the end.]

 

If you post the .lsp file itself as an attachment, someone can check whether the "wrapper" aspects surrounding the code I posted are correct.  If the error message you posted occurs simply upon loading it, as it appears from what you posted, without typing in a command name, then there could be something about it that's triggering, for instance, the recall of the previous command, and that might be where the error is.

Kent Cooper
Valued Mentor
CADaStroumph
Posts: 334
Registered: ‎10-15-2008
Message 9 of 25 (966 Views)

Re: Help with exporting point locations to text file

06-09-2012 02:04 PM in reply to: Cadastrophe1911

If you want, you can try this!

It is a generic extraction for coordinates of many objects to CSV

 

Mentor
devitg
Posts: 1,704
Registered: ‎03-14-2004
Message 10 of 25 (960 Views)

Re: Help with exporting point locations to text file

06-09-2012 07:33 PM in reply to: CADaStroumph
Thanks.
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.