diff --git a/src/ifcopenshell-python/ifcopenshell/entity_instance.py b/src/ifcopenshell-python/ifcopenshell/entity_instance.py index badbaed3d4..675d35420f 100644 --- a/src/ifcopenshell-python/ifcopenshell/entity_instance.py +++ b/src/ifcopenshell-python/ifcopenshell/entity_instance.py @@ -108,22 +108,33 @@ class entity_instance(object): return entity_instance.wrap_value(self.wrapped_data.get_argument(key)) def __setitem__(self, idx, value): + attr_type = real_attr_type = self.attribute_type(idx).title().replace(' ', '') + real_attr_type = real_attr_type.replace('Derived', 'None') + attr_type = attr_type.replace('Binary', 'String') + attr_type = attr_type.replace('Enumeration', 'String') + if value is None: - self.wrapped_data.setArgumentAsNull(idx) + if attr_type != "Derived": + self.wrapped_data.setArgumentAsNull(idx) else: - attr_type = real_attr_type = self.attribute_type(idx).title().replace(' ', '') - attr_type = attr_type.replace('Binary', 'String') - attr_type = attr_type.replace('Enumeration', 'String') - try: - if isinstance(value, unicode): - value = value.encode("utf-8") - except BaseException: - pass - try: - getattr(self.wrapped_data, "setArgumentAs%s" % attr_type)(idx, entity_instance.unwrap_value(value)) - except BaseException: + valid = attr_type != "Derived" + if valid: + try: + if isinstance(value, unicode): + value = value.encode("utf-8") + except BaseException: + pass + + try: + if attr_type != "Derived": + getattr(self.wrapped_data, "setArgumentAs%s" % attr_type)(idx, entity_instance.unwrap_value(value)) + except BaseException as e: + valid = False + + if not valid: raise ValueError("Expected %s for attribute %s.%s, got %r" % ( real_attr_type, self.is_a(), self.attribute_name(idx), value)) + return value def __len__(self):