Unable to "move" layers to new parent layer

Hi all,

I’m trying to create this simple command that preps a survey file to be imported in another rhino file.

First step to do is to take all current layers and move them under a specific layer structure we use. Therefore, what I thought of doing was:

  1. take all current layers in file
  2. create needed layer structure
  3. assign .ParentLayerId property of all layers collected on step 1. to be the layer created in step 2.

Which is pretty much what’s happening here too Python - move layer set under new parent layer

I thought it was quite straight forward, but for some reason it only works up to step 2, but the existing layers are not moving to the created layer.

Here’s my code:

        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
            // Collect all layers in doc
            List<Layer> docLayers = doc.Layers.ToList();

            // Create survey layer
            List<WbRhinoLayer> rhino2dLayers =

            WbRhinoLayer surveyWbLayer = rhino2dLayers.SingleOrDefault(
                    l => l.DisplayName.Contains("Site-Survey"));

            // Create layer in doc
            Layer surveyLayer = surveyWbLayer.AddLayerToDoc(doc, parent);

                // Move all layers in doc to survey layer
                foreach (Layer layer in docLayers)
                    layer.ParentLayerId = surveyLayer.Id;
            catch (System.Exception ex)

            return Result.Success;

The command correctly creates those WB_Template > AR2-Site > AR2-Site-Survey correctly, but then it just doesn’t move the previous layers to AR2-Site-Survey. Also, no exception is thrown, which is strange.

I’m testing with this simple example

What am I doing wrong?



I think I found a solution here Create Layer Hierarchy in C# - #6 by netlander

So it seems that adding the layer to the doc and seeing it there in the layers browser is not enough because it doesn’t have an established Id…even though it’s there and you can see it!
Anyway, the trick is to get the newly created layer again from the LayersTable. That one will have an Id that you can then use to set the other layers’ parent Id.

So you need to add this after having created the layer:

Layer surveyLayer = doc.Layers.FindName(surveyLayerInMemory.Name);

Hopefully this helps!