Monday, November 09, 2009

Putting Alignment on Correct Layer

Sometimes things happen and your alignments are not on the correct layer. There really isn’t an easy way within the program to change the layer. We can create some customization that will make the task easier. This post will outline one way to automate the process.

To start out with I’m utilizing the sample alignment code that ships with the product (C:\Program Files (x86)\AutoCAD Civil 3D 2010\Sample\Civil 3D API\COM\Vba\Alignment\). I’ve gone ahead and deleted most of the code I don’t need.

Next I added the code required. First I check to make sure that Civil 3D is running. Next I create the variables need to get the alignment and then create the layer I wanted. Next I got the Object Layer the alignment should be on. For this code I’m assuming that the layer has the –* modifier for the alignment layer.

Next I’ve gone through every object in Model Space and checked to see if it’s an alignment. If it is then I set what the alignment layer name should be and then I check to see if the alignment is already on the correct layer. If the layer isn’t created I then create the layer. If the layer doesn’t exist in the drawing, and we don’t add it, then an error message will result. Then I assign the alignment to the newly created layer.

Sub PutAlignmentOnLayer()
'always get the objects again since MDI is supported
If (GetBaseCivilObjects = False) Then
Exit Sub
End If

Dim oAcadObject As AcadObject
Dim oAlignment As AeccAlignment
Dim sLayer As String
Dim sNewLayer As String

sLayer = g_oDocument.Settings.DrawingSettings.ObjectLayerSettings.AlignmentLayer.Layer

For Each oAcadObject In ThisDrawing.ModelSpace
If (TypeOf oAcadObject Is AeccAlignment) Then
Set oAlignment = oAcadObject
sNewLayer = sLayer & "-" & oAlignment.Name

' Check to see if the alignment is already on the correct layer
If sNewLayer = oAlignment.Layer Then
' The alignment is already on the correct layer.
' Assign the correct layer
ThisDrawing.Layers.Add sNewLayer
oAlignment.Layer = sNewLayer
End If

End If

End Sub

The code could be optimized by only examining the alignments in the sites and siteless, but I’m just doing this as an example. The same thought process could be used for surfaces, profiles or other Civil 3D objects. You can download the code here:

No comments:


Blog Widget by LinkWithin