mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-08-09 17:31:45 +00:00
Fix #4305. Bug where importing spreadsheets into IFC didn't intelligently cast attribute data types
This commit is contained in:
@@ -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):
|
||||
|
||||
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user