From dafed93c636454ed83ecacb7fb51bf4eb3c7a0e5 Mon Sep 17 00:00:00 2001 From: Roy-043 <70520633+Roy-043@users.noreply.github.com> Date: Tue, 3 Feb 2026 17:16:10 +0100 Subject: [PATCH] BIM: support relative path for hybrid IFC file (#24190) --- src/Mod/BIM/nativeifc/ifc_tools.py | 47 ++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/src/Mod/BIM/nativeifc/ifc_tools.py b/src/Mod/BIM/nativeifc/ifc_tools.py index 4bbeffc405..84cdc4a0a9 100644 --- a/src/Mod/BIM/nativeifc/ifc_tools.py +++ b/src/Mod/BIM/nativeifc/ifc_tools.py @@ -448,23 +448,33 @@ def get_ifcfile(obj): """Returns the ifcfile that handles this object""" project = get_project(obj) - if project: + if project is None: + return None + if getattr(project, "Proxy", None): + if hasattr(project.Proxy, "ifcfile"): + return project.Proxy.ifcfile + if getattr(project, "IfcFilePath", None): + filepath = project.IfcFilePath + if filepath[0] == ".": + # path relative to the FreeCAD file directory + filepath = os.path.join(os.path.dirname(obj.Document.FileName), filepath) + # absolute path + filepath = os.path.abspath(filepath) + try: + ifcfile = ifcopenshell.open(filepath) + except OSError: + FreeCAD.Console.PrintError("Error: Cannot open IFC file: " + filepath + "\n") + return None + if hasattr(project, "Proxy"): + if project.Proxy is None: + if not isinstance(project, FreeCAD.DocumentObject): + project.Proxy = ifc_objects.document_object() if getattr(project, "Proxy", None): - if hasattr(project.Proxy, "ifcfile"): - return project.Proxy.ifcfile - if getattr(project, "IfcFilePath", None): - ifcfile = ifcopenshell.open(project.IfcFilePath) - if hasattr(project, "Proxy"): - if project.Proxy is None: - if not isinstance(project, FreeCAD.DocumentObject): - project.Proxy = ifc_objects.document_object() - if getattr(project, "Proxy", None): - project.Proxy.ifcfile = ifcfile - return ifcfile - else: - FreeCAD.Console.PrintError( - "Error: No IFC file attached to this project: " + project.Label - ) + project.Proxy.ifcfile = ifcfile + return ifcfile + FreeCAD.Console.PrintError( + "Error: No IFC file attached to this project: " + project.Label + "\n" + ) return None @@ -1179,6 +1189,11 @@ def save_ifc(obj, filepath=None): if not filepath: if getattr(obj, "IfcFilePath", None): filepath = obj.IfcFilePath + if filepath[0] == ".": + # path relative to the FreeCAD file directory + filepath = os.path.join(os.path.dirname(obj.Document.FileName), filepath) + # absolute path + filepath = os.path.abspath(filepath) if filepath: ifcfile = get_ifcfile(obj) if not ifcfile: