mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-08-10 01:41:57 +00:00
You can now add and edit monetary units, and basic editing of other unit types.
This commit is contained in:
@@ -9,5 +9,5 @@ class Usecase:
|
||||
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)
|
||||
value = self.file.createIfcMonetaryMeasure(value)
|
||||
setattr(self.settings["cost_value"], name, value)
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
class Usecase:
|
||||
def __init__(self, file, **settings):
|
||||
self.file = file
|
||||
self.settings = {"currency": "DOLLARYDOO"}
|
||||
for key, value in settings.items():
|
||||
self.settings[key] = value
|
||||
|
||||
def execute(self):
|
||||
return self.file.create_entity("IfcMonetaryUnit", self.settings["currency"])
|
||||
@@ -1,44 +1,55 @@
|
||||
import ifcopenshell
|
||||
import ifcopenshell.util.unit
|
||||
|
||||
|
||||
class Usecase():
|
||||
class Usecase:
|
||||
def __init__(self, file, **settings):
|
||||
self.file = file
|
||||
self.settings = {
|
||||
"length": {
|
||||
"is_metric": True,
|
||||
"raw": "MILLIMETERS"
|
||||
},
|
||||
"area": {
|
||||
"is_metric": True,
|
||||
"raw": "METERS"
|
||||
},
|
||||
"volume": {
|
||||
"is_metric": True,
|
||||
"raw": "METERS"
|
||||
},
|
||||
"units": None,
|
||||
"length": {"is_metric": True, "raw": "MILLIMETERS"},
|
||||
"area": {"is_metric": True, "raw": "METERS"},
|
||||
"volume": {"is_metric": True, "raw": "METERS"},
|
||||
}
|
||||
for key, value in settings.items():
|
||||
self.settings[key] = value
|
||||
|
||||
def execute(self):
|
||||
for unit_type, data in self.settings.items():
|
||||
if data["is_metric"]:
|
||||
data["ifc"] = self.create_metric_unit(unit_type, data)
|
||||
else:
|
||||
data["ifc"] = self.create_imperial_unit(unit_type, data)
|
||||
# We're going to refactor this to split unit creation and assignment
|
||||
if self.settings["units"]:
|
||||
units = self.settings["units"]
|
||||
else:
|
||||
del self.settings["units"] # TODO refactor
|
||||
units = []
|
||||
for unit_type, data in self.settings.items():
|
||||
if data["is_metric"]:
|
||||
units.append(self.create_metric_unit(unit_type, data))
|
||||
else:
|
||||
units.append(self.create_imperial_unit(unit_type, data))
|
||||
|
||||
unit_assignment = self.get_unit_assignment()
|
||||
self.assign_units(unit_assignment, units)
|
||||
return unit_assignment
|
||||
|
||||
def get_unit_assignment(self):
|
||||
unit_assignment = self.file.by_type("IfcUnitAssignment")
|
||||
if unit_assignment:
|
||||
unit_assignment = unit_assignment[0]
|
||||
# TODO: handle unit rewriting, which is complicated
|
||||
else:
|
||||
unit_assignment = self.file.createIfcUnitAssignment([u["ifc"] for u in self.settings.values()])
|
||||
unit_assignment = self.file.createIfcUnitAssignment()
|
||||
if self.file.schema == "IFC2X3":
|
||||
self.file.by_type("IfcProject")[0].UnitsInContext = unit_assignment
|
||||
else:
|
||||
self.file.by_type("IfcContext")[0].UnitsInContext = unit_assignment
|
||||
return unit_assignment
|
||||
|
||||
def assign_units(self, unit_assignment, new_units):
|
||||
units = set(unit_assignment.Units or [])
|
||||
for unit in new_units:
|
||||
units.add(unit)
|
||||
unit_assignment.Units = list(units)
|
||||
|
||||
def create_metric_unit(self, unit_type, data):
|
||||
type_prefix = ""
|
||||
if unit_type == "area":
|
||||
@@ -72,7 +83,9 @@ class Usecase():
|
||||
name = "{}inch".format(name_prefix + " " if name_prefix else "")
|
||||
elif data["raw"] == "FEET":
|
||||
name = "{}foot".format(name_prefix + " " if name_prefix else "")
|
||||
value_component = self.file.create_entity("IfcReal", **{"wrappedValue": ifcopenshell.util.unit.si_conversions[name]})
|
||||
value_component = self.file.create_entity(
|
||||
"IfcReal", **{"wrappedValue": ifcopenshell.util.unit.si_conversions[name]}
|
||||
)
|
||||
conversion_factor = self.file.createIfcMeasureWithUnit(value_component, si_unit)
|
||||
return self.file.createIfcConversionBasedUnit(
|
||||
dimensional_exponents, "{}UNIT".format(unit_type.upper()), name, conversion_factor
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
class Usecase:
|
||||
def __init__(self, file, **settings):
|
||||
self.file = file
|
||||
self.settings = {"unit": 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["unit"], name, value)
|
||||
@@ -0,0 +1,10 @@
|
||||
class Usecase:
|
||||
def __init__(self, file, **settings):
|
||||
self.file = file
|
||||
self.settings = {"unit": 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["unit"], name, value)
|
||||
@@ -0,0 +1,10 @@
|
||||
class Usecase:
|
||||
def __init__(self, file, **settings):
|
||||
self.file = file
|
||||
self.settings = {"unit": 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["unit"], name, value)
|
||||
@@ -18,6 +18,17 @@ def is_a(entity, ifc_class):
|
||||
return False
|
||||
|
||||
|
||||
def get_subtypes(entity):
|
||||
def get_classes(declaration):
|
||||
results = []
|
||||
if not declaration.is_abstract():
|
||||
results.append(declaration)
|
||||
for subtype in declaration.subtypes():
|
||||
results.extend(get_classes(subtype))
|
||||
return results
|
||||
return get_classes(entity)
|
||||
|
||||
|
||||
def reassign_class(ifc_file, element, new_class):
|
||||
try:
|
||||
new_element = ifc_file.create_entity(new_class)
|
||||
|
||||
Reference in New Issue
Block a user