mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-09-27 18:57:17 +00:00
bim.load_project to support relative paths #5407
This commit is contained in:
@@ -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",
|
description="Load IFC file with advanced settings. Checking this option will skip loading IFC file and will open advanced load settings",
|
||||||
default=False,
|
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(
|
should_start_fresh_session: bpy.props.BoolProperty(
|
||||||
name="Should Start Fresh Session",
|
name="Should Start Fresh Session",
|
||||||
description="Clear current Blender session before loading IFC",
|
description="Clear current Blender session before loading IFC",
|
||||||
@@ -757,6 +761,7 @@ class LoadProject(bpy.types.Operator, IFCFileSelector):
|
|||||||
def finish_loading_project(self, context):
|
def finish_loading_project(self, context):
|
||||||
try:
|
try:
|
||||||
if not self.is_existing_ifc_file():
|
if not self.is_existing_ifc_file():
|
||||||
|
self.report({"ERROR"}, f"Couldn't find IFC file: '{self.get_filepath()}'.")
|
||||||
return {"FINISHED"}
|
return {"FINISHED"}
|
||||||
|
|
||||||
if self.should_start_fresh_session and tool.Blender.is_default_scene():
|
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:
|
if not self.is_advanced and not self.should_start_fresh_session:
|
||||||
bpy.ops.bim.convert_to_blender()
|
bpy.ops.bim.convert_to_blender()
|
||||||
|
|
||||||
filepath = Path(self.get_filepath())
|
context.scene.BIMProperties.ifc_file = self.get_filepath()
|
||||||
context.scene.BIMProperties.ifc_file = filepath.as_posix()
|
|
||||||
context.scene.BIMProjectProperties.is_loading = True
|
context.scene.BIMProjectProperties.is_loading = True
|
||||||
context.scene.BIMProjectProperties.total_elements = len(tool.Ifc.get().by_type("IfcElement"))
|
context.scene.BIMProjectProperties.total_elements = len(tool.Ifc.get().by_type("IfcElement"))
|
||||||
tool.Blender.register_toolbar()
|
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:
|
if self.is_advanced:
|
||||||
pass
|
pass
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ from typing import Optional
|
|||||||
|
|
||||||
class IFCFileSelector:
|
class IFCFileSelector:
|
||||||
filepath: str
|
filepath: str
|
||||||
|
use_relative_path: bool
|
||||||
|
|
||||||
def is_existing_ifc_file(self, filepath: Optional[str] = None) -> bool:
|
def is_existing_ifc_file(self, filepath: Optional[str] = None) -> bool:
|
||||||
"""Check if file path exists and if it's an IFC file.
|
"""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 not provided, will use filepath property from the current operator.
|
||||||
"""
|
"""
|
||||||
if filepath is None:
|
if filepath is None:
|
||||||
filepath = self.filepath
|
path = self.get_filepath_abs()
|
||||||
return os.path.exists(filepath) and "ifc" in os.path.splitext(filepath)[1].lower()
|
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:
|
def get_filepath(self) -> str:
|
||||||
"""get filepath taking into account relative paths"""
|
"""get filepath taking into account relative paths"""
|
||||||
|
filepath = self.get_filepath_abs()
|
||||||
|
|
||||||
if self.use_relative_path:
|
if self.use_relative_path:
|
||||||
filepath = os.path.relpath(self.filepath, bpy.path.abspath("//"))
|
filepath = filepath.relative_to(bpy.path.abspath("//"))
|
||||||
else:
|
return filepath.as_posix()
|
||||||
filepath = self.filepath
|
|
||||||
return filepath
|
|
||||||
|
|
||||||
def draw(self, context):
|
def draw(self, context):
|
||||||
# Access filepath & Directory https://blender.stackexchange.com/a/207665
|
# Access filepath & Directory https://blender.stackexchange.com/a/207665
|
||||||
|
|||||||
Reference in New Issue
Block a user