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
@@ -15,3 +15,14 @@
#
# You should have received a copy of the GNU Lesser General Public License
# along with IfcOpenShell. If not, see <http://www.gnu.org/licenses/>.
from .add_context_dependent_unit import add_context_dependent_unit
from .add_conversion_based_unit import add_conversion_based_unit
from .add_monetary_unit import add_monetary_unit
from .add_si_unit import add_si_unit
from .assign_unit import assign_unit
from .edit_derived_unit import edit_derived_unit
from .edit_monetary_unit import edit_monetary_unit
from .edit_named_unit import edit_named_unit
from .remove_unit import remove_unit
from .unassign_unit import unassign_unit
@@ -17,48 +17,45 @@
# along with IfcOpenShell. If not, see <http://www.gnu.org/licenses/>.
class Usecase:
def __init__(self, file, unit_type="USERDEFINED", name="THINGAMAJIG", dimensions=None):
"""Add a new arbitrary unit that can only be interpreted in a project specific context
def add_context_dependent_unit(file, unit_type="USERDEFINED", name="THINGAMAJIG", dimensions=None) -> None:
"""Add a new arbitrary unit that can only be interpreted in a project specific context
Occasionally the construction industry uses arbitrary units to quantify
objects, like "pairs" of door hardware, "palettes" or "boxes" of fixings
or equipment.
Occasionally the construction industry uses arbitrary units to quantify
objects, like "pairs" of door hardware, "palettes" or "boxes" of fixings
or equipment.
:param unit_type: Typically should be left as USERDEFINED, unless for
some bizarre reason you are redefining something you could use a
sensible normal unit for. In that case, firstly stop whatever you're
doing and have a hard think about your life, and then if life really
is going that badly for you, check out the IFC docs for IfcUnitEnum.
:type unit_type: str
:param name: Give your unit a name. X what? X bananas?
:type name: str
:param dimensions: Units typically measure one of 7 fundamental physical
dimensions: length, mass, time, electric current, temperature,
substance amount, or luminous intensity. These are represented as a
list of 7 integers, representing the exponents of each one of these
dimensions. For example, a length unit is (1, 0, 0, 0, 0, 0, 0),
where as an area unit is (2, 0, 0, 0, 0, 0, 0). A unit of meters per
second is (1, 0, -1, 0, 0, 0, 0). For context dependent units, it is
recommended to leave this as the default of (0, 0, 0, 0, 0, 0, 0).
:type dimensions: list[int]
:return: The new IfcContextDependentUnit
:rtype: ifcopenshell.entity_instance
:param unit_type: Typically should be left as USERDEFINED, unless for
some bizarre reason you are redefining something you could use a
sensible normal unit for. In that case, firstly stop whatever you're
doing and have a hard think about your life, and then if life really
is going that badly for you, check out the IFC docs for IfcUnitEnum.
:type unit_type: str
:param name: Give your unit a name. X what? X bananas?
:type name: str
:param dimensions: Units typically measure one of 7 fundamental physical
dimensions: length, mass, time, electric current, temperature,
substance amount, or luminous intensity. These are represented as a
list of 7 integers, representing the exponents of each one of these
dimensions. For example, a length unit is (1, 0, 0, 0, 0, 0, 0),
where as an area unit is (2, 0, 0, 0, 0, 0, 0). A unit of meters per
second is (1, 0, -1, 0, 0, 0, 0). For context dependent units, it is
recommended to leave this as the default of (0, 0, 0, 0, 0, 0, 0).
:type dimensions: list[int]
:return: The new IfcContextDependentUnit
:rtype: ifcopenshell.entity_instance
Example:
Example:
.. code:: python
.. code:: python
# Boxes of things
ifcopenshell.api.run("unit.add_context_dependent_unit", model, name="BOXES")
"""
self.file = file
self.settings = {"unit_type": unit_type, "name": name, "dimensions": dimensions or (0, 0, 0, 0, 0, 0, 0)}
# Boxes of things
ifcopenshell.api.run("unit.add_context_dependent_unit", model, name="BOXES")
"""
settings = {"unit_type": unit_type, "name": name, "dimensions": dimensions or (0, 0, 0, 0, 0, 0, 0)}
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"],
)
return file.create_entity(
"IfcContextDependentUnit",
Dimensions=file.createIfcDimensionalExponents(*settings["dimensions"]),
UnitType=settings["unit_type"],
Name=settings["name"],
)
@@ -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)
@@ -17,32 +17,29 @@
# along with IfcOpenShell. If not, see <http://www.gnu.org/licenses/>.
class Usecase:
def __init__(self, file, currency="DOLLARYDOO"):
"""Add a new currency
def add_monetary_unit(file, currency="DOLLARYDOO") -> None:
"""Add a new currency
Currency units are useful in cost plans to know in what currency the
costs are calculated in. The currencies should follow ISO 4217, like
USD, GBP, AUD, MYR, etc.
Currency units are useful in cost plans to know in what currency the
costs are calculated in. The currencies should follow ISO 4217, like
USD, GBP, AUD, MYR, etc.
:param currency: The currency code
:type currency: str
:return: The newly created IfcMonetaryUnit
:rtype: ifcopenshell.entity_instance
:param currency: The currency code
:type currency: str
:return: The newly created IfcMonetaryUnit
:rtype: ifcopenshell.entity_instance
Example:
Example:
.. code:: python
.. code:: python
# If you do all your cost plans in Zimbabwean dollars then nobody
# knows how accurate the numbers are.
zwl = ifcopenshell.api.run("unit.add_monetary_unit", model, currency="ZWL")
# If you do all your cost plans in Zimbabwean dollars then nobody
# knows how accurate the numbers are.
zwl = ifcopenshell.api.run("unit.add_monetary_unit", model, currency="ZWL")
# Make it our default currency
ifcopenshell.api.run("unit.assign_unit", model, units=[zwl])
"""
self.file = file
self.settings = {"currency": currency}
# Make it our default currency
ifcopenshell.api.run("unit.assign_unit", model, units=[zwl])
"""
settings = {"currency": currency}
def execute(self):
return self.file.create_entity("IfcMonetaryUnit", self.settings["currency"])
return file.create_entity("IfcMonetaryUnit", settings["currency"])
@@ -20,48 +20,45 @@ import ifcopenshell.util.unit
from typing import Optional
class Usecase:
def __init__(self, file: ifcopenshell.file, unit_type: str = "LENGTHUNIT", prefix: Optional[str] = None):
"""Add a new SI unit
def add_si_unit(
file: ifcopenshell.file, unit_type: str = "LENGTHUNIT", prefix: Optional[str] = None
) -> ifcopenshell.entity_instance:
"""Add a new SI unit
The supported types are ABSORBEDDOSEUNIT, AMOUNTOFSUBSTANCEUNIT,
AREAUNIT, DOSEEQUIVALENTUNIT, ELECTRICCAPACITANCEUNIT,
ELECTRICCHARGEUNIT, ELECTRICCONDUCTANCEUNIT, ELECTRICCURRENTUNIT,
ELECTRICRESISTANCEUNIT, ELECTRICVOLTAGEUNIT, ENERGYUNIT, FORCEUNIT,
FREQUENCYUNIT, ILLUMINANCEUNIT, INDUCTANCEUNIT, LENGTHUNIT,
LUMINOUSFLUXUNIT, LUMINOUSINTENSITYUNIT, MAGNETICFLUXDENSITYUNIT,
MAGNETICFLUXUNIT, MASSUNIT, PLANEANGLEUNIT, POWERUNIT, PRESSUREUNIT,
RADIOACTIVITYUNIT, SOLIDANGLEUNIT, THERMODYNAMICTEMPERATUREUNIT,
TIMEUNIT, VOLUMEUNIT.
The supported types are ABSORBEDDOSEUNIT, AMOUNTOFSUBSTANCEUNIT,
AREAUNIT, DOSEEQUIVALENTUNIT, ELECTRICCAPACITANCEUNIT,
ELECTRICCHARGEUNIT, ELECTRICCONDUCTANCEUNIT, ELECTRICCURRENTUNIT,
ELECTRICRESISTANCEUNIT, ELECTRICVOLTAGEUNIT, ENERGYUNIT, FORCEUNIT,
FREQUENCYUNIT, ILLUMINANCEUNIT, INDUCTANCEUNIT, LENGTHUNIT,
LUMINOUSFLUXUNIT, LUMINOUSINTENSITYUNIT, MAGNETICFLUXDENSITYUNIT,
MAGNETICFLUXUNIT, MASSUNIT, PLANEANGLEUNIT, POWERUNIT, PRESSUREUNIT,
RADIOACTIVITYUNIT, SOLIDANGLEUNIT, THERMODYNAMICTEMPERATUREUNIT,
TIMEUNIT, VOLUMEUNIT.
Prefixes supported are ATTO, CENTI, DECA, DECI, EXA, FEMTO, GIGA, HECTO,
KILO, MEGA, MICRO, MILLI, NANO, PETA, PICO, TERA.
Prefixes supported are ATTO, CENTI, DECA, DECI, EXA, FEMTO, GIGA, HECTO,
KILO, MEGA, MICRO, MILLI, NANO, PETA, PICO, TERA.
:param unit_type: A type of unit chosen from the list above. For
example, choosing LENGTHUNIT will give you a metre.
:type unit_type: str
:param prefix: A prefix chosen from the list above, or None for no
prefix.
:type prefix: str,optional
:return: The newly created IfcSIUnit
:rtype: ifcopenshell.entity_instance
:param unit_type: A type of unit chosen from the list above. For
example, choosing LENGTHUNIT will give you a metre.
:type unit_type: str
:param prefix: A prefix chosen from the list above, or None for no
prefix.
:type prefix: str,optional
:return: The newly created IfcSIUnit
:rtype: ifcopenshell.entity_instance
Example:
Example:
.. code:: python
.. code:: python
# 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")
# 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])
"""
self.file = file
self.settings = {"unit_type": unit_type, "prefix": prefix}
# Make it our default units, if we are doing a metric building
ifcopenshell.api.run("unit.assign_unit", model, units=[length, area])
"""
settings = {"unit_type": unit_type, "prefix": prefix}
def execute(self) -> ifcopenshell.entity_instance:
name = ifcopenshell.util.unit.si_type_names.get(self.settings["unit_type"], None)
return self.file.create_entity(
"IfcSIUnit", UnitType=self.settings["unit_type"], Name=name, Prefix=self.settings["prefix"]
)
name = ifcopenshell.util.unit.si_type_names.get(settings["unit_type"], None)
return file.create_entity("IfcSIUnit", UnitType=settings["unit_type"], Name=name, Prefix=settings["prefix"])
@@ -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"]
@@ -17,23 +17,20 @@
# along with IfcOpenShell. If not, see <http://www.gnu.org/licenses/>.
class Usecase:
def __init__(self, file, unit=None, attributes=None):
"""Edits the attributes of an IfcDerivedUnit
def edit_derived_unit(file, unit=None, attributes=None) -> None:
"""Edits the attributes of an IfcDerivedUnit
For more information about the attributes and data types of an
IfcDerivedUnit, consult the IFC documentation.
For more information about the attributes and data types of an
IfcDerivedUnit, consult the IFC documentation.
:param unit: The IfcDerivedUnit entity you want to edit
:type unit: ifcopenshell.entity_instance
:param attributes: a dictionary of attribute names and values.
:type attributes: dict, optional
:return: None
:rtype: None
"""
self.file = file
self.settings = {"unit": unit, "attributes": attributes or {}}
:param unit: The IfcDerivedUnit entity you want to edit
:type unit: ifcopenshell.entity_instance
:param attributes: a dictionary of attribute names and values.
:type attributes: dict, optional
:return: None
:rtype: None
"""
settings = {"unit": unit, "attributes": attributes or {}}
def execute(self):
for name, value in self.settings["attributes"].items():
setattr(self.settings["unit"], name, value)
for name, value in settings["attributes"].items():
setattr(settings["unit"], name, value)
@@ -17,34 +17,31 @@
# along with IfcOpenShell. If not, see <http://www.gnu.org/licenses/>.
class Usecase:
def __init__(self, file, unit=None, attributes=None):
"""Edits the attributes of an IfcMonetaryUnit
def edit_monetary_unit(file, unit=None, attributes=None) -> None:
"""Edits the attributes of an IfcMonetaryUnit
For more information about the attributes and data types of an
IfcMonetaryUnit, consult the IFC documentation.
For more information about the attributes and data types of an
IfcMonetaryUnit, consult the IFC documentation.
:param unit: The IfcMonetaryUnit entity you want to edit
:type unit: ifcopenshell.entity_instance
:param attributes: a dictionary of attribute names and values.
:type attributes: dict, optional
:return: None
:rtype: None
:param unit: The IfcMonetaryUnit entity you want to edit
:type unit: ifcopenshell.entity_instance
:param attributes: a dictionary of attribute names and values.
:type attributes: dict, optional
:return: None
:rtype: None
Example:
Example:
.. code:: python
.. code:: python
# If you do all your cost plans in Zimbabwean dollars then nobody
# knows how accurate the numbers are.
zwl = ifcopenshell.api.run("unit.add_monetary_unit", model, currency="ZWL")
# If you do all your cost plans in Zimbabwean dollars then nobody
# knows how accurate the numbers are.
zwl = ifcopenshell.api.run("unit.add_monetary_unit", model, currency="ZWL")
# Ah who are we kidding
ifcopenshell.api.run("unit.edit_monetary_unit", model, unit=zwl, attributes={"Currency": "USD"})
"""
self.file = file
self.settings = {"unit": unit, "attributes": attributes or {}}
# Ah who are we kidding
ifcopenshell.api.run("unit.edit_monetary_unit", model, unit=zwl, attributes={"Currency": "USD"})
"""
settings = {"unit": unit, "attributes": attributes or {}}
def execute(self):
for name, value in self.settings["attributes"].items():
setattr(self.settings["unit"], name, value)
for name, value in settings["attributes"].items():
setattr(settings["unit"], name, value)
@@ -17,44 +17,41 @@
# along with IfcOpenShell. If not, see <http://www.gnu.org/licenses/>.
class Usecase:
def __init__(self, file, unit=None, attributes=None):
"""Edits the attributes of an IfcNamedUnit
def edit_named_unit(file, unit=None, attributes=None) -> None:
"""Edits the attributes of an IfcNamedUnit
Named units include SI units, conversion based units (imperial units),
and context dependent units.
Named units include SI units, conversion based units (imperial units),
and context dependent units.
For more information about the attributes and data types of an
IfcNamedUnit, consult the IFC documentation.
For more information about the attributes and data types of an
IfcNamedUnit, consult the IFC documentation.
:param unit: The IfcNamedUnit entity you want to edit
:type unit: ifcopenshell.entity_instance
:param attributes: a dictionary of attribute names and values.
:type attributes: dict, optional
:return: None
:rtype: None
:param unit: The IfcNamedUnit entity you want to edit
:type unit: ifcopenshell.entity_instance
:param attributes: a dictionary of attribute names and values.
:type attributes: dict, optional
:return: None
:rtype: None
Example:
Example:
.. code:: python
.. code:: python
# Boxes of things
unit = ifcopenshell.api.run("unit.add_context_dependent_unit", model, name="BOXES")
# Boxes of things
unit = ifcopenshell.api.run("unit.add_context_dependent_unit", model, name="BOXES")
# Uh, crates? Boxes? Whatever.
ifcopenshell.api.run("unit.edit_named_unit", model, unit=unit, attibutes={"Name": "CRATES"})
"""
self.file = file
self.settings = {"unit": unit, "attributes": attributes or {}}
# Uh, crates? Boxes? Whatever.
ifcopenshell.api.run("unit.edit_named_unit", model, unit=unit, attibutes={"Name": "CRATES"})
"""
settings = {"unit": unit, "attributes": attributes or {}}
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)
for name, value in settings["attributes"].items():
if name == "Dimensions":
dimensions = settings["unit"].Dimensions
if len(file.get_inverse(dimensions)) > 1:
settings["unit"].Dimensions = file.createIfcDimensionalExponents(*value)
else:
for i, exponent in enumerate(value):
dimensions[i] = exponent
continue
setattr(settings["unit"], name, value)
@@ -20,38 +20,35 @@ import ifcopenshell.util.unit
import ifcopenshell.util.element
class Usecase:
def __init__(self, file, unit=None):
"""Remove a unit
def remove_unit(file, unit=None) -> None:
"""Remove a unit
Be very careful when a unit is removed, as it may mean that previously
defined quantities in the model completely lose their meaning.
Be very careful when a unit is removed, as it may mean that previously
defined quantities in the model completely lose their meaning.
:param unit: The unit element to remove
:type unit: ifcopenshell.entity_instance
:return: None
:rtype: None
:param unit: The unit element to remove
:type unit: ifcopenshell.entity_instance
:return: None
:rtype: None
Example:
Example:
.. code:: python
.. code:: python
# What?
unit = ifcopenshell.api.run("unit.add_context_dependent_unit", model, name="HANDFULS")
# What?
unit = ifcopenshell.api.run("unit.add_context_dependent_unit", model, name="HANDFULS")
# Yeah maybe not.
ifcopenshell.api.run("unit.remove_unit", model, unit=unit)
"""
self.file = file
self.settings = {"unit": unit}
# Yeah maybe not.
ifcopenshell.api.run("unit.remove_unit", model, unit=unit)
"""
settings = {"unit": unit}
def execute(self):
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"])
unit_assignment = ifcopenshell.util.unit.get_unit_assignment(file)
if unit_assignment and settings["unit"] in unit_assignment.Units:
units = list(unit_assignment.Units)
units.remove(settings["unit"])
if units:
unit_assignment.Units = units
else:
file.remove(unit_assignment)
ifcopenshell.util.element.remove_deep(file, settings["unit"])
@@ -19,43 +19,40 @@ import ifcopenshell
from typing import Optional
class Usecase:
def __init__(self, file: ifcopenshell.file, units: Optional[list[ifcopenshell.entity_instance]] = None):
"""Unassigns units as default units for the project
def unassign_unit(file: ifcopenshell.file, units: Optional[list[ifcopenshell.entity_instance]] = None) -> None:
"""Unassigns units as default units for the project
:param units: A list of units to assign as project defaults.
:type units: list[ifcopenshell.entity_instance],optional
:return: None
:rtype: None
:param units: A list of units to assign as project defaults.
:type units: list[ifcopenshell.entity_instance],optional
:return: None
:rtype: None
Example:
Example:
.. code:: python
.. code:: python
# You need a project before you can assign units.
ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcProject")
# 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")
# 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])
# Make it our default units, if we are doing a metric building
ifcopenshell.api.run("unit.assign_unit", model, units=[length, area])
# Actually, we don't need areas.
ifcopenshell.api.run("unit.unassign_unit", model, units=[area])
"""
self.file = file
self.settings = {"units": units}
# Actually, we don't need areas.
ifcopenshell.api.run("unit.unassign_unit", model, units=[area])
"""
settings = {"units": units}
def execute(self):
unit_assignment = self.file.by_type("IfcUnitAssignment")
if not unit_assignment:
return
unit_assignment = unit_assignment[0]
units = set(unit_assignment.Units or [])
units = units - set(self.settings["units"])
if units:
unit_assignment.Units = list(units)
return unit_assignment
self.file.remove(unit_assignment)
unit_assignment = file.by_type("IfcUnitAssignment")
if not unit_assignment:
return
unit_assignment = unit_assignment[0]
units = set(unit_assignment.Units or [])
units = units - set(settings["units"])
if units:
unit_assignment.Units = list(units)
return unit_assignment
file.remove(unit_assignment)