From 0f39b0939628660c5705f7b7b4917c16bb9e4fa0 Mon Sep 17 00:00:00 2001 From: Thomas Krijnen Date: Sat, 17 Jul 2021 09:38:31 +0200 Subject: [PATCH] #1567 add instance after the values are set --- src/ifcopenshell-python/ifcopenshell/file.py | 29 ++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/ifcopenshell-python/ifcopenshell/file.py b/src/ifcopenshell-python/ifcopenshell/file.py index 2963cf662b..5f9fbc7a3d 100644 --- a/src/ifcopenshell-python/ifcopenshell/file.py +++ b/src/ifcopenshell-python/ifcopenshell/file.py @@ -264,19 +264,44 @@ class file(object): eid = kwargs.pop("id", -1) except: pass + e = entity_instance((self.schema, type), self) - self.wrapped_data.add(e.wrapped_data, eid) - e.wrapped_data.this.disown() + + # Create pairs of {attribute index, attribute value}. + # Keyword arguments are mapped to their corresponding + # numeric index with get_argument_index(). + + # @todo we should probably check that values for + # attributes are not passed as duplicates using + # both regular arguments and keyword arguments. attrs = list(enumerate(args)) + [(e.wrapped_data.get_argument_index(name), arg) for name, arg in kwargs.items()] + + # Don't store these attributes as transactions + # as the creation it self is already stored with + # it's arguments if attrs: transaction = self.transaction self.transaction = None + for idx, arg in attrs: e[idx] = arg + + # Restore transaction status if attrs: self.transaction = transaction + if self.transaction: self.transaction.store_create(e) + + # Once the values are populated add the instance + # to the file. + self.wrapped_data.add(e.wrapped_data, eid) + + # The file container now handles the lifetime of + # this instance. Tell SWIG that it is no longer + # the owner. + e.wrapped_data.this.disown() + return e def __getattr__(self, attr):