Visual Basic Customization

Visual Basic Customization

Reply
Active Contributor
lanieuwe
Posts: 40
Registered: ‎10-26-2011
Message 1 of 7 (337 Views)
Accepted Solution

For next slow why?

337 Views, 6 Replies
12-22-2012 10:28 AM

Why is the for next so slow?

 

I have a form with combobox filled up with 40 layers. The layers are from the layermanager.

The form has a command button too.

When I run this code. It's very slow to lock all layers.

Is there a solution for that?

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

 gr. Laszlo

 

 

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 2 of 7 (320 Views)

Re: For next slow why?

12-24-2012 02:03 PM in reply to: lanieuwe
I repeat again, definelayer collection once then iterate through it, eg.: Option Explicit Private Sub CommandButton1_Click() On Error Resume Next 'handle exceptions inline Dim objlayers As AcadLayers Dim objlayer As AcadLayer Set objlayers = ThisDrawing.Layers 'make the layer selected in the combobox current ThisDrawing.ActiveLayer = objlayers.Item(ComboBox1.Text) For Each objlayer In objlayers objlayer.LayerOn = False 'turn off all the layers Next objlayer Set objlayer = objlayers.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 MsgBox "Done" 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
_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Active Contributor
lanieuwe
Posts: 40
Registered: ‎10-26-2011
Message 3 of 7 (310 Views)

Re: For next slow why?

12-24-2012 11:28 PM in reply to: Hallex

Thank you Hallex for patient.

Its still slow but maybe an other way than you mean.

The problem is the for...next:

For Each objlayer In objlayers
objlayer.Lock = True 'lock all the layers
Next objlayer. 

I have 40 layers.

Maybe I have to use the sendcommand  method for that?

ps. I changed "turn off" in "lock = true" too:smileyhappy:

Have a nice christmas!

 

Option Explicit
Private Sub CommandButton1_Click()

On Error Resume Next 'handle exceptions inline
Dim objlayers As AcadLayers
Dim objlayer As AcadLayer
Set objlayers = ThisDrawing.Layers 'make the layer selected in the combobox current
ThisDrawing.ActiveLayer = objlayers.Item(ComboBox1.Text)
For Each objlayer In objlayers
objlayer.Lock = True 'lock all the layers
Next objlayer
Set objlayer = objlayers.Item(ComboBox1.Text)
If objlayer Is Nothing Then
MsgBox "layer does not exist"
Exit Sub 'exit if layer not found
End If

objlayer.Lock = False 'unlock the desired layer MsgBox "Done"
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

 

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 4 of 7 (304 Views)

Re: For next slow why?

12-24-2012 11:51 PM in reply to: lanieuwe
Hi gr. Laszlo, I tested my code on drawing with about 125 layers for the test seems it's working very fast (something like less then 0.5 second) No problem if your code with SendCommand will do your job though Happy Holydays, ~'J'~
_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Moderator
Alexander.Rivilis
Posts: 1,406
Registered: ‎04-09-2008
Message 5 of 7 (282 Views)

Re: For next slow why?

12-28-2012 02:01 AM in reply to: lanieuwe

Are you using AutoCAD x64?

64 Bit VBA Performance Issue


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Active Contributor
lanieuwe
Posts: 40
Registered: ‎10-26-2011
Message 6 of 7 (270 Views)

Re: For next slow why?

12-28-2012 12:00 PM in reply to: lanieuwe

Yes I do.....

???

Moderator
Alexander.Rivilis
Posts: 1,406
Registered: ‎04-09-2008
Message 7 of 7 (266 Views)

Re: For next slow why?

12-28-2012 12:24 PM in reply to: lanieuwe

lanieuwe wrote:

Yes I do.....

???


Then read: http://adndevblog.typepad.com/autocad/2012/05/64-bit-vba-performance-issue.html


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

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 or visit the Installation and Licensing Forum to get help installing your software.