Add AutoLISP to a Custom AutoCAD Command10 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.