typing for pythonocc types

This commit is contained in:
Andrej730
2024-07-15 15:00:59 +05:00
parent eff778d5be
commit 54cc4192bd
2 changed files with 36 additions and 16 deletions
@@ -28,7 +28,10 @@ from ..entity_instance import entity_instance
from . import has_occ from . import has_occ
from typing import TypeVar, Union, Optional, Generator, Any, Literal, overload from typing import TypeVar, Union, Optional, Generator, Any, Literal, overload, TYPE_CHECKING
if TYPE_CHECKING:
from OCC.Core import TopoDS
T = TypeVar("T") T = TypeVar("T")
ShapeElementType = Union[ ShapeElementType = Union[
@@ -356,7 +359,7 @@ def create_shape(
inst: entity_instance, inst: entity_instance,
repr: Optional[entity_instance] = None, repr: Optional[entity_instance] = None,
geometry_library: GEOMETRY_LIBRARY = "opencascade", geometry_library: GEOMETRY_LIBRARY = "opencascade",
) -> Union[ShapeType, ShapeElementType, ifcopenshell_wrapper.Transformation]: ) -> Union[ShapeType, ShapeElementType, ifcopenshell_wrapper.Transformation, utils.shape_tuple, TopoDS.TopoDS_Shape]:
""" """
Return a geometric representation from STEP-based IFCREPRESENTATIONSHAPE Return a geometric representation from STEP-based IFCREPRESENTATIONSHAPE
or or
@@ -368,14 +371,18 @@ def create_shape(
:raises RuntimeError: If failed to process shape. You can turn detailed logging to get more details. :raises RuntimeError: If failed to process shape. You can turn detailed logging to get more details.
:return: :return:
- `inst` is IfcProduct and `repr` provided / None -> ShapeElementType - `inst` is IfcProduct and `repr` provided / None -> ShapeElementType\n
- `inst` is IfcRepresentation and `repr` is None -> ShapeType - `inst` is IfcRepresentation and `repr` is None -> ShapeType\n
- `inst` is IfcRepresentationItem and `repr` is None -> ShapeType - `inst` is IfcRepresentationItem and `repr` is None -> ShapeType\n
- `inst` is IfcProfileDef and `repr` is None -> ShapeType - `inst` is IfcProfileDef and `repr` is None -> ShapeType\n
- `inst` is IfcPlacement / IfcObjectPlacement -> Transformation - `inst` is IfcPlacement / IfcObjectPlacement -> Transformation\n
- `inst` is IfcTypeProduct and `repr` is None -> None - `inst` is IfcTypeProduct and `repr` is None -> None\n
- `inst` is IfcTypeProduct and `repr` is provided -> RuntimeError - `inst` is IfcTypeProduct and `repr` is provided -> RuntimeError
(for IfcTypeProducts provide just IfcRepresentation as `inst`). (for IfcTypeProducts provide just IfcRepresentation as `inst`).\n
If 'use-python-opencascade' is enabled in settings then\n
- instead of ShapeElementType it returns shape_tuple, \n
- instead of ShapeType it returns TopoDS.TopoDS_Shape.
Example: Example:
@@ -17,11 +17,14 @@
# along with IfcOpenShell. If not, see <http://www.gnu.org/licenses/>. # along with IfcOpenShell. If not, see <http://www.gnu.org/licenses/>.
from __future__ import annotations
import random import random
import operator import operator
import warnings import warnings
import ifcopenshell.ifcopenshell_wrapper as ifcopenshell_wrapper
from collections import namedtuple from typing import NamedTuple, Any, Union
from typing_extensions import assert_never
from collections.abc import Iterable from collections.abc import Iterable
import OCC import OCC
@@ -35,7 +38,13 @@ except ImportError:
USE_OCCT_HANDLE = True USE_OCCT_HANDLE = True
shape_tuple = namedtuple("shape_tuple", ("data", "geometry", "styles", "style_ids"))
class shape_tuple(NamedTuple):
data: Union[ifcopenshell_wrapper.SerializedElement, ifcopenshell_wrapper.Serialization]
geometry: TopoDS.TopoDS_Shape
styles: tuple[tuple[float, float, float, float], ...]
style_ids: tuple[int, ...]
handle, main_loop, add_menu, add_function_to_menu = None, None, None, None handle, main_loop, add_menu, add_function_to_menu = None, None, None, None
@@ -217,22 +226,26 @@ def serialize_shape(shape):
return shapes.WriteToString() return shapes.WriteToString()
def create_shape_from_serialization(brep_object): def create_shape_from_serialization(
brep_object: Union[ifcopenshell_wrapper.SerializedElement, ifcopenshell_wrapper.Serialization]
) -> Union[shape_tuple, TopoDS.TopoDS_Shape]:
brep_data, occ_shape, styles, style_ids = None, None, (), () brep_data, occ_shape, styles, style_ids = None, None, (), ()
is_product_shape = True is_product_shape = True
try: if isinstance(brep_object, ifcopenshell_wrapper.SerializedElement):
brep_data = brep_object.geometry.brep_data brep_data = brep_object.geometry.brep_data
styles = brep_object.geometry.surface_styles styles = brep_object.geometry.surface_styles
style_ids = brep_object.geometry.surface_style_ids style_ids = brep_object.geometry.surface_style_ids
except BaseException: elif isinstance(brep_object, ifcopenshell_wrapper.Serialization):
try: try:
brep_data = brep_object.brep_data brep_data = brep_object.brep_data
styles = brep_object.surface_styles styles = brep_object.surface_styles
style_ids = brep_object.surface_style_ids style_ids = brep_object.surface_style_ids
is_product_shape = False is_product_shape = False
except BaseException: except BaseException as e:
pass print("Error occurred creating a shape:", e)
else:
assert_never(brep_object)
styles = tuple(styles[i : i + 4] for i in range(0, len(styles), 4)) styles = tuple(styles[i : i + 4] for i in range(0, len(styles), 4))