I have two functions.
- 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;
}
- 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 …