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

Added AreaNthSelector #688

Merged
merged 8 commits into from
Mar 17, 2021
Merged
Show file tree
Hide file tree
Changes from 6 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
93 changes: 90 additions & 3 deletions cadquery/selectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,19 @@
License along with this library; If not, see <http://www.gnu.org/licenses/>
"""

from abc import abstractmethod, ABC
import math
from .occ_impl.geom import Vector
from .occ_impl.shapes import Shape, Edge, Face, Wire, geom_LUT_EDGE, geom_LUT_FACE
from .occ_impl.shapes import (
Shape,
Edge,
Face,
Wire,
Shell,
Solid,
geom_LUT_EDGE,
geom_LUT_FACE,
)
from pyparsing import (
Literal,
Word,
Expand All @@ -37,7 +47,7 @@
Keyword,
)
from functools import reduce
from typing import List, Union, Sequence
from typing import List, Union, Sequence, cast


class Selector(object):
Expand Down Expand Up @@ -294,7 +304,7 @@ def filter(self, objectList: Sequence[Shape]) -> List[Shape]:
return r


class _NthSelector(Selector):
class _NthSelector(Selector, ABC):
"""
An abstract class that provides the methods to select the Nth object/objects of an ordered list.
"""
Expand Down Expand Up @@ -324,6 +334,7 @@ def filter(self, objectlist: Sequence[Shape]) -> List[Shape]:

return out

@abstractmethod
def key(self, obj: Shape) -> float:
"""
Return the key for ordering. Can raise a ValueError if obj can not be
Expand Down Expand Up @@ -454,6 +465,82 @@ def filter(self, objectlist: Sequence[Shape]) -> List[Shape]:
return objectlist


class LengthNthSelector(_NthSelector):
"""
Select the object(s) with the Nth length

Applicability:
All Edge and Wire objects
"""

def key(self, obj: Shape) -> float:
if isinstance(obj, (Edge, Wire)):
return obj.Length()
else:
raise ValueError(
f"LengthNthSelector supports only Edges and Wires, not {type(obj).__name__}"
)


class AreaNthSelector(_NthSelector):
"""
Selects the object(s) with Nth area

Applicability:
Faces, Shells, Solids - Shape.Area() is used to compute area
planar Wires - a temporary face is created to compute area

Among other things can be used to select one of
the nested coplanar wires or faces.

For example to create a fillet on a shank:

result = (
cq.Workplane("XY")
.circle(5)
.extrude(2)
.circle(2)
.extrude(10)
.faces(">Z[-2]")
.wires(AreaNthSelector(0))
.fillet(2)
)

Or to create a lip on a case seam:

result = (
cq.Workplane("XY")
.rect(20, 20)
.extrude(10)
.edges("|Z or <Z")
.fillet(2)
.faces(">Z")
.shell(2)
.faces(">Z")
.wires(AreaNthSelector(-1))
.toPending()
.workplane()
.offset2D(-1)
.extrude(1)
.faces(">Z[-2]")
.wires(AreaNthSelector(0))
.toPending()
.workplane()
.cutBlind(2)
)
"""

def key(self, obj: Shape) -> float:
if isinstance(obj, (Face, Shell, Solid)):
return obj.Area()
elif isinstance(obj, Wire):
return Face.makeFromWires(obj).Area()
else:
raise ValueError(
f"AreaNthSelector supports only Wires, Faces, Shells and Solids, not {type(obj).__name__}"
)


class BinarySelector(Selector):
"""
Base class for selectors that operates with two other
Expand Down
2 changes: 2 additions & 0 deletions doc/apireference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ as a basis for futher operations.
ParallelDirSelector
DirectionSelector
DirectionNthSelector
LengthNthSelector
AreaNthSelector
RadiusNthSelector
PerpendicularDirSelector
TypeSelector
Expand Down
2 changes: 2 additions & 0 deletions doc/classreference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ Selector Classes
CenterNthSelector
DirectionMinMaxSelector
DirectionNthSelector
LengthNthSelector
AreaNthSelector
BinarySelector
AndSelector
SumSelector
Expand Down
47 changes: 47 additions & 0 deletions examples/Ex026_Case_Seam_Lip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import cadquery as cq
from cadquery.selectors import AreaNthSelector

case_bottom = (
cq.Workplane("XY")
.rect(20, 20)
.extrude(10) # solid 20x20x10 box
Comment on lines +6 to +7
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually I would insist these lines be changed to .box(20, 20, 10, centered=(True, True, False)) but this hangs the kernel in the final cutBlind step, and I can't work around it either! So it will have to stay as this rect version until that bug is fixed. No change required here, this is just a comment.

.edges("|Z or <Z")
.fillet(2) # rounding all edges except 4 edges of the top face
.faces(">Z")
.shell(2) # shell of thickness 2 with top face open
.faces(">Z")
.wires(AreaNthSelector(-1)) # selecting top outer wire
.toPending()
.workplane()
.offset2D(-1) # creating centerline wire of case seam face
.extrude(1) # covering the sell with temporary "lid"
.faces(">Z[-2]")
.wires(AreaNthSelector(0)) # selecting case crossection wire
.toPending()
.workplane()
.cutBlind(2) # cutting through the "lid" leaving a lip on case seam surface
)

# similar process repeated for the top part
# but instead of "growing" an inner lip
# material is removed inside case seam centerline
# to create an outer lip
case_top = (
cq.Workplane("XY")
.move(25)
.rect(20, 20)
.extrude(5)
.edges("|Z or >Z")
.fillet(2)
.faces("<Z")
.shell(2)
.faces("<Z")
.wires(AreaNthSelector(-1))
.toPending()
.workplane()
.offset2D(-1)
.cutBlind(-1)
)

show_object(case_bottom)
show_object(case_top, options={"alpha": 0.5})
4 changes: 2 additions & 2 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ def toTuple(v):


class BaseTest(unittest.TestCase):
def assertTupleAlmostEquals(self, expected, actual, places):
def assertTupleAlmostEquals(self, expected, actual, places, msg=None):
for i, j in zip(actual, expected):
self.assertAlmostEqual(i, j, places)
self.assertAlmostEqual(i, j, places, msg=msg)


__all__ = [
Expand Down
Loading