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,61 +21,63 @@ import ifcopenshell.util.unit
from typing import Optional
def assign_unit(
file: ifcopenshell.file,
units: Optional[list[ifcopenshell.entity_instance]] = None,
length: Optional[dict] = None,
area: Optional[dict] = None,
volume: Optional[dict] = None,
) -> ifcopenshell.entity_instance:
"""Assign default project units
Whenever a unitised quantity is specified, such as a length, area,
voltage, pressure, etc, these project units are used by default.
It is also possible to override units for specific properties. For
example, generally you might want square metres for area measurements,
but you might want square millimeters for the measurements of the cross
sectional area of cables in cable trays. However, this function only
deals with the default project units.
:param units: A list of units to assign as project defaults. See
ifcopenshell.api.unit.add_si_unit, unit.add_conversion_based_unit,
and unit.add_monetary_unit for information on how to create units.
:type units: list[ifcopenshell.entity_instance],optional
:return: The IfcUnitAssignment element
:rtype: ifcopenshell.entity_instance
Example:
.. code:: python
# You need a project before you can assign units.
ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcProject")
# Millimeters and square meters
length = ifcopenshell.api.run("unit.add_si_unit", model, unit_type="LENGTHUNIT", prefix="MILLI")
area = ifcopenshell.api.run("unit.add_si_unit", model, unit_type="AREAUNIT")
# Make it our default units, if we are doing a metric building
ifcopenshell.api.run("unit.assign_unit", model, units=[length, area])
# Alternatively, you may specify without any arguments to
# automatically create millimeters, square meters, and cubic meters
# as a convenience for testing purposes. Sorry imperial folks, we
# prioritise metric here.
ifcopenshell.api.run("unit.assign_unit", model)
"""
usecase = Usecase()
usecase.file = file
usecase.settings = {"units": units}
# This is a convenience function, likely to be deprecated in the future.
usecase.settings["length"] = length or {"is_metric": True, "raw": "MILLIMETERS"}
usecase.settings["area"] = area or {"is_metric": True, "raw": "METERS"}
usecase.settings["volume"] = volume or {"is_metric": True, "raw": "METERS"}
return usecase.execute()
class Usecase:
def __init__(
self,
file: ifcopenshell.file,
units: Optional[list[ifcopenshell.entity_instance]] = None,
length: Optional[dict] = None,
area: Optional[dict] = None,
volume: Optional[dict] = None,
):
"""Assign default project units
Whenever a unitised quantity is specified, such as a length, area,
voltage, pressure, etc, these project units are used by default.
It is also possible to override units for specific properties. For
example, generally you might want square metres for area measurements,
but you might want square millimeters for the measurements of the cross
sectional area of cables in cable trays. However, this function only
deals with the default project units.
:param units: A list of units to assign as project defaults. See
ifcopenshell.api.unit.add_si_unit, unit.add_conversion_based_unit,
and unit.add_monetary_unit for information on how to create units.
:type units: list[ifcopenshell.entity_instance],optional
:return: The IfcUnitAssignment element
:rtype: ifcopenshell.entity_instance
Example:
.. code:: python
# You need a project before you can assign units.
ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcProject")
# Millimeters and square meters
length = ifcopenshell.api.run("unit.add_si_unit", model, unit_type="LENGTHUNIT", prefix="MILLI")
area = ifcopenshell.api.run("unit.add_si_unit", model, unit_type="AREAUNIT")
# Make it our default units, if we are doing a metric building
ifcopenshell.api.run("unit.assign_unit", model, units=[length, area])
# Alternatively, you may specify without any arguments to
# automatically create millimeters, square meters, and cubic meters
# as a convenience for testing purposes. Sorry imperial folks, we
# prioritise metric here.
ifcopenshell.api.run("unit.assign_unit", model)
"""
self.file = file
self.settings = {"units": units}
# This is a convenience function, likely to be deprecated in the future.
self.settings["length"] = length or {"is_metric": True, "raw": "MILLIMETERS"}
self.settings["area"] = area or {"is_metric": True, "raw": "METERS"}
self.settings["volume"] = volume or {"is_metric": True, "raw": "METERS"}
def execute(self) -> ifcopenshell.entity_instance:
def execute(self):
# We're going to refactor this to split unit creation and assignment
if self.settings["units"]:
units = self.settings["units"]