.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "tutorials\plot\plot_mesh.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_tutorials_plot_plot_mesh.py: .. _ref_tutorials_plot_mesh: Plot a mesh =========== This tutorial shows different commands for plotting a mesh without data. A mesh is represented in DPF by a :class:`MeshedRegion`. You can store multiple ``MeshedRegion`` objects in a DPF collection called :class:`MeshesContainer`. You can obtain a ``MeshedRegion`` by creating your own from scratch or by getting it from a result file. For more information, see the :ref:`ref_tutorials_create_a_mesh_from_scratch` and :ref:`ref_tutorials_get_mesh_from_result_file` tutorials. PyDPF-Core has a variety of plotting methods for generating 3D plots with Python. These methods use VTK and leverage the `PyVista `_ library. .. GENERATED FROM PYTHON SOURCE LINES 46-48 Load data to plot ------------------ .. GENERATED FROM PYTHON SOURCE LINES 48-58 .. code-block:: Python import ansys.dpf.core as dpf from ansys.dpf.core import examples, operators as ops # Download and get the path to an example result file result_file_path_1 = examples.download_piston_rod() # Create a model from the result file model_1 = dpf.Model(data_sources=result_file_path_1) .. GENERATED FROM PYTHON SOURCE LINES 59-73 Plot a model ------------ You can directly plot the overall mesh loaded by the model with :py:meth:`Model.plot()`. .. note:: The :class:`DpfPlotter` displays the mesh with edges, lighting and axis widget enabled by default. You can pass additional PyVista arguments to all plotting methods to change the default behavior (see options for `pyvista.plot() `_), such as ``title``, ``text``, ``off_screen``, ``screenshot``, or ``window_size``. .. GENERATED FROM PYTHON SOURCE LINES 73-76 .. code-block:: Python model_1.plot() .. image-sg:: /tutorials/plot/images/sphx_glr_plot_mesh_001.png :alt: plot mesh :srcset: /tutorials/plot/images/sphx_glr_plot_mesh_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none ([], ) .. GENERATED FROM PYTHON SOURCE LINES 77-85 Plot a single mesh ------------------ Get the mesh ^^^^^^^^^^^^ Get the :class:`MeshedRegion` object of the model. .. GENERATED FROM PYTHON SOURCE LINES 85-88 .. code-block:: Python meshed_region_1 = model_1.metadata.meshed_region .. GENERATED FROM PYTHON SOURCE LINES 89-95 Plot the mesh using ``MeshedRegion.plot()`` ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use the :py:meth:`MeshedRegion.plot()` method. .. GENERATED FROM PYTHON SOURCE LINES 95-98 .. code-block:: Python meshed_region_1.plot() .. image-sg:: /tutorials/plot/images/sphx_glr_plot_mesh_002.png :alt: plot mesh :srcset: /tutorials/plot/images/sphx_glr_plot_mesh_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none ([], ) .. GENERATED FROM PYTHON SOURCE LINES 99-110 Plot the mesh using ``DpfPlotter`` ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ To plot the mesh using :class:`DpfPlotter`: 1. Create an instance of ``DpfPlotter``. 2. Add the ``MeshedRegion`` to the scene using :py:meth:`add_mesh()`. 3. Render and show the figure using :py:meth:`show_figure()`. .. GENERATED FROM PYTHON SOURCE LINES 110-115 .. code-block:: Python plotter_1 = dpf.plotter.DpfPlotter() plotter_1.add_mesh(meshed_region=meshed_region_1) plotter_1.show_figure() .. image-sg:: /tutorials/plot/images/sphx_glr_plot_mesh_003.png :alt: plot mesh :srcset: /tutorials/plot/images/sphx_glr_plot_mesh_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none ([], ) .. GENERATED FROM PYTHON SOURCE LINES 116-118 You can also plot data contours on a mesh. For more information, see :ref:`ref_tutorials_plot_contour`. .. GENERATED FROM PYTHON SOURCE LINES 120-136 Plot several meshes -------------------- Build a collection of meshes ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ There are different ways to obtain a :class:`MeshesContainer`. Here, we use the :class:`split_mesh` operator to split the mesh based on the material of each element. This operator returns a ``MeshesContainer`` with meshes labeled according to the split criterion. In our case, each mesh has a ``mat`` label. For more information about how to get a split mesh, see the :ref:`ref_tutorials_split_mesh` and :ref:`ref_tutorials_extract_mesh_in_split_parts` tutorials. .. GENERATED FROM PYTHON SOURCE LINES 136-140 .. code-block:: Python meshes = ops.mesh.split_mesh(mesh=meshed_region_1, property="mat").eval() print(meshes) .. rst-class:: sphx-glr-script-out .. code-block:: none DPF Meshes Container with 2 mesh(es) defined on labels: body mat with: - mesh 0 {mat: 1, body: 1, } with 17281 nodes and 9026 elements. - mesh 1 {mat: 2, body: 2, } with 17610 nodes and 9209 elements. .. GENERATED FROM PYTHON SOURCE LINES 141-148 Plot the meshes ^^^^^^^^^^^^^^^^ Use the :py:meth:`MeshesContainer.plot()` method. This plots all ``MeshedRegion`` objects in the ``MeshesContainer`` and colors them based on the property used to split the mesh. .. GENERATED FROM PYTHON SOURCE LINES 148-151 .. code-block:: Python meshes.plot() .. image-sg:: /tutorials/plot/images/sphx_glr_plot_mesh_004.png :alt: plot mesh :srcset: /tutorials/plot/images/sphx_glr_plot_mesh_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none ([], ) .. GENERATED FROM PYTHON SOURCE LINES 152-154 You can also plot data on a collection of meshes. For more information, see :ref:`ref_tutorials_plot_contour`. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 10.783 seconds) .. _sphx_glr_download_tutorials_plot_plot_mesh.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_mesh.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_mesh.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_mesh.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_