mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-08-11 10:06:47 +00:00
Generate functions for all API usecases for better static code features. See #2693.
This commit is contained in:
@@ -22,61 +22,63 @@ import ifcopenshell.util.schema
|
||||
import ifcopenshell.util.element
|
||||
|
||||
|
||||
def reassign_class(
|
||||
file,
|
||||
product=None,
|
||||
ifc_class="IfcBuildingElementProxy",
|
||||
predefined_type=None,
|
||||
) -> None:
|
||||
"""Changes the class of a product
|
||||
|
||||
If you ever created a wall then realised it's meant to be something
|
||||
else, this function lets you change the IFC class whilst retaining all
|
||||
other geometry and relationships.
|
||||
|
||||
This is especially useful when dealing with poorly classified data from
|
||||
proprietary software with limited IFC capabilities.
|
||||
|
||||
If you are reassigning a type, the occurrence classes are also
|
||||
reassigned to maintain validity.
|
||||
|
||||
Vice versa, if you are reassigning an occurrence, the type is also
|
||||
reassigned in IFC4 and up. In IFC2X3, this may not occur if the type
|
||||
cannot be unambiguously derived, so you are required to manually check
|
||||
this.
|
||||
|
||||
:param product: The IfcProduct that you want to change the class of.
|
||||
:type product: ifcopenshell.entity_instance
|
||||
:param ifc_class: The new IFC class you want to change it to.
|
||||
:type ifc_class: str,optional
|
||||
:param predefined_type: In case you want to change the predefined type
|
||||
too. User defined types are also allowed, just type what you want.
|
||||
:type predefined_type: str,optional
|
||||
:return: The newly modified product.
|
||||
:rtype: ifcopenshell.entity_instance
|
||||
|
||||
Example:
|
||||
|
||||
.. code:: python
|
||||
|
||||
# We have a wall.
|
||||
wall = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcWall")
|
||||
|
||||
# Oh, did I say wall? I meant slab.
|
||||
slab = ifcopenshell.api.run("root.reassign_class", model, product=wall, ifc_class="IfcSlab")
|
||||
|
||||
# Warning: this will crash since wall doesn't exist any more.
|
||||
print(wall) # Kaboom.
|
||||
"""
|
||||
usecase = Usecase()
|
||||
usecase.file = file
|
||||
usecase.settings = {
|
||||
"product": product,
|
||||
"ifc_class": ifc_class,
|
||||
"predefined_type": predefined_type,
|
||||
}
|
||||
return usecase.execute()
|
||||
|
||||
|
||||
class Usecase:
|
||||
def __init__(
|
||||
self,
|
||||
file,
|
||||
product=None,
|
||||
ifc_class="IfcBuildingElementProxy",
|
||||
predefined_type=None,
|
||||
):
|
||||
"""Changes the class of a product
|
||||
|
||||
If you ever created a wall then realised it's meant to be something
|
||||
else, this function lets you change the IFC class whilst retaining all
|
||||
other geometry and relationships.
|
||||
|
||||
This is especially useful when dealing with poorly classified data from
|
||||
proprietary software with limited IFC capabilities.
|
||||
|
||||
If you are reassigning a type, the occurrence classes are also
|
||||
reassigned to maintain validity.
|
||||
|
||||
Vice versa, if you are reassigning an occurrence, the type is also
|
||||
reassigned in IFC4 and up. In IFC2X3, this may not occur if the type
|
||||
cannot be unambiguously derived, so you are required to manually check
|
||||
this.
|
||||
|
||||
:param product: The IfcProduct that you want to change the class of.
|
||||
:type product: ifcopenshell.entity_instance
|
||||
:param ifc_class: The new IFC class you want to change it to.
|
||||
:type ifc_class: str,optional
|
||||
:param predefined_type: In case you want to change the predefined type
|
||||
too. User defined types are also allowed, just type what you want.
|
||||
:type predefined_type: str,optional
|
||||
:return: The newly modified product.
|
||||
:rtype: ifcopenshell.entity_instance
|
||||
|
||||
Example:
|
||||
|
||||
.. code:: python
|
||||
|
||||
# We have a wall.
|
||||
wall = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcWall")
|
||||
|
||||
# Oh, did I say wall? I meant slab.
|
||||
slab = ifcopenshell.api.run("root.reassign_class", model, product=wall, ifc_class="IfcSlab")
|
||||
|
||||
# Warning: this will crash since wall doesn't exist any more.
|
||||
print(wall) # Kaboom.
|
||||
"""
|
||||
self.file = file
|
||||
self.settings = {
|
||||
"product": product,
|
||||
"ifc_class": ifc_class,
|
||||
"predefined_type": predefined_type,
|
||||
}
|
||||
|
||||
def execute(self):
|
||||
element = self.reassign_class(self.settings["product"], self.settings["ifc_class"])
|
||||
|
||||
|
||||
Reference in New Issue
Block a user