Adding Instance Reference from ActiveDoc to File3dm

I am trying to add Instance Objects from my rhino file to a File3dm to save out.
The Instance Objects are embedded block instances.
I have successfully added the block reference to the new 3dm block manager, but the geometry is still missing. The block manager shows ERROR no geometry found for block definition.

                if (partObject is InstanceObject iref) {
                    var idef = iref?.InstanceDefinition;
                    file.AllInstanceDefinitions.Add(idef);
                    file.Objects.AddInstanceObject(idef.Index, iref.InstanceXform, iref.Attributes);
                }

I have tried to use the example from this post, but it uses geometry that was not previously placed in the file.

If you want to use File3dm you need first pass the InstanceDefinition along with its objects (be aware of nested blocks) after that you need to add the instances of this definition in the File3dm object.

If you don’t care about anything else you can do quick work around to select (if not present in doc yet just put those in for a second) and use RhinoDoc Write method using options selected only. It will save you a lot of headache with File3dm.

Przemyslaw, thank you your comments, but I am still not understanding what commands I will need to use to pass the InstanceDefinition with objects. I thought that is what file.AllInstanceDefinitions.Add and file.Objects.AddInstanceObject were doing.

Do I need to essentially Explode the block and loop through each object checking to see if it is a nested block, then assemble all objects into new blocks based on the Exploded blocks Id, index, name, description and objects?

I would like to move forward with File3dm. I am using it for a few different things. I will look into using RhinoDoc with my other features and see if this will work out.

@jake1 You basically have to dig recursively in block definition to get its objects pass those to objects of File3dm and from this point add instance definition basing on uids of objects inside File3dm otherwise if you point objects in the currently opened file the File3dm will be lacking the original geometry of InstanceDefinition.

Well yes and no - those won’t auto copy the geometry and attributes dependencies for you. You have to do it on your own and then use those to instantiate their occurrence in the new document.

Basically yes though I would use InstanceDefinition.GetObjects() and checking if those are not InstanceObjects and dig and dig (recursive “digging” needed). Besides adding those are lacking few params in RhinoCommon still so if you need for eg. URLs doc Write is the only way to not lose those as during IO using File3dm operations those will be gone.

And if you will want to deal with RenderMaterials in IO forget it only workarounds before writing and after reading are possible from doc level as File3dm class won’t let you access those (and in v6+ only for v5 not manageable at all even from doc level)

Thank you for clarifying. This is very helpful.