From e87524ba117f1d2be57e74f53fe036110f7c9d35 Mon Sep 17 00:00:00 2001 From: Thomas Krijnen Date: Tue, 14 Feb 2023 09:52:17 +0100 Subject: [PATCH] ifcopenshell.settings.unpack_non_aggregate_inverses --- .../ifcopenshell/entity_instance.py | 15 +++++++- .../ifcopenshell/settings.py | 36 +++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 src/ifcopenshell-python/ifcopenshell/settings.py diff --git a/src/ifcopenshell-python/ifcopenshell/entity_instance.py b/src/ifcopenshell-python/ifcopenshell/entity_instance.py index 45b276888d..d1e8f8452b 100644 --- a/src/ifcopenshell-python/ifcopenshell/entity_instance.py +++ b/src/ifcopenshell-python/ifcopenshell/entity_instance.py @@ -29,6 +29,7 @@ import operator import functools from . import ifcopenshell_wrapper +from . import settings try: import logging @@ -135,9 +136,21 @@ class entity_instance(object): self.wrapped_data.get_argument(idx), self.wrapped_data.file ) elif attr_cat == INVERSE: - return entity_instance.wrap_value( + vs = entity_instance.wrap_value( self.wrapped_data.get_inverse(name), self.wrapped_data.file ) + if settings.unpack_non_aggregate_inverses: + schema_name = self.wrapped_data.is_a(True).split(".")[0] + ent = ifcopenshell_wrapper.schema_by_name(schema_name).declaration_by_name( + self.is_a() + ) + inv = [i for i in ent.all_inverse_attributes() if i.name() == name][0] + if (inv.bound1(), inv.bound2()) == (-1, -1): + if vs: + vs = vs[0] + else: + vs = None + return vs # derived attribute perhaps? schema_name = self.wrapped_data.is_a(True).split(".")[0] diff --git a/src/ifcopenshell-python/ifcopenshell/settings.py b/src/ifcopenshell-python/ifcopenshell/settings.py new file mode 100644 index 0000000000..7d4ae8bbd3 --- /dev/null +++ b/src/ifcopenshell-python/ifcopenshell/settings.py @@ -0,0 +1,36 @@ +# IfcOpenShell - IFC toolkit and geometry engine +# Copyright (C) 2021 Thomas Krijnen +# +# 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 . + +""" +When true return inverses without an aggregate specifier as a single +element or None. Example: + +>>> import ifcopenshell +>>> f = ifcopenshell.file(schema='ifc2x3') +>>> f.createIfcGroup() +#1=IfcGroup($,$,$,$,$) +>>> f.createIfcRelAssignsToGroup(RelatingGroup=f[1]) +#2=IfcRelAssignsToGroup($,$,$,$,$,$,#1) +>>> f[1].IsGroupedBy +(#2=IfcRelAssignsToGroup($,$,$,$,$,$,#1),) +>>> ifcopenshell.settings.unpack_non_aggregate_inverses = True +>>> f[1].IsGroupedBy +#2=IfcRelAssignsToGroup($,$,$,$,$,$,#1) +""" + +unpack_non_aggregate_inverses = False \ No newline at end of file