Build a Plugin Command for AutoCAD Civil 3D

10 Jul, 2014 By: Andrew G. Roe

Use Visual Studio.NET to customize your Civil 3D experience.

Since its inception, AutoCAD’s open architecture has helped many users customize the software to fit their needs. In more recent years, Autodesk has introduced numerous vertical products, including AutoCAD Civil 3D, Mechanical, and Electrical, leading many to wonder whether further AutoCAD customization would even be necessary. But in practice, no matter how many discipline-specific tools are introduced by Autodesk, CAD users still hunger for more new options and refinements to existing tools. Fortunately for those users, Autodesk has continued to offer customization options such as AutoLISP and .NET for its vertical products, in addition to standard AutoCAD.

In this article, we’ll look at a programming example that you can use to customize Civil 3D. While Civil 3D is primarily geared toward civil engineers and land surveyors, the example demonstrates some key concepts for customizing other Autodesk products, so it is relevant to users in other fields as well. This example will build on the concepts covered in my earlier article, “Create a Plugin for AutoCAD.”

In short, a plugin is a custom command that can be run directly from the AutoCAD environment. The command in this example will obtain some information about intelligent Civil 3D objects in a drawing — specifically, site and alignment objects. In Civil 3D, a site object is used to group collections of other objects that have something in common, such as parcels, alignments, and feature lines that compose a residential subdivision. An alignment object represents road centerlines, pipe networks, and other construction baselines. Civil 3D provides other ways of obtaining site and alignment information, but a custom command allows you to extract this information and manipulate it for your own purposes, such as for a custom report.

As in the article mentioned above, 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.

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 Civil 3D is installed. Select the following files by clicking on one and Ctrl-clicking on the others, in any order: AcCoreMgd.dll, AcDbMgd.dll, AcMgd.dll, AecBaseMgd.dll, and AeccDbMgd.dll. The first three are for accessing standard AutoCAD information and the last two are specific to Civil 3D. (This example is for Civil 3D 2014; references and file names may vary slightly for other versions.)

9. Click OK to close the Add Reference dialog box.


10. In the Properties window, change the Copy Local property to False for all five of the referenced DLL files.

11. If you are using 64-bit AutoCAD, click the Compile tab on the left and click Advanced Compile Options. Change the Target CPU to x64 and click OK. This indicates to Visual Studio that the other application (Civil 3D) is a 64-bit application.

12. In Solution Explorer, rename the class from Class1 to AlignInfo.

13. Double-click on the AlignInfo.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.ApplicationServices
     Imports Autodesk.AutoCAD.EditorInput
     Imports Autodesk.AutoCAD.Runtime
     Imports Autodesk.AutoCAD.DatabaseServices
     Imports Autodesk.AutoCAD.Geometry
     Imports Autodesk.Civil.DatabaseServices
     Imports Autodesk.Civil.ApplicationServices

     Public Class AlignInfo
         Dim MyCivilDoc As CivilDocument = _ 

         <CommandMethod("AlignInfo")> _
         Public Sub GetCivilObjects()
             Dim MyEditor As Editor = _ 
             Dim MyAlignments As ObjectIdCollection = MyCivilDoc.GetAlignmentIds
             Dim MySites As ObjectIdCollection = MyCivilDoc.GetSiteIds
             MyEditor.WriteMessage("The document has: " & MySites.Count & 
     " sites and " & _ MyAlignments.Count & " alignments.")
         End Sub
     End Class

14. Save your work by clicking File > Save All.

15. Build the application by clicking Build and clicking on your project name. 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. Note that if you perform multiple builds after running the command, you may have to exit AutoCAD to avoid error messages when building the application.

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 command. A CommandMethod attribute defines a custom command named AlignInfo. Within a subroutine called GetCivilObjects, several Dim statements declare variables for the various Civil 3D objects we’ll be accessing. Following the Dim statements is the code that obtains the number of sites and alignments in the drawing and displays it at the Command line using a WriteMessage statement.

Execute the Command

Let’s test the command created in the previous steps. You’ll need to open a Civil 3D drawing containing at least one site object and one alignment object.

1. In Civil 3D, type NetLoad at the Command line, and press Enter.

2. Navigate to the location of the new plugin, and select the DLL file created when the application was built, as shown here. The folder will be located beneath the folder location you specified when you created a new .NET project earlier in this exercise.


3. Click Open to open the DLL file. 4. At the AutoCAD Command line, type AlignInfo. The information is displayed on the Command line.

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 from building a plugin without its assistance, to understand the various steps involved.

This example illustrates some key concepts of using .NET to develop a custom AutoCAD Civil 3D plugin. The finished plugin obtains some basic information about a Civil 3D drawing and, for simplicity, displays it on the Command line. In a real application, however, you’d probably want to perform more tasks with this information, or perhaps create your own objects programmatically. You'd also want to provide error handling for those inevitable situations where users try to make the program do something it was not intended for. And you can probably think of various ways to further customize this example, but it shows you how to get started with creating plugins for a vertical product.

In future articles, I'll continue to explore additional facets of AutoCAD programming, including diving deeper into 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