Implement feature for manipulating cost values, with support for calculation of subtotals

This commit is contained in:
Dion Moult
2021-04-26 18:18:42 +10:00
parent bfcc067257
commit 389076bff4
9 changed files with 307 additions and 18 deletions
@@ -11,7 +11,6 @@ class Usecase:
def execute(self):
quantity = self.file.create_entity(self.settings["ifc_class"], Name="Unnamed")
quantity[3] = 0.0
cost_item = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcCostItem")
quantities = list(self.settings["cost_item"].CostQuantities or [])
quantities.append(quantity)
self.settings["cost_item"].CostQuantities = quantities
@@ -0,0 +1,16 @@
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
@@ -6,6 +6,7 @@ class Data:
cost_schedules = {}
cost_items = {}
physical_quantities = {}
cost_values = {}
@classmethod
def purge(cls):
@@ -13,49 +14,126 @@ class Data:
cls.cost_schedules = {}
cls.cost_items = {}
cls.physical_quantities = {}
cls.cost_values = {}
@classmethod
def load(cls, file):
cls.file = file
cls.cost_schedules = {}
cls.cost_items = {}
cls.physical_quantities = {}
cls.cost_values = {}
for cost_schedule in file.by_type("IfcCostSchedule"):
for cost_schedule in cls.file.by_type("IfcCostSchedule"):
data = cost_schedule.get_info()
del data["OwnerHistory"]
if data["SubmittedOn"]:
data["SubmittedOn"] = ifcopenshell.util.date.ifc2datetime(data["SubmittedOn"])
if data["UpdateDate"]:
data["UpdateDate"] = ifcopenshell.util.date.ifc2datetime(data["UpdateDate"])
data["RelatedObjects"] = []
data["Controls"] = []
for rel in cost_schedule.Controls:
for related_object in rel.RelatedObjects:
if related_object.is_a("IfcCostItem"):
data["RelatedObjects"].append(related_object.id())
break # We are only allowed one summary cost item
data["Controls"].append(related_object.id())
break # We are only allowed one summary cost item
cls.cost_schedules[cost_schedule.id()] = data
for cost_item in file.by_type("IfcCostItem"):
for cost_item in cls.file.by_type("IfcCostItem"):
data = cost_item.get_info()
del data["OwnerHistory"]
del data["CostValues"]
data["RelatedObjects"] = []
data["IsNestedBy"] = []
data["Controls"] = []
for rel in cost_item.IsNestedBy:
[data["RelatedObjects"].append(o.id()) for o in rel.RelatedObjects if o.is_a("IfcCostItem")]
[data["IsNestedBy"].append(o.id()) for o in rel.RelatedObjects if o.is_a("IfcCostItem")]
for rel in cost_item.Controls:
[data["Controls"].append(o.id()) for o in rel.RelatedObjects or []]
cls.cost_items[cost_item.id()] = data
cls.load_cost_item_quantities(cost_item, data)
cls.is_loaded=True
cls.load_cost_item_values(cost_item, data)
cls.is_loaded = True
@classmethod
def load_cost_item_quantities(cls, cost_item, data):
data["CostQuantities"] = []
data["TotalCostQuantities"] = 0.0
data["TotalCostQuantity"] = cls.get_total_quantity(cost_item)
for quantity in cost_item.CostQuantities or []:
quantity_data = quantity.get_info()
del quantity_data["Unit"]
cls.physical_quantities[quantity.id()] = quantity_data
data["CostQuantities"].append(quantity.id())
data["TotalCostQuantities"] += quantity[3]
@classmethod
def load_cost_item_values(cls, cost_item, data):
data["CostValues"] = []
data["TotalCostValue"] = 0.0
data["TotalAppliedValue"] = 0.0
for cost_value in cost_item.CostValues or []:
cls.load_cost_item_value(cost_item, cost_value)
data["CostValues"].append(cost_value.id())
print('applied value is', cls.cost_values[cost_value.id()]["AppliedValue"])
data["TotalAppliedValue"] += cls.cost_values[cost_value.id()]["AppliedValue"]
data["TotalCostValue"] = data["TotalCostQuantity"] * data["TotalAppliedValue"]
@classmethod
def load_cost_item_value(cls, cost_item, cost_value):
value_data = cost_value.get_info()
del value_data["AppliedValue"]
del value_data["UnitBasis"]
if value_data["ApplicableDate"]:
value_data["ApplicableDate"] = ifcopenshell.util.date.ifc2datetime(value_data["ApplicableDate"])
if value_data["FixedUntilDate"]:
value_data["FixedUntilDate"] = ifcopenshell.util.date.ifc2datetime(value_data["FixedUntilDate"])
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
@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
if cost_value.Category is None:
return cls.get_primitive_applied_value(cost_value.AppliedValue)
elif cost_value.Category == "*":
if cost_item.IsNestedBy:
return cls.sum_child_cost_items(cost_item)
else:
return cls.get_primitive_applied_value(cost_value.AppliedValue)
elif cost_value.Category:
if cost_item.IsNestedBy:
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
@classmethod
def sum_child_cost_items(cls, cost_item, category_filter=None):
result = 0
for rel in cost_item.IsNestedBy:
for child_cost_item in rel.RelatedObjects:
for child_cost_value in child_cost_item.CostValues or []:
if category_filter and child_cost_value.Category != category_filter:
continue
child_applied_value = cls.calculate_applied_value(child_cost_item, child_cost_value)
child_quantity = cls.get_total_quantity(child_cost_item)
result += child_applied_value * child_quantity
return result
@classmethod
def get_total_quantity(cls, cost_item):
return sum([q[3] for q in cost_item.CostQuantities or []]) or 1.0
@classmethod
def get_primitive_applied_value(cls, applied_value):
print('applied value is ', applied_value)
if not applied_value:
return 0.0
elif isinstance(applied_value, float):
return applied_value
elif hasattr(applied_value, "wrappedValue") and isinstance(applied_value.wrappedValue, float):
return applied_value.wrappedValue
elif applied_value.is_a("IfcMeasureWithUnit"):
return applied_value.ValueComponent
assert False, "Applied value {applied_value} not implemented"
@@ -0,0 +1,13 @@
class Usecase:
def __init__(self, file, **settings):
self.file = file
self.settings = {"cost_value": None, "attributes": {}}
for key, value in settings.items():
self.settings[key] = value
def execute(self):
for name, value in self.settings["attributes"].items():
if name == "AppliedValue" and value is not None:
# TODO: support all applied value select types
value = self.file.createIfcReal(value)
setattr(self.settings["cost_value"], name, value)
@@ -0,0 +1,9 @@
class Usecase:
def __init__(self, file, **settings):
self.file = file
self.settings = {"cost_value": None}
for key, value in settings.items():
self.settings[key] = value
def execute(self):
self.file.remove(self.settings["cost_value"])