mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-09-27 18:57:17 +00:00
Fix #1795. New support for conversion based units with offsets, like fahrenheit.
This commit is contained in:
@@ -5,33 +5,29 @@ import ifcopenshell.util.unit
|
|||||||
class Usecase:
|
class Usecase:
|
||||||
def __init__(self, file, **settings):
|
def __init__(self, file, **settings):
|
||||||
self.file = file
|
self.file = file
|
||||||
self.settings = {"unit_type": "LENGTHUNIT", "name": "METRE", "prefix": None, "conversion_offset": None}
|
self.settings = {"name": "foot", "conversion_offset": None}
|
||||||
for key, value in settings.items():
|
for key, value in settings.items():
|
||||||
self.settings[key] = value
|
self.settings[key] = value
|
||||||
|
|
||||||
def execute(self):
|
def execute(self):
|
||||||
if self.settings["unit_type"] == "LENGTHUNIT":
|
unit_type = ifcopenshell.util.unit.imperial_types.get(self.settings["name"], "USERDEFINED")
|
||||||
dimensional_exponents = self.file.createIfcDimensionalExponents(1, 0, 0, 0, 0, 0, 0)
|
dimensions = ifcopenshell.util.unit.named_dimensions[unit_type]
|
||||||
si_unit = self.file.createIfcSIUnit(UnitType=self.settings["unit_type"], Name="METRE")
|
exponents = self.file.createIfcDimensionalExponents(*dimensions)
|
||||||
elif self.settings["unit_type"] == "AREAUNIT":
|
si_name = ifcopenshell.util.unit.si_type_names[unit_type]
|
||||||
dimensional_exponents = self.file.createIfcDimensionalExponents(2, 0, 0, 0, 0, 0, 0)
|
si_unit = self.file.createIfcSIUnit(UnitType=unit_type, Name=si_name)
|
||||||
si_unit = self.file.createIfcSIUnit(UnitType=self.settings["unit_type"], Name="SQUARE_METRE")
|
|
||||||
elif self.settings["unit_type"] == "VOLUMEUNIT":
|
|
||||||
dimensional_exponents = self.file.createIfcDimensionalExponents(3, 0, 0, 0, 0, 0, 0)
|
|
||||||
si_unit = self.file.createIfcSIUnit(UnitType=self.settings["unit_type"], Name="CUBIC_METRE")
|
|
||||||
|
|
||||||
conversion_real = ifcopenshell.util.unit.si_conversions.get(self.settings["name"], 1)
|
conversion_real = ifcopenshell.util.unit.si_conversions.get(self.settings["name"], 1)
|
||||||
value_component = self.file.create_entity("IfcReal", **{"wrappedValue": conversion_real})
|
value_component = self.file.create_entity("IfcReal", **{"wrappedValue": conversion_real})
|
||||||
conversion_factor = self.file.createIfcMeasureWithUnit(value_component, si_unit)
|
conversion_factor = self.file.createIfcMeasureWithUnit(value_component, si_unit)
|
||||||
|
|
||||||
if self.settings["conversion_offset"]:
|
conversion_offset = self.settings["conversion_offset"]
|
||||||
return self.file.createIfcConversionBasedUnit(
|
if not conversion_offset:
|
||||||
dimensional_exponents,
|
conversion_offset = ifcopenshell.util.unit.si_offsets.get(self.settings["name"], 0)
|
||||||
self.settings["unit_type"],
|
|
||||||
self.settings["name"],
|
if conversion_offset:
|
||||||
conversion_factor,
|
return self.file.createIfcConversionBasedUnitWithOffset(
|
||||||
self.settings["conversion_offset"],
|
exponents, unit_type, self.settings["name"], conversion_factor, conversion_offset,
|
||||||
)
|
)
|
||||||
return self.file.createIfcConversionBasedUnit(
|
return self.file.createIfcConversionBasedUnit(
|
||||||
dimensional_exponents, self.settings["unit_type"], self.settings["name"], conversion_factor
|
exponents, unit_type, self.settings["name"], conversion_factor
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -192,9 +192,14 @@ si_conversions = {
|
|||||||
"hour": 3600,
|
"hour": 3600,
|
||||||
"day": 86400,
|
"day": 86400,
|
||||||
"btu": 1055.056,
|
"btu": 1055.056,
|
||||||
|
"fahrenheit": 1.8,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
si_offsets = {
|
||||||
|
"fahrenheit": -459.67,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
imperial_types = {
|
imperial_types = {
|
||||||
"thou": "LENGTHUNIT",
|
"thou": "LENGTHUNIT",
|
||||||
@@ -233,6 +238,7 @@ imperial_types = {
|
|||||||
"hour": "TIMEUNIT",
|
"hour": "TIMEUNIT",
|
||||||
"day": "TIMEUNIT",
|
"day": "TIMEUNIT",
|
||||||
"btu": "ENERGYUNIT",
|
"btu": "ENERGYUNIT",
|
||||||
|
"fahrenheit": "THERMODYNAMICTEMPERATUREUNIT",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,43 @@
|
|||||||
|
import test.bootstrap
|
||||||
|
import ifcopenshell.api
|
||||||
|
|
||||||
|
|
||||||
|
class TestAddConversionBasedUnit(test.bootstrap.IFC4):
|
||||||
|
def test_run(self):
|
||||||
|
unit = ifcopenshell.api.run("unit.add_conversion_based_unit", self.file, name="foot")
|
||||||
|
assert unit.is_a("IfcConversionBasedUnit")
|
||||||
|
assert unit.Dimensions.LengthExponent == 1
|
||||||
|
assert unit.Dimensions.MassExponent == 0
|
||||||
|
assert unit.Dimensions.TimeExponent == 0
|
||||||
|
assert unit.Dimensions.ElectricCurrentExponent == 0
|
||||||
|
assert unit.Dimensions.ThermodynamicTemperatureExponent == 0
|
||||||
|
assert unit.Dimensions.AmountOfSubstanceExponent == 0
|
||||||
|
assert unit.Dimensions.LuminousIntensityExponent == 0
|
||||||
|
assert unit.UnitType == "LENGTHUNIT"
|
||||||
|
assert unit.Name == "foot"
|
||||||
|
assert unit.ConversionFactor.ValueComponent.wrappedValue == 0.3048
|
||||||
|
si_unit = unit.ConversionFactor.UnitComponent
|
||||||
|
assert si_unit.is_a("IfcSIUnit")
|
||||||
|
assert si_unit.UnitType == "LENGTHUNIT"
|
||||||
|
assert si_unit.Prefix is None
|
||||||
|
assert si_unit.Name == "METRE"
|
||||||
|
|
||||||
|
def test_adding_a_unit_with_offset(self):
|
||||||
|
unit = ifcopenshell.api.run("unit.add_conversion_based_unit", self.file, name="fahrenheit")
|
||||||
|
assert unit.is_a("IfcConversionBasedUnitWithOffset")
|
||||||
|
assert unit.Dimensions.LengthExponent == 0
|
||||||
|
assert unit.Dimensions.MassExponent == 0
|
||||||
|
assert unit.Dimensions.TimeExponent == 0
|
||||||
|
assert unit.Dimensions.ElectricCurrentExponent == 0
|
||||||
|
assert unit.Dimensions.ThermodynamicTemperatureExponent == 1
|
||||||
|
assert unit.Dimensions.AmountOfSubstanceExponent == 0
|
||||||
|
assert unit.Dimensions.LuminousIntensityExponent == 0
|
||||||
|
assert unit.UnitType == "THERMODYNAMICTEMPERATUREUNIT"
|
||||||
|
assert unit.Name == "fahrenheit"
|
||||||
|
assert unit.ConversionFactor.ValueComponent.wrappedValue == 1.8
|
||||||
|
si_unit = unit.ConversionFactor.UnitComponent
|
||||||
|
assert si_unit.is_a("IfcSIUnit")
|
||||||
|
assert si_unit.UnitType == "THERMODYNAMICTEMPERATUREUNIT"
|
||||||
|
assert si_unit.Prefix is None
|
||||||
|
assert si_unit.Name == "KELVIN"
|
||||||
|
assert unit.ConversionOffset == -459.67
|
||||||
Reference in New Issue
Block a user