I’m having difficulty understanding where to find the definition of an instanceReference in Rhino3dm.js
I have a simple scene of a extrusion turned into a block and copied over twice. (see below)
Now my question is how do I find out which instanceReference is pointing to which block definition object? It’s simple in this case since we just have one definition object, but what happens when I have more than one definition objects?
You can loop over model.instanceDefinitions() to find all the InstanceDefinitions contained in a model.
Looping over model.objects() you will find the InstanceReferences, as you’ve already found. In your case you have two instances of InstanceReference. Both have the same InstanceDefinition. The InstanceDefinition is your blueprint, these are called block instances in Rhino UI. In your model you have two block instances.
InstanceDefinition has getObjectIds(), which will give you a list of object IDs that are part of the InstanceDefinition.
InstanceReference has parentIdefId(), which gives the UUID of the InstanceDefinition it was instanced from. It also has xform(), which gives you the point where the block instance is inserted.
I’m sharing the code here for anyone with the same issue in the future:
let parentId = geometry.parentIdefId;
let instanceDef = model.instanceDefinitions().findId(parentId);
let instanceDefIds = instanceDef.getObjectIds();
instanceDefIds.forEach(id => {
let instanceDefObj = model.objects().findId(id);
let geomCopy = instanceDefObj.geometry().duplicate();
geomCopy.transform(geometry.xform);
});