Site Tools


Selecting Objects that are in a Group

.NET

Version: Rhino 4
NOTICE: The Rhino.NET SDK is deprecated in Rhino 5. This example adapted for the new RhinoCommon SDK is here

Question

I understand that, using a MRhinoGetObject object, I can select a group of objects. But, how can I select a group of objects non-interactively, or without the user having to pick one of the group members?

Answer

To non-interactively select all of the objects that belong to an object group, you must iterate through the geometry table and look for all of the group members.

For example:

C#

public int SelectObjectGroup(MRhinoDoc doc, int group_index, bool bRedraw)
{
  if( group_index < 0 | group_index >= doc.m_group_table.GroupCount() )
    return 0;
 
  MRhinoObjectIterator it = new MRhinoObjectIterator(doc,IRhinoObjectIterator.object_state.undeleted_objects,
                                                         IRhinoObjectIterator.object_category.active_and_reference_objects );
  it.IncludeLights(true);
  it.IncludePhantoms(true);
 
  int selected_count = 0;
  foreach (MRhinoObject obj in it)
  {
    if (obj.Attributes().IsInGroup(group_index) && obj.IsSelectable() )
    {
      obj.Select();
      selected_count++;
    }
  }
 
  if (bRedraw)
    doc.Redraw();
 
  return selected_count;
}

Here is an example, perhaps not a good one, of using the above function.

public override IRhinoCommand.result RunCommand(IRhinoCommandContext context)
{
  MRhinoGetObject go = new MRhinoGetObject();
  go.SetCommandPrompt("Select object");
  go.GetObjects(1, 1);
  if (go.CommandResult() != IRhinoCommand.result.success)
    return go.CommandResult();
 
  IRhinoObject obj = go.Object(0).Object();
  if( obj == null)
    return IRhinoCommand.result.failure;
 
  int group_count = obj.Attributes().GroupCount();
  if( group_count > 0 )
  {
    Arrayint groups  = new Arrayint(group_count);
    obj.Attributes().GetGroupList(ref groups);
 
    for (int i = 0; i < groups.Count(); i++)
      SelectObjectGroup(context.m_doc, groups[i], false);
 
    context.m_doc.Redraw();
  }
 
  return IRhinoCommand.result.success;
}
developer/sdksamples/selectgroupobject.txt ยท Last modified: 2014/01/23 (external edit)