Skip to content

Commit feb5af6

Browse files
AlejandroFernandezLucespyansys-ci-botRobPasMue
authored
fix: Trame issues (#1148)
Co-authored-by: pyansys-ci-bot <pyansys.github.bot@ansys.com> Co-authored-by: Roberto Pastor Muela <37798125+RobPasMue@users.noreply.github.com>
1 parent fb35396 commit feb5af6

File tree

3 files changed

+79
-17
lines changed

3 files changed

+79
-17
lines changed

doc/changelog.d/1148.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fix: Trame issues

src/ansys/geometry/core/plotting/plotter_helper.py

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -252,16 +252,38 @@ def disable_picking(self):
252252
"""Disable picking capabilities in the plotter."""
253253
self._pl.scene.disable_picking()
254254

255-
def add(self, object: Any, **plotting_options):
255+
def add(
256+
self,
257+
object: Any,
258+
merge_bodies: bool = False,
259+
merge_component: bool = False,
260+
filter: str = None,
261+
**plotting_options,
262+
):
256263
"""
257264
Add a ``pyansys-geometry`` or ``PyVista`` object to the plotter.
258265
259266
Parameters
260267
----------
261268
object : Any
262-
Object you want to show.
269+
Object or list of objects you want to show.
270+
merge_bodies : bool, default: False
271+
Whether to merge each body into a single dataset. When ``True``,
272+
all the faces of each individual body are effectively combined
273+
into a single dataset without separating faces.
274+
merge_component : bool, default: False
275+
Whether to merge this component into a single dataset. When ``True``,
276+
all the individual bodies are effectively combined into a single
277+
dataset without any hierarchy.
278+
**plotting_options : dict, default: None
279+
Keyword arguments. For allowable keyword arguments, see the
280+
:meth:`Plotter.add_mesh <pyvista.Plotter.add_mesh>` method.
263281
"""
264-
self._pl.add(object=object, **plotting_options)
282+
if isinstance(object, List) and not isinstance(object[0], pv.PolyData):
283+
logger.debug("Plotting objects in list...")
284+
self._pl.add_list(object, merge_bodies, merge_component, filter, **plotting_options)
285+
else:
286+
self._pl.add(object, merge_bodies, merge_component, filter, **plotting_options)
265287

266288
def plot(
267289
self,
@@ -306,11 +328,14 @@ def plot(
306328
List[Any]
307329
List with the picked bodies in the picked order.
308330
"""
309-
if isinstance(object, List) and not isinstance(object[0], pv.PolyData):
310-
logger.debug("Plotting objects in list...")
311-
self._pl.add_list(object, merge_bodies, merge_component, filter, **plotting_options)
312-
else:
313-
self._pl.add(object, merge_bodies, merge_component, filter, **plotting_options)
331+
self.add(
332+
object=object,
333+
merge_bodies=merge_bodies,
334+
merge_component=merge_component,
335+
filter=filter,
336+
**plotting_options,
337+
)
338+
314339
if self._pl.geom_object_actors_map:
315340
self._geom_object_actors_map = self._pl.geom_object_actors_map
316341
else:

src/ansys/geometry/core/plotting/trame_gui.py

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,19 @@
2020
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
# SOFTWARE.
2222
"""Module for using `trame <https://kitware.github.io/trame/index.html>`_ for visualization."""
23+
from beartype.typing import TYPE_CHECKING, Union
24+
import pyvista as pv
25+
26+
from ansys.geometry.core import LOG as logger
27+
from ansys.geometry.core.plotting.plotter import Plotter
28+
29+
if TYPE_CHECKING:
30+
from ansys.geometry.core.plotting.plotter_helper import PlotterHelper
31+
2332
try:
2433
from pyvista.trame.ui import plotter_ui
2534
from trame.app import get_server
26-
from trame.ui.vuetify import SinglePageLayout
35+
from trame.ui import vuetify, vuetify3
2736

2837
_HAS_TRAME = True
2938

@@ -32,36 +41,63 @@
3241

3342

3443
class TrameVisualizer:
35-
"""Defines the trame layout view."""
44+
"""
45+
Defines the trame layout view.
3646
37-
def __init__(self) -> None:
47+
Parameters
48+
----------
49+
client_type : str, optional
50+
Client type to use for the trame server. Options are 'vue2' and 'vue3'.
51+
Default is 'vue3'.
52+
"""
53+
54+
def __init__(self, client_type: str = "vue3") -> None:
3855
"""Initialize the trame server and server-related variables."""
3956
if not _HAS_TRAME: # pragma: no cover
4057
raise ModuleNotFoundError(
4158
"The package 'pyvista[trame]' is required to use this function."
4259
)
60+
self._client_type = client_type
61+
if client_type not in ["vue2", "vue3"]:
62+
self._client_type = "vue3"
63+
logger.warning("Invalid client type {}. Defaulting to 'vue3'.", client_type)
4364

44-
self.server = get_server()
65+
self.server = get_server(client_type=client_type)
4566
self.state, self.ctrl = self.server.state, self.server.controller
4667

47-
def set_scene(self, plotter):
68+
def set_scene(self, plotter: Union["pv.Plotter", Plotter, "PlotterHelper"]):
4869
"""
4970
Set the trame layout view and the mesh to show through the PyVista plotter.
5071
5172
Parameters
5273
----------
53-
plotter : ~pyvista.Plotter
54-
PyVista plotter with the rendered mesh.
74+
plotter : Union[~pyvista.Plotter, ~Plotter, ~PlotterHelper]
75+
PyVista plotter or PyAnsys plotter to render.
5576
"""
77+
from ansys.geometry.core.plotting.plotter_helper import PlotterHelper
78+
79+
if isinstance(plotter, PlotterHelper):
80+
pv_plotter = plotter._pl.scene
81+
elif isinstance(plotter, Plotter):
82+
pv_plotter = plotter.scene
83+
elif isinstance(plotter, pv.Plotter):
84+
pv_plotter = plotter
85+
else:
86+
logger.warning("Invalid plotter type. Expected PyVista plotter or PyAnsys plotter.")
87+
5688
self.state.trame__title = "PyAnsys Geometry Viewer"
89+
if self._client_type == "vue3":
90+
page_layout = vuetify3.SinglePageLayout(self.server)
91+
else:
92+
page_layout = vuetify.SinglePageLayout(self.server)
5793

58-
with SinglePageLayout(self.server) as layout:
94+
with page_layout as layout:
5995
layout.icon.click = self.ctrl.view_reset_camera
6096
layout.title.set_text("PyAnsys Geometry")
6197

6298
with layout.content:
6399
# Use PyVista UI template for Plotters
64-
view = plotter_ui(plotter.scene)
100+
view = plotter_ui(pv_plotter)
65101
self.ctrl.view_update = view.update
66102

67103
# hide footer with trame watermark

0 commit comments

Comments
 (0)