Skip to content

Commit

Permalink
Add support for slicing (#339)
Browse files Browse the repository at this point in the history
* Allow constructing infinite planes

* Preliminary section implementation

* Better error message

* Section related tests
  • Loading branch information
adam-urbanczyk authored May 6, 2020
1 parent 1491e0c commit a140d10
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
21 changes: 21 additions & 0 deletions cadquery/cq.py
Original file line number Diff line number Diff line change
Expand Up @@ -3420,6 +3420,27 @@ def text(
newS = newS.clean()
return newS

def section(self, height=0.0):
"""
Slices current solid at the given height.
:param float height: height to slice at (default: 0)
:return: a CQ object with the resulting face(s).
"""

solidRef = self.findSolid(searchStack=True, searchParents=True)

if solidRef is None:
raise ValueError("Cannot find solid to slice")

plane = Face.makePlane(
basePnt=self.plane.origin + self.plane.zDir * height, dir=self.plane.zDir
)

r = solidRef.intersect(plane)

return self.newObject([r])

def _repr_html_(self):
"""
Special method for rendering current object in a jupyter notebook
Expand Down
11 changes: 7 additions & 4 deletions cadquery/occ_impl/shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1208,17 +1208,20 @@ def makeNSidedSurface(
return cls.cast(face).fix()

@classmethod
def makePlane(cls, length, width, basePnt=(0, 0, 0), dir=(0, 0, 1)):
def makePlane(cls, length=None, width=None, basePnt=(0, 0, 0), dir=(0, 0, 1)):
basePnt = Vector(basePnt)
dir = Vector(dir)

pln_geom = gp_Pln(basePnt.toPnt(), dir.toDir())

return cls(
BRepBuilderAPI_MakeFace(
if length and width:
pln_shape = BRepBuilderAPI_MakeFace(
pln_geom, -width * 0.5, width * 0.5, -length * 0.5, length * 0.5
).Face()
)
else:
pln_shape = BRepBuilderAPI_MakeFace(pln_geom).Face()

return cls(pln_shape)

@classmethod
def makeRuledSurface(cls, edgeOrWire1, edgeOrWire2, dist=None):
Expand Down
15 changes: 15 additions & 0 deletions tests/test_cadquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -3395,3 +3395,18 @@ def testUnionCompound(self):
obj = Workplane("XY").newObject(list_of_shapes).cut(shape_to_cut)

assert obj.val().isValid()

def testSection(self):

box = Workplane("XY", origin=(1, 2, 3)).box(1, 1, 1)

s1 = box.section()
s2 = box.section(0.5)

self.assertAlmostEqual(s1.faces().val().Area(), 1)
self.assertAlmostEqual(s2.faces().val().Area(), 1)

line = Workplane("XY").hLine(1)

with self.assertRaises(ValueError):
line.section()

0 comments on commit a140d10

Please sign in to comment.