Click or drag to resize
LayerIsLocked Property
Gets or sets a value indicating the locked state of this layer.

Namespace: Rhino.DocObjects
Assembly: RhinoCommon (in RhinoCommon.dll) Version: 5.1.50000.0 (5.0.20693.0)
Syntax
public bool IsLocked { get; set; }

Property Value

Type: Boolean
Remarks
If you are modifying a layer inside a Rhino document, you must call CommitChanges for the modifications to take effect.
Examples
using Rhino;
using Rhino.Input;
using Rhino.Commands;
using System;
using System.Linq;

namespace examples_cs
{
  public class LockLayerCommand : Command
  {
    public override string EnglishName { get { return "csLockLayer"; } }

    protected override Result RunCommand(RhinoDoc doc, RunMode mode)
    {
      string layer_name = "";
      var rc = RhinoGet.GetString("Name of layer to lock", true, ref layer_name);
      if (rc != Result.Success)
        return rc;
      if (String.IsNullOrWhiteSpace(layer_name))
        return Result.Nothing;

      // because of sublayers it's possible that mone than one layer has the same name
      // so simply calling doc.Layers.Find(layerName) isn't good enough.  If "layerName" returns
      // more than one layer then present them to the user and let him decide.
      var matching_layers = (from layer in doc.Layers
                             where layer.Name == layer_name
                             select layer).ToList<Rhino.DocObjects.Layer>();

      Rhino.DocObjects.Layer layer_to_lock = null;
      if (matching_layers.Count == 0)
      {
        RhinoApp.WriteLine("Layer \"{0}\" does not exist.", layer_name);
        return Result.Nothing;
      }
      else if (matching_layers.Count == 1)
      {
        layer_to_lock = matching_layers[0];
      }
      else if (matching_layers.Count > 1)
      {
        for (int i = 0; i < matching_layers.Count; i++)
        {
          RhinoApp.WriteLine("({0}) {1}", i+1, matching_layers[i].FullPath.Replace("::", "->"));
        }
        int selected_layer = -1;
        rc = RhinoGet.GetInteger("which layer?", true, ref selected_layer);
        if (rc != Result.Success)
          return rc;
        if (selected_layer > 0 && selected_layer <= matching_layers.Count)
          layer_to_lock = matching_layers[selected_layer - 1];
        else return Result.Nothing;
      }

      if (layer_to_lock == null)
        return Result.Nothing;

      if (!layer_to_lock.IsLocked)
      {
        layer_to_lock.IsLocked = true;
        layer_to_lock.CommitChanges();
        return Result.Success;
      }
      else
      {
        RhinoApp.WriteLine("layer {0} is already locked.", layer_to_lock.FullPath);
        return Result.Nothing;
      } 
    }
  }
}
import rhinoscriptsyntax as rs
from scriptcontext import doc

def lock():
    layerName = rs.GetString("Name of layer to lock")

    matchingLayers = [layer for layer in doc.Layers if layer.Name == layerName]

    layerToLock = None
    if len(matchingLayers) == 0:
        print "Layer \"{0}\" does not exist.".format(layerName)
    elif len(matchingLayers) == 1:
        layerToLock = matchingLayers[0]
    elif len(matchingLayers) > 1:
        i = 0;
        for layer in matchingLayers:
            print "({0}) {1}".format(i+1, matchingLayers[i].FullPath.replace("::", "->"))
            i += 1

        selectedLayer = rs.GetInteger("which layer?", -1, 1, len(matchingLayers))
        if selectedLayer == None:
            return
        layerToLock = matchingLayers[selectedLayer - 1]

    if layerToLock.IsLocked:
        print "layer {0} is already locked.".format(layerToLock.FullPath)
    else:
        layerToLock.IsLocked = True
        layerToLock.CommitChanges()

if __name__ == "__main__":
    lock()
See Also