.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "_examples/25-intermediate/02-utils.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

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

        :ref:`Go to the end <sphx_glr_download__examples_25-intermediate_02-utils.py>`
        to download the full example code

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

.. _sphx_glr__examples_25-intermediate_02-utils.py:


.. _ref_utils_example:

EnSight Utilities
=====================
The PyEnSight ``utils`` modules have been designed to expose standard
postprocessing operations via simplified APIs. This example shows how to
use the ``utils`` to easily perform specific operations.

.. GENERATED FROM PYTHON SOURCE LINES 13-17

Start an EnSight session
------------------------
Launch and connect to an instance of EnSight.
This example uses a local EnSight installation.

.. GENERATED FROM PYTHON SOURCE LINES 17-24

.. code-block:: default


    from ansys.pyensight.core import LocalLauncher
    from ansys.pyensight.core.enscontext import EnsContext

    session = LocalLauncher().start()



.. GENERATED FROM PYTHON SOURCE LINES 25-31

Load the data
-------------
Use a remote session to load a simple time-varying dataset of
waterflow over a break.

.. image:: /_static/02_utils_0.png

.. GENERATED FROM PYTHON SOURCE LINES 31-36

.. code-block:: default


    session.load_example("waterbreak.ens")
    session.show("image", width=800, height=600)



.. GENERATED FROM PYTHON SOURCE LINES 37-43

Load the ``utils`` modules
--------------------------

The ``utils`` modules are available as instances of ``ensight.utils``. To
provide a simple use case, this example casts them into new variables
with the same names.

.. GENERATED FROM PYTHON SOURCE LINES 43-49

.. code-block:: default


    parts = session.ensight.utils.parts
    views = session.ensight.utils.views
    query = session.ensight.utils.query



.. GENERATED FROM PYTHON SOURCE LINES 50-55

Capture a context of the current state
--------------------------------------
Use the :func:`capture_context<ansys.pyensight.core.Session.capture_context>`
method to save an in-memory context, which is retrieved later in this example.
Also save the context to a file for use in a future PyEnSight session.

.. GENERATED FROM PYTHON SOURCE LINES 55-60

.. code-block:: default


    init_state = session.capture_context()
    init_state.save("init_state.ctxz")



.. GENERATED FROM PYTHON SOURCE LINES 61-80

Change view direction and restore an in-memory context
------------------------------------------------------
Save an isometric view along the direction vector (1,1,1) and a new in-memory
context. Save the view, naming it ``isometric``. Use the
:func:`select_parts_by_tag<ansys.pyensight.core.utils.parts.Parts.select_parts_by_tag>`
method to select all parts. (All parts are returned because no tags have been supplied
and because the dataset has no metadata for the parts).

When no tags are supplied, all parts are selected.

.. image:: /_static/02_utils_1.png

All parts are hidden.

.. image:: /_static/02_utils_2.png

Restore the state, showing the isometric view once again.

.. image:: /_static/02_utils_1.png

.. GENERATED FROM PYTHON SOURCE LINES 80-96

.. code-block:: default


    views.set_view_direction(1, 1, 1, name="isometric")
    iso_state = session.capture_context()
    session.show("image", width=800, height=600)
    # Because no tags are supplied, all parts are selected

    # Hide the parts.
    parts.select_parts_by_tag().set_attr("VISIBLE", False)

    # Restore the state, showing the isometric view once again.

    session.show("image", width=800, height=600)
    session.restore_context(iso_state)
    session.show("image", width=800, height=600)



.. GENERATED FROM PYTHON SOURCE LINES 97-112

Create scoped name
------------------
A scoped name provides for easily using EnSight submodules to generate
distance queries. PyEnSight supports the generation of context managers for
the PyEnSight modules. Its context manager features, along with the context
manager features in Python, can simplify the workflow.

This code generates a query along a 1D part on the fly. It uses the ``Parts`` class to
select the parent part and the
:func:`select_parts_by_dimension<ansys.pyensight.core.utils.parts.Parts.select_parts_by_dimension>`
method to select all 3D parts. Lastly, it saves a context for later use.

