Site Tools


How To: Modify an Object's Name

C++, .NET

Summary: Demonstrates how to modify an object's user-defined name.
NOTICE: The Rhino.NET SDK is deprecated in Rhino 5. This example adapted for the new RhinoCommon SDK is here

C++

Sample One

This sample code demonstrates how to modify the name of a single object.

CRhinoCommand::result CCommandTest::RunCommand(const CRhinoCommandContext& context)
{
  // Select an object to modify
  CRhinoGetObject go;
  go.SetCommandPrompt( L"Select object to change name" );
  go.EnablePreSelect( TRUE );
  go.EnableSubObjectSelect( FALSE );
  go.GetObjects( 1, 1 );
  if( go.CommandResult() != CRhinoCommand::success )
    return go.CommandResult();
 
  // Get the object reference
  const CRhinoObjRef& objref = go.Object(0);
 
  // Get the object
  const CRhinoObject* obj = objref.Object();
  if( !obj )
    return CRhinoCommand::failure;
 
  // Make copy of object attributes. This objects
  // holds an object's user-defined name.
  ON_3dmObjectAttributes obj_attribs = obj->Attributes();
 
  // Prompt for new object name
  CRhinoGetString gs;
  gs.SetCommandPrompt( L"New object name" );
  gs.SetDefaultString( obj_attribs.m_name );
  gs.AcceptNothing( TRUE );
  gs.GetString();
  if( gs.CommandResult() != CRhinoCommand::success )
    return gs.CommandResult();
 
  // Get the string entered by the user
  ON_wString obj_name = gs.String();
  obj_name.TrimLeftAndRight();
 
  // Is name the same?
  if( obj_name.Compare(obj_attribs.m_name) == 0 )
    return CRhinoCommand::nothing;
 
  // Modify the attributes of the object
  obj_attribs.m_name = obj_name;
  context.m_doc.ModifyObjectAttributes( objref, obj_attribs );
 
  return CRhinoCommand::success;
}

Sample Two

This sample code demonstrates how to modify the name of one or more objects.

CRhinoCommand::result CCommandTest::RunCommand( const CRhinoCommandContext& context )
{
  // Select objects to modify
  CRhinoGetObject go;
  go.SetCommandPrompt( L"Select objects to change name" );
  go.EnablePreSelect( TRUE );
  go.EnableSubObjectSelect( FALSE );
  go.GetObjects( 1, 0 );
  if( go.CommandResult() != CRhinoCommand::success )
    return go.CommandResult();
 
  // Prompt for new object name
  CRhinoGetString gs;
  gs.SetCommandPrompt( L"New object name" );
  gs.GetString();
  if( gs.CommandResult() != CRhinoCommand::success )
    return gs.CommandResult();
 
  // Get the string entered by the user
  ON_wString obj_name = gs.String();
  obj_name.TrimLeftAndRight();
 
  // Process each selected object
  int i;
  for( i = 0; i < go.ObjectCount(); i++ )
  {
    // Get the object reference
    const CRhinoObjRef& objref = go.Object(i);
 
    // Get the object
    const CRhinoObject* obj = objref.Object();
    if( !obj )
      return CRhinoCommand::failure;
 
    // Make copy of object attributes. This objects
    // holds an object's user-defined name.
    ON_3dmObjectAttributes obj_attribs = obj->Attributes();
 
    // Is name the same?
    if( obj_name.Compare(obj_attribs.m_name) == 0 )
      continue;
 
    // Modify the attributes of the object
    obj_attribs.m_name = obj_name;
    context.m_doc.ModifyObjectAttributes( objref, obj_attribs );
  }
 
  return CRhinoCommand::success;
}

Sample Three

This sample code demonstrates how to modify all normal (not locked and not hidden) objects.

