40
40
)
41
41
from ansys .api .geometry .v0 .commands_pb2_grpc import CommandsStub
42
42
from 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
44
44
from pint import Quantity
45
45
46
46
from ansys .geometry .core .connection .client import GrpcClient
@@ -446,9 +446,9 @@ def plot(
446
446
"""
447
447
return
448
448
449
- def intersect (self , other : "Body" ) -> None :
449
+ def intersect (self , other : Union [ "Body" , Iterable [ "Body" ]] ) -> None :
450
450
"""
451
- Intersect two bodies.
451
+ Intersect two (or more) bodies.
452
452
453
453
Notes
454
454
-----
@@ -469,9 +469,9 @@ def intersect(self, other: "Body") -> None:
469
469
return
470
470
471
471
@protect_grpc
472
- def subtract (self , other : "Body" ) -> None :
472
+ def subtract (self , other : Union [ "Body" , Iterable [ "Body" ]] ) -> None :
473
473
"""
474
- Subtract two bodies.
474
+ Subtract two (or more) bodies.
475
475
476
476
Notes
477
477
-----
@@ -492,9 +492,9 @@ def subtract(self, other: "Body") -> None:
492
492
return
493
493
494
494
@protect_grpc
495
- def unite (self , other : "Body" ) -> None :
495
+ def unite (self , other : Union [ "Body" , Iterable [ "Body" ]] ) -> None :
496
496
"""
497
- Unite two bodies.
497
+ Unite two (or more) bodies.
498
498
499
499
Notes
500
500
-----
@@ -803,17 +803,17 @@ def plot(
803
803
"MasterBody does not implement plot methods. Call this method on a body instead."
804
804
)
805
805
806
- def intersect (self , other : "Body" ) -> None : # noqa: D102
806
+ def intersect (self , other : Union [ "Body" , Iterable [ "Body" ]] ) -> None : # noqa: D102
807
807
raise NotImplementedError (
808
808
"MasterBody does not implement Boolean methods. Call this method on a body instead."
809
809
)
810
810
811
- def subtract (self , other : "Body" ) -> None : # noqa: D102
811
+ def subtract (self , other : Union [ "Body" , Iterable [ "Body" ]] ) -> None : # noqa: D102
812
812
raise NotImplementedError (
813
813
"MasterBody does not implement Boolean methods. Call this method on a body instead."
814
814
)
815
815
816
- def unite (self , other : "Body" ) -> None :
816
+ def unite (self , other : Union [ "Body" , Iterable [ "Body" ]] ) -> None :
817
817
# noqa: D102
818
818
raise NotImplementedError (
819
819
"MasterBody does not implement Boolean methods. Call this method on a body instead."
@@ -1108,40 +1108,56 @@ def plot(
1108
1108
self , merge_bodies = merge , screenshot = screenshot , ** plotting_options
1109
1109
)
1110
1110
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" )
1118
1113
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 " )
1121
1116
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" )
1123
1119
1124
1120
@protect_grpc
1125
1121
@reset_tessellation_cache
1126
1122
@ensure_design_is_active
1127
- def subtract (self , other : "Body" ) -> None : # noqa: D102
1128
- response = self ._template ._bodies_stub .Boolean (
1129
- BooleanRequest (body1 = self .id , body2 = other .id , method = "subtract" )
1130
- ).empty_result
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 ]
1128
+ try :
1129
+ response = self ._template ._bodies_stub .Boolean (
1130
+ BooleanRequest (
1131
+ body1 = self .id , tool_bodies = [b .id for b in grpc_other ], method = type_bool_op
1132
+ )
1133
+ ).empty_result
1134
+ except Exception as err :
1135
+ # TODO: to be deleted - old versions did not have "tool_bodies" in the request
1136
+ # This is a temporary fix to support old versions of the server - should be deleted
1137
+ # once the server is no longer supported.
1138
+ if not isinstance (other , Iterable ):
1139
+ response = self ._template ._bodies_stub .Boolean (
1140
+ BooleanRequest (body1 = self .id , body2 = other .id , method = type_bool_op )
1141
+ ).empty_result
1142
+ else :
1143
+ all_response = []
1144
+ for body2 in other :
1145
+ response = self ._template ._bodies_stub .Boolean (
1146
+ BooleanRequest (body1 = self .id , body2 = body2 .id , method = type_bool_op )
1147
+ ).empty_result
1148
+ all_response .append (response )
1149
+
1150
+ if all_response .count (1 ) > 0 :
1151
+ response = 1
1131
1152
1132
1153
if response == 1 :
1133
- raise ValueError ("Subtraction of bodies results in an empty (complete) subtraction." )
1134
-
1135
- other .parent_component .delete_body (other )
1154
+ raise ValueError (
1155
+ f"Boolean operation of type '{ type_bool_op } ' failed: { err_bool_op } .\n "
1156
+ f"Involving bodies:{ self } , { grpc_other } "
1157
+ )
1136
1158
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 )
1159
+ for b in grpc_other :
1160
+ b .parent_component .delete_body (b )
1145
1161
1146
1162
def __repr__ (self ) -> str :
1147
1163
"""Represent the ``Body`` as a string."""
0 commit comments