Lock layers

Lock layers

julian.schoenenberger
Explorer Explorer
972 Views
3 Replies
Message 1 of 4

Lock layers

julian.schoenenberger
Explorer
Explorer

Hi, is there a simpler solution to automatically lock a layer that has been unlocked by the user? I have some layers that I do not want to be unlocked.
I currently have a solution that uses OnCommandEnded, but it is not as elegant as I want it to be.

I thought about an Eventhandler attached to the specific layers, but all my tests failed into AutoCAD crashing as the layer was already being modified by AutoCAD (if I understood correctly), delaying the modification didn't work either. 

Thanks

0 Likes
Accepted solutions (2)
973 Views
3 Replies
Replies (3)
Message 2 of 4

norman.yuan
Mentor
Mentor
Accepted solution

The possible solution is not to relock the layer after users unlock it.

 

You might want to try to use ObjectOverrule to prevent the layer being unlocked, that is, in the overridden Close() method, you can test if the target layer that being opened for writing is unlocked, if it is, throw an AutoCAD runtime exception, which would effectively abort the change. You might want to let users be aware the layer unlock is denied by your code, so that they are not confused as why AutoCAD does not work properly as they thought.

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 4

BlackBox_
Advisor
Advisor

This sounds like a training / drawing management issue.

 

If the layers must reside in a drawing the user has modify access to on your server, train them. 

 

If the layers can exist outside of the drawing they have modify access to on your server, consider placing the layers you don't want modified in a separate drawing, that they can XREF, but do not have modify access to.


"How we think determines what we do, and what we do determines what we get."

Sincpac C3D ~ Autodesk Exchange Apps

Message 4 of 4

ActivistInvestor
Mentor
Mentor
Accepted solution

Long ago, I built a library of overrules for cadd standards enforcement, and one of them targets layers.

 

While the existing code didn't specifically target your use case, It was trivial to modify it to do what you need. 

 

The code below also requires the code in this repository folder.

 

 

   /// <summary>
   /// Example showing how to use the LayerOverrule
   /// base type (included in LayerOverrule.cs).
   /// 
   /// This example prevents layers from being locked
   /// only if their name starts with "NOLOCK", which
   /// is just an example. Real-world application of
   /// this class can involve realistic criteria.
   /// 
   /// Issue:
   /// 
   /// The Layer properties manager does not update to
   /// display the correct locked/unlocked state of a
   /// layer which the user tried to lock via the layer
   /// manager dialog. 
   /// 
   /// So, if the user tries to lock a layer that this
   /// overrule does not allow them to lock, the layer
   /// properties manager will show the layer as being
   /// locked, even thought it is not locked.
   /// 
   /// The LayerManagerThunk class included in the code
   /// file required by this example addresses that issue,
   /// by forcing the LayerManager control to update when
   /// the user tries to lock a layer that can't be locked.
   /// 
   /// </summary>

   public class MyNoLockLayerOverrule : LayerOverrule
   {
      public MyNoLockLayerOverrule() : base("NOLOCK*")
      {
      }

      /// <summary>
      /// The base type filters the layers by their names
      /// according to the string passed to its constructor
      /// above, so this method will only be called if the 
      /// layer's name matches the wildcard pattern "NOLOCK*".
      /// 
      /// You can change the pattern to meet your needs or
      /// you can override the IsMatch() method to directly
      /// specify what layers the overrule should act on.
      /// </summary>

      protected override void OnLayerModified(LayerTableRecord layer)
      {
         if(layer.IsLocked)
         {
            layer.IsLocked = false;
            LayerManagerThunk.UpdateLayerManager(true);
            Async.WriteLine($"\nALERT: Layer {layer.Name} cannot be locked!");
         }
      }
   }

 

 

0 Likes