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

rework Workplane.split #752

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
60 changes: 31 additions & 29 deletions cadquery/cq.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,12 @@ def _collectProperty(self, propName: str) -> List[CQObject]:

return list(all.values())

def _split(self: T, s: Union[Shape, List[Shape]]) -> Shape:

solid = self.findSolid()
tools = (s,) if isinstance(s, Shape) else s
return solid.split(*tools)

@overload
def split(self: T, keepTop: bool = False, keepBottom: bool = False) -> T:
...
Expand All @@ -272,6 +278,10 @@ def split(self: T, keepTop: bool = False, keepBottom: bool = False) -> T:
def split(self: T, splitter: Union[T, Shape]) -> T:
...

@overload
def split(self: T, shapes: List[Shape]) -> T:
...

def split(self: T, *args, **kwargs) -> T:
"""
Splits a solid on the stack into two parts, optionally keeping the separate parts.
Expand All @@ -293,19 +303,21 @@ def split(self: T, *args, **kwargs) -> T:
"""

# split using an object
if len(args) == 1 and isinstance(args[0], (Workplane, Shape)):
if len(args) == 1 and isinstance(args[0], Workplane):

arg = args[0]
tools = [x for x in args[0].vals() if isinstance(x, Shape)]
rv = [self._split(tools)]

solid = self.findSolid()
tools = (
(arg,)
if isinstance(arg, Shape)
else [v for v in arg.vals() if isinstance(v, Shape)]
)
rv = [solid.split(*tools)]
elif args and all(isinstance(x, Shape) for x in args):

rv = [self._split(tools)]

# split using the current wokrplane
elif len(args) == 1 and isinstance(args[0], Plane):

splitter = Face.makePlane(basePnt=args[0].origin, dir=args[0].zDir)
rv = [self._split(splitter)]

# split using the current workplane
else:

# boilerplate for arg/kwarg parsing
Expand All @@ -329,25 +341,15 @@ def split(self: T, *args, **kwargs) -> T:
if (not keepTop) and (not keepBottom):
raise ValueError("You have to keep at least one half")

solid = self.findSolid()

maxDim = solid.BoundingBox().DiagonalLength * 10.0
topCutBox = self.rect(maxDim, maxDim)._extrude(maxDim)
bottomCutBox = self.rect(maxDim, maxDim)._extrude(-maxDim)

top = solid.cut(bottomCutBox)
bottom = solid.cut(topCutBox)

if keepTop and keepBottom:
# Put both on the stack, leave original unchanged.
rv = [top, bottom]
else:
# Put the one we are keeping on the stack, and also update the
# context solid to the one we kept.
if keepTop:
rv = [top]
else:
rv = [bottom]
splitter = Face.makePlane(basePnt=self.plane.origin, dir=self.plane.zDir)
split_shapes = self._split(splitter)
rv = []
for s in split_shapes.Solids():
dist = self.plane.toLocalCoords(s.Center()).z
if keepTop and dist > 0:
rv.append(s)
if keepBottom and dist < 0:
rv.append(s)

return self.newObject(rv)

Expand Down