Script to convert embedded blocks to linked?

Hi guys!

I am starting a new thread for this matter but I actually found one where this was pretty much adressed except that I need to convert many embedded blocks into linked ones.

The script supplied in that thread only works for individuals

I imagine that if you have a file with all the blocks you wish to link inserted, then its just a matter of matching names:

You could select all your 200 embedded blocks on your current file, and when asked which file to obtain the linked blocks from, this file should have the same blocks from your original file, with the exact same name.

import rhinoscriptsyntax as rs

def LinkBlock():

Ids = rs.GetObjects("Select blocks to link.", filter=4096, preselect =True)
if Ids is None: return

path = "yourfilepathhere"

for Id in Ids:
    name = rs.BlockInstanceName(Id)
    filepath = path + "\\" + name + ".3dm"
    rs.Command("_-BlockManager _Properties " + chr(34) + name + chr(34) + " _UpdateType _Linked filepath _Enter _Enter", False)

if name == “main”:
LinkBlock()

I can’t test it right now, but something along this should work. you obviously need to replace path with the folder for your parts and have the files named the same as your blocks.Also, double “\” so it won’t escape the sequence.

if you want to browse for each file individually, use:

rs.Command(“_-BlockManager _Properties " + chr(34) + name + chr(34) + " _UpdateType _Linked _Browse _Enter _Enter”, False)

You could also construct a raw string literals so you don’t have to double-escape:

Hi, thank you!

I am afraid I don’t understand one thing though. Do I need multiple files, (One file for each block) ?
Can’t all blocks be inside one single document and use that document to replace the blocks on the other one?

Thanks in advance!

You have to specify the file for each block, but you could just replace the “filepath” in the RS.Command line like so:

rs.Command(“_-BlockManager _Properties " + chr(34) + name + chr(34) + " _UpdateType _Linked path _Enter _Enter”, False)

and change the path to point at the .3dm file, not just the directory.

I can’t make it work :sweat:

    import rhinoscriptsyntax as rs

    def LinkBlock():

    Ids = rs.GetObjects("Select blocks to link.", filter=4096, preselect =True)
    if Ids is None: return

    path = "C:\Users\Shynn\Desktop\BlocksToLink.3dm"

    for Id in Ids:
        name = rs.BlockInstanceName(Id)
        filepath = path + "\\" + name + ".3dm"
        rs.Command("_-BlockManager _Properties " + chr(34) + name + chr(34) + " _UpdateType _Linked path _Enter _Enter", False)

    if name == “main”:
    LinkBlock()

I get this error:

File "C:\Users\Shynn\Desktop\ReplaceBlocksnew.py", line                
SyntaxError: Non-ASCII character '\xe2' in file C:\Users\Shynn\Desktop\ReplaceBlocksnew.py on line 17, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

ReplaceBlocksnew.py (518 Bytes)

Hi, could be a file encoding problem? I get garbled characters in place of the underscores in __name__

    if name == “main”:
    LinkBlock()

Should be:

if __name__ == '__main__':
    LinkBlock()