From ef57c209a05451c5baa67b859aefad3a213c8a9c Mon Sep 17 00:00:00 2001 From: Andrej730 Date: Wed, 21 Jun 2023 17:30:34 +0500 Subject: [PATCH] More options to save .ifc as relative path in blender --- .../blenderbim/bim/module/project/operator.py | 11 +++++++---- src/blenderbim/blenderbim/bim/operator.py | 14 +++++++++----- src/blenderbim/blenderbim/bim/ui.py | 18 +++++++++++++++++- 3 files changed, 33 insertions(+), 10 deletions(-) diff --git a/src/blenderbim/blenderbim/bim/module/project/operator.py b/src/blenderbim/blenderbim/bim/module/project/operator.py index 42b8dbb4e9..ab6446af3f 100644 --- a/src/blenderbim/blenderbim/bim/module/project/operator.py +++ b/src/blenderbim/blenderbim/bim/module/project/operator.py @@ -74,19 +74,21 @@ class SelectLibraryFile(bpy.types.Operator, IFCFileSelector): filepath: bpy.props.StringProperty(subtype="FILE_PATH") filter_glob: bpy.props.StringProperty(default="*.ifc;*.ifczip;*.ifcxml", options={"HIDDEN"}) append_all: bpy.props.BoolProperty(default=False) + use_relative_path: bpy.props.BoolProperty(name="Use Relative Path", default=False) def execute(self, context): IfcStore.begin_transaction(self) old_filepath = IfcStore.library_path result = self._execute(context) - self.transaction_data = {"old_filepath": old_filepath, "filepath": self.filepath} + self.transaction_data = {"old_filepath": old_filepath, "filepath": self.get_filepath()} IfcStore.add_transaction_operation(self) IfcStore.end_transaction(self) return result def _execute(self, context): - IfcStore.library_path = self.filepath - IfcStore.library_file = ifcopenshell.open(self.filepath) + filepath = self.get_filepath() + IfcStore.library_path = filepath + IfcStore.library_file = ifcopenshell.open(filepath) bpy.ops.bim.refresh_library() if context.area: context.area.tag_redraw() @@ -525,11 +527,12 @@ class LoadProject(bpy.types.Operator, IFCFileSelector): filepath: bpy.props.StringProperty(subtype="FILE_PATH") filter_glob: bpy.props.StringProperty(default="*.ifc;*.ifczip;*.ifcxml", options={"HIDDEN"}) is_advanced: bpy.props.BoolProperty(name="Enable Advanced Mode", default=False) + use_relative_path: bpy.props.BoolProperty(name="Use Relative Path", default=False) def execute(self, context): if not self.is_existing_ifc_file(): return {"FINISHED"} - context.scene.BIMProperties.ifc_file = self.filepath + context.scene.BIMProperties.ifc_file = self.get_filepath() context.scene.BIMProjectProperties.is_loading = True context.scene.BIMProjectProperties.total_elements = len(tool.Ifc.get().by_type("IfcElement")) if not self.is_advanced: diff --git a/src/blenderbim/blenderbim/bim/operator.py b/src/blenderbim/blenderbim/bim/operator.py index f81124a7be..7c3eb67426 100644 --- a/src/blenderbim/blenderbim/bim/operator.py +++ b/src/blenderbim/blenderbim/bim/operator.py @@ -94,10 +94,11 @@ class SelectIfcFile(bpy.types.Operator, IFCFileSelector): bl_description = "Select a different IFC file" filepath: bpy.props.StringProperty(subtype="FILE_PATH") filter_glob: bpy.props.StringProperty(default="*.ifc;*.ifczip;*.ifcxml", options={"HIDDEN"}) + use_relative_path: bpy.props.BoolProperty(name="Use Relative Path", default=False) def execute(self, context): if self.is_existing_ifc_file(): - context.scene.BIMProperties.ifc_file = self.filepath + context.scene.BIMProperties.ifc_file = self.get_filepath() return {"FINISHED"} def invoke(self, context, event): @@ -105,7 +106,7 @@ class SelectIfcFile(bpy.types.Operator, IFCFileSelector): return {"RUNNING_MODAL"} -class ReloadSelectedIfcFile(bpy.types.Operator, IFCFileSelector): +class ReloadSelectedIfcFile(bpy.types.Operator): bl_idname = "bim.reload_selected_ifc_file" bl_label = "Reload selected IFC File" bl_options = {"REGISTER", "UNDO"} @@ -113,9 +114,12 @@ class ReloadSelectedIfcFile(bpy.types.Operator, IFCFileSelector): filepath: bpy.props.StringProperty(subtype="FILE_PATH") def execute(self, context): - self.filepath = context.scene.BIMProperties.ifc_file - if self.is_existing_ifc_file(): - context.scene.BIMProperties.ifc_file = context.scene.BIMProperties.ifc_file + filepath = context.scene.BIMProperties.ifc_file + valid_file = os.path.exists(filepath) and "ifc" in os.path.splitext(filepath)[1].lower() + if not valid_file: + self.report({"ERROR"}, f"Couldn't find .ifc file by the path '{filepath}'") + return {"ERROR"} + context.scene.BIMProperties.ifc_file = context.scene.BIMProperties.ifc_file return {"FINISHED"} diff --git a/src/blenderbim/blenderbim/bim/ui.py b/src/blenderbim/blenderbim/bim/ui.py index 2ff02593d8..d930d4d20b 100644 --- a/src/blenderbim/blenderbim/bim/ui.py +++ b/src/blenderbim/blenderbim/bim/ui.py @@ -39,14 +39,23 @@ class IFCFileSelector: filepath = self.filepath return os.path.exists(filepath) and "ifc" in os.path.splitext(filepath)[1].lower() + def get_filepath(self): + """get filepath taking into account relative paths""" + if self.use_relative_path: + filepath = os.path.relpath(self.filepath, bpy.path.abspath("//")) + else: + filepath = self.filepath + return filepath + def draw(self, context): # Access filepath & Directory https://blender.stackexchange.com/a/207665 params = context.space_data.params # Decode byte string https://stackoverflow.com/a/47737082/ directory = Path(params.directory.decode("utf-8")) filepath = os.path.join(directory, params.filename) + layout = self.layout if self.is_existing_ifc_file(filepath): - box = self.layout.box() + box = layout.box() box.label(text="IFC Header Specifications", icon="INFO") header_data = IfcHeaderExtractor(filepath).extract() for key, value in header_data.items(): @@ -65,6 +74,13 @@ class IFCFileSelector: op.outfile = filepath[0:-4] + "-IFC4.ifc" op.schema = "IFC4" + if bpy.data.is_saved: + layout.prop(self, "use_relative_path") + else: + self.use_relative_path = False + layout.label(text="Save the .blend file first ") + layout.label(text="to use relative paths for .ifc.") + class BIM_PT_section_plane(Panel): bl_label = "Temporary Section Cutaways"