Site Tools


How To: Copy a CRhinoObject Object

C++, .NET

Summary: Demonstrates how to make a copy of a CRhinoObject-derived object.

Question

I want to take a constant CRhinoObject, make a copy of it, and add the copy to the document. What is the correct way to do this?

C++

const CRhinoObject* object = ..... // some object
CRhinoObject* duplicate = object->Duplicate();
if( duplicate )
{
  if( context.m_doc.AddObject(duplicate) )
    context.m_doc.Redraw;
  else
    delete duplicate;
}

VB.NET

Public Overrides Function RunCommand(ByVal context As IRhinoCommandContext)_
  As IRhinoCommand.result
 
  Dim go As New MRhinoGetObject()
  go.GetObjects(1, 1)
 
  If (go.CommandResult() <> IRhinoCommand.result.success) Then Return go.CommandResult()
 
  Dim objref As MRhinoObjRef = go.Object(0)
  Dim base_object As IRhinoObject = objref.Object()
  Dim duplicate As MRhinoObject = base_object.DuplicateRhinoObject()
  If (duplicate IsNot Nothing) Then
    If (context.m_doc.AddObject(duplicate)) Then
      context.m_doc.Redraw()
    End If
  End If
End Function

C#

public override IRhinoCommand.result RunCommand(IRhinoCommandContext context)
{
  MRhinoGetObject go = new MRhinoGetObject();
  go.GetObjects(1, 1);
  if(go.CommandResult() != IRhinoCommand.result.success)
    return go.CommandResult();
 
  MRhinoObjRef objref = go.Object(0);
  IRhinoObject base_object = objref.Object();
  MRhinoObject duplicate = base_object.DuplicateRhinoObject();
  if(duplicate != null)
  {
    if(context.m_doc.AddObject(duplicate))
      context.m_doc.Redraw();
  }
  return IRhinoCommand.result.success;
}
developer/sdksamples/duplicateobject.txt ยท Last modified: 2010/01/26 (external edit)