.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "examples/gallery_examples/00_basic/example_02_capture_images.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_examples_gallery_examples_00_basic_example_02_capture_images.py>`
        to download the full example code

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_examples_gallery_examples_00_basic_example_02_capture_images.py:

.. _ref_example_02_capture_images:

Capture images after a solve
----------------------------

Using supplied files, this example shows how to resume a MECHDAT file
and capture the images of all results in a folder on the disk.

.. GENERATED FROM PYTHON SOURCE LINES 12-16

Download required files
~~~~~~~~~~~~~~~~~~~~~~~
Download the required files. Print the file paths for the MECHDAT file and
script files.

.. GENERATED FROM PYTHON SOURCE LINES 16-31

.. code-block:: default


    import os

    from matplotlib import image as mpimg
    from matplotlib import pyplot as plt

    from ansys.mechanical.core import launch_mechanical
    from ansys.mechanical.core.examples import download_file

    mechdat_path = download_file("example_03_simple_bolt_new.mechdat", "pymechanical", "00_basic")
    print(f"Downloaded the MECHDAT file to: {mechdat_path}")

    script_file_path = download_file("example_02_capture_images_helper.py", "pymechanical", "00_basic")
    print(f"Downloaded the script files to: {script_file_path}")





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    Downloaded the MECHDAT file to: /home/runner/.local/share/ansys_mechanical_core/examples/example_03_simple_bolt_new.mechdat
    Downloaded the script files to: /home/runner/.local/share/ansys_mechanical_core/examples/example_02_capture_images_helper.py




.. GENERATED FROM PYTHON SOURCE LINES 32-37

Launch Mechanical
~~~~~~~~~~~~~~~~~
Launch a new Mechanical session in batch, setting ``cleanup_on_exit`` to
``False``. To close this Mechanical session when finished, this example
must call  the ``mechanical.exit()`` method.

.. GENERATED FROM PYTHON SOURCE LINES 37-41

.. code-block:: default


    mechanical = launch_mechanical(batch=True, cleanup_on_exit=False)
    print(mechanical)





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    Ansys Mechanical [Ansys Mechanical Enterprise]
    Product Version:231
    Software build date:Sat Nov 26 20:15:28 2022




.. GENERATED FROM PYTHON SOURCE LINES 42-46

Initialize the variable needed for opening the MECHDAT file
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Set the ``mechdat_path`` variable for later use.
Make the variable compatible for Windows, Linux, and Docker containers.

.. GENERATED FROM PYTHON SOURCE LINES 46-63

.. code-block:: default


    project_directory = mechanical.project_directory
    print(f"project directory = {project_directory}")

    # Upload the file to the project directory.
    mechanical.upload(file_name=mechdat_path, file_location_destination=project_directory)

    # Build the path relative to the project directory.
    base_name = os.path.basename(mechdat_path)
    combined_path = os.path.join(project_directory, base_name)
    mechdat_path_modified = combined_path.replace("\\", "\\\\")
    mechanical.run_python_script(f"mechdat_path='{mechdat_path_modified}'")

    # Verify the path for the MECHDAT file.
    result = mechanical.run_python_script(f"mechdat_path")
    print(f"MECHDATA file is stored on the server at: {result}")





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    project directory = /tmp/AnsysMech363C/Project_Mech_Files/

    Uploading example_03_simple_bolt_new.mechdat to 127.0.0.1:10000:/tmp/AnsysMech363C/Project_Mech_Files/.:   0%|          | 0.00/7.06M [00:00<?, ?B/s]
    Uploading example_03_simple_bolt_new.mechdat to 127.0.0.1:10000:/tmp/AnsysMech363C/Project_Mech_Files/.: 100%|##########| 7.06M/7.06M [00:00<00:00, 635MB/s]
    MECHDATA file is stored on the server at: /tmp/AnsysMech363C/Project_Mech_Files/example_03_simple_bolt_new.mechdat




.. GENERATED FROM PYTHON SOURCE LINES 64-67

Open the MECHDAT file
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Run the script to open the MECHDAT file.

.. GENERATED FROM PYTHON SOURCE LINES 67-70

.. code-block:: default


    mechanical.run_python_script("ExtAPI.DataModel.Project.Open(mechdat_path)")





.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    ''



.. GENERATED FROM PYTHON SOURCE LINES 71-75

Initialize the variable needed for the image directory
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Set the ``image_dir`` for later use.
Make the variable compatible for Windows, Linux, and Docker containers.

