-
Controlling the Machine is no longer being updated. Don't worry, though, you can still follow Nate Holt at his new blog, AutoCAD Electrical Etcetera. You'll find it at http://nateholt.wordpress.com. Or you can subscribe to his feed to get latest words of wisdom automatically: http://nateholt.wordpress.com/feed/. You also can continue to view the Controlling Machine archives for Nate's AutoCAD Electrical tips and tricks.
-
Simple drawing parser using AutoLISP
March 5, 2008, 11:42 PM Nate HoltThis illustrates that 'simple' can also be very useful. A friend calls needing a quick way to list all layers defined in a drawing and write this information out to a spreadsheet. For this example, there is an added twist - the entity count on each layer is to be included in a parallel column in the spreadsheet.
A quick AutoLisp utility is one way to tackle this. There are two main functions that can be used to make this happen. The first is a call to function "tblnext". This allows a program to cycle through the "LAYER" table for the active drawing and pull out all of the defined layer names. The second is a call to 'ssget'. This gives a program the ability to cast a window around the whole drawing and capture all entities filtered by some criteria... in our case, filtered on the layers that entities are inserted on.
Here is the little utility:

How it works
The first step is to begin at the front end of the "LAYER" table for the active drawing and then loop through this table and collect a list of all the layer names defined in the drawing. This is done by the 'while' loop near the beginning of the program. Data for each layer record pulled from the table ends up in variable 'x'. The layer's name is extracted from this data and pushed on to the 'laylst' layer name list.
When no more layer records, the while-loop exits. The list of layer names is sorted.
Next, user is prompted to select a text file name to write this data out to. It will be written in comma-delimited 'csv' format instead of native Excel format. The csv format file is easy to construct and easily read in by Excel.
Finally, the program hits the 'foreach' loop. For each layer name in the sorted list, the program creates a selection set of all entities in the active drawing that appear on this specific layer. The size of the selection set is determined (i.e. number of entities on the target layer). The layer name and the entity count are formatted into a line of text and output to the target output file. Program loops until all layer names have been processed and output to the csv file.
Program closes the csv file and exits.
To run:
1. Appload the above file (type it up using Visual Lisp or any ascii text editor - give it a ".lsp" file extension)
2. Type LAYER_LIST [Enter] at command line. Enter a file name when prompted.
3. Start Excel. Read in the file name from step #2.
Here's an example Excel spreadsheet display of data extracted by the above utility.

You must be logged in to post a comment.