Skip to content

Commit 06f3974

Browse files
AlejandroFernandezLucespre-commit-ci[bot]pyansys-ci-botPipKat
authored
doc: Add extended migration guide (#363)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: pyansys-ci-bot <[email protected]> Co-authored-by: Kathy Pippert <[email protected]>
1 parent 23dc422 commit 06f3974

File tree

7 files changed

+237
-10
lines changed

7 files changed

+237
-10
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Doc: Add extended migration guide

doc/source/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ The Visualization Interface Tool offers these main features:
6868

6969
getting_started/index
7070
user_guide/index
71+
user_guide/migration
7172
{% if build_api %}
7273
api/index
7374
{% endif %}

doc/source/user_guide/index.rst

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ User guide
66

77
This section explains key concepts for implementing the Visualization Interface Tool in your workflow.
88
You can use the Visualization Interface Tool in your examples as well as integrate this library into
9-
your own code.
9+
your own code. For information on how to migrate from PyVista to the Ansys Visualization Interface Tool, see
10+
:ref:`ref_migration_guide`.
1011

1112
Default plotter usage
1213
=====================
@@ -45,7 +46,7 @@ Use with PyAnsys custom objects
4546

4647
You can also use the default plotter to visualize PyAnsys custom objects. The only requirement is that the
4748
custom object must have a method that returns a PyVista mesh a method that exposes a ``name`` or
48-
``id`` attribute of your object. To expose a custom object, you use a ``MeshObjectPlot`` instance. This class
49+
``id`` attribute of your object. To expose a custom object, you use a :class:`~ansys.tools.visualization_interface.types.mesh_object_plot.MeshObjectPlot` instance. This class
4950
relates PyVista meshes with any object.
5051

5152
The following code shows how to use the default plotter to visualize a PyAnsys custom object:
@@ -99,7 +100,7 @@ class. After that, see these main use cases for customizing the plotter:
99100
* The most common use case is to customize the way that the objects you represent are shown in the plotter.
100101
To this end, you can override the ``plot`` and ``plot_iter`` methods. These methods are called every time
101102
a new object is added to the plotter. The default implementation of this method is to add a PyVista mesh
102-
or a ``MeshObjectPlot`` instance to the plotter. You can override this method to add your own meshes or
103+
or a :class:`~ansys.tools.visualization_interface.types.mesh_object_plot.MeshObjectPlot` instance to the plotter. You can override this method to add your own meshes or
103104
objects to the plotter in a manner that fits the way that you want to represent the meshes.
104105

105106
* Another use case is the need to have custom button functionalities for your library. For example, you may
@@ -109,15 +110,17 @@ class. After that, see these main use cases for customizing the plotter:
109110
Some practical examples of how to use the ``PlotterInterface`` class are included in some PyAnsys libraries,
110111
such as `PyAnsys Geometry <https://github.com/ansys/pyansys-geometry/pull/959>`_.
111112

113+
For comprehensive migration information with code examples, see :ref:`ref_migration_guide`.
114+
112115

113116
Customizing the picker and hover callbacks
114117
==========================================
115118

116-
The Visualization Interface Tool provides a base class, ``AbstractPicker``, for customizing the picker and hover
119+
The Visualization Interface Tool provides a base class, :class:`~ansys.tools.visualization_interface.backends.pyvista.picker.AbstractPicker`, for customizing the picker and hover
117120
callbacks of the plotter. This class provides a set of methods that can be overridden so that you can adapt the
118121
picker and hover functionalities to the specific need of your PyAnsys library.
119122

120-
The first thing you must do is to create a class that inherits from the ``AbstractPicker`` class. After that, see
123+
The first thing you must do is to create a class that inherits from the :class:`~ansys.tools.visualization_interface.backends.pyvista.picker.AbstractPicker` class. After that, see
121124
these main use cases for customizing the picker and hover callbacks:
122125

123126
* You may want to change the way that objects are picked in the plotter. To do this, you can override the
@@ -128,4 +131,4 @@ these main use cases for customizing the picker and hover callbacks:
128131
override the ``hover_select_object`` and ``hover_unselect_object`` methods. These methods are called when an
129132
object is hovered over or unhovered, respectively.
130133

131-
A practical example of how to use the ``AbstractPicker`` class are included in the examples section of the documentation.
134+
A practical example of how to use the :class:`~ansys.tools.visualization_interface.backends.pyvista.picker.AbstractPicker` class is included in :ref:`sphx_glr_examples_00-basic-pyvista-examples_custom_picker.py`.
Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
.. _ref_migration_guide:
2+
3+
Migration
4+
#########
5+
6+
This section helps you migrate from PyVista plotters to the Ansys Tools Visualization Interface plotters.
7+
It consists of two major topics:
8+
9+
- `Code migration`_
10+
- `Documentation configuration migration`_
11+
12+
Code migration
13+
==============
14+
This topic explains how to migrate from PyVista plotters to the new Ansys Tools Visualization Interface plotters. Because cases vary greatly, it provides a few examples that cover the most common scenarios.
15+
16+
Replace PyVista plotter code with Ansys Tools Visualization Interface plotter code
17+
----------------------------------------------------------------------------------
18+
If you only need to plot simple PyVista meshes, you can directly replace your PyVista plotter code with the Ansys Tools Visualization Interface plotter code.
19+
On top of common PyVista functionalities, the Ansys Tools Visualization Interface plotter provides additional interactivity such as view buttons and mesh slicing.
20+
21+
The following code shows how to do the plotter code replacement:
22+
23+
- PyVista code:
24+
25+
.. code-block:: python
26+
27+
import pyvista as pv
28+
29+
# Create a PyVista mesh
30+
mesh = pv.Cube()
31+
32+
# Create a plotter
33+
pl = pv.Plotter()
34+
35+
# Add the mesh to the plotter
36+
pl.add_mesh(mesh)
37+
38+
# Show the plotter
39+
pl.show()
40+
41+
- Ansys Tools Visualization Interface code:
42+
43+
.. code-block:: python
44+
45+
import pyvista as pv
46+
from ansys.tools.visualization_interface import Plotter
47+
48+
# Create a PyVista mesh
49+
mesh = pv.Cube()
50+
51+
# Create a plotter
52+
pl = Plotter()
53+
54+
# Add the mesh to the plotter
55+
pl.plot(mesh)
56+
57+
# Show the plotter
58+
pl.show()
59+
60+
61+
Convert your custom meshes to objects usable by the Ansys Tools Visualization Interface plotter
62+
-----------------------------------------------------------------------------------------------
63+
64+
Your custom object must have a method that returns a PyVista mesh and a method that exposes a ``name`` or ``id`` attribute of your object:
65+
66+
.. code-block:: python
67+
68+
class CustomObject:
69+
def __init__(self):
70+
self.name = "CustomObject"
71+
self.mesh = pv.Cube(center=(1, 1, 0))
72+
73+
def get_mesh(self):
74+
return self.mesh
75+
76+
def name(self):
77+
return self.name
78+
79+
80+
You then need to create a :class:`~ansys.tools.visualization_interface.types.mesh_object_plot.MeshObjectPlot` instance that relates the PyVista mesh with your custom object:
81+
82+
.. code-block:: python
83+
84+
from ansys.tools.visualization_interface import MeshObjectPlot
85+
86+
custom_object = CustomObject()
87+
mesh_object_plot = MeshObjectPlot(
88+
custom_object=custom_object,
89+
mesh=custom_object.get_mesh(),
90+
)
91+
92+
With this, you can use the Ansys Tools Visualization Interface plotter to visualize your custom object. It enables interactivity such as picking and hovering.
93+
94+
95+
Customize the PyVista backend
96+
-----------------------------
97+
98+
You can customize the backend of the Ansys Tools Visualization Interface plotter to enable or turn off certain functionalities. The following code shows how to enable picking:
99+
100+
.. code-block:: python
101+
102+
from ansys.tools.visualization_interface import Plotter
103+
from ansys.tools.visualization_interface.backends import PyVistaBackend
104+
105+
backend = PyVistaBackend(allow_picking=True)
106+
107+
# Create a plotter
108+
pl = Plotter(backend=backend)
109+
110+
# Add the MeshObjectPlot instance to the plotter
111+
pl.plot(mesh_object_plot)
112+
113+
# Show the plotter
114+
pl.show()
115+
116+
If you want to customize the backend even more, you can create your own backend by inheriting from the :class:`~ansys.tools.visualization_interface.backends.pyvista.PyVistaBackendInterface` class
117+
and implementing the required methods:
118+
119+
.. code-block:: python
120+
121+
@abstractmethod
122+
def plot_iter(self, plottable_object: Any, name_filter: str = None, **plotting_options):
123+
"""Plot one or more compatible objects to the plotter.
124+
125+
Parameters
126+
----------
127+
plottable_object : Any
128+
One or more objects plot.
129+
name_filter : str, default: None.
130+
Regular expression with the desired name or names to include in the plotter.
131+
**plotting_options : dict, default: None
132+
Keyword arguments. For allowable keyword arguments, see the
133+
:meth:`Plotter.add_mesh <pyvista.Plotter.add_mesh>` method.
134+
135+
"""
136+
pass
137+
138+
139+
@abstractmethod
140+
def plot(self, plottable_object: Any, name_filter: str = None, **plotting_options):
141+
"""Plot a single object to the plotter.
142+
143+
Parameters
144+
----------
145+
plottable_object : Any
146+
Object to plot.
147+
name_filter : str
148+
Regular expression with the desired name or names to include in the plotter.
149+
**plotting_options : dict, default: None
150+
Keyword arguments. For allowable keyword arguments, see the
151+
:meth:`Plotter.add_mesh <pyvista.Plotter.add_mesh>` method.
152+
153+
"""
154+
pass
155+
156+
157+
The rest of the methods are implemented for you. This ensures that while you can customize what you need for plotting,
158+
the rest of the functionalities still work as expected. For more information, see the backend documentation. If you
159+
need to even go further, you can create your own plotter by inheriting from the :class:`~ansys.tools.visualization_interface.backends._base.BaseBackend` class and implementing the required methods,
160+
although this may break existing features.
161+
162+
Customize the picker or hover behavior
163+
--------------------------------------
164+
You can customize the picker of the Ansys Tools Visualization Interface plotter to decide what happens when you pick or hover over an object.
165+
For example, if you want to print the name of the picked object, you can do it as described in the :ref:`sphx_glr_examples_00-basic-pyvista-examples_custom_picker.py` example.
166+
167+
Use the PyVista Qt backend
168+
--------------------------
169+
You can use the PyVista Qt backend with the Ansys Tools Visualization Interface plotter. To do this, you must set the PyVista backend to Qt
170+
before creating the plotter:
171+
172+
.. code-block:: python
173+
174+
cube = pv.Cube()
175+
pv_backend = PyVistaBackend(use_qt=True, show_qt=True)
176+
pl = Plotter(backend=pv_backend)
177+
pl.plot(cube)
178+
pl.backend.enable_widgets()
179+
pv_backend.scene.show()
180+
181+
You can then integrate the plotter into a PyQt or PySide app by disabling the ``show_qt`` parameter.
182+
For more information about this, see the `PyVista documentation <https://qtdocs.pyvista.org/>`_.
183+
184+
185+
Documentation configuration migration
186+
=====================================
187+
188+
This topic explains how to migrate from the PyVista documentation configuration to the new Ansys Tools Visualization Interface documentation configuration.
189+
190+
1. Add environment variables for documentation:
191+
192+
.. code-block:: python
193+
194+
os.environ["PYANSYS_VISUALIZER_DOC_MODE"] = "true"
195+
os.environ["PYANSYS_VISUALIZER_HTML_BACKEND"] = "true"
196+
197+
2. Use PyVista DynamicScraper:
198+
199+
.. code-block:: python
200+
201+
from pyvista.plotting.utilities.sphinx_gallery import DynamicScraper
202+
203+
sphinx_gallery_conf = {
204+
"image_scrapers": (DynamicScraper()),
205+
}
206+
207+
3. Add PyVista viewer directive to extensions:
208+
209+
.. code-block:: python
210+
211+
extensions = ["pyvista.ext.viewer_directive"]
212+
213+
4. Make sure you are executing the notebook cells:
214+
215+
.. code-block:: python
216+
217+
nbsphinx_execute = "always"
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
(?i)Ansys
22
pytest
3-
unhovered
3+
unhovered
4+
5+
t.M
6+
a.P
7+
e.B
8+
r.A
11.8 MB
Binary file not shown.

src/ansys/tools/visualization_interface/backends/pyvista/pyvista.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ def plot_iter(self, plottable_object: Any, name_filter: str = None, **plotting_o
509509
plottable_object : Any
510510
One or more objects to add.
511511
name_filter : str, default: None.
512-
Regular expression with the desired name or names to include in the plotter.
512+
Regular expression with the desired name or names to include in the plotter.
513513
**plotting_options : dict, default: None
514514
Keyword arguments. For allowable keyword arguments, see the
515515
:meth:`Plotter.add_mesh <pyvista.Plotter.add_mesh>` method.
@@ -524,7 +524,7 @@ def plot(self, plottable_object: Any, name_filter: str = None, **plotting_option
524524
Parameters
525525
----------
526526
plottable_object : Any
527-
Object to add.
527+
Object to plot.
528528
name_filter : str
529529
Regular expression with the desired name or names to include in the plotter.
530530
**plotting_options : dict, default: None
@@ -618,7 +618,7 @@ def plot(self, plottable_object: Any, name_filter: str = None, **plotting_option
618618
Parameters
619619
----------
620620
plottable_object : Any
621-
Object to add.
621+
Object to plot.
622622
name_filter : str
623623
Regular expression with the desired name or names to include in the plotter.
624624
**plotting_options : dict, default: None

0 commit comments

Comments
 (0)