Tuesday, September 15, 2009

Change Layer and Rotation

Sometimes you get a file with text from another source that needs to be put on the correct layer and set to a different rotation. The code below performs the task:

Option Explicit

Sub ChangeLayerAndRotate()

Dim sLayer As String
Dim dRotation As Double

sLayer = ThisDrawing.Utility.GetString(True, "Enter Layer Name: ")

dRotation = ThisDrawing.Utility.GetReal("Enter rotation: ")

GetObject sLayer, dRotation

End Sub

Function GetObject(sLayer As String, dRotation As Double)

Dim oAcadObj As AcadObject
Dim entpt As Variant

On Error GoTo ErrorHandler
ThisDrawing.Utility.GetEntity oAcadObj, entpt, "Pick text/mtext object: "
On Error GoTo 0 ' Turn off error trapping.

If (TypeOf oAcadObj Is AcadText) Then
Dim oText As AcadText
Set oText = oAcadObj

On Error GoTo ErrorHandler
oText.Layer = sLayer
On Error GoTo 0 ' Turn off error trapping.

oText.Rotation = dRotation

ElseIf (TypeOf oAcadObj Is AcadMText) Then
Dim oMText As AcadMText
Set oMText = oAcadObj

On Error GoTo ErrorHandler
oMText.Layer = sLayer
On Error GoTo 0 ' Turn off error trapping.

oMText.Rotation = dRotation

End If

GetObject sLayer, dRotation

Exit Function

Select Case Err.Number
Case -2145386476
Dim Response
Response = MsgBox("Do you want to add the layer to the drawing?, ", vbYesNo)
If Response = vbYes Then
ThisDrawing.Layers.Add sLayer
ThisDrawing.Utility.Prompt "Layer " & sLayer & " added"
Exit Function
End If
Case -2147352567
ThisDrawing.Utility.Prompt "Text/MTEXT not selected" & vbCrLf & "Command: "
Exit Function
Case Else
ThisDrawing.Utility.Prompt Err.Number
MsgBox "Something went wrong"
Exit Function
End Select

End Function

As an added bonus I even added some error catching.

No comments:


Blog Widget by LinkWithin