First, I want to give credit to Drew Burgasser for finding this in the latest update.
How do you export styles with Civil3D using the improved managed .Net API?
The first idea that comes to mind is to use WblockCloneObjects. This is the method used to copy most objects in one drawing’s database to another (i.e., Linetypes, Layers, etc.). However, this method (currently) does not work.
Thankfully, with the latest update to AutoCAD Civil3D 2010 there was more added then what is in the ReadMe. There is a new method added to the Autodesk.Civil.DatabaseServices.Styles.StyleBase class called ExportTo.
So let’s write a routine which will import styles from a selected drawing into the current drawing. The routine is below:
<CommandMethod(“AlignmentStyleImport”, CommandFlags.Session)> _
Public Sub cmdAlignmentStyleImport()
‘Get the file to get the alignments from using Autodesk’s OpenFileDialog
Dim fsb As New Autodesk.AutoCAD.Windows.OpenFileDialog(“Select file to import alignment styles from”, _
“”, “dwg;dwt”, “Import alignment styles”, Autodesk.AutoCAD.Windows.OpenFileDialog.OpenFileDialogFlags.AllowAnyExtension)
‘If there is no drawing file selected, then exit the routine
If fsb.ShowDialog <> Windows.Forms.DialogResult.OK Then Exit Sub
‘Currently there is no way to get to the alignment styles without opening the drawing
‘Thus set the current drawing to a variable to bring up later
Dim dbOut As Database = Application.DocumentManager.MdiActiveDocument.Database
‘Now open the drawing from the file dialog
Dim aDocOut As Document = Application.DocumentManager.Open(fsb.Filename)
‘Get all the alignment styles objectId collection
‘First we get the CivilDocument object
Dim docIn As Autodesk.Civil.ApplicationServices.CivilDocument = Autodesk.Civil.ApplicationServices.CivilApplication.ActiveDocument
Dim objIds As ObjectIdCollection = New ObjectIdCollection
‘Now iterate through the alignment style object collection and get their objectIds
For i As Integer = 0 To docIn.Styles.AlignmentStyles.Count – 1
‘Finally export the styles out to the beginning drawing
Autodesk.Civil.DatabaseServices.Styles.StyleBase.ExportTo(objIds, dbOut, Autodesk.Civil.StyleConflictResolverType.Override)
‘Switch your first drawing as the active drawing
Dim aDocTemp As Document = Application.DocumentManager.GetDocument(dbOut)
Application.DocumentManager.MdiActiveDocument = aDocTemp
‘Finally close the drawing you got the styles from