Exporting ON_InstanceDefinition / ON_InstanceRef (in C++)

I’ve been trying to get Instance export (via ONX_Model) working for the last week or so, but I’m still having mysterious issues. The procedure I’m using is something like this:

create new ON_InstanceDefinition
Create object and add it to file using AddManagedModelGeometryComponent
get its uuid via ModelComponentId
pass the uuid to instance def with AddInstanceGeometryId
create new ON_InstanceRef
set its m_instance_definition_uuid to the ON_InstanceDefintion's uuid
add the ON_InstanceRef to file using AddManagedModelGeometryComponent

Obviously this has a bit of wrongness in that it seems to add the geometry to BOTH the overall file and the instance definition. But after export and then import again, every ON_InstanceRef I created is imported, but none of their ON_InstanceRefs have any geometry in them at all.

Any hints or pointers to documentation / examples I’m missing?


Hi @colomon,

Referencing the example_write project, included with the openNURBS distribution, here is a very simple example:

static bool write_instance_definition(const wchar_t* filename, ON_TextLog& error_log)
  // write an instance definition and reference
  ONX_Model model;

  model.AddDefaultLayer(L"Default", ON_Color::Black);

  // Add a circle to the document as instance definition geometry
  ON_ArcCurve* managed_curve = new ON_ArcCurve(ON_Circle(ON_Plane::World_xy, 5.0));
  ON_3dmObjectAttributes* curve_attributes = Internal_CreateManagedAttributes(0, nullptr);
  curve_attributes->SetMode(ON::idef_object); // IMPORTANT!

  // Add an instance definition that uses the above geometry.
  ON_InstanceDefinition* managed_idef = new ON_InstanceDefinition();
  managed_idef->SetDescription(L"sample instance");

  return Internal_WriteExampleModel(model, filename, error_log);

Note, when opening the 3dm file in Rhino you won’t see anything until you run the Insert command.

Does this help?

– Dale

Yes! I thought it was likely to be the SetMode(ON::idef_object) thing, but that didn’t help. (Though I bet it helps with the side issue I was worried about.)

However, switching from using AddModelComponent to AddManagedModelComponent did the trick! Now I’m back in business and moving forward again…