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