Visual Basic Customization

Reply
Active Contributor
lanieuwe
Posts: 40
Registered: ‎10-26-2011
Message 1 of 5 (232 Views)

All layers locked except the active layer chosen in combobox?

232 Views, 4 Replies
12-21-2012 07:47 AM

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

Active Contributor
lanieuwe
Posts: 40
Registered: ‎10-26-2011
Message 2 of 5 (222 Views)

Re: All layers locked except the active layer chosen in combobox?

12-21-2012 12:13 PM 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? :smileysad: Is there a solution?

 

gr Laszlo

 

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 3 of 5 (213 Views)

Re: All layers locked except the active layer chosen in combobox?

12-23-2012 01:19 AM 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
Active Contributor
lanieuwe
Posts: 40
Registered: ‎10-26-2011
Message 4 of 5 (202 Views)

Re: All layers locked except the active layer chosen in combobox?

12-23-2012 11:09 AM 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ó

 

 

Active Contributor
lanieuwe
Posts: 40
Registered: ‎10-26-2011
Message 5 of 5 (190 Views)

Re: All layers locked except the active layer chosen in combobox?

12-23-2012 10:35 PM 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?

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community