Get sub layer's object from rhino to grasshopper in c# visual studio

Hi All,

I’m developing a plug-in using c# in the visual studio environment.
I tried to simply use the sub layer name to find my objects.

  private void RunScript(object x, object y, ref object A)
  {

    Rhino.DocObjects.RhinoObject[] myObjs = RhinoDoc.ActiveDoc.Objects.FindByLayer("subLayerName");
    
    A = myObjs;
  }

But it’s a no go, and the output is empty.
Any suggestion?

Regards,
Shaun

You probably need to use the full layer name:

Rhino.DocObjects.RhinoObject[] myObjs = RhinoDoc.ActiveDoc.Objects.FindByLayer("parentLayer::subLayerName");

Note the double-colon between parent layer and sub layer name.

1 Like

Hi Menno,

Unfortunately, it also doesn’t work if I only input parent layer name.
I think it should be something like how to access active rhino document correctly.

Hi @shaunwu25,

Try doing something like this:

var layer_index = doc.Layers.FindByFullPath("ParentLayerName::SubLayerName", -1);
if (layer_index > -1)
{
  var objects = doc.Objects.FindByLayer(doc.Layers[layer_index]);
  // to do...
}

– Dale

@dale Thanks! it works!


Now I have a following question.
How can I convert a rhino mesh object to grasshopper geometry(from goo to mesh)?

Regards,
Shaun

RhinoObject is a wrapper around the actual geometry and has much more information than just the geometry. Likewise, GH_Mesh is the grasshopper wrapper. So basically you unwrap the Rhino object, and wrap the contents in a Grasshoper wrapper :wink:

RhinoObject meshObject; // assigned elsewhere
if (meshObject.Geometry is Mesh m)
{
  GH_Mesh gh = new GH_Mesh(m);
  A = gh;
}

Hi @menno,

Thanks for your clear explanation!
I attach the complete code below in case anyone need it.

var doc = RhinoDoc.ActiveDoc;
var layer_index = doc.Layers.FindByFullPath("grandParentLayer::parentLayer::subLayer", -1);
Rhino.DocObjects.RhinoObject[] objects = null;
if (layer_index > -1)
{
    objects = doc.Objects.FindByLayer(doc.Layers[layer_index]);
}
List<GH_Mesh> ghList = new List<GH_Mesh>();
foreach (Rhino.DocObjects.RhinoObject rhObj in objects)
{
    if (rhObj.Geometry is Mesh m)
    {
        GH_Mesh gh = new GH_Mesh(m);
        ghList.Add(gh);
    }
}

Regards,
Shaun