From d458dcb22ce338e342f4e22b8b3d84f7a358ffaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20N=C3=A4slund?= Date: Wed, 25 Apr 2018 17:47:02 +0200 Subject: [PATCH] Add logic to the close command 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. --- cadquery/cq.py | 9 ++++++++- tests/TestCadQuery.py | 12 ++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/cadquery/cq.py b/cadquery/cq.py index 38d9b41..fd614ef 100644 --- a/cadquery/cq.py +++ b/cadquery/cq.py @@ -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 diff --git a/tests/TestCadQuery.py b/tests/TestCadQuery.py index 5b16a7f..9b5a245 100644 --- a/tests/TestCadQuery.py +++ b/tests/TestCadQuery.py @@ -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)