Organized Block / Organize Blocks

After following this discussion, I’ve made a script that you can use to create a block definition layer structure when creating a block.
And another to organize blocks onto layers with their block name
@pascal @Helvetosaur what do you think?

organizedBlock.py (1.6 KB)

see the code
import rhinoscriptsyntax as rs
import Rhino


def organizedBlock():
    """
    create a block from selection and add it to a layer with the block's name
    nested under layer Block_Definitions
    tested in Rhino 6.14
    www.studiogijs.nl
    """
    
    #get objects to create block from
    objs = rs.GetObjects("select objects to creat block from")
    if not objs: return
    
    base_point = rs.GetPoint("block base point")
    if not base_point: return
    
    def checkName():    
        block_name = rs.GetString("enter block name")
        if block_name =='' or block_name==None:
            print "block name can't be empty"
            return checkName()
        #check if layer Block_Definitions exist, else create it
        if not rs.IsLayer("Block_Definitions"): rs.AddLayer("Block_Definitions")
        
        #check if layer with block name exists, else create it
        if not rs.IsLayer("Block_Definitions::"+block_name):
            block_layer = rs.AddLayer(block_name, parent = "Block_Definitions")
            
        else:
            print "block definition with this name already exists"
            return checkName()
        return block_layer, block_name
        
            
    block_layer, block_name = checkName()
    
    #create the block
    block = rs.AddBlock(objs, base_point, block_name, True)
    if not block:
        return
    temp_layer = rs.CurrentLayer()
    rs.CurrentLayer(block_layer)
    rs.InsertBlock(block, base_point)
    #restore to previous layer
    rs.CurrentLayer(temp_layer)

if __name__ == '__main__':
    organizedBlock()

organizeBlocks.py (1.0 KB)

see the code
import rhinoscriptsyntax as rs
import Rhino

def organizeBlocks():
    """
    organize blocks onto layers with their blockname nested under layer Block_Definitions
    tested in Rhino 6.14
    www.studiogijs.nl
    """
    blocks = Rhino.RhinoDoc.ActiveDoc.Objects.FindByObjectType(Rhino.DocObjects.ObjectType.InstanceReference)
    if not blocks:
        print 'no blocks found in this document'
        return
    for block in blocks:
        block_name = block.InstanceDefinition.Name
        #check if layer Block_Definitions exist, else create it
        if not rs.IsLayer("Block_Definitions"): rs.AddLayer("Block_Definitions")
        
        #check if layer with block name exists, else create it
        layer_name = "Block_Definitions::"+block_name
        if not rs.IsLayer(layer_name):
            block_layer = rs.AddLayer(block_name, parent = "Block_Definitions")
        else:
            block_layer = layer_name
        rs.ObjectLayer(block.Id, block_layer)
        
if __name__ == '__main__':
    organizeBlocks()
3 Likes

hello Gijs

I’m using rhino7. Would that work for me?

In principle yes, let me know if it doesn’t

I think it doesnt. I unblock it in the file properties already

seems to work here (almost). The almost being that the nesting of block layer names is not working… trying to find the cause. What doesn’t work for you? Do you get any error messages? How are you running these scripts? Did you try with RunPythonScript first?

I put the file into the plug-in folder of rhino7 but nothing happens. And I can’t find it in the toolbar. So I drag the file to the canvas of rhino and got this message.

That’s not how it works. See this extensive info by @Helvetosaur:

Running Python scripts from aliases or toolbar buttons