diff --git a/src/ifcopenshell-python/ifcopenshell/file.py b/src/ifcopenshell-python/ifcopenshell/file.py index a96c5c7687..d34086c175 100644 --- a/src/ifcopenshell-python/ifcopenshell/file.py +++ b/src/ifcopenshell-python/ifcopenshell/file.py @@ -378,7 +378,7 @@ class file(object): return [entity_instance(e, self) for e in fn(inst.wrapped_data, max_levels)] - def get_inverse(self, inst): + def get_inverse(self, inst, allow_duplicate=False): """Return a list of entities that reference this entity :param inst: The entity instance to get inverse relationships @@ -386,7 +386,10 @@ class file(object): :returns: A list of ifcopenshell.entity_instance.entity_instance objects :rtype: list """ - return [entity_instance(e, self) for e in self.wrapped_data.get_inverse(inst.wrapped_data)] + inverses = [entity_instance(e, self) for e in self.wrapped_data.get_inverse(inst.wrapped_data)] + if allow_duplicate: + return inverses + return set(inverses) def remove(self, inst): """Deletes an IFC object in the file. diff --git a/src/ifcopenshell-python/test/test_file.py b/src/ifcopenshell-python/test/test_file.py index 7cf713b733..1e41af6160 100644 --- a/src/ifcopenshell-python/test/test_file.py +++ b/src/ifcopenshell-python/test/test_file.py @@ -191,7 +191,12 @@ class TestFile(test.bootstrap.IFC4): def test_getting_inverse_references_of_an_element(self): owner = self.file.createIfcOwnerHistory() element = self.file.createIfcWall(OwnerHistory=owner) - assert self.file.get_inverse(owner) == [element] + assert self.file.get_inverse(owner) == {element} + + def test_getting_multiple_inverses_if_an_element_is_referenced_twice_by_the_same_element(self): + user = self.file.createIfcPersonAndOrganization() + owner = self.file.createIfcOwnerHistory(OwningUser=user, LastModifyingUser=user) + assert self.file.get_inverse(user, allow_duplicate=True) == [owner, owner] def test_removing_an_element(self): element = self.file.createIfcWall(GlobalId="global_id")