Correct method to update existing Instance Reference (blocks) [SOLVED]

Hi can anyone show me the right method to update Instance References in RhinoDoc.ActiveDoc. I am Currently using the ModifyGeometry method, but the only result I get so far is to empty all the blocks!!

any idea abou the right procedure?
thanks in advance!

Hi @Federico_Giacomarra,

Does this help?

import Rhino
import scriptcontext as sc

def TestModifyInstanceDefinition():
    # Find the instance definition
    idef = sc.doc.InstanceDefinitions.Find("test")
    if not idef: return
    
    # Placeholders for new and existing stuff
    geometry = []
    attributes = []
    
    # Add some new geometry
    circle = Rhino.Geometry.Circle(Rhino.Geometry.Plane.WorldXY, 1.0)
    curve = Rhino.Geometry.ArcCurve(circle)
    geometry.append(curve)
    attributes.append(sc.doc.CreateDefaultAttributes())
    
    # Get/copy/add the existing geometry
    for rh_obj in idef.GetObjects():
        geometry.append(rh_obj.Geometry.Duplicate())
        attributes.append(rh_obj.Attributes)
    
    # Modify the instance defintion
    sc.doc.InstanceDefinitions.ModifyGeometry(idef.Index, geometry, attributes)
    sc.doc.Views.Redraw()
    
TestModifyInstanceDefinition()    

– Dale

1 Like

Thanks Dale!!!
That is exactly what i was looking for ! I was missig the whole part about the attributes, I will give it a try as soon!

all right! its works perfectly, using GH python I had just to replace the sc.doc. with Rhino.RhinoDoc.ActiveDoc . Now I am trying to retrieve the existing elements in the idef, and perform a certain operation on the rh_obj according to the type of geometry and layer name. What should i do next?

I don´t really know this part of the library, and working with the active doc is even more confusing. My only problem is not I am not able to perfom any operation that I normally do with the Rhino.Geometry. I am not any more in gh geometry let`s say, but on the active doc right? if I get the type of the rh_obj in the InstanceDefinition, after the GetObjects, I get ‘CurvedObject’ ( the circle you created) and ‘ExtrusionObject’ (my “test” block is a cube).what objects these are? What is this part of the library?

I am not sure how to retrieve the layer attributes from the rh_obj to create an if condition ( for example) and perfom geometric operation ( get one side of the box in the block and extrude it, for example) on it. Do I need to bring the rh_obj to the Rhino.Geonmetry ?

thanks again for the support!

Nice!
with a simple rh_obj.Geometry I made it work!

I post the whole thing again:

import Rhino
import scriptcontext as sc

def TestModifyInstanceDefinition():
    # Find the instance definition
    
    idef = Rhino.RhinoDoc.ActiveDoc.InstanceDefinitions.Find("test")
    if not idef: return
    
    # Placeholders for new and existing stuff
    geometry = []
    attributes = []
    
    # Add some new geometry
    circle = Rhino.Geometry.Circle(Rhino.Geometry.Plane.WorldXY, 1.0)
    curve = Rhino.Geometry.ArcCurve(circle)
    #geometry.append(curve)
    
    attributes.append(Rhino.RhinoDoc.ActiveDoc.CreateDefaultAttributes())
    
    # Get/copy/add the existing geometry
    
    
    for rh_obj in idef.GetObjects():
        
        geo= rh_obj.Geometry
        
        bre=Rhino.Geometry.Extrusion.ToBrep(geo,False)
        edg=bre.Edges
        lin= Rhino.Geometry.BrepEdge.ToNurbsCurve(edg[2])
        ext=Rhino.Geometry.Extrusion.Create(lin,2,False)
        
        geometry.append(rh_obj.Geometry.Duplicate())
        geometry.append(ext)
        attributes.append(rh_obj.Attributes)
    
    # Modify the instance defintion
    Rhino.RhinoDoc.ActiveDoc.InstanceDefinitions.ModifyGeometry(idef.Index, geometry, attributes)
    Rhino.RhinoDoc.ActiveDoc.Views.Redraw()
    
    
TestModifyInstanceDefinition()
1 Like