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

Adding screenshot method. #2846

Merged
merged 7 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 3 additions & 1 deletion src/ansys/mapdl/core/mapdl_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2273,16 +2273,18 @@ def __del__(self):
pass

def _get_plot_name(self, text: str) -> str:
""" "Obtain the plot filename. It also downloads it if in remote session."""
"""Obtain the plot filename."""
self._log.debug(text)
png_found = PNG_IS_WRITTEN_TO_FILE.findall(text)

if png_found:
# flush graphics writer
previous_device = self.file_type_for_plots
self.show("CLOSE", mute=True)
# self.show("PNG", mute=True)

filename = self._screenshot_path()
self.show(previous_device)
self._log.debug(f"Screenshot at: {filename}")

if os.path.isfile(filename):
Expand Down
36 changes: 36 additions & 0 deletions src/ansys/mapdl/core/mapdl_grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3477,3 +3477,39 @@ def parain(
scale=scale,
**kwargs,
)

def screenshot(self, savefig: Optional[str] = None):
"""Screenshot Take an MAPDL screenshot and show it in a popup window.
germa89 marked this conversation as resolved.
Show resolved Hide resolved

Parameters
----------
savefig : Optional[str], optional
Specify the name of the screenshot file, by default None
germa89 marked this conversation as resolved.
Show resolved Hide resolved
"""
previous_device = self.file_type_for_plots
self.show("PNG")
out_ = self.replot()
self.show(previous_device) # previous device
file_name = self._get_plot_name(out_)

if savefig is None or savefig is False:
self._display_plot(file_name)

else:
if isinstance(savefig, bool):
if savefig:
# Copying to working directory
target_dir = os.getcwd()
elif isinstance(savefig, str):
if os.path.exists(os.path.dirname(savefig)):
target_dir = savefig
else:
raise FileNotFoundError("The path is not a valid directory.")

else:
raise ValueError(
"Only strings or booleans are valid inputs for 'savefig'."
germa89 marked this conversation as resolved.
Show resolved Hide resolved
)

shutil.copy(file_name, target_dir)
return os.path.basename(file_name)
31 changes: 31 additions & 0 deletions tests/test_mapdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -2363,3 +2363,34 @@ def test_lgwrite(mapdl, cleared, filename, ext, remove_grpc_extra, kedit):
assert "LGWRITE" in content

os.remove(filename_)


@requires("matplotlib")
@requires("grpc")
def test_screenshot(mapdl, make_block, tmpdir):
"""Test screenshot capabilities"""
previous_device = mapdl.file_type_for_plots
mapdl.show("TIFF")
assert "TIFF" == mapdl.file_type_for_plots

assert mapdl.screenshot() is None
assert "TIFF" == mapdl.file_type_for_plots

assert mapdl.screenshot(False) is None
assert "TIFF" == mapdl.file_type_for_plots

file_name = mapdl.screenshot(True)
assert "TIFF" == mapdl.file_type_for_plots
assert file_name in os.listdir(os.getcwd())
os.remove(file_name)

file_name = mapdl.screenshot(str(tmpdir))
assert "TIFF" == mapdl.file_type_for_plots
assert file_name in os.listdir(str(tmpdir))

dest = os.path.join(tmpdir, "myscreenshot.png")
file_name = mapdl.screenshot(dest)
assert "TIFF" == mapdl.file_type_for_plots
assert file_name in os.listdir(str(tmpdir))

mapdl.file_type_for_plots = previous_device
Loading