Program AutoCAD-based products using AutoLISP



March 18, 2015

By: Andrew G. Roe



Customize AutoCAD Electrical and other software products without .NET APIs.

In previous articles in this programming series, we explored how to customize AutoCAD-based products using Visual Studio .NET, AutoLISP, and other techniques. Although .NET is often the first choice for experienced programmers, some AutoCAD-based products do not include a .NET application programming interface (API). Another approach is therefore necessary. The Raster Design article showed an example where AutoLISP provided the most viable customization solution. AutoCAD Electrical is another application without a .NET API, so again AutoLISP may be your best bet for customization.

AutoCAD Electrical, a vertical product from Autodesk designed for circuit and wiring layout, schematic design, and other electrical tasks, was extensively developed in AutoLISP. Using AutoLISP for customization is therefore a natural solution. If you are not an Electrical user, most of the concepts still apply to customizing AutoCAD-based products.

One of the most common tasks in AutoCAD Electrical is inserting wires into a drawing. Electrical offers several options for inserting and labeling wires, but you may need to provide additional custom labeling. For example, you may want to track the sequence in which a wire was drawn and indicate the last point of a wire. To do this, run the standard Wire command in an AutoLISP routine, then extract data from the last wire segment and insert text to label it. This example shows how to run AutoCAD Electrical commands and standard AutoCAD commands consecutively in the same routine.

As a reminder, you can develop AutoLISP routines in a text editor or in the Visual LISP interactive development environment (IDE) included with AutoCAD. You can also type AutoLISP expressions directly into the AutoCAD command line and see the results immediately. For example, enter the following expression:

     (+ 4 4)

In AutoLISP syntax, this is how you calculate 4+4, with the addition function (+) followed by the numbers you want to add. After pressing Enter, you should see the response displayed on the command line:

For multi-line routines, however, you’ll want to launch the Visual LISP IDE, where you can develop routines more efficiently. This example will use both the IDE and the command line to create and run the routine.

Creating an AutoLISP Routine

1. From the command line, type VLISP to load the Visual LISP IDE, as shown below.

2. Click File > New File to open a code window.
3. Type (or copy and paste) the following code into the Visual LISP code window:

     (defun c:WireEnd ()     
       (c:aewire)       
       (setq en (entget (entlast)))     
       (setq EndPt (cdr(assoc 11 en)))       
       (command "text" EndPt "" "" "Last wire drawn here.")     
     )

4. In the Visual LISP IDE, click File > Save and save the file to a location of your choice. The file is saved with an LSP extension, but it is a simple text file editable by any word processing software.

5. In the AutoCAD command line, type APPLOAD and press Enter.

6. Navigate to the previously saved LSP file and click Load. Click Close to close the Load/Unload Applications dialog box.

1 2