Programming
Programming
Programming
NET Programming
Josh Modglin – InMotion Consulting
SD4888-L Have you ever wanted to program and customize AutoCAD Civil 3D software but couldn’t seem
to make the jump from Microsoft VBA (Visual Basic for Applications Module)? We will walk through a real, live
project idea from start to deployment using VBA, Microsoft .NET, and the AutoCAD Civil 3D software .NET API.
In the process we will learn about proper referencing, object structure, transactions, and the loading of our built
application into AutoCAD Civil 3D software.
Learning Objectives
At the end of this class, you will be able to:
Learn how to start and create a Microsoft .NET project
Learn how to build a .NET AutoCAD software command
Learn how to interact with the AutoCAD Civil 3D software .NET API
Learn how to deploy the completed code
Josh started with AutoCAD Release 12 over 18 years ago and progressing through each release is
now building .Net applications for AutoCAD Civil 3D 2015. For the past 3 years, Josh has served as
the technical editor for the best-selling book, Civil 3D Essentials. In addition to writing and working with
the software, Josh has been a top-rated presenter at Autodesk University for four years.
Currently, Josh serves as the Managing Partner for InMotion Consulting, a technical consulting
solution provider and a member of the Autodesk Developer’s Network.
Page 1 of 18
SD4888-L – Getting Started With AutoCAD Civil 3D .NET
Programming
Microsoft Visual Studio Express 2013 for Windows Desktop is free and, to get started with .Net
programming, is probably the best choice since it has most of the tools you will need to begin creating
very complex .Net add-ins for AutoCAD Civil 3D. In our class, we will be working with Microsoft Visual
Studio Professional 2013.
Nevertheless, once you have chosen to use .Net the next big step is to choose a language that works
FOR YOU. The great thing about .Net is that it takes whatever is written in the coding language and
compiles it into a common language at runtime meaning it makes no difference what language you
choose to write the code in. C++, C#, VB, and even F# all have the same capabilities when working
with the managed .Net API from Autodesk. Thus, the choice is left with what you are most comfortable
with, what language has the most samples to learn from, and what other type of coding you anticipate
doing in the future. In our class today, we will be using VB.Net since it is usually the easiest transition
for those who have coded in VBA.
Page 2 of 18
SD4888-L – Getting Started With AutoCAD Civil 3D
.NET Programming
Page 3 of 18
SD4888-L – Getting Started With AutoCAD Civil 3D .NET
Programming
Below the references list, click the Add… button. Once inside the Reference manager, select the
8 Browse… button down at the bottom right.
We need to add references to Autodesk’s AutoCAD (since Civil 3D is built on top of AutoCAD) and Civil 3D APIs.
Page 4 of 18
SD4888-L – Getting Started With AutoCAD Civil 3D
.NET Programming
Page 5 of 18
SD4888-L – Getting Started With AutoCAD Civil 3D .NET
Programming
Page 6 of 18
SD4888-L – Getting Started With AutoCAD Civil 3D
.NET Programming
As you can see, using the Wizard is much easier in the setup of our .Net project. Since this is the case,
all the rest of the exercises are built based upon the use of this Wizard. However, information will be
provided in the appendix to assist if you are building your add-in for earlier versions.
1 With your Visual Studio Project Open, click the Start button.
If you started with the .Net Wizard from Autodesk, that’s it. Your assembly (dll file) is loaded. Type
the command(s) at the command line.
2
If you did not start your project with the .Net Wizard, go to step 3.
For more information on how this works, see Deploy the Completed Code
3 From the command line within Civil 3D, type NETLOAD
Page 8 of 18
SD4888-L – Getting Started With AutoCAD Civil 3D
.NET Programming
Transactions
To get anything from the document’s database, you need to query it. To query you need to start a
Transaction which ‘opens’, or connects you to the drawing database. Using the word, “Transaction”
let’s use a word picture. You go to the bank to:
With the way a bank works, each event is a transaction. You start the transaction and then provide
some unique means to identify to the bank what account you are getting information on or even
making changes to.
Page 9 of 18
SD4888-L – Getting Started With AutoCAD Civil 3D .NET
Programming
Inside the cmdChgAlignStyles sub routine, we are going to make edits below the comment line.
First we are going to create a transaction. Type the following:
Using db As Database = HostApplicationServices.WorkingDatabase
6
End Using
This gets the active drawing’s database and assigns it to a variable named db.
The Using Statement is a special way to code cleanly in VB.Net. The database variable is disposed of immediately after
the using Statement
Inside the db using statement added in Step 6, add the following:
Using db As Database = HostApplicationServices.WorkingDatabase
Using trans As Transaction = db.TransactionManager.StartTransaction
7
End Using
End Using
We are taking advantage of the Using Statement for starting a transaction within the active database. The transaction is
assigned to the trans variable.
Inside the trans Using statement added in Step 7, add the following:
Dim align As Alignment
For Each id As ObjectId In alignIds
align = trans.GetObject(id, OpenMode.ForWrite)
align.StyleId = civDoc.Styles.AlignmentStyles(0)
Next
8 We are interacting with our first REAL Civil 3D object within the .Net API. We are getting each alignment object created
in the Civil 3D drawing and assigning the first Civil 3D alignment style created to it.
To do this, we create a variable called align. We iterate through each ObjectId we got in Exercise 1, step 9 and get the
actual alignment object associated with the ObjectId. With the alignment object, we now have the ability to read to its
properties and even change its properties – such as assigning the alignment’s style by associating a style ObjectId to the
property.
Add the following below the line and outside of the next statement added in step 8:
9 trans.Commit
Although we have made the changes to the alignments, no changes are committed or actually saved to the database
unless we call this method.
This is a basic example of applying changes to the properties of alignment objects. Let’s take this a little
bit further and provide information regarding what was done to our user. In the process, we will get
interact with the Alignment Style further.
Page 10 of 18
SD4888-L – Getting Started With AutoCAD Civil 3D
.NET Programming
Inside the cmdChgAlignStyles sub routine, we are going to make edits below the comment line.
First we are going to create a transaction. Type the following:
6 Dim alignStyle As Styles.AlignmentStyle
alignStyle = trans.GetObject(civDoc.Styles.AlignmentStyles(0), OpenMode.ForRead)
We create a new variable to store our alignment style object and then using the transaction, we get the object associated
with the objectId derived from selecting the first style in the alignmentstyles collection. We open it only to read
information but not make any changes.
Inside the For Each…Next statement, change align.StyleId = civDoc.Styles.AlignmentStyles(0)
7 to the following:
align.StyleId = alignStyle.Id
The old process we used in exercise 2 still works but this is another way to assign a specific style’s ID to an alignment.
Inside the trans Using statement, right above trans.Commit(), add the following:
ed.WriteMessage(vbLf & alignStyle.Name & " style has been applied to " & _
8
alignIds.Count & " alignments")
Using the name of the alignment style that we retrieved in step 6, we tell the user (at the command line) what style we
change the alignments to and how many alignments received this change.
Page 11 of 18
SD4888-L – Getting Started With AutoCAD Civil 3D .NET
Programming
All you have to do is copy your dll to the C:\Program Files\Autodesk\ApplicationPlugins directory in a
special folder for your project. For example,
myproject.bundle folder. The two files required to exist in The folder must contain “.bundle”
this folder is your project assembly (the dll) and a at the end of the name
PackageContents.xml file.
When you use the Wizard to start your project it creates the base PackageContents.xml file for
you!
This is why you haven’t had to load your assembly for testing. It loads it for you by compiling your code
and placing a .bundle folder in your Roaming directory. Let’s look at editing this PackageContents.xml.
Page 12 of 18
SD4888-L – Getting Started With AutoCAD Civil 3D
.NET Programming
Page 13 of 18
SD4888-L – Getting Started With AutoCAD Civil 3D .NET
Programming
APPENDIX
Additional Resources
http://autodesk.com/developautocad
http://autodesk.com/developcivil
http://adndevblog.typepad.com/infrastructure
http://adndevblog.typepad.com/autocad
http://through-the-interface.typepad.com
http://www.inmotioncon.com
Page 14 of 18
SD4888-L – Getting Started With AutoCAD Civil 3D
.NET Programming
Project Properties
In our Solution Explorer, if we right-click on our project’s name and select properties this will open a
project’s properties tab.
Notice that our assembly name (or the dll that will be made for our plug-in once everything is compiled)
is the same name as our project. We can change the name of our output assembly but it is usually a
good practice to keep it the same as our project.
Namespaces
Namespaces are what you might consider a directory structure (tree structure) for libraries. Thus, it
is a good practice to assign namespaces that organize all your libraries under one major
namespace (think Root directory).
The easiest way to make sure that all libraries we build are under the same root directory is to set
the root directory to our company name.
For example, in our project the main root directory, or first namespace, will be “AU”.
Now since this project will include all functions, methods, etc that is specific to working with Civil 3D,
we will add one more directory or namespace –
“Civil3D”. Thus, in our Root namespace we will
have “AU.Civil3D”.
Page 15 of 18
SD4888-L – Getting Started With AutoCAD Civil 3D .NET
Programming
References
We have already added references to the required AutoCAD API libraries. However, Autodesk
provides additional libraries that can be used when developing in AutoCAD. For example,
adWindows.dll provides more integrated user interface tools.
Autodesk references aren’t the only references we can add. We can select additional Microsoft,
third party, or even references from other projects we have created.
Copy Local
Since we are building an AutoCAD add-on, then when our software runs the AutoCAD references
will already exist in and be called from AutoCAD. Thus, we need to set each of these references to
not be copied with our project. In fact most references we add do not need to be copied locally.
Pay attention to this property as you add references.
Classes
What is a Class?
Oftentimes we refer to the file holding a class object as a class. However, classes in .Net are
actually objects. For example, you have in Civil 3D an Alignment object. This alignment object
INHERITS the traits and properties of a base AutoCAD object. You can create your own special
object in .Net with its own properties, methods, and functions by creating a new class. Then you
can use these for presenting user interfaces to the user and accomplish quicker coding.
To do this, we are going to create a base class and populate this base class using functions. To
create a new class, right-click on our project name in Solutions Explorer and choose to
Add>Class…
This class is going to have two properties - Name and Id. We add properties using the following:
Page 16 of 18
SD4888-L – Getting Started With AutoCAD Civil 3D
.NET Programming
In this class, we have a variable defined which is only available within the class (declared Private)
set to something as default. Then we create a property which is available everywhere (declared
Public) and store the value for use.
Can you think you think of another property that belongs to all C3D objects? Maybe a description?
Or maybe you could add a property called IsSelected. This property will be a Boolean (or a
True/False) which can be bound to Controls in your Windows (forms or dialog boxes).
Using Classes
Now that we have this base class, let’s see its real power by using it in a function. The function can
be called from anywhere and can populate a collection of these base class objects with all the
information we need. The function could be similar to the following:
Page 17 of 18
SD4888-L – Getting Started With AutoCAD Civil 3D .NET
Programming
Page 18 of 18