Working with Civil3D 2010 Managed .Net API 101 – 4

In our last exercise, we learned about getting the alignment objects from our objectId collection through a transaction. Well, now that we have our alignment objects how do we display data about the alignment object?? Let’s add a form to our Visual Studio Project.

Go to the Project menu and select Add New Item. This will open up a dialog box containing a default list of different items we can add to our project. We want to add a dialog (which is really a form with a few settings already setup for us). Name it “frmAlin” and click Add.

This will add a new windows form called frmAlin. A windows form is pretty much the base for any GUI. The main application (like Internet Explorer or AutoCAD) is a form. Each child of a MDI application is a form (like each drawing in AutoCAD). Also, each dialog box is a form. Thus we use forms extensively when building software applications.

In our Properties window, we want to change the text for our form. We want the text to read, “Alignment Data”.

Now we are going to add from our Toolbox (if you don’t have Toolbox available go to the View menu), we are going to add a TreeView.

With our new TreeView object, we are going to set the following properties:

  • Name: treMain
  • Dock: Top

Okay, so now we have our form setup let’s go back to our Sub routine and call this form and add data to it.

First, above our “Using db as Database” statement, we want to place the following:

Dim frm As New frmAlin

Dim tN As Windows.Forms.TreeNode

Dim profIds As ObjectIdCollection

 

The first line will create a new frmAlin form and will assign it to the variable “frm”. The next two variables we are going to use in our loop. Now in our For Each loop, we want to add the following right after assigning the “alin” variable.

tN = frm.treMain.Nodes.Add(alin.Name)

tN.Nodes.Add(“Description: “ & alin.Description)

tN.Nodes.Add(“Alignment Type: “ & alin.AlignmentType.ToString)

tN.Nodes.Add(“Alignment Starting Station: “ & alin.GetStationStringWithEquations(alin.StartingStation))

tN.Nodes.Add(“Alignment Ending Station: “ & alin.GetStationStringWithEquations(alin.EndingStation))

profIds = alin.GetProfileIds

If profIds Is Nothing Then

tN.Nodes.Add(“Total Profiles: 0”)

ElseIf profIds.Count = 0 Then

tN.Nodes.Add(“Total Profiles: 0”)

Else

tN.Nodes.Add(“Total Profiles: “ & profIds.Count)

End If

tN.Nodes.Add(“Is Referenced?: “ & alin.IsReferenceObject.ToString)

tN.Nodes.Add(“Site: “ & alin.SiteName)

tN.Nodes.Add(“Style: “ & alin.StyleName)

This creates a new node in our treeview in our form with the alignment name and then stores all the properties of the alignment as subnodes to the main node.

The last thing we want to add is to show our form. We want to do this AFTER our transaction. So at the very end of our code, we want to add the following line:

Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(frm)

 

This line shows the form we just created and makes sure that it is connected to the AutoCAD application.

So we have our coding done! Great! Now how do we check it or run it?? Check in tomorrow for our last part to this series.

By the end of today you should have the following stored in your class:

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

 

        Dim frm As New frmAlin

        Dim tN As Windows.Forms.TreeNode

        Dim profIds As ObjectIdCollection

 

        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)

                    tN = frm.treMain.Nodes.Add(alin.Name)

                    tN.Nodes.Add(“Description: “ & alin.Description)

                    tN.Nodes.Add(“Alignment Type: “ & alin.AlignmentType.ToString)

                    tN.Nodes.Add(“Alignment Starting Station: “ & alin.GetStationStringWithEquations(alin.StartingStation))

                    tN.Nodes.Add(“Alignment Ending Station: “ & alin.GetStationStringWithEquations(alin.EndingStation))

                    profIds = alin.GetProfileIds

                    If profIds Is Nothing Then

                        tN.Nodes.Add(“Total Profiles: 0”)

                    ElseIf profIds.Count = 0 Then

                        tN.Nodes.Add(“Total Profiles: 0”)

                    Else

                        tN.Nodes.Add(“Total Profiles: “ & profIds.Count)

                    End If

                    tN.Nodes.Add(“Is Referenced?: “ & alin.IsReferenceObject.ToString)

                    tN.Nodes.Add(“Site: “ & alin.SiteName)

                    tN.Nodes.Add(“Style: “ & alin.StyleName)

                Next

            End Using

        End Using

 

        Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(frm)

 

    End Sub

End Class

 

One comment