diff --git a/src/ifcopenshell-python/ifcopenshell/util/element.py b/src/ifcopenshell-python/ifcopenshell/util/element.py index c7821390df..48d65ae7ae 100644 --- a/src/ifcopenshell-python/ifcopenshell/util/element.py +++ b/src/ifcopenshell-python/ifcopenshell/util/element.py @@ -1169,6 +1169,25 @@ def get_groups(element: ifcopenshell.entity_instance) -> list[ifcopenshell.entit return groups +def get_controls(element: ifcopenshell.entity_instance) -> Generator[ifcopenshell.entity_instance]: + """ + Retrieves the controls of an element. + + :param element: The IFC element + :return: Generator of IfcControl elements assigned to the element. + + Example: + + .. code:: python + + task = file.by_type("IfcTask")[0] + control = ifcopenshell.util.element.get_controls(task)[0] + """ + for rel in element.HasAssignments: + if rel.is_a("IfcRelAssignsToControl"): + yield rel.RelatingControl + + def get_parent(element: ifcopenshell.entity_instance) -> Union[ifcopenshell.entity_instance, None]: """Get the parent in the spatial heirarchy diff --git a/src/ifcopenshell-python/test/util/test_element.py b/src/ifcopenshell-python/test/util/test_element.py index b691620824..1a1e28d58b 100644 --- a/src/ifcopenshell-python/test/util/test_element.py +++ b/src/ifcopenshell-python/test/util/test_element.py @@ -16,6 +16,8 @@ # You should have received a copy of the GNU Lesser General Public License # along with IfcOpenShell. If not, see . +import ifcopenshell.api.control +import ifcopenshell.api.cost import ifcopenshell.api.profile import pytest import test.bootstrap @@ -926,6 +928,23 @@ class TestGetGroupsIFC2X3(test.bootstrap.IFC2X3, TestGetGroupsIFC4): pass +class TestGetControls(test.bootstrap.IFC2X3): + def test_run(self): + model = self.file + element = ifcopenshell.api.root.create_entity(model, ifc_class="IfcWall") + control = ifcopenshell.api.cost.add_cost_schedule(model) + ifcopenshell.api.control.assign_control(model, related_objects=[element], relating_control=control) + assert list(subject.get_controls(element)) == [control] + + +class TestGetControlsIFC4(test.bootstrap.IFC4, TestGetControls): + pass + + +class TestGetControlsIFC4X3(test.bootstrap.IFC4X3, TestGetControls): + pass + + class TestGetAggregateIFC4(test.bootstrap.IFC4): def test_getting_the_containing_aggregate_of_a_subelement(self): element = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")