Refactor cost operator to use helpers

This commit is contained in:
Dion Moult
2021-05-07 15:10:45 +10:00
parent 7919c193b8
commit 15927dd501
@@ -2,6 +2,7 @@ import os
import bpy import bpy
import json import json
import ifcopenshell.api import ifcopenshell.api
import blenderbim.bim.helper
from blenderbim.bim.ifc import IfcStore from blenderbim.bim.ifc import IfcStore
from ifcopenshell.api.cost.data import Data from ifcopenshell.api.cost.data import Data
@@ -22,15 +23,7 @@ class EditCostSchedule(bpy.types.Operator):
def execute(self, context): def execute(self, context):
props = context.scene.BIMCostProperties props = context.scene.BIMCostProperties
attributes = {} attributes = blenderbim.bim.helper.export_attributes(props.cost_schedule_attributes)
for attribute in props.cost_schedule_attributes:
if attribute.is_null:
attributes[attribute.name] = None
else:
if attribute.data_type == "string":
attributes[attribute.name] = attribute.string_value
elif attribute.data_type == "enum":
attributes[attribute.name] = attribute.enum_value
self.file = IfcStore.get_file() self.file = IfcStore.get_file()
ifcopenshell.api.run( ifcopenshell.api.run(
"cost.edit_cost_schedule", "cost.edit_cost_schedule",
@@ -73,24 +66,14 @@ class EnableEditingCostSchedule(bpy.types.Operator):
def enable_editing_cost_schedule(self): def enable_editing_cost_schedule(self):
data = Data.cost_schedules[self.cost_schedule] data = Data.cost_schedules[self.cost_schedule]
blenderbim.bim.helper.import_attributes(
"IfcCostSchedule", self.props.cost_schedule_attributes, data, self.import_attributes
)
for attribute in IfcStore.get_schema().declaration_by_name("IfcCostSchedule").all_attributes(): def import_attributes(self, name, prop, data):
data_type = ifcopenshell.util.attribute.get_primitive_type(attribute) if name in ["SubmittedOn", "UpdateDate"]:
if data_type == "entity": prop.string_value = "" if prop.is_null else data[name].isoformat()
continue return True
new = self.props.cost_schedule_attributes.add()
new.name = attribute.name()
new.is_null = data[attribute.name()] is None
new.is_optional = attribute.optional()
new.data_type = data_type
if attribute.name() in ["SubmittedOn", "UpdateDate"]:
new.string_value = "" if new.is_null else data[attribute.name()].isoformat()
elif data_type == "string":
new.string_value = "" if new.is_null else data[attribute.name()]
elif data_type == "enum":
new.enum_items = json.dumps(ifcopenshell.util.attribute.get_enum_items(attribute))
if data[attribute.name()]:
new.enum_value = data[attribute.name()]
class EnableEditingCostItems(bpy.types.Operator): class EnableEditingCostItems(bpy.types.Operator):
@@ -242,22 +225,7 @@ class EnableEditingCostItem(bpy.types.Operator):
props.cost_item_attributes.remove(0) props.cost_item_attributes.remove(0)
data = Data.cost_items[self.cost_item] data = Data.cost_items[self.cost_item]
blenderbim.bim.helper.import_attributes("IfcCostItem", props.cost_item_attributes, data)
for attribute in IfcStore.get_schema().declaration_by_name("IfcCostItem").all_attributes():
data_type = ifcopenshell.util.attribute.get_primitive_type(attribute)
if data_type == "entity" or isinstance(data_type, tuple):
continue
new = props.cost_item_attributes.add()
new.name = attribute.name()
new.is_null = data[attribute.name()] is None
new.is_optional = attribute.optional()
new.data_type = data_type
if data_type == "string":
new.string_value = "" if new.is_null else data[attribute.name()]
elif data_type == "enum":
new.enum_items = json.dumps(ifcopenshell.util.attribute.get_enum_items(attribute))
if data[attribute.name()]:
new.enum_value = data[attribute.name()]
props.active_cost_item_id = self.cost_item props.active_cost_item_id = self.cost_item
props.cost_item_editing_type = "ATTRIBUTES" props.cost_item_editing_type = "ATTRIBUTES"
return {"FINISHED"} return {"FINISHED"}
@@ -278,19 +246,7 @@ class EditCostItem(bpy.types.Operator):
def execute(self, context): def execute(self, context):
props = context.scene.BIMCostProperties props = context.scene.BIMCostProperties
attributes = {} attributes = blenderbim.bim.helper.export_attributes(props.cost_item_attributes)
for attribute in props.cost_item_attributes:
if attribute.is_null:
attributes[attribute.name] = None
else:
if attribute.data_type == "string":
attributes[attribute.name] = attribute.string_value
elif attribute.data_type == "boolean":
attributes[attribute.name] = attribute.bool_value
elif attribute.data_type == "integer":
attributes[attribute.name] = attribute.int_value
elif attribute.data_type == "enum":
attributes[attribute.name] = attribute.enum_value
self.file = IfcStore.get_file() self.file = IfcStore.get_file()
ifcopenshell.api.run( ifcopenshell.api.run(
"cost.edit_cost_item", "cost.edit_cost_item",
@@ -434,22 +390,7 @@ class EnableEditingCostItemQuantity(bpy.types.Operator):
self.props.quantity_attributes.remove(0) self.props.quantity_attributes.remove(0)
self.props.active_cost_item_quantity_id = self.physical_quantity self.props.active_cost_item_quantity_id = self.physical_quantity
data = Data.physical_quantities[self.physical_quantity] data = Data.physical_quantities[self.physical_quantity]
blenderbim.bim.helper.import_attributes(data["type"], self.props.quantity_attributes, data)
for attribute in IfcStore.get_schema().declaration_by_name(data["type"]).all_attributes():
data_type = ifcopenshell.util.attribute.get_primitive_type(attribute)
if data_type == "entity":
continue
new = self.props.quantity_attributes.add()
new.name = attribute.name()
new.is_null = data[attribute.name()] is None
new.is_optional = attribute.optional()
new.data_type = data_type
if data_type == "string":
new.string_value = "" if new.is_null else data[attribute.name()]
elif data_type == "float":
new.float_value = 0.0 if new.is_null else data[attribute.name()]
elif data_type == "integer":
new.int_value = 0 if new.is_null else data[attribute.name()]
return {"FINISHED"} return {"FINISHED"}
@@ -470,17 +411,7 @@ class EditCostItemQuantity(bpy.types.Operator):
def execute(self, context): def execute(self, context):
props = context.scene.BIMCostProperties props = context.scene.BIMCostProperties
attributes = {} attributes = blenderbim.bim.helper.export_attributes(props.quantity_attributes)
for attribute in props.quantity_attributes:
if attribute.is_null:
attributes[attribute.name] = None
else:
if attribute.data_type == "string":
attributes[attribute.name] = attribute.string_value
if attribute.data_type == "float":
attributes[attribute.name] = attribute.float_value
if attribute.data_type == "integer":
attributes[attribute.name] = attribute.int_value
self.file = IfcStore.get_file() self.file = IfcStore.get_file()
ifcopenshell.api.run( ifcopenshell.api.run(
"cost.edit_cost_item_quantity", "cost.edit_cost_item_quantity",
@@ -543,31 +474,18 @@ class EnableEditingCostItemValue(bpy.types.Operator):
self.props.active_cost_item_value_id = self.cost_value self.props.active_cost_item_value_id = self.cost_value
data = Data.cost_values[self.cost_value] data = Data.cost_values[self.cost_value]
for attribute in IfcStore.get_schema().declaration_by_name(data["type"]).all_attributes(): blenderbim.bim.helper.import_attributes(
data_type = ifcopenshell.util.attribute.get_primitive_type(attribute) data["type"], self.props.cost_value_attributes, data, self.import_attributes
if data_type == "entity" or isinstance(data_type, tuple): )
continue
new = self.props.cost_value_attributes.add()
new.name = attribute.name()
new.is_null = data[attribute.name()] is None
new.is_optional = attribute.optional()
new.data_type = data_type
if attribute.name() == "AppliedValue":
# TODO: for now, only support simple values
new.data_type = "float"
new.float_value = 0.0 if new.is_null else data[attribute.name()]
if data_type == "string":
new.string_value = "" if new.is_null else data[attribute.name()]
elif data_type == "float":
new.float_value = 0.0 if new.is_null else data[attribute.name()]
elif data_type == "integer":
new.int_value = 0 if new.is_null else data[attribute.name()]
elif data_type == "enum":
new.enum_items = json.dumps(ifcopenshell.util.attribute.get_enum_items(attribute))
if data[attribute.name()]:
new.enum_value = data[attribute.name()]
return {"FINISHED"} return {"FINISHED"}
def import_attributes(self, name, prop, data):
if name == "AppliedValue":
# TODO: for now, only support simple values
prop.data_type = "float"
prop.float_value = 0.0 if prop.is_null else data[name]
return True
class DisableEditingCostItemValue(bpy.types.Operator): class DisableEditingCostItemValue(bpy.types.Operator):
bl_idname = "bim.disable_editing_cost_item_value" bl_idname = "bim.disable_editing_cost_item_value"
@@ -586,17 +504,7 @@ class EditCostItemValue(bpy.types.Operator):
def execute(self, context): def execute(self, context):
props = context.scene.BIMCostProperties props = context.scene.BIMCostProperties
attributes = {} attributes = blenderbim.bim.helper.export_attributes(props.cost_value_attributes)
for attribute in props.cost_value_attributes:
if attribute.is_null:
attributes[attribute.name] = None
else:
if attribute.data_type == "string":
attributes[attribute.name] = attribute.string_value
if attribute.data_type == "float":
attributes[attribute.name] = attribute.float_value
if attribute.data_type == "integer":
attributes[attribute.name] = attribute.int_value
self.file = IfcStore.get_file() self.file = IfcStore.get_file()
ifcopenshell.api.run( ifcopenshell.api.run(
"cost.edit_cost_item_value", "cost.edit_cost_item_value",