Skip to content

Commit 845ac89

Browse files
committed
Combine tessellate calls into one method. Other minor suggested changes.
1 parent b28443e commit 845ac89

File tree

2 files changed

+47
-122
lines changed

2 files changed

+47
-122
lines changed

src/ansys/geometry/core/designer/body.py

+43-121
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ def copy(self, parent: "Component", name: str = None) -> "Body":
566566
return
567567

568568
@abstractmethod
569-
def tessellate(self, merge: bool = False) -> Union["PolyData", "MultiBlock"]:
569+
def tessellate(self, merge: bool = False, tessellationOptions: TessellationOptions = None) -> Union["PolyData", "MultiBlock"]:
570570
"""Tessellate the body and return the geometry as triangles.
571571
572572
Parameters
@@ -575,6 +575,8 @@ def tessellate(self, merge: bool = False) -> Union["PolyData", "MultiBlock"]:
575575
Whether to merge the body into a single mesh. When ``False`` (default), the
576576
number of triangles are preserved and only the topology is merged.
577577
When ``True``, the individual faces of the tessellation are merged.
578+
tessellationOptions : TessellationOptions, default: None
579+
A set of options to determine the tessellation quality.
578580
579581
Returns
580582
-------
@@ -620,42 +622,6 @@ def tessellate(self, merge: bool = False) -> Union["PolyData", "MultiBlock"]:
620622
"""
621623
return
622624

