|
5 | 5 | from pyvista.plotting.tools import create_axes_marker |
6 | 6 |
|
7 | 7 | from ansys.geometry.core.designer import Body, Component |
| 8 | +from ansys.geometry.core.logger import LOG as logger |
8 | 9 | from ansys.geometry.core.math import Frame, Plane |
| 10 | +from ansys.geometry.core.plotting.trame_gui import _HAS_TRAME, TrameVisualizer |
9 | 11 | from ansys.geometry.core.plotting.widgets import ( |
10 | 12 | CameraPanDirection, |
11 | 13 | DisplacementArrow, |
|
18 | 20 |
|
19 | 21 |
|
20 | 22 | class Plotter: |
21 | | - """Provides for plotting sketches and bodies.""" |
| 23 | + """Provides for plotting sketches and bodies. |
| 24 | +
|
| 25 | + Parameters |
| 26 | + ---------- |
| 27 | + scene : ~pyvista.Plotter, default: None |
| 28 | + Scene instance for rendering the objects. |
| 29 | + color_opts : dict, default: None |
| 30 | + Dictionary containing the background and top colors. |
| 31 | + num_points : int, default: 100 |
| 32 | + Number of points to use to render the shapes. |
| 33 | + enable_widgets: bool, default: True |
| 34 | + Enables/disables widget buttons in the plotter window. |
| 35 | + They must be disabled for trame viewer. |
| 36 | + """ |
22 | 37 |
|
23 | 38 | def __init__( |
24 | 39 | self, |
25 | 40 | scene: Optional[pv.Plotter] = None, |
26 | | - background_opts: Optional[Dict] = None, |
| 41 | + color_opts: Optional[Dict] = None, |
27 | 42 | num_points: int = 100, |
| 43 | + enable_widgets: bool = True, |
28 | 44 | ): |
29 | | - """Initializes the plotter. |
| 45 | + """Initializes the plotter.""" |
30 | 46 |
|
31 | | - Parameters |
32 | | - ---------- |
33 | | - scene : ~pyvista.Plotter, default: None |
34 | | - Scene instance for rendering the objects. |
35 | | - background_opts : dict, default: None |
36 | | - Dictionary containing the background and top colors. |
37 | | - num_points : int, default: 100 |
38 | | - Number of points to use to render the shapes. |
39 | | - """ |
40 | 47 | # Generate custom scene if ``None`` is provided |
41 | 48 | if scene is None: |
42 | 49 | scene = pv.Plotter() |
43 | 50 |
|
44 | 51 | # If required, use a white background with no gradient |
45 | | - if not background_opts: |
46 | | - background_opts = dict(color="white") |
| 52 | + if not color_opts: |
| 53 | + color_opts = dict(color="white") |
47 | 54 |
|
48 | 55 | # Create the scene |
49 | 56 | self._scene = scene |
50 | 57 | # Scene: assign the background |
51 | | - self._scene.set_background(**background_opts) |
| 58 | + self._scene.set_background(**color_opts) |
52 | 59 | view_box = self._scene.add_axes(line_width=5, color="black") |
53 | 60 |
|
54 | 61 | # Save the desired number of points |
55 | 62 | self._num_points = num_points |
56 | 63 |
|
57 | 64 | # Create Plotter widgets |
58 | | - self._widgets: List[PlotterWidget] = [] |
59 | | - self._widgets.append(Ruler(self._scene)) |
60 | | - [ |
61 | | - self._widgets.append(DisplacementArrow(self._scene, direction=dir)) |
62 | | - for dir in CameraPanDirection |
63 | | - ] |
64 | | - [self._widgets.append(ViewButton(self._scene, direction=dir)) for dir in ViewDirection] |
| 65 | + if enable_widgets: |
| 66 | + self._widgets: List[PlotterWidget] = [] |
| 67 | + self._widgets.append(Ruler(self._scene)) |
| 68 | + [ |
| 69 | + self._widgets.append(DisplacementArrow(self._scene, direction=dir)) |
| 70 | + for dir in CameraPanDirection |
| 71 | + ] |
| 72 | + [self._widgets.append(ViewButton(self._scene, direction=dir)) for dir in ViewDirection] |
65 | 73 |
|
66 | 74 | @property |
67 | 75 | def scene(self) -> pv.Plotter: |
@@ -322,3 +330,68 @@ def __set_add_mesh_defaults(self, plotting_options: Optional[Dict]) -> None: |
322 | 330 | # This method should only be applied in 3D objects: bodies, components |
323 | 331 | plotting_options.setdefault("smooth_shading", True) |
324 | 332 | plotting_options.setdefault("color", "#D6F7D1") |
| 333 | + |
| 334 | + |
| 335 | +class PlotterHelper: |
| 336 | + """This class simplifies the selection of Trame visualizer in plot() |
| 337 | + functions. |
| 338 | +
|
| 339 | + Parameters |
| 340 | + ---------- |
| 341 | + use_trame: bool, optional |
| 342 | + Enables/disables the usage of the trame web visualizer. Defaults to the |
| 343 | + global setting ``USE_TRAME``. |
| 344 | + """ |
| 345 | + |
| 346 | + def __init__(self, use_trame: Optional[bool] = None) -> None: |
| 347 | + """Initializes use_trame and saves current pv.OFF_SCREEN value.""" |
| 348 | + # Check if the use of trame was requested |
| 349 | + if use_trame is None: |
| 350 | + import ansys.geometry.core as pygeom |
| 351 | + |
| 352 | + use_trame = pygeom.USE_TRAME |
| 353 | + |
| 354 | + self._use_trame = use_trame |
| 355 | + self._pv_off_screen_original = bool(pv.OFF_SCREEN) |
| 356 | + |
| 357 | + def init_plotter(self): |
| 358 | + """Initializes the plotter with or without trame visualizer. |
| 359 | +
|
| 360 | + Returns |
| 361 | + ------- |
| 362 | + Plotter |
| 363 | + PyGeometry plotter initialized. |
| 364 | + """ |
| 365 | + if self._use_trame and _HAS_TRAME: |
| 366 | + # avoids GUI window popping up |
| 367 | + pv.OFF_SCREEN = True |
| 368 | + pl = Plotter(enable_widgets=False) |
| 369 | + elif self._use_trame and not _HAS_TRAME: |
| 370 | + warn_msg = ( |
| 371 | + "'use_trame' is active but Trame dependencies are not installed." |
| 372 | + "Consider installing 'pyvista[trame]' to use this functionality." |
| 373 | + ) |
| 374 | + logger.warning(warn_msg) |
| 375 | + pl = Plotter() |
| 376 | + else: |
| 377 | + pl = Plotter() |
| 378 | + return pl |
| 379 | + |
| 380 | + def show_plotter(self, plotter, screenshot): |
| 381 | + """Shows the plotter or starts Trame service. |
| 382 | +
|
| 383 | + Parameters |
| 384 | + ---------- |
| 385 | + plotter : Plotter |
| 386 | + PyGeometry plotter with the meshes added. |
| 387 | + screenshot : str, default: None |
| 388 | + Save a screenshot of the image being represented. The image is |
| 389 | + stored in the path provided as an argument. |
| 390 | + """ |
| 391 | + if self._use_trame and _HAS_TRAME: |
| 392 | + visualizer = TrameVisualizer() |
| 393 | + visualizer.set_scene(plotter) |
| 394 | + visualizer.show() |
| 395 | + else: |
| 396 | + plotter.show(screenshot=screenshot) |
| 397 | + pv.OFF_SCREEN = self._pv_off_screen_original |
0 commit comments