Generate functions for all API usecases for better static code features. See #2693.

This commit is contained in:
Dion Moult
2024-05-06 14:35:39 +10:00
parent 10f894e2ea
commit d11ec67129
330 changed files with 13283 additions and 13751 deletions
@@ -21,67 +21,66 @@ import ifcopenshell.util.unit
from typing import Optional
class Usecase:
def __init__(self, file: ifcopenshell.file, name: str = "foot", conversion_offset: Optional[float] = None):
"""Add a conversion based unit
def add_conversion_based_unit(
file: ifcopenshell.file, name: str = "foot", conversion_offset: Optional[float] = None
) -> ifcopenshell.entity_instance:
"""Add a conversion based unit
If you're in one of those countries who don't use SI units, you're
probably simply using SI units converted into another unit. If you want
to use _those_ units, you can create a conversion based unit with this
function. You can choose from one of: inch, foot, yard, mile, square
inch, square foot, square yard, acre, square mile, cubic inch, cubic
foot, cubic yard, litre, fluid ounce UK, fluid ounce US, pint UK, pint
US, gallon UK, gallon US, degree, ounce, pound, ton UK, ton US, lbf,
kip, psi, ksi, minute, hour, day, btu, and fahrenheit.
If you're in one of those countries who don't use SI units, you're
probably simply using SI units converted into another unit. If you want
to use _those_ units, you can create a conversion based unit with this
function. You can choose from one of: inch, foot, yard, mile, square
inch, square foot, square yard, acre, square mile, cubic inch, cubic
foot, cubic yard, litre, fluid ounce UK, fluid ounce US, pint UK, pint
US, gallon UK, gallon US, degree, ounce, pound, ton UK, ton US, lbf,
kip, psi, ksi, minute, hour, day, btu, and fahrenheit.
:param name: A converted name chosen from the list above.
:type name: str
:param conversion_offset: If you want to offset the conversion further
by a set number, you may specify it here. For example, fahrenheit is
1.8 * kelvin - 459.67. The -459.67 is the conversion offset. Note
that this is just an example and you don't actually need to specify
that for fahrenheit as it's built into this API function. For
advanced users only.
:type conversion_offset: float, optional
:return: The new IfcConversionBasedUnit or
IfcConversionBasedUnitWithOffset
:rtype: ifcopenshell.entity_instance
:param name: A converted name chosen from the list above.
:type name: str
:param conversion_offset: If you want to offset the conversion further
by a set number, you may specify it here. For example, fahrenheit is
1.8 * kelvin - 459.67. The -459.67 is the conversion offset. Note
that this is just an example and you don't actually need to specify
that for fahrenheit as it's built into this API function. For
advanced users only.
:type conversion_offset: float, optional
:return: The new IfcConversionBasedUnit or
IfcConversionBasedUnitWithOffset
:rtype: ifcopenshell.entity_instance
Example:
Example:
.. code:: python
.. code:: python
# Some common imperial measurements
length = ifcopenshell.api.run("unit.add_conversion_based_unit", model, name="inch")
area = ifcopenshell.api.run("unit.add_conversion_based_unit", model, name="square foot")
# Some common imperial measurements
length = ifcopenshell.api.run("unit.add_conversion_based_unit", model, name="inch")
area = ifcopenshell.api.run("unit.add_conversion_based_unit", model, name="square foot")
# Make it our default units, if we are doing an imperial building
ifcopenshell.api.run("unit.assign_unit", model, units=[length, area])
"""
self.file = file
self.settings = {"name": name, "conversion_offset": conversion_offset}
# Make it our default units, if we are doing an imperial building
ifcopenshell.api.run("unit.assign_unit", model, units=[length, area])
"""
settings = {"name": name, "conversion_offset": conversion_offset}
def execute(self) -> ifcopenshell.entity_instance:
unit_type = ifcopenshell.util.unit.imperial_types.get(self.settings["name"], "USERDEFINED")
dimensions = ifcopenshell.util.unit.named_dimensions[unit_type]
exponents = self.file.createIfcDimensionalExponents(*dimensions)
si_name = ifcopenshell.util.unit.si_type_names[unit_type]
si_unit = self.file.createIfcSIUnit(UnitType=unit_type, Name=si_name)
unit_type = ifcopenshell.util.unit.imperial_types.get(settings["name"], "USERDEFINED")
dimensions = ifcopenshell.util.unit.named_dimensions[unit_type]
exponents = file.createIfcDimensionalExponents(*dimensions)
si_name = ifcopenshell.util.unit.si_type_names[unit_type]
si_unit = file.createIfcSIUnit(UnitType=unit_type, Name=si_name)
conversion_real = ifcopenshell.util.unit.si_conversions.get(self.settings["name"], 1)
value_component = self.file.create_entity("IfcReal", **{"wrappedValue": conversion_real})
conversion_factor = self.file.createIfcMeasureWithUnit(value_component, si_unit)
conversion_real = ifcopenshell.util.unit.si_conversions.get(settings["name"], 1)
value_component = file.create_entity("IfcReal", **{"wrappedValue": conversion_real})
conversion_factor = file.createIfcMeasureWithUnit(value_component, si_unit)
conversion_offset = self.settings["conversion_offset"]
if not conversion_offset:
conversion_offset = ifcopenshell.util.unit.si_offsets.get(self.settings["name"], 0)
conversion_offset = settings["conversion_offset"]
if not conversion_offset:
conversion_offset = ifcopenshell.util.unit.si_offsets.get(settings["name"], 0)
if conversion_offset:
return self.file.createIfcConversionBasedUnitWithOffset(
exponents,
unit_type,
self.settings["name"],
conversion_factor,
conversion_offset,
)
return self.file.createIfcConversionBasedUnit(exponents, unit_type, self.settings["name"], conversion_factor)
if conversion_offset:
return file.createIfcConversionBasedUnitWithOffset(
exponents,
unit_type,
settings["name"],
conversion_factor,
conversion_offset,
)
return file.createIfcConversionBasedUnit(exponents, unit_type, settings["name"], conversion_factor)