Turning Layers on through VBA

Turning Layers on through VBA

Anonymous
Not applicable
3,324 Views
7 Replies
Message 1 of 8

Turning Layers on through VBA

Anonymous
Not applicable

I have written VBA code in AutoCad that will open a certain drawing for the user, but I want it to turn on certain layers based on user input.  Let's say for this example I want to turn on the layer named "Walls".  How would I go about this?  The layer is off when the document is opened.  I'm sure this is a simple couple lines of code but I just can't seem to figure it out. 

0 Likes
Accepted solutions (1)
3,325 Views
7 Replies
Replies (7)
Message 2 of 8

Ed__Jobe
Mentor
Mentor

If you're not concerned about viewport visibility, then its just like below.

    Dim oLay As AcadLayer
    For Each oLay In ThisDrawing.Layers
        If oLay.Name = "Walls" Then
            oLay.LayerOn = True
        End If
    Next

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 3 of 8

Anonymous
Not applicable

Looks like oLay is throwing an error. "Can't find project or library" not sure what is going on there.

0 Likes
Message 4 of 8

Ed__Jobe
Mentor
Mentor
Accepted solution

Are you working in AutoCAD vba? You're not doing this from exel are you?

 

Here is a more efficient version. Although it could throw an error if "Walls" doesn't exist.

 

ThisDrawing.Layers("Walls").LayerOn = True

 

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

Message 5 of 8

grobnik
Collaborator
Collaborator

Hi @Anonymous @Ed__Jobe 

If I have read well, the initial requirement was ".....based on user input....", and question is if would you like to have the same dialog "layer state" windows or a simple user form.

On the opposite @Anonymous accepted Ed solution so the starting request now falls.

I'm wrong ?

Bye

0 Likes
Message 6 of 8

Anonymous
Not applicable

That did it.  I am in AutoCad VBA not excel.

 

thanks for your help!

0 Likes
Message 7 of 8

Anonymous
Not applicable

I had it based on a userform, depending on what the user chooses in a drop box is what layer turns on.  I got it figured out now, sorry for marking the wrong solution I meant to just reply.

 

thanks!

0 Likes
Message 8 of 8

Ed__Jobe
Mentor
Mentor

The only thing I can think of is that it was a typo. Anyway, glad the second one helped.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes