Add Block Instance into file

Good afternoon sirs!

I’m currently experimenting on how I can exchange information from rhino to python and the other way arround. So I was testing how to write a block instance into a 3dm file.
Here is what I coded so far:

    success, block = check_block_in_file3dm(True, block_name, rhino_file3dm)
    rhino_layer = Rhino.DocObjects.Layer()
    rhino_layer.Name = 'objects'
    rhino_layer.Color = System.Drawing.Color.FromArgb(0, 227, 144)
    file3dm.AllLayers.Add(rhino_layer)
    rhino_object_attributes = Rhino.DocObjects.ObjectAttributes()
    rhino_object_attributes.LayerIndex = rhino_layer.Index
    rhino_object_attributes.Name = block_name
    xform = rg.Transform()
    xform.M03 = 1
    xform.M13 = 1
    xform.M23 = 0
    file3dm.Objects.AddInstanceObject(block.Index, xform, rhino_object_attributes)
    file3dm.Write("test_exports/test-export-to-rhino.3dm", 6)

Where check_block_in_file3dm(True, block_name, rhino_file3dm) is:

def check_block_in_file3dm(prev_success: bool, block_name: str, file3dm):
    success = False
    block = None
    all_blocks = file3dm.AllInstanceDefinitions
    for b in all_blocks:
        if b.Name == block_name:
            success = True
            block = b
    return(success, block)

The ‘block’ variable used is a valid InstanceDefinitionGeometry and the code runs without erros, but the 3dm file created has no objects in it. Any ideia on where the problem is?
I guess that I still don’t understand how to add InstanceObjects into a document.

Thank you.

Hi @thomas.takeuchi,

Do you really need to write an instance definition to a 3dm file? Doing so will cause Rhino to created a nested block upon insertion.

– Dale

Hello @dale, thank you for the reply!

The idea was to have a central file, template, with all needed blocks, like so:

And then, when needed, place one of those blocks into a new, empty, file.
Would there be another way to achieve the same intented objective?

-Thomas

Imo a better way would be to store every block as a single file and just insert each file as you need it. You can script the insert command or do it with rhino common.

Hello rgr,

Thank you for replying. Could you give me an example on how I could do that?
Is there an Insert Method in Rhino Common?

-Thomas

Hi,
Maybe have a look at this thread:

Basicly what you are doing is a) using the RhinoScript Method
oe b) see the link @jeffoulet just posted.

Thank you very much @rgr @jeffoulet!
I’ll definitely try that!

-Thomas