4040)
4141from ansys .api .geometry .v0 .commands_pb2_grpc import CommandsStub
4242from beartype import beartype as check_input_types
43- from beartype .typing import TYPE_CHECKING , List , Optional , Tuple , Union
43+ from beartype .typing import TYPE_CHECKING , Iterable , List , Optional , Tuple , Union
4444from pint import Quantity
4545
4646from ansys .geometry .core .connection .client import GrpcClient
@@ -446,9 +446,9 @@ def plot(
446446 """
447447 return
448448
449- def intersect (self , other : "Body" ) -> None :
449+ def intersect (self , other : Union [ "Body" , Iterable [ "Body" ]] ) -> None :
450450 """
451- Intersect two bodies.
451+ Intersect two (or more) bodies.
452452
453453 Notes
454454 -----
@@ -469,9 +469,9 @@ def intersect(self, other: "Body") -> None:
469469 return
470470
471471 @protect_grpc
472- def subtract (self , other : "Body" ) -> None :
472+ def subtract (self , other : Union [ "Body" , Iterable [ "Body" ]] ) -> None :
473473 """
474- Subtract two bodies.
474+ Subtract two (or more) bodies.
475475
476476 Notes
477477 -----
@@ -492,9 +492,9 @@ def subtract(self, other: "Body") -> None:
492492 return
493493
494494 @protect_grpc
495- def unite (self , other : "Body" ) -> None :
495+ def unite (self , other : Union [ "Body" , Iterable [ "Body" ]] ) -> None :
496496 """
497- Unite two bodies.
497+ Unite two (or more) bodies.
498498
499499 Notes
500500 -----
@@ -803,17 +803,17 @@ def plot(
803803 "MasterBody does not implement plot methods. Call this method on a body instead."
804804 )
805805
806- def intersect (self , other : "Body" ) -> None : # noqa: D102
806+ def intersect (self , other : Union [ "Body" , Iterable [ "Body" ]] ) -> None : # noqa: D102
807807 raise NotImplementedError (
808808 "MasterBody does not implement Boolean methods. Call this method on a body instead."
809809 )
810810
811- def subtract (self , other : "Body" ) -> None : # noqa: D102
811+ def subtract (self , other : Union [ "Body" , Iterable [ "Body" ]] ) -> None : # noqa: D102
812812 raise NotImplementedError (
813813 "MasterBody does not implement Boolean methods. Call this method on a body instead."
814814 )
815815
816- def unite (self , other : "Body" ) -> None :
816+ def unite (self , other : Union [ "Body" , Iterable [ "Body" ]] ) -> None :
817817 # noqa: D102
818818 raise NotImplementedError (
819819 "MasterBody does not implement Boolean methods. Call this method on a body instead."
@@ -1108,40 +1108,37 @@ def plot(
11081108 self , merge_bodies = merge , screenshot = screenshot , ** plotting_options
11091109 )
11101110
1111- @protect_grpc
1112- @reset_tessellation_cache
1113- @ensure_design_is_active
1114- def intersect (self , other : "Body" ) -> None : # noqa: D102
1115- response = self ._template ._bodies_stub .Boolean (
1116- BooleanRequest (body1 = self .id , body2 = other .id , method = "intersect" )
1117- ).empty_result
1111+ def intersect (self , other : Union ["Body" , Iterable ["Body" ]]) -> None : # noqa: D102
1112+ self .__generic_boolean_op (other , "intersect" , "bodies do not intersect" )
11181113
1119- if response == 1 :
1120- raise ValueError ( "Bodies do not intersect. " )
1114+ def subtract ( self , other : Union [ "Body" , Iterable [ "Body" ]]) -> None : # noqa: D102
1115+ self . __generic_boolean_op ( other , "subtract" , "empty (complete) subtraction " )
11211116
1122- other .parent_component .delete_body (other )
1117+ def unite (self , other : Union ["Body" , Iterable ["Body" ]]) -> None : # noqa: D102
1118+ self .__generic_boolean_op (other , "unite" , "union operation failed" )
11231119
11241120 @protect_grpc
11251121 @reset_tessellation_cache
11261122 @ensure_design_is_active
1127- def subtract (self , other : "Body" ) -> None : # noqa: D102
1123+ @check_input_types
1124+ def __generic_boolean_op (
1125+ self , other : Union ["Body" , Iterable ["Body" ]], type_bool_op : str , err_bool_op : str
1126+ ) -> None :
1127+ grpc_other = other if isinstance (other , Iterable ) else [other ]
11281128 response = self ._template ._bodies_stub .Boolean (
1129- BooleanRequest (body1 = self .id , body2 = other .id , method = "subtract" )
1129+ BooleanRequest (
1130+ body1 = self .id , tool_bodies = [b .id for b in grpc_other ], method = type_bool_op
1131+ )
11301132 ).empty_result
11311133
11321134 if response == 1 :
1133- raise ValueError ("Subtraction of bodies results in an empty (complete) subtraction." )
1134-
1135- other .parent_component .delete_body (other )
1135+ raise ValueError (
1136+ f"Boolean operation of type '{ type_bool_op } ' failed: { err_bool_op } .\n "
1137+ f"Involving bodies:{ self } , { grpc_other } "
1138+ )
11361139
1137- @protect_grpc
1138- @reset_tessellation_cache
1139- @ensure_design_is_active
1140- def unite (self , other : "Body" ) -> None : # noqa: D102
1141- self ._template ._bodies_stub .Boolean (
1142- BooleanRequest (body1 = self .id , body2 = other .id , method = "unite" )
1143- )
1144- other .parent_component .delete_body (other )
1140+ for b in grpc_other :
1141+ b .parent_component .delete_body (b )
11451142
11461143 def __repr__ (self ) -> str :
11471144 """Represent the ``Body`` as a string."""
0 commit comments