Reuse builder.create_axis2_placement_3d

This commit is contained in:
Andrej730
2025-09-04 10:58:04 +05:00
parent 90e6838ddb
commit 8c12bd0322
8 changed files with 35 additions and 77 deletions
@@ -22,6 +22,7 @@ import ifcopenshell.api.owner
import ifcopenshell.util.unit
import ifcopenshell.util.element
import ifcopenshell.util.placement
from ifcopenshell.util.shape_builder import ShapeBuilder
from typing import Optional, Union, Any
NPArrayOfFloats = npt.NDArray[np.float64]
@@ -74,6 +75,7 @@ class Usecase:
if not hasattr(self.settings["product"], "ObjectPlacement"):
return
self.unit_scale = ifcopenshell.util.unit.calculate_unit_scale(self.file)
self.builder = ShapeBuilder(self.file)
if not self.settings["is_si"]:
self.convert_matrix_to_si(self.settings["matrix"])
@@ -183,25 +185,12 @@ class Usecase:
o = np.array((m[0][3], m[1][3], m[2][3]))
object_matrix = ifcopenshell.util.placement.a2p(o, z, x)
relative_placement_matrix = np.linalg.inv(relating_object_matrix) @ object_matrix
return self.create_ifc_axis_2_placement_3d(
relative_placement_matrix[:, 3][0:3],
return self.builder.create_axis2_placement_3d(
self.convert_si_to_unit(relative_placement_matrix[:, 3][0:3]),
relative_placement_matrix[:, 2][0:3],
relative_placement_matrix[:, 0][0:3],
)
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: NPArrayOfFloats) -> ifcopenshell.entity_instance:
co = self.convert_si_to_unit(co)
return self.file.createIfcCartesianPoint(co.tolist())
def convert_si_to_unit(self, co: NPArrayOfFloats) -> NPArrayOfFloats:
return co / self.unit_scale
@@ -18,9 +18,9 @@
import ifcopenshell
import ifcopenshell.util.element
import ifcopenshell.util.geolocation
import ifcopenshell.util.unit
import numpy as np
from ifcopenshell.util.shape_builder import ShapeBuilder
from math import sin, cos, radians
@@ -62,6 +62,7 @@ def edit_wcs(
ifcopenshell.api.georeference.edit_wcs(model)
"""
unit_scale = ifcopenshell.util.unit.calculate_unit_scale(file)
builder = ShapeBuilder(file)
if np.isclose(rotation, 0):
xaxis_x = 1.0
xaxis_y = 0.0
@@ -74,14 +75,10 @@ def edit_wcs(
old_wcs = context.WorldCoordinateSystem
if context.CoordinateSpaceDimension == 3:
if is_si:
point = file.createIfcCartesianPoint((x / unit_scale, y / unit_scale, z / unit_scale))
xyz = (x / unit_scale, y / unit_scale, z / unit_scale)
else:
point = file.createIfcCartesianPoint((x, y, z))
placement = file.createIfcAxis2Placement3D(
point,
file.createIfcDirection((0.0, 0.0, 1.0)),
file.createIfcDirection((xaxis_x, xaxis_y, 0.0)),
)
xyz = (x, y, z)
placement = builder.create_axis2_placement_3d(xyz, (0.0, 0.0, 1.0), (xaxis_x, xaxis_y, 0.0))
elif context.CoordinateSpaceDimension == 2:
if is_si:
point = file.createIfcCartesianPoint((x / unit_scale, y / unit_scale))
@@ -21,6 +21,7 @@ import numpy as np
import ifcopenshell
from typing import Any, Union
from dataclasses import dataclass
from ifcopenshell.util.shape_builder import ShapeBuilder
@dataclass
@@ -78,9 +79,7 @@ class Clipping:
if not ifc_file:
ifc_file = first_operand.file
location = ifc_file.createIfcCartesianPoint([i / unit_scale for i in self.location])
direction = ifc_file.createIfcDirection(self.normal)
builder = ShapeBuilder(ifc_file)
normal = np.array(self.normal)
if np.allclose(normal, np.array([0.0, 0.0, 1.0]), atol=1e-2) or np.allclose(
@@ -92,9 +91,9 @@ class Clipping:
x_axis = np.cross(normal, arbitrary_vector)
x_axis /= np.linalg.norm(x_axis)
x_axis = ifc_file.createIfcDirection(x_axis.tolist())
plane = ifc_file.createIfcPlane(ifc_file.createIfcAxis2Placement3D(location, direction, x_axis))
placement = builder.create_axis2_placement_3d([i / unit_scale for i in self.location], self.normal, x_axis)
plane = ifc_file.create_entity("IfcPlane", placement)
second_operand = ifc_file.createIfcHalfSpaceSolid(plane, False)
return ifc_file.createIfcBooleanClippingResult("DIFFERENCE", first_operand, second_operand)