From 48cd1a78f23bc28b3293f67447397aeb2c2951a3 Mon Sep 17 00:00:00 2001 From: bweissinger Date: Sun, 1 Sep 2019 18:40:09 -0500 Subject: [PATCH 1/8] Add rounded slot function --- cadquery/cq.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/cadquery/cq.py b/cadquery/cq.py index cd00c9b79..ed70a48f5 100644 --- a/cadquery/cq.py +++ b/cadquery/cq.py @@ -1346,6 +1346,48 @@ def move(self, xDist=0, yDist=0): newCenter = p + Vector(xDist, yDist, 0) return self.newObject([self.plane.toWorldCoords(newCenter)]) + def slot(self, length, diameter, angle): + """ + Creates a rounded slot for each point on the stack. + + :param diameter: desired diameter, or width, of slot + :param length: desired end to end length of slot + :param angle: angle of slot in degrees, with 0 being along x-axis + :return: a new CQ object with the created wires on the stack + + Can be used to create arrays of slots, such as in cooling applications: + + result = cq.Workplane("XY").box(10,25,1).rarray(1,2,1,10).slot(8,1,0).cutThruAll() + """ + + def _makeslot(pnt): + """ + Inner function that is used to create a slot for each point/object on the workplane + :param pnt: The center point for the slot + :return: A CQ object representing a slot + """ + + radius = diameter/2 + centerPoint = self.plane.toWorldCoords(pnt) + + p1 = centerPoint.add(Vector((-length/2) + radius, diameter/2)) + p2 = p1.add(Vector(length - diameter, 0)) + p3 = p1.add(Vector(length - diameter, -diameter)) + p4 = p1.add(Vector(0, -diameter)) + arc1 = p2.add(Vector(radius, -radius)) + arc2 = p4.add(Vector(-radius, radius)) + + edges=[(Edge.makeLine(p1,p2))] + edges.append(Edge.makeThreePointArc(p2, arc1, p3)) + edges.append(Edge.makeLine(p3, p4)) + edges.append(Edge.makeThreePointArc(p4, arc2, p1)) + + slot = Wire.assembleEdges(edges) + + return slot.rotate(centerPoint, centerPoint.add(Vector(0,0,1)), angle) + + return self.eachpoint(_makeslot, True) + def spline(self, listOfXYTuple, tangents=None, periodic=False, forConstruction=False, includeCurrent=False, makeWire=False): """ From c22cdd1c0bedbdc82bf208b32e726a979baf6f20 Mon Sep 17 00:00:00 2001 From: bweissinger Date: Wed, 4 Sep 2019 01:11:07 -0500 Subject: [PATCH 2/8] Add test for slot function. --- tests/TestCadQuery.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/TestCadQuery.py b/tests/TestCadQuery.py index 6a93374d1..fc25e6ae2 100644 --- a/tests/TestCadQuery.py +++ b/tests/TestCadQuery.py @@ -2059,3 +2059,17 @@ def testFindSolid(self): s = r.findSolid() self.assertEqual(len(s.Solids()),2) self.assertTrue(isinstance(s,Compound)) + + def testSlot(self): + + decimal_places = 9 + + #Ensure it produces a solid with the correct volume + result = Workplane("XY").slot(4,1,0).extrude(1) + self.assertAlmostEqual(result.val().Volume(), 3.785398163) + + #Test to see if slot is rotated correctly + result = Workplane("XY").slot(4,1,45).extrude(1) + point = result.faces(">Z").edges(">X").first().val().startPoint().toTuple() + self.assertTupleAlmostEquals(point, (0.707106781, 1.414213562, 1.0), decimal_places) + From 1ef22b779890894d0c4ae5c914d30e83e69614aa Mon Sep 17 00:00:00 2001 From: bweissinger Date: Fri, 6 Sep 2019 14:57:40 -0500 Subject: [PATCH 3/8] Use + operator for vector addition in slot() --- cadquery/cq.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/cadquery/cq.py b/cadquery/cq.py index ed70a48f5..3a6af452b 100644 --- a/cadquery/cq.py +++ b/cadquery/cq.py @@ -1370,12 +1370,12 @@ def _makeslot(pnt): radius = diameter/2 centerPoint = self.plane.toWorldCoords(pnt) - p1 = centerPoint.add(Vector((-length/2) + radius, diameter/2)) - p2 = p1.add(Vector(length - diameter, 0)) - p3 = p1.add(Vector(length - diameter, -diameter)) - p4 = p1.add(Vector(0, -diameter)) - arc1 = p2.add(Vector(radius, -radius)) - arc2 = p4.add(Vector(-radius, radius)) + p1 = centerPoint + Vector((-length/2) + radius, diameter/2) + p2 = p1 + Vector(length - diameter, 0) + p3 = p1 + Vector(length - diameter, -diameter) + p4 = p1 + Vector(0, -diameter) + arc1 = p2 + Vector(radius, -radius) + arc2 = p4 + Vector(-radius, radius) edges=[(Edge.makeLine(p1,p2))] edges.append(Edge.makeThreePointArc(p2, arc1, p3)) @@ -1384,7 +1384,7 @@ def _makeslot(pnt): slot = Wire.assembleEdges(edges) - return slot.rotate(centerPoint, centerPoint.add(Vector(0,0,1)), angle) + return slot.rotate(centerPoint, centerPoint + Vector(0,0,1), angle) return self.eachpoint(_makeslot, True) From fe01b0f58090b94dd2afc4b38f1c542d0f9798e9 Mon Sep 17 00:00:00 2001 From: bweissinger Date: Fri, 6 Sep 2019 15:16:20 -0500 Subject: [PATCH 4/8] remove cast of pnt to world coords --- cadquery/cq.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/cadquery/cq.py b/cadquery/cq.py index 3a6af452b..a910dbca2 100644 --- a/cadquery/cq.py +++ b/cadquery/cq.py @@ -1368,9 +1368,8 @@ def _makeslot(pnt): """ radius = diameter/2 - centerPoint = self.plane.toWorldCoords(pnt) - p1 = centerPoint + Vector((-length/2) + radius, diameter/2) + p1 = pnt + Vector((-length/2) + radius, diameter/2) p2 = p1 + Vector(length - diameter, 0) p3 = p1 + Vector(length - diameter, -diameter) p4 = p1 + Vector(0, -diameter) @@ -1384,7 +1383,7 @@ def _makeslot(pnt): slot = Wire.assembleEdges(edges) - return slot.rotate(centerPoint, centerPoint + Vector(0,0,1), angle) + return slot.rotate(pnt, pnt + Vector(0,0,1), angle) return self.eachpoint(_makeslot, True) From faedc942d5acae552a3cfd4a4b5669c43a5f4164 Mon Sep 17 00:00:00 2001 From: bweissinger Date: Fri, 6 Sep 2019 15:16:53 -0500 Subject: [PATCH 5/8] Add test cases for cutBlind() and cutThruAll() --- tests/TestCadQuery.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/tests/TestCadQuery.py b/tests/TestCadQuery.py index fc25e6ae2..7af014bb1 100644 --- a/tests/TestCadQuery.py +++ b/tests/TestCadQuery.py @@ -2064,11 +2064,18 @@ def testSlot(self): decimal_places = 9 - #Ensure it produces a solid with the correct volume + # Ensure it produces a solid with the correct volume result = Workplane("XY").slot(4,1,0).extrude(1) self.assertAlmostEqual(result.val().Volume(), 3.785398163) - #Test to see if slot is rotated correctly + # Test for proper expected behaviour when cutting + box = Workplane("XY").box(5,5,1) + result = box.faces(">Z").workplane().slot(4,1,0).cutThruAll() + self.assertAlmostEqual(result.val().Volume(), 21.214601837) + result = box.faces(">Z").workplane().slot(4,1,0).cutBlind(-0.5) + self.assertAlmostEqual(result.val().Volume(), 23.107300918) + + # Test to see if slot is rotated correctly result = Workplane("XY").slot(4,1,45).extrude(1) point = result.faces(">Z").edges(">X").first().val().startPoint().toTuple() self.assertTupleAlmostEquals(point, (0.707106781, 1.414213562, 1.0), decimal_places) From fd3a61972cd5483e893ab2970d66c2584457b096 Mon Sep 17 00:00:00 2001 From: bweissinger Date: Fri, 6 Sep 2019 15:19:55 -0500 Subject: [PATCH 6/8] Ensure all tests in testSlot() use decimal_places --- tests/TestCadQuery.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/TestCadQuery.py b/tests/TestCadQuery.py index 7af014bb1..07c389451 100644 --- a/tests/TestCadQuery.py +++ b/tests/TestCadQuery.py @@ -2066,14 +2066,14 @@ def testSlot(self): # Ensure it produces a solid with the correct volume result = Workplane("XY").slot(4,1,0).extrude(1) - self.assertAlmostEqual(result.val().Volume(), 3.785398163) + self.assertAlmostEqual(result.val().Volume(), 3.785398163, decimal_places) # Test for proper expected behaviour when cutting box = Workplane("XY").box(5,5,1) result = box.faces(">Z").workplane().slot(4,1,0).cutThruAll() - self.assertAlmostEqual(result.val().Volume(), 21.214601837) + self.assertAlmostEqual(result.val().Volume(), 21.214601837, decimal_places) result = box.faces(">Z").workplane().slot(4,1,0).cutBlind(-0.5) - self.assertAlmostEqual(result.val().Volume(), 23.107300918) + self.assertAlmostEqual(result.val().Volume(), 23.107300918, decimal_places) # Test to see if slot is rotated correctly result = Workplane("XY").slot(4,1,45).extrude(1) From b80ab618a1535f11bec85f9909cf20a4149dde87 Mon Sep 17 00:00:00 2001 From: bweissinger Date: Mon, 9 Sep 2019 00:44:50 -0500 Subject: [PATCH 7/8] slot(): Set angle default to zero --- cadquery/cq.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cadquery/cq.py b/cadquery/cq.py index a910dbca2..7275b4633 100644 --- a/cadquery/cq.py +++ b/cadquery/cq.py @@ -1346,7 +1346,7 @@ def move(self, xDist=0, yDist=0): newCenter = p + Vector(xDist, yDist, 0) return self.newObject([self.plane.toWorldCoords(newCenter)]) - def slot(self, length, diameter, angle): + def slot(self, length, diameter, angle=0): """ Creates a rounded slot for each point on the stack. From c378d496ba05e0aa6c3b47310c4d78dcc402183b Mon Sep 17 00:00:00 2001 From: bweissinger Date: Sat, 14 Sep 2019 00:18:50 -0500 Subject: [PATCH 8/8] Rename slot() to slot2D() --- cadquery/cq.py | 2 +- tests/TestCadQuery.py | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/cadquery/cq.py b/cadquery/cq.py index 7275b4633..bc289659c 100644 --- a/cadquery/cq.py +++ b/cadquery/cq.py @@ -1346,7 +1346,7 @@ def move(self, xDist=0, yDist=0): newCenter = p + Vector(xDist, yDist, 0) return self.newObject([self.plane.toWorldCoords(newCenter)]) - def slot(self, length, diameter, angle=0): + def slot2D(self, length, diameter, angle=0): """ Creates a rounded slot for each point on the stack. diff --git a/tests/TestCadQuery.py b/tests/TestCadQuery.py index 07c389451..205c1d8ec 100644 --- a/tests/TestCadQuery.py +++ b/tests/TestCadQuery.py @@ -2060,23 +2060,23 @@ def testFindSolid(self): self.assertEqual(len(s.Solids()),2) self.assertTrue(isinstance(s,Compound)) - def testSlot(self): + def testSlot2D(self): decimal_places = 9 # Ensure it produces a solid with the correct volume - result = Workplane("XY").slot(4,1,0).extrude(1) + result = Workplane("XY").slot2D(4,1,0).extrude(1) self.assertAlmostEqual(result.val().Volume(), 3.785398163, decimal_places) # Test for proper expected behaviour when cutting box = Workplane("XY").box(5,5,1) - result = box.faces(">Z").workplane().slot(4,1,0).cutThruAll() + result = box.faces(">Z").workplane().slot2D(4,1,0).cutThruAll() self.assertAlmostEqual(result.val().Volume(), 21.214601837, decimal_places) - result = box.faces(">Z").workplane().slot(4,1,0).cutBlind(-0.5) + result = box.faces(">Z").workplane().slot2D(4,1,0).cutBlind(-0.5) self.assertAlmostEqual(result.val().Volume(), 23.107300918, decimal_places) # Test to see if slot is rotated correctly - result = Workplane("XY").slot(4,1,45).extrude(1) + result = Workplane("XY").slot2D(4,1,45).extrude(1) point = result.faces(">Z").edges(">X").first().val().startPoint().toTuple() self.assertTupleAlmostEquals(point, (0.707106781, 1.414213562, 1.0), decimal_places)