Add Your Plugin to an AutoCAD Menu

26 Sep, 2013 By: Andrew G. Roe

Load and run a plugin the quick way — without typing.

In my previous article about AutoCAD programming, we learned how to use the .NET programming environment to create an AutoCAD plugin — a custom command that can be run directly from the AutoCAD environment. The plugin included a custom command that created a circle object and some accompanying text simultaneously. We used AutoCAD’s NetLoad command to load the plugin, and we typed the custom command name (CreateMyCircle) at the Command line to run it.

Although that approach served the purpose of demonstrating plugins, in reality most AutoCAD users would rather not expend time and effort typing in the Command line. Let’s look at how we can use some of AutoCAD’s built-in customization tools to add our plugin to one of AutoCAD’s pull-down menus. We will create an AutoCAD script file to set some key parameters for our custom command, and we will use AutoCAD’s Customize User Interface (CUI) dialog box to assign our custom command to a menu.

Create a Script

In AutoCAD, a script is simply a text file containing a sequence of AutoCAD instructions. We can create a text file as follows:

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

     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 the previous article) in the same location. You can choose a different save location to fit your needs if, for example, you have other macros saved in a specific folder.

(Note: You may need to adjust some settings to see file extensions in Windows. If you do not see the SCR file extension in Windows 7, go to Control Panel, select Appearance and Personalization > Folder Options and click the View tab. Uncheck the item called Hide extensions for known file types and click OK.)

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 Custom Command

Next, we’ll create a custom command to run the plugin.

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

Click image to enlarge.

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 MyCircleCommand in the Name box.

4. In the Macro box, type the following:

     ^C^CCreateMyCircle;Filedia 1;

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 last portion of the custom command sets the Filedia variable back to 1.

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

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

7. Drag the MyCircleCommand 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:

1. Click the Run Script icon in the Manage tab of the ribbon. (You can also type Script in the Command line and 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 MyCircleCommand. The command is executed, creating a circle and text as explained in the previous article.

This example demonstrates one way to decrease typing and further automate use of your plugins. You can use scripts and custom commands in various other combinations to meet your needs. You can also assign custom commands to other portions of the AutoCAD interface, such as the ribbon and toolbars. If you’re so inclined, you can even create keyboard shortcuts, assign macros to mouse buttons, and use custom commands in conjunction with LISP files. AutoCAD’s online help tool describes these options in greater detail. The wide variety of customization choices, available since the early days of AutoCAD, can be coupled with modern technology such as .NET plugins to make AutoCAD more efficient for you.

In future articles, I'll continue to explore additional facets of AutoCAD programming, including working with Autodesk’s vertical products. 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