# How can I replace a nested "Instance Definition" Object in C#?

**URL:** <https://discourse.mcneel.com/t/how-can-i-replace-a-nested-instance-definition-object-in-c/136265>\
**Category:** Scripting\
**Tags:** windows, csharp, grasshopperrhino\
**Created:** [January 13, 2022, 8:05am UTC](https://discourse.mcneel.com/t/how-can-i-replace-a-nested-instance-definition-object-in-c/136265 "2022-01-13T08:05:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![wortley.jb](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/wortley.jb/32/544345_2.png) [@wortley.jb](https://discourse.mcneel.com/u/wortley.jb)\
**Post date:** [January 13, 2022, 8:05am UTC](https://discourse.mcneel.com/t/how-can-i-replace-a-nested-instance-definition-object-in-c/136265/1 "2022-01-13T08:05:19Z")

</div>

I have library of blocks and inside my blocks I have others (a few hundred).

I’d like to add something to a C# script I’m already running on the Instance Definitions, to swap out the nested blocks with other nested blocks. Unconventional, but there is no other work-around because I’m pulling the blocks from an external file and that’s by design.

Right now I am able to find the object I want no problem ( **InstanceDefinition.Object(i)** or just the var “obj”).

**var libraryInstanceDefinition = libraryDoc.InstanceDefinitions.Find(libDefName);**  
**var obj = libraryInstanceDefinition.Object(i);**

I know the block name for my chosen block for replacement and want to do something like this:

**obj = (rhinodoc.InstanceDefinitions.Find(theDesiredBlock.Name) as RhinoObject);**

But I can’t think of what I’d need to do to cast it corrrectly. Anybody have any thoughts?

---

<div class="post-metadata">

**Author:** ![dale](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/dale/32/772741_2.png) [@dale](https://discourse.mcneel.com/u/dale)\
**Post date:** [January 17, 2022, 6:02pm UTC](https://discourse.mcneel.com/t/how-can-i-replace-a-nested-instance-definition-object-in-c/136265/2 "2022-01-17T18:02:22Z")

</div>

Hi @wortley.jb,

To modify the geometry used by an instance definition, you must use [InstanceDefinitionTable.ModifyGeometry](https://developer.rhino3d.com/api/RhinoCommon/html/Overload_Rhino_DocObjects_Tables_InstanceDefinitionTable_ModifyGeometry.htm).

Does this help?

– Dale

---

<div class="post-metadata">

**Author:** ![wortley.jb](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/wortley.jb/32/544345_2.png) [@wortley.jb](https://discourse.mcneel.com/u/wortley.jb)\
**Post date:** [February 15, 2022, 3:28am UTC](https://discourse.mcneel.com/t/how-can-i-replace-a-nested-instance-definition-object-in-c/136265/3 "2022-02-15T03:28:05Z")

</div>

Thanks Dale,

I haven’t had time to work on this again, but will give that a try next!
