Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Help with exporting point locations to text file

25 REPLIES 25
SOLVED
Reply
Message 1 of 26
Cadastrophe1911
4243 Views, 25 Replies

Help with exporting point locations to text file

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

 

 

25 REPLIES 25
Message 2 of 26

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, AIA
Message 3 of 26

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.

Message 4 of 26


@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, AIA
Message 5 of 26

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

Message 6 of 26


@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, AIA
Message 7 of 26

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.

Message 8 of 26


@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, AIA
Message 9 of 26

If you want, you can try this!

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

 

Message 10 of 26
devitg
in reply to: CADaSchtroumpf

Thanks.
Message 11 of 26

 

Hello,

 

Two solutions::

 

Solution 1: If you are a Lisp programmer and need help, look at our FREE resources on TechCenter:

 

This is the link: http:///www.4d-technologies.com/techcenter

 

There are a bunch of routines there under various library headings which you can use to export XYZ data into a text file, manage objects, retrieve information like coordinates etc etc.

 

Look at the LSP and if you need help, send me a message.

 

Solution 2: Alternatively, if you are looking at a ready-made solution, you may try our CADPower CP_IMPEX command

 

http://www.4d-technologies.com/cadpower/manual/export_tools.htm#IMPEX

 

You can try it fully functional for 30 days.

 

Regards
Rakesh

http://rakeshrao.typepad.com
http://www.coordsys.com
http://www.4d-technologies.com

- rakesh.rao@4d-technologies.com
- www.4d-technologies.com / www.coordsys.com
- Blog:EN: http://rakeshrao.typepad.com
- Blog: ES: http://kiranabhat.typepad.com
Message 12 of 26
Cadastrophe1911
in reply to: devitg

File has loaded and is exporting point locations, however, when i open the text file the locations are not the same as then ones in the cad file. Would moving my origin effect this??? Normally what I do is move my origin to the zero corner and then place the points in the drawing??? Also, how do I change the precision of the numbers it exports. Thanks again

Message 13 of 26


@Cadastrophe1911 wrote:

File has loaded and is exporting point locations, however, when i open the text file the locations are not the same as then ones in the cad file. Would moving my origin effect this??? Normally what I do is move my origin to the zero corner and then place the points in the drawing??? Also, how do I change the precision of the numbers it exports. Thanks again


If you're talking about my suggested routine, I would guess that you're in some non-World Coordinate System.  The coordinates in the entity data for each Point (which is what the routine uses) will be in WCS terms, no matter what your current UCS.  But what you'll see as locations if you pick something is displayed in the current UCS.  So yes, you should set the Coordinate System to World, and if necessary, move everything from whatever you want the origin to be in relation to the points, to 0,0.  Alternatively, you can use (trans) to convert them -- write back if you'd rather do that and not change the UCS.

 

The precision of the numbers is controlled by the last number in the (rtos) functions [2 in both of them as I posted it, = 2 decimal places] -- see (rtos) in the AutoLISP Reference [you can also change the type of units to something other than decimal, if you want].  If you want whole numbers only, you can replace those last 2's with 0's [it will round things to the nearest integer value].  It turns out you can't use (itoa) for that -- it won't accept real numbers as input, but all Point location coordinates will be real numbers.

Kent Cooper, AIA
Message 14 of 26

Is there anyway I can make it work off of the UCS location???

Message 15 of 26


@Cadastrophe1911 wrote:

Is there anyway I can make it work off of the UCS location???



Yes.  Replace this:

 

      (rtos (car x) 2 2) "\t" ; X coordinate <--- edit precision

      (rtos (cadr x) 2 2); Y coordinate <--- edit precision

 

with this:

 

      (trans (rtos (car x) 2 2) 0 1) "\t" ; X coordinate <--- edit precision

      (trans (rtos (cadr x) 2 2) 0 1); Y coordinate <--- edit precision
 

The 0 means to translate from World Coordinates; the 1 means to current coordinates.

Kent Cooper, AIA
Message 16 of 26

Command:
Command: _appload drill20.LSP successfully loaded.


Command:
Command:
Command: drill20
; error: bad argument type: 2D/3D point: "29.31"

Message 17 of 26

I am using the move origin command with points set at 0,0   .5000, 2.1875       and 2.1875, .5000

Message 18 of 26

defun C:drill20 (/ ptfile inc ptlist);
 
  (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"
      (trans (rtos (car x) 2 4) 0 1) "\t" ; X coordinate <--- edit precision
      (trans (rtos (cadr x) 2 4) 0 1); Y coordinate <--- edit precision
    ); strcat
    ptfile
  )
)


  (close ptfile)

); end defun

 

Message 19 of 26


@Cadastrophe1911 wrote:

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


I take it that's the format that does what you want [with, of course, the missing-here but requisite opening left parenthesis at the very beginning...].  If so, you could now remove all the " <---- edit ..." commentaries.  And there are other things you could do if you choose, such as have it alert the User if there are no Points in the drawing, and/or exit quietly with a (princ) just before the final closing right parenthesis so it doesn't put 'nil' on the Command: prompt line for inexperienced Users to be confused by.

Kent Cooper, AIA
Message 20 of 26

Why am i getting this error

 

Command:
Command: _appload drill20.LSP successfully loaded.


Command:
Command:
Command: drill20
; error: bad argument type: 2D/3D point: "29.31"

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost