developer:sdksamples:sellayer


How To: Select All Objects on a Layer.

C++, .NET

Summary: Demonstrates how to select all of the objects on a specified layer.

The following sample code demonstrates how to select all of the objects on a specified layer.

C++

CRhinoCommand::result CCommandTest::RunCommand( const CRhinoCommandContext& context )
{
  // Get a reference to the layer table
  CRhinoLayerTable& layer_table = context.m_doc.m_layer_table;
  // Get the current layer
  const CRhinoLayer& current_layer = layer_table.CurrentLayer();
 
  // Prompt for a layer name
  CRhinoGetString gs;
  gs.SetCommandPrompt( L"Name of layer to select object" );
  gs.SetDefaultString( current_layer.LayerName() );
  gs.AcceptNothing( TRUE );
  gs.GetString();
  if( gs.CommandResult() != CRhinoCommand::success )
    return gs.CommandResult();
 
  // Validate the string
  ON_wString layer_name = gs.String();
  layer_name.TrimLeftAndRight();
  if( layer_name.IsEmpty() )
    return CRhinoCommand::cancel;
 
  // Find the layer
  int layer_index = layer_table.FindLayer( layer_name );
  if( layer_index < 0 )
  {
    RhinoApp().Print( L"\"%s\" does not exists.\n", layer_name );
    return CRhinoCommand::cancel;
  }
 
  // Get the layer
  const CRhinoLayer& layer = layer_table[layer_index];
 
  // Get all of the objects on the layer
  ON_SimpleArray<CRhinoObject*> obj_list;
  int i, obj_count = context.m_doc.LookupObject( layer, obj_list );
 
  // Select all of the layer objects
  for( i = 0; i < obj_count; i++ )
  {
    CRhinoObject* obj = obj_list[i];
    if( obj && obj->IsSelectable() )
      obj->Select();
  }
 
  if( obj_count )
    context.m_doc.Redraw();
  return CRhinoCommand::success;
}

VB.NET

Public Overrides Function RunCommand(ByVal context As IRhinoCommandContext)_
  As IRhinoCommand.result
 
  ' Get a reference to the layer table
  Dim layer_table As MRhinoLayerTable = context.m_doc.m_layer_table
  ' Get the current layer
  Dim current_layer As IRhinoLayer = layer_table.CurrentLayer()
 
  ' Prompt for a layer name
  Dim gs As New MRhinoGetString()
  gs.SetCommandPrompt("Name of layer to select object")
  gs.SetDefaultString(current_layer.LayerName())
  gs.AcceptNothing(True)
  gs.GetString()
  If (gs.CommandResult() <> IRhinoCommand.result.success) Then Return gs.CommandResult()
 
  ' Validate the string
  Dim layer_name As String = gs.String().Trim()
  If (String.IsNullOrEmpty(layer_name)) Then
    Return IRhinoCommand.result.cancel
  End If
 
  ' Find the layer
  Dim layer_index As Integer = layer_table.FindLayer(layer_name)
  If (layer_index < 0) Then
    RhUtil.RhinoApp.Print(layer_name + "does not exist." + vbCrLf)
    Return IRhinoCommand.result.cancel
  End If
 
  ' Get the layer
  Dim layer As IRhinoLayer = layer_table(layer_index)
 
  ' Get all of the objects on the layer
  Dim obj_list As MRhinoObject() = Nothing
  Dim obj_count As Integer = context.m_doc.LookupObject(layer, obj_list)
 
  ' Select all of the layer objects
  If (obj_list Is Nothing) Then Return IRhinoCommand.result.cancel
 
  For Each obj As MRhinoObject In obj_list
    If (obj IsNot Nothing And obj.IsSelectable()) Then obj.Select()
  Next
 
  If (obj_count > 0) Then context.m_doc.Redraw()
  Return IRhinoCommand.result.success
End Function

C#

public override IRhinoCommand.result RunCommand(IRhinoCommandContext context)
{
  // Get a reference to the layer table
  MRhinoLayerTable layer_table = context.m_doc.m_layer_table;
  // Get the current layer
  IRhinoLayer current_layer = layer_table.CurrentLayer();
 
  // Prompt for a layer name
  MRhinoGetString gs = new MRhinoGetString();
  gs.SetCommandPrompt("Name of layer to select object");
  gs.SetDefaultString(current_layer.LayerName());
  gs.AcceptNothing(true);
  gs.GetString();
  if(gs.CommandResult() != IRhinoCommand.result.success)
    return gs.CommandResult();
 
  // Validate the string
  string layer_name = gs.String().Trim();
  if(string.IsNullOrEmpty(layer_name))
    return IRhinoCommand.result.cancel;
 
  // Find the layer
  int layer_index = layer_table.FindLayer(layer_name);
  if(layer_index < 0)
  {
    RhUtil.RhinoApp().Print(layer_name + "does not exist.\n");
    return IRhinoCommand.result.cancel;
  }
 
  // Get the layer
  IRhinoLayer layer = layer_table[layer_index];
 
  // Get all of the objects on the layer
  MRhinoObject[] obj_list = null;
  int obj_count = context.m_doc.LookupObject(layer, ref obj_list);
 
  // Select all of the layer objects
  if(obj_list == null)
    return IRhinoCommand.result.cancel;
 
  foreach( MRhinoObject obj in obj_list)
  {
    if( obj != null && obj.IsSelectable() )
      obj.Select();
  }
 
  if (obj_count > 0)
    context.m_doc.Redraw();
  return IRhinoCommand.result.success;
}
developer/sdksamples/sellayer.txt · Last modified: 2010/01/26 16:37 (external edit) Driven by DokuWiki Recent changes RSS feed

 © 1997-2012 

McNeel North America Europe Latin AmericaAsia