Is this the best way to automatically update linked blocks?

Blockmanager isn’t very effective in updating blocks so I wrote a small script,
but does this seem like the most reliable and effective way to do so?

import rhinoscriptsyntax as rs

blockList=rs.BlockNames()

if blockList:
    n=0
    nn=0
    
    for block in blockList:
        status=rs.BlockStatus(block)
        if status==-3:
            n=n+1
        if status==1:
            nn=nn+1
            print ("Updating "+str(block) )
            rs.Command("-BlockManager _Update "+str(block)+" _Enter")
    
    print "File has "+str(len(blockList) - n )+" linked blocks, "+str(nn)+" updated"

print "File has "+str(len(blockList) - n )+" linked blocks, "+str(nn)+" updated"

Why?

Well, basically to ensure the best speed and stability.
I could not find a dedicated command for it and I’m trying to avoid using Rhino.Command as much as possible, so I just wondered.

We need an effective and good way to update changed linked blocks.
Blockmanager is ineffective at doing so, and it messes with the layer structure so I don’t want to use “update all” (it doesn’t remember which layers are on and which are off) so that will be the next step for the script.

Hi Jorgen,

To avoid using a scripted command use the below code:

import rhinoscriptsyntax as rs
import scriptcontext

block_table=scriptcontext.doc.InstanceDefinitions

if block_table:
    updated=0
    not_linked = 0
    
    for block in block_table:
        
        status = int(block.ArchiveFileStatus)
        
        if status == -3:
            not_linked += 1
            continue
            
        if status == 1:
            updated += 1
            print ("Updating {}".format(block.Name) )
            block_table.UpdateLinkedInstanceDefinition(block.Index,block.SourceArchive,block.SkipNestedLinkedDefinitions,False)
    
    print "File has {} linked block(s), {} updated".format(block_table.Count - not_linked ,updated)


I did not add comments, but let me know if you need clarification.

-Willem

2 Likes

Nice, Willem, thanks as always!
I never figure out how to find what I need in scriptcontext, how do you do it?

Thanks a lot!

Hi @Willem,
working perfectly fine.
I have a question though - and I guess it has been discussed in some other topics here.

Is there away to avoid this popup asking what to do with nested blocks?
I personally would like to chose option “Replace with imported block” all the time.

grafik

Thanks in adavance…
T.

1 Like