Skip to content

Commit 502dc3a

Browse files
committed
updated text and added deprecation warning
1 parent 3631e07 commit 502dc3a

File tree

3 files changed

+61
-19
lines changed

3 files changed

+61
-19
lines changed

cadquery/cq.py

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151

5252
from .occ_impl.exporters.svg import getSVG, exportSVG
5353

54-
from .utils import deprecate_kwarg, deprecate
54+
from .utils import deprecate_kwarg, deprecate, deprecate_kwarg_name
5555

5656
from .selectors import (
5757
Selector,
@@ -2394,6 +2394,8 @@ def each(
23942394
self: T,
23952395
callback: Callable[[CQObject], Shape],
23962396
useLocalCoordinates: bool = False,
2397+
combine: Union[bool, str] = True,
2398+
clean: bool = True,
23972399
) -> T:
23982400
"""
23992401
Runs the provided function on each value in the stack, and collects the return values into
@@ -2404,6 +2406,9 @@ def each(
24042406
:param callBackFunction: the function to call for each item on the current stack.
24052407
:param useLocalCoordinates: should values be converted from local coordinates first?
24062408
:type useLocalCoordinates: boolean
2409+
: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.
2410+
:param boolean clean: call :py:meth:`clean` afterwards to have a clean shape
2411+
24072412
24082413
The callback function must accept one argument, which is the item on the stack, and return
24092414
one object, which is collected. If the function returns None, nothing is added to the stack.
@@ -2439,7 +2444,6 @@ def each(
24392444
if isinstance(r, Wire):
24402445
if not r.forConstruction:
24412446
self._addPendingWire(r)
2442-
24432447
results.append(r)
24442448

24452449
return self.newObject(results)
@@ -2448,6 +2452,7 @@ def eachpoint(
24482452
self: T,
24492453
callback: Callable[[Location], Shape],
24502454
useLocalCoordinates: bool = False,
2455+
combine: Union[bool, str] = False,
24512456
) -> T:
24522457
"""
24532458
Same as each(), except each item on the stack is converted into a point before it
@@ -2457,6 +2462,8 @@ def eachpoint(
24572462
24582463
:param useLocalCoordinates: should points be in local or global coordinates
24592464
:type useLocalCoordinates: boolean
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+
24602467
24612468
The resulting object has a point on the stack for each object on the original stack.
24622469
Vertices and points remain a point. Faces, Wires, Solids, Edges, and Shells are converted
@@ -2492,6 +2499,10 @@ def eachpoint(
24922499
if isinstance(r, Wire) and not r.forConstruction:
24932500
self._addPendingWire(r)
24942501

2502+
if combine:
2503+
compound = Compound.makeCompound(res)
2504+
res = [self._combineWithBase(compound, combine).val()]
2505+
24952506
return self.newObject(res)
24962507

24972508
def rect(
@@ -3001,7 +3012,7 @@ def twistExtrude(
30013012
def extrude(
30023013
self: T,
30033014
until: Union[float, Literal["next", "last"], Face],
3004-
combine: bool = True,
3015+
combine: Union[bool,str] = True,
30053016
clean: bool = True,
30063017
both: bool = False,
30073018
taper: Optional[float] = None,
@@ -3015,7 +3026,7 @@ def extrude(
30153026
to the normal of the plane. The string "next" extrudes until the next face orthogonal to
30163027
the wire normal. "last" extrudes to the last face. If a object of type Face is passed then
30173028
the extrusion will extend until this face.
3018-
:param boolean combine: True to combine the resulting solid with parent solids if found. (Cannot be set to False when `until` is not set as a float)
3029+
: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.
30193030
:param boolean clean: call :py:meth:`clean` afterwards to have a clean shape
30203031
:param boolean both: extrude in both directions symmetrically
30213032
:param float taper: angle for optional tapered extrusion
@@ -3038,7 +3049,7 @@ def extrude(
30383049
elif until == "last":
30393050
faceIndex = -1
30403051

3041-
r = self._extrude(distance=None, both=both, taper=taper, upToFace=faceIndex)
3052+
r = self._extrude(None, both=both, taper=taper, upToFace=faceIndex)
30423053

30433054
elif isinstance(until, Face) and combine:
30443055
r = self._extrude(None, both=both, taper=taper, upToFace=until)
@@ -3192,12 +3203,11 @@ def _combineWithBase(
31923203
:return: a new object that represents the result of combining the base object with obj,
31933204
or obj if one could not be found
31943205
"""
3195-
31963206
if isinstance(combine_mode, str) and combine_mode == "cut":
31973207
newS = self._cutFromBase(obj)
31983208
elif isinstance(combine_mode, bool) and combine_mode:
31993209
newS = self._fuseWithBase(obj)
3200-
elif not combine_mode:
3210+
else:
32013211
newS = self.newObject([obj])
32023212

32033213
return newS
@@ -3226,9 +3236,8 @@ def _cutFromBase(self: T, obj: Shape) -> T:
32263236
:return: a new object that represents the result of combining the base object with obj,
32273237
or obj if one could not be found
32283238
"""
3229-
baseSolid = self._findType(
3230-
(Solid, Compound), searchStack=True, searchParents=True
3231-
)
3239+
baseSolid = self._findType((Solid, Compound), True, True)
3240+
32323241
r = obj
32333242
if baseSolid is not None:
32343243
r = baseSolid.cut(obj)
@@ -4119,14 +4128,15 @@ def clean(self: T) -> T:
41194128

41204129
return self.newObject(cleanObjects)
41214130

4131+
@deprecate_kwarg_name("cut", "combine='cut'")
41224132
def text(
41234133
self: T,
41244134
txt: str,
41254135
fontsize: float,
41264136
distance: float,
41274137
cut: bool = True,
41284138
combine: bool = False,
4129-
clean: bool = True,
4139+
clean: Union[bool, str] = True,
41304140
font: str = "Arial",
41314141
fontPath: Optional[str] = None,
41324142
kind: Literal["regular", "bold", "italic"] = "regular",
@@ -4141,7 +4151,7 @@ def text(
41414151
:param distance: the distance to extrude or cut, normal to the workplane plane
41424152
:type distance: float, negative means opposite the normal direction
41434153
:param cut: True to cut the resulting solid from the parent solids if found
4144-
:param combine: True to combine the resulting solid with parent solids if found
4154+
: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.
41454155
:param clean: call :py:meth:`clean` afterwards to have a clean shape
41464156
:param font: font name
41474157
:param fontPath: path to font file
@@ -4186,15 +4196,10 @@ def text(
41864196
position=self.plane,
41874197
)
41884198

4189-
combine_mode:Union[bool, str]
41904199
if cut:
4191-
combine_mode = "cut"
4192-
elif combine:
4193-
combine_mode = True
4194-
else:
4195-
combine_mode = False
4200+
combine = "cut"
41964201

4197-
newS = self._combineWithBase(r, combine_mode)
4202+
newS = self._combineWithBase(r, combine)
41984203

41994204
if clean:
42004205
newS = newS.clean()

cadquery/utils.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,25 @@ def wrapped(*args, **kwargs):
3636
return f(*args, **kwargs)
3737

3838
return wrapped
39+
40+
class deprecate_kwarg_name:
41+
def __init__(self, name, new_name):
42+
43+
self.name = name
44+
self.new_name = new_name
45+
46+
def __call__(self, f):
47+
@wraps(f)
48+
def wrapped(*args, **kwargs):
49+
50+
f_sig_params = signature(f).parameters
51+
52+
if f_sig_params[self.name]:
53+
warn(
54+
f"Kwarg <{self.name}> will be removed. Plase use <{self.new_name}>",
55+
FutureWarning,
56+
)
57+
58+
return f(*args, **kwargs)
59+
60+
return wrapped

tests/test_cadquery.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3524,6 +3524,11 @@ def testExtrude(self):
35243524
with self.assertRaises(ValueError):
35253525
Workplane().rect(2, 2).rect(1, 1).extrude(2, taper=4)
35263526

3527+
# Test extrude with combine="cut"
3528+
box = Workplane().box(5,5,5)
3529+
r = box.faces(">Z").workplane(invert=True).circle(0.5).extrude(4, combine="cut")
3530+
self.assertGreater(box.val().Volume(), r.val().Volume())
3531+
35273532
def testTaperedExtrudeCutBlind(self):
35283533

35293534
h = 1.0
@@ -5093,6 +5098,16 @@ def testEachpoint(self):
50935098
for v in r1.vals():
50945099
self.assertTupleAlmostEquals(v.Center().toTuple(), (0, 0, 0), 6)
50955100

5101+
# test eachpoint with combine = True
5102+
box = Workplane().box(2, 1, 1).val()
5103+
ref = Workplane().box(5, 5, 5)
5104+
r = ref.vertices().eachpoint(lambda loc: box.moved(loc), combine=True)
5105+
self.assertGreater(r.val().Volume(), ref.val().Volume())
5106+
5107+
# test eachpoint with combine = "cut"
5108+
r = ref.vertices().eachpoint(lambda loc: box.moved(loc), combine="cut")
5109+
self.assertGreater(ref.val().Volume(), r.val().Volume())
5110+
50965111
def testSketch(self):
50975112

50985113
r1 = (

0 commit comments

Comments
 (0)