Replace object in a block

Hello,

I have posted a similar question at the end of this topic:

But maybe it is more visible inside a new topic.

I would like to replace an object which is not inside Document.Objects, but part of an (or more) InstanceDefinition(s).
How should I do it?
For a normal object I can use Document.Objects.Replace, but because this object is not registered in Document.Objects this doesn’t work for me.

Is there a list for RhinoObjects which are not in Document.Objects but belongs to one or more blocks? What I can see this object has Id and also Document property, so it is attached to the document.

Márton

#Python
from scriptcontext import doc
import Rhino.Geometry as rg
import Rhino

def ModifyInstanceDefinition():
    rc, objref = Rhino.Input.RhinoGet.GetOneObject("Select instance", False, Rhino.DocObjects.ObjectType.InstanceReference)
    if rc != Rhino.Commands.Result.Success: return
    iref = objref.Object()
    if not iref: return
    idef = iref.InstanceDefinition
    geos = [obj.Geometry for obj in idef.GetObjects()]
    geos[0] = rg.Sphere(rg.Plane.WorldXY, 3).ToBrep()
    i = doc.InstanceDefinitions.InstanceDefinitionIndex(idef.Id, True)
    doc.InstanceDefinitions.ModifyGeometry(i, geos)
if __name__=="__main__":
    ModifyInstanceDefinition()
//C#
protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
  var rc = RhinoGet.GetOneObject("Select instance", false, ObjectType.InstanceReference, out var objRef);
  if (rc != Result.Success) return Result.Nothing;
  var iRef = (InstanceObject)objRef.Object();
  if (iRef == null) return Result.Nothing;
  var iDef = iRef.InstanceDefinition;
  var geos = iDef.GetObjects().Select(obj => obj.Geometry).ToList();
  geos[0] = new Sphere(Plane.WorldXY, 3).ToBrep();
  var i = doc.InstanceDefinitions.InstanceDefinitionIndex(iDef.Id, true);
  doc.InstanceDefinitions.ModifyGeometry(i, geos);
  return Result.Success;
}

ModifyInstanceDefinition.py (667 Bytes)
ModifyInstanceDefinition.cs (1.3 KB)

Hello Mahdiyar,

Thanks for the good example, but unfortunately my problem is mode difficult.

Replacing the geometry itself is not enough:(.
I have a CustomMeshObject and I would like to replace the MeshObject inside the block to my CustomMeshObject.

Márton