Lock Violation when hiding layer

This widget could not be displayed.

Lock Violation when hiding layer

Anonymous
Not applicable

I have three subroutines in my macro (code below):

- myDraw to draw a line

- myOff to hide the line's layer

- myOn to show the line's layer

 

When I'm in AutoCAD and I call myDraw  using -VBARUN, the line is drawn. When I then call myOff, I get a Lock Violation runtime error. When I click End on the popup, I can use myOff and myOn without problems. When I delete the line and re-call myDraw, I get the Lock Violation again.

 

The error does not occur when I run the subroutines from within the editor using "Run (F5)".

 

I am using AutoCAD 2020 on Windows 10

 

Code:

Public Sub myDraw()
  Dim pLine As AcadPolyline
  Dim LinePoints(0 To 5) As Double

  LinePoints(0) = -100
  LinePoints(1) = 100
  LinePoints(2) = 0

  LinePoints(3) = 0
  LinePoints(4) = 0
  LinePoints(5) = 0
  
  Set pLine = ThisDrawing.ModelSpace.AddPolyline(LinePoints)
  pLine.Layer = "0"
End Sub

Public Sub myOn()
  ThisDrawing.Layers("0").LayerOn = True
End Sub

Public Sub myOff()
  ThisDrawing.Layers("0").LayerOn = False
End Sub
0 Likes
Reply
Accepted solutions (1)
1,477 Views
4 Replies
Replies (4)

ed57gmc
Mentor
Mentor

Put the line on a layer other than "0". That layer is a special layer reserved for use by AutoCAD. Never lock that layer or turn it off. The same is true for "Defpoints". Its best if you ignore those layers and do not use them. The exception is when you are creating blocks. You can put entities on layer 0 if you want them to take on the layer properties of the layer that the block is inserted on.

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

Anonymous
Not applicable

Thanks for your reply, Ed

 

I created a new layer called myLayer and changed the macro to use "myLayer" instead of "0". However, I get the same behaviour with the Lock Violation error...

 

Any other idea?

 

Regards,

Frank

0 Likes

ed57gmc
Mentor
Mentor
Accepted solution

Is the layer the current layer when you try to turn it off? You need to make some other layer active before you turn it off. I tried your code and I don't get an error.

 

Also, you need to add a regen to the on/off subs.

ThisDrawing.Regen acAllViewports

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

Anonymous
Not applicable

Amazing, thank you very much Ed

 

Adding ThisDrawing.Regen acAllViewports above the line that threw the error resolved the issue. I had it occur in multiple subroutines and it resolved it in every case!

0 Likes