pythonautocaddxfezdxf

Is there a way to group entities by BLOCK in a dxf file with ezdxf library?


I want to parse and group all entities inside a dxf file by BLOCK and then print each BLOCK's name with the entities that make up that block underneath. Is there a way to do this using the ezdxf library (maybe the groupby() function) ? Entities that do not make up a block don't need to be grouped nor printed.

I've tried using the groupby() function but didn't find a way to group by BLOCKS


Solution

  • import ezdxf
    
    def main(filename: str) -> None:
        doc = ezdxf.readfile(filename)
        for block in doc.blocks:
            print(f"Block name: {block.name}")
            print(f"BLOCK entity: {str(block.block)}")
            print(f"ENDBLK entity: {str(block.endblk)}")
            print("Block content entities:")
            print("-----------------------")
            for entity in block:
                print(f"  {str(entity)}")
    
    if __name__ == "__main__":
        main("your.dxf")
    
    

    Please read the docs: https://ezdxf.readthedocs.io/en/stable/