1
1
"""Plotting helper for MAPDL using pyvista"""
2
+ from warnings import warn
3
+
2
4
import numpy as np
3
5
4
6
from ansys .mapdl .core import _HAS_PYVISTA
@@ -158,6 +160,7 @@ def _general_plotter(
158
160
font_family = None ,
159
161
text_color = None ,
160
162
theme = None ,
163
+ plotter = None ,
161
164
):
162
165
"""General pymapdl plotter for APDL geometry and meshes.
163
166
@@ -285,6 +288,13 @@ def _general_plotter(
285
288
PyVista theme. Defaults to `PyMAPDL theme <https://github
286
289
.com/pyansys/pyansys-sphinx-theme>`_.
287
290
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
+
288
298
Returns
289
299
-------
290
300
pyvista.Plotter
@@ -321,13 +331,23 @@ def _general_plotter(
321
331
if theme is None :
322
332
theme = MapdlTheme ()
323
333
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
+ )
325
345
326
346
if background :
327
- pl .set_background (background )
347
+ plotter .set_background (background )
328
348
329
349
for point in points :
330
- pl .add_points (
350
+ plotter .add_points (
331
351
point ["points" ],
332
352
scalars = point .get ("scalars" , None ),
333
353
color = color ,
@@ -346,7 +366,7 @@ def _general_plotter(
346
366
)
347
367
348
368
for mesh in meshes :
349
- pl .add_mesh (
369
+ plotter .add_mesh (
350
370
mesh ["mesh" ],
351
371
scalars = mesh .get ("scalars" ),
352
372
scalar_bar_args = scalar_bar_args ,
@@ -375,7 +395,7 @@ def _general_plotter(
375
395
points , idx , _ = unique_rows (np .array (label ["points" ]))
376
396
labels = np .array (label ["labels" ])[idx ].tolist ()
377
397
378
- pl .add_point_labels (
398
+ plotter .add_point_labels (
379
399
points ,
380
400
labels ,
381
401
show_points = False ,
@@ -386,15 +406,15 @@ def _general_plotter(
386
406
)
387
407
388
408
if cpos :
389
- pl .camera_position = cpos
409
+ plotter .camera_position = cpos
390
410
391
411
if show_bounds :
392
- pl .show_bounds ()
412
+ plotter .show_bounds ()
393
413
394
414
if show_axes :
395
- pl .show_axes ()
415
+ plotter .show_axes ()
396
416
397
- return pl
417
+ return plotter
398
418
399
419
400
420
# Using * to force all the following arguments to be keyword only.
@@ -446,6 +466,7 @@ def general_plotter(
446
466
bc_target = None ,
447
467
bc_glyph_size = None ,
448
468
bc_labels_font_size = 16 ,
469
+ plotter = None ,
449
470
):
450
471
"""General pymapdl plotter for APDL geometry and meshes.
451
472
@@ -625,6 +646,13 @@ def general_plotter(
625
646
Size of the text on the boundary conditions labels.
626
647
By default it is 16.
627
648
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
+
628
656
Returns
629
657
-------
630
658
cpos or pyvista.Plotter or None
@@ -719,6 +747,7 @@ def general_plotter(
719
747
font_family = font_family ,
720
748
text_color = text_color ,
721
749
theme = theme ,
750
+ plotter = plotter ,
722
751
)
723
752
724
753
if plot_bc :
0 commit comments