AutoCAD

Create a Plugin for AutoCAD

20 Jun, 2013 By: Andrew G. Roe

Expand your customization skills as you program a command using Visual Studio.NET.


In my previous articles about AutoCAD programming, we learned how to use the .NET programming environment to extract drawing information, create and modify AutoCAD entities, and modify entities programmatically. We primarily focused on running applications from the .NET environment, accessing AutoCAD with component object model (COM) interoperability. In a nutshell, this means we were using both .NET and COM technologies to manipulate AutoCAD in ways similar to those of the built-in VBA environment of years past.

In this article, we will create an AutoCAD plugin, a custom command that can be run directly from the AutoCAD environment. We will focus on .NET techniques, introducing a new concept called Transactions to create AutoCAD entities programmatically. Specifically, we will develop a custom command that creates a circle object and some accompanying text in one fell swoop.

As before, this example uses the Visual Basic.NET programming environment, although you can use Visual C#.NET instead. To complete this exercise, you'll need either Visual Studio.NET or the free Visual Studio Express. (If you're using AutoCAD 2013, you should also download the ObjectARX software development kit [SDK] to run this example. Even though it's not an ObjectARX example, the necessary references are located in the SDK for 2013.)

Create a Plugin

  1. Start Visual Studio and create a new project by clicking File > New > Project.
  2. In the New Project dialog box, select Class Library, then type a name and file location at the bottom of the box, as shown here.

  3. Click OK to close the New Project dialog box.
  4. Click View > Solution Explorer to display the Solution Explorer.
  5. Double-click on My Project in the Solution Explorer; the Project Properties screen will appear.
  6. Click the References tab on the left.
  7. Click the Add button to display the Add Reference dialog box.
  8. In the Add Reference dialog box, click the Browse tab, and navigate to where your AutoCAD DLL files are located. If you downloaded the ObjectARX SDK, they are likely located in the ObjectARX 2013\inc-win32 or ObjectARX 2013\inc-win64 folder, depending on which version of AutoCAD you are using. Select the AcCoreMgd.dll, AcDbMgd.dll, and AcMgd.dll files by clicking on one and Ctrl-clicking on the others. (Note: This example is for AutoCAD 2013. If you are using another version, you can download the ObjectARX SDK for that version and reference the DLL files listed earlier.)
  9. Click OK to close the Add Reference dialog box.
  10. In the Properties window, change the Copy Local properties of the three DLL files to False.
  11. In Solution Explorer, rename the class from Class1 to CreateMyCircle.
  12. Double-click on the CreateMyCircle.vb file, and modify the code to read as shown below. (Hint: You can copy and paste the code instead of retyping it!)

       Imports Autodesk.AutoCAD.Runtime
       Imports Autodesk.AutoCAD.Geometry
       Imports Autodesk.AutoCAD.DatabaseServices
       Imports Autodesk.AutoCAD.ApplicationServices

       Public Class CreateEntities

          Private Shared myroutine As Action

          <CommandMethod("CreateMyCircle")>
          Public Sub CreateEntities()
             Dim acDoc As Document =
                Application.DocumentManager.MdiActiveDocument
             Dim acDb As Database = acDoc.Database
             Dim acBlkTbl As BlockTable
             Dim acBlkTblRec As BlockTableRecord
             Dim acCirc As Circle = New Circle()
             Dim acText As MText = New MText()

             ' Start a transaction
             Using acTrans As Transaction =
                   acDb.TransactionManager.StartTransaction()
                acBlkTbl = acTrans.GetObject(acDb.BlockTableId,
                   OpenMode.ForRead)
                acBlkTblRec =
                   acTrans.GetObject
                   (acBlkTbl(BlockTableRecord.ModelSpace), _
                   OpenMode.ForWrite)

                ' Create a circle with a center point located at
                   0,2,0 and a radius of 4.0.
                acCirc.SetDatabaseDefaults()
                acCirc.Center = New Point3d(0, 2, 0)
                acCirc.Radius = 4.0
                acText.Location = acCirc.Center
                acText.TextHeight = 1
                acText.Contents = "Text starts at center point."
                acBlkTblRec.AppendEntity(acCirc)
                acTrans.AddNewlyCreatedDBObject(acCirc, True)
                acBlkTblRec.AppendEntity(acText)
                acTrans.AddNewlyCreatedDBObject(acText, True)
                acTrans.Commit()
             End Using
          End Sub

       End Class
     
  13. Save your work by clicking File > Save All.
  14. Build the application by clicking Build > Build MyCircleCommand. If you receive any error messages, Visual Studio provides guidance in the Output window about where the errors occurred and what the fix might be. The figure below shows the results of a typographical error in the Imports statement.

 

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.

  1. In AutoCAD, type NetLoad at the Command line, and press Enter.
  2. 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.


     
  3. Click Open to open the DLL file.
  4. 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.


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


Poll
Which file format do you use most often for CAD drawing/model exchange?
Native format
PDF
3D PDF
DWF
STEP or IGES
JT
IFC
Other
Submit Vote