The rendering view should look like this:

.. image:: /_static/02_utils_3.png

.. GENERATED FROM PYTHON SOURCE LINES 112-130

.. code-block:: default


    sn = session.ensight.utils.support.scoped_name
    zclip_state = None
    with sn(session.ensight) as ensight, sn(session.ensight.objs.core) as core:
        clip_default = core.DEFAULTPARTS[ensight.PART_CLIP_PLANE]
        clip = clip_default.createpart(name="XClip", sources=parts.select_parts_by_dimension(3))[0]
        attrs = []
        attrs.append(["MESHPLANE", 2])  # Z axis
        attrs.append(["TOOL", 9])  # XYZ Tool
        attrs.append(["VALUE", 0.55])  # Z value
        zclip = clip_default.createpart(name="ZClip", sources=clip)[0]
        query.create_distance(
            "zlip_query", query.DISTANCE_PART1D, [zclip], core.VARIABLES["p"][0], new_plotter=True
        )
        zclip_state = session.capture_context()
    session.show("image", width=800, height=600)



.. GENERATED FROM PYTHON SOURCE LINES 131-142

Restore a view
--------------
This code chances the model orientation, position, and zoom. It then restores
the isometric view. While restoring a context restores the orientation,
position, zoom level, and the objects available at the time that the context
was saved, restoring a view only restores the orientation and
position.

The rendering view should look like this:

.. image:: /_static/02_utils_4.png

.. GENERATED FROM PYTHON SOURCE LINES 142-154

.. code-block:: default


    session.ensight.view_transf.rotate(-66.5934067, 1.71428561, 0)
    session.ensight.view_transf.rotate(18.0219765, -31.6363659, 0)
    session.ensight.view_transf.rotate(-4.83516455, 9.5064888, 0)
    session.ensight.view_transf.zoom(0.740957975)
    session.ensight.view_transf.zoom(0.792766333)
    session.ensight.view_transf.translate(0.0719177574, 0.0678303316, 0)
    session.ensight.view_transf.rotate(4.83516455, 3.42857122, 0)
    views.restore_view("isometric")
    session.show("image", width=800, height=600)



.. GENERATED FROM PYTHON SOURCE LINES 155-165

Create a temporal query
-----------------------
This code restores the distance query context and then generates a temporal
query. This query is applied to a specific XYZ point, querying the
``"alpha1"`` variable. The XYZ point is set to the model centroid and computed
via the ``Views`` class. The data generated is then printed.

The value returned should look like this:

.. image:: /_static/02_utils_5.png

.. GENERATED FROM PYTHON SOURCE LINES 165-177

.. code-block:: default


    session.restore_context(zclip_state)
    temp_query = query.create_temporal(
        "temporal_query",
        query.TEMPORAL_XYZ,
        parts.select_parts_by_dimension(3),
        "alpha1",
        xyz=views.compute_model_centroid(),
    )
    print(temp_query.QUERY_DATA)



.. GENERATED FROM PYTHON SOURCE LINES 178-187

Restore a context from disk
---------------------------
This code shows how to restore a context previously saved on disk.
Because PyEnSight context files do not store the location of the dataset by
default, you must load the dataset before restoring the context.

The rendering view should look like this.

.. image:: /_static/02_utils_6.png

.. GENERATED FROM PYTHON SOURCE LINES 187-194

.. code-block:: default


    ctx = EnsContext()
    ctx.load("init_state.ctxz")
    session.restore_context(ctx)
    session.show("image", width=800, height=600)

    # sphinx_gallery_thumbnail_path = '_static/02_utils_3.png'


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

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


.. _sphx_glr_download__examples_25-intermediate_02-utils.py:

.. only:: html

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




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

      :download:`Download Python source code: 02-utils.py <02-utils.py>`

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

      :download:`Download Jupyter notebook: 02-utils.ipynb <02-utils.ipynb>`


.. only:: html

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

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