Add AutoLISP to a Custom AutoCAD Command

11 Mar, 2014 By: Andrew G. Roe

Combine AutoLISP commands with a VB.NET plugin.

In my previous article about AutoCAD programming, we learned how to add a custom command to one of AutoCAD’s pull-down menus. The example demonstrated how to create an AutoCAD script file to set some key parameters, then use AutoCAD’s Customize User Interface (CUI) dialog box to assign the custom command to a menu. The custom command was developed in VB.NET as a plugin to create a circle object and some accompanying text in one fell swoop.

To take this a step further, let’s look at how to add AutoLISP commands to our custom command. As a refresher, AutoLISP uses a somewhat cryptic syntax relying heavily on parentheses, but it provides direct interaction with AutoCAD to add or edit entities, retrieve drawing information, and set system variables. You can type an AutoLISP statement in the AutoCAD Command line and see the results directly, or you can develop more complex routines with Visual LISP (VLISP), which provides an integrated development environment (IDE) similar to that of VBA. While you can accomplish most of the same tasks in either AutoLISP or VB.NET, occasionally you may want to employ both platforms, leveraging legacy AutoLISP programs with the latest .NET technology.

Consider the following AutoLISP statements:

     (setq en (entlast))
     (entget en)

The first statement retrieves information about the last entity added to a drawing and assigns it to a variable called en. The second statement uses the entget function to retrieve the en variable and display the entity information on the Command line. We can add these statements to the custom command and retrieve the entity information immediately after creating the circle and text entities.

Review the Script

In the previous tutorial, we first created a script file, which is simply a text file with a sequence of AutoCAD instructions. If you worked through that exercise, you can use the same script file. If not, you can create the file as follows:

1. Using a text editor such as Notepad, type the following lines of text:

     Filedia 0
     Netload "c:\Scripts\MyCircleCommand.dll"

Make sure to include a space at the end of the second line.

2. Save the file as MyCircleScript.scr in a location of your choice. For simplicity, I’ve put it in a folder directly under my root directory called Scripts, as shown below. I’ve also saved the plugin called MyCircleCommand.dll (created in a previous article) in the same location. You can adjust this to fit your needs.

The script file sets the AutoCAD variable called Filedia to 0. This suppresses dialog boxes and directs all user input to the Command line. We’ll set it back to 1, the default value, in our custom command to enable dialog boxes. The second line in the script executes AutoCAD’s Netload command to load the plugin.


Create a New Custom Command

We’ll create a new custom command, slightly different than the one we created previously, to run the plugin and also execute the AutoLISP statements.

1. In AutoCAD, click the CUI icon in the Manage tab of the ribbon to load the Customize User Interface dialog box. (You can also load this dialog box by typing CUI at the Command line and pressing Enter.) The Customize User Interface dialog box is displayed.

2. In the Command List portion of the CUI dialog box, click the Create a new command icon.

3. In the Properties portion of the CUI dialog box, type MyCircleCommand2 in the Name box.

4. In the Macro box, type the following:

     ^C^CCreateMyCircle;Filedia 1;(setq en (entlast));(entget en);

The ^C^C is already present in the Macro box, indicating the beginning of the macro, and is the equivalent of pressing the Escape key twice to clear any active commands. The semicolon (;) is the equivalent of pressing Enter. The custom command sets the Filedia variable back to 1, then executes the two AutoLISP statements.

5. In the Customizations in All Files portion of the CUI dialog box, expand the Menus item to show the various menus.

6. In the Command List portion of the CUI dialog box, click the drop-down list and select Custom Commands. The MyCircleCommand2 should be listed.

7. Drag the MyCircleCommand2 to the Modify menu above.

8. Click OK to close the CUI dialog box.


Run the Script and Custom Command

To run the script and custom command, follow these steps:

1. Click the Run Script icon in the Manage tab of the ribbon. (You can also type Script in the Command line, then press Enter.) The Select Script File dialog box is displayed.

2. Navigate to the previously created file and select it.

3. Click Open to run the script.

4. From the Modify menu, select MyCircleCommand2. The command is executed, creating a circle and text as in the previous article and also displaying information on the Command line about the last entity created.

This example provides a simple demonstration of combining AutoLISP commands with a VB.NET plugin. You could expand on this approach by building a more complex AutoLISP routine, saving it in a separate text file, then loading and executing it from the custom command. You can also load an AutoLISP routine prior to executing the command in several different ways. For more information on this, consult the AutoLISP online help guide entry, “About Auto-Loading and Running AutoLISP Routines.”

As you can see, scripts and custom commands can be combined in various ways to meet your needs. In future articles, I'll continue to explore additional facets of AutoCAD programming. If you would like to suggest a topic, feel free to send me an e-mail.

Add comment

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

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

At your company, who has the most say in CAD-related software purchasing?
CAD manager
CAD users
IT personnel
vice-president/department manager
CEO/company owner
Submit Vote