AutoCAD

AutoLISP: A Civil Designer's Helpmate

18 Mar, 2009

Use this powerful programming tool to query and label GIS object data, convert unusable points, and much more.


AutoLISP can assist civil designers in many ways. If you want to push your software beyond its limits, you should consider customized programming using AutoLISP. With AutoLISP, you can make unusable survey drawings workable in AutoCAD Civil 3D. You can extract elevation data from a plain AutoCAD profile. You can also query and label object data in plain AutoCAD. I've found AutoLISP to be invaluable for doing things that supposedly can't be done with out-of-the-box AutoCAD and Civil 3D.

Make Unusable Survey Drawings Workable in Civil 3D
Nothing is more frustrating than receiving hundreds of unusable points. You select points from a survey drawing for Civil 3D surface creation. At the select points prompt, Civil 3D will respond at the command line, "0 found"! The problem: The existing conditions plan has spot elevation points as custom blocks with attributes (case 1). The custom blocks are totally useless in Civil 3D. Without an existing surface, we can't do any of our proposed grading work.

I found AutoLISP to be a quick solution to this problem. The AutoLISP program I wrote prompts the user to select the blocks, and it will create AutoCAD points at the elevation given by the elevation attribute. Then one just needs to use Civil 3D to create Civil 3D points from the AutoCAD points. An alternate solution is to use attribute extraction to output elevation data as a text file. Then you simply import the point data from the text file to create Civil 3D points.

Sometimes the custom blocks will not have attribute elevations, making a custom program your only option to make those points usable with Civil 3D. There will just be dumb text beside the block (case 2). The blocks usually are at the z-elevation indicated by the dumb text. For these circumstances, I wrote an AutoLISP program to create AutoCAD points at the custom block x, y, and z coordinate as shown below:

; file: pnt-at-block.LSP
; Adds AutoCAD Points from block x,y,z Insertion Point

(alert "I will insert AutoCAD points
at block insertion points
I was written by S. Napolitano of AECOM
Please Type PNT-BLK to get me going")

(defun C:pnt-blk ()
(setq TOTAL 0.)
(setvar "OSMODE" 0)
(print "Please Select blocks")
(setq SSPICK (ssget))
(setq SSAMT (sslength SSPICK))
(setq INDEX 0.)
;
(while (< INDEX SSAMT); Selection Set Loop
(setq ENT-NO (ssname SSPICK INDEX))
(setq DXF (entget ENT-NO))
(setq XYZ (cdr (assoc 10 DXF)))
(command "point" XYZ)
(princ)
(setq INDEX (+ INDEX 1.))
(princ INDEX) (print ENT-NO)
); end while
(princ)
)

The program can be easily enhanced with additional lines of programming to return the user's osnap setting and error handling for a user who selects objects that are not blocks.

The most hopeless-looking spot elevation is when the 'X' that marks the spot location and the elevation label are just a single line of text (case 3). Again, you can populate your drawing with usable points with an AutoLISP program.

Three examples of unusable points for Civil 3D:

 

Extract Elevation Data from a Plain AutoCAD Profile
You are given a ground surface profile and asked for ground elevations at given station intervals. An engineer in your firm wants this data as input into a program for a FEMA report. The profile is a plain AutoCAD drawing, not an LDT or C3D profile. You cannot get an output file of profile stations and elevations from a plain AutoCAD profile, but an AutoLISP program could do it. AutoLISP is the easy solution to obtain an ASCII file of the needed data. The program also divides all y-coordinates by the vertical exaggeration of the profile before writing to an output file.

Query and Label Object Data in Plain AutoCAD
AutoLISP can also be used to work with object data. Most of our civil designers' map work is querying and labeling the object data we receive from municipalities for use in AutoCAD. Some firms may be purchasing Map 3D licenses for just labeling and querying GIS data, when all that is needed is a simple AutoLISP program. (Civil 3D comes with map, but AutoCAD does not). If you do not plan to use the myriad of other features of Map 3D, you should seriously consider the AutoLISP option before making additional software purchases.

First use your AutoCAD Properties palette to see the object data attached to an entity selected. You will find the Object Data Table Name and field names with values for the entity selected as shown in the figure below.

 

You can use this AutoLISP code to get object data for pipe diameter and length:

; Diameter
(setq DIA (ade_odgetfield ENT-NO "Default_SewerLinesAlongIn" "Dia" 0))
Sets DIA variable to Pipe Diameter using table name and field name.
(setq DIASTR (itoa DIA))
Converts integer to string value for AutoCAD text.
(setq DIA1 (strcat DIASTR (CHR 034)))
Appends ASCII character 034, an inch symbol

; Length
(setq LEN (ade_odgetfield ENT-NO "Default_SewerLinesAlongIn" "Length" 0))
(setq LENSTR (rtos LEN 2 0))
(setq LENSTR1 (strcat (strcat (CHR 032) (strcat "L=" LENSTR)) (CHR 039)))

(Prefixes "L=" and appends a foot symbol)

The AutoLISP program also writes the pipe length and diameter along the pipeline direction. It determines which way is up and flips the text 180 degrees if it is upside down. I was not able to do that with Map 3D. It was magical to see hundreds of pipes labeled in seconds with length and diameter typed along each pipe at a proper offset distance. No map program was needed to extract and query object data!

Where to Learn More
If you are considering learning programming for AutoCAD, Cadalyst offers a variety of resources, including the following:

  • “Programming Topics,” a four-part series on programming in AutoCAD by Robert Green. Consult the archives of his CAD Manager’s Newsletter, issue numbers 203-206.

  • Harry’s Code Class archives. This newsletter by programming expert Bill Kramer in Cadalyst to learn more about how programming can help your firm. If you decide not to learn programming, you can still take advantage of AutoLISP with Cadalyst's CAD Tips Library.

Conclusion
AutoLISP is a great assistant for civil designers. AutoLISP will query and label GIS object data, convert unusable points, and much more. It can be a powerful tool to help make you more productive and should not be overlooked by any firm interested in a production helpmate.

Related content: AEC, AutoCAD, Civil Engineering


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 free Tips & Tools Weekly e-newsletter and we'll notify you every time a new video tip is published. All exclusively from Cadalyst!
Follow Lynn on Twitter Follow Lynn on Twitter