Insert block from other file... with python script

i’m trying to make python script…
but i really don’t know how to make it right.

here is my script

import Rhino
import rhinoscriptsyntax as rs

def InsertFile():
# insert options
file = chr(34) + r"my file location" + chr(34)
point = rs.GetPoint(“Block base point”)
scale = 1.0
rotation = 0.0

# command
cmd = "_-Insert _File=Yes _LinkMode=_Embedded {} ".format(file)
cmd += "Objects _Enter {0}".format(point)
rc = rs.Command(cmd, False)

rs.UnselectAllObjects()

InsertFile()

i want to bring blocks from other file, and want to pick the block’s point

and if it is possible i want to set default setting ‘# command’ parts

# command
cmd = "_-Insert _File=Yes _LinkMode=_Embedded {} ".format(file)
cmd += "Objects _Enter {0}".format(point)
rc = rs.Command(cmd, False)

over here. i don’t want to choose any options.

my English is short… please help me :frowning:

It looks like you are on the right track with your Python script. Here are a few suggestions to help you refine it:

  1. Make sure to use consistent quotation marks. In the InsertFile function, you are using both straight quotes (") and curly quotes (), which can cause syntax errors. It’s best to stick with straight quotes.
  2. You don’t need to use the chr function to add quotes around your file path. Instead, you can simply enclose the path in quotes, like this:
file = r'"my file location"'
  1. It’s a good idea to check if the rs.GetPoint function returns a valid point before proceeding with the rest of the script. Here’s an example:
point = rs.GetPoint("Block base point")
if point:
    # insert command here
else:
    print "No point selected"
  1. To set the default settings for the insert command, you can create a string that includes all of the command options and values, like this:
cmd = "_-Insert "
cmd += "_File=Yes "
cmd += "_LinkMode=_Embedded "
cmd += "{} ".format(file)
cmd += "Objects _Enter "
cmd += "{} ".format(point)
cmd += "_Scale={0} ".format(scale)
cmd += "_Rotation={0} ".format(rotation)
rc = rs.Command(cmd, False)

This will set the file, link mode, objects, point, scale, and rotation to their default values, but you can still override any of these settings by changing the values of the file, point, scale, and rotation variables before calling the rs.Command function.

To insert a block from another file :

import rhinoscriptsyntax as rs

def InsertBlock():
    # Get the path to the file that contains the block definition
    block_file = rs.OpenFileName("Select block definition file", "Rhino 3D Models (*.3dm)|*.3dm||")
    if not block_file:
        return

    # Get the name of the block to insert
    block_name = rs.GetString("Block name to insert")
    if not block_name:
        return

    # Insert the block at the current 3D cursor location
    insertion_point = rs.GetPoint("Insertion point")
    if insertion_point:
        rs.InsertBlock(block_file, block_name, insertion_point)

InsertBlock()

In this example, the rs.OpenFileName() function is used to prompt the user to select the file that contains the block definition. Once the file is selected, the rs.GetString() function is used to prompt the user for the name of the block to insert. Finally, the rs.GetPoint() function is used to prompt the user for the insertion point. Modify it to your use case

1 Like

wow…@ Farouk Serragedine

thank you so much!!
i’ll try it
thank you really

You’re welcome.
Have a good day.

@farouk.serragedine In case you are still around, I have a very similar or basically identical question, but cannot get it to work. Do you know if this is still possible? I simply cannot get it to work with your sample code and the InsertBlock documentation explicitly mentions that the block definition needs to exist in the file already?!