Rhinocommon lock layer

360 views Asked by At

I am trying to avoid that a user locks a layer in a Rhino plugin written using Rhinocommon. When the plugin is initialized, an event handler is coupled to the LayerTableEvent using

RhinoDoc.LayerTableEvent += this.OnLayerEvent;

The event handler is defined as

public void OnLayerEvent(object sender, Rhino.DocObjects.Tables.LayerTableEventArgs e)
{
    if (e.NewState.IsLocked)
    {
        e.Document.Layers[e.LayerIndex].IsLocked = false;
        e.Document.Layers[e.LayerIndex].CommitChanges();
    }
}

The event is triggered when a lock in the Rhino layers panel is clicked and the if condition evaluates to true if the layer gets locked (a breakpoint inside the if statement is reached). However, after the event, the layer remains locked. Is it possible that e.Document.Layers[e.LayerIndex].IsLocked = false; does not lock a layer. Or am I missing something else here?

1

There are 1 answers

0
Luis E. Fraguada On

The documentation is slightly misleading regarding setting the locked state of a layer. In your case for Rhino 5 you will need to create a new layer and modify the layer table. I have not tested your code exactly, but I have tested the general method to create a new Rhino.DocObjects.Layer and use this to Modify the layer table. Here is how this would look like in your code:

public void OnLayerEvent(object sender, Rhino.DocObjects.Tables.LayerTableEventArgs e)
{
    if (e.NewState.IsLocked)
    {
        var newLayerSettings = new Rhino.DocObjects.Layer();
        newLayerSettings.Name = e.Document.Layers[e.LayerIndex].Name;
        newLayerSettings.IsVisible = true;
        e.Document.Layers.Modify(newLayerSettings, e.LayerIndex, true);
    }
}

The procedure to do this has changed for the next version of Rhino to be more like your original example. You can test this in the RhinoWIP if you have a valid Rhino 5 license.

As a last suggestion, you can also ask your question on https://discourse.mcneel.com/c/rhino-developer where you might get more a bit more traffic and probably a faster response to your queries.