I’ve followed the code in https://github.com/mcneel/rhino-developer-samples/blob/6/cpp/SampleMarker/SampleMarkerEventWatcher.cpp for pasting of custom objects.
I’ve gotten as far as the vtable pointer comparison (marked with a comment below), but the comparison fails and the object type change never occurs. I’ve checked and double checked this and don’t see an obvious reason why it fails. I’m so close here…
Btw… manually manipulating vtables is extremely ugly and likely not portable… Is there a more sane way to do this in production code?
class SuperDEventWatcher : public CRhinoEventWatcher
{
public:
CCommandSuperD2 *owner;
SuperDEventWatcher(CCommandSuperD2 *owner) : owner(owner)
{
}
static void promoteRhinoMeshObjectToSuperDObject(const CRhinoMeshObject *meshObj)
{
static void *CSuperDMeshObject__vtable_ptr = 0;
static void *CRhinoMeshObject__vtable_ptr = 0;
if(!meshObj)
return;
if(!CSuperDMeshObject__vtable_ptr)
{
CRhinoSuperDMeshObject a;
CSuperDMeshObject__vtable_ptr = ON_ClassVtable((void *)(&a));
CRhinoMeshObject b;
CRhinoMeshObject__vtable_ptr = ON_ClassVtable((void *)(&b));
}
//HERE: This if statement is always false **********
if(CRhinoMeshObject__vtable_ptr == *((void **)meshObj))
{
// Change from CRhinoMeshObject vtable to CSuperDMeshObject__vtable_ptr vtable
*((void**)meshObj) = CSuperDMeshObject__vtable_ptr;
}
}
void OnEndOpenDocument(CRhinoDoc &doc, const wchar_t *filename, BOOL bMerge, BOOL bReference) override
{
UNREFERENCED_PARAMETER(filename);
UNREFERENCED_PARAMETER(bMerge);
UNREFERENCED_PARAMETER(bReference);
CRhinoObjectIterator it(doc, CRhinoObjectIterator::undeleted_objects, CRhinoObjectIterator::active_objects);
it.SetObjectFilter(ON::mesh_object);
const CRhinoObject *obj = 0;
for(obj = it.First(); obj; obj = it.Next())
{
const CRhinoMeshObject *meshObj = CRhinoMeshObject::Cast(obj);
if(meshObj && CSuperDUserData::HasSuperDUserData(meshObj))
{
promoteRhinoMeshObjectToSuperDObject(meshObj);
}
}
}
};