diff --git a/src/ifcopenshell-python/ifcopenshell/util/selector.py b/src/ifcopenshell-python/ifcopenshell/util/selector.py index c0e0201a5d..4e7106558c 100644 --- a/src/ifcopenshell-python/ifcopenshell/util/selector.py +++ b/src/ifcopenshell-python/ifcopenshell/util/selector.py @@ -387,7 +387,30 @@ def set_element_value( if isinstance(key, str) and hasattr(element, key): if getattr(element, key) != value: - return setattr(element, key, value) + try: + # Try our luck + return setattr(element, key, value) + except: + # Try to cast + data_type = ifcopenshell.util.attribute.get_primitive_type( + element.wrapped_data.declaration() + .as_entity() + .attribute_by_index(element.wrapped_data.get_argument_index(key)) + ) + if data_type == "string": + value = str(value) + elif data_type == "float": + value = float(value) + elif data_type == "integer": + value = int(value) + elif data_type == "boolean": + if value in ("True", "true", "TRUE", "Yes", "1"): + value = True + elif value in ("False", "false", "FALSE", "No", "0"): + value = True + else: + value = bool(value) + return setattr(element, key, value) else: # Try to extract pset if isinstance(key, re.Pattern): diff --git a/src/ifcopenshell-python/test/util/test_selector.py b/src/ifcopenshell-python/test/util/test_selector.py index 9a5f9cadce..7e37506ac1 100644 --- a/src/ifcopenshell-python/test/util/test_selector.py +++ b/src/ifcopenshell-python/test/util/test_selector.py @@ -292,6 +292,13 @@ class TestSetElementValue(test.bootstrap.IFC4): ifcopenshell.util.placement.get_local_placement(element_without_placement.ObjectPlacement), matrix ) + def test_set_attribute(self): + element = self.file.createIfcWall() + subject.set_element_value(self.file, element, "Name", "Foo") + assert element.Name == "Foo" + subject.set_element_value(self.file, element, "Name", 123) + assert element.Name == "123" + class TestSelector(test.bootstrap.IFC4): def test_selecting_from_specified_elements(self):