Thanks to Scott Wolter from Digital Vellum for suggesting this topic, and to Partha Sarkar (a member of the DevTech team in Bangalore, India) for contributing it...
Welcome to the world of developing Autodesk Civil 3D 2007 applications with Visual Studio .NET. Autodesk Civil 3D provides interop assemblies that expose its COM interfaces for use from .NET technologies, such as VB.NET, C#.
Before you start developing .NET applications for Autodesk Civil 3D 2007, I strongly recommend you apply the Autodesk Civil 3D 2007 Service Pack 1A, otherwise you may find some issues in referencing the Civil 3D TLB files such as AeccXLand40.tlb, AeccXUiLand40.tlb etc.
I’m now going to look at how to write an application using Visual Studio .NET to generate a Profile (also called a vertical alignment) for a given horizontal alignment with an associated surface.
Before we get into the nitty-gritty of developing a .NET application for Civil 3D, first let us understand what a Profile object is. Profiles are used to visualize the terrain along a route of interest, such as a proposed road or pipeline. There are two distinct Civil 3D entities when using Profiles, the Profile itself and the Profile View. The Profile is the actual section cut through a surface or the proposed design profile and has its associated style and label style. The Profile View is the data surrounding the display of the profile line - grids, axes, labels and desired band styles, which contain data on any side of the grid, horizontal / vertical geometry data etc.
The first step when developing a .NET application for Civil 3D is to add the references to Civil 3D COM libraries such as AeccXLandLib, AeccXUiLandLib etc. If you are planning to work with Corridor objects or Pipe objects, you need to reference the appropriate libraries for Corridor and Pipes.
You need to make sure you add a ProfileView, before you call AddFromSurface and this is how you do it:
Dim oProfileViews As AeccLandLib.AeccProfileViews
oProfileViews = oAlignment.ProfileViews
oProfileViews.Add("Test Profile View", "0", originPt, oVStyle, oBandStyle)
The final step is to add the Profile for a given surface, profile type, profile style, sample starting and ending stations, and layer name by calling AddFromSurface:
Dim oProfile As AeccLandLib.AeccProfile
oProfile = oAlignment.Profiles.AddFromSurface("Existing Ground Profile", _
AeccLandLib.AeccProfileType.aeccExistingGround, _
oProfileStyle, oSurface.Name, startStn, endStn, "0")
Here is the complete VB.NET sample code:
Imports System
Imports Autodesk
Imports Autodesk.AutoCAD
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports AcadNET = Autodesk.AutoCAD
Imports AcadCOM = Autodesk.AutoCAD.Interop
'To resolve Application ambiguities
Imports AcadNETApp = Autodesk.AutoCAD.ApplicationServices.Application
Imports AeccLandLib = Autodesk.AECC.Interop.Land
Imports AeccUiLandLib = Autodesk.AECC.Interop.UiLand
Imports AecUIBase = Autodesk.AEC.Interop.UIBase
Public Class MyCivil3DApp
Private m_oAcadApp As AcadCOM.AcadApplication
Private m_oAeccApp As AeccUiLandLib.IAeccApplication
Private m_oAeccDoc As AeccUiLandLib.IAeccDocument
Private m_oAeccDb As AeccLandLib.IAeccDatabase
<CommandMethod("GP")> _
Public Sub GroundProfile()
Dim ed As Editor = AcadNETApp.DocumentManager.MdiActiveDocument.Editor
Try
m_oAcadApp = AcadNETApp.AcadApplication
m_oAeccApp = m_oAcadApp.GetInterfaceObject("AeccXUiLand.AeccApplication")
Catch
MsgBox("Failed to Load the Civil 3D Application!")
Exit Sub
End Try
Try
m_oAeccDoc = m_oAeccApp.ActiveDocument
m_oAeccDb = m_oAeccApp.ActiveDocument.Database
Catch
MsgBox("No document loaded in Civil3D (zero-doc state)!")
Exit Sub
End Try
Try
Dim oSite As AeccLandLib.AeccSite
oSite = m_oAeccDb.Sites.Item(0)
Dim oAlignment As AeccLandLib.AeccAlignment
oAlignment = oSite.Alignments.Item(0)
Dim originPt As Object
originPt = m_oAcadApp.ActiveDocument.Utility.GetPoint(, _
"Select the Origin Point for the Profile View")
Dim oVStyle As AeccLandLib.AeccProfileViewStyle
'This value is used from the C3D template styles
oVStyle = m_oAeccDb.ProfileViewStyles.Item("Major Grids")
Dim oBandStyle As AeccLandLib.AeccProfileViewBandStyleSet
'This value is used from the C3D template styles
oBandStyle = m_oAeccDb.ProfileViewBandStyleSets.Item("Profile Data & Geometry")
Dim oProfileViews As AeccLandLib.AeccProfileViews
oProfileViews = oAlignment.ProfileViews
oProfileViews.Add("Test Profile View", "0", originPt, oVStyle, oBandStyle)
Dim oProfileStyle As AeccLandLib.AeccProfileStyle
oProfileStyle = m_oAeccDoc.LandProfileStyles.Item("Existing Ground")
Dim oSurface As AeccLandLib.AeccSurface
'You can change the name of the Surface here
oSurface = m_oAeccDb.Surfaces.Item("EG")
Dim startStn As Double
Dim endStn As Double
startStn = oAlignment.StartingStation
endStn = oAlignment.EndingStation
Dim oProfile As AeccLandLib.AeccProfile
oProfile = oAlignment.Profiles.AddFromSurface("Existing Ground Profile", _
AeccLandLib.AeccProfileType.aeccExistingGround, _
oProfileStyle, oSurface.Name, startStn, endStn, "0")
m_oAcadApp = Nothing
m_oAeccApp = Nothing
m_oAeccDoc = Nothing
m_oAeccDb = Nothing
Catch ex As Exception
ed.WriteMessage("Error: ", ex.Message & vbCrLf)
End Try
End Sub
End Class
And that's it for creating a VB.NET application to generate a profile inside Autodesk Civil 3D.