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

feat: raising error if plot image cannot be obtained #3559

Merged
merged 6 commits into from
Nov 21, 2024
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/3559.miscellaneous.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
feat: raising error if plot image cannot be obtained
20 changes: 15 additions & 5 deletions src/ansys/mapdl/core/mapdl_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2359,12 +2359,15 @@ def _cleanup_loggers(self):
logger.std_out_handler.close()
logger.std_out_handler = None

def is_png_found(self, text: str) -> bool:
# findall returns None if there is no match
return PNG_IS_WRITTEN_TO_FILE.findall(text) is not None

def _get_plot_name(self, text: str) -> str:
"""Obtain the plot filename."""
self._log.debug(text)
png_found = PNG_IS_WRITTEN_TO_FILE.findall(text)
self._log.debug(f"Output from terminal used to find plot name: {text}")

if png_found:
if self.is_png_found(text):
# flush graphics writer
previous_device = self.file_type_for_plots
self.show("CLOSE", mute=True)
Expand All @@ -2377,9 +2380,16 @@ def _get_plot_name(self, text: str) -> str:
if os.path.isfile(filename):
return filename
else: # pragma: no cover
self._log.error("Unable to find screenshot at %s", filename)
raise MapdlRuntimeError("Unable to find screenshot at %s", filename)
else:
self._log.error("Unable to find file in MAPDL command output.")
raise MapdlRuntimeError(
"Unable to find plotted file in MAPDL command output. "
"One possible reason is that the graphics device is not correct. "
"Please check you are using FULL graphics device. "
"For example:\n"
">>> mapdl.graphics('FULL')"
f"\nThe text output from MAPDL is:\n{text}"
)

def _display_plot(self, filename: str) -> None:
"""Display the last generated plot (*.png) from MAPDL"""
Expand Down
28 changes: 27 additions & 1 deletion tests/test_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

"""Unit tests regarding plotting."""
import os
from unittest.mock import patch

import numpy as np
import pytest
Expand All @@ -31,7 +32,7 @@
if not has_dependency("pyvista"):
pytest.skip(allow_module_level=True)

from ansys.mapdl.core.errors import ComponentDoesNotExits
from ansys.mapdl.core.errors import ComponentDoesNotExits, MapdlRuntimeError
from ansys.mapdl.core.plotting.visualizer import MapdlPlotter

FORCE_LABELS = [["FX", "FY", "FZ"], ["HEAT"], ["CHRG"]]
Expand Down Expand Up @@ -1254,3 +1255,28 @@ def test_aplot_quality_fail(mapdl, make_block, quality):
match="The argument 'quality' can only be an integer between 1 and 10",
):
mapdl.aplot(quality=quality)


@patch("ansys.mapdl.core.Mapdl.is_png_found", lambda *args, **kwargs: False)
def test_plot_path(mapdl, tmpdir):
mapdl.graphics("POWER")

with pytest.raises(
MapdlRuntimeError,
match="One possible reason is that the graphics device is not correct",
):
mapdl.eplot(vtk=False)


def test_plot_path_screenshoot(mapdl, cleared, tmpdir):
mapdl.graphics("POWER")
# mapdl.screenshot is not affected by the device.
# It should not raise exceptions
scheenshot_path = os.path.join(tmpdir, "screenshot.png")
mapdl.screenshot(scheenshot_path)

assert os.path.exists(scheenshot_path)
assert os.path.getsize(scheenshot_path) > 100 # check if it is not empty

# Returning to previous state.
mapdl.graphics("FULL")
Loading