Working with Civil3D 2010 Managed .Net API 101 – 2

Now that we have everything setup for our first Civil3D add-on project based upon our first class. It is time to add some code.

First, we are going to add to software class. A class can represent an object. For example, you can create a bicycle class. This class would be a place to store states, behaviors, and/or attributes of the bicycle. However, in our simple exercise we are using a class as a location to store methods for reusability and ease of debugging.

Our project automatically created a class called Class1.vb. Go to Solutions Explorer and open this class. To reduce the amount of coding we have to do we are going to add some imports of namespaces. This will be done ABOVE the line that says Public Class Class1.

Imports Autodesk.AutoCAD.Runtime

Imports Autodesk.AutoCAD.DatabaseServices

Imports Autodesk.Civil.Land.DatabaseServices

Once we have done that then step inside the Public Class and type the following:

<CommandMethod(“AlignmentInfo”)> _

    Public Sub cmdAlignInfo()

 

    End Sub

The command method is part of Autodesk’s runtime namespace and will allow us to actually create a new command. The command is named “AlignmentInfo” and is connected to the Sub called cmdAlignInfo.

Inside the sub, we will begin writing our code. The goal is to get a collection of data regarding the alignments in the drawing. To do this we need to get a collection of alignment object ids. An objectId is a unique ID that is assigned to each and every object in a drawing’s database at its runtime. It is the way we identify which object we are trying to get. You will see more of what I mean in our next exercise.

Dim alinIds As ObjectIdCollection

 

alinIds = Autodesk.Civil.ApplicationServices.CivilApplication.ActiveDocument.GetAlignmentIds

 

If alinIds Is Nothing Then Exit Sub

If alinIds.Count = 0 Then Exit Sub

So first we define a local variable called alinIds. All variables are to be associated with a object type. In this case our variable is associated with an ObjectIdCollection which is part of Autodesk.AutoCAD.DatabaseServices namespace.

We then assign data to our variable. We go to our active CivilDocument and get alignment objectId’s. This collection will represent all the alignments found within the current document, or drawing.

If the drawing has no alignments, then our variable could either be empty or contain no ObjectId’s. Thus, we build in a little error trapping by stepping out of our routine (Sub) if that is the case. There is no reason to continue if there are no alignments in the drawing. I suppose we could get a little fancier but we will save that for another day.

Next, let’s work with our collection and gather some data.

At the end of our exercise today, you should have the following: