Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(plotting): Improve interface of the plotting class. #3702

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
Draft
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/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 @@ -280,9 +281,9 @@

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 @@ -331,6 +332,10 @@
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
AlejandroFernandezLuces marked this conversation as resolved.
Show resolved Hide resolved

if theme is None:
theme = MapdlTheme()

Expand Down Expand Up @@ -378,31 +383,43 @@
**(add_points_kwargs or {}),
)

if isinstance(meshes, pv.PolyData):
meshes = [meshes]

Check warning on line 387 in src/ansys/mapdl/core/plotting/visualizer.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/plotting/visualizer.py#L387

Added line #L387 was not covered by tests
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

Check warning on line 399 in src/ansys/mapdl/core/plotting/visualizer.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/plotting/visualizer.py#L399

Added line #L399 was not covered by tests

# 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

Check warning on line 407 in src/ansys/mapdl/core/plotting/visualizer.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/plotting/visualizer.py#L406-L407

Added lines #L406 - L407 were not covered by tests
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 @@ -651,9 +668,9 @@

def plot(
self,
meshes,
points,
labels,
meshes: Union[pv.PolyData, Dict[str, Any]] = [],
points=[],
labels=[],
*,
title="",
cpos=None,
Expand Down