Files
IfcOpenShell/src/ifcopenshell-python/test/api/alignment/test_create_layout_segment.py
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

278 lines
9.2 KiB
Python
Raw Normal View History

2025-07-08 10:09:14 -07:00
# IfcOpenShell - IFC toolkit and geometry engine
# Copyright (C) 2025 Thomas Krijnen <thomas@aecgeeks.com>
#
# This file is part of IfcOpenShell.
#
# IfcOpenShell is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# IfcOpenShell is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with IfcOpenShell. If not, see <http://www.gnu.org/licenses/>.
import pytest
import ifcopenshell.api.alignment
import ifcopenshell.api.context
2025-07-10 11:30:09 +05:00
import ifcopenshell.api.unit
2025-07-08 10:09:14 -07:00
import math
2025-07-09 13:47:32 +05:00
2025-07-08 10:09:14 -07:00
def _test_horizontal() -> ifcopenshell.file:
file = ifcopenshell.file(schema="IFC4X3_ADD2")
2025-07-09 13:47:32 +05:00
project = file.createIfcProject(GlobalId=ifcopenshell.guid.new(), Name="Test")
length = ifcopenshell.api.unit.add_si_unit(file, unit_type="LENGTHUNIT")
ifcopenshell.api.unit.assign_unit(file, units=[length])
2025-07-08 10:09:14 -07:00
geometric_representation_context = ifcopenshell.api.context.add_context(file, context_type="Model")
axis_model_representation_subcontext = ifcopenshell.api.context.add_context(
file,
context_type="Model",
context_identifier="Axis",
target_view="MODEL_VIEW",
parent=geometric_representation_context,
)
# creates an IfcAlignment with an IfcAlignmentHorizontal layout containing only the zero length segment
2025-07-09 13:47:32 +05:00
ali = ifcopenshell.api.alignment.create(file, "A1")
2025-07-08 10:09:14 -07:00
# append a segment to the horizontal layout
horizontal_alignment = ifcopenshell.api.alignment.get_horizontal_layout(ali)
curve = ifcopenshell.api.alignment.get_curve(horizontal_alignment)
2025-07-09 13:47:32 +05:00
assert curve == None # for single horizontal, geometric representation is on IfcAlignment
2025-07-08 10:09:14 -07:00
curve = ifcopenshell.api.alignment.get_curve(ali)
assert curve.is_a("IfcCompositeCurve")
assert len(curve.Segments) == 1
design_parameters = file.create_entity(
type="IfcAlignmentHorizontalSegment",
StartTag=None,
EndTag=None,
StartPoint=file.createIfcCartesianPoint(Coordinates=((0.0, 0.0))),
StartDirection=0.0,
StartRadiusOfCurvature=0.0,
EndRadiusOfCurvature=0.0,
SegmentLength=100.0,
GravityCenterLineHeight=None,
PredefinedType="LINE",
)
2025-07-09 13:47:32 +05:00
end = ifcopenshell.api.alignment.create_layout_segment(file, horizontal_alignment, design_parameters)
2025-07-08 10:09:14 -07:00
assert len(horizontal_alignment.IsNestedBy[0].RelatedObjects) == 2
2025-07-09 13:47:32 +05:00
x = end[0, 3]
y = end[1, 3]
z = end[2, 3]
2025-07-08 10:09:14 -07:00
assert x == 100.0
assert y == 0.0
assert z == 0.0
curve = ifcopenshell.api.alignment.get_curve(ali)
assert curve.is_a("IfcCompositeCurve")
assert len(curve.Segments) == 2
design_parameters = file.create_entity(
type="IfcAlignmentHorizontalSegment",
StartTag=None,
EndTag=None,
StartPoint=file.createIfcCartesianPoint(Coordinates=((x.item(), y.item()))),
2025-07-09 13:47:32 +05:00
StartDirection=math.pi / 6,
2025-07-08 10:09:14 -07:00
StartRadiusOfCurvature=0.0,
EndRadiusOfCurvature=0.0,
SegmentLength=50.0,
GravityCenterLineHeight=None,
PredefinedType="LINE",
)
2025-07-09 13:47:32 +05:00
end = ifcopenshell.api.alignment.create_layout_segment(file, horizontal_alignment, design_parameters)
2025-07-08 10:09:14 -07:00
assert len(horizontal_alignment.IsNestedBy[0].RelatedObjects) == 3
2025-07-09 13:47:32 +05:00
x = end[0, 3]
y = end[1, 3]
z = end[2, 3]
assert x == 100.0 + 50.0 * math.cos(math.pi / 6)
assert y == 50.0 * math.sin(math.pi / 6)
2025-07-08 10:09:14 -07:00
assert z == 0.0
curve = ifcopenshell.api.alignment.get_curve(ali)
assert curve.is_a("IfcCompositeCurve")
assert len(curve.Segments) == 3
return file
def _test_horizontal_vertical():
file = ifcopenshell.file(schema="IFC4X3_ADD2")
2025-07-09 13:47:32 +05:00
project = file.createIfcProject(GlobalId=ifcopenshell.guid.new(), Name="Test")
length = ifcopenshell.api.unit.add_si_unit(file, unit_type="LENGTHUNIT")
ifcopenshell.api.unit.assign_unit(file, units=[length])
2025-07-08 10:09:14 -07:00
geometric_representation_context = ifcopenshell.api.context.add_context(file, context_type="Model")
axis_model_representation_subcontext = ifcopenshell.api.context.add_context(
file,
context_type="Model",
context_identifier="Axis",
target_view="MODEL_VIEW",
parent=geometric_representation_context,
)
# creates an IfcAlignment with an IfcAlignmentHorizontal layout containing only the zero length segment
2025-07-09 13:47:32 +05:00
ali = ifcopenshell.api.alignment.create(file, "A1", True)
2025-07-08 10:09:14 -07:00
# append a segment to the horizontal layout
horizontal_alignment = ifcopenshell.api.alignment.get_horizontal_layout(ali)
vertical_alignment = ifcopenshell.api.alignment.get_vertical_layout(ali)
curve = ifcopenshell.api.alignment.get_curve(horizontal_alignment)
assert curve == None
curve = ifcopenshell.api.alignment.get_curve(vertical_alignment)
assert curve == None
basis_curve = ifcopenshell.api.alignment.get_basis_curve(ali)
assert basis_curve.is_a("IfcCompositeCurve")
curve = ifcopenshell.api.alignment.get_curve(ali)
assert curve.is_a("IfcGradientCurve")
assert len(basis_curve.Segments) == 1
assert len(curve.Segments) == 1
design_parameters = file.create_entity(
type="IfcAlignmentHorizontalSegment",
StartTag=None,
EndTag=None,
StartPoint=file.createIfcCartesianPoint(Coordinates=((0.0, 0.0))),
StartDirection=0.0,
StartRadiusOfCurvature=0.0,
EndRadiusOfCurvature=0.0,
SegmentLength=100.0,
GravityCenterLineHeight=None,
PredefinedType="LINE",
)
2025-07-09 13:47:32 +05:00
end = ifcopenshell.api.alignment.create_layout_segment(file, horizontal_alignment, design_parameters)
2025-07-08 10:09:14 -07:00
basis_curve = ifcopenshell.api.alignment.get_basis_curve(ali)
assert basis_curve.is_a("IfcCompositeCurve")
assert len(basis_curve.Segments) == 2
2025-07-09 13:47:32 +05:00
2025-07-08 10:09:14 -07:00
design_parameters = file.createIfcAlignmentVerticalSegment(
StartDistAlong=0.0,
HorizontalLength=50.0,
StartHeight=20.0,
2025-07-09 13:47:32 +05:00
StartGradient=1.0 / 100.0,
EndGradient=1.0 / 100.0,
PredefinedType="CONSTANTGRADIENT",
)
end = ifcopenshell.api.alignment.create_layout_segment(file, vertical_alignment, design_parameters)
2025-07-08 10:09:14 -07:00
assert len(vertical_alignment.IsNestedBy[0].RelatedObjects) == 2
2025-07-09 13:47:32 +05:00
x = end[0, 3]
y = end[1, 3]
z = end[2, 3]
assert x == 50.0
2025-07-08 10:09:14 -07:00
assert y == 20.5
assert z == 0.0
2025-07-09 13:47:32 +05:00
dx = end[0, 0]
dy = end[1, 0]
gradient = dy / dx
2025-07-08 10:09:14 -07:00
design_parameters = file.createIfcAlignmentVerticalSegment(
StartDistAlong=50.0,
HorizontalLength=50.0,
StartHeight=y.item(),
2025-07-09 13:47:32 +05:00
StartGradient=-gradient,
EndGradient=-gradient,
PredefinedType="CONSTANTGRADIENT",
)
end = ifcopenshell.api.alignment.create_layout_segment(file, vertical_alignment, design_parameters)
2025-07-08 10:09:14 -07:00
assert len(vertical_alignment.IsNestedBy[0].RelatedObjects) == 3
2025-07-09 13:47:32 +05:00
x = end[0, 3]
y = end[1, 3]
z = end[2, 3]
assert x == 100.0
assert y == 20.0
2025-07-08 10:09:14 -07:00
assert z == 0.0
basis_curve = ifcopenshell.api.alignment.get_basis_curve(ali)
assert basis_curve.is_a("IfcCompositeCurve")
curve = ifcopenshell.api.alignment.get_curve(ali)
assert curve.is_a("IfcGradientCurve")
assert len(basis_curve.Segments) == 2
assert len(curve.Segments) == 3
def _test_horizontal_vertical2(file: ifcopenshell.file):
ali = file.by_type("IfcAlignment")[0]
vertical_alignment = ifcopenshell.api.alignment.get_vertical_layout(ali)
assert vertical_alignment == None
2025-07-09 13:47:32 +05:00
vertical_alignment = ifcopenshell.api.alignment.add_vertical_layout(file, ali)
2025-07-08 10:09:14 -07:00
design_parameters = file.createIfcAlignmentVerticalSegment(
StartDistAlong=0.0,
HorizontalLength=50.0,
StartHeight=20.0,
2025-07-09 13:47:32 +05:00
StartGradient=1.0 / 100.0,
EndGradient=1.0 / 100.0,
PredefinedType="CONSTANTGRADIENT",
)
end = ifcopenshell.api.alignment.create_layout_segment(file, vertical_alignment, design_parameters)
2025-07-08 10:09:14 -07:00
assert len(vertical_alignment.IsNestedBy[0].RelatedObjects) == 2
2025-07-09 13:47:32 +05:00
x = end[0, 3]
y = end[1, 3]
z = end[2, 3]
assert x == 50.0
2025-07-08 10:09:14 -07:00
assert y == 20.5
assert z == 0.0
2025-07-09 13:47:32 +05:00
dx = end[0, 0]
dy = end[1, 0]
gradient = dy / dx
2025-07-08 10:09:14 -07:00
design_parameters = file.createIfcAlignmentVerticalSegment(
StartDistAlong=50.0,
HorizontalLength=50.0,
StartHeight=y.item(),
2025-07-09 13:47:32 +05:00
StartGradient=-gradient,
EndGradient=-gradient,
PredefinedType="CONSTANTGRADIENT",
)
end = ifcopenshell.api.alignment.create_layout_segment(file, vertical_alignment, design_parameters)
2025-07-08 10:09:14 -07:00
assert len(vertical_alignment.IsNestedBy[0].RelatedObjects) == 3
2025-07-09 13:47:32 +05:00
x = end[0, 3]
y = end[1, 3]
z = end[2, 3]
assert x == 100.0
assert y == 20.0
2025-07-08 10:09:14 -07:00
assert z == 0.0
curve = ifcopenshell.api.alignment.get_curve(ali)
assert curve.is_a("IfcGradientCurve")
assert len(curve.Segments) == 3
2025-07-09 13:47:32 +05:00
2025-07-08 10:09:14 -07:00
def test_append_segment():
file = _test_horizontal()
_test_horizontal_vertical()
_test_horizontal_vertical2(file)
2025-07-09 13:47:32 +05:00
test_append_segment()