C# - select objects by Layer

Hi,

Firstly, I am still a beginner in C# and still trying to understand how it works.
So, what I am trying to do is to make a script that automatically selects all objects in a specific layer.

private void RunScript(int inLayer, ref object A, ref object B)
{

//select specific layer in rhino doc
Rhino.RhinoDoc doc = Rhino.RhinoDoc.ActiveDoc;
var selLayer = doc.Layers.FindIndex(inLayer);

//get objects in layer
Rhino.DocObjects.RhinoObject[] objs = doc.Objects.FindByLayer(selLayer);

A = selLayer;
B = objs;

}

However, instead of selecting the objects, it just selects the object’s properties ( I guess).

MeshObject: (unnamed) (2065376000)
ExtrusionObject: (unnamed) (0)
ExtrusionObject: (unnamed) (0)

Can someone explain to me why does this happen? and how to solve this?
Thanks!

You must select objects after finding them:

private void RunScript(string layername)
  {
    Rhino.DocObjects.RhinoObject[] rhobjs = doc.Objects.FindByLayer(layername);
    for (int i = 0; i < rhobjs.Length; i++)
      rhobjs[i].Select(true);
  }

Zaqi.gh (12.9 KB)

1 Like

Hi Mahdiyar,

Thanks for your help! But it still shows these in the output:

BrepObject: (unnamed) (0)
MeshObject: (unnamed) (-2102460576)
BrepObject: (unnamed) (0)
ExtrusionObject: (unnamed) (0)

Would it be possible to reference the geometry instead of the properties in the output?

Thanks,
Z

Sorry for misunderstanding, I thought you want to select them in Rhino Viewport.

Rhino.DocObjects.RhinoObject[] rhobjs = doc.Objects.FindByLayer(layername);
    List<object> ghobjs = new List<object>();
    foreach (Rhino.DocObjects.RhinoObject rhObj in rhobjs)
      ghobjs.Add(rhObj.Geometry);
    A = ghobjs;

Zaqi.gh (12.2 KB)

ahh, great!! super!

Thank you so much for your help!

Best,
Zaqi