Skip to content

Commit

Permalink
Add glue option to combine() (#535)
Browse files Browse the repository at this point in the history
* Update cq.py

Add glue option to combine()

* Update test_cadquery.py

Add test of glue=True to combine()

* Update test_cadquery.py

update testcombine()

* Update cq.py

Reformat combine()

* Reformat Black

* Update test_cadquery.py

Added tol=None to testCombine()

* Update test_cadquery.py

Correct testCombine()

* Update test_cadquery.py

* Update test_cadquery.py

* Update test_cadquery.py

Black formatting

* Update test_cadquery.py
  • Loading branch information
bragostin authored Dec 11, 2020
1 parent a03648c commit ac585b4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
17 changes: 10 additions & 7 deletions cadquery/cq.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,7 @@ def __init__(
) -> None:
...

def __init__(
self, inPlane="XY", origin=(0, 0, 0), obj=None,
):
def __init__(self, inPlane="XY", origin=(0, 0, 0), obj=None):
"""
make a workplane from a particular plane
Expand Down Expand Up @@ -2126,7 +2124,7 @@ def wire(self, forConstruction: bool = False) -> "Workplane":
return self.newObject(others + [w])

def each(
self, callback: Callable[[CQObject], Shape], useLocalCoordinates: bool = False,
self, callback: Callable[[CQObject], Shape], useLocalCoordinates: bool = False
) -> "Workplane":
"""
Runs the provided function on each value in the stack, and collects the return values into
Expand Down Expand Up @@ -2178,7 +2176,7 @@ def each(
return self.newObject(results)

def eachpoint(
self, callback: Callable[[Location], Shape], useLocalCoordinates: bool = False,
self, callback: Callable[[Location], Shape], useLocalCoordinates: bool = False
) -> "Workplane":
"""
Same as each(), except each item on the stack is converted into a point before it
Expand Down Expand Up @@ -2899,20 +2897,25 @@ def _cutFromBase(self, obj: Shape) -> "Workplane":

return self.newObject([r])

def combine(self, clean: bool = True) -> "Workplane":
def combine(
self, clean: bool = True, glue: bool = False, tol: Optional[float] = None
) -> "Workplane":
"""
Attempts to combine all of the items on the stack into a single item.
WARNING: all of the items must be of the same type!
:param boolean clean: call :py:meth:`clean` afterwards to have a clean shape
:param boolean glue: use a faster gluing mode for non-overlapping shapes (default False)
:param float tol: tolerance value for fuzzy bool operation mode (default None)
:raises: ValueError if there are no items on the stack, or if they cannot be combined
:return: a CQ object with the resulting object selected
"""

items: List[Shape] = [o for o in self.objects if isinstance(o, Shape)]
s = items.pop(0)

if items:
s = s.fuse(*items)
s = s.fuse(*items, glue=glue, tol=tol)

if clean:
s = s.clean()
Expand Down
6 changes: 5 additions & 1 deletion tests/test_cadquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -2180,9 +2180,13 @@ def testCombine(self):
objects1 = s.rect(2.0, 2.0).extrude(0.5).faces(">Z").rect(1.0, 1.0).extrude(0.5)

objects1.combine()

self.assertEqual(11, objects1.faces().size())

objects1 = s.rect(2.0, 2.0).extrude(0.5)
objects2 = s.rect(1.0, 1.0).extrude(0.5).translate((0, 0, 0.5))
objects2 = objects1.add(objects2).combine(glue=True, tol=None)
self.assertEqual(11, objects2.faces().size())

def testCombineSolidsInLoop(self):
# duplicates a memory problem of some kind reported when combining lots of objects
s = Workplane("XY").rect(0.5, 0.5).extrude(5.0)
Expand Down

0 comments on commit ac585b4

Please sign in to comment.