VBA
Discuss AutoCAD ActiveX and VBA (Visual Basic for Applications) questions here.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

All layers locked except the active layer chosen in combobox?

4 REPLIES 4
Reply
Message 1 of 5
lanieuwe
651 Views, 4 Replies

All layers locked except the active layer chosen in combobox?

Hi!

Can someone help me a bit?

2 questions.

I have a form with a combobox and a commandbutton.

 

  1. All layers in the layermanager must be locked except the active layer chosen in the combobox.

 I try it with this code but it’s not the right solution.

Private Sub CommandButton1_Click()
Dim layer As AcadLayer
'check if all layers are locked
If layer.Lock = False Then
layer.Lock = True
'make the layer selected in the combobox current
ThisDrawing.ActiveLayer = ThisDrawing.Layers.Item(ComboBox1.Text)
End Sub

Private Sub UserForm_Initialize()
  Dim layerColl As AcadLayers
 Dim layer As AcadLayer
 Dim LayList As String
  ' all layers locked
 For Each layer In ThisDrawing.Layers
    If layer.Lock = False Then
    layer.Lock = True
    End If
Next
  'fill combobox
 Set layerColl = ThisDrawing.Layers
 For Each layer In layerColl
    ComboBox1.AddItem layer.Name
 Next
 End Sub

 2. When I choose the for next method to lock all layers (40 layers) it’s very slow.

Is there a solution for that?

 

Gr. Laszlo

4 REPLIES 4
Message 2 of 5
lanieuwe
in reply to: lanieuwe

The first question I answered my self. I turned te code into this:

Private Sub CommandButton1_Click()
On Error Resume Next    'handle exceptions inline
Dim objlayer As AcadLayer

'make the layer selected in the combobox current
ThisDrawing.ActiveLayer = ThisDrawing.Layers.Item(ComboBox1.Text)
For Each objlayer In ThisDrawing.Layers
    objlayer.LayerOn = False    'turn off all the layers
Next objlayer
Set objlayer = ThisDrawing.Layers.Item(ComboBox1.Text)
If objlayer Is Nothing Then
    MsgBox "layer does not exist"
    Exit Sub    'exit if layer not found
End If

objlayer.LayerOn = True 'turn on the desired layer
End Sub

Private Sub UserForm_Initialize()
 Dim layerColl As AcadLayers
 Dim objlayer As AcadLayer


  'fill combobox
 Set layerColl = ThisDrawing.Layers
 For Each objlayer In layerColl
    ComboBox1.AddItem objlayer.Name
 Next
 End Sub

 It is working but,

My second question: Why is it so slowly to locked all layers? Smiley Sad Is there a solution?

 

gr Laszlo

 

Message 3 of 5
Hallex
in reply to: lanieuwe

Just on a quick glance Why are you iterating through the layers collection this way, it's a reason of slow work: For Each objlayer In ThisDrawing.Layers objlayer.LayerOn = False 'turn off all the layers Next objlayer Better yet declare layer collection first instead, i.e.: Dim objLayers as AcadLayers Set objLayers=ThisDrawing.Layers Then do the same: For Each objlayer In objLayers objlayer.LayerOn = False 'turn off all the layers Next objlayer
_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 4 of 5
lanieuwe
in reply to: Hallex

Is this what you mean?

Private Sub CommandButton1_Click()
On Error Resume Next    'handle exceptions inline
Dim objlayer As AcadLayer

'make the layer selected in the combobox current
ThisDrawing.ActiveLayer = ThisDrawing.Layers.Item(ComboBox1.Text)
For Each objlayer In ThisDrawing.Layers
    objlayer.LayerOn = False    'turn off all the layers
Next objlayer

Dim objLayers As AcadLayers
Set objLayers = ThisDrawing.Layers

For Each objlayer In objLayers
objlayer.LayerOn = False 'turn off all the layers
Next objlayer

Set objlayer = ThisDrawing.Layers.Item(ComboBox1.Text)
If objlayer Is Nothing Then
    MsgBox "layer does not exist"
    Exit Sub    'exit if layer not found
End If

objlayer.LayerOn = True 'turn on the desired layer
End Sub

Private Sub UserForm_Initialize()
 Dim layerColl As AcadLayers
 Dim objlayer As AcadLayer


  'fill combobox
 Set layerColl = ThisDrawing.Layers
 For Each objlayer In layerColl
    ComboBox1.AddItem objlayer.Name
 Next
 End Sub

 It still slow!

gr. László

 

 

Message 5 of 5
lanieuwe
in reply to: lanieuwe

Above wrong code sorry!

See this code it's still slow (For next is slow)

Private Sub CommandButton1_Click()
On Error Resume Next    'handle exceptions inline
Dim objlayer As AcadLayer
Dim objLayers As AcadLayers
Set objLayers = ThisDrawing.Layers

For Each objlayer In objLayers
objlayer.LayerOn = False 'turn off all the layers
Next objlayer

Set objlayer = ThisDrawing.Layers.Item(ComboBox1.Text)
If objlayer Is Nothing Then
    MsgBox "layer does not exist"
    Exit Sub    'exit if layer not found
End If

objlayer.LayerOn = True 'turn on the desired layer
End Sub

Private Sub UserForm_Initialize()
 Dim layerColl As AcadLayers
 Dim objlayer As AcadLayer


  'fill combobox
 Set layerColl = ThisDrawing.Layers
 For Each objlayer In layerColl
    ComboBox1.AddItem objlayer.Name
 Next
 End Sub

 help?

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

”Boost