From fd603f7035a6310472460e6186874305f5ffb873 Mon Sep 17 00:00:00 2001 From: Dion Moult Date: Sat, 21 Aug 2021 16:16:53 +1000 Subject: [PATCH] IfcZip and IfcXML can now be set manually, and prevent double loading when importing, which can save ~20 seconds for large file imports. --- src/blenderbim/blenderbim/bim/handler.py | 3 +-- src/blenderbim/blenderbim/bim/ifc.py | 19 +++++++++++++++++-- src/blenderbim/blenderbim/bim/import_ifc.py | 21 +-------------------- 3 files changed, 19 insertions(+), 24 deletions(-) diff --git a/src/blenderbim/blenderbim/bim/handler.py b/src/blenderbim/blenderbim/bim/handler.py index b56636e9f2..50dfea5ff8 100644 --- a/src/blenderbim/blenderbim/bim/handler.py +++ b/src/blenderbim/blenderbim/bim/handler.py @@ -153,8 +153,7 @@ def purge_module_data(): def loadIfcStore(scene): IfcStore.purge() purge_module_data() - ifc_file = IfcStore.get_file() - if not ifc_file: + if not IfcStore.get_file(): return IfcStore.get_schema() IfcStore.reload_linked_elements() diff --git a/src/blenderbim/blenderbim/bim/ifc.py b/src/blenderbim/blenderbim/bim/ifc.py index 58c173acbf..e779befc27 100644 --- a/src/blenderbim/blenderbim/bim/ifc.py +++ b/src/blenderbim/blenderbim/bim/ifc.py @@ -60,11 +60,26 @@ class IfcStore: IfcStore.path = bpy.context.scene.BIMProperties.ifc_file if IfcStore.path: try: - IfcStore.file = ifcopenshell.open(IfcStore.path) + IfcStore.load_file(IfcStore.path) except: - IfcStore.file + pass return IfcStore.file + @staticmethod + def load_file(path): + extension = path.split(".")[-1] + if extension.lower() == "ifczip": + with tempfile.TemporaryDirectory() as unzipped_path: + with zipfile.ZipFile(path, "r") as zip_ref: + zip_ref.extractall(unzipped_path) + for filename in Path(unzipped_path).glob("**/*.ifc"): + IfcStore.file = ifcopenshell.open(filename) + return + elif extension.lower() == "ifcxml": + IfcStore.file = ifcopenshell.file(ifcopenshell.ifcopenshell_wrapper.parse_ifcxml(path)) + elif extension.lower() == "ifc": + IfcStore.file = ifcopenshell.open(path) + @staticmethod def get_schema(): if IfcStore.file is None: diff --git a/src/blenderbim/blenderbim/bim/import_ifc.py b/src/blenderbim/blenderbim/bim/import_ifc.py index 907beecd39..df92f37c47 100644 --- a/src/blenderbim/blenderbim/bim/import_ifc.py +++ b/src/blenderbim/blenderbim/bim/import_ifc.py @@ -214,8 +214,6 @@ class IfcImporter: self.profile_code("Purge diffs") self.load_file() self.profile_code("Loading file") - self.set_ifc_file() - self.profile_code("Setting file") self.calculate_unit_scale() self.profile_code("Calculate unit scale") self.calculate_model_offset() @@ -973,25 +971,8 @@ class IfcImporter: def load_file(self): self.ifc_import_settings.logger.info("loading file %s", self.ifc_import_settings.input_file) - extension = self.ifc_import_settings.input_file.split(".")[-1] - if extension.lower() == "ifczip": - with tempfile.TemporaryDirectory() as unzipped_path: - with zipfile.ZipFile(self.ifc_import_settings.input_file, "r") as zip_ref: - zip_ref.extractall(unzipped_path) - for filename in Path(unzipped_path).glob("**/*.ifc"): - self.file = ifcopenshell.open(filename) - break - elif extension.lower() == "ifcxml": - self.file = ifcopenshell.file( - ifcopenshell.ifcopenshell_wrapper.parse_ifcxml(self.ifc_import_settings.input_file) - ) - elif extension.lower() == "ifc": - self.file = ifcopenshell.open(self.ifc_import_settings.input_file) - IfcStore.file = self.file - - def set_ifc_file(self): bpy.context.scene.BIMProperties.ifc_file = self.ifc_import_settings.input_file - IfcStore.path = self.ifc_import_settings.input_file + self.file = IfcStore.get_file() def calculate_unit_scale(self): self.unit_scale = ifcopenshell.util.unit.calculate_unit_scale(self.file)