AutoCAD

Program AutoCAD-Based Products Using AutoLISP

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

1 2 


Add comment

Note: Comments are moderated and will appear live after approval by the site moderator.

AutoCAD Tips!

Lynn Allen

In her easy-to-follow, friendly style, long-time Cadalyst contributing editor and Autodesk Technical Evangelist Lynn Allen guides you through a new feature or time-saving trick in every episode of her popular AutoCAD video tips. Subscribe to the free Cadalyst Video Picks newsletter and we'll notify you every time a new video tip is published. All exclusively from Cadalyst!

Follow Lynn on TwitterFollow Lynn on Twitter


Poll
Which device do you typically use to read Cadalyst.com content?
A desktop computer / tower workstation
A tablet
A smartphone
A laptop or mobile workstation
I regularly use both a desktop computer and a smartphone for this purpose
I regularly use another combination of devices for this purpose
I prefer to print out articles from the website and read them on paper
Submit Vote



Download Cadalyst Magazine Special Edition