Program AutoCAD-Based Products Using AutoLISP19 Mar, 2015 By: Andrew G. Roe
Customize AutoCAD Electrical and other software products that lack a .NET API.
Here’s a brief overview of the code. The first line uses a function called defun to define a new AutoLISP routine, in this case one called “WireEnd.” The “c:” in front of the function name means it can be run directly from the AutoCAD Command line.
The next line, (c:aewire), executes the standard Electrical Wire command (AEWIRE) within the AutoLISP routine. The “c:” is not necessary for executing standard AutoCAD commands from within AutoLISP, but is needed here because AEWIRE is a built-in command added onto AutoCAD. If you don’t have AutoCAD Electrical, you can substitute an AutoCAD command here.
The setq statement sets the value of a variable called en. This variable holds the entity data for a wire drawn by the user. The entget function, coupled with the entlast function, retrieves the data for the last entity drawn; in this case, a wire segment.
Another setq statement identifies the endpoint of this segment. This statement also uses the assoc function, which searches a list for group 11 data, representing the end point. The cdr function returns all but the first element of this list, providing the coordinates of the end point.
The Command function executes a standard AutoCAD command to place some text. In this case, the end point of the last line segment is used to establish the starting point of the text. The text labels the end point of the most recently placed wire segment. More information on AutoLISP functions can be found in Autodesk's online AutoCAD help resource.
Execute the Command
With the application loaded, you can test the routine created in the previous steps.
1. At the AutoCAD Command line, type “WireEnd” and press Enter.
2. Insert a wire when prompted. It can be a simple one-segment wire or a multi-segment wire. Press Enter when you are done inserting the wire.
3. The text is placed at the end of the last wire segment.
This example accomplishes a fairly simple task and uses the Command line to interact with the user. To make the application more user-friendly, you could also create a plugin to build a custom interface and make the command accessible from a menu. And you can probably think of various ways to further customize this example, but it shows you how to use AutoLISP with AutoCAD Electrical.
In future articles, I'll continue to explore additional facets of AutoCAD programming, both with conventional AutoCAD and with other vertical products. If you would like to suggest a topic, feel free to send me an e-mail.