Debian Codegraph @

src/bonsai/bonsai/tool/qto.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 bpy
21 import bonsai.core.tool
22 import bonsai.bim.schema
23 import bonsai.tool as tool
24 import ifcopenshell
25 import ifcopenshell.util.unit
26 import ifcopenshell.util.element
27 from mathutils import Vector
28 from typing import Optional, Union, Literal, TYPE_CHECKING, Any
29
30 if TYPE_CHECKING:
31 from bonsai.bim.module.qto.prop import BIMQtoProperties
32
33 QuantityTypes = Literal["Q_LENGTH", "Q_AREA", "Q_VOLUME"]
34
35
36 class Qto(bonsai.core.tool.Qto):
37 @classmethod
38 def get_qto_props(cls) -> BIMQtoProperties:
39 return bpy.context.scene.BIMQtoProperties
40
41 @classmethod
42 def get_radius_of_selected_vertices(cls, obj: bpy.types.Object) -> float:
43 selected_verts = [v.co for v in obj.data.vertices if v.select]
44 total = Vector()
45 for v in selected_verts:
46 total += v
47 circle_center = total / len(selected_verts)
48 return max([(v - circle_center).length for v in selected_verts])
49
50 @classmethod
51 def set_qto_result(cls, result: float) -> None:
52 props = cls.get_qto_props()
53 props.qto_result = str(round(result, 3))
54
55 @classmethod
56 def get_rounded_value(cls, new_quantity: float) -> float:
57 return round(new_quantity, 3)
58
59 @classmethod
60 def convert_to_project_units(
61 cls,
62 value: float,
63 qto_name: Optional[str] = None,
64 quantity_name: Optional[str] = None,
65 quantity_type: Optional[QuantityTypes] = None,
66 ) -> Union[float, None]:
67 """You can either specify `quantity_type` or provide `qto_name/quantity_name`
68 to let method figure the `quantity_type` from the templates
69 """
70 ifc_file = tool.Ifc.get()
71 quantity_to_unit_types = {
72 "Q_LENGTH": ("LENGTHUNIT", "METRE"),
73 "Q_AREA": ("AREAUNIT", "SQUARE_METRE"),
74 "Q_VOLUME": ("VOLUMEUNIT", "CUBIC_METRE"),
75 }
76 if not quantity_type:
77 qt = bonsai.bim.schema.ifc.psetqto.get_by_name(qto_name)
78 quantity_type = next(q.TemplateType for q in qt.HasPropertyTemplates if q.Name == quantity_name)
79
80 unit_type = quantity_to_unit_types.get(quantity_type, None)
81 if not unit_type:
82 return
83
84 unit_type, base_unit = unit_type
85 project_unit = ifcopenshell.util.unit.get_project_unit(ifc_file, unit_type)
86 if not project_unit:
87 return
88 value = ifcopenshell.util.unit.convert(
89 value,
90 from_prefix=None,
91 from_unit=base_unit,
92 to_prefix=getattr(project_unit, "Prefix", None),
93 to_unit=project_unit.Name,
94 )
95 return value
96
97 @classmethod
98 def get_base_qto(cls, product: ifcopenshell.entity_instance) -> Union[ifcopenshell.entity_instance, None]:
99 if not hasattr(product, "IsDefinedBy"):
100 return
101 base_qto_definition = None
102 base_qto_definition_name: Union[str, None] = None
103 for rel in product.IsDefinedBy or []:
104 definition = rel.RelatingPropertyDefinition
105 if not rel.is_a("IfcRelDefinesByProperties"):
106 continue
107 definition = rel.RelatingPropertyDefinition
108 definition_name = definition.Name
109 if "Qto_" not in definition_name:
110 continue
111 if "Base" in definition_name:
112 return definition
113 if base_qto_definition and "BodyGeometryValidation" not in base_qto_definition_name:
114 continue
115 base_qto_definition = definition
116 base_qto_definition_name = definition_name
117 return base_qto_definition
118
119 @classmethod
120 def get_related_cost_item_quantities(cls, product: ifcopenshell.entity_instance) -> list[dict]:
121 """_summary_: Returns the related cost item and related quantities of the product
122
123 :param ifc-instance product: ifc instance
124 :type product: ifcopenshell.entity_instance.entity_instance
125
126 :return list of dictionaries in the form [
127 {
128 "cost_item_id" : XX,
129 "cost_item_name" : XX,
130 "quantity_id" : XX,
131 "quantity_name" : XX,
132 "quantity_value" : XX,
133 "quantity_type" : XX
134 }]
135 :rtype: list
136
137 Example:
138
139 .. code::Python
140 import bonsai.tool as tool
141
142 relating_cost_items = tool.Qto.relating_cost_items(my_beautiful_wall)
143 for relating_cost_item in relating_cost_items:
144 print(f"RELATING COST ITEM NAME: {relating_cost_item["cost_item_name"]}")
145 print(f"RELATING COST QUANTITY NAME: {relating_cost_item["quantity_name"]}")
146 ...
147 """
148 model = tool.Ifc.get()
149 cost_items = model.by_type("IfcCostItem")
150 result = []
151 base_qto = cls.get_base_qto(product)
152 quantities = base_qto.Quantities if base_qto else []
153
154 for cost_item in cost_items:
155 cost_item_quantities = cost_item.CostQuantities if cost_item.CostQuantities is not None else []
156 for cost_item_quantity in cost_item_quantities:
157 for quantity in quantities:
158 if quantity == cost_item_quantity:
159 result.append(
160 {
161 "cost_item_id": cost_item.id(),
162 "cost_item_name": cost_item.Name,
163 "quantity_id": quantity.id(),
164 "quantity_name": quantity.Name,
165 "quantity_value": quantity[3],
166 "quantity_type": quantity.is_a(),
167 }
168 )
169 return result
170
171 @classmethod
172 def get_qto_rules(cls) -> dict[str, dict[str, Any]]:
173 import ifc5d.qto
174
175 ifc_file = tool.Ifc.get()
176 is_ifc4x3 = ifc_file.schema == "IFC4X3"
177 return {rule_id: rule for rule_id, rule in ifc5d.qto.rules.items() if rule_id.startswith("IFC4X3") == is_ifc4x3}
178
179 @classmethod
180 def get_not_quantified_elements_message(cls, not_quantified_elements: set[ifcopenshell.entity_instance]) -> str:
181 not_quantified_message = ""
182 if not_quantified_elements:
183 print("Elements that were not quantified:")
184 for element in not_quantified_elements:
185 print(f"- {element}")
186 not_quantified_message = (
187 f" {len(not_quantified_elements)} of them were not quantified, see system console for the details."
188 )
189 return not_quantified_message