[python] How do I delete block definitions

How do I delete block definitions using blocks_table.Clear() but only after reassuring that all blocks have been exploded?

I need to be sure no geometry objects are inside any block before deleting.

Do I need to loop through all blocks and check if there’s something inside?

You can easily use Purge command.

Thanks for the suggestions @Mahdiyar, but I do not want to delete empty layers and stuff

You can change this command settings to get the result you’re looking for:

_Purge _BlockDefinitions=Yes _AnnotationStyles=No _Groups=No HatchPatterns=No _Layers=No _Linetypes=No _Materials=No _Enter
1 Like

Thanks I solved it:


import rhinoscriptsyntax as rs
import scriptcontext as sc


def blockaecide():
    block_obj_inst_id = rs.GetObject("Select Block: ", rs.filter.instance)
    rs.ExplodeBlockInstance(block_obj_inst_id,explode_nested_instances=True)
    
    blocks_leftovers = [block_obj for block_obj in rs.AllObjects() if block_obj is rs.filter.instance]
    blocks_table = sc.doc.InstanceDefinitions
    print len(blocks_leftovers)
    print len(blocks_table)
    if len(blocks_leftovers) == 0 and len(blocks_table) != 0:
        sc.doc.InstanceDefinitions.Clear()


if __name__=="__main__":
    blockaecide()

1 Like