Skip to content

Commit 7a21fee

Browse files
germa89akaszynski
andauthored
Adding plotter to inputs. (#1545)
* Adding plotter to inputs. * Added unit test to increase coverage * Apply suggestions from code review Thank you @akaszynski ! My bad! Co-authored-by: Alex Kaszynski <[email protected]> Co-authored-by: Alex Kaszynski <[email protected]>
1 parent 15fdbe7 commit 7a21fee

File tree

2 files changed

+79
-9
lines changed

2 files changed

+79
-9
lines changed

src/ansys/mapdl/core/plotting.py

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
"""Plotting helper for MAPDL using pyvista"""
2+
from warnings import warn
3+
24
import numpy as np
35

46
from ansys.mapdl.core import _HAS_PYVISTA
@@ -158,6 +160,7 @@ def _general_plotter(
158160
font_family=None,
159161
text_color=None,
160162
theme=None,
163+
plotter=None,
161164
):
162165
"""General pymapdl plotter for APDL geometry and meshes.
163166
@@ -285,6 +288,13 @@ def _general_plotter(
285288
PyVista theme. Defaults to `PyMAPDL theme <https://github
286289
.com/pyansys/pyansys-sphinx-theme>`_.
287290
291+
plotter : pyvista.Plotter, optional
292+
If a :class:`pyvista.Plotter` not is provided, then creates its
293+
own plotter. If a :class:`pyvista.Plotter` is provided, the arguments
294+
``notebook``, ``off_screen`` and ``theme`` are ignored, since
295+
they should be set when instantiated the provided plotter.
296+
Defaults to ``None`` (create the Plotter object).
297+
288298
Returns
289299
-------
290300
pyvista.Plotter
@@ -321,13 +331,23 @@ def _general_plotter(
321331
if theme is None:
322332
theme = MapdlTheme()
323333

324-
pl = pv.Plotter(off_screen=off_screen, notebook=notebook, theme=theme)
334+
if not (plotter is None or isinstance(plotter, pv.Plotter)):
335+
raise TypeError("The kwarg 'plotter' can only accept pv.Plotter objects.")
336+
337+
if not plotter:
338+
plotter = pv.Plotter(off_screen=off_screen, notebook=notebook, theme=theme)
339+
else:
340+
if off_screen or notebook or theme:
341+
warn(
342+
"The kwargs 'off_screen', 'notebook' and 'theme' are ignored when using 'plotter' kwarg.",
343+
UserWarning,
344+
)
325345

326346
if background:
327-
pl.set_background(background)
347+
plotter.set_background(background)
328348

329349
for point in points:
330-
pl.add_points(
350+
plotter.add_points(
331351
point["points"],
332352
scalars=point.get("scalars", None),
333353
color=color,
@@ -346,7 +366,7 @@ def _general_plotter(
346366
)
347367

348368
for mesh in meshes:
349-
pl.add_mesh(
369+
plotter.add_mesh(
350370
mesh["mesh"],
351371
scalars=mesh.get("scalars"),
352372
scalar_bar_args=scalar_bar_args,
@@ -375,7 +395,7 @@ def _general_plotter(
375395
points, idx, _ = unique_rows(np.array(label["points"]))
376396
labels = np.array(label["labels"])[idx].tolist()
377397

378-
pl.add_point_labels(
398+
plotter.add_point_labels(
379399
points,
380400
labels,
381401
show_points=False,
@@ -386,15 +406,15 @@ def _general_plotter(
386406
)
387407

388408
if cpos:
389-
pl.camera_position = cpos
409+
plotter.camera_position = cpos
390410

391411
if show_bounds:
392-
pl.show_bounds()
412+
plotter.show_bounds()
393413

394414
if show_axes:
395-
pl.show_axes()
415+
plotter.show_axes()
396416

397-
return pl
417+
return plotter
398418

399419

400420
# Using * to force all the following arguments to be keyword only.
@@ -446,6 +466,7 @@ def general_plotter(
446466
bc_target=None,
447467
bc_glyph_size=None,
448468
bc_labels_font_size=16,
469+
plotter=None,
449470
):
450471
"""General pymapdl plotter for APDL geometry and meshes.
451472
@@ -625,6 +646,13 @@ def general_plotter(
625646
Size of the text on the boundary conditions labels.
626647
By default it is 16.
627648
649+
plotter : pyvista.Plotter, optional
650+
If a :class:`pyvista.Plotter` not is provided, then creates its
651+
own plotter. If a :class:`pyvista.Plotter` is provided, the arguments
652+
``notebook``, ``off_screen`` and ``theme`` are ignored, since
653+
they should be set when instantiated the provided plotter.
654+
Defaults to ``None`` (create the Plotter object).
655+
628656
Returns
629657
-------
630658
cpos or pyvista.Plotter or None
@@ -719,6 +747,7 @@ def general_plotter(
719747
font_family=font_family,
720748
text_color=text_color,
721749
theme=theme,
750+
plotter=plotter,
722751
)
723752

724753
if plot_bc:

tests/test_plotting.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,3 +469,44 @@ def debug_orders_1(pl, point):
469469
"S", "P", _debug=lambda x: debug_orders_1(x, point=point), tolerance=0.1
470470
)
471471
assert selected == []
472+
473+
474+
def test_plotter_input(mapdl, make_block):
475+
pl = Plotter(off_screen=True)
476+
# because in CICD we use 'screen_off', this will trigger a warning,
477+
# since using 'plotter' will overwrite this kwarg.
478+
with pytest.warns(UserWarning):
479+
pl2 = mapdl.eplot(return_plotter=True, plotter=pl)
480+
assert pl == pl2
481+
assert pl is pl2
482+
483+
# invalid plotter type
484+
with pytest.raises(TypeError):
485+
pl2 = mapdl.eplot(return_plotter=True, plotter=[])
486+
487+
488+
def test_cpos_input(mapdl, make_block):
489+
cpos = [
490+
(0.3914, 0.4542, 0.7670),
491+
(0.0243, 0.0336, -0.0222),
492+
(-0.2148, 0.8998, -0.3796),
493+
]
494+
495+
cpos1 = mapdl.eplot(cpos=cpos, return_cpos=True)
496+
assert np.allclose(np.array(cpos), np.array([each for each in cpos1]), rtol=1e-4)
497+
498+
499+
def test_show_bounds(mapdl, make_block):
500+
default_bounds = [-1.0, 1.0, -1.0, 1.0, -1.0, 1.0]
501+
pl = mapdl.eplot(show_bounds=True, return_plotter=True)
502+
503+
assert pl.bounds
504+
assert len(pl.bounds) == 6
505+
assert pl.bounds != default_bounds
506+
507+
508+
def test_background(mapdl, make_block):
509+
default_color = "#4c4c4cff"
510+
pl = mapdl.eplot(background="red", return_plotter=True)
511+
assert pl.background_color != default_color
512+
assert pl.background_color == "red"

0 commit comments

Comments
 (0)