mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-08-10 17:58:20 +00:00
typing
This commit is contained in:
@@ -17,20 +17,34 @@
|
||||
# along with IfcOpenShell. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import numpy as np
|
||||
import numpy.typing as npt
|
||||
import ifcopenshell.api
|
||||
import ifcopenshell.util.unit
|
||||
import ifcopenshell.util.element
|
||||
import ifcopenshell.util.placement
|
||||
from typing import Optional, Union
|
||||
|
||||
NPArrayOfFloats = npt.NDArray[np.float64]
|
||||
|
||||
|
||||
class Usecase:
|
||||
def __init__(self, file, **settings):
|
||||
def __init__(
|
||||
self,
|
||||
file: ifcopenshell.file,
|
||||
product: ifcopenshell.entity_instance,
|
||||
matrix: Optional[NPArrayOfFloats] = None,
|
||||
is_si=True,
|
||||
should_transform_children=False,
|
||||
):
|
||||
self.file = file
|
||||
self.settings = {"product": None, "matrix": np.eye(4), "is_si": True, "should_transform_children": False}
|
||||
for key, value in settings.items():
|
||||
self.settings[key] = value
|
||||
self.settings = {
|
||||
"product": product,
|
||||
"matrix": matrix if matrix is not None else np.eye(4),
|
||||
"is_si": is_si,
|
||||
"should_transform_children": should_transform_children,
|
||||
}
|
||||
|
||||
def execute(self):
|
||||
def execute(self) -> ifcopenshell.entity_instance:
|
||||
if not hasattr(self.settings["product"], "ObjectPlacement"):
|
||||
return
|
||||
self.unit_scale = ifcopenshell.util.unit.calculate_unit_scale(self.file)
|
||||
@@ -69,12 +83,12 @@ class Usecase:
|
||||
|
||||
return new_placement
|
||||
|
||||
def convert_matrix_to_si(self, matrix):
|
||||
def convert_matrix_to_si(self, matrix: NPArrayOfFloats):
|
||||
matrix[0][3] *= self.unit_scale
|
||||
matrix[1][3] *= self.unit_scale
|
||||
matrix[2][3] *= self.unit_scale
|
||||
|
||||
def get_placement_rel_to(self):
|
||||
def get_placement_rel_to(self) -> Union[ifcopenshell.entity_instance, None]:
|
||||
if getattr(self.settings["product"], "Decomposes", None):
|
||||
relating_object = self.settings["product"].Decomposes[0].RelatingObject
|
||||
return relating_object.ObjectPlacement if hasattr(relating_object, "ObjectPlacement") else None
|
||||
@@ -96,7 +110,7 @@ class Usecase:
|
||||
elif getattr(self.settings["product"], "ContainedInStructure", None):
|
||||
return self.settings["product"].ContainedInStructure[0].RelatingStructure.ObjectPlacement
|
||||
|
||||
def get_children_settings(self, placement):
|
||||
def get_children_settings(self, placement: Union[ifcopenshell.entity_instance, None]) -> list[dict]:
|
||||
if not placement:
|
||||
return []
|
||||
results = []
|
||||
@@ -116,7 +130,9 @@ class Usecase:
|
||||
results.append({"product": obj, "matrix": matrix, "is_si": False, "should_transform_children": True})
|
||||
return results
|
||||
|
||||
def get_relative_placement(self, placement_rel_to):
|
||||
def get_relative_placement(
|
||||
self, placement_rel_to: Union[ifcopenshell.entity_instance, None]
|
||||
) -> ifcopenshell.entity_instance:
|
||||
if placement_rel_to:
|
||||
relating_object_matrix = ifcopenshell.util.placement.get_local_placement(placement_rel_to)
|
||||
relating_object_matrix[0][3] = self.convert_unit_to_si(relating_object_matrix[0][3])
|
||||
@@ -136,19 +152,21 @@ class Usecase:
|
||||
relative_placement_matrix[:, 0][0:3],
|
||||
)
|
||||
|
||||
def create_ifc_axis_2_placement_3d(self, point, up, forward):
|
||||
def create_ifc_axis_2_placement_3d(
|
||||
self, point: NPArrayOfFloats, up: NPArrayOfFloats, forward: NPArrayOfFloats
|
||||
) -> ifcopenshell.entity_instance:
|
||||
return self.file.createIfcAxis2Placement3D(
|
||||
self.create_cartesian_point(point),
|
||||
self.file.createIfcDirection(up.tolist()),
|
||||
self.file.createIfcDirection(forward.tolist()),
|
||||
)
|
||||
|
||||
def create_cartesian_point(self, co):
|
||||
def create_cartesian_point(self, co: NPArrayOfFloats) -> ifcopenshell.entity_instance:
|
||||
co = self.convert_si_to_unit(co)
|
||||
return self.file.createIfcCartesianPoint(co.tolist())
|
||||
|
||||
def convert_si_to_unit(self, co):
|
||||
def convert_si_to_unit(self, co: NPArrayOfFloats) -> NPArrayOfFloats:
|
||||
return co / self.unit_scale
|
||||
|
||||
def convert_unit_to_si(self, co):
|
||||
def convert_unit_to_si(self, co: NPArrayOfFloats) -> NPArrayOfFloats:
|
||||
return co * self.unit_scale
|
||||
|
||||
Reference in New Issue
Block a user