How to move an object without selection[C++]

Hi there,
I use C++ to develop plugin. Now I want to move an object which has been added to the context.m_doc without selection.
For example:
ON_Curve* cur;//////defined somewhere
context.m_doc.AddCurveObject(*cur);
ON_UUID id;
cur->getUserData(id);
CRhinoObjRef cur_ref = CRhinoObjRef::CRhinoObjRef(id);
ON_Xform trans;
trans.Translation(10,10,10);
context.m_doc.TransformObject(cur_ref,trans);
context.m_doc.Redraw();
The curve doesn’t move at all, I am really confused about this.

But it works if the curve is selected like this:
CRhinoGetObject go;
go.SetCommandPrompt( L"Select objects" );
go.GetObjects( 1, 0 );
const CRhinoObjRef cur_ref = go.Object(0);
ON_Xform trans;
trans.Translation(10,10,10);
context.m_doc.TransformObject(cur_ref,trans);
context.m_doc.Redraw();

Why don’t you transform the curve prior to adding it to the document?

Because I want to make a simulation of the moving process.
For example, the Rhino keeps reading data from a file which is full of vectors, and I need to make the objects in document moving along these vectors with each step shown in Rhino.
Make long story short, it is like an animation.

I see. I think this is what’s going wrong, you’re retrieving the curve ID from the geometry, and not from the curve object you added to the document. I’m not sure why the id is in the user data, but that is not how it is supposed to work.

ON_Curve* cur;//////defined somewhere 
context.m_doc.AddCurveObject(*cur);
ON_UUID id;
cur->getUserData(id); //this is incorrect

I think it should be this:

ON_Curve *cur; // defined elsewhere
CRhinoCurveObject* pObj = context.m_doc.AddCurveObject(*cur); // a new UUID is assigned to the curve object
ON_UUID id = pObj->Attributes().m_uuid

Thank you so much! You are right. The problem has been solved.

IMPORTANT: CRhinoDoc::AddCurveObject copies the input curve. Thus, the caller is responsible for cleaning up the memory allocate for the curve, in this example:

This is a better example:

ON_Curve* curve = GetMyCurveHere();
if (0 != curve)
{
  CRhinoCurveObject* curve_obj = context.m_doc.AddCurveObject(*curve);
  delete curve; // Don't leak...
  if (0 != curve_obj)
  {
    ON_UUID curve_id = curve_obj->ModelObjectId();
    // Continue...
  }
}
1 Like