From 7c61c7d200fff1b32792a97fb17f3fdfb38049c5 Mon Sep 17 00:00:00 2001 From: Dion Moult Date: Mon, 22 Mar 2021 16:19:29 +1100 Subject: [PATCH] You can now import and export IFCCSV to the current file in memory without writing to disk --- .../blenderbim/bim/handler.py | 23 ++++++++++--------- .../blenderbim/bim/module/csv/operator.py | 9 +++++++- src/ifccsv/ifccsv.py | 8 +++---- 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/ifcblenderexport/blenderbim/bim/handler.py b/src/ifcblenderexport/blenderbim/bim/handler.py index 3bbd503a28..a54440a62f 100644 --- a/src/ifcblenderexport/blenderbim/bim/handler.py +++ b/src/ifcblenderexport/blenderbim/bim/handler.py @@ -46,6 +46,17 @@ def subscribe_to(object, data_path, callback): }, ) +def purge_module_data(): + from blenderbim.bim import modules + + for module in modules.values(): + if not module: + continue + try: + getattr(getattr(module, "data"), "Data").purge() + except AttributeError: + pass + @persistent def loadIfcStore(scene): @@ -58,17 +69,7 @@ def loadIfcStore(scene): IfcStore.guid_map = ( {k: bpy.data.objects.get(v) for k, v in json.loads(props.guid_map).items()} if props.id_map else {} ) - - # Purge data cache - from blenderbim.bim import modules - - for module in modules.values(): - if not module: - continue - try: - getattr(getattr(module, "data"), "Data").purge() - except AttributeError: - pass + purge_module_data() @persistent diff --git a/src/ifcblenderexport/blenderbim/bim/module/csv/operator.py b/src/ifcblenderexport/blenderbim/bim/module/csv/operator.py index 2ee88c9631..0ca726c7aa 100644 --- a/src/ifcblenderexport/blenderbim/bim/module/csv/operator.py +++ b/src/ifcblenderexport/blenderbim/bim/module/csv/operator.py @@ -7,6 +7,7 @@ import json import webbrowser import tempfile from blenderbim.bim.ifc import IfcStore +from blenderbim.bim.handler import purge_module_data class AddCsvAttribute(bpy.types.Operator): @@ -78,13 +79,19 @@ class ImportIfcCsv(bpy.types.Operator): def execute(self, context): import ifccsv + props = context.scene.CsvProperties + if props.should_load_from_memory: + ifc_file = IfcStore.get_file() + else: + ifc_file = ifcopenshell.open(props.csv_ifc_file) ifc_csv = ifccsv.IfcCsv() ifc_csv.output = self.filepath if props.csv_delimiter == "CUSTOM": ifc_csv.delimiter = props.csv_custom_delimiter else: ifc_csv.delimiter = props.csv_delimiter - ifc_csv.Import(context.scene.CsvProperties.csv_ifc_file) + ifc_csv.Import(ifc_file) + purge_module_data() return {"FINISHED"} diff --git a/src/ifccsv/ifccsv.py b/src/ifccsv/ifccsv.py index 52d30cee7b..0f9d2aa4b8 100755 --- a/src/ifccsv/ifccsv.py +++ b/src/ifccsv/ifccsv.py @@ -131,8 +131,7 @@ class IfcCsv: results.update([p.Name for p in element.Quantities]) return ["{}.{}".format(pset_qto_name, n) for n in results] - def Import(self, ifc): - ifc_file = ifcopenshell.open(ifc) + def Import(self, ifc_file): with open(self.output, newline="", encoding="utf-8") as f: reader = csv.reader(f, delimiter=self.delimiter) headers = [] @@ -149,7 +148,6 @@ class IfcCsv: if i == 0: continue # Skip GlobalId element = IfcAttributeSetter.set_element_key(ifc_file, element, headers[i], value) - ifc_file.write(ifc) if __name__ == "__main__": @@ -174,4 +172,6 @@ if __name__ == "__main__": elif getattr(args, "import"): ifc_csv = IfcCsv() ifc_csv.output = args.csv - ifc_csv.Import(args.ifc) + ifc_file = ifcopenshell.open(args.ifc) + ifc_csv.Import(ifc_file) + ifc_file.write(args.ifc)