import ifcopenshell from ifcopenshell.util.selector import filter_elements from ifccsv import IfcCsv import os # ------------------------------------------- # 1. Load IFC models into a list of dictionaries # ------------------------------------------- def load_model_list(model_paths: list[str]) -> list[dict]: return [ { "model": ifcopenshell.open(path), "name": os.path.splitext(os.path.basename(path))[0], "path": path } for path in model_paths ] # ------------------------------------------- # 2. Export filtered elements from a model to a CSV in the same folder # ------------------------------------------- def export_model_to_csv(model, model_name: str, model_path: str, query: str, attributes: list[str]) -> None: elements = filter_elements(model, query) if not elements: print(f"āš ļø No matching elements in model '{model_name}'") return model_dir = os.path.dirname(model_path) output_file = os.path.join(model_dir, f"{model_name}_extract.csv") ifc_csv = IfcCsv() ifc_csv.export( model, elements, attributes, output=output_file, format="csv", delimiter=",", null="-" ) print(f"āœ… Exported model '{model_name}' to:\n {output_file}") # ------------------------------------------- # 3. Main function # ------------------------------------------- def main(): model_paths = [ r"path to ifc file\filename1.ifc", r"path to ifc file\filename2.ifc" ] query = 'IfcWall, type=/WAL-.*/' attributes = ["Name", "type.Name", "Qto_WallBaseQuantities.NetSideArea"] models = load_model_list(model_paths) print("\nšŸ“‚ Loaded Models:") for m in models: print(f"- {m['name']}: {m['path']}") for m in models: export_model_to_csv( model=m["model"], model_name=m["name"], model_path=m["path"], query=query, attributes=attributes ) # ------------------------------------------- # Run the script # ------------------------------------------- if __name__ == "__main__": main()