From d638405ff8422120689b393807ab3f8ba18251b1 Mon Sep 17 00:00:00 2001 From: Dion Moult Date: Mon, 29 Nov 2021 20:16:25 +1100 Subject: [PATCH] Assigning library references now works on IFC2X3. --- .../api/library/assign_reference.py | 10 +++++++++- .../test/api/library/test_assign_reference.py | 20 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/ifcopenshell-python/ifcopenshell/api/library/assign_reference.py b/src/ifcopenshell-python/ifcopenshell/api/library/assign_reference.py index d2d8816527..06c3efe537 100644 --- a/src/ifcopenshell-python/ifcopenshell/api/library/assign_reference.py +++ b/src/ifcopenshell-python/ifcopenshell/api/library/assign_reference.py @@ -12,7 +12,10 @@ class Usecase: self.settings[key] = value def execute(self): - rels = self.settings["reference"].LibraryRefForObjects + if self.file.schema == "IFC2X3": + rels = self.get_ifc2x3_rels() + else: + rels = self.settings["reference"].LibraryRefForObjects if not rels: return self.file.create_entity( "IfcRelAssociatesLibrary", @@ -32,3 +35,8 @@ class Usecase: rel.RelatedObjects = related_objects ifcopenshell.api.run("owner.update_owner_history", self.file, element=rel) return rel + + def get_ifc2x3_rels(self): + return [ + r for r in self.file.by_type("IfcRelAssociatesLibrary") if r.RelatingLibrary == self.settings["reference"] + ] diff --git a/src/ifcopenshell-python/test/api/library/test_assign_reference.py b/src/ifcopenshell-python/test/api/library/test_assign_reference.py index aeac898fd0..5df5b19cb5 100644 --- a/src/ifcopenshell-python/test/api/library/test_assign_reference.py +++ b/src/ifcopenshell-python/test/api/library/test_assign_reference.py @@ -18,3 +18,23 @@ class TestAssignReference(test.bootstrap.IFC4): ifcopenshell.api.run("library.assign_reference", self.file, product=product, reference=reference) ifcopenshell.api.run("library.assign_reference", self.file, product=product, reference=reference) assert reference.LibraryRefForObjects[0].RelatedObjects == (product,) + + +class TestAssignReference(test.bootstrap.IFC2X3): + def test_assigning_a_reference(self): + reference = self.file.createIfcLibraryReference() + product = self.file.createIfcWall() + product2 = self.file.createIfcWall() + ifcopenshell.api.run("library.assign_reference", self.file, product=product, reference=reference) + rel = self.file.by_type("IfcRelAssociatesLibrary")[0] + assert rel.RelatedObjects == (product,) + ifcopenshell.api.run("library.assign_reference", self.file, product=product2, reference=reference) + assert rel.RelatedObjects == (product, product2) + + def test_not_assigning_twice(self): + reference = self.file.createIfcLibraryReference() + product = self.file.createIfcWall() + ifcopenshell.api.run("library.assign_reference", self.file, product=product, reference=reference) + ifcopenshell.api.run("library.assign_reference", self.file, product=product, reference=reference) + rel = self.file.by_type("IfcRelAssociatesLibrary")[0] + assert rel.RelatedObjects == (product,)