Create a Plugin for AutoCAD20 Jun, 2013 By: Andrew G. Roe
Expand your customization skills as you program a command using Visual Studio.NET.
Let’s take a closer look at the code. With references properly set, the Imports statements are used to specify which libraries, or namespaces, will be accessed by the application. A CommandMethod attribute defines a custom command named CreateMyCircle. Within a subroutine called CreateEntities, several Dim statements declare variables for the AutoCAD Document object, as well as for the various AutoCAD objects we’ll be accessing.
Following the Dim statements is the transaction: the code that creates a circle with a fixed radius and some text that begins at the circle center point. A block table is opened for reading from the table, then for writing to the table. The circle is created with an object named acCirc, while the text is created under the guise of acText. The new objects are then added to the block table record and the transaction, and finally, saved to the database using the Commit method.
Execute the Command
Let’s test the command we created in the previous steps.
- In AutoCAD, type NetLoad at the Command line, and press Enter.
Navigate to the location of the new plugin, and select the file called MyCircleCommand.dll, as shown here. The folder will be located beneath the folder location you specified when creating a new .NET project earlier in this exercise.
- Click Open to open the DLL file.
At the AutoCAD command line, type CreateMyCircle. A circle and accompanying text is created.
To aid in plugin development, Autodesk provides a plugin wizard, which is described in an online training document, "My First Plug-in Training." Although the wizard is a handy tool, beginning programmers will benefit by building a plugin without its assistance, to understand the various steps used to build a plugin.
This example illustrates some key concepts of using .NET to develop a custom AutoCAD plugin. For simplicity, it creates a circle with fixed dimensions and some text with hard-coded characters. In a real application, you’d probably want to build in flexibility for the user to create objects of variable dimension and content. You'd also want to provide error handling for those inevitable situations where users try to make the program do something unintended. And you can probably think of various ways to further customize this example, but this shows you how to get started creating plugins.
In future articles, I'll continue to explore additional facets of AutoCAD programming, including creating more advanced plugins and working with Autodesk’s vertical products. If you would like to suggest a topic, feel free to send me an e-mail.
About the Author: Andrew G. Roe
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!