C++, Rhino 6, How to Purge Object from Document?


I track many temporary object by object ID. In my plug-in, I regularly add and remove objects for temporary visualization or intermediate result display. Below is a rough outline of the process:

  1. Add object to document using CRhinoDoc::AddMeshObject(const ON_Mesh&)
  2. Do some calculations until a new intermediate result is available
  3. Lookup the old CRhinoObject using CRhinoDoc::LookupObject(ON_UUID)
  4. Remove the object from the document using CRhinoDoc::PurgeObject(CRhinoObject*)

In Rhino 5, LookupObject would return a non-const CRhinoObject. But in Rhino 6, the returned object is now const. I want to use PurgeObject so no delete record is created. I may be adding hundreds of temporary or intermediate mesh objects during calculations, none of which require an undo record.

Is it possible to retrieve a non-const CRhinoObject from the document in Rhino 6? If not, is there a quick alternative way to purge an object from the document without creating an undo record?



Hi @lukeniwranski,

This should do the trick:

ON_UUID rhino_object_id = ...;
unsigned int doc_serial_numnber = context.m_doc.RuntimeSerialNumber();
const CRhinoObject* pConstRhinoObject = CRhinoObject::FromId(doc_serial_numnber, rhino_object_id);
if (nullptr != pConstRhinoObject)
  CRhinoObject* pRhinoObject = const_cast<CRhinoObject*>(pConstRhinoObject);
  bool rc = context.m_doc.PurgeObject(pRhinoObject);

– Dale

Hi @dale,


Simply casting the object to non-const won’t break Rhino in any way? Are there any pitfalls I need to watch out for when doing this? Is it also safe to use this method for calling CRhinoObject:: CreateMeshes() (and all other CRhinoObject functions)?


Hi @lukeniwranski,

We don’t encourage casting a const pointer to a non-const one. But sometimes you don’t have much choice.

In the case of document objects, you want to stay away from modifying objects behind Rhino’s back. Better to play by the rules…

– Dale