Skip to content

Commit

Permalink
Merge pull request CadQuery#569 from RubenRubens/boolean
Browse files Browse the repository at this point in the history
Boolean operation syntactic sugar
  • Loading branch information
jmwright authored Feb 12, 2021
2 parents b6beba7 + 79f4bb8 commit 0d96f63
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
45 changes: 45 additions & 0 deletions cadquery/cq.py
Original file line number Diff line number Diff line change
Expand Up @@ -2975,6 +2975,25 @@ def union(

return self.newObject([r])

def __or__(self, toUnion: Union["Workplane", Solid, Compound]) -> "Workplane":
"""
Syntactic sugar for union.
Notice that `r = a | b` is equivalent to `r = a.union(b)` and `r = a + b`.
Example::
Box = Workplane("XY").box(1, 1, 1, centered=(False, False, False))
Sphere = Workplane("XY").sphere(1)
result = Box | Sphere
"""
return self.union(toUnion)

def __add__(self, toUnion: Union["Workplane", Solid, Compound]) -> "Workplane":
"""
Syntactic sugar for union.
Notice that `r = a + b` is equivalent to `r = a.union(b)` and `r = a | b`.
"""
return self.union(toUnion)

def cut(
self, toCut: Union["Workplane", Solid, Compound], clean: bool = True
) -> "Workplane":
Expand Down Expand Up @@ -3010,6 +3029,19 @@ def cut(

return self.newObject([newS])

def __sub__(self, toUnion: Union["Workplane", Solid, Compound]) -> "Workplane":
"""
Syntactic sugar for cut.
Notice that `r = a - b` is equivalent to `r = a.cut(b)`.
Example::
Box = Workplane("XY").box(1, 1, 1, centered=(False, False, False))
Sphere = Workplane("XY").sphere(1)
result = Box - Sphere
"""
return self.cut(toUnion)

def intersect(
self, toIntersect: Union["Workplane", Solid, Compound], clean: bool = True
) -> "Workplane":
Expand Down Expand Up @@ -3045,6 +3077,19 @@ def intersect(

return self.newObject([newS])

def __and__(self, toUnion: Union["Workplane", Solid, Compound]) -> "Workplane":
"""
Syntactic sugar for intersect.
Notice that `r = a & b` is equivalent to `r = a.intersect(b)`.
Example::
Box = Workplane("XY").box(1, 1, 1, centered=(False, False, False))
Sphere = Workplane("XY").sphere(1)
result = Box & Sphere
"""
return self.intersect(toUnion)

def cutBlind(
self, distanceToCut: float, clean: bool = True, taper: Optional[float] = None
) -> "Workplane":
Expand Down
1 change: 1 addition & 0 deletions doc/classreference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ Class Details
.. automodule:: cadquery
:show-inheritance:
:members:
:special-members:

.. autoclass:: cadquery.occ_impl.shapes.Mixin1D
:show-inheritance:
Expand Down
15 changes: 15 additions & 0 deletions tests/test_cadquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -1368,6 +1368,10 @@ def testCut(self):
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.
Expand Down Expand Up @@ -1395,6 +1399,10 @@ def testIntersect(self):
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
Expand Down Expand Up @@ -2340,6 +2348,13 @@ def testUnions(self):
with self.assertRaises(ValueError):
resS.union(toUnion.faces().val())

# Test syntactic sugar [__add__ method]
sugar1 = currentS | toUnion
sugar2 = currentS + toUnion

self.assertEqual(resS.faces().size(), sugar1.faces().size())
self.assertEqual(resS.faces().size(), sugar2.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)
Expand Down

0 comments on commit 0d96f63

Please sign in to comment.