Add syntactic sugar for boolean operations

This commit is contained in:
Ruben
2021-01-07 15:42:24 +01:00
parent f4b5e8210b
commit 33286c2a60
2 changed files with 42 additions and 0 deletions

View File

@ -2968,6 +2968,16 @@ class Workplane(object):
r = r.clean()
return self.newObject([r])
def __add__(self, toUnion: Union["Workplane", Solid, Compound]) -> "Workplane":
"""
Syntactic sugar for union.
Example::
r = box(1, 1, 1) + sphere(1)
"""
return self.union(toUnion)
def cut(
self, toCut: Union["Workplane", Solid, Compound], clean: bool = True
@ -3004,6 +3014,16 @@ class Workplane(object):
return self.newObject([newS])
def __sub__(self, toUnion: Union["Workplane", Solid, Compound]) -> "Workplane":
"""
Syntactic sugar for cut.
Example::
r = box(1, 1, 1) - sphere(1)
"""
return self.cut(toUnion)
def intersect(
self, toIntersect: Union["Workplane", Solid, Compound], clean: bool = True
) -> "Workplane":
@ -3038,6 +3058,16 @@ class Workplane(object):
newS = newS.clean()
return self.newObject([newS])
def __mul__(self, toUnion: Union["Workplane", Solid, Compound]) -> "Workplane":
"""
Syntactic sugar for intersect.
Example::
r = box(1, 1, 1) * sphere(1)
"""
return self.intersect(toUnion)
def cutBlind(
self, distanceToCut: float, clean: bool = True, taper: Optional[float] = None

View File

@ -1306,6 +1306,10 @@ class TestCadQuery(BaseTest):
with self.assertRaises(ValueError):
currentS.cut(toCut.faces().val())
# Test syntactic sugar [__sub__ method]
sugar = currentS - toCut.val()
self.assertEqual(resS.faces().size(), sugar.faces().size())
def testIntersect(self):
"""
Tests the intersect function.
@ -1333,6 +1337,10 @@ class TestCadQuery(BaseTest):
with self.assertRaises(ValueError):
b1.intersect(b2.faces().val())
# Test syntactic sugar [__mul__ method]
sugar = b1 * b2
self.assertEqual(resS.val().Volume(), sugar.val().Volume())
def testBoundingBox(self):
"""
Tests the boudingbox center of a model
@ -2175,6 +2183,10 @@ class TestCadQuery(BaseTest):
with self.assertRaises(ValueError):
resS.union(toUnion.faces().val())
# Test syntactic sugar [__add__ method]
sugar = currentS + toUnion
self.assertEqual(resS.faces().size(), sugar.faces().size())
def testCombine(self):
s = Workplane(Plane.XY())
objects1 = s.rect(2.0, 2.0).extrude(0.5).faces(">Z").rect(1.0, 1.0).extrude(0.5)