'Error Setting Active Layer' occurs while deleting Layer

'Error Setting Active Layer' occurs while deleting Layer

Anonymous
Not applicable
1,572 Views
2 Replies
Message 1 of 3

'Error Setting Active Layer' occurs while deleting Layer

Anonymous
Not applicable

I m creating temporary new layer to put some mtext objects.  Later im deleting this layer through VBA macros. But it shows error on setting active layer '0" before deleting all objects and new layer. If i execute the same delete layer macros again, this time it doesnt show error. I got this code from help websites. 

Private Sub Ch4_NewLayer()
 Dim acadApp As AcadApplication
Dim acadDoc As AcadDocument
Dim newLayer As AcadLayer
Dim LayerName As String
'Check if AutoCAD is open. On Error Resume Next Set acadApp = GetObject(, "AutoCAD.Application") On Error GoTo 0 'If AutoCAD is not opened create a new instance and make it visible. If acadApp Is Nothing Then Set acadApp = New AcadApplication acadApp.Visible = True End If 'Check if there is an active drawing. On Error Resume Next Set acadDoc = acadApp.ActiveDocument On Error GoTo 0 'No active drawing found. Create a new one. If acadDoc Is Nothing Then Set acadDoc = acadApp.Documents.Add acadApp.Visible = True End If 'check to see if layer already exists and if not make a new On Error Resume Next LayerName = "Layer_1" Set newLayer = acadDoc.Layers.Add(LayerName) newLayer.color = acYellow On Error GoTo 0 If newLayer Is Nothing Then Set newLayer = acadDoc.Layers.Add(LayerName) End If acadDoc.ActiveLayer = newLayer ' Create a Sample circle
Dim circleObj As AcadCircle
Dim center(0 To 2) As Double
Dim radius As Double
center(0) = 2: center(1) = 2: center(2) = 0
radius = 1
Set circleObj = ThisDrawing.ModelSpace.AddCircle(center, radius) End Sub Public Sub DeleteLayer() ThisDrawing.ActiveLayer = ThisDrawing.Layers("0") '<-- it shows error here DeleteEntitiesOnLayer ("Layer_1") ThisDrawing.Layers("Layer_1").Delete End Sub
0 Likes
1,573 Views
2 Replies
Replies (2)
Message 2 of 3

norman.yuan
Mentor
Mentor

Where does this VBA code run? Inside AutoCAD, or run from Other application's VBA (MS Word, Excel, Access...)? If it is AutoCAD VBA code, why do you need to test whether AutoCAD is open/running or not? In runs inside AutoCAD, shouldn't AutoCAD be running BEFORE you can execute your VBA macro? In AutoCAD VBA, AcadApplication is ALWAYS available (because of AutoCAD is running).

 

Also, the code to check if a layer alreadu exists is wrong:

 

On Error Resume Next
    LayerName = "Layer_1"
    Set newLayer = acadDoc.Layers.Add(LayerName)
    newLayer.color = acYellow
On Error GoTo 0

If newLayer Is Nothing Then
    Set newLayer = acadDoc.Layers.Add(LayerName)
End If
acadDoc.ActiveLayer = newLayer

The red line should have been:

 

On Error Resume Next

LayerName="Layer_1"

Set newLayer = acadDoc.Layer(LayerName)

On Error GoTo 0

If newLayer Is Nothing Then

    Set newLayer=acadDoc.Layers.Add(LayerName)

End if

newLayer.Color=acYellow

acadDoc.ActiveLayer = newLayer

 

In this case, if the layer does not exists, the code would raise error and go on to next line

 

As for the error when set active layer to "0", since you did not show all relevant code (where the DeleteLayer() method is called), or you describe what the error is (err description). I can only guess:

1. If your code runs outside AutoCAD (from Word/Excel/Access VBA), how do you get the object ThisDrawing, which ONLY directly available in AutoCAD VBA. However, if your code does run as AutoCAD VBA, ThisDrawing should be good. 

2. Is layer "0" forzen, or turned off when the code runs?

 

That is, either you do not have a legitimate "ThisDrawing" object avilable, or layer "0" is frozon/turned off.

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 3

Anonymous
Not applicable

Im calling VBA macros inside autocad drawing. I took this new layer creation code form VBA tutorial.  And also i  need handle of active document of the autocad.  Im deleting these layer by using another macro code. Exactly what im doing ..I have one macro in startup, to create new layer and plotting mtext with the block information taken from layer"0". Then once user dont want this new layer, they may delete this new layer ,now they have only Layer'0' which having original drawing and that will be active drawing.

 

So one macro  AcadStartup() have creating new layer with block information.

 Another macro DeleteLayer() to delete the layer

Public Sub AcadStartup()
      Call Ch4_NewLayer
End Sub


'Another macro 
Public Sub DeleteLayer()
ThisDrawing.ActiveLayer = ThisDrawing.Layers("0")
DeleteEntitiesOnLayer ("Layer_1")
ThisDrawing.Layers("Layer_1").Delete

End Sub

 

Now i have changed code for adding new layer like below.

LayerName = "Layer_1"
    Set newLayer = ThisDrawing.Layers(LayerName)
    newLayer.color = acYellow
On Error GoTo 0
If newLayer Is Nothing Then
    Set newLayer = ThisDrawing.Layers.Add(LayerName)
End If
ThisDrawing.ActiveLayer = newLayer

 

0 Likes