Description

Select blocks according to a prefix. Useful if blocks are correctly named, for example if all 3D joinery blocks are named with a prefix “3D - MI”, it allows to select them all.

Author and license

Script by MLAV.LAND, licensed under the GNU GPL 3 License.

Code

import rhinoscriptsyntax as rs
 
def select_blocks_starting_with(prefix):
    rs.EnableRedraw(False)
    # Get all block definitions in the document
    block_definitions = rs.BlockNames()
    
    if block_definitions:
        for block_name in block_definitions:
            # Check if the block name starts with the specified prefix
            if block_name.startswith(prefix):
                # Get all instances (insert points) of this block definition
                block_instances = rs.BlockInstances(block_name)
                # Select each instance of the block
                for instance in block_instances:
                    rs.SelectObject(instance)
 
def main():
    # Prompt the user to enter a string
    user_input = rs.StringBox("Enter the prefix for the block names", "3D - MI", "Block Prefix")
    if user_input:
        select_blocks_starting_with(user_input)
    else:
        print("No prefix entered. Operation cancelled.")
 
if __name__ == "__main__":
    main()