Show templates based on selected IFC schema #5530

This commit is contained in:
Andrej730
2024-10-04 16:46:57 +05:00
parent e5c83b1538
commit 38d450e111
2 changed files with 24 additions and 8 deletions
+17 -6
View File
@@ -22,6 +22,7 @@ import bonsai.tool as tool
import ifcopenshell.util.file import ifcopenshell.util.file
from bonsai.bim.ifc import IfcStore from bonsai.bim.ifc import IfcStore
from pathlib import Path from pathlib import Path
from collections import defaultdict
from typing import Union from typing import Union
@@ -40,9 +41,10 @@ class ProjectData:
cls.data = { cls.data = {
"export_schema": cls.get_export_schema(), "export_schema": cls.get_export_schema(),
"library_file": cls.library_file(), "library_file": cls.library_file(),
"template_file": cls.template_file(),
"last_saved": cls.last_saved(), "last_saved": cls.last_saved(),
} }
# After export_schema.
cls.data["template_file"] = cls.template_file()
cls.is_loaded = True cls.is_loaded = True
@classmethod @classmethod
@@ -77,11 +79,20 @@ class ProjectData:
return results return results
@classmethod @classmethod
def template_file(cls): def template_file(cls) -> dict[str, list[tuple[str, str, str]]]:
files = os.listdir(os.path.join(bpy.context.scene.BIMProperties.data_dir, "templates", "projects")) template_files: dict[str, list[tuple[str, str, str]]] = defaultdict(list)
results = [("0", "Blank Project", "")] for export_schema in cls.data["export_schema"]:
results.extend([(f, os.path.splitext(f)[0], "") for f in files if ".ifc" in f]) template_files[export_schema[0]].append(("0", "Blank Project", ""))
return results
files = (Path(bpy.context.scene.BIMProperties.data_dir) / "templates" / "projects").iterdir()
for f in files:
if not f.suffix.lower().startswith(".ifc"):
continue
current_schema = cls.get_file_schema(f)
if current_schema not in template_files:
continue
template_files[current_schema].append((f.name, f.stem, "Template"))
return template_files
@classmethod @classmethod
def last_saved(cls): def last_saved(cls):
+7 -2
View File
@@ -41,10 +41,15 @@ def get_export_schema(self: "BIMProjectProperties", context: bpy.types.Context)
return ProjectData.data["export_schema"] return ProjectData.data["export_schema"]
def update_export_schema(self: "BIMProjectProperties", context: bpy.types.Context) -> None:
# Avoid breaking empty enum.
self["template_file"] = 0
def get_template_file(self: "BIMProjectProperties", context: bpy.types.Context) -> list[tuple[str, str, str]]: def get_template_file(self: "BIMProjectProperties", context: bpy.types.Context) -> list[tuple[str, str, str]]:
if not ProjectData.is_loaded: if not ProjectData.is_loaded:
ProjectData.load() ProjectData.load()
return ProjectData.data["template_file"] return ProjectData.data["template_file"][self.export_schema]
def get_library_file(self: "BIMProjectProperties", context: bpy.types.Context) -> list[tuple[str, str, str]]: def get_library_file(self: "BIMProjectProperties", context: bpy.types.Context) -> list[tuple[str, str, str]]:
@@ -201,7 +206,7 @@ class BIMProjectProperties(PropertyGroup):
) )
links: CollectionProperty(name="Links", type=Link) links: CollectionProperty(name="Links", type=Link)
active_link_index: IntProperty(name="Active Link Index") active_link_index: IntProperty(name="Active Link Index")
export_schema: EnumProperty(items=get_export_schema, name="IFC Schema") export_schema: EnumProperty(items=get_export_schema, name="IFC Schema", update=update_export_schema)
template_file: EnumProperty(items=get_template_file, name="Template File") template_file: EnumProperty(items=get_template_file, name="Template File")
library_file: EnumProperty(items=get_library_file, name="Library File", update=update_library_file) library_file: EnumProperty(items=get_library_file, name="Library File", update=update_library_file)
use_relative_project_path: BoolProperty(name="Use Relative Project Path", default=False) use_relative_project_path: BoolProperty(name="Use Relative Project Path", default=False)