diff --git a/src/ifcopenshell-python/ifcopenshell/api/document/add_information.py b/src/ifcopenshell-python/ifcopenshell/api/document/add_information.py index c23ec11db0..d54cede7dc 100644 --- a/src/ifcopenshell-python/ifcopenshell/api/document/add_information.py +++ b/src/ifcopenshell-python/ifcopenshell/api/document/add_information.py @@ -29,12 +29,12 @@ class Usecase: def execute(self): id_attribute = "DocumentId" if self.file.schema == "IFC2X3" else "Identification" information = self.file.create_entity( - "IfcDocumentInformation", **{id_attribute: ifcopenshell.guid.new(), "Name": "Unnamed"} + "IfcDocumentInformation", **{id_attribute: "X", "Name": "Unnamed"} ) parent = self.settings["parent"] if not parent and self.file.by_type("IfcProject"): parent = self.file.by_type("IfcProject")[0] - if parent.is_a("IfcProject") or prarent.is_a("IfcContext"): + if parent.is_a("IfcProject") or parent.is_a("IfcContext"): self.file.create_entity( "IfcRelAssociatesDocument", GlobalId=ifcopenshell.guid.new(), @@ -42,4 +42,16 @@ class Usecase: RelatingDocument=information, RelatedObjects=[parent], ) + elif parent.is_a("IfcDocumentInformation"): + if parent.IsPointer: + rel = parent.IsPointer[0] + documents = set(rel.RelatedDocuments) + documents.add(information) + rel.RelatedDocuments = list(documents) + else: + self.file.create_entity( + "IfcDocumentInformationRelationship", + RelatingDocument=parent, + RelatedDocuments=[information] + ) return information diff --git a/src/ifcopenshell-python/test/api/document/test_add_information.py b/src/ifcopenshell-python/test/api/document/test_add_information.py index 1499449348..4d225e1f39 100644 --- a/src/ifcopenshell-python/test/api/document/test_add_information.py +++ b/src/ifcopenshell-python/test/api/document/test_add_information.py @@ -22,7 +22,7 @@ import ifcopenshell.api class TestAddInformation(test.bootstrap.IFC4): def test_adding_information(self): - self.file.createIfcProject() + project = self.file.createIfcProject() element = ifcopenshell.api.run("document.add_information", self.file, parent=None) assert element.is_a("IfcDocumentInformation") assert len(self.file.by_type("IfcDocumentInformation")) == 1 @@ -33,3 +33,15 @@ class TestAddInformation(test.bootstrap.IFC4): rel = element.DocumentInfoForObjects[0] assert rel.is_a("IfcRelAssociatesDocument") assert rel.RelatedObjects[0] == project + + def test_adding_a_subdocument(self): + project = self.file.createIfcProject() + parent = ifcopenshell.api.run("document.add_information", self.file, parent=None) + element = ifcopenshell.api.run("document.add_information", self.file, parent=parent) + assert element.is_a("IfcDocumentInformation") + assert len(self.file.by_type("IfcDocumentInformation")) == 2 + assert element.IsPointedTo[0].RelatingDocument == parent + assert parent.IsPointer[0].RelatedDocuments[0] == element + element2 = ifcopenshell.api.run("document.add_information", self.file, parent=parent) + assert element in parent.IsPointer[0].RelatedDocuments + assert element2 in parent.IsPointer[0].RelatedDocuments