Add api.structural.assign_product, assign_to_building, and api.geometry.add_topology_representation

assign_product creates IfcRelAssignsToProduct linking a structural member to
a physical building element. assign_to_building creates IfcRelServicesBuildings
linking a structural analysis model to a building. add_topology_representation
creates IfcTopologyRepresentation for structural elements, inferring the
representation type from the item class.

Generated with the assistance of an AI coding tool.
This commit is contained in:
Bruno Postle
2026-03-30 07:28:01 +01:00
parent be05d771a2
commit f46be80193
8 changed files with 432 additions and 0 deletions
@@ -0,0 +1,82 @@
# IfcOpenShell - IFC toolkit and geometry engine
# Copyright (C) 2026 Dion Moult <dion@thinkmoult.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/>.
# This file was generated with the assistance of an AI coding tool.
import ifcopenshell.api.context
import ifcopenshell.api.geometry
import ifcopenshell.api.root
import test.bootstrap
class TestAddTopologyRepresentation(test.bootstrap.IFC4):
def setup_context(self):
ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcProject")
model = ifcopenshell.api.context.add_context(self.file, context_type="Model")
return ifcopenshell.api.context.add_context(
self.file,
context_type="Model",
context_identifier="Reference",
target_view="GRAPH_VIEW",
parent=model,
)
def test_creates_topology_representation(self):
context = self.setup_context()
face = self.file.create_entity("IfcFaceSurface")
rep = ifcopenshell.api.geometry.add_topology_representation(self.file, context=context, item=face)
assert rep.is_a("IfcTopologyRepresentation")
assert rep.ContextOfItems == context
assert face in rep.Items
def test_infers_face_representation_type(self):
context = self.setup_context()
face = self.file.create_entity("IfcFaceSurface")
rep = ifcopenshell.api.geometry.add_topology_representation(self.file, context=context, item=face)
assert rep.RepresentationType == "Face"
def test_infers_edge_representation_type(self):
context = self.setup_context()
edge = self.file.create_entity("IfcEdge")
rep = ifcopenshell.api.geometry.add_topology_representation(self.file, context=context, item=edge)
assert rep.RepresentationType == "Edge"
def test_defaults_representation_identifier_to_context_identifier(self):
context = self.setup_context()
face = self.file.create_entity("IfcFaceSurface")
rep = ifcopenshell.api.geometry.add_topology_representation(self.file, context=context, item=face)
assert rep.RepresentationIdentifier == context.ContextIdentifier
def test_custom_representation_identifier(self):
context = self.setup_context()
face = self.file.create_entity("IfcFaceSurface")
rep = ifcopenshell.api.geometry.add_topology_representation(
self.file, context=context, item=face, representation_identifier="Body"
)
assert rep.RepresentationIdentifier == "Body"
def test_custom_representation_type_overrides_inferred(self):
context = self.setup_context()
face = self.file.create_entity("IfcFaceSurface")
rep = ifcopenshell.api.geometry.add_topology_representation(
self.file, context=context, item=face, representation_type="Undefined"
)
assert rep.RepresentationType == "Undefined"
class TestAddTopologyRepresentationIFC2X3(test.bootstrap.IFC2X3, TestAddTopologyRepresentation):
pass
@@ -0,0 +1,56 @@
# IfcOpenShell - IFC toolkit and geometry engine
# Copyright (C) 2026 Dion Moult <dion@thinkmoult.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/>.
# This file was generated with the assistance of an AI coding tool.
import ifcopenshell.api.root
import ifcopenshell.api.structural
import test.bootstrap
class TestAssignProduct(test.bootstrap.IFC4):
def test_creating_a_new_relationship(self):
member = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcStructuralSurfaceMember")
wall = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
rel = ifcopenshell.api.structural.assign_product(self.file, relating_product=member, related_object=wall)
assert rel.is_a("IfcRelAssignsToProduct")
assert rel.RelatingProduct == member
assert wall in rel.RelatedObjects
def test_adding_a_second_object_to_an_existing_relationship(self):
member = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcStructuralSurfaceMember")
wall1 = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
wall2 = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
rel1 = ifcopenshell.api.structural.assign_product(self.file, relating_product=member, related_object=wall1)
rel2 = ifcopenshell.api.structural.assign_product(self.file, relating_product=member, related_object=wall2)
assert rel1 == rel2
assert len(self.file.by_type("IfcRelAssignsToProduct")) == 1
assert wall1 in rel1.RelatedObjects
assert wall2 in rel1.RelatedObjects
def test_does_not_duplicate_an_existing_assignment(self):
member = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcStructuralSurfaceMember")
wall = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
ifcopenshell.api.structural.assign_product(self.file, relating_product=member, related_object=wall)
ifcopenshell.api.structural.assign_product(self.file, relating_product=member, related_object=wall)
assert len(self.file.by_type("IfcRelAssignsToProduct")) == 1
rels = self.file.by_type("IfcRelAssignsToProduct")
assert len(rels[0].RelatedObjects) == 1
class TestAssignProductIFC2X3(test.bootstrap.IFC2X3, TestAssignProduct):
pass
@@ -0,0 +1,62 @@
# IfcOpenShell - IFC toolkit and geometry engine
# Copyright (C) 2026 Dion Moult <dion@thinkmoult.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/>.
# This file was generated with the assistance of an AI coding tool.
import ifcopenshell.api.root
import ifcopenshell.api.structural
import test.bootstrap
class TestAssignToBuilding(test.bootstrap.IFC4):
def test_creating_a_new_relationship(self):
model = ifcopenshell.api.structural.add_structural_analysis_model(self.file)
building = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcBuilding")
rel = ifcopenshell.api.structural.assign_to_building(
self.file, structural_analysis_model=model, building=building
)
assert rel.is_a("IfcRelServicesBuildings")
assert rel.RelatingSystem == model
assert building in rel.RelatedBuildings
def test_adding_a_second_building_to_an_existing_relationship(self):
model = ifcopenshell.api.structural.add_structural_analysis_model(self.file)
building1 = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcBuilding")
building2 = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcBuilding")
rel1 = ifcopenshell.api.structural.assign_to_building(
self.file, structural_analysis_model=model, building=building1
)
rel2 = ifcopenshell.api.structural.assign_to_building(
self.file, structural_analysis_model=model, building=building2
)
assert rel1 == rel2
assert len(self.file.by_type("IfcRelServicesBuildings")) == 1
assert building1 in rel1.RelatedBuildings
assert building2 in rel1.RelatedBuildings
def test_does_not_duplicate_an_existing_assignment(self):
model = ifcopenshell.api.structural.add_structural_analysis_model(self.file)
building = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcBuilding")
ifcopenshell.api.structural.assign_to_building(self.file, structural_analysis_model=model, building=building)
ifcopenshell.api.structural.assign_to_building(self.file, structural_analysis_model=model, building=building)
assert len(self.file.by_type("IfcRelServicesBuildings")) == 1
rels = self.file.by_type("IfcRelServicesBuildings")
assert len(rels[0].RelatedBuildings) == 1
class TestAssignToBuildingIFC2X3(test.bootstrap.IFC2X3, TestAssignToBuilding):
pass