Click or drag to resize

LayerTableAdd Method (Layer)

Adds a new layer with specified definition to the layer table.

Namespace:  Rhino.DocObjects.Tables
Assembly:  RhinoCommon (in RhinoCommon.dll)
Since: 5.0
Syntax
public int Add(
	Layer layer
)

Parameters

layer
Type: Rhino.DocObjectsLayer
definition of new layer. The information in layer is copied. If layer.Name is empty the a unique name of the form "Layer 01" will be automatically created.

Return Value

Type: Int32
>=0 index of new layer -1 layer not added because a layer with that name already exists.
Examples
partial class Examples
{
  public static Rhino.Commands.Result AddChildLayer(Rhino.RhinoDoc doc)
  {
    // Get an existing layer
    string default_name = doc.Layers.CurrentLayer.Name;

    // Prompt the user to enter a layer name
    Rhino.Input.Custom.GetString gs = new Rhino.Input.Custom.GetString();
    gs.SetCommandPrompt("Name of existing layer");
    gs.SetDefaultString(default_name);
    gs.AcceptNothing(true);
    gs.Get();
    if (gs.CommandResult() != Rhino.Commands.Result.Success)
      return gs.CommandResult();

    // Was a layer named entered?
    string layer_name = gs.StringResult().Trim();
    int index = doc.Layers.Find(layer_name, true);
    if (index<0)
      return Rhino.Commands.Result.Cancel;

    Rhino.DocObjects.Layer parent_layer = doc.Layers[index];

    // Create a child layer
    string child_name = parent_layer.Name + "_child";
    Rhino.DocObjects.Layer childlayer = new Rhino.DocObjects.Layer();
    childlayer.ParentLayerId = parent_layer.Id;
    childlayer.Name = child_name;
    childlayer.Color = System.Drawing.Color.Red;

    index = doc.Layers.Add(childlayer);
    if (index < 0)
    {
      Rhino.RhinoApp.WriteLine("Unable to add {0} layer.", child_name);
      return Rhino.Commands.Result.Failure;
    }
    return Rhino.Commands.Result.Success;
  }
}
Python
import Rhino
import scriptcontext
import System.Drawing.Color

def AddChildLayer():
    # Get an existing layer
    default_name = scriptcontext.doc.Layers.CurrentLayer.Name
    # Prompt the user to enter a layer name
    gs = Rhino.Input.Custom.GetString()
    gs.SetCommandPrompt("Name of existing layer")
    gs.SetDefaultString(default_name)
    gs.AcceptNothing(True)
    gs.Get()
    if gs.CommandResult()!=Rhino.Commands.Result.Success:
        return gs.CommandResult()

    # Was a layer named entered?
    layer_name = gs.StringResult().Trim()
    index = scriptcontext.doc.Layers.Find(layer_name, True)
    if index<0: return Rhino.Commands.Result.Cancel

    parent_layer = scriptcontext.doc.Layers[index]

    # Create a child layer
    child_name = parent_layer.Name + "_child"
    childlayer = Rhino.DocObjects.Layer()
    childlayer.ParentLayerId = parent_layer.Id
    childlayer.Name = child_name
    childlayer.Color = System.Drawing.Color.Red

    index = scriptcontext.doc.Layers.Add(childlayer)
    if index<0:
      print "Unable to add", child_name, "layer."
      return Rhino.Commands.Result.Failure
    return Rhino.Commands.Result.Success

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