From e9b047a58119a2984fa566b1245deda7fd1774e6 Mon Sep 17 00:00:00 2001 From: afernand Date: Fri, 24 Jan 2025 15:34:22 +0100 Subject: [PATCH 1/6] fix(plotting): Test plotting changes --- src/ansys/mapdl/core/plotting/visualizer.py | 73 ++++++++++++++------- 1 file changed, 48 insertions(+), 25 deletions(-) diff --git a/src/ansys/mapdl/core/plotting/visualizer.py b/src/ansys/mapdl/core/plotting/visualizer.py index f4df50e8d8..98ae9d8a5f 100644 --- a/src/ansys/mapdl/core/plotting/visualizer.py +++ b/src/ansys/mapdl/core/plotting/visualizer.py @@ -22,7 +22,7 @@ """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 @@ -280,9 +280,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, @@ -331,6 +331,9 @@ def add_mesh( plotter_kwargs : dict, optional Extra kwargs, by default {} """ + if not meshes and not points and not labels: + raise ValueError("Nothing to plot.") + if theme is None: theme = MapdlTheme() @@ -378,31 +381,48 @@ 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 + print( + mesh.get("color") + if isinstance(mesh, Dict) and "color" in mesh + else color + ) 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, @@ -651,9 +671,9 @@ def bc_nodes_plot( def plot( self, - meshes, - points, - labels, + meshes: Union[pv.PolyData, Dict[str, Any]] = [], + points=[], + labels=[], *, title="", cpos=None, @@ -710,6 +730,9 @@ def plot( Filter to apply to the object. The default is ``None``. """ + if not meshes and not points and not labels: + raise ValueError("Nothing to plot.") + # Getting the plotter self.add_mesh( meshes, From 4ab81042bc447769f04138208fe4105925bfe5c2 Mon Sep 17 00:00:00 2001 From: pyansys-ci-bot <92810346+pyansys-ci-bot@users.noreply.github.com> Date: Fri, 24 Jan 2025 14:36:27 +0000 Subject: [PATCH 2/6] chore: adding changelog file 3702.fixed.md [dependabot-skip] --- doc/changelog.d/3702.fixed.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 doc/changelog.d/3702.fixed.md diff --git a/doc/changelog.d/3702.fixed.md b/doc/changelog.d/3702.fixed.md new file mode 100644 index 0000000000..fedbeeaffd --- /dev/null +++ b/doc/changelog.d/3702.fixed.md @@ -0,0 +1 @@ +fix(plotting): Test plotting changes \ No newline at end of file From 3ee7447439b4216b449323ab738ef8f57091a75b Mon Sep 17 00:00:00 2001 From: afernand Date: Fri, 24 Jan 2025 16:16:47 +0100 Subject: [PATCH 3/6] fix(plotting): Small bug --- src/ansys/mapdl/core/plotting/visualizer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ansys/mapdl/core/plotting/visualizer.py b/src/ansys/mapdl/core/plotting/visualizer.py index 98ae9d8a5f..bb72bf7d13 100644 --- a/src/ansys/mapdl/core/plotting/visualizer.py +++ b/src/ansys/mapdl/core/plotting/visualizer.py @@ -731,7 +731,7 @@ def plot( """ if not meshes and not points and not labels: - raise ValueError("Nothing to plot.") + return # Getting the plotter self.add_mesh( From eaf910197678a8ba2c30c2bf44e3c24dddd5f364 Mon Sep 17 00:00:00 2001 From: afernand Date: Fri, 24 Jan 2025 16:43:27 +0100 Subject: [PATCH 4/6] fix: Bug caused tests failure --- src/ansys/mapdl/core/plotting/visualizer.py | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/ansys/mapdl/core/plotting/visualizer.py b/src/ansys/mapdl/core/plotting/visualizer.py index bb72bf7d13..cee5e58b00 100644 --- a/src/ansys/mapdl/core/plotting/visualizer.py +++ b/src/ansys/mapdl/core/plotting/visualizer.py @@ -332,7 +332,7 @@ def add_mesh( Extra kwargs, by default {} """ if not meshes and not points and not labels: - raise ValueError("Nothing to plot.") + return if theme is None: theme = MapdlTheme() @@ -403,11 +403,6 @@ def add_mesh( else: scalars = None mesh_ = meshes - print( - mesh.get("color") - if isinstance(mesh, Dict) and "color" in mesh - else color - ) for each_mesh in mesh_: self.scene.add_mesh( each_mesh, @@ -730,9 +725,6 @@ def plot( Filter to apply to the object. The default is ``None``. """ - if not meshes and not points and not labels: - return - # Getting the plotter self.add_mesh( meshes, From 5c52f92abb081a79a6668787736b6746a6749adc Mon Sep 17 00:00:00 2001 From: afernand Date: Mon, 27 Jan 2025 10:03:59 +0100 Subject: [PATCH 5/6] fix: Add warning --- src/ansys/mapdl/core/plotting/visualizer.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/ansys/mapdl/core/plotting/visualizer.py b/src/ansys/mapdl/core/plotting/visualizer.py index cee5e58b00..6d99ed376f 100644 --- a/src/ansys/mapdl/core/plotting/visualizer.py +++ b/src/ansys/mapdl/core/plotting/visualizer.py @@ -29,6 +29,7 @@ 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 ( @@ -332,6 +333,7 @@ def add_mesh( 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: From cbe4330dd3de9afc886aa7b7c2eefc4a4724dcff Mon Sep 17 00:00:00 2001 From: pyansys-ci-bot <92810346+pyansys-ci-bot@users.noreply.github.com> Date: Mon, 27 Jan 2025 09:14:57 +0000 Subject: [PATCH 6/6] chore: adding changelog file 3702.fixed.md [dependabot-skip] --- doc/changelog.d/3702.fixed.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/changelog.d/3702.fixed.md b/doc/changelog.d/3702.fixed.md index fedbeeaffd..ee2be199c3 100644 --- a/doc/changelog.d/3702.fixed.md +++ b/doc/changelog.d/3702.fixed.md @@ -1 +1 @@ -fix(plotting): Test plotting changes \ No newline at end of file +fix(plotting): Improve interface of the plotting class. \ No newline at end of file