.. GENERATED FROM PYTHON SOURCE LINES 75-87

.. code-block:: default


    # Open the MECHDAT file changes the project directory.
    project_directory = mechanical.project_directory
    print(f"project directory = {project_directory}")

    image_directory_modified = project_directory.replace("\\", "\\\\")
    mechanical.run_python_script(f"image_dir='{image_directory_modified}'")

    # Verify the path for image directory.
    result_image_dir_server = mechanical.run_python_script(f"image_dir")
    print(f"Images are stored on the server at: {result_image_dir_server}")





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    project directory = /tmp/AnsysMech363C/Project_Mech_Files/example_03_simple_bolt_new_Mech_Files/
    Images are stored on the server at: /tmp/AnsysMech363C/Project_Mech_Files/example_03_simple_bolt_new_Mech_Files/




.. GENERATED FROM PYTHON SOURCE LINES 88-91

Execute the Mechanical script
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Run the Mechanical script file for creating the images.

.. GENERATED FROM PYTHON SOURCE LINES 91-94

.. code-block:: default


    mechanical.run_python_script_from_file(script_file_path)





.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    ''



.. GENERATED FROM PYTHON SOURCE LINES 95-99

Download the image and plot
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Download one image file from the server to the current working directory and plot
using matplotlib.

.. GENERATED FROM PYTHON SOURCE LINES 99-128

.. code-block:: default



    def get_image_path(image_name):
        return result_image_dir_server + image_name


    def display_image(path):
        print(f"Printing {path} using matplotlib")
        image1 = mpimg.imread(path)
        plt.figure(figsize=(15, 15))
        plt.axis("off")
        plt.imshow(image1)
        plt.show()


    image_name = "Total Deformation @ 1 sec_Right.png"
    image_path_server = get_image_path(image_name)

    if image_path_server != "":
        current_working_directory = os.getcwd()

        local_file_path_list = mechanical.download(
            image_path_server, target_dir=current_working_directory
        )
        image_local_path = local_file_path_list[0]
        print(f"Local image path : {image_local_path}")

        display_image(image_local_path)




.. image-sg:: /examples/gallery_examples/00_basic/images/sphx_glr_example_02_capture_images_001.png
   :alt: example 02 capture images
   :srcset: /examples/gallery_examples/00_basic/images/sphx_glr_example_02_capture_images_001.png
   :class: sphx-glr-single-img


.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    Downloading 127.0.0.1:10000:/tmp/AnsysMech363C/Project_Mech_Files/example_03_simple_bolt_new_Mech_Files/Total Deformation @ 1 sec_Right.png to /home/runner/work/pymechanical/pymechanical/examples/00_basic/Total Deformation @ 1 sec_Right.png:   0%|          | 0.00/115k [00:00<?, ?B/s]
    Downloading 127.0.0.1:10000:/tmp/AnsysMech363C/Project_Mech_Files/example_03_simple_bolt_new_Mech_Files/Total Deformation @ 1 sec_Right.png to /home/runner/work/pymechanical/pymechanical/examples/00_basic/Total Deformation @ 1 sec_Right.png: 100%|##########| 115k/115k [00:00<00:00, 2.99GB/s]
    Local image path : /home/runner/work/pymechanical/pymechanical/examples/00_basic/Total Deformation @ 1 sec_Right.png
    Printing /home/runner/work/pymechanical/pymechanical/examples/00_basic/Total Deformation @ 1 sec_Right.png using matplotlib




.. GENERATED FROM PYTHON SOURCE LINES 129-132

Clear the data
~~~~~~~~~~~~~~
Clear the data so it isn't saved to the project.

.. GENERATED FROM PYTHON SOURCE LINES 132-135

.. code-block:: default


    mechanical.clear()








.. GENERATED FROM PYTHON SOURCE LINES 136-139

Close Mechanical
~~~~~~~~~~~~~~~~
Close the Mechanical instance.

.. GENERATED FROM PYTHON SOURCE LINES 139-141

.. code-block:: default


    mechanical.exit()








.. rst-class:: sphx-glr-timing

   **Total running time of the script:** ( 0 minutes  26.683 seconds)


.. _sphx_glr_download_examples_gallery_examples_00_basic_example_02_capture_images.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example




    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: example_02_capture_images.py <example_02_capture_images.py>`

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: example_02_capture_images.ipynb <example_02_capture_images.ipynb>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_