62
62
63
63
CQObject = Union [Vector , Location , Shape , Sketch ]
64
64
VectorLike = Union [Tuple [float , float ], Tuple [float , float , float ], Vector ]
65
+ CombineMode = Union [bool , Literal ["cut" , "a" , "s" ]] # a : additive, s: subtractive
65
66
66
67
T = TypeVar ("T" , bound = "Workplane" )
67
68
"""A type variable used to make the return type of a method the same as the
@@ -2392,7 +2393,7 @@ def each(
2392
2393
self : T ,
2393
2394
callback : Callable [[CQObject ], Shape ],
2394
2395
useLocalCoordinates : bool = False ,
2395
- combine : Union [ bool , str ] = True ,
2396
+ combine : CombineMode = True ,
2396
2397
clean : bool = True ,
2397
2398
) -> T :
2398
2399
"""
@@ -2444,13 +2445,14 @@ def each(
2444
2445
self ._addPendingWire (r )
2445
2446
results .append (r )
2446
2447
2447
- return self .newObject (results )
2448
+ return self ._combineWithBase (results , combine , clean )
2448
2449
2449
2450
def eachpoint (
2450
2451
self : T ,
2451
2452
callback : Callable [[Location ], Shape ],
2452
2453
useLocalCoordinates : bool = False ,
2453
- combine : Union [bool , str ] = False ,
2454
+ combine : CombineMode = False ,
2455
+ clean : bool = True ,
2454
2456
) -> T :
2455
2457
"""
2456
2458
Same as each(), except each item on the stack is converted into a point before it
@@ -2461,6 +2463,7 @@ def eachpoint(
2461
2463
:param useLocalCoordinates: should points be in local or global coordinates
2462
2464
:type useLocalCoordinates: boolean
2463
2465
:param boolean or string combine: True to combine the resulting solid with parent solids if found, "cut" to remove the resulting solid from the parent solids if found.
2466
+ :param boolean clean: call :py:meth:`clean` afterwards to have a clean shape
2464
2467
2465
2468
2466
2469
The resulting object has a point on the stack for each object on the original stack.
@@ -2497,12 +2500,7 @@ def eachpoint(
2497
2500
if isinstance (r , Wire ) and not r .forConstruction :
2498
2501
self ._addPendingWire (r )
2499
2502
2500
- if combine :
2501
- compound = Compound .makeCompound (res )
2502
- return self ._combineWithBase (compound , combine )
2503
-
2504
- else :
2505
- return self .newObject (res )
2503
+ return self ._combineWithBase (res , combine , clean )
2506
2504
2507
2505
def rect (
2508
2506
self : T ,
@@ -2961,7 +2959,7 @@ def twistExtrude(
2961
2959
self : T ,
2962
2960
distance : float ,
2963
2961
angleDegrees : float ,
2964
- combine : Union [ bool , str ] = True ,
2962
+ combine : CombineMode = True ,
2965
2963
clean : bool = True ,
2966
2964
) -> T :
2967
2965
"""
@@ -3003,15 +3001,12 @@ def twistExtrude(
3003
3001
3004
3002
r = Compound .makeCompound (shapes ).fuse ()
3005
3003
3006
- newS = self ._combineWithBase (r , combine )
3007
- if clean :
3008
- newS = newS .clean ()
3009
- return newS
3004
+ return self ._combineWithBase (r , combine , clean )
3010
3005
3011
3006
def extrude (
3012
3007
self : T ,
3013
3008
until : Union [float , Literal ["next" , "last" ], Face ],
3014
- combine : Union [ bool , str ] = True ,
3009
+ combine : CombineMode = True ,
3015
3010
clean : bool = True ,
3016
3011
both : bool = False ,
3017
3012
taper : Optional [float ] = None ,
@@ -3041,46 +3036,40 @@ def extrude(
3041
3036
* if combine is true, the value is combined with the context solid if it exists,
3042
3037
and the resulting solid becomes the new context solid.
3043
3038
"""
3044
- if combine == "cut" :
3045
- return self .cutBlind (until , clean , taper )
3046
- else :
3047
- # Handle `until` multiple values
3048
- if isinstance (until , str ) and until in ("next" , "last" ) and combine :
3049
- if until == "next" :
3050
- faceIndex = 0
3051
- elif until == "last" :
3052
- faceIndex = - 1
3053
3039
3054
- r = self ._extrude (None , both = both , taper = taper , upToFace = faceIndex )
3040
+ # Handle `until` multiple values
3041
+ if isinstance (until , str ) and until in ("next" , "last" ) and combine :
3042
+ if until == "next" :
3043
+ faceIndex = 0
3044
+ elif until == "last" :
3045
+ faceIndex = - 1
3055
3046
3056
- elif isinstance (until , Face ) and combine :
3057
- r = self ._extrude (None , both = both , taper = taper , upToFace = until )
3047
+ r = self ._extrude (None , both = both , taper = taper , upToFace = faceIndex )
3058
3048
3059
- elif isinstance (until , ( int , float )) :
3060
- r = self ._extrude (until , both = both , taper = taper , upToFace = None )
3049
+ elif isinstance (until , Face ) and combine :
3050
+ r = self ._extrude (None , both = both , taper = taper , upToFace = until )
3061
3051
3062
- elif isinstance (until , (str , Face )) and combine is False :
3063
- raise ValueError (
3064
- "`combine` can't be set to False when extruding until a face"
3065
- )
3052
+ elif isinstance (until , (int , float )):
3053
+ r = self ._extrude (until , both = both , taper = taper , upToFace = None )
3066
3054
3067
- else :
3068
- raise ValueError (
3069
- f"Do not know how to handle until argument of type { type ( until ) } "
3070
- )
3055
+ elif isinstance ( until , ( str , Face )) and combine is False :
3056
+ raise ValueError (
3057
+ "`combine` can't be set to False when extruding until a face "
3058
+ )
3071
3059
3072
- newS = self ._combineWithBase (r , combine )
3060
+ else :
3061
+ raise ValueError (
3062
+ f"Do not know how to handle until argument of type { type (until )} "
3063
+ )
3073
3064
3074
- if clean :
3075
- newS = newS .clean ()
3076
- return newS
3065
+ return self ._combineWithBase (r , combine , clean )
3077
3066
3078
3067
def revolve (
3079
3068
self : T ,
3080
3069
angleDegrees : float = 360.0 ,
3081
3070
axisStart : Optional [VectorLike ] = None ,
3082
3071
axisEnd : Optional [VectorLike ] = None ,
3083
- combine : Union [ bool , str ] = True ,
3072
+ combine : CombineMode = True ,
3084
3073
clean : bool = True ,
3085
3074
) -> T :
3086
3075
"""
@@ -3137,10 +3126,8 @@ def revolve(
3137
3126
3138
3127
# returns a Solid (or a compound if there were multiple)
3139
3128
r = self ._revolve (angleDegrees , axisStart , axisEnd )
3140
- newS = self ._combineWithBase (r , combine )
3141
- if clean :
3142
- newS = newS .clean ()
3143
- return newS
3129
+
3130
+ return self ._combineWithBase (r , combine , clean )
3144
3131
3145
3132
def sweep (
3146
3133
self : T ,
@@ -3149,7 +3136,7 @@ def sweep(
3149
3136
sweepAlongWires : Optional [bool ] = None ,
3150
3137
makeSolid : bool = True ,
3151
3138
isFrenet : bool = False ,
3152
- combine : Union [ bool , str ] = True ,
3139
+ combine : CombineMode = True ,
3153
3140
clean : bool = True ,
3154
3141
transition : Literal ["right" , "round" , "transformed" ] = "right" ,
3155
3142
normal : Optional [VectorLike ] = None ,
@@ -3189,28 +3176,39 @@ def sweep(
3189
3176
auxSpine ,
3190
3177
) # returns a Solid (or a compound if there were multiple)
3191
3178
3192
- newS : T
3193
- newS = self ._combineWithBase (r , combine )
3194
- if clean :
3195
- newS = newS .clean ()
3196
- return newS
3179
+ return self ._combineWithBase (r , combine , clean )
3197
3180
3198
3181
def _combineWithBase (
3199
- self : T , obj : Shape , combine_mode : Union [bool , str ] = True
3182
+ self : T ,
3183
+ obj : Union [Shape , Iterable [Shape ]],
3184
+ mode : CombineMode = True ,
3185
+ clean : bool = False ,
3200
3186
) -> T :
3201
3187
"""
3202
3188
Combines the provided object with the base solid, if one can be found.
3203
3189
:param obj: The object to be combined with the context solid
3204
- :param combine_mode : The mode to combine with the base solid (True, False or "cut")
3190
+ :param mode : The mode to combine with the base solid (True, False or "cut")
3205
3191
:return: a new object that represents the result of combining the base object with obj,
3206
3192
or obj if one could not be found
3207
3193
"""
3208
- if isinstance (combine_mode , str ) and combine_mode == "cut" :
3209
- newS = self ._cutFromBase (obj )
3210
- elif isinstance (combine_mode , bool ) and combine_mode :
3211
- newS = self ._fuseWithBase (obj )
3194
+
3195
+ if mode :
3196
+ # since we are going to do something convert the iterable if needed
3197
+ if not isinstance (obj , Shape ):
3198
+ obj = Compound .makeCompound (obj )
3199
+
3200
+ # dispatch on the mode
3201
+ if mode in ("cut" , "s" ):
3202
+ newS = self ._cutFromBase (obj )
3203
+ elif mode in (True , "a" ):
3204
+ newS = self ._fuseWithBase (obj )
3205
+
3212
3206
else :
3213
- newS = self .newObject ([obj ])
3207
+ # do not combine branch
3208
+ newS = self .newObject (obj if not isinstance (obj , Shape ) else [obj ])
3209
+
3210
+ if clean :
3211
+ newS = newS .clean ()
3214
3212
3215
3213
return newS
3216
3214
@@ -3511,7 +3509,9 @@ def cutThruAll(self: T, clean: bool = True, taper: float = 0) -> T:
3511
3509
3512
3510
return self .newObject ([s ])
3513
3511
3514
- def loft (self : T , ruled : bool = False , combine : Union [bool , str ] = True ) -> T :
3512
+ def loft (
3513
+ self : T , ruled : bool = False , combine : CombineMode = True , clean : bool = True
3514
+ ) -> T :
3515
3515
"""
3516
3516
Make a lofted solid, through the set of wires.
3517
3517
:return: a Workplane object containing the created loft
@@ -3527,7 +3527,7 @@ def loft(self: T, ruled: bool = False, combine: Union[bool, str] = True) -> T:
3527
3527
3528
3528
r : Shape = Solid .makeLoft (wiresToLoft , ruled )
3529
3529
3530
- newS = self ._combineWithBase (r , combine )
3530
+ newS = self ._combineWithBase (r , combine , clean )
3531
3531
3532
3532
return newS
3533
3533
@@ -4137,8 +4137,8 @@ def text(
4137
4137
fontsize : float ,
4138
4138
distance : float ,
4139
4139
cut : bool = True ,
4140
- combine : Union [ bool , str ] = False ,
4141
- clean : Union [ bool , str ] = True ,
4140
+ combine : CombineMode = False ,
4141
+ clean : bool = True ,
4142
4142
font : str = "Arial" ,
4143
4143
fontPath : Optional [str ] = None ,
4144
4144
kind : Literal ["regular" , "bold" , "italic" ] = "regular" ,
@@ -4201,11 +4201,7 @@ def text(
4201
4201
if cut :
4202
4202
combine = "cut"
4203
4203
4204
- newS = self ._combineWithBase (r , combine )
4205
-
4206
- if clean :
4207
- newS = newS .clean ()
4208
- return newS
4204
+ return self ._combineWithBase (r , combine , clean )
4209
4205
4210
4206
def section (self : T , height : float = 0.0 ) -> T :
4211
4207
"""
0 commit comments