Units for cost items are now autodetected

This commit is contained in:
Dion Moult
2021-08-06 18:19:41 +10:00
parent 07bf60819a
commit e22c68a842
5 changed files with 48 additions and 5 deletions
@@ -1,4 +1,5 @@
import ifcopenshell.util.date
import ifcopenshell.util.unit
class Data:
@@ -63,6 +64,13 @@ class Data:
del quantity_data["Unit"]
cls.physical_quantities[quantity.id()] = quantity_data
data["CostQuantities"].append(quantity.id())
data["Unit"] = None
data["UnitSymbol"] = "?"
if cost_item.CostQuantities:
quantity = cost_item.CostQuantities[0]
unit = ifcopenshell.util.unit.get_property_unit(quantity, cls.file)
data["Unit"] = unit.id()
data["UnitSymbol"] = ifcopenshell.util.unit.get_unit_symbol(unit)
@classmethod
def load_cost_item_values(cls, cost_item, data):
@@ -52,6 +52,7 @@ unit_names = [
"WEBER",
]
si_dimensions = {
"METRE": (1, 0, 0, 0, 0, 0, 0),
"SQUARE_METRE": (2, 0, 0, 0, 0, 0, 0),
@@ -121,6 +122,33 @@ si_conversions = {
"btu": 1055.056,
}
prefix_symbols = {
"EXA": "E",
"PETA": "P",
"TERA": "T",
"GIGA": "G",
"MEGA": "M",
"KILO": "k",
"HECTO": "h",
"DECA": "da",
"DECI": "d",
"CENTI": "c",
"MILLI": "m",
"MICRO": "μ",
"NANO": "n",
"PICO": "p",
"FEMTO": "f",
"ATTO": "a",
}
unit_symbols = {
"CUBIC_METRE": "m3",
"GRAM": "g",
"SECOND": "s",
"SQUARE_METRE": "m2",
"METRE": "m",
}
def get_prefix(text):
if text:
@@ -169,6 +197,15 @@ def get_property_unit(prop, ifc_file):
return units[0]
def get_unit_symbol(unit):
if unit.is_a("IfcSIUnit"):
symbol = ""
symbol += prefix_symbols.get(unit.Prefix, "")
symbol += unit_symbols.get(unit.Name.replace("METER", "METRE"), "?")
return symbol
return "?"
def convert(value, from_prefix, from_unit, to_prefix, to_unit):
"""Converts between length, area, and volume units