623-
@abstractmethod
624-
def tessellate_with_options(
625-
self,
626-
surf_deviation: Real,
627-
ang_deviation: Real,
628-
aspect_ratio: Real = 0.0,
629-
edge_length: Real = 0.0,
630-
watertight: bool = False,
631-
merge: bool = False,
632-
) -> Union["PolyData", "MultiBlock"]:
633-
"""Tessellate the body and return the geometry as triangles.
634-
635-
Parameters
636-
----------
637-
surf_deviation : Real
638-
The maximum deviation from the true surface position.
639-
ang_deviation : Real
640-
The maximum deviation from the true surface normal.
641-
aspect_ratio : Real, default: 0.0
642-
The maximum aspect ratio of facets.
643-
edge_length : Real, default: 0.0
644-
The maximum facet edge length.
645-
watertight : bool, default: False
646-
Whether triangles on opposite sides of an edge match.
647-
merge : bool, default: False
648-
Whether to merge the body into a single mesh. When ``False`` (default), the
649-
number of triangles are preserved and only the topology is merged.
650-
When ``True``, the individual faces of the tessellation are merged.
651-
652-
Returns
653-
-------
654-
~pyvista.PolyData, ~pyvista.MultiBlock
655-
Merged :class:`pyvista.PolyData` if ``merge=True`` or a composite dataset.
656-
"""
657-
return
658-
659625
@abstractmethod
660626
def shell_body(self, offset: Real) -> bool:
661627
"""Shell the body to the thickness specified.
@@ -1304,7 +1270,7 @@ def copy(self, parent: "Component", name: str = None) -> "Body": # noqa: D102
13041270
@protect_grpc
13051271
@graphics_required
13061272
def tessellate( # noqa: D102
1307-
self, merge: bool = False, transform: Matrix44 = IDENTITY_MATRIX44
1273+
self, merge: bool = False, tessellationOptions: TessellationOptions = None, transform: Matrix44 = IDENTITY_MATRIX44
13081274
) -> Union["PolyData", "MultiBlock"]:
13091275
# lazy import here to improve initial module load time
13101276
import pyvista as pv
@@ -1316,69 +1282,45 @@ def tessellate( # noqa: D102
13161282

13171283
# cache tessellation
13181284
if not self._tessellation:
1319-
resp = self._bodies_stub.GetTessellation(self._grpc_id)
1320-
self._tessellation = {
1321-
str(face_id): tess_to_pd(face_tess)
1322-
for face_id, face_tess in resp.face_tessellation.items()
1323-
}
1324-
1325-
pdata = [tess.transform(transform, inplace=False) for tess in self._tessellation.values()]
1326-
comp = pv.MultiBlock(pdata)
1327-
1328-
if merge:
1329-
ugrid = comp.combine()
1330-
return pv.PolyData(var_inp=ugrid.points, faces=ugrid.cells)
1331-
else:
1332-
return comp
1333-
1334-
@protect_grpc
1335-
@graphics_required
1336-
@check_input_types
1337-
@min_backend_version(25, 2, 0)
1338-
def tessellate_with_options( # noqa: D102
1339-
self,
1340-
surf_deviation: Real,
1341-
ang_deviation: Real,
1342-
aspect_ratio: Real = 0.0,
1343-
edge_length: Real = 0.0,
1344-
watertight: bool = False,
1345-
merge: bool = False,
1346-
transform: Matrix44 = IDENTITY_MATRIX44,
1347-
) -> Union["PolyData", "MultiBlock"]:
1348-
# lazy import here to improve initial module load time
1349-
import pyvista as pv
1350-
1351-
if not self.is_alive:
1352-
return pv.PolyData() if merge else pv.MultiBlock()
1353-
1354-
self._grpc_client.log.debug(f"Requesting tessellation for body {self.id}.")
1355-
1356-
request = GetTessellationRequest(
1357-
id=self._grpc_id,
1358-
options=TessellationOptions(
1359-
surface_deviation=surf_deviation,
1360-
angle_deviation=ang_deviation,
1361-
maximum_aspect_ratio=aspect_ratio,
1362-
maximum_edge_length=edge_length,
1363-
watertight=watertight,
1364-
),
1365-
)
1366-
1367-
# cache tessellation
1368-
if not self._tessellation:
1369-
try:
1370-
resp = self._bodies_stub.GetTessellationWithOptions(request)
1371-
self._tessellation = {
1372-
str(face_id): tess_to_pd(face_tess)
1373-
for face_id, face_tess in resp.face_tessellation.items()
1374-
}
1375-
except Exception:
1376-
tessellation_map = {}
1377-
for response in self._bodies_stub.GetTessellationStream(request):
1378-
for key, value in response.face_tessellation.items():
1379-
tessellation_map[key] = tess_to_pd(value)
1285+
if tessellationOptions is not None:
1286+
request = GetTessellationRequest(
1287+
id=self._grpc_id,
1288+
options=TessellationOptions(
1289+
surface_deviation=tessellationOptions.surface_deviation,
1290+
angle_deviation=tessellationOptions.angle_deviation,
1291+
maximum_aspect_ratio=tessellationOptions.maximum_aspect_ratio,
1292+
maximum_edge_length=tessellationOptions.maximum_edge_length,
1293+
watertight=tessellationOptions.watertight,
1294+
),
1295+
)
1296+
try:
1297+
resp = self._bodies_stub.GetTessellationWithOptions(request)
1298+
self._tessellation = {
1299+
str(face_id): tess_to_pd(face_tess)
1300+
for face_id, face_tess in resp.face_tessellation.items()
1301+
}
1302+
except Exception:
1303+
tessellation_map = {}
1304+
for response in self._bodies_stub.GetTessellationStream(request):
1305+
for key, value in response.face_tessellation.items():
1306+
tessellation_map[key] = tess_to_pd(value)
13801307

13811308
self._tessellation = tessellation_map
1309+
else:
1310+
try:
1311+
resp = self._bodies_stub.GetTessellation(self._grpc_id)
1312+
self._tessellation = {
1313+
str(face_id): tess_to_pd(face_tess)
1314+
for face_id, face_tess in resp.face_tessellation.items()
1315+
}
1316+
except Exception:
1317+
tessellation_map = {}
1318+
request = GetTessellationRequest(self._grpc_id)
1319+
for response in self._bodies_stub.GetTessellationStream(request):
1320+
for key, value in response.face_tessellation.items():
1321+
tessellation_map[key] = tess_to_pd(value)
1322+
1323+
self._tessellation = tessellation_map
13821324

13831325
pdata = [tess.transform(transform, inplace=False) for tess in self._tessellation.values()]
13841326
comp = pv.MultiBlock(pdata)
@@ -1906,29 +1848,9 @@ def copy(self, parent: "Component", name: str = None) -> "Body": # noqa: D102
19061848

19071849
@ensure_design_is_active
19081850
def tessellate( # noqa: D102
1909-
self, merge: bool = False
1910-
) -> Union["PolyData", "MultiBlock"]:
1911-
return self._template.tessellate(merge, self.parent_component.get_world_transform())
1912-
1913-
@ensure_design_is_active
1914-
def tessellate_with_options( # noqa: D102
1915-
self,
1916-
surf_deviation: Real,
1917-
ang_deviation: Real,
1918-
aspect_ratio: Real = 0.0,
1919-
edge_length: Real = 0.0,
1920-
watertight: bool = False,
1921-
merge: bool = False,
1851+
self, merge: bool = False, tessellationOptions: TessellationOptions = None
19221852
) -> Union["PolyData", "MultiBlock"]:
1923-
return self._template.tessellate_with_options(
1924-
surf_deviation,
1925-
ang_deviation,
1926-
aspect_ratio,
1927-
edge_length,
1928-
watertight,
1929-
merge,
1930-
self.parent_component.get_world_transform(),
1931-
)
1853+
return self._template.tessellate(merge, tessellationOptions, self.parent_component.get_world_transform())
19321854

19331855
@ensure_design_is_active
19341856
def shell_body(self, offset: Real) -> bool: # noqa: D102

src/ansys/geometry/core/designer/design.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,10 @@ def download(
308308
if self._modeler.client.backend_version < (25, 2, 0):
309309
received_bytes = self.__export_and_download_legacy(format=format)
310310
else:
311-
received_bytes = self.__export_and_download(format=format)
311+
try:
312+
received_bytes = self.__export_and_download(format=format)
313+
except Exception:
314+
received_bytes = self.__export_and_download_stream(format=format)
312315

313316
# Write to file
314317
file_location.write_bytes(received_bytes)

0 commit comments

Comments
 (0)