Wednesday, November 25, 2009

Rotate Headwall

When you move or adjust a location of a headwall it doesn’t remain perpendicular to a pipe like you may want. This post will create a quick VBA macro to rotate the headwall back into place. The code has the user select a structure and then rotates it depending on if it is attached to the start of the pipe or the end of the pipe. The rotation is done in radians, so the rotation is pi.

Option Explicit

' Command to rotate headwall to be 90 degrees from pipe.
Sub RotateHeadwall()
' Always get the objects again since MDI is supported.
If (GetBasePipeObjects = False) Then
MsgBox "Error accessing base civil objects."
Exit Sub
End If

On Error Resume Next

' Select headwall
Dim oStructure As AeccStructure
Dim vPoint As Variant
Dim oAcadObj As AcadObject

ThisDrawing.Utility.GetEntity oAcadObj, vPoint, "Select Headwall to rotate: "

If (TypeOf oAcadObj Is AeccStructure) Then
Set oStructure = oAcadObj
If oStructure.ConnectedPipesCount = 0 Then
' THe structure is not connected to a pipe.
MsgBox "The structure is not connected to a pipe."
End If

Dim oPipe As AeccPipe
Set oPipe = oStructure.ConnectedPipe(0)
Dim dRotation As Double
Dim dStartPt(0 To 2) As Double
Dim dEndPt(0 To 2) As Double
dStartPt(0) = oPipe.StartPoint.X: dStartPt(1) = oPipe.StartPoint.Y
dEndPt(0) = oPipe.EndPoint.X: dEndPt(1) = oPipe.EndPoint.Y

If oPipe.StartStructure.ObjectID = oStructure.ObjectID Then
dRotation = ThisDrawing.Utility.AngleFromXAxis(dStartPt, dEndPt) + (4 * Atn(1)) ' Added 180 degrees to the value
dRotation = ThisDrawing.Utility.AngleFromXAxis(dStartPt, dEndPt)
End If
oStructure.Rotation = dRotation
End If

End Sub

Once compiled you can then add the command to the cui and add it to the right click menu for a structure. Here’s a short video showing the command in action and added to the shortcut menu for a structure object.

It could use some improvement by allowing it to use an already selected object. The code may be found here:

More information on creating a command in the cui may be found in this previous post.


Anonymous said...

When I attempt to run this code I get a 'compile error: Cant find project or library' and a highlight of the g_opipeapplication line. I am using Civil 3d 2010. Any ideas?


Christopher Fugitt said...

It may be a problem with Excel (you don't have 2007 installed). Type VBAIDE at the command line. Go to Tools, References and make sure all of them are there. If it can't find the Excel one, uncheck it. It should run after that.


Blog Widget by LinkWithin