Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
e9b047a
fix(plotting): Test plotting changes
AlejandroFernandezLuces Jan 24, 2025
4ab8104
chore: adding changelog file 3702.fixed.md [dependabot-skip]
pyansys-ci-bot Jan 24, 2025
3ee7447
fix(plotting): Small bug
AlejandroFernandezLuces Jan 24, 2025
89ad28b
Merge branch 'fix/plotter-issues' of https://github.com/pyansys/pymap…
AlejandroFernandezLuces Jan 24, 2025
eaf9101
fix: Bug caused tests failure
AlejandroFernandezLuces Jan 24, 2025
34459bf
Merge branch 'main' into fix/plotter-issues
AlejandroFernandezLuces Jan 24, 2025
5c52f92
fix: Add warning
AlejandroFernandezLuces Jan 27, 2025
2503336
Merge branch 'fix/plotter-issues' of https://github.com/pyansys/pymap…
AlejandroFernandezLuces Jan 27, 2025
4753b7a
Merge branch 'main' into fix/plotter-issues
AlejandroFernandezLuces Jan 27, 2025
cbe4330
chore: adding changelog file 3702.fixed.md [dependabot-skip]
pyansys-ci-bot Jan 27, 2025
dd79733
Merge branch 'main' into fix/plotter-issues
clatapie Jan 29, 2025
867794d
Merge branch 'main' into fix/plotter-issues
clatapie Feb 13, 2025
3139191
Merge branch 'main' into fix/plotter-issues
germa89 Feb 17, 2025
7b0084a
Merge branch 'main' into fix/plotter-issues
germa89 Feb 26, 2025
64207f2
Merge branch 'main' into fix/plotter-issues
germa89 Mar 7, 2025
04315ab
Merge branch 'main' into fix/plotter-issues
clatapie Mar 13, 2025
1e828f5
Merge branch 'main' into fix/plotter-issues
AlejandroFernandezLuces Mar 14, 2025
f40f571
test: Add mesh testing
AlejandroFernandezLuces Mar 14, 2025
9065839
test: Add docstring to test
AlejandroFernandezLuces Mar 14, 2025
eb48840
fix: PyVista lazy import
AlejandroFernandezLuces Mar 14, 2025
6718ab3
Merge branch 'main' into fix/plotter-issues
AlejandroFernandezLuces Mar 14, 2025
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/3702.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fix(plotting): Improve interface of the plotting class.
67 changes: 42 additions & 25 deletions src/ansys/mapdl/core/plotting/visualizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@

"""Module for the MapdlPlotter class."""
from collections import OrderedDict
from typing import Any, Iterable, Optional, Union
from typing import Any, Dict, Iterable, Optional, Union

from ansys.tools.visualization_interface import Plotter
from ansys.tools.visualization_interface.backends.pyvista import PyVistaBackendInterface
import numpy as np
from numpy.typing import NDArray

from ansys.mapdl.core import LOG as logger
from ansys.mapdl.core import _HAS_VISUALIZER
from ansys.mapdl.core.misc import get_bounding_box
from ansys.mapdl.core.plotting.consts import (
Expand Down Expand Up @@ -277,9 +278,9 @@ def plot_iter(

def add_mesh(
self,
meshes,
points,
labels,
meshes: Union[pv.PolyData, Dict[str, Any]] = [],
points=[],
labels=[],
*,
cpos=None,
show_bounds=False,
Expand Down Expand Up @@ -328,6 +329,10 @@ def add_mesh(
plotter_kwargs : dict, optional
Extra kwargs, by default {}
"""
if not meshes and not points and not labels:
logger.warning("No meshes, points or labels to plot.")
return

if theme is None:
theme = MapdlTheme()

Expand Down Expand Up @@ -375,31 +380,43 @@ def add_mesh(
**(add_points_kwargs or {}),
)

if isinstance(meshes, pv.PolyData):
meshes = [meshes]
for mesh in meshes:
scalars: Optional[NDArray[Any]] = mesh.get("scalars")

if (
"scalars" in mesh
and scalars.ndim == 2
and (scalars.shape[1] == 3 or scalars.shape[1] == 4)
):
# for the case we are using scalars for plotting
rgb = True
rgb = False
if isinstance(mesh, Dict):
scalars: Optional[NDArray[Any]] = mesh.get("scalars")

if (
"scalars" in mesh
and scalars.ndim == 2
and (scalars.shape[1] == 3 or scalars.shape[1] == 4)
):
# for the case we are using scalars for plotting
rgb = True

# To avoid index error.
mesh_ = mesh["mesh"]
if not isinstance(mesh_, list):
mesh_ = [mesh_]
else:
rgb = False

# To avoid index error.
mesh_ = mesh["mesh"]
if not isinstance(mesh_, list):
mesh_ = [mesh_]

scalars = None
mesh_ = meshes
for each_mesh in mesh_:
self.scene.add_mesh(
each_mesh,
scalars=scalars,
scalar_bar_args=scalar_bar_args,
color=mesh.get("color", color),
style=mesh.get("style", style),
color=(
mesh.get("color", color)
if isinstance(mesh, Dict) and "color" in mesh
else color
),
style=(
mesh.get("style", style)
if isinstance(mesh, Dict) and "style" in mesh
else style
),
show_edges=show_edges,
edge_color=edge_color,
smooth_shading=smooth_shading,
Expand Down Expand Up @@ -648,9 +665,9 @@ def bc_nodes_plot(

def plot(
self,
meshes,
points,
labels,
meshes: Union[pv.PolyData, Dict[str, Any]] = [],
points=[],
labels=[],
*,
title="",
cpos=None,
Expand Down
31 changes: 31 additions & 0 deletions tests/test_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -1262,6 +1262,37 @@ def test_plot_path(mapdl, tmpdir):
mapdl.eplot(vtk=False)


def test_add_mesh():
"""Test the add_mesh method from MapdlPlotter class."""
import pyvista as pv

cube1 = pv.Cube()
pl1 = MapdlPlotter()
pl1.add_mesh(cube1)

cube2 = pv.Cube()
meshes_dict = [
{
"mesh": cube2,
"scalars": np.random.default_rng(seed=1).random((8, 3)),
}
]

pl2 = MapdlPlotter()
pl2.add_mesh(meshes_dict)

cube3 = pv.Cube()
sphere = pv.Sphere()

pl3 = MapdlPlotter()
pl3.add_mesh([cube3, sphere])

assert pl1.meshes[0] == cube1
assert pl2.meshes[0] == cube2
assert pl3.meshes[0] == cube3
assert pl3.meshes[1] == sphere


def test_plot_path_screenshoot(mapdl, cleared, tmpdir):
mapdl.graphics("POWER")
# mapdl.screenshot is not affected by the device.
Expand Down
Loading