Merge remote-tracking branch 'origin/v0.6.0' into v0.7.0

This commit is contained in:
Thomas Krijnen
2021-08-23 13:13:40 +02:00
330 changed files with 9637 additions and 2189 deletions
@@ -0,0 +1,21 @@
import ifcopenshell
class Usecase:
def __init__(self, file, **settings):
self.file = file
self.settings = {
"objective": None,
}
for key, value in settings.items():
self.settings[key] = value
def execute(self):
metric = self.file.create_entity("IfcMetric", **{
"Name": "Unnamed",
"ConstraintGrade": "NOTDEFINED",
"Benchmark": "EQUALTO",
})
if self.settings["objective"]:
benchmark_values = list(self.settings["objective"].BenchmarkValues or [])
benchmark_values.append(metric)
return metric
@@ -16,6 +16,7 @@ class Usecase:
related_objects = set(rel.RelatedObjects) if rel.RelatedObjects else set()
related_objects.add(self.settings["product"])
rel.RelatedObjects = list(related_objects)
return rel
def get_constraint_rel(self):
for rel in self.file.by_type("IfcRelAssociatesConstraint"):
@@ -7,12 +7,17 @@ class Data:
is_loaded = False
products = {}
objectives = {}
metrics = {}
references = {}
@classmethod
def purge(cls):
cls.is_loaded = False
cls.products = {}
cls.objectives = {}
cls.metrics ={}
cls.references = {}
@classmethod
def load(cls, file, product_id=None):
@@ -22,6 +27,8 @@ class Data:
if product_id:
return cls.load_product(product_id)
cls.load_objectives()
cls.load_metrics()
cls.load_references()
cls.is_loaded = True
@classmethod
@@ -41,8 +48,41 @@ class Data:
cls.objectives = {}
for constraint in cls._file.by_type("IfcObjective"):
data = constraint.get_info()
for key, value in data.items():
if not value:
continue
if cls._file.schema == "IFC2X3":
for attribute in ["CreationTime"]:
if data[attribute]:
data[attribute] = ifcopenshell.util.date.ifc2datetime(data[attribute]).isoformat()
data["BenchmarkValues"] = [metric.id() for metric in constraint.BenchmarkValues or []]
cls.objectives[constraint.id()] = data
@classmethod
def load_metrics(cls):
cls.metrics = {}
for metric in cls._file.by_type("IfcMetric"):
data = metric.get_info()
for key, value in data.items():
if not value:
continue
data["ConstrainedObjects"] = []
for association in cls._file.by_type("IfcRelAssociatesConstraint"):
if association.RelatingConstraint.id() == metric.id():
data["ConstrainedObjects"] = [o.id() for o in association.RelatedObjects or []]
if metric.DataValue:
data["DataValue"] = data["DataValue"].id()
if metric.ReferencePath:
data["ReferencePath"] = data["ReferencePath"].id()
cls.metrics[metric.id()] = data
@classmethod
def load_references(cls):
cls.references = {}
for reference in cls._file.by_type("IfcReference"):
data = reference.get_info()
for key, value in data.items():
if not value:
continue
cls.references[refenrece.id()] = data
@@ -0,0 +1,13 @@
class Usecase:
def __init__(self, file, **settings):
self.file = file
self.settings = {
"metric": None,
"attributes": {}
}
for key, value in settings.items():
self.settings[key] = value
def execute(self):
for name, value in self.settings["attributes"].items():
setattr(self.settings["metric"], name, value)
@@ -0,0 +1,15 @@
class Usecase:
def __init__(self, file, **settings):
self.file = file
self.settings = {"metric": None}
for key, value in settings.items():
self.settings[key] = value
def execute(self):
self.file.remove(self.settings["metric"])
for rel in self.file.by_type("IfcRelAssociatesConstraint"):
if not rel.RelatingConstraint:
self.file.remove(rel)
for resource_rel in self.file.by_type("IfcResourceConstraintRelationship"):
if not resource_rel.RelatingConstraint:
self.file.remove(resource_rel)
@@ -0,0 +1,17 @@
import ifcopenshell.api
class Usecase:
def __init__(self, file, **settings):
self.file = file
self.settings = {"cost_item": None, "cost_rate": None}
for key, value in settings.items():
self.settings[key] = value
def execute(self):
for cost_value in self.settings["cost_item"].CostValues:
ifcopenshell.api.run(
"cost.remove_cost_item_value", self.file, parent=self.settings["cost_item"], cost_value=cost_value
)
# This is an assumption, and not part of the official IFC documentation
self.settings["cost_item"].CostValues = self.settings["cost_rate"].CostValues
@@ -107,23 +107,35 @@ class Data:
@classmethod
def load_cost_item_values(cls, cost_item, data):
data["CostValues"] = []
data["TotalCostValue"] = 0.0
data["TotalAppliedValue"] = 0.0
data["CategoryValues"] = {}
data["UnitBasisValueComponent"] = None
data["UnitBasisUnitSymbol"] = None
data["TotalAppliedValue"] = 0.0
data["TotalCost"] = 0.0
for cost_value in cost_item.CostValues or []:
cls.load_cost_item_value(data, cost_item, cost_value)
cls.load_cost_item_value(cost_item, data, cost_value)
data["CostValues"].append(cost_value.id())
data["TotalAppliedValue"] += cls.cost_values[cost_value.id()]["AppliedValue"]
data["TotalCostValue"] = data["TotalCostQuantity"] * data["TotalAppliedValue"]
if cost_value.UnitBasis:
cost_value_data = cls.cost_values[cost_value.id()]
data["UnitBasisValueComponent"] = cost_value_data["UnitBasis"]["ValueComponent"]
data["UnitBasisUnitSymbol"] = cost_value_data["UnitBasis"]["UnitSymbol"]
if data["UnitBasisValueComponent"]:
data["TotalCost"] = (
data["TotalCostQuantity"] / data["UnitBasisValueComponent"] * data["TotalAppliedValue"]
)
else:
data["TotalCost"] = data["TotalCostQuantity"] * data["TotalAppliedValue"]
@classmethod
def load_cost_item_value(cls, cost_item_data, cost_item, cost_value):
def load_cost_item_value(cls, cost_item, cost_item_data, cost_value):
value_data = cost_value.get_info()
del value_data["AppliedValue"]
if value_data["UnitBasis"]:
data = cost_value.UnitBasis.get_info()
data["ValueComponent"] = data["ValueComponent"].wrappedValue
data["UnitComponent"] = data["UnitComponent"].id()
data["UnitSymbol"] = ifcopenshell.util.unit.get_unit_symbol(cost_value.UnitBasis.UnitComponent)
value_data["UnitBasis"] = data
if value_data["ApplicableDate"]:
value_data["ApplicableDate"] = ifcopenshell.util.date.ifc2datetime(value_data["ApplicableDate"])
@@ -138,7 +150,7 @@ class Data:
cls.cost_values[cost_value.id()] = value_data
for component in cost_value.Components or []:
cls.load_cost_item_value(cost_item_data, cost_item, component)
cls.load_cost_item_value(cost_item, cost_item_data, component)
@classmethod
def calculate_applied_value(cls, cost_item, cost_value, category_filter=None):
@@ -186,7 +198,11 @@ class Data:
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
if child_cost_value.UnitBasis:
value_component = child_cost_value.UnitBasis.ValueComponent.wrappedValue
result += child_quantity / value_component * child_applied_value
else:
result += child_quantity * child_applied_value
return result
@classmethod
@@ -16,18 +16,13 @@ class Usecase:
# TODO: support all applied value select types
value = self.file.createIfcMonetaryMeasure(value)
elif name == "UnitBasis":
self.remove_existing_unit_basis()
old_unit_basis = self.settings["cost_value"].UnitBasis
if value:
value_component = self.file.create_entity(
ifcopenshell.util.unit.get_unit_measure_type(value["UnitComponent"].UnitType),
ifcopenshell.util.unit.get_unit_measure_class(value["UnitComponent"].UnitType),
value["ValueComponent"],
)
value = self.file.create_entity("IfcMeasureWithUnit", value_component, value["UnitComponent"])
if old_unit_basis and len(self.file.get_inverse(old_unit_basis)) == 0:
ifcopenshell.util.element.remove_deep(self.file, old_unit_basis)
setattr(self.settings["cost_value"], name, value)
def remove_existing_unit_basis(self):
if (
self.settings["cost_value"].UnitBasis
and len(self.file.get_inverse(self.settings["cost_value"].UnitBasis)) == 1
):
ifcopenshell.util.element.remove_deep(self.file, self.settings["cost_value"].UnitBasis)
@@ -1,9 +1,19 @@
class Usecase:
def __init__(self, file, **settings):
self.file = file
self.settings = {"cost_value": None}
self.settings = {"parent": None, "cost_value": None}
for key, value in settings.items():
self.settings[key] = value
def execute(self):
self.file.remove(self.settings["cost_value"])
if len(self.file.get_inverse(self.settings["cost_value"])) == 1:
self.file.remove(self.settings["cost_value"])
# TODO deep purge
elif self.settings["parent"].is_a("IfcCostItem"):
values = list(self.settings["parent"].CostValues)
values.remove(self.settings["cost_value"])
self.settings["parent"].CostValues = values if values else None
elif self.settings["parent"].is_a("IfcCostValue"):
components = list(self.settings["parent"].Components)
components.remove(self.settings["cost_value"])
self.settings["parent"].Components = components if components else None
@@ -34,4 +34,9 @@ class Usecase:
ifcopenshell.api.run("grid.remove_grid_axis", self.file, axis=axis)
# TODO: remove object placement and other relationships
for inverse in self.file.get_inverse(self.settings["product"]):
if inverse.is_a("IfcRelFillsElement"):
self.file.remove(inverse)
elif inverse.is_a("IfcRelVoidsElement"):
self.file.remove(inverse)
self.file.remove(self.settings["product"])
@@ -63,7 +63,7 @@ class Usecase:
def get_styled_representation(self, definition_representation):
representations = [
r
for r in definition_representation.Representations is r.is_a("IfcStyledRepresentation")
for r in definition_representation.Representations if r.is_a("IfcStyledRepresentation")
and r.ContextOfItems == self.settings["context"]
]
if representations:
@@ -0,0 +1,14 @@
class Usecase:
def __init__(self, file, **settings):
self.file = file
self.settings = {"unit_type": "USERDEFINED", "name": "THINGAMAJIG", "dimensions": (0, 0, 0, 0, 0, 0, 0)}
for key, value in settings.items():
self.settings[key] = value
def execute(self):
return self.file.create_entity(
"IfcContextDependentUnit",
Dimensions=self.file.createIfcDimensionalExponents(*self.settings["dimensions"]),
UnitType=self.settings["unit_type"],
Name=self.settings["name"],
)
@@ -7,4 +7,12 @@ class Usecase:
def execute(self):
for name, value in self.settings["attributes"].items():
if name == "Dimensions":
dimensions = self.settings["unit"].Dimensions
if len(self.file.get_inverse(dimensions)) > 1:
self.settings["unit"].Dimensions = self.file.createIfcDimensionalExponents(*value)
else:
for i, exponent in enumerate(value):
dimensions[i] = exponent
continue
setattr(self.settings["unit"], name, value)
@@ -1,3 +1,4 @@
import ifcopenshell.util.unit
import ifcopenshell.util.element
@@ -9,10 +10,12 @@ class Usecase():
self.settings[key] = value
def execute(self):
unit_assignment = self.file.by_type("IfcUnitAssignment")[0]
units = list(unit_assignment.Units)
units.remove(self.settings["unit"])
if not units:
return
unit_assignment.Units = units
unit_assignment = ifcopenshell.util.unit.get_unit_assignment(self.file)
if unit_assignment and self.settings["unit"] in unit_assignment.Units:
units = list(unit_assignment.Units)
units.remove(self.settings["unit"])
if units:
unit_assignment.Units = units
else:
self.file.remove(unit_assignment)
ifcopenshell.util.element.remove_deep(self.file, self.settings["unit"])
@@ -3,10 +3,16 @@ def get_primitive_type(attribute_or_data_type):
data_type = str(attribute_or_data_type.type_of_attribute())
else:
data_type = str(attribute_or_data_type)
if "<select" in data_type:
return "select"
elif "<list" in data_type:
return ("list", get_primitive_type(data_type.replace("<list", "")))
if data_type.find("<type") == 0:
return get_primitive_type(data_type[data_type[1:].find("<")+1:])
elif data_type.find("<list") == 0:
return ("list", get_primitive_type(data_type[data_type[1:].find("<")+1:]))
elif data_type.find("<set") == 0:
return ("set", get_primitive_type(data_type[data_type[1:].find("<")+1:]))
elif data_type.find("<select") == 0:
select_definition = data_type[data_type.find("(")+1:data_type.find(")")].split("|")
select_types = [get_primitive_type(d.strip()) for d in select_definition]
return ("select", tuple(select_types))
elif "<entity" in data_type:
return "entity"
elif "<string>" in data_type:
@@ -124,11 +124,11 @@ class Selector:
def get_class_selector(self, class_selector):
if class_selector.children[0] == "COBie":
ifcopenshell.util.fm.get_cobie_components(self.file)
elements = ifcopenshell.util.fm.get_cobie_components(self.file)
elif class_selector.children[0] == "COBieType":
ifcopenshell.util.fm.get_cobie_types(self.file)
elements = ifcopenshell.util.fm.get_cobie_types(self.file)
elif class_selector.children[0] == "FMHEM":
ifcopenshell.util.fm.get_fmhem_types(self.file)
elements = ifcopenshell.util.fm.get_fmhem_types(self.file)
else:
elements = self.file.by_type(class_selector.children[0])
if len(class_selector.children) > 1 and class_selector.children[1].data == "filter":
@@ -119,33 +119,34 @@ si_type_names = {
"VOLUMEUNIT": "CUBIC_METRE",
}
# Are you good at physics? Want to help fill these in? :)
# See IfcDimensionalExponents:
# (Length, Mass, Time, ElectricCurrent, ThermodynamicTemperature, AmountOfSubstance, LuminousIntensity)
named_dimensions = {
# "ABSORBEDDOSEUNIT": (0, 0, 0, 0, 0, 0, 0),
"ABSORBEDDOSEUNIT": (2, 0, -2, 0, 0, 0, 0),
"AMOUNTOFSUBSTANCEUNIT": (0, 0, 0, 0, 0, 1, 0),
"AREAUNIT": (2, 0, 0, 0, 0, 0, 0),
# "DOSEEQUIVALENTUNIT": (0, 0, 0, 0, 0, 0, 0),
# "ELECTRICCAPACITANCEUNIT": (0, 0, 0, 0, 0, 0, 0),
# "ELECTRICCHARGEUNIT": (0, 0, 0, 0, 0, 0, 0),
# "ELECTRICCONDUCTANCEUNIT": (0, 0, 0, 0, 0, 0, 0),
"DOSEEQUIVALENTUNIT": (2, 0, -2, 0, 0, 0, 0),
"ELECTRICCAPACITANCEUNIT": (-2, -1, 4, 2, 0, 0, 0),
"ELECTRICCHARGEUNIT": (0, 0, 1, 1, 0, 0, 0),
"ELECTRICCONDUCTANCEUNIT": (-2, -1, 3, 2, 0, 0, 0),
"ELECTRICCURRENTUNIT": (0, 0, 0, 1, 0, 0, 0),
# "ELECTRICRESISTANCEUNIT": (0, 0, 0, 0, 0, 0, 0),
# "ELECTRICVOLTAGEUNIT": (0, 0, 0, 0, 0, 0, 0),
"ELECTRICRESISTANCEUNIT": (2, 1, -3, -2, 0, 0, 0),
"ELECTRICVOLTAGEUNIT": (2, 1, -3, -1, 0, 0, 0),
"ENERGYUNIT": (2, 1, -2, 0, 0, 0, 0),
"FORCEUNIT": (1, 1, -2, 0, 0, 0, 0),
"FREQUENCYUNIT": (0, 0, -1, 0, 0, 0, 0),
# "ILLUMINANCEUNIT": (0, 0, 0, 0, 0, 0, 0),
# "INDUCTANCEUNIT": (0, 0, 0, 0, 0, 0, 0),
"ILLUMINANCEUNIT": (-2, 0, 0, 0, 0, 1, 1),
"INDUCTANCEUNIT": (2, 1, -2, -2, 0, 0, 0),
"LENGTHUNIT": (1, 0, 0, 0, 0, 0, 0),
# "LUMINOUSFLUXUNIT": (0, 0, 0, 0, 0, 0, 0),
"LUMINOUSFLUXUNIT": (0, 0, 0, 0, 0, 1, 1),
"LUMINOUSINTENSITYUNIT": (0, 0, 0, 0, 0, 0, 1),
# "MAGNETICFLUXDENSITYUNIT": (0, 0, 0, 0, 0, 0, 0),
# "MAGNETICFLUXUNIT": (0, 0, 0, 0, 0, 0, 0),
"MAGNETICFLUXDENSITYUNIT": (0, 1, -2, -1, 0, 0, 0),
"MAGNETICFLUXUNIT": (2, 1, -2, -1, 0, 0, 0),
"MASSUNIT": (0, 1, 0, 0, 0, 0, 0),
"PLANEANGLEUNIT": (0, 0, 0, 0, 0, 0, 0),
"POWERUNIT": (2, 1, -3, 0, 0, 0, 0),
"PRESSUREUNIT": (-1, 1, -2, 0, 0, 0, 0),
# "RADIOACTIVITYUNIT": (0, 0, 0, 0, 0, 0, 0),
"RADIOACTIVITYUNIT": (0, 0, -1, 0, 0, 0, 0),
"SOLIDANGLEUNIT": (0, 0, 0, 0, 0, 0, 0),
"THERMODYNAMICTEMPERATUREUNIT": (0, 0, 0, 0, 1, 0, 0),
"TIMEUNIT": (0, 0, 1, 0, 0, 0, 0),
@@ -246,45 +247,79 @@ def get_named_dimensions(name):
return named_dimensions.get(name, (0, 0, 0, 0, 0, 0, 0))
def get_unit_assignment(ifc_file):
unit_assignments = ifc_file.by_type("IfcUnitAssignment")
if unit_assignments:
return unit_assignments[0]
def get_property_unit(prop, ifc_file):
unit = getattr(prop, "Unit", None)
if unit:
return unit
unit_assignment = ifc_file.by_type("IfcUnitAssignment")
unit_assignment = get_unit_assignment(ifc_file)
if not unit_assignment:
return
entity = prop.wrapped_data.declaration().as_entity()
if prop.is_a("IfcPhysicalSimpleQuantity"):
measure_type = entity.attribute_by_index(3).type_of_attribute().declared_type().name()
measure_class = entity.attribute_by_index(3).type_of_attribute().declared_type().name()
elif prop.is_a("IfcPropertySingleValue") and prop.NominalValue:
measure_type = prop.NominalValue.is_a()
for text in ("Ifc", "Measure", "Non", "Positive", "Negative"):
measure_type = measure_type.replace(text, "")
measure_type = measure_type.upper() + "UNIT"
units = [u for u in unit_assignment[0].Units if getattr(u, "UnitType", None) == measure_type]
measure_class = prop.NominalValue.is_a()
unit_type = get_measure_unit_type(measure_class)
units = [u for u in unit_assignment.Units if getattr(u, "UnitType", None) == unit_type]
if units:
return units[0]
def get_unit_measure_type(unit_type):
def get_unit_measure_class(unit_type):
if unit_type == "USERDEFINED":
# See https://github.com/buildingSMART/IFC4.3.x-development/issues/71
return "IfcNumericMeasure"
return "Ifc" + unit_type[0:-4].lower().capitalize() + "Measure"
def get_measure_unit_type(measure_class):
if measure_class == "IfcNumericMeasure":
# See https://github.com/buildingSMART/IFC4.3.x-development/issues/71
return "USERDEFINED"
for text in ("Ifc", "Measure", "Non", "Positive", "Negative"):
measure_class = measure_class.replace(text, "")
return measure_class.upper() + "UNIT"
def get_symbol_measure_class(symbol):
# Dumb, but everybody gets it, unlike regex golf
if not symbol:
return "IfcNumericMeasure"
symbol = symbol.lower()
if symbol in ["km", "m", "cm", "mm", "ly", "lf", "lin", "yd", "ft", "in"]:
return "IfcLengthMeasure"
elif symbol in ["km2", "m2", "cm2", "mm2", "sqy", "sqft", "sqin"]:
return "IfcAreaMeasure"
elif symbol in ["km3", "m3", "cm3", "mm3", "cy", "cft", "cin"]:
return "IfcVolumeMeasure"
elif symbol in ["kg", "g", "mt", "kt", "t"]:
return "IfcMassMeasure"
elif symbol in ["day", "d", "hour", "hr", "h", "minute", "min", "m", "second", "sec", "s"]:
return "IfcTimeMeasure"
return "IfcNumericMeasure"
def get_symbol_quantity_class(symbol):
# Dumb, but everybody gets it, unlike regex golf
if not symbol:
return "IfcQuantityCount"
symbol = symbol.lower()
if symbol in ["kg", "g", "mt", "kt", "t"]:
if symbol in ["km", "m", "cm", "mm", "ly", "lf", "lin", "yd", "ft", "in"]:
return "IfcQuantityLength"
elif symbol in ["km2", "m2", "cm2", "mm2", "sqy", "sqft", "sqin"]:
return "IfcQuantityArea"
elif symbol in ["km3", "m3", "cm3", "mm3", "cy", "cft", "cin"]:
return "IfcQuantityVolume"
elif symbol in ["kg", "g", "mt", "kt", "t"]:
return "IfcQuantityWeight"
elif symbol in ["day", "d", "hour", "hr", "h", "minute", "min", "m", "second", "sec", "s"]:
return "IfcQuantityTime"
elif symbol in ["km3", "m3", "cm3", "mm3", "cy", "cft", "cin"]:
return "IfcQuantityVolume"
elif symbol in ["km2", "m2", "cm2", "mm2", "sqy", "sqft", "sqin"]:
return "IfcQuantityArea"
elif symbol in ["km", "m", "cm", "mm", "ly", "lf", "lin", "yd", "ft", "in"]:
return "IfcQuantityLength"
return "IfcQuantityCount"