Working with Civil3D 2010 Managed .Net API 101 – 3

In our last post, we started building a new command which will iterate through the alignments in the drawing and get certain details about them. In this post, we are going to really delve into understanding how to work with Autodesk’s database.

The best way to understand how to work with .Net in Autodesk is to think of each document as one large database. You may have different tables in a database, whereas in a document you have different objects. However, everything has to work through or be transmitted through this database. Databases are the foundation to managing data in the world. For example, banks use software that controls all the accounts and transactions based upon databases. Even if you just want to check how much you have in your account, they would use the software to make a ‘transaction’ with the database and search for you account. When it finds your account it returns the account object and the software can do all kinds of things with that account object. Why in world am I wasting your time talking about bank transactions?? Because to get an object (alignment) from the document’s database, we would feed our ‘account number’ (objectId) to a transaction that we create.

Let’s see how this works.

 Using db As Database = HostApplicationServices.WorkingDatabase

The last line before “End Sub” we add to our new sub routine is what is above. We are using a special way to call the current (or working) database. We are using the “Using” keyword to call a new variable called db which is an autodesk database object and then we assign this variable to the working database. The “Using” keyword has to be closed with an “End Using” clause. This makes sure that the variable db is properly disposed. Inside the “Using db” phrase (next line), add the following:

Using trans As Transaction = db.TransactionManager.StartTransaction

 

This starts a transaction with the current drawing’s database. The transaction is assigned to the variable “trans”. This is where it gets really important to make sure that we dispose (or close) our transaction, otherwise things could get dicey. However, now that we started a transaction with the current drawing’s database and we have all the objectId’s that belong to the alignment objects in the drawing let’s loop each one and get the alignment object.

Dim alin As Alignment

For Each objId As ObjectId In alinIds

   alin = trans.GetObject(objId, OpenMode.ForRead)

Next

So right inside our transaction, we assign a new variable called alin which will be an Autodesk.Civil.Land.Database.Alignment (isn’t alignment easier to write? That why we imported that namespace in the last exercise so we only have to type Alignment.).

Then we create a loop that we go through each objectId in the alinIds objectIdCollection. In the definition of the loop we assign a temporary variable (objId) and assign the variables value to ObjectId. Next, we assign the alin variable to the object found through our transaction. Since we are only getting data from the object, we open in a read-only state. Great, now that we have the object, now what? Tune in for the next exercise.

By the end of this exercise your class should look like the following:

Imports Autodesk.AutoCAD.Runtime

Imports Autodesk.AutoCAD.DatabaseServices

 

Imports Autodesk.Civil.Land.DatabaseServices

 

Public Class Class1

    <CommandMethod(“AlignmentInfo”)> _

    Public Sub cmdAlignInfo()

        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

 

        Using db As Database = HostApplicationServices.WorkingDatabase

            Using trans As Transaction = db.TransactionManager.StartTransaction

                Dim alin As Alignment

                For Each objId As ObjectId In alinIds

                    alin = trans.GetObject(objId, OpenMode.ForRead)

                Next

            End Using

        End Using

    End Sub

End Class

 

4 comments

  1. Joshua

    I cannot get my code to compile due to an error with the line

    Imports Autodesk.Civil.Land.DatabaseServices

    It syas civil in not a member of Autodesk is there a reference or something else I need to add to the project?

    Regards

    Justin Ralston

  2. Justin,

    Make sure that you have the references added as stated in the first part of this series.

    http://www.civil3d.com/2009/04/working-with-civil3d-2010-managed-net-api-101-1/

  3. Joshua

    I had missed the second two references as I have been refering to this paper from AU 2007 that is quite good could but needs updating.

    http://au.autodesk.com/?nd=class&session_id=2857

    Regards

    Justin Ralston