How to add layers with hierarchy from a plugin with Grasshopper?

        protected override void SolveInstance(IGH_DataAccess DA)
        {
            bool toogle = false;
            DA.GetData("Switch", ref toogle);

            List<string> Parents = new List<string> { "Design", "Existing" };


            if (toogle)
            {
                Layer GrandParentLayer = new Layer() { Name = "GS_Layers5" };
                Rhino.RhinoDoc.ActiveDoc.Layers.Add(GrandParentLayer);

                foreach (string parentlistlayer in Parents)
                {
                    Layer ParentLayer = new Layer()
                    {
                        Color = System.Drawing.Color.Black,
                        Name = parentlistlayer,
                        ParentLayerId = GrandParentLayer.Id

                    };

                    Rhino.RhinoDoc.ActiveDoc.Layers.Add(ParentLayer);
                }
            }
        }

image

The layers created do not have a hierarchy… to my dismay.

                        ParentLayerId = GrandParentLayer.Id

this does not work as it supposed to…

Hi @Wiley ,
Once you add GrandParentLayer to the layer table, the instance is not updated, so the Guid is still empty. So you need to retrieve the newly added layer from the table using index.


Layer GrandParentLayer = new Layer() { Name = "GS_Layers5" };
int grandparentLayerIndex = Rhino.RhinoDoc.ActiveDoc.Layers.Add(GrandParentLayer);
            
       foreach (string parentlistlayer in Parents)
            {
                Layer ParentLayer = new Layer()
                {
                    Color = System.Drawing.Color.Black,
                    Name = parentlistlayer,
                    ParentLayerId = RhinoDoc.ActiveDoc.Layers[grandparentLayerIndex].Id

                };

                Rhino.RhinoDoc.ActiveDoc.Layers.Add(ParentLayer);
            }
1 Like