Delete nested blocks

Hi everyone,

I’m working with nested blocks (InstanceDefinitions / InstanceObjects) in RhinoCommon and I’m running into conceptual trouble identifying the correct way to delete nested instances while preserving block hierarchy.

My scenario

I have the following block structure:

Block A (InstanceDefinition)
 ├─ InstanceReference → ChildBlock1
 │    └─ Geometry
 ├─ InstanceReference → ChildBlock2
 │    └─ Geometry
 └─ Geometry (inside Block A)

In the model, there may be multiple instances of Block A.

Goal

Given an InstanceObject that represents ChildBlock1, I want to:

  1. If ChildBlock1 is top-level → delete it directly

  2. If ChildBlock1 is nested inside Block A:

    • If Block A contains multiple child instances → remove only ChildBlock1 from Block A’s InstanceDefinition

    • If ChildBlock1 is the only remaining child → delete the parent instance of Block A instead

  3. Avoid flattening geometry or corrupting nested block structure

What I’m doing so far

  • I rebuild the parent InstanceDefinition using InstanceDefinitions.ModifyGeometry(...)

  • I correctly recreate nested references using InstanceReferenceGeometry(def.Id, instanceRef.InstanceTransform)

  • This part works as expected when removing one child from a definition

Where I’m stuck

When ChildBlock1 is the last child inside its parent definition, I need to delete the parent InstanceObject, not the definition.

However, I’m unable to reliably find the InstanceObject of the parent definition that contains the current instance.

I tried approaches like:

parentDefinition.GetReferences(1)

but this only returns instances of the definition — it does not tell me which instance contains the nested child.

I currently maintain my own block-containment graph (definition → parent definitions), but I still can’t determine which parent instance should be deleted in this case.

My questions

  1. Is there a recommended RhinoCommon pattern for navigating upward in nested block hierarchies (instance → parent instance)?

  2. Is maintaining a custom block graph the only correct approach?

  3. Is there any supported way to determine which InstanceObject contains a given nested InstanceObject?

Any guidance or confirmation of the correct approach would be greatly appreciated.

A post was split to a new topic: Download Windows version?

its working for me now .