Move Objects To Current Layer
Last changed: stevebaer-204.177.179.109

.
DeveloperC++, .NET
SummaryDiscusses how to iterate through the Rhino geometry table and modify the layer of selected objects.

The Rhino SDK provides an object iterator class, CRhinoObjectIterator, that allows you to walk through every object in the geometry table. This class makes it easy to perform operations on Rhino object without user intervention.

Example

The following is an example of how to iterate through the geometry table, filtering on only selected objects. The proper method for modifying an object's layer is also demonstrated.

C++

  CRhinoCommand::result CCommandTest::RunCommand( const CRhinoCommandContext& context )
  {
    // Get the current layer index
    const CRhinoLayerTable& layer_table = context.m_doc.m_layer_table;
    int layer_index = layer_table.CurrentLayerIndex();


    // Create an object iterator that filters on selected,
    // non-light objects in the current document only
    CRhinoObjectIterator it( CRhinoObjectIterator::normal_objects,
                             CRhinoObjectIterator::active_objects  );
    it.EnableSelectedFilter( TRUE );
    it.IncludeLights( FALSE );
    CRhinoObject* obj = NULL;
    int count = 0;


    // Walk the geometry table
    for( obj = it.First(); obj; obj = it.Next() )
    {
      // Ignore select objects that are already on the current layer
      if( obj->Attributes().m_layer_index == layer_index )
        continue;
      // Copy the object's attributes and set the new layer index
      CRhinoObjectAttributes atts( obj->Attributes() );
      atts.m_layer_index = layer_index;
      // Modify the object's attributes
      CRhinoObjRef ref(obj);
      if( context.m_doc.ModifyObjectAttributes(ref, atts) )
        count++;
    }
    if( count > 0 )
      context.m_doc.Redraw();
   return CRhinoCommand::success;
  }

VB.NET (Rhino 4)

  Public Overrides Function RunCommand(ByVal context As IRhinoCommandContext) _
    As IRhinoCommand.result


    ' Get the current layer index
    Dim layer_table As IRhinoLayerTable = context.m_doc.m_layer_table
    Dim layer_index As Integer = layer_table.CurrentLayerIndex()


    ' Create an object iterator that filters on selected,
    ' non-light objects in the current document only
    Dim it As New MRhinoObjectIterator( _
                    IRhinoObjectIterator.object_state.normal_objects,_
                    IRhinoObjectIterator.object_category.active_objects)
    it.EnableSelectedFilter(True)
    it.IncludeLights(False)


    Dim count As Integer = 0
    ' Walk the geometry table
    For Each obj As MRhinoObject In it
      ' Ignore select objects that are already on the current layer
      If (obj.Attributes().m_layer_index = layer_index) Then Continue For


      ' Copy the object's attributes and set the new layer index
      Dim atts As New MRhinoObjectAttributes(obj.Attributes())
      atts.m_layer_index = layer_index
      ' Modify the object's attributes
      Dim objref As New MRhinoObjRef(obj.Attributes.m_uuid)
      If (context.m_doc.ModifyObjectAttributes(objref, atts)) Then
        count = count + 1
      End If
    Next


    If (count > 0) Then context.m_doc.Redraw()
    Return IRhinoCommand.result.success
  End Function

C# (Rhino 4)

  public override IRhinoCommand.result RunCommand(IRhinoCommandContext context)
  {
    MRhinoDoc doc = RhUtil.RhinoApp().ActiveDoc();
    // Get the current layer index
    IRhinoLayerTable layer_table = doc.m_layer_table;
    int layer_index = layer_table.CurrentLayerIndex();


    // Create an object iterator that filters on selected,
    // non-light objects in the current document only
    MRhinoObjectIterator it =
        new MRhinoObjectIterator(IRhinoObjectIterator.object_state.normal_objects,
                                 IRhinoObjectIterator.object_category.active_objects);
    it.EnableSelectedFilter(true);
    it.IncludeLights(false);


    int count = 0;
    // Walk the geometry table
    for (MRhinoObject obj = it.First(); obj != null; obj = it.Next())
    {
      // Ignore select objects that are already on the current layer
      if( obj.Attributes().m_layer_index == layer_index )
        continue;


      // Copy the object's attributes and set the new layer index
      MRhinoObjectAttributes atts = new MRhinoObjectAttributes(obj.Attributes());
      atts.m_layer_index = layer_index;
      // Modify the object's attributes
      MRhinoObjRef objref = new MRhinoObjRef(obj.Attributes().m_uuid);
      if( doc.ModifyObjectAttributes( objref, atts))
        count ++;
    }
    if (count > 0)
      doc.Redraw();
    return IRhinoCommand.result.success;
  }