General Software

AutoLISP Solutions: Export point coordinates

1 Dec, 2002 By: Tony Hotchkiss

Create a comma-delimited text file of polyline and point coordinates.


Pedro Gerstberger e-mailed a request to export x,y,z points from either polylines or AutoCAD point entities. The AutoLISP solution plist.lsp lets you select lightweight polylines, 2D polylines, or point objects, then makes a comma-delimited text file suitable for opening in Microsoft Excel.

plist.lsp lists the points grouped in this order: lightweight polylines, 2D polylines, and point objects. Figure 1 shows an AutoCAD drawing with an assortment of polyline types and some point entities. Figure 2 shows a Microsoft Excel worksheet that displays the x,y,z locations

Figure 1. AutoCAD drawing with polylines and points.
of points selected from the drawing in Figure 1.

I wrote an AutoLISP Solution more than two years ago (Cadalyst, July 2000) that extracted the vertex points from polylines. That program, vertext.lsp, was suitable for early versions of AutoCAD. This month's solution is an update that includes polylines and point entities and uses the more modern approach of object-oriented programming with ActiveX objects, methods, and properties.

How to use PLIST.LSP
Download the AutoLISP file from Cadalyst's CAD Tips site and save it in the AutoCAD support directory. From the AutoCAD tools menu, choose Load Applications, or enter Appload at the AutoCAD Command prompt. In the Load/Unload Applications dialog box, select the plist.lsp file from the support directory where you installed it, then click Close. After you load the program, AutoCAD prompts you to enter PTS to start.

Figure 2. plist.lsp makes a point list you can open in Microsoft Excel.

The program first prompts you to select objects. If you don't include any polylines or points in your selection, the routine prompts you to try again. When you select appropriate entities, you are asked to enter a filename using AutoCAD's regular File dialog box, as shown in figure 3. plist.lsp creates a comma-delimited text file that you can open using Microsoft Excel.

Activex benefits
The program uses ActiveX methods (functions) and therefore works only in AutoCAD 2000 or later. The beauty of using ActiveX objects, properties, and methods in plist.lsp is that you can obtain the x,y,z coordinates of many entities simply by asking for the property "coordinates:"

(VLAX-GET-PROPERTY OBJ
     "COORDINATES")

Figure 3. The File dialog box appears when plist.lsp creates its comma-delimited output file.

I used this function to return the coordinates of all vertex points of both types of polyline and also of the point objects.

The vertex points of lightweight polylines contain only x and y coordinates. Those of 2D polylines and point entities contain all three x,y,z coordinates, as seen in figure 2.

The main function, (vert), calls a (make-filter) function to filter the objects produced by the subsequent function (get-objects), which returns a list of ActiveX objects separated into the categories of lightweight polylines, 2D polylines, and point objects. The (make-list) function processes each of these three categories in turn to generate three separate lists of points.

Arrayed of the dark
Because the lightweight polylines have only two coordinates per point (the x and y values), the arrays that contain them must be grouped in pairs instead of threes. This code fragment shows that an integer n is used with either the value 2 or 3 to deal with the different array lengths:

(repeat (/ (length (vlax-safearray->list ca)) n)
(setq x (vlax-safearray-get-element ca
      (setq j (1+ j))))
(setq y (vlax-safearray-get-element ca
      (setq j (1+ j))))
(if (= n 2)
      (setq xy (list x y))
      (progn
           (setq z (vlax-safearray-get-element ca
           (setq j (1+ j))))
           (setq xy (list x y z))
      ) ;_ end of progn
) ;_ end of if
(setq vlist (append vlist (list xy)))
) ;_ end-of repeat

Here, the counter j increases by either 2 or 3, depending on whether a z coordinate is present.

Go to great lenghts
Similarly, in order to write the coordinates to the text file, a (do-point) function takes into account the different lengths of the point coordinates. The (do-point) function includes this (foreach) loop:

(foreach point vl
(setq x (nth 0 point)
      y (nth 1 point)
) ;_ end of setq
(if (= n 2)
(setq str (strcat (rtos x) "," (rtos y)))
(progn
      (setq z (nth 2 point))
      (setq str (strcat (rtos x) "," (rtos y) "," (rtos z)))
) ;_ end of progn
) ;_ end of if
(write-line str f)
) ;_ end of foreach

 

In this code fragment, the coordinates were previously extracted as real numbers by the ActiveX function (method) VLAX-SAFEARRAY-GET- ELEMENT, which returns one particular element of an array.

Last words
The upside of using ActiveX is that it works faster when manipulating AutoCAD drawing objects, and you don't have to use confusing and arcane DXF codes. The downside for some readers is that the programs do not run in AutoCAD Release 14. To those readers, I can only implore you to get on board with AutoCAD 2000 or later.

As usual, good programming, and keep those requests coming in.

cartoon by Roger Penwill

 


About the Author: Tony Hotchkiss


AutoCAD Tips!

Lynn Allen

Autodesk Technical Evangelist Lynn Allen guides you through a different AutoCAD feature in every edition of her popular "Circles and Lines" tutorial series. For even more AutoCAD how-to, check out Lynn's quick tips in the Cadalyst Video Gallery. Subscribe to Cadalyst's Tips & Tricks Tuesdays free e-newsletter and we'll notify you every time a new video tip is available. All exclusively from Cadalyst!
Follow Lynn on Twitter Follow Lynn on Twitter


Latest News from Cadalyst Partners
Feed
AutoCAD 2013 Service Packs   17 May, 2013

Service Pack 2 for 2013 full AutoCAD and the LT version have been released. The links are below: AutoCAD 2013 SP2:...More>>Read more Without a Net blog posts>>

Feed
Teaching Old Designs New Tricks   17 May, 2013

Question: What do the QWERTY keyboard, the railroad, and shoelaces have in common? Answer: Their inventors are long gone. The QWERTY keyboard was...More>>Read more PTC Creo blog posts>>

Feed
Ideate - Ideate BIMLink for Revit 2014 now available   17 May, 2013

An update for my favorite Revit tool and most favorite Autodesk reseller. I miss you Bob! ...More>>Read more BIMbuilder blog posts>>

Feed
Discover and Fix Your Vibration Vulnerability with SolidWorks Simulation   17 May, 2013

When I go biking during the summer, I rely on the frame of my bicycle to withstand stresses such as vibration and impact. As I speed down the trail,...More>>Read more SolidWorks Blog posts>>

Feed
Behind the Design: How Words and Sketches Become a Picture Book   18 May, 2013

I bet, next to those dusty engineering and design textbooks on the shelf, you have a few picture books that have led to endless inspiration since you...More>>Read more SolidSmack blog posts>>

Poll
What type of input device (besides the keyboard) do you use at your primary CAD workstation?
Standard mouse
Trackball mouse
Programmable mouse
3D navigation device
Pen/tablet
Multiple devices
Submit Vote




Considering Wide Format Printing Solutions?
Four FREE White Papers Available:






Subscribe Cadalyst Newsletters