Skip to content

Commit 773cfd6

Browse files
feat: Add name filtering in the plotter (#735)
Co-authored-by: Roberto Pastor Muela <37798125+RobPasMue@users.noreply.github.com>
1 parent 1ac036d commit 773cfd6

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

src/ansys/geometry/core/plotting/plotter.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
# SOFTWARE.
2222
"""Provides plotting for various PyAnsys Geometry objects."""
23+
import re
24+
2325
from beartype.typing import Any, Dict, List, Optional
2426
import numpy as np
2527
import pyvista as pv
@@ -349,6 +351,7 @@ def add(
349351
object: Any,
350352
merge_bodies: bool = False,
351353
merge_components: bool = False,
354+
filter: str = None,
352355
**plotting_options,
353356
) -> Dict[pv.Actor, GeomObjectPlot]:
354357
"""
@@ -369,6 +372,8 @@ def add(
369372
Whether to merge the component into a single dataset. When
370373
``True``, all the individual bodies are effectively combined
371374
into a single dataset without any hierarchy.
375+
filter : str, default: None
376+
Regular expression with the desired name or names you want to include in the plotter.
372377
**plotting_options : dict, default: None
373378
Keyword arguments. For allowable keyword arguments, see the
374379
:meth:`Plotter.add_mesh <pyvista.Plotter.add_mesh>` method.
@@ -379,7 +384,13 @@ def add(
379384
Mapping between the ~pyvista.Actor and the PyAnsys Geometry object.
380385
"""
381386
logger.debug(f"Adding object type {type(object)} to the PyVista plotter")
387+
if filter:
388+
if hasattr(object, "name"):
389+
if not re.search(filter, object.name):
390+
logger.debug(f"Name {object.name} not found in regex {filter}.")
391+
return self._geom_object_actors_map
382392

393+
# Check what kind of object we are dealing with
383394
if isinstance(object, List) and isinstance(object[0], pv.PolyData):
384395
self.add_sketch_polydata(object, **plotting_options)
385396
elif isinstance(object, pv.PolyData):
@@ -401,6 +412,7 @@ def add_list(
401412
plotting_list: List[Any],
402413
merge_bodies: bool = False,
403414
merge_components: bool = False,
415+
filter: str = None,
404416
**plotting_options,
405417
) -> Dict[pv.Actor, GeomObjectPlot]:
406418
"""
@@ -421,6 +433,8 @@ def add_list(
421433
Whether to merge each body into a single dataset. When ``True``,
422434
all the faces of each individual body are effectively combined
423435
into a single dataset without separating faces.
436+
filter : str, default: None
437+
Regular expression with the desired name or names you want to include in the plotter.
424438
**plotting_options : dict, default: None
425439
Keyword arguments. For allowable keyword arguments, see the
426440
:meth:`Plotter.add_mesh <pyvista.Plotter.add_mesh>` method.
@@ -431,7 +445,7 @@ def add_list(
431445
Mapping between the ~pyvista.Actor and the PyAnsys Geometry objects.
432446
"""
433447
for object in plotting_list:
434-
_ = self.add(object, merge_bodies, merge_components, **plotting_options)
448+
_ = self.add(object, merge_bodies, merge_components, filter, **plotting_options)
435449
return self._geom_object_actors_map
436450

437451
def show(

src/ansys/geometry/core/plotting/plotter_helper.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ def plot(
244244
merge_bodies: bool = False,
245245
merge_component: bool = False,
246246
view_2d: Dict = None,
247+
filter: str = None,
247248
**plotting_options,
248249
) -> List[Any]:
249250
"""
@@ -268,6 +269,8 @@ def plot(
268269
dataset without any hierarchy.
269270
view_2d : Dict, default: None
270271
Dictionary with the plane and the viewup vectors of the 2D plane.
272+
filter : str, default: None
273+
Regular expression with the desired name or names you want to include in the plotter.
271274
**plotting_options : dict, default: None
272275
Keyword arguments. For allowable keyword arguments, see the
273276
:meth:`Plotter.add_mesh <pyvista.Plotter.add_mesh>` method.
@@ -280,11 +283,11 @@ def plot(
280283
if isinstance(object, List) and not isinstance(object[0], pv.PolyData):
281284
logger.debug("Plotting objects in list...")
282285
self._geom_object_actors_map = self._pl.add_list(
283-
object, merge_bodies, merge_component, **plotting_options
286+
object, merge_bodies, merge_component, filter, **plotting_options
284287
)
285288
else:
286289
self._geom_object_actors_map = self._pl.add(
287-
object, merge_bodies, merge_component, **plotting_options
290+
object, merge_bodies, merge_component, filter, **plotting_options
288291
)
289292

290293
self.compute_edge_object_map()

tests/integration/test_plotter.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,3 +635,25 @@ def test_visualization_polydata():
635635
assert box.visualization_polydata.n_cells == 1
636636
assert box.visualization_polydata.n_points == 4
637637
assert box.visualization_polydata.n_open_edges == 4
638+
639+
640+
def test_name_filter(modeler: Modeler, verify_image_cache):
641+
# init modeler
642+
design = modeler.create_design("Multiplot")
643+
644+
# define cylinder
645+
cyl_sketch = Sketch()
646+
cyl_sketch.circle(Point2D([-20, 5], UNITS.m), Quantity(10, UNITS.m))
647+
cyl_body = design.extrude_sketch("JustACyl", cyl_sketch, Quantity(10, UNITS.m))
648+
649+
# define box
650+
body_sketch = Sketch()
651+
body_sketch.box(Point2D([10, 10], UNITS.m), Quantity(10, UNITS.m), Quantity(10, UNITS.m))
652+
box_body = design.extrude_sketch("JustABox", body_sketch, Quantity(10, UNITS.m))
653+
654+
# plot together
655+
PlotterHelper().plot(
656+
[cyl_body, box_body],
657+
filter="Cyl",
658+
screenshot=Path(IMAGE_RESULTS_DIR, "test_name_filter.png"),
659+
)

0 commit comments

Comments
 (0)