Files
IfcOpenShell/src/ifcopenshell-python/test/test_create_shape.py
T

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

124 lines
4.7 KiB
Python
Raw Normal View History

import pytest
2023-06-19 13:48:35 +02:00
import ifcopenshell
import ifcopenshell.api
import ifcopenshell.geom
2023-06-19 14:34:14 +02:00
import ifcopenshell.api.owner.settings
from typing import get_args
class TestGeomSettings:
2024-06-27 10:58:10 +05:00
def test_settings(self):
settings = ifcopenshell.geom.settings()
assert set(get_args(ifcopenshell.geom.SETTING)) == set(settings.setting_names())
2024-06-27 10:58:10 +05:00
assert "use-python-opencascade" in settings.setting_names()
assert settings.get(settings.USE_PYTHON_OPENCASCADE) is False
assert settings.get("use-python-opencascade") is False
assert "USE_PYTHON_OPENCASCADE = False" in repr(settings)
if ifcopenshell.geom.has_occ:
settings.set("use-python-opencascade", True)
settings.set(settings.USE_PYTHON_OPENCASCADE, True)
assert settings.get(settings.USE_PYTHON_OPENCASCADE) is True
assert "USE_PYTHON_OPENCASCADE = True" in repr(settings)
else:
with pytest.raises(AttributeError):
settings.set("use-python-opencascade", True)
with pytest.raises(AttributeError):
settings.set(settings.USE_PYTHON_OPENCASCADE, True)
assert "USE_PYTHON_OPENCASCADE = False" in repr(settings)
2024-06-27 10:58:10 +05:00
def test_serializer_settings(self):
settings = ifcopenshell.geom.serializer_settings()
assert set(get_args(ifcopenshell.geom.SERIALIZER_SETTING)) == set(settings.setting_names())
# Only for settings.
assert "use-python-opencascade" not in settings.setting_names()
with pytest.raises(AttributeError):
settings.get(settings.USE_PYTHON_OPENCASCADE)
with pytest.raises(RuntimeError):
settings.get("use-python-opencascade")
assert "USE_PYTHON_OPENCASCADE" not in repr(settings)
2023-06-19 13:48:35 +02:00
2023-06-19 14:34:14 +02:00
class TestAssignObject:
2023-06-19 13:48:35 +02:00
def test_no_welding_on_distinct_items(self):
2023-06-19 14:34:14 +02:00
self.file = ifcopenshell.api.run("project.create_file")
ifcopenshell.api.owner.settings.get_user = lambda ifc: (ifc.by_type("IfcPersonAndOrganization") or [None])[0]
ifcopenshell.api.owner.settings.get_application = lambda ifc: (ifc.by_type("IfcApplication") or [None])[0]
2023-06-19 13:48:35 +02:00
ifcopenshell.api.run(
"root.create_entity", self.file, ifc_class="IfcProject", name="Test"
)
unit = ifcopenshell.api.run(
"unit.add_si_unit", self.file, unit_type="LENGTHUNIT"
)
ifcopenshell.api.run("unit.assign_unit", self.file, units=[unit])
context = ifcopenshell.api.run(
"context.add_context", self.file, context_type="Model"
)
element = ifcopenshell.api.run(
"root.create_entity", self.file, ifc_class="IfcWall"
)
def create_extrusion(x, y):
points = (
(x + 0.0, y + 0.0),
(x + 0.0, y + 1.0),
(x + 1.0, y + 1.0),
(x + 1.0, y + 0.0),
(x + 0.0, y + 0.0),
)
curve = self.file.createIfcPolyline(
[self.file.createIfcCartesianPoint(p) for p in points]
)
extrusion_direction = self.file.createIfcDirection((0.0, 0.0, 1.0))
return self.file.createIfcExtrudedAreaSolid(
self.file.createIfcArbitraryClosedProfileDef("AREA", None, curve),
self.file.createIfcAxis2Placement3D(
self.file.createIfcCartesianPoint((0.0, 0.0, 0.0)),
),
extrusion_direction,
1.0,
)
extrusions = [create_extrusion(x, 0.0) for x in [0.0, 1.0]]
element.Representation = self.file.createIfcProductDefinitionShape(
Representations=[
self.file.createIfcShapeRepresentation(
context,
context.ContextIdentifier,
"SweptSolid",
extrusions,
)
]
)
obj = ifcopenshell.geom.create_shape(
ifcopenshell.geom.settings(WELD_VERTICES=True), element
)
# item_ids is a per-triangle array, so we have 12 triangles per cube
# even though not documented, the order in representation items should match
assert (
obj.geometry.item_ids
== (extrusions[0].id(),) * 12 + (extrusions[1].id(),) * 12
)
# group the vertices
vs = [
obj.geometry.verts[i : i + 3] for i in range(0, len(obj.geometry.verts), 3)
]
# welding should not happen between distinct items so the total number of verts should be 2 times 8
assert len(vs) == 16
# even though there are only 12 unique vertices as the cubes are touching
assert len(set(vs)) == 12
if __name__ == "__main__":
import pytest
pytest.main(["-vvsx", __file__])