Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add glue option to combine() #535

Merged
merged 11 commits into from
Dec 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -2082,7 +2080,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 @@ -2134,7 +2132,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 @@ -2855,20 +2853,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)

adam-urbanczyk marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -2165,9 +2165,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