class test_segment(unittest.TestCase): """Test for segment.py""" def setUp(self): self.pt1 = Point(0, 0, 0) self.pt2 = Point(100, 0, 100) self.pt3 = Point(100, 0, 0) self.pt4 = Point(0, 0, 100) self.pt5 = Point(-120.12, 0, 214.09) self.pt6 = Point(-179.88, 0, 85.91) self.pt7 = Point(-214.09, 0, 179.88) self.pt8 = Point(-85.91, 0, 120.12) self.pt9 = Point(-164.74, 0, 118.39) self.pt10 = Point(-137.55, 0, 176.70) self.lineSegment1 = Segment(self.pt1, self.pt2) self.lineSegment2 = Segment(self.pt5, self.pt6) self.lineSegment3 = Segment(self.pt9, self.pt10) self.inverseLineSegment1 = Segment(self.pt3, self.pt4) self.arcSegment1 = Segment(self.pt1, self.pt2, 1.5) self.arcSegment2 = Segment(self.pt7, self.pt8, 1.5) self.arcSegment3 = Segment(self.pt3, self.pt4, 1.5) self.inverseArcSegment1 = Segment(self.pt1, self.pt2, -1.5) self.inverseArcSegment2 = Segment(self.pt7, self.pt8, -1.5) def test_get_angle(self): angle = self.lineSegment1.get_angle() self.assertEqual(angle, 180) arcAngle = self.arcSegment1.get_angle() self.assertEqual(arcAngle, 3.931174892989316) def test_set_bulge(self): tempSegment = self.lineSegment1 tempSegment.set_bulge(3.931174892989316) self.assertEqual(tempSegment.bulge, 1.5) def test_set_bulge_from_radius(self): tempSegment = self.lineSegment1 tempSegment.set_bulge_from_radius(76.60323462854265) self.assertEqual(tempSegment.bulge, 0.6666666666666667) def test_get_centre_point(self): lineCentrePt = self.lineSegment1.get_centre_point() self.assertEqual(lineCentrePt, None) arcCentrePt = self.arcSegment1.get_centre_point() centrePt = Point(70.83333333333333, 0.0, 29.166666666666668) self.assertTrue(arcCentrePt.is_same(centrePt)) invArcCenPt = self.inverseArcSegment1.get_centre_point() centrePt = Point(29.166666666666668, 0.0, 70.83333333333333) self.assertTrue(invArcCenPt.is_same(centrePt)) def test_get_radius(self): lineRadius = self.lineSegment1.get_radius() self.assertEqual(lineRadius, 0) arcRadius = self.arcSegment1.get_radius() self.assertEqual(arcRadius, 76.60323462854265) def test_get_rotation(self): angle = self.lineSegment1.get_rotation() self.assertEqual(angle, 45) def test_get_extent_min(self): extentMinX = self.lineSegment2.get_extent_min('X') self.assertEqual(extentMinX, self.pt5.X) extentMinY = self.lineSegment2.get_extent_min('Y') self.assertEqual(extentMinY, self.pt5.Y) extentMinZ = self.lineSegment2.get_extent_min('Z') self.assertEqual(extentMinZ, self.pt6.Z) AecExtentMinX = self.arcSegment2.get_extent_min('X') self.assertEqual(AecExtentMinX, self.pt8.X) AecExtentMinY = self.arcSegment2.get_extent_min('Y') self.assertEqual(AecExtentMinY, self.pt5.Y) AecExtentMinZ = self.arcSegment2.get_extent_min('Z') self.assertEqual(AecExtentMinZ, 46.68997508893331) def test_get_extent_max(self): extentMaxX = self.lineSegment2.get_extent_max('X') self.assertEqual(extentMaxX, self.pt6.X) extentMaxY = self.lineSegment2.get_extent_max('Y') self.assertEqual(extentMaxY, self.pt5.Y) extentMaxZ = self.lineSegment2.get_extent_max('Z') self.assertEqual(extentMaxZ, self.pt5.Z) AecExtentMaxX = self.arcSegment2.get_extent_max('X') self.assertEqual(AecExtentMaxX, -239.0558582444) AecExtentMaxY = self.arcSegment2.get_extent_max('Y') self.assertEqual(AecExtentMaxY, -76.60585824440003) AecExtentMaxZ = self.arcSegment2.get_extent_max('Z') self.assertEqual(AecExtentMaxZ, self.pt7.Z) def test_get_all_axis_positions(self): allAxisPosX = self.lineSegment2.get_all_axis_positions('X') self.assertEqual(allAxisPosX, [self.pt5.X, self.pt6.X]) allAxisPosY = self.lineSegment2.get_all_axis_positions('Y') self.assertEqual(allAxisPosY, [self.pt5.Y, self.pt6.Y]) allAxisPosZ = self.lineSegment2.get_all_axis_positions('Z') self.assertEqual(allAxisPosZ, [self.pt5.Z, self.pt6.Z]) arcAllAxisPosX = self.arcSegment2.get_all_axis_positions('X') self.assertEqual(arcAllAxisPosX, [self.pt7.X, self.pt8.X, -239.0558582444]) arcAllAxisPosY = self.arcSegment2.get_all_axis_positions('Y') self.assertEqual(arcAllAxisPosY, [self.pt7.Y, self.pt8.Y, -76.60585824440003]) arcAllAxisPosZ = self.arcSegment2.get_all_axis_positions('Z') self.assertEqual(arcAllAxisPosZ, [self.pt7.Z, self.pt8.Z, 46.68997508893331]) def test_get_length(self): length = self.lineSegment1.get_length() self.assertEqual(length, 141.4213562373095) arcSegmentLength = self.arcSegment1.get_length() self.assertEqual(arcSegmentLength, 141.4213562373095) def test_get_eta(self): eta = self.lineSegment1.get_eta() self.assertEqual(eta, 90) arcEta = self.arcSegment1.get_eta() self.assertEqual(arcEta, 1.965587446494658) def test_get_epsilon(self): epsilon = self.lineSegment1.get_epsilon() self.assertEqual(epsilon, 0) arcEpsilon = self.arcSegment1.get_epsilon() self.assertEqual(arcEpsilon, 0.982793723247329) def test_get_phi(self): phi = self.lineSegment1.get_phi() self.assertEqual(phi, -88.42920367320511) arcPhi = self.arcSegment1.get_phi() self.assertEqual(arcPhi, 0.5880026035475675) def test_is_same(self): lineComparison = self.lineSegment1.is_same(self.lineSegment1) self.assertTrue(lineComparison) arcComparison = self.arcSegment1.is_same(self.arcSegment1) self.assertTrue(arcComparison) lineArcComparison = self.lineSegment1.is_same(self.arcSegment1) self.assertFalse(lineArcComparison) def test_intersect(self): intersect, pts = self.lineSegment1.intersect(self.inverseLineSegment1) pt = pts[0] self.assertTrue(intersect) intersectionPt = Point(50, 0.0, 50) self.assertTrue(pt.is_same(intersectionPt)) intersect, pts = self.arcSegment1.intersect(self.inverseLineSegment1) pt = pts[0] self.assertTrue(intersect) intersectionPt = Point(16.666666666666657, 0.0, 83.33333333333334) self.assertTrue(pt.is_same(intersectionPt)) intersect, pts = self.inverseArcSegment1.intersect( self.inverseLineSegment1) pt = pts[0] self.assertTrue(intersect) intersectionPt = Point(83.33333333333334, 0.0, 16.66666666666666) self.assertTrue(pt.is_same(intersectionPt)) intersect, pts = self.arcSegment2.intersect(self.lineSegment2) pt = pts[0] self.assertTrue(intersect) intersectionPt = Point(-130.08000000000004, 0.0, 192.72666666666657) self.assertTrue(pt.is_same(intersectionPt)) intersect, pts = self.inverseArcSegment2.intersect(self.lineSegment2) pt = pts[0] self.assertTrue(intersect) intersectionPt = Point(-169.91999999999996, 0.0, 107.27333333333338) self.assertTrue(pt.is_same(intersectionPt)) intersect, pts = self.arcSegment1.intersect(self.arcSegment3) pt = pts[0] self.assertTrue(intersect) intersectionPt = Point(-2.882525053975556, 0.0, 50) self.assertTrue(pts[0].is_same(intersectionPt)) # false intersection tests intersect, pts = self.inverseArcSegment2.intersect(self.lineSegment1) self.assertFalse(intersect) intersect, pts = self.arcSegment2.intersect(self.lineSegment3) self.assertFalse(intersect)
def generate_path(self): """Generate the path for the Rough operation""" self.part_segment_group = self.part_segment_group.remove_the_groove( self.stock.z_min, self.tool, self.allow_grooving) self.clearing_paths = [] z_max = self.stock.z_max + self.start_offset + self.clearance line_count = int( math.ceil((self.stock.x_length() + self.extra_dia * 0.5) / self.step_over)) xstart = 0 - (self.step_over * line_count + self.min_dia * 0.5) # create roughing boundary offset by the stock to leave value roughing_boundary = self.part_segment_group.offset_path( self.stock_to_leave) for roughing_pass in range(line_count): xpt = xstart + roughing_pass * self.step_over # check if the roughing pass start is outside the stock boundary_z = roughing_boundary.z_at_x(xpt) if boundary_z and round(boundary_z, 5) >= round( self.stock.z_max, 5): continue pt1 = Point(xpt, 0, z_max) pt2 = Point(xpt, 0, z_max - self.stock.z_length() - self.start_offset) path_line = Segment(pt1, pt2) intersections = [] for seg in roughing_boundary.get_segments(): intersect, point = seg.intersect(path_line) if intersect: for p in point: intersection = Intersection(p, seg) intersections.append(intersection) # build list of segments segmentgroup = SegmentGroup() if not intersections: seg = path_line segmentgroup.add_segment(seg) if len(intersections) == 1: # Only one intersection, trim line to intersection. seg = Segment(pt1, intersections[0].point) segmentgroup.add_segment(seg) if intersections[0].seg: # add lead out startPt = intersections[0].point endPt = startPt.project(self.leadout_angle, self.step_over) path_line = Segment(startPt, endPt) segmentgroup.add_segment(path_line) if len(intersections) > 1: # more than one intersection # add the end points of the pass to generate new segments intersection = Intersection(pt1, None) intersections.insert(0, intersection) intersection2 = Intersection(pt2, None) intersections.append(intersection2) # sort the a list of intersections by their z position intersections = sorted(intersections, key=lambda p: p.point.Z, reverse=True) for i in range(len(intersections)): if i + 1 < len(intersections): if i % 2 == 0: # primary segment primary_segment = Segment( intersections[i].point, intersections[i + 1].point) # check the length of the pass before adding to the segmentgroup if primary_segment.get_length() < self.step_over: continue # if the intersection is connected to another segment if intersections[i].seg: # add a lead in # TODO: optimise this to match the max tool angle endPt = intersections[i].point startPt = endPt.project( self.leadin_angle, self.step_over) path_line = Segment(startPt, endPt) segmentgroup.add_segment(path_line) # add the primary segment to the segment group segmentgroup.add_segment(primary_segment) # if the intersection is connected to another segment if intersections[i + 1].seg: # add a lead out startPt = intersections[i + 1].point endPt = startPt.project( self.leadout_angle, self.step_over) path_line = Segment(startPt, endPt) segmentgroup.add_segment(path_line) if segmentgroup.count(): self.tool_paths.append(segmentgroup)