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