Skip to content
This repository has been archived by the owner on Feb 9, 2021. It is now read-only.

Commit

Permalink
Add logic to the close command
Browse files Browse the repository at this point in the history
If start and end point of a set of 2d edges coincide; create a wire directly.
If there is a distance between start and end point; add a line segment before creating the wire.
  • Loading branch information
gntech committed Apr 25, 2018
1 parent 86f3b7c commit d458dcb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
9 changes: 8 additions & 1 deletion cadquery/cq.py
Original file line number Diff line number Diff line change
Expand Up @@ -1773,7 +1773,14 @@ def close(self):
s = Workplane().lineTo(1,0).lineTo(1,1).close().extrude(0.2)
"""
self.lineTo(self.ctx.firstPoint.x, self.ctx.firstPoint.y)
endPoint = self._findFromPoint(False)
startPoint = self.ctx.firstPoint

# Check if there is a distance between startPoint and endPoint
# that is larger than what is considered a numerical error.
# If so; add a line segment between endPoint and startPoint
if endPoint.sub(startPoint).Length > 1e-6:
self.lineTo(self.ctx.firstPoint.x, self.ctx.firstPoint.y)

# Need to reset the first point after closing a wire
self.ctx.firstPoint=None
Expand Down
12 changes: 12 additions & 0 deletions tests/TestCadQuery.py
Original file line number Diff line number Diff line change
Expand Up @@ -1595,3 +1595,15 @@ def testExtrude(self):
self.assertTupleAlmostEquals(delta.toTuple(),
(0.,0.,2.*h),
decimal_places)

def testClose(self):
# Close without endPoint and startPoint coincide.
# Create a half-circle
a = Workplane(Plane.XY()).sagittaArc((10, 0), 2).close().extrude(2)

# Close when endPoint and startPoint coincide.
# Create a double half-circle
b = Workplane(Plane.XY()).sagittaArc((10, 0), 2).sagittaArc((0, 0), 2).close().extrude(2)

# The b shape shall have twice the volume of the a shape.
self.assertAlmostEqual(a.val().wrapped.Volume * 2.0, b.val().wrapped.Volume)

0 comments on commit d458dcb

Please sign in to comment.