Implement arithmetic formulas for cost value components

This commit is contained in:
Dion Moult
2021-05-08 12:18:27 +10:00
parent 46bc688f26
commit a80fbdbc1d
8 changed files with 99 additions and 53 deletions
@@ -1,16 +0,0 @@
import ifcopenshell.api
class Usecase:
def __init__(self, file, **settings):
self.file = file
self.settings = {"cost_item": None, "ifc_class": "IfcQuantityCount"}
for key, value in settings.items():
self.settings[key] = value
def execute(self):
value = self.file.create_entity("IfcCostValue")
values = list(self.settings["cost_item"].CostValues or [])
values.append(value)
self.settings["cost_item"].CostValues = values
return value
@@ -0,0 +1,18 @@
class Usecase:
def __init__(self, file, **settings):
self.file = file
self.settings = {"parent": None}
for key, value in settings.items():
self.settings[key] = value
def execute(self):
value = self.file.create_entity("IfcCostValue")
if self.settings["parent"].is_a("IfcCostItem"):
values = list(self.settings["parent"].CostValues or [])
values.append(value)
self.settings["parent"].CostValues = values
elif self.settings["parent"].is_a("IfcCostValue"):
values = list(self.settings["parent"].Components or [])
values.append(value)
self.settings["parent"].Components = values
return value
@@ -87,12 +87,31 @@ class Data:
value_data["Components"] = [c.id() for c in value_data["Components"] or []]
value_data["AppliedValue"] = cls.calculate_applied_value(cost_item, cost_value)
cls.cost_values[cost_value.id()] = value_data
for component in cost_value.Components or []:
cls.load_cost_item_value(cost_item, component)
@classmethod
def calculate_applied_value(cls, cost_item, cost_value, category_filter=None):
result = 0
if cost_value.ArithmeticOperator and cost_value.Components:
pass # TODO
component_values = []
for component in cost_value.Components:
component_values.append(cls.calculate_applied_value(cost_item, component, category_filter))
if cost_value.ArithmeticOperator == "ADD":
return sum(component_values)
result = component_values.pop(0)
if cost_value.ArithmeticOperator == "DIVIDE":
for value in component_values:
try:
result /= value
except ZeroDivisionError:
pass
elif cost_value.ArithmeticOperator == "MULTIPLY":
for value in component_values:
result *= value
elif cost_value.ArithmeticOperator == "SUBTRACT":
for value in component_values:
result -= value
return result
if cost_value.Category is None:
return cls.get_primitive_applied_value(cost_value.AppliedValue)
elif cost_value.Category == "*":
@@ -105,7 +124,7 @@ class Data:
return cls.sum_child_cost_items(cost_item, category_filter=cost_value.Category)
else:
return cls.get_primitive_applied_value(cost_value.AppliedValue)
return result
return 0
@classmethod
def sum_child_cost_items(cls, cost_item, category_filter=None):