Program AutoCAD-Based Products Using AutoLISP18 Mar, 2015 By: Andrew G. Roe
Customize AutoCAD Electrical and other software products that lack a .NET API.
In previous articles in this programming series, we have explored how to customize AutoCAD-based products using Visual Studio .NET, AutoLISP, and other techniques. While .NET is often the first choice for experienced programmers, some AutoCAD-based products do not include a .NET application programming interface (API), so another approach is needed. The article addressing Raster Design demonstrated 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, an Autodesk vertical product geared for circuit and wiring layout, schematic design, and other electrical tasks, was largely developed in AutoLISP, so using AutoLISP for customization is a natural fit. If you are not an Electrical user, most of the concepts still apply in customizing AutoCAD-based products.
One of the more common AutoCAD Electrical tasks is inserting wires into a drawing. Electrical provides several options for inserting and labeling wires, but perhaps you need to provide additional customized labeling. For example, you may want to track the sequence in which a wire was drawn and indicate the last point of a wire. You can do this by executing the standard Wire command in an AutoLISP routine, then extracting some data from the last wire segment, and inserting some text to label it. This example will show how to consecutively execute AutoCAD Electrical commands and standard AutoCAD commands in the same routine.
As a refresher, 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, type 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 together. After you press Enter, you should see the answer displayed on the Command line:
For multiple-line routines, however, you’ll want to fire up the Visual LISP IDE, where you can develop routines more efficiently. This example will make use of both the IDE and the Command line to build and run the routine.
Build 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 in a location of your choice. The file is saved with a LSP extension, but it is a simple text file editable by any word processing software.
5. From 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.
About the Author: Andrew G. Roe
Create Professional 3D Models Anywhere - Shapr3D is an industrial-strength CAD tool that runs natively on your iPad. Import and Export files from most major desktop applications. Find out more and download a free copy here!
Running AutoCAD? Test Your Hardware! Designed to test and compare the performance of systems running AutoCAD, Cadalyst's Benchmark Test is a popular and long-time favorite.