Вы находитесь на странице: 1из 12

TECHNICAL WHITEPAPER

Using Visual Studio Express 2012 to Create


an AutoCAD and Civil 3D 2015 .NET Add-in

Introduction
This document provides a step-by-step tutorial on how to create, load and debug a simple VB .NET application for AutoCAD using the Visual Studio Express development environment. The document focuses on setting up and using the Visual Studio Express environment. We do not elaborate on the AutoCAD .NET API
itself. There are resources noted at the end of this tutorial to help with learning the AutoCAD .NET API.

Creating the Project


1) Launch Visual Studio 2012 Express Edition
2) Select the Menu item File New Project or type Ctrl+N. The New Project dialog will appear:

3) Select the Class Library Template (shown above) and enter a suitable name in the Name: text

field. Here, we chose MyVBAcadApp.

4) A new Project is created and the Visual Studio Express window should look something like this:

Create a AutoCAD VB .NET Application


1.1

Set Target Framework and Compile Options

1) In your Visual Studio 2012 Express window you should see the Solution Explorer tab on the right. If

you dont see it, click the ViewSolution Explorer menu item or type Ctrl+W+S.
2) Go to the Solution Explorer tab on the right side of the Visual Studio 2010 Express window select
the Show All Files button as shown below.

3) Select the project folder MyVBAcadApp, right click and select properties.
4) In the application tab Ensure that .NET Framework 4.5 is the target framework.

US/CANADA
800-356-9050

Page 2 Using Visual Studio Express 2012 to Create an AutoCAD and Civil 3D 2015 .NET Add-in

imaginit.com

5) Select the Compile tab and in the Target CPU select x64 to compile to 64bit

1.2

Adding the Required References

1) Go to the Solution Explorer tab on the right side of the Visual Studio 2012 Express window select
the Show All Files button as shown below.
2) The above step will display the References folder in the Solution Explorer as shown below.

3) Right-click on the References folder and select Add Reference item in the resulting context

Reference Manager menu.

4) Click on the Browse button and Navigate to the location of the AutoCAD-specific Reference files.

Note: You will find AutoCAD-specific reference files in a couple of locations. If you have ObjectARX
SDK installed, you can navigate to the Include folder (I would navigate to C:\ObjectARX\2012\inc-

win32 for 32 bit platforms and C:\ObjectARX\2012\inc-x64 for 64 bit) for the references. In the snap

shot above, we navigated to the inc-win32 folder.

US/CANADA
800-356-9050

Page 3 Using Visual Studio Express 2012 to Create an AutoCAD and Civil 3D 2015 .NET Add-in

imaginit.com

Alternatively, you can navigate to the AutoCAD or Civil 3D Install Folder and find the references
there as shown below:

5) Select the DLL files acmgd.dll, acdbmgd.dll and AcCoreMgd.dll from the location you browse to

and click the ADD button. These files contain all the necessary .NET APIs to create a generic Auto

CAD .NET application

You can also type *mgd.dll to filter for the required assemblies.

Click OK to close the Reference Manager dialog.


6) In the solution explorer window to the right select each of the references and in the properties

window change the Copy Local to False. (If this copy local is true then the dll does not load properly

when debugging).

US/CANADA
800-356-9050

Page 4 Using Visual Studio Express 2012 to Create an AutoCAD and Civil 3D 2015 .NET Add-in

imaginit.com


Select the DLL files acmgd.dll, acdbmgd.dll, AcCoreMgd.dll and AecBaseMdg.dll, AecDBMgd.
dll from the location you browse to and click the OK button. These files contain all the necessary

.NET APIs to create a generic AutoCAD .NET application.

Note: You can select multiple files (two files in this case) by pressing the Ctrl key while selecting
them with the mouse.

7) Now that we have the classes referenced we can import them. Add the necessary Namespaces to

the source files for convenient access to the AutoCAD .NET APIs. Here are the most commonly used

AutoCAD .NET API specific namespaces:
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.DatabaseServices

You can add these namespaces at the top of the Class1.vb source file as shown below.


Note: We will not be elaborating on the purposes of the namespaces and the AutoCAD .NET APIs in

this tutorial. You can find a lot more information on this in the references provided at the end of this
tutorial.

1.3

Creating a Command

Add the command method that implements the command(s) you need. Here is an example of a command method implementing the command we use:
Define command MyVBCommand
<CommandMethod(MyVBCommand)> _
Public Sub Cmd1() This method can have any name

Type your code here

Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
ed.WriteMessage(My First command implemented in VB .NET Express Edition)
End Sub
Note: We will not be elaborating on the creation of commands and the AutoCAD .NET APIs in this
tutorial. You can find a lot more information on this in the references provided at the end of this tutorial.

US/CANADA
800-356-9050

Page 5 Using Visual Studio Express 2012 to Create an AutoCAD and Civil 3D 2015 .NET Add-in

