bim.load_project to support relative paths #5407

This commit is contained in:
Andrej730
2024-10-15 17:56:11 +05:00
parent 8940ec7b80
commit 12742bd08c
2 changed files with 25 additions and 10 deletions
@@ -676,7 +676,11 @@ class LoadProject(bpy.types.Operator, IFCFileSelector):
description="Load IFC file with advanced settings. Checking this option will skip loading IFC file and will open advanced load settings",
default=False,
)
use_relative_path: bpy.props.BoolProperty(name="Use Relative Path", default=False)
use_relative_path: bpy.props.BoolProperty(
name="Use Relative Path",
description="Store the IFC project path relative to the .blend file. Requires .blend file to be saved",
default=False,
)
should_start_fresh_session: bpy.props.BoolProperty(
name="Should Start Fresh Session",
description="Clear current Blender session before loading IFC",
@@ -757,6 +761,7 @@ class LoadProject(bpy.types.Operator, IFCFileSelector):
def finish_loading_project(self, context):
try:
if not self.is_existing_ifc_file():
self.report({"ERROR"}, f"Couldn't find IFC file: '{self.get_filepath()}'.")
return {"FINISHED"}
if self.should_start_fresh_session and tool.Blender.is_default_scene():
@@ -767,12 +772,11 @@ class LoadProject(bpy.types.Operator, IFCFileSelector):
if not self.is_advanced and not self.should_start_fresh_session:
bpy.ops.bim.convert_to_blender()
filepath = Path(self.get_filepath())
context.scene.BIMProperties.ifc_file = filepath.as_posix()
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"))
tool.Blender.register_toolbar()
tool.Project.add_recent_ifc_project(filepath.absolute())
tool.Project.add_recent_ifc_project(self.get_filepath_abs())
if self.is_advanced:
pass
+17 -6
View File
@@ -39,6 +39,7 @@ from typing import Optional
class IFCFileSelector:
filepath: str
use_relative_path: bool
def is_existing_ifc_file(self, filepath: Optional[str] = None) -> bool:
"""Check if file path exists and if it's an IFC file.
@@ -46,16 +47,26 @@ class IFCFileSelector:
If `filepath` is not provided, will use filepath property from the current operator.
"""
if filepath is None:
filepath = self.filepath
return os.path.exists(filepath) and "ifc" in os.path.splitext(filepath)[1].lower()
path = self.get_filepath_abs()
else:
path = Path(filepath)
return path.exists() and "ifc" in path.suffix.lower()
def get_filepath_abs(self) -> Path:
# self.filepath filled by fileselect_add is absolute
# but we support relative paths provided by custom scripts.
filepath = Path(self.filepath)
if not filepath.is_absolute():
filepath = Path(bpy.path.abspath("//")) / filepath
return filepath
def get_filepath(self) -> str:
"""get filepath taking into account relative paths"""
filepath = self.get_filepath_abs()
if self.use_relative_path:
filepath = os.path.relpath(self.filepath, bpy.path.abspath("//"))
else:
filepath = self.filepath
return filepath
filepath = filepath.relative_to(bpy.path.abspath("//"))
return filepath.as_posix()
def draw(self, context):
# Access filepath & Directory https://blender.stackexchange.com/a/207665