Site Tools


Differences

This shows you the differences between two versions of the page.

Link to this comparison view

developer:rhinocommonsamples:locklayer [2015/09/14] (current)
Line 1: Line 1:
 +===== Sample: Lock a Layer =====
 +
 +===== C# =====
 +<code c#>
 +[System.Runtime.InteropServices.Guid("​A77507C3-DEEE-4A2C-ADB3-3FFAF89B7EDD"​)]
 +public class LockLayerCommand : Command
 +{
 +  public override string EnglishName { get { return "​csLockLayer";​ } }
 +
 +  protected override Result RunCommand(RhinoDoc doc, RunMode mode)
 +  {
 +    string layerName = "";​
 +    var rc = RhinoGet.GetString("​Name of layer to lock", true, ref layerName);
 +    if (rc != Result.Success)
 +      return rc;
 +    if (String.IsNullOrWhiteSpace(layerName))
 +      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 matchingLayers = (from layer in doc.Layers
 +                          where layer.Name == layerName
 +                          select layer).ToList<​Rhino.DocObjects.Layer>​();​
 +
 +    Rhino.DocObjects.Layer layerToRename = null;
 +    if (matchingLayers.Count == 0)
 +    {
 +      RhinoApp.WriteLine(String.Format("​Layer \"​{0}\"​ does not exist.",​ layerName));​
 +      return Result.Nothing;​
 +    }
 +    else if (matchingLayers.Count == 1)
 +    {
 +      layerToRename = matchingLayers[0];​
 +    }
 +    else if (matchingLayers.Count > 1)
 +    {
 +      for (int i = 0; i < matchingLayers.Count;​ i++)
 +      {
 +        RhinoApp.WriteLine(String.Format("​({0}) {1}", i+1, matchingLayers[i].FullPath.Replace("::",​ "​->"​)));​
 +      }
 +      int selectedLayer = -1;
 +      rc = RhinoGet.GetInteger("​which layer?",​ true, ref selectedLayer);​
 +      if (rc != Result.Success)
 +        return rc;
 +      if (selectedLayer > 0 && selectedLayer <= matchingLayers.Count)
 +        layerToRename = matchingLayers[selectedLayer - 1];
 +      else return Result.Nothing;​
 +    }
 +
 +    if (layerToRename == null)
 +      return Result.Nothing;​
 +
 +    if (!layerToRename.IsLocked)
 +    {
 +      layerToRename.IsLocked = true;
 +      layerToRename.CommitChanges();​
 +      return Result.Success;​
 +    }
 +    else
 +    {
 +      RhinoApp.WriteLine(String.Format("​layer {0} is already locked.",​ layerToRename.FullPath));​
 +      return Result.Nothing;​
 +    } 
 +  }
 +}
 +</​code>​
 +===== VB.NET =====
 +<code vb>
 +<​System.Runtime.InteropServices.Guid("​5F44BAEA-6675-46AB-8221-77BD341C5563"​)>​ _
 +Public Class LockLayerCommand
 +  Inherits Command
 +  Public Overrides ReadOnly Property EnglishName() As String
 +    Get
 +      Return "​vbLockLayer"​
 +    End Get
 +  End Property
 +
 +  Protected Overrides Function RunCommand(doc As RhinoDoc, mode As RunMode) As Result
 +    Dim layerName As String = ""​
 +    Dim rc = Rhino.Input.RhinoGet.GetString("​Name of layer to lock", True, layerName)
 +    If rc <> Result.Success Then
 +      Return rc
 +    End If
 +    If [String].IsNullOrWhiteSpace(layerName) Then
 +      Return Result.[Nothing]
 +    End If
 +
 +    ' 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.
 +    Dim matchingLayers = (From layer In doc.Layers Where layer.Name = layerName Select layer).ToList()
 +
 +    Dim layerToRename As Rhino.DocObjects.Layer = Nothing
 +    If matchingLayers.Count = 0 Then
 +      RhinoApp.WriteLine([String].Format("​Layer ""​{0}""​ does not exist.",​ layerName))
 +      Return Result.[Nothing]
 +    ElseIf matchingLayers.Count = 1 Then
 +      layerToRename = matchingLayers(0)
 +    ElseIf matchingLayers.Count > 1 Then
 +      For i As Integer = 0 To matchingLayers.Count - 1
 +        RhinoApp.WriteLine([String].Format("​({0}) {1}", i + 1, matchingLayers(i).FullPath.Replace("::",​ "​->"​)))
 +      Next
 +      Dim selectedLayer As Integer = -1
 +      rc = Rhino.Input.RhinoGet.GetInteger("​which layer?",​ True, selectedLayer)
 +      If rc <> Result.Success Then
 +        Return rc
 +      End If
 +      If selectedLayer > 0 AndAlso selectedLayer <= matchingLayers.Count Then
 +        layerToRename = matchingLayers(selectedLayer - 1)
 +      Else
 +        Return Result.[Nothing]
 +      End If
 +    End If
 +
 +    If layerToRename Is Nothing Then
 +      Return Result.Nothing
 +    End If
 +
 +    If Not layerToRename.IsLocked Then
 +      layerToRename.IsLocked = True
 +      layerToRename.CommitChanges()
 +      Return Result.Success
 +    Else
 +      RhinoApp.WriteLine([String].Format("​layer {0} is already locked.",​ layerToRename.FullPath))
 +      Return Result.[Nothing]
 +    End If
 +  End Function
 +End Class
 +d Namespace
 +</​code>​
 +===== Python =====
 +<code python>
 +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]
 +    ​
 +    layerToRename = None
 +    if len(matchingLayers) == 0:
 +        print "Layer \"​{0}\"​ does not exist."​.format(layerName)
 +    elif len(matchingLayers) == 1:
 +        layerToRename = 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
 +        layerToRename = matchingLayers[selectedLayer - 1]
 +        ​
 +    if layerToRename.IsLocked:​
 +        print "layer {0} is already locked."​.format(layerToRename.FullPath)
 +    else:
 +        layerToRename.IsLocked = True
 +        layerToRename.CommitChanges()
 +          ​
 +if __name__ == "​__main__":​
 +    lock()
 +        ​
 +</​code>​
 +
 +{{tag>​Developer rhinocommon}}
  
developer/rhinocommonsamples/locklayer.txt ยท Last modified: 2015/09/14 (external edit)