Debian Codegraph @

src/bonsai/bonsai/tool/library.py

raw
1 # Bonsai - OpenBIM Blender Add-on
2 # Copyright (C) 2021 Dion Moult <[email protected]>
3 #
4 # This file is part of Bonsai.
5 #
6 # Bonsai is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # Bonsai is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with Bonsai. If not, see <http://www.gnu.org/licenses/>.
18
19 from __future__ import annotations
20 import ifcopenshell
21 import bpy
22 import bonsai.bim.helper
23 import bonsai.core.tool
24 import bonsai.tool as tool
25 from typing import Literal, Any, Union, TYPE_CHECKING
26
27 if TYPE_CHECKING:
28 from bonsai.bim.module.library.prop import BIMLibraryProperties
29
30
31 class Library(bonsai.core.tool.Library):
32 @classmethod
33 def get_library_props(cls) -> BIMLibraryProperties:
34 return bpy.context.scene.BIMLibraryProperties
35
36 @classmethod
37 def clear_editing_mode(cls) -> None:
38 cls.get_library_props().editing_mode = "NONE"
39
40 @classmethod
41 def export_library_attributes(cls) -> dict[str, Any]:
42 props = cls.get_library_props()
43 return bonsai.bim.helper.export_attributes(props.library_attributes)
44
45 @classmethod
46 def export_reference_attributes(cls) -> dict[str, Any]:
47 props = cls.get_library_props()
48 return bonsai.bim.helper.export_attributes(props.reference_attributes)
49
50 @classmethod
51 def get_active_library(cls) -> ifcopenshell.entity_instance:
52 props = cls.get_library_props()
53 return tool.Ifc.get().by_id(props.active_library_id)
54
55 @classmethod
56 def get_active_reference(cls) -> ifcopenshell.entity_instance:
57 props = cls.get_library_props()
58 return tool.Ifc.get().by_id(props.active_reference_id)
59
60 @classmethod
61 def import_library_attributes(cls, library: ifcopenshell.entity_instance) -> None:
62 props = cls.get_library_props()
63 props.library_attributes.clear()
64 bonsai.bim.helper.import_attributes2(library, props.library_attributes)
65
66 @classmethod
67 def import_reference_attributes(cls, reference: ifcopenshell.entity_instance) -> None:
68 props = cls.get_library_props()
69 props.reference_attributes.clear()
70 bonsai.bim.helper.import_attributes2(reference, props.reference_attributes)
71
72 @classmethod
73 def import_references(cls, library: ifcopenshell.entity_instance) -> None:
74 props = cls.get_library_props()
75 props.references.clear()
76 if tool.Ifc.get_schema() == "IFC2X3":
77 references = library.LibraryReference
78 else:
79 references = library.HasLibraryReferences
80 for reference in references:
81 new = props.references.add()
82 new.ifc_definition_id = reference.id()
83 new.name = reference.Name or "Unnamed"
84
85 @classmethod
86 def set_active_library(cls, library: Union[ifcopenshell.entity_instance, None]) -> None:
87 props = cls.get_library_props()
88 if library is None:
89 props.active_library_id = 0
90 else:
91 props.active_library_id = library.id()
92
93 @classmethod
94 def set_active_reference(cls, reference: ifcopenshell.entity_instance) -> None:
95 props = cls.get_library_props()
96 props.active_reference_id = reference.id()
97
98 @classmethod
99 def set_editing_mode(cls, mode: Literal["LIBRARY", "REFERENCES", "REFERENCE"]) -> None:
100 props = cls.get_library_props()
101 props.editing_mode = mode