Layer Table update?

I have two functions.

  1. generates a Layer if it is not already in the Layer list:
Code
    public static Layer getLayer(string _layerName, System.Drawing.Color C)
    {
        Rhino.RhinoDoc doc = Rhino.RhinoDoc.ActiveDoc;
        Layer _Layer = doc.Layers.FindName(_layerName);
        if (_Layer == null)
        {
            _Layer = new Layer();
            _Layer.Name = _layerName;
            _Layer.Color = C;
            doc.Layers.Add(_Layer);
        }
        else
        {
            Layer[\*] children = _Layer.GetChildren();
            if (children != null)
            {
                foreach (Layer L in children)
                {
                    PPMStatic.clearLayer(L);
                    doc.Layers.Delete(L.Index, true);
                }
            }
        }
        doc.Layers.SetCurrentLayerIndex(_Layer.Index, true);
        return _Layer;
    }
  1. bakes blocks on individual layers and uses the layer created with the first function as parent layer:
Code

public static Guid bakeObject(GeometryBase geom, Plane planes, String Name, Layer parent = null)
{
Rhino.RhinoDoc doc = Rhino.RhinoDoc.ActiveDoc;
// delet old block definition…
string blockName = Name;
InstanceDefinition blockDef = doc.InstanceDefinitions.Find(blockName);
int blockID = -1;
if (blockDef != null)
{
doc.InstanceDefinitions.Delete(blockDef.Index, true, true);
}
ObjectAttributes _Att = PPMStatic.getObjectAttributes(Name, System.Drawing.Color.White);
if (parent != null)
{
parent = doc.Layers.FindName(parent.Name);
Layer child = doc.Layers[_Att.LayerIndex];
child.ParentLayerId = parent.Id;
child.Color = parent.Color;
}
_Att.ColorSource = ObjectColorSource.ColorFromParent;
_Att.MaterialSource = ObjectMaterialSource.MaterialFromParent;
ObjectAttributes blockAtt = new ObjectAttributes[geom.Length];
List _G = new List();
for (int i = 0; i < geom.Length; i++)
{
blockAtt[i] = _Att;
}
blockID = doc.InstanceDefinitions.Add(blockName, “PPM_autogenerated”, new Point3d(0, 0, 0), geom, blockAtt);
foreach (Plane p in planes)
{
_G.Add(doc.Objects.AddInstanceObject(blockID, Transform.PlaneToPlane(Plane.WorldXY, p)));
}
return _G.ToArray();
}

this doesn’t work for the first time, as soon as the parent layers are within the Layer list it works. As a work arround I have added the following line to kind of reget the parent layer in the second function.

parent = doc.Layers.FindName(parent.Name);

This is for Rhino 6 …

Hi @richard_schaffranek,

Can you provide an example of the layer name you’re looking for and.or adding?

– Dale

Something like:

Obj_255,0,0

Sorry, I should of asked for the full layer structure. You mentioned as soon as the parent layers are within the Layer list it works. I’d like to try to repeat what you are seeing so I can help provide a solution.

Thanks,

– Dale

Please find attached an example Script that illustrates what I mean.
First time you run it it will produce two layers in the layer table but will not add the child layer to the parent layer.
Second time you run it will add the child layer to the parent layer. However there is a work arround in the code, if you uncomment it will work every time, but I don’t guess this can be the solution…

best

Richard

LayerError.gh (2.7 KB)

Hi @richard_schaffranek,

Try this:

  public Layer getLayer(string _layerName, System.Drawing.Color C)
  {
    Rhino.RhinoDoc doc = Rhino.RhinoDoc.ActiveDoc;
    Layer _Layer = doc.Layers.FindName(_layerName);
    if (_Layer == null)
    {
      var layer = new Layer();
      layer.Name = _layerName;
      layer.Color = C;
      // LayerTable.Add makes a copy of the input layer.
      // So retrieve reference to the newly added layer.
      var idx = doc.Layers.Add(layer);
      _Layer = doc.Layers[idx];
    }
    else
    {
      Layer[] children = _Layer.GetChildren();
      if (children != null)
      {
        foreach (Layer L in children)
        {
          clearLayer(L);
          doc.Layers.Delete(L.Index, true);
        }
      }
    }
    doc.Layers.SetCurrentLayerIndex(_Layer.Index, true);
    return _Layer;
  }

– Dale

A okay that is the missing additional information, could you update API documentation?