imaginit.com

Here is a snapshot of how the command code would look like:

Now we have a functional AutoCAD VB .NET project!

1.4

Save the Project

You can save the project by clicking the FileSave All menu item or using Ctrl+Shift+S.

1.5

Build the Project

The last step is to Build the application. Use the BuildBuild menu item to build your application.
You now have a functional AutoCAD VB .NET Application!

1.6

Debugging the Application

You can load your compiled application into AutoCAD now using the NETLOAD command and run the
application by typing myVBCommand at the command prompt, however, before we can truly debug our
application we have to edit our ApplicationName.vbproj.user file so that Visual Studio Express 2012 can
play nice with AutoCAD.

1.7

Set the Debug Application Path

The Visual Studio 2012 Express Edition does not provide any method through its user interface to specify
a debug application path to launch AutoCAD from the Visual Studio 2012 Express Edition environment.
So we have to play a small trick here:
1) Save the project and close Visual Studio 2012 Express Edition.
2) Navigate to the location of the project in Windows Explorer. My Project is in the folder
C:\My Documents\Visual Studio 2012\Projects\MyVBAcadApp\MyVBAcadApp.

US/CANADA
800-356-9050

Page 6 Using Visual Studio Express 2012 to Create an AutoCAD and Civil 3D 2015 .NET Add-in

imaginit.com

3) Open the Project file <ApplicationName.vbproj.user> in a text editor. I used Notepad as the
text editor and my application project file is named MyVBAcadApp.vbproj.user. You will notice that
it is an XML file. Add the following two XML Nodes:
<PropertyGroup Condition=$(Configuration)|$(Platform) == Debug|AnyCPU>
<EnableUnmanagedDebugging>false</EnableUnmanagedDebugging>
<StartAction>Program</StartAction>
<StartProgram>C:\Program Files\Autodesk\AutoCAD 2015\acad.exe</StartProgram>
</PropertyGroup>

Note: The StartProgram node stores the path of the AutoCAD application acad.exe. You will
have to modify this path to the correct path to the acad.exe executable on your machine.

Here is a snapshot of my VBPROJ.user file opened in notepad:

4) Save the VBPROJ.user file.


5) The next step is to set up the debugger.

1.8

Loading our VB .NET Application into AutoCAD

Finally we are done all our setup for creating our VB project as well as setting up you VBE debugger.
1) Select the StartDebug Menu item or press F5. This will launch the AutoCAD application from the

Visual Studio 2012 Express Edition debugging environment.

US/CANADA
800-356-9050

Page 7 Using Visual Studio Express 2012 to Create an AutoCAD and Civil 3D 2015 .NET Add-in

imaginit.com

2) Once AutoCAD is launched, run the NETLOAD command. This will display the Choose .NET

Assembly dialog (as shown below):

3) Navigate to the location of your built application. My application lived in the



C:\My Documents\Visual Studio 2012\Projects\MyVBAcadApp\MyVBAcadApp\bin\Debug folder.
4) Select the application DLL and click Open. The application is now loaded into AutoCAD.
5) Run the command MyVBCommand (or the command name that you defined in your .NET
application.).
6) As per the line in your code

ed.WriteMessage(My First command implemented in VB .NET Express Edition)

The message will be displayed in the command prompt.

1.9

Set a Breakpoint in the Code

1) In VS2012 Set the cursor at the line of code to which you want to break.
2) Click the DebugToggle Breakpoint menu item or press the F9 key. Here is a snapshot of the

break point at the point of code where we set the Editor object (The Red dot indicates the break point):

US/CANADA
800-356-9050

Page 8 Using Visual Studio Express 2012 to Create an AutoCAD and Civil 3D 2015 .NET Add-in

imaginit.com

3) In AutoCAD, run the command (in our case MyVBCommand) on the AutoCAD command line and

you will see the break point hit:

You can now use the debugger keys like F8 or Shift+F8 etc. to traverse through the code.
Congratulations, you have successfully used Visual Studio 2012 Express Edition to create and use a
.NET application in AutoCAD!

Create a Civil 3D .NET Application


2.1

Adding the Required References

1) We need to add C3D references, in the solution explorer Right-click on the References folder

and select Add Reference
2) Browse to the Civil 3D Install Folder (the default location is C:\Program Files\Autodesk\AutoCAD

2015\ACA) and select and add the AecBaseMgd.dll.
3) Next we need to add the C3D references, browse to the Civil 3D Install Folder (the default location is

C:\Program Files\Autodesk\AutoCAD 2015\C3D) and select and add the AecDbMgd.dll.
4) Once these references have been added, ensure you change the properties to set Copy Local to

False.
5) Add the necessary Namespaces to the source files for convenient access to the Civil 3D .NET APIs.

Imports Autodesk.Civil.ApplicationServices

