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 5 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
69 changes: 69 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,72 @@ 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
Name of or path to the screenshot file.
The default is ``None``.

Returns
-------
str
File name.

Raises
------
FileNotFoundError
When the path given in `savefig` is not found or is not consistent.
ValueError
When given a wrong type for `savefig` parameter.
germa89 marked this conversation as resolved.
Show resolved Hide resolved
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_)

def get_file_name(path):
"""Get a new file name to not overwrite an existing one"""
germa89 marked this conversation as resolved.
Show resolved Hide resolved
target_dir = os.path.join(path, "mapdl_screenshot_0.png")
i = 0
while os.path.exists(target_dir):
# Making sure we are not overwriting a file.
germa89 marked this conversation as resolved.
Show resolved Hide resolved
i += 1
target_dir = os.path.join(path, f"mapdl_screenshot_{i}.png")
return target_dir

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

else:
if savefig is True:
# Copying to working directory
target_dir = get_file_name(os.getcwd())

elif isinstance(savefig, str):
if not os.path.dirname(savefig):
# File name given only
target_dir = os.path.join(os.getcwd(), savefig)

elif os.path.isdir(savefig):
# Given directory path only, but not file name.
target_dir = get_file_name(savefig)

elif os.path.exists(os.path.dirname(savefig)):
# Only directory is given. Checking if directory exists
germa89 marked this conversation as resolved.
Show resolved Hide resolved
target_dir = savefig

else:
raise FileNotFoundError("The filename or path is not a valid.")
germa89 marked this conversation as resolved.
Show resolved Hide resolved

else:
raise ValueError(
"Only strings or Booleans are valid inputs for the 'savefig' parameter."
)

shutil.copy(file_name, target_dir)
return os.path.basename(target_dir)
45 changes: 45 additions & 0 deletions tests/test_mapdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -2363,3 +2363,48 @@ 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 "mapdl_screenshot_0.png" == file_name
assert "TIFF" == mapdl.file_type_for_plots
assert file_name in os.listdir(os.getcwd())

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

os.remove("mapdl_screenshot_0.png")
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 os.path.exists(dest)

file_name = mapdl.screenshot("myscreenshot.png")
assert "TIFF" == mapdl.file_type_for_plots
assert os.path.exists(file_name)
assert os.path.exists(os.path.join(os.getcwd(), "myscreenshot.png"))
os.remove(file_name)

mapdl.file_type_for_plots = previous_device
Loading