diff --git a/src/ifcopenshell-python/ifcopenshell/util/cost.py b/src/ifcopenshell-python/ifcopenshell/util/cost.py index 875594f1a5..4354e49e90 100644 --- a/src/ifcopenshell-python/ifcopenshell/util/cost.py +++ b/src/ifcopenshell-python/ifcopenshell/util/cost.py @@ -196,9 +196,12 @@ def get_cost_items_for_product(product: ifcopenshell.entity_instance) -> list[if :return: A list of IfcCostItem objects representing the cost items related to the product. """ cost_items = [] - for assignment in product.HasAssignments: - if assignment.is_a("IfcRelAssignsToControl") and assignment.RelatingControl.is_a("IfcCostItem"): - cost_items.append(assignment.RelatingControl) + for assignment in product.HasAssignments or []: + if assignment.is_a("IfcRelAssignsToControl"): + control = assignment.RelatingControl + if control and control.is_a("IfcCostItem"): + cost_items.append(control) + return cost_items diff --git a/src/ifcopenshell-python/test/util/test_cost.py b/src/ifcopenshell-python/test/util/test_cost.py new file mode 100644 index 0000000000..516a69edd0 --- /dev/null +++ b/src/ifcopenshell-python/test/util/test_cost.py @@ -0,0 +1,52 @@ +# IfcOpenShell - IFC toolkit and geometry engine +# Copyright (C) 2021 Dion Moult +# +# 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 . + +import pytest + +import ifcopenshell.api.control +import ifcopenshell.api.cost +import test.bootstrap +import ifcopenshell.api.root + +import ifcopenshell.util.cost as subject + +class TestGetCostItemForProduct(test.bootstrap.IFC4): + def test_run(self): + model = self.file + element = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall") + cost_schedule = ifcopenshell.api.cost.add_cost_schedule(model) + item1 = ifcopenshell.api.cost.add_cost_item(model, cost_schedule=cost_schedule) + ifcopenshell.api.control.assign_control(model, related_objects=[element], relating_control=item1) + assert list(subject.get_cost_items_for_product(element)) == [item1] + + def test_remove_cost_item(self): + model = self.file + element = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall") + cost_schedule = ifcopenshell.api.cost.add_cost_schedule(model) + item1 = ifcopenshell.api.cost.add_cost_item(model, cost_schedule=cost_schedule) + ifcopenshell.api.control.assign_control(model, related_objects=[element], relating_control=item1) + ifcopenshell.api.cost.remove_cost_item(model, cost_item = item1) + assert list(subject.get_cost_items_for_product(element)) == [] + + def test_no_assigned_cost_items(self): + model = self.file + element = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall") + cost_schedule = ifcopenshell.api.cost.add_cost_schedule(model) + item1 = ifcopenshell.api.cost.add_cost_item(model, cost_schedule=cost_schedule) + assert list(subject.get_cost_items_for_product(element)) == [] +