From 793b28e43b773e07a92a8fb0fd7c810371d0d97a Mon Sep 17 00:00:00 2001 From: Andrej730 Date: Mon, 14 Aug 2023 11:04:55 +0500 Subject: [PATCH] Remove empty IfcDocumentInformationRelationship #3581 --- .../ifcopenshell/api/document/remove_information.py | 7 +++++++ .../test/api/document/test_remove_information.py | 13 +++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/ifcopenshell-python/ifcopenshell/api/document/remove_information.py b/src/ifcopenshell-python/ifcopenshell/api/document/remove_information.py index 9506c51478..64fba5975c 100644 --- a/src/ifcopenshell-python/ifcopenshell/api/document/remove_information.py +++ b/src/ifcopenshell-python/ifcopenshell/api/document/remove_information.py @@ -46,10 +46,17 @@ class Usecase: def execute(self): for reference in self.settings["information"].HasDocumentReferences or []: ifcopenshell.api.run("document.remove_reference", self.file, reference=reference) + for rel in self.settings["information"].IsPointer or []: for information in rel.RelatedDocuments: ifcopenshell.api.run("document.remove_information", self.file, information=information) self.file.remove(rel) + + # remove IfcDocumentInformationRelationship so it won't become invalid + for rel in self.settings["information"].IsPointedTo or []: + if rel.RelatedDocuments == (self.settings["information"],): + self.file.remove(rel) + for rel in self.settings["information"].DocumentInfoForObjects or []: self.file.remove(rel) self.file.remove(self.settings["information"]) diff --git a/src/ifcopenshell-python/test/api/document/test_remove_information.py b/src/ifcopenshell-python/test/api/document/test_remove_information.py index 4df4857052..d03a7341ad 100644 --- a/src/ifcopenshell-python/test/api/document/test_remove_information.py +++ b/src/ifcopenshell-python/test/api/document/test_remove_information.py @@ -37,6 +37,19 @@ class TestRemoveInformation(test.bootstrap.IFC4): assert len(self.file.by_type("IfcDocumentReference")) == 0 assert len(self.file.by_type("IfcRelAssociatesDocument")) == 0 + # test removing relationship to another information if it was the only relating element + information = ifcopenshell.api.run("document.add_information", self.file, parent=None) + information1 = ifcopenshell.api.run("document.add_information", self.file, parent=information) + information2 = ifcopenshell.api.run("document.add_information", self.file, parent=information) + + ifcopenshell.api.run("document.remove_information", self.file, information=information1) + assert len(self.file.by_type("IfcDocumentInformation")) == 2 + assert len(self.file.by_type("IfcDocumentInformationRelationship")) == 1 + + ifcopenshell.api.run("document.remove_information", self.file, information=information2) + assert len(self.file.by_type("IfcDocumentInformation")) == 1 + assert len(self.file.by_type("IfcDocumentInformationRelationship")) == 0 + def test_removing_all_subdocuments_and_their_references_too(self): project = self.file.createIfcProject() information = ifcopenshell.api.run("document.add_information", self.file, parent=None)