Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/changelog.d/1148.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fix: Trame issues
41 changes: 33 additions & 8 deletions src/ansys/geometry/core/plotting/plotter_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,16 +252,38 @@ def disable_picking(self):
"""Disable picking capabilities in the plotter."""
self._pl.scene.disable_picking()

def add(self, object: Any, **plotting_options):
def add(
self,
object: Any,
merge_bodies: bool = False,
merge_component: bool = False,
filter: str = None,
**plotting_options,
):
"""
Add a ``pyansys-geometry`` or ``PyVista`` object to the plotter.

Parameters
----------
object : Any
Object you want to show.
Object or list of objects you want to show.
merge_bodies : bool, default: False
Whether to merge each body into a single dataset. When ``True``,
all the faces of each individual body are effectively combined
into a single dataset without separating faces.
merge_component : bool, default: False
Whether to merge this component into a single dataset. When ``True``,
all the individual bodies are effectively combined into a single
dataset without any hierarchy.
**plotting_options : dict, default: None
Keyword arguments. For allowable keyword arguments, see the
:meth:`Plotter.add_mesh <pyvista.Plotter.add_mesh>` method.
"""
self._pl.add(object=object, **plotting_options)
if isinstance(object, List) and not isinstance(object[0], pv.PolyData):
logger.debug("Plotting objects in list...")
self._pl.add_list(object, merge_bodies, merge_component, filter, **plotting_options)
else:
self._pl.add(object, merge_bodies, merge_component, filter, **plotting_options)

def plot(
self,
Expand Down Expand Up @@ -306,11 +328,14 @@ def plot(
List[Any]
List with the picked bodies in the picked order.
"""
if isinstance(object, List) and not isinstance(object[0], pv.PolyData):
logger.debug("Plotting objects in list...")
self._pl.add_list(object, merge_bodies, merge_component, filter, **plotting_options)
else:
self._pl.add(object, merge_bodies, merge_component, filter, **plotting_options)
self.add(
object=object,
merge_bodies=merge_bodies,
merge_component=merge_component,
filter=filter,
**plotting_options,
)

if self._pl.geom_object_actors_map:
self._geom_object_actors_map = self._pl.geom_object_actors_map
else:
Expand Down
54 changes: 45 additions & 9 deletions src/ansys/geometry/core/plotting/trame_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,19 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""Module for using `trame <https://kitware.github.io/trame/index.html>`_ for visualization."""
from beartype.typing import TYPE_CHECKING, Union
import pyvista as pv

from ansys.geometry.core import LOG as logger
from ansys.geometry.core.plotting.plotter import Plotter

if TYPE_CHECKING:
from ansys.geometry.core.plotting.plotter_helper import PlotterHelper

try:
from pyvista.trame.ui import plotter_ui
from trame.app import get_server
from trame.ui.vuetify import SinglePageLayout
from trame.ui import vuetify, vuetify3

_HAS_TRAME = True

Expand All @@ -32,36 +41,63 @@


class TrameVisualizer:
"""Defines the trame layout view."""
"""
Defines the trame layout view.

def __init__(self) -> None:
Parameters
----------
client_type : str, optional
Client type to use for the trame server. Options are 'vue2' and 'vue3'.
Default is 'vue3'.
"""

def __init__(self, client_type: str = "vue3") -> None:
"""Initialize the trame server and server-related variables."""
if not _HAS_TRAME: # pragma: no cover
raise ModuleNotFoundError(
"The package 'pyvista[trame]' is required to use this function."
)
self._client_type = client_type
if client_type not in ["vue2", "vue3"]:
self._client_type = "vue3"
logger.warning("Invalid client type {}. Defaulting to 'vue3'.", client_type)

self.server = get_server()
self.server = get_server(client_type=client_type)
self.state, self.ctrl = self.server.state, self.server.controller

def set_scene(self, plotter):
def set_scene(self, plotter: Union["pv.Plotter", Plotter, "PlotterHelper"]):
"""
Set the trame layout view and the mesh to show through the PyVista plotter.

Parameters
----------
plotter : ~pyvista.Plotter
PyVista plotter with the rendered mesh.
plotter : Union[~pyvista.Plotter, ~Plotter, ~PlotterHelper]
PyVista plotter or PyAnsys plotter to render.
"""
from ansys.geometry.core.plotting.plotter_helper import PlotterHelper

if isinstance(plotter, PlotterHelper):
pv_plotter = plotter._pl.scene
elif isinstance(plotter, Plotter):
pv_plotter = plotter.scene
elif isinstance(plotter, pv.Plotter):
pv_plotter = plotter
else:
logger.warning("Invalid plotter type. Expected PyVista plotter or PyAnsys plotter.")

self.state.trame__title = "PyAnsys Geometry Viewer"
if self._client_type == "vue3":
page_layout = vuetify3.SinglePageLayout(self.server)
else:
page_layout = vuetify.SinglePageLayout(self.server)

with SinglePageLayout(self.server) as layout:
with page_layout as layout:
layout.icon.click = self.ctrl.view_reset_camera
layout.title.set_text("PyAnsys Geometry")

with layout.content:
# Use PyVista UI template for Plotters
view = plotter_ui(plotter.scene)
view = plotter_ui(pv_plotter)
self.ctrl.view_update = view.update

# hide footer with trame watermark
Expand Down