Site Tools


How To: Rename a Layer

C++, .NET

Summary: Discusses how to rename a layer using the Rhino SDK.
NOTICE: The Rhino.NET SDK is deprecated in Rhino 5. This example adapted for the new RhinoCommon SDK is here

Rhino layers (CRhinoLayer) are stored on a layer table (CRhinoLayerTable) which is located on the active document. The process for modifying an exising layer, such as changing it's name, is as follows.

  1. Get the exising layer.
  2. Make a copy of it.
  3. Modify the copy.
  4. Call CRhinoLayerTable::ModifyLayer().

The following code sample demonstrates how to rename an existing layer.

C++

CRhinoCommand::result CCommandTestSdk::RunCommand(const CRhinoCommandContext& context)
{
  // Get the layer name
  CRhinoGetString gs;
  gs.SetCommandPrompt( L"Name of layer to rename" );
  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::nothing;
 
  // Get a reference to the layer table  
  CRhinoLayerTable& layer_table = context.m_doc.m_layer_table;
 
  // Find the layer
  int layer_index = layer_table.FindLayer( layer_name );
  if( layer_index < 0 )
  {
    RhinoApp().Print( L"Layer \"%s\" does not exist.\n", layer_name );
    return CRhinoCommand::cancel;
  }
 
  // Get the new layer name  
  gs.SetCommandPrompt( L"New layer name" );
  gs.GetString();
  if( gs.CommandResult() != CRhinoCommand::success )
    return gs.CommandResult();
 
  // Validate the string
  ON_wString new_name = gs.String();
  layer_name.TrimLeftAndRight();
  if( layer_name.IsEmpty() )
    return CRhinoCommand::nothing;
 
  // Compare both names  
  if( layer_name.CompareNoCase(new_name) == 0 )
    return CRhinoCommand::nothing;
 
  // Get the layer
  const CRhinoLayer& layer = layer_table[layer_index];
 
  // Make a copy of it, and modify the name
  ON_Layer onlayer( layer );
  onlayer.SetLayerName( new_name );
 
  // Modify the exising layer with the new definition  
  CRhinoCommand::result rc = CRhinoCommand::cancel;
  if( layer_table.ModifyLayer(onlayer, layer_index) )
    rc = CRhinoCommand::success;
 
  return rc;
}

VB.NET

Public Overrides Function RunCommand(ByVal context As IRhinoCommandContext) _
  As IRhinoCommand.result
 
  ' Get the layer name
  Dim gs As New MRhinoGetString()
  gs.SetCommandPrompt("Name of layer to rename")
  gs.GetString()
  If (gs.CommandResult() <> IRhinoCommand.result.success) Then
    Return gs.CommandResult()
  End If
 
  ' Validate the string
  Dim layer_name As String = gs.String().Trim()
  If (String.IsNullOrEmpty(layer_name)) Then
    Return IRhinoCommand.result.nothing
  End If
 
  ' Get a reference to the layer table        
  Dim layer_table As MRhinoLayerTable = context.m_doc.m_layer_table
  ' Find the layer
  Dim layer_index As Integer = layer_table.FindLayer(layer_name)
  If (layer_index < 0) Then
    RhUtil.RhinoApp.Print(String.Format("Layer {0} does not exist."+vbCrLf, layer_name))
    Return IRhinoCommand.result.cancel
  End If
 
  ' Get the new layer name        
  gs.SetCommandPrompt("New layer name")
  gs.GetString()
  If (gs.CommandResult() <> IRhinoCommand.result.success) Then
    Return gs.CommandResult()
  End If
 
  ' Validate the string
  Dim new_name As String = gs.String().Trim()
  If (String.IsNullOrEmpty(new_name)) Then
    Return IRhinoCommand.result.nothing
  End If
 
  ' Compare both names        
  If (layer_name.Equals(new_name, StringComparison.OrdinalIgnoreCase)) Then
    Return IRhinoCommand.result.nothing
  End If
 
  ' Get the layer
  Dim layer As IRhinoLayer = layer_table(layer_index)
  ' Make a copy of it, and modify the name
  Dim copy_layer As New OnLayer(layer)
  copy_layer.SetLayerName(new_name)
 
  ' Modify the exising layer with the new definition        
  Dim rc As IRhinoCommand.result = IRhinoCommand.result.cancel
  If (layer_table.ModifyLayer(copy_layer, layer_index)) Then
    rc = IRhinoCommand.result.success
  End If
  Return rc
End Function

C#

public override IRhinoCommand.result RunCommand(IRhinoCommandContext context)
{
  // Get the layer name
  MRhinoGetString gs = new MRhinoGetString();
  gs.SetCommandPrompt("Name of layer to rename");
  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.nothing;
 
  // Get a reference to the layer table        
  MRhinoLayerTable layer_table = context.m_doc.m_layer_table;
  // Find the layer
  int layer_index = layer_table.FindLayer(layer_name);
  if(layer_index < 0)
  {
    RhUtil.RhinoApp().Print(string.Format("Layer {0} does not exist.\n", layer_name));
    return IRhinoCommand.result.cancel;
  }
 
  // Get the new layer name        
  gs.SetCommandPrompt("New layer name");
  gs.GetString();
  if(gs.CommandResult() != IRhinoCommand.result.success)
    return gs.CommandResult();
 
  // Validate the string
  string new_name = gs.String().Trim();
  if(string.IsNullOrEmpty(new_name))
    return IRhinoCommand.result.nothing;
 
  // Compare both names        
  if(layer_name.Equals(new_name, System.StringComparison.OrdinalIgnoreCase))
    return IRhinoCommand.result.nothing;
 
  // Get the layer
  IRhinoLayer layer = layer_table[layer_index];
  // Make a copy of it, and modify the name
  OnLayer copy_layer = new OnLayer(layer);
  copy_layer.SetLayerName(new_name);
 
  // Modify the exising layer with the new definition        
  IRhinoCommand.result rc = IRhinoCommand.result.cancel;
  if(layer_table.ModifyLayer(copy_layer, layer_index))
    rc = IRhinoCommand.result.success;
  return rc;
}
developer/sdksamples/renamelayer.txt ยท Last modified: 2014/06/26 (external edit)