Simple drawing parser using AutoLISP

  • Coinciding with the acquisition of VIA Development, Nate joined Autodesk in March of 2003 after a decade stint as an entrepreneur following a two-decade stint as a controls engineer and software applications developer at Owens-Corning. Nate is now the lead product architect for AutoCAD Electrical. He loves this stuff.

    About Nate

Latest Post

  • Simple drawing parser using AutoLISP
    March 5, 2008, 11:42 PM Nate Holt

    This 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.

     

    0 Comment | Add Comment Controlling the Machine >

Comments



You must be logged in to post a comment.

Subscribe to Blog

Want to keep up with the latest? Subscribe to the RSS feed today.

RSS

Login

Register now to access tips, discussions and more.
Forgot your password ?

Tags

You must be logged in to add a tag.

Send to a Peer

You must login to share pages.

Feedback

Tell us what you think of the site.

Send Feedback