|
23 | 23 |
|
24 | 24 | from enum import Enum, unique |
25 | 25 | from pathlib import Path |
26 | | -from typing import Union |
| 26 | +from typing import TYPE_CHECKING, Union |
27 | 27 |
|
28 | 28 | from ansys.api.dbu.v0.dbumodels_pb2 import EntityIdentifier |
29 | 29 | from ansys.api.geometry.v0.commands_pb2 import ( |
|
71 | 71 | from ansys.geometry.core.math.plane import Plane |
72 | 72 | from ansys.geometry.core.math.point import Point3D |
73 | 73 | from ansys.geometry.core.math.vector import UnitVector3D, Vector3D |
74 | | -from ansys.geometry.core.misc.checks import ensure_design_is_active, min_backend_version |
| 74 | +from ansys.geometry.core.misc.checks import ( |
| 75 | + ensure_design_is_active, |
| 76 | + graphics_required, |
| 77 | + min_backend_version, |
| 78 | +) |
75 | 79 | from ansys.geometry.core.misc.measurements import DEFAULT_UNITS, Distance |
76 | | -from ansys.geometry.core.misc.options import ImportOptions |
| 80 | +from ansys.geometry.core.misc.options import ImportOptions, TessellationOptions |
77 | 81 | from ansys.geometry.core.modeler import Modeler |
78 | 82 | from ansys.geometry.core.parameters.parameter import Parameter, ParameterUpdateStatus |
79 | 83 | from ansys.geometry.core.shapes.curves.trimmed_curve import TrimmedCurve |
80 | 84 | from ansys.geometry.core.shapes.parameterization import Interval, ParamUV |
81 | 85 | from ansys.geometry.core.typing import RealSequence |
82 | 86 |
|
| 87 | +if TYPE_CHECKING: # pragma: no cover |
| 88 | + from pyvista import MultiBlock, PolyData |
| 89 | + |
83 | 90 |
|
84 | 91 | @unique |
85 | 92 | class DesignFileFormat(Enum): |
@@ -139,6 +146,7 @@ def __init__(self, name: str, modeler: Modeler, read_existing_design: bool = Fal |
139 | 146 | self._design_id = "" |
140 | 147 | self._is_active = False |
141 | 148 | self._modeler = modeler |
| 149 | + self._tessellation = None |
142 | 150 |
|
143 | 151 | # Check whether we want to process an existing design or create a new one. |
144 | 152 | if read_existing_design: |
@@ -1028,6 +1036,51 @@ def insert_file( |
1028 | 1036 |
|
1029 | 1037 | # Return the newly inserted component |
1030 | 1038 | return self._components[-1] |
| 1039 | + |
| 1040 | + @min_backend_version(26, 1, 0) |
| 1041 | + @check_input_types |
| 1042 | + @graphics_required |
| 1043 | + def tessellate( |
| 1044 | + self, merge: bool = False, tess_options: TessellationOptions | None = None |
| 1045 | + ) -> Union["PolyData", "MultiBlock"]: |
| 1046 | + """Tessellate the entire design and return the geometry as triangles. |
| 1047 | +
|
| 1048 | + Parameters |
| 1049 | + ---------- |
| 1050 | + merge : bool, default: False |
| 1051 | + Whether to merge all bodies into a single mesh. |
| 1052 | + tess_options : TessellationOptions, optional |
| 1053 | + Options for the tessellation. If None, default options are used. |
| 1054 | +
|
| 1055 | + Returns |
| 1056 | + ------- |
| 1057 | + ~pyvista.PolyData | ~pyvista.MultiBlock |
| 1058 | + The tessellated mesh. If `merge` is True, a single PolyData is returned. |
| 1059 | + Otherwise, a MultiBlock is returned with each block corresponding to a body. |
| 1060 | + """ |
| 1061 | + import pyvista as pv |
| 1062 | + |
| 1063 | + if not self.is_alive: |
| 1064 | + return pv.PolyData() if merge else pv.MultiBlock() |
| 1065 | + |
| 1066 | + self._grpc_client.log.debug(f"Requesting tessellation for body {self.id}.") |
| 1067 | + |
| 1068 | + # cache tessellation |
| 1069 | + if not self._tessellation: |
| 1070 | + response = self._grpc_client.services.designs.stream_design_tessellation( |
| 1071 | + options=tess_options, |
| 1072 | + ) |
| 1073 | + |
| 1074 | + self._tessellation = response.get("tessellation") |
| 1075 | + |
| 1076 | + pdata = [tess for tess in self._tessellation.values()] |
| 1077 | + comp = pv.MultiBlock(pdata) |
| 1078 | + |
| 1079 | + if merge: |
| 1080 | + ugrid = comp.combine() |
| 1081 | + return pv.PolyData(var_inp=ugrid.points, faces=ugrid.cells) |
| 1082 | + else: |
| 1083 | + return comp |
1031 | 1084 |
|
1032 | 1085 | def __repr__(self) -> str: |
1033 | 1086 | """Represent the ``Design`` as a string.""" |
|
0 commit comments