Skip to content

Commit 9117dc3

Browse files
committed
Add ScopingsContainer.plot
1 parent 92d7508 commit 9117dc3

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

src/ansys/dpf/core/scopings_container.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,16 @@
2828
Contains classes associated to the DPF ScopingsContainer
2929
"""
3030

31+
from __future__ import annotations
32+
33+
from typing import TYPE_CHECKING
34+
3135
from ansys.dpf.core import scoping
3236
from ansys.dpf.core.collection_base import CollectionBase
3337

38+
if TYPE_CHECKING:
39+
from ansys.dpf.core import MeshedRegion, MeshesContainer
40+
3441

3542
class ScopingsContainer(CollectionBase[scoping.Scoping]):
3643
"""A class used to represent a ScopingsContainer which contains scopings split on a given space.
@@ -125,3 +132,69 @@ def add_scoping(self, label_space, scoping):
125132
DPF scoping to add.
126133
"""
127134
return super()._add_entry(label_space, scoping)
135+
136+
def plot(
137+
self,
138+
mesh: MeshedRegion | MeshesContainer,
139+
show_mesh: bool = False,
140+
colors: list[str] = None,
141+
**kwargs,
142+
):
143+
"""Plot the entities of the mesh or meshes corresponding to the scopings.
144+
145+
Parameters
146+
----------
147+
mesh:
148+
Mesh or meshes to use to translate the scopings into mesh entities.
149+
Associates each scoping to a mesh using labels if ``mesh`` is a collection of meshes.
150+
show_mesh:
151+
Whether to also show the mesh with low opacity.
152+
colors:
153+
List of colors to use for the scoping entities.
154+
**kwargs : optional
155+
Additional keyword arguments for the plotter. More information
156+
are available at :func:`pyvista.plot`.
157+
158+
Returns
159+
-------
160+
(cpos, image):
161+
Returns what the pyvista.show() method returns based on arguments.
162+
163+
Examples
164+
--------
165+
>>> from ansys.dpf import core as dpf
166+
>>> from ansys.dpf.core import examples
167+
>>> model = dpf.Model(examples.download_cfx_mixing_elbow())
168+
>>> mesh = model.metadata.meshed_region
169+
>>> node_scoping_1 = dpf.Scoping(
170+
... location=dpf.locations.nodal,
171+
... ids=mesh.nodes.scoping.ids[0:100]
172+
...)
173+
>>> node_scoping_2 = dpf.Scoping(
174+
... location=dpf.locations.nodal,
175+
... ids=mesh.nodes.scoping.ids[300:400]
176+
...)
177+
>>> node_sc = dpf.ScopingsContainer()
178+
>>> node_sc.add_label(label="scoping", default_value=1)
179+
>>> node_sc.add_scoping(label_space={"scoping": 1}, scoping=node_scoping_1)
180+
>>> node_sc.add_scoping(label_space={"scoping": 2}, scoping=node_scoping_2)
181+
>>> node_sc.plot(mesh=mesh, show_mesh=True)
182+
183+
"""
184+
from itertools import cycle
185+
186+
from ansys.dpf.core.plotter import DpfPlotter
187+
188+
colors_cycle = cycle(
189+
colors if colors else ["red", "blue", "green", "orange", "black", "yellow"]
190+
)
191+
plt = DpfPlotter(**kwargs)
192+
for i, scoping_i in enumerate(self):
193+
plt.add_scoping(
194+
scoping=scoping_i,
195+
mesh=mesh,
196+
color=next(colors_cycle),
197+
show_mesh=show_mesh if i == 0 else False,
198+
**kwargs,
199+
)
200+
return plt.show_figure(**kwargs)

0 commit comments

Comments
 (0)