Visual Basic AutoCAD Civil3D Add-In

Embed Size (px)

Citation preview

  • 8/9/2019 Visual Basic AutoCAD Civil3D Add-In

    1/12

    TECHNICAL WHITEPAP

    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 applica-

    tion for AutoCAD using the Visual Studio Express development environment. The document focuses on set-

    ting 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 Projector type Ctrl+N. The New Project dialog will appear:

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

    eld. Here, we chose MyVBAcadApp.

  • 8/9/2019 Visual Basic AutoCAD Civil3D Add-In

    2/12

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

    imaginit.com800-356-9050

    US/CANADA

    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 Explorermenu item or type Ctrl+W+S.

    2) Go to the Solution Explorertab on the right side of the Visual Studio 2010 Express window select

    the Show All Filesbutton 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.

    http://imaginit.com/http://imaginit.com/
  • 8/9/2019 Visual Basic AutoCAD Civil3D Add-In

    3/12

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

    imaginit.com800-356-9050

    US/CANADA

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

    1.2 Adding the Required References

    1) Go to the Solution Explorertab on the right side of the Visual Studio 2012 Express window select

    the Show All Filesbutton as shown below.

    2) The above step will display the Referencesfolder in the Solution Explorer as shown below.

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

    Reference Manager menu.

    4) Click on the Browsebutton and Navigate to the location of the AutoCAD-specic Reference les.

    Note:You will nd AutoCAD-specic reference les in a couple of locations. If you have ObjectARX

    SDKinstalled, you can navigate to the Include folder (I would navigate to C:\ObjectARX\2012\inc-

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

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

    http://imaginit.com/http://imaginit.com/
  • 8/9/2019 Visual Basic AutoCAD Civil3D Add-In

    4/12

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

    imaginit.com800-356-9050

    US/CANADA

    Alternatively, you can navigate to the AutoCAD or Civil 3D Install Folderand nd the references

    there as shown below:

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

    and click the ADDbutton. These les contain all the necessary .NET APIsto create a generic Auto

    CAD .NET application

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

    Click OKto 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).

    http://imaginit.com/http://imaginit.com/
  • 8/9/2019 Visual Basic AutoCAD Civil3D Add-In

    5/12

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

    imaginit.com800-356-9050

    US/CANADA

    Select the DLL les acmgd.dll, acdbmgd.dll, AcCoreMgd.dll and AecBaseMdg.dll, AecDBMgd.

    dllfrom the location you browse to and click the OKbutton. These les contain all the necessary

    .NET APIsto create a generic AutoCAD .NET application.

    Note: You can select multiple les (two les 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 Namespacesto

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

    AutoCAD .NET API specic namespaces:

    ImportsAutodesk.AutoCAD.ApplicationServices

    ImportsAutodesk.AutoCAD.EditorInput

    ImportsAutodesk.AutoCAD.Runtime

    ImportsAutodesk.AutoCAD.DatabaseServices

    You can add these namespaces at the top of the Class1.vbsource le 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 nd a lot more information on this in the references provided at the end of thistutorial.

    1.3 Creating a Command

    Add the command method that implements the command(s) you need. Here is an example of a com-

    mand method implementing the command we use:

    Dene command MyVBCommand

    _

    Public SubCmd1() This method can have any name

    Type your code here

    DimedAsEditor = 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 nd a lot more information on this in the references provided at the end of this tutorial.

    http://imaginit.com/http://imaginit.com/
  • 8/9/2019 Visual Basic AutoCAD Civil3D Add-In

    6/12

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

    imaginit.com800-356-9050

    US/CANADA

    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 Buildthe application. Use theBuildBuildmenu 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 le 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.

    http://imaginit.com/http://imaginit.com/
  • 8/9/2019 Visual Basic AutoCAD Civil3D Add-In

    7/12

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

    imaginit.com800-356-9050

    US/CANADA

    3) Open the Project le in a text editor. I used Notepad as the

    text editor and my application project le is named MyVBAcadApp.vbproj.user. You will notice that

    it is an XML le. Add the following two XML Nodes:

    false

    Program

    C:\Program Files\Autodesk\AutoCAD 2015\acad.exe

    Note: The StartProgramnode 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 le opened in notepad:

    4) Save the VBPROJ.user le.

    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 StartDebugMenu item or press F5. This will launch the AutoCAD application from the

    Visual Studio 2012 Express Edition debugging environment.

    http://imaginit.com/http://imaginit.com/
  • 8/9/2019 Visual Basic AutoCAD Civil3D Add-In

    8/12

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

    imaginit.com800-356-9050

    US/CANADA

    2) Once AutoCAD is launched, run the NETLOADcommand. 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 dened 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 Breakpointmenu 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):

    http://imaginit.com/http://imaginit.com/
  • 8/9/2019 Visual Basic AutoCAD Civil3D Add-In

    9/12

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

    imaginit.com800-356-9050

    US/CANADA

    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-clickon the Referencesfolder

    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 isC:\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 Localto

    False.

    5) Add the necessary Namespacesto the source les for convenient access to the Civil 3D .NET APIs.

    ImportsAutodesk.Civil.ApplicationServices

    ImportsAutodesk.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).

    http://imaginit.com/http://imaginit.com/
  • 8/9/2019 Visual Basic AutoCAD Civil3D Add-In

    10/12

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

    imaginit.com800-356-9050

    US/CANADA

    _

    Public SubGetTheAlignmentStyles()

    This sample Demonstrates Accessing the Alignment Styles collection

    Created by Partha Sarkar - DevTech, Autodesk

    DimtransAsTransaction= HostApplicationServices.WorkingDatabase.TransactionManager.Start-

    Transaction()

    DimcivilDocAsCivilDocument= Autodesk.Civil.ApplicationServices.CivilApplication.ActiveDocument

    DimedAsEditor= Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActi-

    veDocument.Editor

    Try

    DimoAlignmentStylesAsAlignmentStyleCollection= civilDoc.Styles.AlignmentStyles

    MsgBox(This DWG File has : + oAlignmentStyles.Count.ToString + Alignment Styles !)

    DimalignmentStyleIdAsObjectId= Nothing

    DimoAlignmentStyleAsAlignmentStyle= Nothing

    For EachalignmentStyleId InoAlignmentStyles

    oAlignmentStyle = trans.GetObject(alignmentStyleId, OpenMode.ForWrite)

    MsgBox(Style Name : + oAlignmentStyle.Name.ToString)

    Next

    trans.Commit()

    CatchexAsException

    ed.WriteMessage(Exception Message is : + ex.Message.ToString())

    Finally

    trans.Dispose()

    End Try

    End Sub

    Note:We will notbe elaborating on the creation of commands and the AutoCAD .NET APIs in this

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

    http://imaginit.com/http://imaginit.com/
  • 8/9/2019 Visual Basic AutoCAD Civil3D Add-In

    11/12

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

    imaginit.com800-356-9050

    US/CANADA

    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 nally 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 le(addendum by Justin Ralston 06-12-2010 02:08 AM)

    To speed up the debug process create a .scr script le with the following contains to do the netload and

    open your test drawing.

    http://imaginit.com/http://imaginit.com/
  • 8/9/2019 Visual Basic AutoCAD Civil3D Add-In

    12/12

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

    imaginit.com800-356-9050

    US/CANADA

    Once you have created the script le 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/joinadnfor more details ofthe 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-ow to increase efciency 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 develop-

    ing applications i.e. vb.net subassemblies, VBA or simply XML design criteria le. 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.

    http://imaginit.com/http://www.autodesk.com/developautocadhttp://through-the-interface.typepad.com/http://www.autodesk.com/joinadnhttp://www.autodesk.com/joinadnhttp://through-the-interface.typepad.com/http://www.autodesk.com/developautocadhttp://imaginit.com/