Click or drag to resize

LayerName Property

Gets or sets the name of this layer.

Namespace:  Rhino.DocObjects
Assembly:  RhinoCommon (in RhinoCommon.dll)
Since: 5.0
Syntax
public override string Name { get; set; }

Property Value

Type: String
Examples
using Rhino.Commands;

partial class Examples
{
  public static Rhino.Commands.Result SelLayer(Rhino.RhinoDoc doc)
  {
    // Prompt for a layer name
    string layername = doc.Layers.CurrentLayer.Name;
    Result rc = Rhino.Input.RhinoGet.GetString("Name of layer to select objects", true, ref layername);
    if (rc != Rhino.Commands.Result.Success)
      return rc;

    // Get all of the objects on the layer. If layername is bogus, you will
    // just get an empty list back
    Rhino.DocObjects.RhinoObject[] rhobjs = doc.Objects.FindByLayer(layername);
    if (rhobjs == null || rhobjs.Length < 1)
      return Rhino.Commands.Result.Cancel;

    for (int i = 0; i < rhobjs.Length; i++)
      rhobjs[i].Select(true);
    doc.Views.Redraw();
    return Rhino.Commands.Result.Success;
  }
}
Python
import Rhino
import scriptcontext
import System.Guid, System.Drawing.Color

def SelLayer():
    # Prompt for a layer name
    layername = scriptcontext.doc.Layers.CurrentLayer.Name
    rc, layername = Rhino.Input.RhinoGet.GetString("Name of layer to select objects", True, layername)
    if rc!=Rhino.Commands.Result.Success: return rc

    # Get all of the objects on the layer. If layername is bogus, you will
    # just get an empty list back
    rhobjs = scriptcontext.doc.Objects.FindByLayer(layername)
    if not rhobjs: Rhino.Commands.Result.Cancel

    for obj in rhobjs: obj.Select(True)
    scriptcontext.doc.Views.Redraw()
    return Rhino.Commands.Result.Success

if __name__=="__main__":
    SelLayer()
Examples
using Rhino;
using Rhino.Input;
using Rhino.Commands;
using Rhino.DocObjects;
using System;
using System.Linq;

namespace examples_cs
{
  public class RenameLayerCommand : Command
  {
    public override string EnglishName { get { return "csRenameLayer"; } }

    protected override Result RunCommand(RhinoDoc doc, RunMode mode)
    {
      string layer_name = "";
      var rc = RhinoGet.GetString("Name of layer to rename", 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 more 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<Layer>();

      Layer layer_to_rename = 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_rename = 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_rename = matching_layers[selected_layer - 1];
        else return Result.Nothing;
      }

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

      layer_name = "";
      rc = RhinoGet.GetString("New layer name", true, ref layer_name);
      if (rc != Result.Success)
        return rc;
      if (String.IsNullOrWhiteSpace(layer_name))
        return Result.Nothing;

      layer_to_rename.Name = layer_name;
      if (!layer_to_rename.CommitChanges())
        return Result.Failure;
      return Result.Success;
    }
  }
}
Python
import rhinoscriptsyntax as rs
from scriptcontext import doc

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

    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)
        return
    if 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]

    layerName = rs.GetString("New layer name")
    layerToRename.Name = layerName
    layerToRename.CommitChanges()
    return

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