Get ObjRef or Guid for a mesh

Hi

I have an array of meshes that I iterate through. If the mesh doesn’t match certain criteria I want to move it to another layer. I am struggling to work out how to get the Object/ObjRef or Guid for the mesh in order to change its layer

Mesh[] _meshes; //assigned by user selection
layer_index = 2;//unmatched layer

for (int meshNumber = 0; meshNumber < _meshes.Length; meshNumber++)
{
 
     ...
      //if certain criteria aren't matched

     //get Guid/Objref or Object for the mesh (this is the thing I can't do) - then

     Rhino.DocObjects.ObjRef oRef = new Rhino.DocObjects.ObjRef(guid);
     Rhino.DocObjects.RhinoObject rhobj = oRef.Object();
     rhobj.Attributes.LayerIndex = layer_index;
     rhobj.CommitChanges();

}

Can anyone help?

Hello,

How do you get your array of meshes?

Hi,

I do a

    GetObject goMeshes = new GetObject();
    goMeshes.SetCommandPrompt("Select meshes");
    goMeshes.GeometryFilter = ObjectType.Mesh;
    goMeshes.SubObjectSelect = false;
    goMeshes.GetMultiple(1, 0);

But - I then go through and do some processing and drop some of the meshes the user selected - which is why it’s now an array of Mesh rather than a GetObject. I could go back and change lots of code to store the ObjRef but I just wondered if that’s necessary.

Uhm… You can make a second array where you store the ID’s.
And add a little code in between where when you remove a mesh you also remove the ID from the array. That way you dont have to change your code but only add a bit.

Second way is to loop through all objects. compare the mesh in the array with the meshes in the drawing and if its a hit use that mesh but this can consume a lot of time if its a big drawing.

Ok thanks. I had considered the first option but thought there might be a better way of doing it. I’ll have a look at the second option now.

Thanks for your time.

As I know it when you get the geometry like RhinoObj too Mesh the object ID will be lost.

Good luck.

Just though of maybe a faster way…

Loop through your meshes at the start and add a UserString
Dim m As Geometry.Mesh
m.SetUserString("", “”)
m.GetUserString()

Then when you need it GetUserString and m.UserDictionary.Remove()

Nice. Thank you - I’m going to do with that one!

Hi Hannah,

Does this help?

protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
  var go = new GetObject();
  go.SetCommandPrompt("Select meshes");
  go.GeometryFilter = ObjectType.Mesh;
  go.SubObjectSelect = false;
  go.GetMultiple(1, 0);
  if (go.CommandResult() != Result.Success)
    return go.CommandResult();

  foreach (var obj_ref in go.Objects())
  {
    var rh_obj = obj_ref.Object();
    var mesh = obj_ref.Mesh();
    if (null != rh_obj && null != mesh)
    {
      if (!MyMeshCriteriaTest(mesh))
      {
        rh_obj.Attributes.LayerIndex = MyMeshLayerIndex();
        rh_obj.CommitChanges();
      }
    }
  }

  doc.Views.Redraw();

  return Result.Success;
}

Thanks. Yes it helps - It’s confirmation that I’m doing it in a reasonable way!