From 17146d3572b38affb5cb01dd39a5772a7f680aad Mon Sep 17 00:00:00 2001 From: Petru Conduraru Date: Thu, 9 Jul 2026 13:24:42 +0300 Subject: [PATCH] resource.assign_resource: fix typo in duplicate guard #8203 The guard that avoids re-assigning the same object to the same resource tested is_a("IfclRelAssignsToResource") (stray "l"), so it never matched. A repeat assignment therefore fell through and appended the related object to RelatedObjects a second time. Corrected to "IfcRelAssignsToResource". Co-Authored-By: Claude Opus 4.8 (cherry picked from commit 21ae78fbc2b2ae6aa1c9cec0684825cc12a4b808) --- .../api/resource/assign_resource.py | 2 +- .../test/api/resource/test_assign_resource.py | 45 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 src/ifcopenshell-python/test/api/resource/test_assign_resource.py diff --git a/src/ifcopenshell-python/ifcopenshell/api/resource/assign_resource.py b/src/ifcopenshell-python/ifcopenshell/api/resource/assign_resource.py index 8ba19a650d..32f9f2ced9 100644 --- a/src/ifcopenshell-python/ifcopenshell/api/resource/assign_resource.py +++ b/src/ifcopenshell-python/ifcopenshell/api/resource/assign_resource.py @@ -81,7 +81,7 @@ def assign_resource( """ if related_object.HasAssignments: for assignment in related_object.HasAssignments: - if assignment.is_a("IfclRelAssignsToResource") and assignment.RelatingResource == relating_resource: + if assignment.is_a("IfcRelAssignsToResource") and assignment.RelatingResource == relating_resource: return assignment resource_of = None diff --git a/src/ifcopenshell-python/test/api/resource/test_assign_resource.py b/src/ifcopenshell-python/test/api/resource/test_assign_resource.py new file mode 100644 index 0000000000..9c8ce2c2a4 --- /dev/null +++ b/src/ifcopenshell-python/test/api/resource/test_assign_resource.py @@ -0,0 +1,45 @@ +# IfcOpenShell - IFC toolkit and geometry engine +# Copyright (C) 2024 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 ifcopenshell.api.resource +import ifcopenshell.api.root +import test.bootstrap + + +class TestAssignResource(test.bootstrap.IFC4): + def test_assigning_a_new_object_to_a_resource(self): + self.file.create_entity("IfcProject") + resource = ifcopenshell.api.resource.add_resource(self.file, ifc_class="IfcCrewResource") + actor = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcActor") + rel = ifcopenshell.api.resource.assign_resource(self.file, relating_resource=resource, related_object=actor) + assert rel.is_a("IfcRelAssignsToResource") + assert rel.RelatingResource == resource + assert rel.RelatedObjects == (actor,) + + def test_assigning_the_same_object_twice_does_not_duplicate_related_objects(self): + # Regression test for #8203: a typo in the duplicate guard + # ("IfclRelAssignsToResource") meant the guard never matched, so a + # repeat assignment appended the related object to RelatedObjects again. + self.file.create_entity("IfcProject") + resource = ifcopenshell.api.resource.add_resource(self.file, ifc_class="IfcCrewResource") + actor = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcActor") + rel1 = ifcopenshell.api.resource.assign_resource(self.file, relating_resource=resource, related_object=actor) + rel2 = ifcopenshell.api.resource.assign_resource(self.file, relating_resource=resource, related_object=actor) + assert rel1 == rel2 + assert len(self.file.by_type("IfcRelAssignsToResource")) == 1 + assert rel2.RelatedObjects == (actor,)