22
22
"""Provides plotting for various PyAnsys Geometry objects."""
23
23
import re
24
24
25
- from beartype .typing import Any , Dict , List , Optional
25
+ from beartype .typing import Any , Dict , List , Optional , Union
26
26
import numpy as np
27
27
import pyvista as pv
28
28
from pyvista .plotting .plotter import Plotter as PyVistaPlotter
@@ -231,6 +231,10 @@ def plot_sketch(
231
231
if show_frame :
232
232
self .plot_frame (sketch ._plane )
233
233
234
+ if "clipping_plane" in plotting_options :
235
+ logger .warning ("Clipping is not available in Sketch objects." )
236
+ plotting_options .pop ("clipping_plane" )
237
+
234
238
self .add_sketch_polydata (sketch .sketch_polydata_faces (), opacity = 0.7 , ** plotting_options )
235
239
self .add_sketch_polydata (sketch .sketch_polydata_edges (), ** plotting_options )
236
240
@@ -281,6 +285,9 @@ def add_body(
281
285
# Use the default PyAnsys Geometry add_mesh arguments
282
286
self .__set_add_mesh_defaults (plotting_options )
283
287
dataset = body .tessellate (merge = merge )
288
+ if "clipping_plane" in plotting_options :
289
+ dataset = self .clip (dataset , plotting_options .get ("clipping_plane" ))
290
+ plotting_options .pop ("clipping_plane" , None )
284
291
if isinstance (dataset , pv .MultiBlock ):
285
292
actor , _ = self .scene .add_composite (dataset , ** plotting_options )
286
293
else :
@@ -324,6 +331,11 @@ def add_component(
324
331
# Use the default PyAnsys Geometry add_mesh arguments
325
332
self .__set_add_mesh_defaults (plotting_options )
326
333
dataset = component .tessellate (merge_component = merge_component , merge_bodies = merge_bodies )
334
+
335
+ if "clipping_plane" in plotting_options :
336
+ dataset = self .clip (dataset , plotting_options ["clipping_plane" ])
337
+ plotting_options .pop ("clipping_plane" , None )
338
+
327
339
if isinstance (dataset , pv .MultiBlock ):
328
340
actor , _ = self .scene .add_composite (dataset , ** plotting_options )
329
341
else :
@@ -344,8 +356,38 @@ def add_sketch_polydata(self, polydata_entries: List[pv.PolyData], **plotting_op
344
356
:meth:`Plotter.add_mesh <pyvista.Plotter.add_mesh>` method.
345
357
"""
346
358
# Use the default PyAnsys Geometry add_mesh arguments
359
+ mb = pv .MultiBlock ()
347
360
for polydata in polydata_entries :
348
- self .scene .add_mesh (polydata , color = EDGE_COLOR , ** plotting_options )
361
+ mb .append (polydata )
362
+
363
+ if "clipping_plane" in plotting_options :
364
+ mb = self .clip (mb , plane = plotting_options ["clipping_plane" ])
365
+ plotting_options .pop ("clipping_plane" , None )
366
+
367
+ self .scene .add_mesh (mb , color = EDGE_COLOR , ** plotting_options )
368
+
369
+ def clip (
370
+ self , mesh : Union [pv .PolyData , pv .MultiBlock ], plane : Plane = None
371
+ ) -> Union [pv .PolyData , pv .MultiBlock ]:
372
+ """
373
+ Clip the passed mesh with a plane.
374
+
375
+ Parameters
376
+ ----------
377
+ mesh : Union[pv.PolyData, pv.MultiBlock]
378
+ Mesh you want to clip.
379
+ normal : str, optional
380
+ Plane you want to use for clipping, by default "x".
381
+ Available options: ["x", "-x", "y", "-y", "z", "-z"]
382
+ origin : tuple, optional
383
+ Origin point of the plane, by default None
384
+
385
+ Returns
386
+ -------
387
+ Union[pv.PolyData,pv.MultiBlock]
388
+ The clipped mesh.
389
+ """
390
+ return mesh .clip (normal = plane .normal , origin = plane .origin )
349
391
350
392
def add_design_point (self , design_point : DesignPoint , ** plotting_options ) -> None :
351
393
"""
@@ -369,7 +411,7 @@ def add(
369
411
merge_components : bool = False ,
370
412
filter : str = None ,
371
413
** plotting_options ,
372
- ) -> Dict [ pv . Actor , GeomObjectPlot ] :
414
+ ) -> None :
373
415
"""
374
416
Add any type of object to the scene.
375
417
@@ -393,11 +435,6 @@ def add(
393
435
**plotting_options : dict, default: None
394
436
Keyword arguments. For allowable keyword arguments, see the
395
437
:meth:`Plotter.add_mesh <pyvista.Plotter.add_mesh>` method.
396
-
397
- Returns
398
- -------
399
- Dict[~pyvista.Actor, GeomObjectPlot]
400
- Mapping between the ~pyvista.Actor and the PyAnsys Geometry object.
401
438
"""
402
439
logger .debug (f"Adding object type { type (object )} to the PyVista plotter" )
403
440
if filter :
@@ -410,8 +447,14 @@ def add(
410
447
if isinstance (object , List ) and isinstance (object [0 ], pv .PolyData ):
411
448
self .add_sketch_polydata (object , ** plotting_options )
412
449
elif isinstance (object , pv .PolyData ):
450
+ if "clipping_plane" in plotting_options :
451
+ object = self .clip (object , plotting_options ["clipping_plane" ])
452
+ plotting_options .pop ("clipping_plane" , None )
413
453
self .scene .add_mesh (object , ** plotting_options )
414
454
elif isinstance (object , pv .MultiBlock ):
455
+ if "clipping_plane" in plotting_options :
456
+ object = self .clip (object , plotting_options ["clipping_plane" ])
457
+ plotting_options .pop ("clipping_plane" , None )
415
458
self .scene .add_composite (object , ** plotting_options )
416
459
elif isinstance (object , Sketch ):
417
460
self .plot_sketch (object , ** plotting_options )
@@ -423,7 +466,6 @@ def add(
423
466
self .add_design_point (object , ** plotting_options )
424
467
else :
425
468
logger .warning (f"Object type { type (object )} can not be plotted." )
426
- return self ._geom_object_actors_map
427
469
428
470
def add_list (
429
471
self ,
@@ -432,7 +474,7 @@ def add_list(
432
474
merge_components : bool = False ,
433
475
filter : str = None ,
434
476
** plotting_options ,
435
- ) -> Dict [ pv . Actor , GeomObjectPlot ] :
477
+ ) -> None :
436
478
"""
437
479
Add a list of any type of object to the scene.
438
480
@@ -456,15 +498,9 @@ def add_list(
456
498
**plotting_options : dict, default: None
457
499
Keyword arguments. For allowable keyword arguments, see the
458
500
:meth:`Plotter.add_mesh <pyvista.Plotter.add_mesh>` method.
459
-
460
- Returns
461
- -------
462
- Dict[~pyvista.Actor, GeomObjectPlot]
463
- Mapping between the ~pyvista.Actor and the PyAnsys Geometry objects.
464
501
"""
465
502
for object in plotting_list :
466
503
_ = self .add (object , merge_bodies , merge_components , filter , ** plotting_options )
467
- return self ._geom_object_actors_map
468
504
469
505
def show (
470
506
self ,
@@ -523,3 +559,8 @@ def __set_add_mesh_defaults(self, plotting_options: Optional[Dict]) -> None:
523
559
# This method should only be applied in 3D objects: bodies, components
524
560
plotting_options .setdefault ("smooth_shading" , True )
525
561
plotting_options .setdefault ("color" , DEFAULT_COLOR )
562
+
563
+ @property
564
+ def geom_object_actors_map (self ) -> Dict [pv .Actor , GeomObjectPlot ]:
565
+ """Mapping between the ~pyvista.Actor and the PyAnsys Geometry objects."""
566
+ return self ._geom_object_actors_map
0 commit comments