Skip to content

Commit daa994b

Browse files
committed
refactor _combineWithBase
1 parent 0cf408a commit daa994b

File tree

1 file changed

+33
-35
lines changed

1 file changed

+33
-35
lines changed

cadquery/cq.py

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2993,12 +2993,7 @@ def twistExtrude(
29932993

29942994
r = Compound.makeCompound(shapes).fuse()
29952995

2996-
if isinstance(combine, str) and combine == "cut":
2997-
newS = self._cutFromBase(r)
2998-
elif isinstance(combine, bool) and combine:
2999-
newS = self._combineWithBase(r)
3000-
else:
3001-
newS = self.newObject([r])
2996+
newS = self._combineWithBase(r, combine)
30022997
if clean:
30032998
newS = newS.clean()
30042999
return newS
@@ -3061,10 +3056,8 @@ def extrude(
30613056
f"Do not know how to handle until argument of type {type(until)}"
30623057
)
30633058

3064-
if combine:
3065-
newS = self._combineWithBase(r)
3066-
else:
3067-
newS = self.newObject([r])
3059+
newS = self._combineWithBase(r, combine)
3060+
30683061
if clean:
30693062
newS = newS.clean()
30703063
return newS
@@ -3131,12 +3124,7 @@ def revolve(
31313124

31323125
# returns a Solid (or a compound if there were multiple)
31333126
r = self._revolve(angleDegrees, axisStart, axisEnd)
3134-
if isinstance(combine, str) and combine == "cut":
3135-
newS = self._cutFromBase(r)
3136-
elif isinstance(combine, bool) and combine:
3137-
newS = self._combineWithBase(r)
3138-
else:
3139-
newS = self.newObject([r])
3127+
newS = self._combineWithBase(r, combine)
31403128
if clean:
31413129
newS = newS.clean()
31423130
return newS
@@ -3189,19 +3177,34 @@ def sweep(
31893177
) # returns a Solid (or a compound if there were multiple)
31903178

31913179
newS: T
3192-
if isinstance(combine, str) and combine == "cut":
3193-
newS = self._cutFromBase(r)
3194-
elif isinstance(combine, bool) and combine:
3195-
newS = self._combineWithBase(r)
3196-
else:
3197-
newS = self.newObject([r])
3180+
newS = self._combineWithBase(r, combine)
31983181
if clean:
31993182
newS = newS.clean()
32003183
return newS
32013184

3202-
def _combineWithBase(self: T, obj: Shape) -> T:
3185+
def _combineWithBase(
3186+
self: T, obj: Shape, combine_mode: Union[bool, str] = True
3187+
) -> T:
32033188
"""
32043189
Combines the provided object with the base solid, if one can be found.
3190+
:param obj: The object to be combined with the context solid
3191+
:param combine_mode: The mode to combine with the base solid (True, False or "cut")
3192+
:return: a new object that represents the result of combining the base object with obj,
3193+
or obj if one could not be found
3194+
"""
3195+
3196+
if isinstance(combine_mode, str) and combine_mode == "cut":
3197+
newS = self._cutFromBase(obj)
3198+
elif isinstance(combine_mode, bool) and combine_mode:
3199+
newS = self._fuseWithBase(obj)
3200+
elif not combine_mode:
3201+
newS = self.newObject([obj])
3202+
3203+
return newS
3204+
3205+
def _fuseWithBase(self: T, obj: Shape) -> T:
3206+
"""
3207+
Fuse the provided object with the base solid, if one can be found.
32053208
:param obj:
32063209
:return: a new object that represents the result of combining the base object with obj,
32073210
or obj if one could not be found
@@ -3214,7 +3217,6 @@ def _combineWithBase(self: T, obj: Shape) -> T:
32143217
r = baseSolid.fuse(obj)
32153218
elif isinstance(obj, Compound):
32163219
r = obj.fuse()
3217-
32183220
return self.newObject([r])
32193221

32203222
def _cutFromBase(self: T, obj: Shape) -> T:
@@ -3514,14 +3516,7 @@ def loft(self: T, ruled: bool = False, combine: Union[bool, str] = True) -> T:
35143516

35153517
r: Shape = Solid.makeLoft(wiresToLoft, ruled)
35163518

3517-
if isinstance(combine, str) and combine == "cut":
3518-
newS = self._cutFromBase(r)
3519-
3520-
elif isinstance(combine, bool) and combine:
3521-
newS = self._combineWithBase(r)
3522-
3523-
else:
3524-
newS = self.newObject([r])
3519+
newS = self._combineWithBase(r, combine)
35253520

35263521
return newS
35273522

@@ -4192,11 +4187,14 @@ def text(
41924187
)
41934188

41944189
if cut:
4195-
newS = self._cutFromBase(r)
4190+
combine_mode = "cut"
41964191
elif combine:
4197-
newS = self._combineWithBase(r)
4192+
combine_mode = True
41984193
else:
4199-
newS = self.newObject([r])
4194+
combine_mode = False
4195+
4196+
newS = self._combineWithBase(r, combine_mode)
4197+
42004198
if clean:
42014199
newS = newS.clean()
42024200
return newS

0 commit comments

Comments
 (0)