add_door_representation - remove mathutils dependency #5192

This commit is contained in:
Andrej730
2024-12-18 17:08:16 +05:00
parent d6e6a6d559
commit 284ec46b81
@@ -17,18 +17,17 @@
# along with IfcOpenShell. If not, see <http://www.gnu.org/licenses/>.
from __future__ import annotations
import collections.abc
import ifcopenshell.util.unit
from ifcopenshell.util.shape_builder import ShapeBuilder, V
import ifcopenshell.api.geometry
from ifcopenshell.api.geometry.add_window_representation import create_ifc_window
from mathutils import Vector
from math import cos, radians
from typing import Any, Optional, Literal, Union
import dataclasses
import numpy as np
from ifcopenshell.util.shape_builder import ShapeBuilder, V
from ifcopenshell.api.geometry.add_window_representation import create_ifc_window
from math import cos, radians
from typing import Any, Optional, Literal, Union, get_args, overload
SUPPORTED_DOOR_TYPES = (
DOOR_TYPE = Literal[
"SINGLE_SWING_LEFT",
"SINGLE_SWING_RIGHT",
"DOUBLE_SWING_RIGHT",
@@ -38,7 +37,8 @@ SUPPORTED_DOOR_TYPES = (
"SLIDING_TO_LEFT",
"SLIDING_TO_RIGHT",
"DOUBLE_DOOR_SLIDING",
)
]
SUPPORTED_DOOR_TYPES = get_args(DOOR_TYPE)
def mm(x: float) -> float:
@@ -47,41 +47,50 @@ def mm(x: float) -> float:
def create_ifc_door_lining(
builder: ShapeBuilder, size: Vector, thickness: list, position: Vector = V(0, 0, 0).freeze()
builder: ShapeBuilder, size: np.ndarray, thickness: Union[list[float], float], position: Optional[np.ndarray] = None
) -> ifcopenshell.entity_instance:
"""`thickness` of the profile is defined as list in the following order: `(SIDE, TOP)`
`thickness` can be also defined just as 1 float value.
"""
if not isinstance(thickness, collections.abc.Iterable):
thickness = [thickness] * 2
np_X, np_Y, np_Z = 0, 1, 2
np_XZ = [0, 2]
if not isinstance(thickness, list):
thickness = [thickness, thickness]
th_side, th_up = thickness
points = [
V(0.0, 0.0, 0.0),
V(0.0, 0.0, size.z),
V(size.x, 0.0, size.z),
V(size.x, 0.0, 0.0),
V(size.x - th_side, 0.0, 0.0),
V(size.x - th_side, 0.0, size.z - th_up),
V(th_side, 0.0, size.z - th_up),
V(th_side, 0.0, 0.0),
]
points = V(
[
(0.0, 0.0, 0.0),
(0.0, 0.0, size[np_Z]),
(size[np_X], 0.0, size[np_Z]),
(size[np_X], 0.0, 0.0),
(size[np_X] - th_side, 0.0, 0.0),
(size[np_X] - th_side, 0.0, size[np_Z] - th_up),
(th_side, 0.0, size[np_Z] - th_up),
(th_side, 0.0, 0.0),
]
)
points = [p.xz for p in points]
points = points[:, np_XZ]
door_lining = builder.polyline(points, closed=True)
door_lining = builder.extrude(door_lining, size.y, **builder.extrude_kwargs("Y"))
door_lining = builder.extrude(door_lining, size[np_Y], **builder.extrude_kwargs("Y"))
if position is None:
position = np.zeros(3)
builder.translate(door_lining, position)
return door_lining
def create_ifc_box(
builder: ShapeBuilder, size: Vector, position: Vector = V(0, 0, 0).freeze()
builder: ShapeBuilder, size: np.ndarray, position: Optional[np.ndarray] = None
) -> ifcopenshell.entity_instance:
rect = builder.rectangle(size.xy)
box = builder.extrude(rect, size.z, position=position, extrusion_vector=V(0, 0, 1))
np_Z, np_XY = 2, slice(2)
rect = builder.rectangle(size[np_XY])
if position is None:
position = np.zeros(3)
box = builder.extrude(rect, size[np_Z], position=position, extrusion_vector=(0, 0, 1))
return box
@@ -210,44 +219,27 @@ def add_door_representation(
overall_width: Optional[float] = None,
# door type
# http://ifc43-docs.standards.buildingsmart.org/IFC/RELEASE/IFC4x3/HTML/lexical/IfcDoorTypeOperationEnum.htm
operation_type: Literal[
"SINGLE_SWING_LEFT",
"SINGLE_SWING_RIGHT",
"DOUBLE_SWING_RIGHT",
"DOUBLE_SWING_LEFT",
"DOUBLE_DOOR_SINGLE_SWING",
"DOUBLE_DOOR_DOUBLE_SWING",
"SLIDING_TO_LEFT",
"SLIDING_TO_RIGHT",
"DOUBLE_DOOR_SLIDING",
] = "SINGLE_SWING_LEFT",
operation_type: DOOR_TYPE = "SINGLE_SWING_LEFT",
lining_properties: Optional[Union[DoorLiningProperties, dict[str, Any]]] = None,
panel_properties: Optional[Union[DoorPanelProperties, dict[str, Any]]] = None,
unit_scale: Optional[float] = None,
) -> ifcopenshell.entity_instance:
"""units in usecase_settings expected to be in ifc project units
) -> Union[ifcopenshell.entity_instance, None]:
"""Add a geometric representation for a door.
units in usecase_settings expected to be in ifc project units
:param context: IfcGeometricRepresentationContext for the representation.
:type context: ifcopenshell.entity_instance
:param overall_height: Overall door height. Defaults to 2m.
:type overall_height: float, optional
:param overall_width: Overall door width. Defaults to 0.9m.
:type overall_width: float, optional
:param operation_type: Type of the door. Defaults to SINGLE_SWING_LEFT.
:type operation_type: str, optional
:param lining_properties: DoorLiningProperties or a dictionary to create one.
See DoorLiningProperties description for details.
:type lining_properties: Union[DoorLiningProperties, dict[str, Any]]]
:param panel_properties: DoorPanelProperties or a dictionary to create one.
See DoorPanelProperties description for details.
:type panel_properties: Union[DoorPanelProperties, dict[str, Any]]]
:param unit_scale: The unit scale as calculated by
ifcopenshell.util.unit.calculate_unit_scale. If not provided, it
will be automatically calculated for you.
:type unit_scale: float, optional
:return: IfcShapeRepresentation for a door.
:rtype: ifcopenshell.entity_instance
"""
usecase = Usecase()
usecase.file = file
@@ -287,20 +279,25 @@ def add_door_representation(
class Usecase:
def execute(self):
file: ifcopenshell.file
settings: dict[str, Any]
def execute(self) -> Union[ifcopenshell.entity_instance, None]:
builder = ShapeBuilder(self.file)
overall_height = self.settings["overall_height"]
overall_width = self.settings["overall_width"]
door_type = self.settings["operation_type"]
np_X, np_Y, np_Z = 0, 1, 2
np_XY = slice(2)
np_YX = [1, 0]
overall_height: float = self.settings["overall_height"]
overall_width: float = self.settings["overall_width"]
door_type: DOOR_TYPE = self.settings["operation_type"]
double_swing_door = "DOUBLE_SWING" in door_type
double_door = "DOUBLE_DOOR" in door_type
sliding_door = "SLIDING" in door_type
if door_type not in SUPPORTED_DOOR_TYPES:
raise NotImplementedError(f'Door type "{door_type}" is not currently supported.')
if self.settings["context"].TargetView == "ELEVATION_VIEW":
rect = builder.rectangle(V(overall_width, 0, overall_height))
rect = builder.rectangle((overall_width, 0, overall_height))
representation_evelevation = builder.get_representation(self.settings["context"], rect)
return representation_evelevation
@@ -308,17 +305,19 @@ class Usecase:
lining_props = self.settings["lining_properties"]
# lining params
lining_depth = lining_props["LiningDepth"]
lining_thickness_default = lining_props["LiningThickness"]
lining_offset = lining_props["LiningOffset"]
lining_to_panel_offset_x = (
lining_depth: float = lining_props["LiningDepth"]
lining_thickness_default: float = lining_props["LiningThickness"]
lining_offset: float = lining_props["LiningOffset"]
lining_to_panel_offset_x: float = (
lining_props["LiningToPanelOffsetX"] if not sliding_door else lining_thickness_default
)
panel_depth = panel_props["PanelDepth"]
lining_to_panel_offset_y_full = lining_props["LiningToPanelOffsetY"] if not sliding_door else -panel_depth
panel_depth: float = panel_props["PanelDepth"]
lining_to_panel_offset_y_full: float = (
lining_props["LiningToPanelOffsetY"] if not sliding_door else -panel_depth
)
transom_thickness = lining_props["TransomThickness"] / 2
transfom_offset = lining_props["TransomOffset"]
transom_thickness: float = lining_props["TransomThickness"] / 2
transfom_offset: float = lining_props["TransomOffset"]
if transom_thickness == 0:
transfom_offset = 0
window_lining_height = overall_height - transfom_offset - transom_thickness
@@ -333,25 +332,25 @@ class Usecase:
side_lining_thickness = side_lining_thickness - panel_lining_overlap_x
top_lining_thickness = top_lining_thickness - panel_top_lining_overlap_x
threshold_thickness = lining_props["ThresholdThickness"]
threshold_depth = lining_props["ThresholdDepth"]
threshold_offset = lining_props["ThresholdOffset"]
threshold_thickness: float = lining_props["ThresholdThickness"]
threshold_depth: float = lining_props["ThresholdDepth"]
threshold_offset: float = lining_props["ThresholdOffset"]
threshold_width = overall_width - side_lining_thickness * 2
casing_thickness = lining_props["CasingThickness"]
casing_depth = lining_props["CasingDepth"]
casing_thickness: float = lining_props["CasingThickness"]
casing_depth: float = lining_props["CasingDepth"]
# panel params
panel_width = door_opening_width * panel_props["PanelWidth"]
frame_depth = panel_props["FrameDepth"]
frame_thickness = panel_props["FrameThickness"]
panel_width: float = door_opening_width * panel_props["PanelWidth"]
frame_depth: float = panel_props["FrameDepth"]
frame_thickness: float = panel_props["FrameThickness"]
frame_height = window_lining_height - lining_to_panel_offset_x * 2
glass_thickness = self.convert_si_to_unit(0.01)
# handle dimensions (hardcoded)
handle_size = self.convert_si_to_unit(V(120, 40, 20) * 0.001)
handle_offset = self.convert_si_to_unit(V(60, 0, 1000) * 0.001) # to the handle center
handle_center_offset = V(handle_size.y / 2, 0, handle_size.z) / 2
handle_center_offset = V(handle_size[np_Y] / 2, 0, handle_size[np_Z]) / 2
slider_arrow_symbol_size = self.convert_si_to_unit(30 * 0.001)
if transfom_offset:
@@ -365,19 +364,19 @@ class Usecase:
lining_size = V(overall_width, lining_depth, lining_height)
lining_thickness = [side_lining_thickness, top_lining_thickness]
def l_shape_check(lining_thickness):
def l_shape_check(lining_thickness: list[float]) -> bool:
return lining_to_panel_offset_y_full < lining_depth and any(
lining_to_panel_offset_x < th for th in lining_thickness
)
# create 2d representation
if self.settings["context"].TargetView == "PLAN_VIEW":
items_2d = []
items_2d: list[ifcopenshell.entity_instance] = []
panel_size = V(panel_width, panel_depth)
if not sliding_door:
panel_position = V(lining_to_panel_offset_x, lining_depth)
else:
panel_position = V(lining_to_panel_offset_x, -panel_size.y)
panel_position = V(lining_to_panel_offset_x, -panel_size[np_Y])
if self.settings["context"].ContextIdentifier == "Annotation":
# only sliding door has annotation representation
@@ -385,45 +384,45 @@ class Usecase:
return None
# arrow symbol
arrow_symbol = []
arrow_symbol: list[ifcopenshell.entity_instance] = []
arrow_offset = slider_arrow_symbol_size / cos(radians(15))
arrow_symbol.append(
builder.polyline(
points=(V(0.35 * panel_size.x, 0), V(0.65 * panel_size.x, 0)),
points=((0.35 * panel_size[np_X], 0), (0.65 * panel_size[np_X], 0)),
)
)
arrow_symbol.append(
builder.polyline(
points=(
V(slider_arrow_symbol_size, arrow_offset),
V(0, 0),
V(slider_arrow_symbol_size, -arrow_offset),
(slider_arrow_symbol_size, arrow_offset),
(0, 0),
(slider_arrow_symbol_size, -arrow_offset),
),
position_offset=V(0.35 * panel_size.x, 0),
position_offset=(0.35 * panel_size[np_X], 0),
)
)
builder.translate(arrow_symbol, panel_position + V(0, -arrow_offset * 1.5))
builder.translate(arrow_symbol, panel_position + (0, -arrow_offset * 1.5))
items_2d.extend(arrow_symbol)
representation_2d = builder.get_representation(self.settings["context"], items_2d, "Curve2D")
return representation_2d
door_items = []
door_items: list[ifcopenshell.entity_instance] = []
# create lining
if l_shape_check([side_lining_thickness]):
lining_points = [
V(0, 0),
V(0, lining_depth),
V(lining_to_panel_offset_x, lining_depth),
V(lining_to_panel_offset_x, lining_to_panel_offset_y_full),
V(lining_thickness_default, lining_to_panel_offset_y_full),
V(lining_thickness_default, 0),
(0, 0),
(0, lining_depth),
(lining_to_panel_offset_x, lining_depth),
(lining_to_panel_offset_x, lining_to_panel_offset_y_full),
(lining_thickness_default, lining_to_panel_offset_y_full),
(lining_thickness_default, 0),
]
lining = builder.polyline(lining_points, closed=True)
else:
lining = builder.rectangle(V(side_lining_thickness, lining_depth))
lining = builder.rectangle((side_lining_thickness, lining_depth))
items_2d.append(lining)
items_2d.append(
@@ -431,26 +430,35 @@ class Usecase:
)
# TODO: make second swing lines dashed
def create_ifc_door_panel_2d(panel_size, panel_position, door_swing_type, sliding=False):
def create_ifc_door_panel_2d(
panel_size: np.ndarray,
panel_position: np.ndarray,
door_swing_type: Literal["LEFT", "RIGHT"],
sliding: bool = False,
) -> list[ifcopenshell.entity_instance]:
if sliding:
return create_ifc_door_sliding_panel_2d(panel_size, panel_position, door_swing_type)
door_items = []
panel_size = panel_size.yx
door_items: list[ifcopenshell.entity_instance] = []
panel_size = panel_size[np_YX]
# create semi-semi-circle
if double_swing_door:
trim_points_mask = (3, 1)
second_swing_line = builder.polyline(
points=(V(0, 0), V(0, -panel_size.y), V(panel_size.x, -panel_size.y))
points=(
(0, 0),
(0, -panel_size[np_Y]),
(panel_size[np_X], -panel_size[np_Y]),
)
)
door_items.append(second_swing_line)
else:
trim_points_mask = (0, 1)
semicircle = builder.create_ellipse_curve(
panel_size.y - panel_size.x,
panel_size.y,
panel_size[np_Y] - panel_size[np_X],
panel_size[np_Y],
trim_points_mask=trim_points_mask,
position=V(panel_size.x, 0),
position=(panel_size[np_X], 0),
)
door_items.append(semicircle)
@@ -461,47 +469,49 @@ class Usecase:
builder.translate(door_items, panel_position)
if door_swing_type == "RIGHT":
mirror_point = panel_position + V(panel_size.y / 2, 0)
builder.mirror(door_items, mirror_axes=V(1, 0), mirror_point=mirror_point)
mirror_point = panel_position + (panel_size[np_Y] / 2, 0)
builder.mirror(door_items, mirror_axes=(1, 0), mirror_point=mirror_point)
return door_items
def create_ifc_door_sliding_panel_2d(panel_size, panel_position, door_swing_type):
door = builder.rectangle(panel_size, position=panel_position - V(panel_size.x * 0.5, 0))
def create_ifc_door_sliding_panel_2d(
panel_size: np.ndarray, panel_position: np.ndarray, door_swing_type: Literal["LEFT", "RIGHT"]
) -> list[ifcopenshell.entity_instance]:
door = builder.rectangle(panel_size, position=panel_position - (panel_size[np_X] * 0.5, 0))
if door_swing_type == "RIGHT":
mirror_point = panel_position + V(panel_size.x / 2, 0)
builder.mirror(door, mirror_axes=V(1, 0), mirror_point=mirror_point)
mirror_point = panel_position + (panel_size[np_X] / 2, 0)
builder.mirror(door, mirror_axes=(1, 0), mirror_point=mirror_point)
return [door]
door_items = []
door_items: list[ifcopenshell.entity_instance] = []
if double_door:
panel_size.x = panel_size.x / 2
panel_size[np_X] = panel_size[np_X] / 2
door_items.extend(create_ifc_door_panel_2d(panel_size, panel_position, "LEFT", sliding_door))
mirror_point = panel_position + V(door_opening_width / 2, 0)
door_items.extend(
builder.mirror(door_items, mirror_axes=V(1, 0), mirror_point=mirror_point, create_copy=True)
builder.mirror(door_items, mirror_axes=(1, 0), mirror_point=mirror_point, create_copy=True)
)
else:
door_swing_type = "LEFT" if door_type.endswith("LEFT") else "RIGHT"
door_items.extend(create_ifc_door_panel_2d(panel_size, panel_position, door_swing_type, sliding_door))
items_2d.extend(door_items)
builder.translate(items_2d, V(0, lining_offset))
builder.translate(items_2d, (0, lining_offset))
representation_2d = builder.get_representation(self.settings["context"], items_2d)
return representation_2d
lining_items = []
lining_items: list[ifcopenshell.entity_instance] = []
main_lining_size = lining_size
# need to check offsets to decide whether lining should be rectangle
# or L shaped
if l_shape_check(lining_thickness):
main_lining_size = lining_size.copy()
main_lining_size.y = lining_to_panel_offset_y_full
main_lining_size[np_Y] = lining_to_panel_offset_y_full
second_lining_size = lining_size.copy()
second_lining_size.y = lining_size.y - lining_to_panel_offset_y_full
second_lining_size[np_Y] = lining_size[np_Y] - lining_to_panel_offset_y_full
second_lining_position = V(0, lining_to_panel_offset_y_full, 0)
second_lining_thickness = [min(th, lining_to_panel_offset_x) for th in lining_thickness]
@@ -514,6 +524,7 @@ class Usecase:
lining_items.append(main_lining)
# add threshold
threshold_items: list[ifcopenshell.entity_instance]
if not threshold_thickness:
threshold_items = []
else:
@@ -522,7 +533,7 @@ class Usecase:
threshold_items = [create_ifc_box(builder, threshold_size, threshold_position)]
# add casings
casing_items = []
casing_items: list[ifcopenshell.entity_instance] = []
if not lining_offset and casing_thickness:
casing_wall_overlap = max(casing_thickness - lining_thickness_default, 0)
inner_casing_thickness = [
@@ -540,53 +551,55 @@ class Usecase:
inner_casing = create_ifc_door_lining(builder, casing_size, inner_casing_thickness, inner_casing_position)
casing_items.append(inner_casing)
def create_ifc_door_panel(panel_size, panel_position, door_swing_type):
door_items = []
def create_ifc_door_panel(
panel_size: np.ndarray, panel_position: np.ndarray, door_swing_type: Literal["LEFT", "RIGHT"]
) -> list[ifcopenshell.entity_instance]:
door_items: list[ifcopenshell.entity_instance] = []
# add door panel
door_items.append(create_ifc_box(builder, panel_size, panel_position))
# add door handle
handle_points = [
V(0, 0),
V(0, -handle_size.y),
V(handle_size.x, -handle_size.y),
V(handle_size.x, -handle_size.y / 2),
V(handle_size.y / 2, -handle_size.y / 2),
V(handle_size.y / 2, 0),
(0, 0),
(0, -handle_size[np_Y]),
(handle_size[np_X], -handle_size[np_Y]),
(handle_size[np_X], -handle_size[np_Y] / 2),
(handle_size[np_Y] / 2, -handle_size[np_Y] / 2),
(handle_size[np_Y] / 2, 0),
]
handle_polyline = builder.polyline(handle_points, closed=True)
handle_position = panel_position + handle_offset - handle_center_offset
door_handle = builder.extrude(handle_polyline, handle_size.z, position=handle_position)
door_handle = builder.extrude(handle_polyline, handle_size[np_Z], position=handle_position)
door_items.append(door_handle)
if door_swing_type == "LEFT":
builder.mirror(
door_handle, mirror_axes=V(1, 0), mirror_point=panel_position.xy + V(panel_size.x / 2, 0)
door_handle, mirror_axes=(1, 0), mirror_point=panel_position[np_XY] + (panel_size[np_X] / 2, 0)
)
door_handle_mirrored = builder.mirror(
door_handle,
mirror_axes=V(0, 1),
mirror_point=handle_position.xy + V(0, panel_size.y / 2),
mirror_axes=(0, 1),
mirror_point=handle_position[np_XY] + (0, panel_size[np_Y] / 2),
create_copy=True,
)
door_items.append(door_handle_mirrored)
return door_items
door_items = []
door_items: list[ifcopenshell.entity_instance] = []
panel_size = V(panel_width, panel_depth, panel_height)
panel_position = V(lining_to_panel_offset_x, lining_to_panel_offset_y_full, threshold_thickness)
if double_door:
# keeping a little space between doors for readibility
double_door_offset = self.convert_si_to_unit(0.001)
panel_size.x = panel_size.x / 2 - double_door_offset
panel_size[np_X] = panel_size[np_X] / 2 - double_door_offset
door_items.extend(create_ifc_door_panel(panel_size, panel_position, "LEFT"))
mirror_point = panel_position + V(door_opening_width / 2, 0, 0)
door_items.extend(
builder.mirror(door_items, mirror_axes=V(1, 0), mirror_point=mirror_point.xy, create_copy=True)
builder.mirror(door_items, mirror_axes=(1, 0), mirror_point=mirror_point[np_XY], create_copy=True)
)
else:
door_swing_type = "LEFT" if door_type.endswith("LEFT") else "RIGHT"
@@ -622,15 +635,17 @@ class Usecase:
)
lining_offset_items = lining_items + door_items + window_lining_items + frame_items + glass_items
builder.translate(lining_offset_items, V(0, lining_offset, 0))
builder.translate(lining_offset_items, (0, lining_offset, 0))
output_items = lining_offset_items + threshold_items + casing_items
representation = builder.get_representation(self.settings["context"], output_items)
return representation
def convert_si_to_unit(self, value):
@overload
def convert_si_to_unit(self, value: float) -> float: ...
@overload
def convert_si_to_unit(self, value: np.ndarray) -> np.ndarray: ...
def convert_si_to_unit(self, value: Union[float, np.ndarray]) -> Union[float, np.ndarray]:
si_conversion = 1 / self.settings["unit_scale"]
if isinstance(value, Vector):
return V(*[i * si_conversion for i in value])
return value * si_conversion