Adds IfcPolyline representation for alignment. Also some general cleanup

This commit is contained in:
Richard Brice
2025-07-13 16:34:52 -07:00
parent e93a66be49
commit 73b76ba666
11 changed files with 230 additions and 45 deletions
@@ -31,7 +31,6 @@ import numpy as np
def add_stationing_referent(
file: ifcopenshell.file,
element: entity_instance,
basis_curve: entity_instance,
distance_along: float,
station: float,
name: str,
@@ -41,8 +40,7 @@ def add_stationing_referent(
If element is an IfcAlignment, IfcReferent.PredefinedType is set to "STATION", otherwise "POSITION"
:param element: the element to receive the referent, expected to be an IfcAlignment or IfcAlignmentSegment
:param basis_curve: the basis curve for positining
:param distance_along: distance along the basis curve
:param distance_along: distance along the alignment curve
:param station: station value
:param name: name to assign to IfcReferent.Name, typically a stringized version of the station value
:return: referent
@@ -52,32 +50,19 @@ def add_stationing_referent(
.. code:: python
alignment = model.by_type("IfcAlignment")[0]
basis_curve = ifcopenshell.api.alignment.get_basis_curve(alignment)
ifcopenshell.api.alignment.add_stationing_referent(model,entity=alignment,basis_curve=basis_curve,distance_along=0.0,station=100.0)
ifcopenshell.api.alignment.add_stationing_referent(model,entity=alignment,distance_along=0.0,station=100.0)
"""
alignment = element
if element.is_a("IfcAlignmentSegment"):
layout = element.Nests[0].RelatingObject
alignment = ifcopenshell.api.alignment.get_alignment(layout)
basis_curve = ifcopenshell.api.alignment.get_basis_curve(alignment)
object_placement = None
representation = None
if basis_curve:
unit_scale = ifcopenshell.util.unit.calculate_unit_scale(file)
settings = ifcopenshell.geom.settings()
fn = ifcopenshell_wrapper.map_shape(settings, basis_curve.wrapped_data)
evaluator = ifcopenshell_wrapper.function_item_evaluator(settings, fn)
p = evaluator.evaluate(distance_along * unit_scale)
p = np.array(p)
x = float(p[0, 3]) / unit_scale
y = float(p[1, 3]) / unit_scale
z = float(p[2, 3]) / unit_scale
rx = float(p[0, 0])
ry = float(p[1, 0])
rz = float(p[2, 0])
ax = float(p[0, 2])
ay = float(p[1, 2])
az = float(p[2, 2])
object_placement = file.createIfcLinearPlacement(
RelativePlacement=file.createIfcAxis2PlacementLinear(
Location=file.createIfcPointByDistanceExpression(
@@ -88,12 +73,40 @@ def add_stationing_referent(
BasisCurve=basis_curve,
)
),
CartesianPosition=file.createIfcAxis2Placement3D(
Location=file.createIfcCartesianPoint((x, y, z)),
Axis=file.createIfcDirection((ax, ay, az)),
RefDirection=file.createIfcDirection((rx, ry, rz)),
),
)
is_valid_curve = True
if basis_curve.is_a("IfcPolyline") and len(basis_curve.Points) < 2:
is_valid_curve = False
elif basis_curve.is_a("IfcIndexedPolyCurve") and len(basis_curve.Points.CoordList) < 2:
is_valid_curve = False
if is_valid_curve:
unit_scale = ifcopenshell.util.unit.calculate_unit_scale(file)
settings = ifcopenshell.geom.settings()
fn = ifcopenshell_wrapper.map_shape(settings, basis_curve.wrapped_data)
evaluator = ifcopenshell_wrapper.function_item_evaluator(settings, fn)
p = evaluator.evaluate(distance_along * unit_scale)
p = np.array(p)
x = float(p[0, 3]) / unit_scale
y = float(p[1, 3]) / unit_scale
z = float(p[2, 3]) / unit_scale
rx = float(p[0, 0])
ry = float(p[1, 0])
rz = float(p[2, 0])
ax = float(p[0, 2])
ay = float(p[1, 2])
az = float(p[2, 2])
object_placement.CartesianPosition=file.createIfcAxis2Placement3D(
Location=file.createIfcCartesianPoint((x, y, z)),
Axis=file.createIfcDirection((ax, ay, az)),
RefDirection=file.createIfcDirection((rx, ry, rz)),
)
# this commented out code is what you would do to add a geometric representation of the referent
# the example is a circle. a better way would be to pass a representation into the function
# representation = file.create_entity(
@@ -118,12 +131,6 @@ def add_stationing_referent(
ifcopenshell.api.pset.edit_pset(file, pset=pset_stationing, properties={"Station": station})
ifcopenshell.api.nest.assign_object(file, related_objects=[referent], relating_object=element)
alignment = element
if element.is_a("IfcAlignmentSegment"):
layout = element.Nests[0].RelatingObject
alignment = ifcopenshell.api.alignment.get_alignment(layout)
if len(alignment.Positions) == 0:
rel_positions = file.createIfcRelPositions(
GlobalId=ifcopenshell.guid.new(),