|
36 | 36 | import tempfile |
37 | 37 | from typing import TYPE_CHECKING, List, Union |
38 | 38 | import warnings |
| 39 | +from xml.etree.ElementInclude import include |
39 | 40 |
|
40 | 41 | import numpy as np |
41 | 42 |
|
@@ -234,6 +235,37 @@ def get_label_at_grid_point(index): |
234 | 235 | ) |
235 | 236 | return label_actors |
236 | 237 |
|
| 238 | + def add_scoping( |
| 239 | + self, |
| 240 | + scoping: dpf.core.Scoping, |
| 241 | + mesh: dpf.core.MeshedRegion, |
| 242 | + show_mesh: bool = False, |
| 243 | + **kwargs, |
| 244 | + ): |
| 245 | + # Add the mesh to the scene with low opacity |
| 246 | + if show_mesh: |
| 247 | + self._plotter.add_mesh(mesh=mesh.grid, opacity=0.3) |
| 248 | + |
| 249 | + scoping_mesh = None |
| 250 | + |
| 251 | + # If the scoping is nodal, use the add_points_label method |
| 252 | + if scoping.location == locations.nodal: |
| 253 | + node_indexes = np.where(np.isin(mesh.nodes.scoping.ids, scoping.ids))[0] |
| 254 | + # grid_points = [mesh.grid.points[node_index] for node_index in node_indexes] |
| 255 | + scoping_mesh = mesh.grid.extract_points(ind=node_indexes, include_cells=False) |
| 256 | + # If the scoping is elemental, extract their edges and use active scalars to color them |
| 257 | + if scoping.location == locations.elemental: |
| 258 | + element_indexes = np.where(np.isin(mesh.elements.scoping.ids, scoping.ids))[0] |
| 259 | + scoping_mesh = mesh.grid.extract_cells(ind=element_indexes) |
| 260 | + |
| 261 | + # If the scoping is faces, extract their edges and use active scalars to color them |
| 262 | + if scoping.location == locations.faces: |
| 263 | + raise NotImplementedError("Cannot plot a face scoping.") |
| 264 | + |
| 265 | + # Filter kwargs |
| 266 | + kwargs_in = _sort_supported_kwargs(bound_method=self._plotter.add_mesh, **kwargs) |
| 267 | + self._plotter.add_mesh(mesh=scoping_mesh, **kwargs_in) |
| 268 | + |
237 | 269 | def add_field( |
238 | 270 | self, |
239 | 271 | field, |
@@ -689,6 +721,55 @@ def add_field( |
689 | 721 | **kwargs, |
690 | 722 | ) |
691 | 723 |
|
| 724 | + def add_scoping( |
| 725 | + self, |
| 726 | + scoping: dpf.core.Scoping, |
| 727 | + mesh: dpf.core.MeshedRegion, |
| 728 | + show_mesh: bool = False, |
| 729 | + **kwargs, |
| 730 | + ): |
| 731 | + """Add a scoping to the plotter. |
| 732 | +
|
| 733 | + A mesh is required to translate the scoping into entities to plot. |
| 734 | + Tou can plot the mesh along with the scoping entities using ``show_mesh``. |
| 735 | +
|
| 736 | + Parameters |
| 737 | + ---------- |
| 738 | + scoping: |
| 739 | + Scoping with a mesh-based location and IDs of entities to plot. |
| 740 | + mesh: |
| 741 | + ``MeshedRegion`` to plot the field on. |
| 742 | + show_mesh: |
| 743 | + Whether to show the mesh along with the scoping entities. |
| 744 | + **kwargs : optional |
| 745 | + Additional keyword arguments for the plotter. More information |
| 746 | + are available at :func:`pyvista.plot`. |
| 747 | +
|
| 748 | + Examples |
| 749 | + -------- |
| 750 | + >>> from ansys.dpf import core as dpf |
| 751 | + >>> from ansys.dpf.core import examples |
| 752 | + >>> model = dpf.Model(examples.download_cfx_mixing_elbow()) |
| 753 | + >>> mesh = model.metadata.meshed_region |
| 754 | + >>> node_scoping = dpf.Scoping( |
| 755 | + ... location=dpf.locations.nodal, |
| 756 | + ... ids=mesh.nodes.scoping.ids[0:100] |
| 757 | + ...) |
| 758 | + >>> element_scoping = dpf.Scoping( |
| 759 | + ... location=dpf.locations.elemental, |
| 760 | + ... ids=mesh.elements.scoping.ids[0:100] |
| 761 | + ...) |
| 762 | + >>> from ansys.dpf.core.plotter import DpfPlotter |
| 763 | + >>> plt = DpfPlotter() |
| 764 | + >>> plt.add_scoping(node_scoping, mesh, show_mesh=True, color="red") |
| 765 | + >>> plt.add_scoping(element_scoping, mesh, color="green") |
| 766 | + >>> plt.show_figure() |
| 767 | +
|
| 768 | + """ |
| 769 | + self._internal_plotter.add_scoping( |
| 770 | + scoping=scoping, mesh=mesh, show_mesh=show_mesh, **kwargs |
| 771 | + ) |
| 772 | + |
692 | 773 | def show_figure(self, **kwargs): |
693 | 774 | """Plot the figure built by the plotter object. |
694 | 775 |
|
|
0 commit comments