Sdk Object Name
Last changed: stevebaer-63.226.222.185

.
DeveloperC++, .NET
SummaryDemonstrates how to modify an object's user-defined name.

C++

  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;
  }

VB.NET (Rhino 4)

  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# (Rhino 4)

  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;
  }