The IfcOpenShell API now supports automatic primitive casting for editing properties. See #1943.

This commit is contained in:
Dion Moult
2022-01-07 16:09:54 +11:00
parent eb04a7d579
commit 4f047274bf
4 changed files with 77 additions and 33 deletions
@@ -148,7 +148,6 @@ class EnablePsetEditing(bpy.types.Operator):
new.is_null = data.get(prop_template.Name, None) is None new.is_null = data.get(prop_template.Name, None) is None
new.is_optional = True new.is_optional = True
new.data_type = data_type new.data_type = data_type
new.enum_data_type = prop_template.PrimaryMeasureType
if data_type == "string": if data_type == "string":
new.string_value = "" if new.is_null else data[prop_template.Name] new.string_value = "" if new.is_null else data[prop_template.Name]
@@ -161,7 +160,7 @@ class EnablePsetEditing(bpy.types.Operator):
elif data_type == "enum": elif data_type == "enum":
new.enum_items = json.dumps(enum_items) new.enum_items = json.dumps(enum_items)
if data.get(prop_template.Name): if data.get(prop_template.Name):
new.enum_value = data[prop_template.Name] new.enum_value = str(data[prop_template.Name])
def load_from_pset_data(self, pset_data): def load_from_pset_data(self, pset_data):
for prop_id in pset_data["Properties"]: for prop_id in pset_data["Properties"]:
+30 -26
View File
@@ -82,35 +82,45 @@ def update_is_visible(self, context):
pass pass
def InternStr(s): # If we don't cache strings, accents get mangled due to a Blender bug
if not hasattr(InternStr, "StringCache"): # Another way to define a function attribute # https://blender.stackexchange.com/questions/216230/is-there-a-workaround-for-the-known-bug-in-dynamic-enumproperty
InternStr.StringCache = defaultdict(str) # https://github.com/IfcOpenShell/IfcOpenShell/pull/1945
InternStr.StringCache[s] = s # https://github.com/IfcOpenShell/IfcOpenShell/issues/1941
return InternStr.StringCache[s] def cache_string(s):
s = str(s)
if not hasattr(cache_string, "data"): # Another way to define a function attribute
cache_string.data = defaultdict(str)
cache_string.data[s] = s
return cache_string.data[s]
InternStr.StringCache = {}
cache_string.data = {}
def getAttributeEnumValues(prop, context): def getAttributeEnumValues(prop, context):
# Support weird buildingSMART dictionary mappings which behave like enums # Support weird buildingSMART dictionary mappings which behave like enums
items = [] items = []
data = json.loads(prop.enum_items) data = json.loads(prop.enum_items)
if isinstance(data, dict): if isinstance(data, dict):
for k, v in data.items(): for k, v in data.items():
items.append(( items.append(
InternStr(k), (
InternStr(v), cache_string(k),
"", cache_string(v),
)) "",
)
)
else: else:
for e in data: for e in data:
items.append(( items.append(
InternStr(e), (
InternStr(e), cache_string(e),
"", cache_string(e),
)) "",
)
)
return items return items
@@ -173,17 +183,11 @@ class Attribute(PropertyGroup):
is_optional: BoolProperty(name="Is Optional") is_optional: BoolProperty(name="Is Optional")
enum_items: StringProperty(name="Value") enum_items: StringProperty(name="Value")
enum_value: EnumProperty(items=getAttributeEnumValues, name="Value", update=updateAttributeValue) enum_value: EnumProperty(items=getAttributeEnumValues, name="Value", update=updateAttributeValue)
enum_data_type: StringProperty(name="Enum Data Type")
def get_value(self): def get_value(self):
if self.is_null: if self.is_null:
return None return None
if self.data_type == "enum": return getattr(self, str(self.get_value_name()), None)
type_map = blenderbim.bim.schema.ifc.type_map
type_fn = {'integer':int, 'string':str, 'float':float, 'bool': bool}[type_map[self.enum_data_type]]
return type_fn(self.enum_value)
else:
return getattr(self, str(self.get_value_name()), None)
def get_value_default(self): def get_value_default(self):
if self.data_type == "string": if self.data_type == "string":
@@ -41,7 +41,10 @@ class Usecase:
elif isinstance(value, ifcopenshell.entity_instance): elif isinstance(value, ifcopenshell.entity_instance):
prop.NominalValue = value prop.NominalValue = value
else: else:
primary_measure_type = self.get_primary_measure_type(prop.Name, old_value=prop.NominalValue, new_value=value) primary_measure_type = self.get_primary_measure_type(
prop.Name, old_value=prop.NominalValue, new_value=value
)
value = self.cast_value_to_primary_measure_type(value, primary_measure_type)
prop.NominalValue = self.file.create_entity(primary_measure_type, value) prop.NominalValue = self.file.create_entity(primary_measure_type, value)
del self.settings["properties"][prop.Name] del self.settings["properties"][prop.Name]
@@ -50,13 +53,16 @@ class Usecase:
for name, value in self.settings["properties"].items(): for name, value in self.settings["properties"].items():
if value is None: if value is None:
continue continue
primary_measure_type = self.get_primary_measure_type(name, new_value=value) if isinstance(value, ifcopenshell.entity_instance):
if hasattr(value, "is_a"): nominal_value = value
value = value.wrappedValue else:
primary_measure_type = self.get_primary_measure_type(name, new_value=value)
value = self.cast_value_to_primary_measure_type(value, primary_measure_type)
nominal_value = self.file.create_entity(primary_measure_type, value)
properties.append( properties.append(
self.file.create_entity( self.file.create_entity(
"IfcPropertySingleValue", "IfcPropertySingleValue",
**{"Name": name, "NominalValue": self.file.create_entity(primary_measure_type, value)}, **{"Name": name, "NominalValue": nominal_value},
) )
) )
return properties return properties
@@ -94,3 +100,22 @@ class Usecase:
return "IfcBoolean" return "IfcBoolean"
elif isinstance(new_value, int): elif isinstance(new_value, int):
return "IfcInteger" return "IfcInteger"
def cast_value_to_primary_measure_type(self, value, primary_measure_type):
type_str = self.file.create_entity(primary_measure_type).attribute_type(0)
type_fn = {
"AGGREGATE OF DOUBLE": list,
"AGGREGATE OF INT": list,
"AGGREGATE OF ENTITY INSTANCE": list,
"BINARY": bytes,
"LOGICAL": str,
"BOOL": bool,
"INT": int,
"DOUBLE": float,
"STRING": str,
}[type_str]
if type_str == "AGGREGATE OF DOUBLE":
return [float(i) for i in value]
elif type_str == "AGGREGATE OF INT":
return [int(i) for i in value]
return type_fn(value)
@@ -49,6 +49,22 @@ class TestEditPset(test.bootstrap.IFC4):
assert pset.HasProperties[1].Name == "Status" assert pset.HasProperties[1].Name == "Status"
assert pset.HasProperties[1].NominalValue is None assert pset.HasProperties[1].NominalValue is None
def test_editing_a_templated_pset_with_automatic_casting_of_primitive_data_types(self):
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
pset = ifcopenshell.api.run("pset.add_pset", self.file, product=element, name="Pset_WallCommon")
ifcopenshell.api.run(
"pset.edit_pset",
self.file,
pset=pset,
properties={"ThermalTransmittance": "42"},
)
ifcopenshell.api.run("pset.edit_pset", self.file, pset=pset, properties={"Reference": "bar", "Status": None})
pset = element.IsDefinedBy[0].RelatingPropertyDefinition
assert pset.HasProperties[0].Name == "ThermalTransmittance"
assert pset.HasProperties[0].NominalValue.is_a("IfcThermalTransmittanceMeasure")
assert pset.HasProperties[0].NominalValue.wrappedValue == 42
def test_not_adding_a_property_if_it_is_none(self): def test_not_adding_a_property_if_it_is_none(self):
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall") element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
pset = ifcopenshell.api.run("pset.add_pset", self.file, product=element, name="Pset_WallCommon") pset = ifcopenshell.api.run("pset.add_pset", self.file, product=element, name="Pset_WallCommon")