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
from bonsai.bim.ifc import IfcStore
from pathlib import Path
from collections import defaultdict
from typing import Union
@@ -40,9 +41,10 @@ class ProjectData:
cls.data = {
"export_schema": cls.get_export_schema(),
"library_file": cls.library_file(),
"template_file": cls.template_file(),
"last_saved": cls.last_saved(),
}
# After export_schema.
cls.data["template_file"] = cls.template_file()
cls.is_loaded = True
@classmethod
@@ -77,11 +79,20 @@ class ProjectData:
return results
@classmethod
def template_file(cls):
files = os.listdir(os.path.join(bpy.context.scene.BIMProperties.data_dir, "templates", "projects"))
results = [("0", "Blank Project", "")]
results.extend([(f, os.path.splitext(f)[0], "") for f in files if ".ifc" in f])
return results
def template_file(cls) -> dict[str, list[tuple[str, str, str]]]:
template_files: dict[str, list[tuple[str, str, str]]] = defaultdict(list)
for export_schema in cls.data["export_schema"]:
template_files[export_schema[0]].append(("0", "Blank Project", ""))
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
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"]
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]]:
if not ProjectData.is_loaded:
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]]:
@@ -201,7 +206,7 @@ class BIMProjectProperties(PropertyGroup):
)
links: CollectionProperty(name="Links", type=Link)
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")
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)