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,9 @@
#
# 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_arbitrary_profile import add_arbitrary_profile
from .add_arbitrary_profile_with_voids import add_arbitrary_profile_with_voids
from .add_parameterized_profile import add_parameterized_profile
from .edit_profile import edit_profile
from .remove_profile import remove_profile
@@ -19,39 +19,42 @@
import ifcopenshell.util.unit
def add_arbitrary_profile(file, profile=None, name=None) -> None:
"""Adds a new arbitrary polyline-based profile
The profile is represented as a polyline defined by a list of
coordinates. Only straight segments are allowed. Coordinates must be
provided in SI meters.
To represent a closed curve, the first and last coordinate must be
identical.
:param profile: A list of coordinates
:type profile: list[list[float]]
:param name: If the profile is semantically significant (i.e. to be
managed and reused by the user) then it must be named. Otherwise,
this may be left as none.
:type name: str, optional
:return: The newly created IfcArbitraryClosedProfileDef
:rtype: ifcopenshell.entity_instance
Example:
.. code:: python
# A 10mm by 100mm rectangle, such that might be used as a wooden
# skirting board or kick plate.
square = ifcopenshell.api.run("profile.add_arbitrary_profile", model,
profile=[(0., 0.), (.01, 0.), (.01, .1), (0., .1), (0., 0.)],
name="SK01 Profile")
"""
usecase = Usecase()
usecase.file = file
usecase.settings = {"profile": profile, "name": name}
return usecase.execute()
class Usecase:
def __init__(self, file, profile=None, name=None):
"""Adds a new arbitrary polyline-based profile
The profile is represented as a polyline defined by a list of
coordinates. Only straight segments are allowed. Coordinates must be
provided in SI meters.
To represent a closed curve, the first and last coordinate must be
identical.
:param profile: A list of coordinates
:type profile: list[list[float]]
:param name: If the profile is semantically significant (i.e. to be
managed and reused by the user) then it must be named. Otherwise,
this may be left as none.
:type name: str, optional
:return: The newly created IfcArbitraryClosedProfileDef
:rtype: ifcopenshell.entity_instance
Example:
.. code:: python
# A 10mm by 100mm rectangle, such that might be used as a wooden
# skirting board or kick plate.
square = ifcopenshell.api.run("profile.add_arbitrary_profile", model,
profile=[(0., 0.), (.01, 0.), (.01, .1), (0., .1), (0., 0.)],
name="SK01 Profile")
"""
self.file = file
self.settings = {"profile": profile, "name": name}
def execute(self):
self.settings["unit_scale"] = ifcopenshell.util.unit.calculate_unit_scale(self.file)
points = [self.convert_si_to_unit(p) for p in self.settings["profile"]]
@@ -19,46 +19,49 @@
import ifcopenshell.util.unit
def add_arbitrary_profile_with_voids(file, outer_profile=None, inner_profiles=None, name=None) -> None:
"""Adds a new arbitrary polyline-based profile with voids
The outer profile is represented as a polyline defined by a list of
coordinates. Only straight segments are allowed. Coordinates must be
provided in SI meters.
To represent a closed curve, the first and last coordinate must be
identical.
The inner profiles are represented as a list of polylines.
Every polyline in defined by a list of coordinates.
Only straight segments are allowed. Coordinates must be
provided in SI meters.
:param outer_profile: A list of coordinates
:type profile: list[float]
:param inner_profiles: A list of polylines
:type profile: list[list[float]]
:param name: If the profile is semantically significant (i.e. to be
managed and reused by the user) then it must be named. Otherwise,
this may be left as none.
:type name: str, optional
:return: The newly created IfcArbitraryProfileDefWithVoids
:rtype: ifcopenshell.entity_instance
Example:
.. code:: python
# A 400mm by 400mm square with a 200mm by 200mm hole in it.
square_with_hole = ifcopenshell.api.run("profile.add_arbitrary_profile_with_voids", model,
outer_profile=[(0., 0.), (.4, 0.), (.4, .4), (0., .4), (0., 0.)],
inner_profiles=[[(0.1, 0.1), (0.3, 0.1), (0.3, 0.3), (0.1, 0.3), (0.1, 0.1)]],
name="SK01 Hole Profile")
"""
usecase = Usecase()
usecase.file = file
usecase.settings = {"outer_profile": outer_profile, "inner_profiles": inner_profiles, "name": name}
return usecase.execute()
class Usecase:
def __init__(self, file, outer_profile=None, inner_profiles=None, name=None):
"""Adds a new arbitrary polyline-based profile with voids
The outer profile is represented as a polyline defined by a list of
coordinates. Only straight segments are allowed. Coordinates must be
provided in SI meters.
To represent a closed curve, the first and last coordinate must be
identical.
The inner profiles are represented as a list of polylines.
Every polyline in defined by a list of coordinates.
Only straight segments are allowed. Coordinates must be
provided in SI meters.
:param outer_profile: A list of coordinates
:type profile: list[float]
:param inner_profiles: A list of polylines
:type profile: list[list[float]]
:param name: If the profile is semantically significant (i.e. to be
managed and reused by the user) then it must be named. Otherwise,
this may be left as none.
:type name: str, optional
:return: The newly created IfcArbitraryProfileDefWithVoids
:rtype: ifcopenshell.entity_instance
Example:
.. code:: python
# A 400mm by 400mm square with a 200mm by 200mm hole in it.
square_with_hole = ifcopenshell.api.run("profile.add_arbitrary_profile_with_voids", model,
outer_profile=[(0., 0.), (.4, 0.), (.4, .4), (0., .4), (0., 0.)],
inner_profiles=[[(0.1, 0.1), (0.3, 0.1), (0.3, 0.3), (0.1, 0.3), (0.1, 0.1)]],
name="SK01 Hole Profile")
"""
self.file = file
self.settings = {"outer_profile": outer_profile, "inner_profiles": inner_profiles, "name": name}
def execute(self):
self.settings["unit_scale"] = ifcopenshell.util.unit.calculate_unit_scale(self.file)
outer_points = [self.convert_si_to_unit(p) for p in self.settings["outer_profile"]]
@@ -69,7 +72,9 @@ class Usecase:
outer_curve = self.file.createIfcPolyline([self.file.createIfcCartesianPoint(p) for p in outer_points])
inner_curves = []
for inner_point in inner_points:
inner_curves.append(self.file.createIfcPolyline([self.file.createIfcCartesianPoint(p) for p in inner_point]))
inner_curves.append(
self.file.createIfcPolyline([self.file.createIfcCartesianPoint(p) for p in inner_point])
)
else:
outer_curve = self.file.createIfcIndexedPolyCurve(self.file.createIfcCartesianPointList3D(outer_points))
inner_curves = []
@@ -17,33 +17,30 @@
# along with IfcOpenShell. If not, see <http://www.gnu.org/licenses/>.
class Usecase:
def __init__(self, file, ifc_class=None):
"""Adds a new parameterised profile
def add_parameterized_profile(file, ifc_class=None) -> None:
"""Adds a new parameterised profile
IFC offers parameterised profiles for common standardised hot roll
steel sections and common concrete forms. A full list is available on
the IFC documentation as subclasses of IfcParameterizedProfileDef.
IFC offers parameterised profiles for common standardised hot roll
steel sections and common concrete forms. A full list is available on
the IFC documentation as subclasses of IfcParameterizedProfileDef.
Currently, this API has no benefit over directly calling
ifcopenshell.file.create_entity.
Currently, this API has no benefit over directly calling
ifcopenshell.file.create_entity.
:param ifc_class: The subclass of IfcParameterizedProfileDef that you'd
like to create.
:type ifc_class: str
:return: The newly created element depending on the specified ifc_class.
:rtype: ifcopenshell.entity_instance
:param ifc_class: The subclass of IfcParameterizedProfileDef that you'd
like to create.
:type ifc_class: str
:return: The newly created element depending on the specified ifc_class.
:rtype: ifcopenshell.entity_instance
Example:
Example:
.. code:: python
.. code:: python
circle = ifcopenshell.api.run("profile.add_parameterized_profile", model,
ifc_class="IfcCircleProfileDef")
circle.Radius = 1.
"""
self.file = file
self.settings = {"ifc_class": ifc_class}
circle = ifcopenshell.api.run("profile.add_parameterized_profile", model,
ifc_class="IfcCircleProfileDef")
circle.Radius = 1.
"""
settings = {"ifc_class": ifc_class}
def execute(self):
return self.file.create_entity(self.settings["ifc_class"])
return file.create_entity(settings["ifc_class"])
@@ -17,34 +17,31 @@
# along with IfcOpenShell. If not, see <http://www.gnu.org/licenses/>.
class Usecase:
def __init__(self, file, profile=None, attributes=None):
"""Edits the attributes of an IfcProfileDef
def edit_profile(file, profile=None, attributes=None) -> None:
"""Edits the attributes of an IfcProfileDef
For more information about the attributes and data types of an
IfcProfileDef, consult the IFC documentation.
For more information about the attributes and data types of an
IfcProfileDef, consult the IFC documentation.
:param profile: The IfcProfileDef entity you want to edit
:type profile: ifcopenshell.entity_instance
:param attributes: a dictionary of attribute names and values.
:type attributes: dict, optional
:return: None
:rtype: None
:param profile: The IfcProfileDef entity you want to edit
:type profile: 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
circle = ifcopenshell.api.run("profile.add_parameterized_profile", model,
ifc_class="IfcCircleProfileDef")
circle = 1.
circle = ifcopenshell.api.run("profile.add_parameterized_profile", model,
ifc_class="IfcCircleProfileDef")
circle = 1.
ifcopenshell.api.run("profile.edit_profile", model,
profile=circle, attributes={"ProfileName": "1000mm Dia"})
"""
self.file = file
self.settings = {"profile": profile, "attributes": attributes or {}}
ifcopenshell.api.run("profile.edit_profile", model,
profile=circle, attributes={"ProfileName": "1000mm Dia"})
"""
settings = {"profile": profile, "attributes": attributes or {}}
def execute(self):
for name, value in self.settings["attributes"].items():
setattr(self.settings["profile"], name, value)
for name, value in settings["attributes"].items():
setattr(settings["profile"], name, value)
@@ -20,32 +20,29 @@ import ifcopenshell
import ifcopenshell.util.element
class Usecase:
def __init__(self, file, profile=None):
"""Removes a profile
def remove_profile(file, profile=None) -> None:
"""Removes a profile
:param profile: The IfcProfileDef to remove.
:type profile: ifcopenshell.entity_instance
:return: None
:rtype: None
:param profile: The IfcProfileDef to remove.
:type profile: ifcopenshell.entity_instance
:return: None
:rtype: None
Example:
Example:
.. code:: python
.. code:: python
circle = ifcopenshell.api.run("profile.add_parameterized_profile", model,
ifc_class="IfcCircleProfileDef")
circle = 1.
ifcopenshell.api.run("profile.remove_profile", model, profile=circle)
"""
self.file = file
self.settings = {"profile": profile}
circle = ifcopenshell.api.run("profile.add_parameterized_profile", model,
ifc_class="IfcCircleProfileDef")
circle = 1.
ifcopenshell.api.run("profile.remove_profile", model, profile=circle)
"""
settings = {"profile": profile}
def execute(self):
subelements = set()
for attribute in self.settings["profile"]:
if isinstance(attribute, ifcopenshell.entity_instance):
subelements.add(attribute)
self.file.remove(self.settings["profile"])
for subelement in subelements:
ifcopenshell.util.element.remove_deep2(self.file, subelement)
subelements = set()
for attribute in settings["profile"]:
if isinstance(attribute, ifcopenshell.entity_instance):
subelements.add(attribute)
file.remove(settings["profile"])
for subelement in subelements:
ifcopenshell.util.element.remove_deep2(file, subelement)