Imports Autodesk.Civil.DatabaseServices.Styles

6) Add the command method that implements the command(s) you need.

a. Here is an example of the command method implementing the command we use: I received the
following from my buddy Partha at Autodesk (although I have not had the pleasure of having a beer
with Partha I will consider him a friend, for all the help I got from him).

US/CANADA
800-356-9050

Page 9 Using Visual Studio Express 2012 to Create an AutoCAD and Civil 3D 2015 .NET Add-in

imaginit.com

<CommandMethod(StylesAlignmentInfo)> _
Public Sub GetTheAlignmentStyles()

This sample Demonstrates Accessing the Alignment Styles collection


Created by Partha Sarkar - DevTech, Autodesk

Dim trans As Transaction = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction()


Dim civilDoc As CivilDocument = Autodesk.Civil.ApplicationServices.CivilApplication.ActiveDocument
Dim ed As Editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor
Try
Dim oAlignmentStyles As AlignmentStyleCollection = civilDoc.Styles.AlignmentStyles
MsgBox(This DWG File has : + oAlignmentStyles.Count.ToString + Alignment Styles !)


Dim alignmentStyleId As ObjectId = Nothing

Dim oAlignmentStyle As AlignmentStyle = Nothing


For Each alignmentStyleId In oAlignmentStyles
oAlignmentStyle = trans.GetObject(alignmentStyleId, OpenMode.ForWrite)
MsgBox(Style Name : + oAlignmentStyle.Name.ToString)
Next
trans.Commit()
Catch ex As Exception
ed.WriteMessage(Exception Message is : + ex.Message.ToString())
Finally
trans.Dispose()
End Try
End Sub

Note: We will not be elaborating on the creation of commands and the AutoCAD .NET APIs in this
tutorial. You can find a lot more information on this in the references at the end of this tutorial.

US/CANADA
800-356-9050

Page 10 Using Visual Studio Express 2012 to Create an AutoCAD and Civil 3D 2015 .NET Add-in

imaginit.com

Here is a snapshot of how the command code would look like:

7) Now that your Civil 3D application is created, as in the AutoCAD steps above you can proceed to:

a. 1.4 Save the Project

b. 1.5 Build the Project

c. 1.8 Loading our VB .NET Application

d. And finally run the app by typing StylesAlignmentInfo at the command prompt.

8) The app will display all the alignment styles in your current drawing.

Debug AutoLoad .dll with Script file (addendum by Justin Ralston 06-12-2010 02:08 AM)
To speed up the debug process create a .scr script file with the following contains to do the netload and
open your test drawing.

US/CANADA
800-356-9050

Page 11 Using Visual Studio Express 2012 to Create an AutoCAD and Civil 3D 2015 .NET Add-in

imaginit.com

Once you have created the script file add the command line arguments to VB.net Express at the following
location.

References:
1)



Developer Center for AutoCAD (www.autodesk.com/developautocad): This website provides a variety


of learning resources such as DevTV: Introduction to AutoCAD .NET Programming, an excellent
introduction to the AutoCAD .NET API and AutoCAD .NET Labs, a comprehensive set of self-learning
tutorials. The Developer Center lists many other free development resources, including links to the
Autodesk discussion groups for the various AutoCAD APIs.

2) Through the Interface Blog (http://through-the-interface.typepad.com/): A very interesting and



engaging blog on all AutoCAD APIs and allied technologies
3) Autodesk Developer Network: For professional support for your software development work, consider

joining the Autodesk Developer Network program. Visit www.autodesk.com/joinadn for more details of

the program.

About the Author: Malcolm Fernandes


Malcolm has a wealth of practical experience in the area of Urban Planning, Geomatics, Transportation, Environmental
Design, Engineering Construction, and Urban Design. Some of the projects on which hes worked as a project manager,
designer, or surveyor include airport runways, drainage channels, golf courses, storm ponds, railway lines, DND weapons
ranges, highways, recreational sites, commercial sites, and urban and rural subdivisions.
Malcolm has also worked with a variety of engineering software solutions and has been responsible for setting
standards and design work-flow to increase efficiency in the design and construction of civil engineering projects.
Malcolm has recently worked with a number of Transportation Engineering clients in western and central Canada,
involving training, customization and mentoring on transportation projects; This sometimes also included developing applications i.e. vb.net subassemblies, VBA or simply XML design criteria file. to bridge the gap in Civil 3D in a
production environment.
All brand names, product names, or trademarks belong to their respective holders.
IMAGINiT Technologies, Inc. is not responsible for typographical or graphical errors that may appear in this document.
2014 IMAGINiT Technologies, Inc., All Rights Reserved.

US/CANADA
800-356-9050

Page 12 Using Visual Studio Express 2012 to Create an AutoCAD and Civil 3D 2015 .NET Add-in

imaginit.com

Вам также может понравиться