CRhinoCommand::result CCommandTest::RunCommand( const CRhinoCommandContext& context )
{
  // Prompt for new object name
  CRhinoGetString gs;
  gs.SetCommandPrompt( L"New object name" );
  gs.GetString();
  if( gs.CommandResult() != CRhinoCommand::success )
    return gs.CommandResult();
 
  // Get the string entered by the user
  ON_wString obj_name = gs.String();
  obj_name.TrimLeftAndRight();
 
  // Iterate through all normal (not locked and not hidden) objects
  CRhinoObjectIterator it( CRhinoObjectIterator::normal_objects, CRhinoObjectIterator::active_objects );
  CRhinoObject* obj = 0;
  for( obj = it.First(); obj; obj = it.Next() )
  {
    // Make copy of object attributes. This objects
    // holds an object's user-defined name.
    ON_3dmObjectAttributes obj_attribs = obj->Attributes();
 
    // Is name the same?
    if( obj_name.Compare(obj_attribs.m_name) == 0 )
      continue;
 
    // Modify the attributes of the object
    obj_attribs.m_name = obj_name;
    context.m_doc.ModifyObjectAttributes( CRhinoObjRef(obj), obj_attribs );
  }
 
  return CRhinoCommand::success;
}

VB.NET

Public Overrides Function RunCommand(ByVal context As IRhinoCommandContext) _
  As IRhinoCommand.result
  ' Select an object to modify
  Dim go As New MRhinoGetObject()
  go.SetCommandPrompt("Select object to change name")
  go.EnablePreSelect(True)
  go.EnableSubObjectSelect(False)
  go.GetObjects(1, 1)
  If (go.CommandResult() <> IRhinoCommand.result.success) Then Return go.CommandResult()
 
  ' Get the object reference
  Dim objref As IRhinoObjRef = go.Object(0)
 
  ' Get the object
  Dim obj As IRhinoObject = objref.Object()
  If (obj Is Nothing) Then Return IRhinoCommand.result.failure
 
  ' Make copy of object attributes. This objects
  ' holds an object's user-defined name.
  Dim obj_attribs As New MRhinoObjectAttributes(obj.Attributes())
 
  ' Prompt for new object name
  Dim gs As New MRhinoGetString()
  gs.SetCommandPrompt("New object name")
  gs.SetDefaultString(obj_attribs.m_name)
  gs.AcceptNothing(True)
  gs.GetString()
  If (gs.CommandResult() <> IRhinoCommand.result.success) Then Return go.CommandResult()
 
  ' Get the string entered by the user
  Dim obj_name As String = gs.String().Trim()
 
  ' Is name the same
  If (obj_name.Equals(obj_attribs.m_name, StringComparison.OrdinalIgnoreCase)) Then
    Return IRhinoCommand.result.nothing
  End If
 
  ' Modify the attributes of the object
  obj_attribs.m_name = obj_name
  context.m_doc.ModifyObjectAttributes(objref, obj_attribs)
 
  Return IRhinoCommand.result.success
End Function

C#

public override IRhinoCommand.result RunCommand(IRhinoCommandContext context)
{
  // Select an object to modify
  MRhinoGetObject go = new MRhinoGetObject();
  go.SetCommandPrompt("Select object to change name");
  go.EnablePreSelect(true);
  go.EnableSubObjectSelect(false);
  go.GetObjects(1, 1);
  if(go.CommandResult() != IRhinoCommand.result.success)
    return go.CommandResult();
 
  // Get the object reference
  IRhinoObjRef objref = go.Object(0);
  // Get the object
  IRhinoObject obj = objref.Object();
  if(obj == null)
    return IRhinoCommand.result.failure;
 
  // Make copy of object attributes. This objects
  // holds an object's user-defined name.
  MRhinoObjectAttributes obj_attribs = new MRhinoObjectAttributes(obj.Attributes());
 
  // Prompt for new object name
  MRhinoGetString gs = new MRhinoGetString();
  gs.SetCommandPrompt("New object name");
  gs.SetDefaultString(obj_attribs.m_name);
  gs.AcceptNothing(true);
  gs.GetString();
  if(gs.CommandResult() != IRhinoCommand.result.success)
    return go.CommandResult();
 
  // Get the string entered by the user
  string obj_name = gs.String().Trim();
 
  // Is name the same
  if(obj_name.Equals(obj_attribs.m_name, System.StringComparison.OrdinalIgnoreCase))
    return IRhinoCommand.result.nothing;
 
  // Modify the attributes of the object
  obj_attribs.m_name = obj_name;
  context.m_doc.ModifyObjectAttributes(objref, obj_attribs);
  return IRhinoCommand.result.success;
}
developer/sdksamples/objectname.txt ยท Last modified: 2014/01/23 (external edit)