def make_area_for_roughing(k): num_spans = kurve.num_spans(k) if num_spans == 0: raise "sketch has no spans!" d, startx, starty, ex, ey, cx, cy = kurve.get_span(k, 0) d, sx, sy, endx, endy, cx, cy = kurve.get_span(k, num_spans - 1) a = area.Area() c = area.Curve() largey = 7 for span in range(0, num_spans): d, sx, sy, ex, ey, cx, cy = kurve.get_span(k, span) if span == 0: # first span c.append( area.Vertex(0, area.Point(startx, largey), area.Point(0, 0))) c.append(area.Vertex(d, area.Point(ex, ey), area.Point(cx, cy))) # close the area c.append(area.Vertex(0, area.Point(endx, largey), area.Point(0, 0))) c.append(area.Vertex(0, area.Point(startx, largey), area.Point(0, 0))) a.append(c) return a
def make_circle(p, radius): circle = area.Area() c = area.Curve() c.append(p + area.Point(radius, 0)) c.append(area.Vertex(1, p + area.Point(-radius, 0), p)) c.append(area.Vertex(1, p + area.Point(radius, 0), p)) circle.append(c) return circle
def zigzag(a, stepover, zig_unidirectional): if a.num_curves() == 0: return global rightward_for_zigs global curve_list_for_zigs global sin_angle_for_zigs global cos_angle_for_zigs global sin_minus_angle_for_zigs global cos_minus_angle_for_zigs global one_over_units one_over_units = 1 / area.get_units() a = rotated_area(a) b = area.Box() a.GetBox(b) x0 = b.MinX() - 1.0 x1 = b.MaxX() + 1.0 height = b.MaxY() - b.MinY() num_steps = int(height / stepover + 1) y = b.MinY() + 0.1 * one_over_units null_point = area.Point(0, 0) rightward_for_zigs = True curve_list_for_zigs = [] for i in range(0, num_steps): y0 = y y = y + stepover p0 = area.Point(x0, y0) p1 = area.Point(x0, y) p2 = area.Point(x1, y) p3 = area.Point(x1, y0) c = area.Curve() c.append(area.Vertex(0, p0, null_point, 0)) c.append(area.Vertex(0, p1, null_point, 0)) c.append(area.Vertex(0, p2, null_point, 1)) c.append(area.Vertex(0, p3, null_point, 0)) c.append(area.Vertex(0, p0, null_point, 1)) a2 = area.Area() a2.append(c) a2.Intersect(a) make_zig(a2, y0, y, zig_unidirectional) if zig_unidirectional == False: rightward_for_zigs = (rightward_for_zigs == False) reorder_zigs()
def add_roll_off(curve, roll_off_curve, direction, roll_radius, offset_extra, roll_off): if direction == "on": return if roll_off is None: return if curve.getNumVertices() <= 1: return last_span = curve.GetLastSpan() if roll_off == 'auto': if roll_radius < 0.0000000001: return v = last_span.GetVector(1.0) # get end direction if direction == 'right': off_v = area.Point(v.y, -v.x) else: off_v = area.Point(-v.y, v.x) rollend = last_span.v.p + off_v * roll_radius else: rollend = roll_off # add the end of the original kurve roll_off_curve.append(last_span.v.p) if rollend == last_span.v.p: return rvertex = area.Vertex(rollend) v = last_span.GetVector(1.0) # get end direction rvertex.c, rvertex.type = area.TangentialArc(last_span.v.p, rollend, v) # add the roll off arc roll_off_curve.append(rvertex)
def add_roll_on(curve, roll_on_curve, direction, roll_radius, offset_extra, roll_on): if direction == "on": roll_on = None if curve.getNumVertices() <= 1: return first_span = curve.GetFirstSpan() if roll_on is None: rollstart = first_span.p elif roll_on == 'auto': if roll_radius < 0.0000000001: rollstart = first_span.p v = first_span.GetVector(0.0) if direction == 'right': off_v = area.Point(v.y, -v.x) else: off_v = area.Point(-v.y, v.x) rollstart = first_span.p + off_v * roll_radius else: rollstart = roll_on rvertex = area.Vertex(first_span.p) if first_span.p == rollstart: rvertex.type = 0 else: v = first_span.GetVector(0.0) # get start direction rvertex.c, rvertex.type = area.TangentialArc( first_span.p, rollstart, -v) rvertex.type = -rvertex.type # because TangentialArc was used in reverse # add a start roll on point roll_on_curve.append(rollstart) # add the roll on arc roll_on_curve.append(rvertex)
def make_zig_curve(curve, y0, y): global test_count if rightward_for_zigs: curve.Reverse() zig = area.Curve() zig_started = False zag_found = False prev_p = None for vertex in curve.getVertices(): if prev_p != None: if math.fabs(vertex.p.y - y0) < 0.002: if zig_started: zig.append(unrotated_vertex(vertex)) elif math.fabs(prev_p.y - y0) < 0.002 and vertex.type == 0: zig.append( area.Vertex(0, unrotated_point(prev_p), area.Point(0, 0))) zig.append(unrotated_vertex(vertex)) zig_started = True elif zig_started: zig.append(unrotated_vertex(vertex)) if math.fabs(vertex.p.y - y) < 0.002: zag_found = True break prev_p = vertex.p if zig_started: curve_list_for_zigs.append(zig)
def makeAreaVertex(seg): if seg.ShapeType == 'Edge': if isinstance(seg.Curve, Part.Circle): segtype = int(seg.Curve.Axis.z) # 1=ccw arc,-1=cw arc vertex = area.Vertex( segtype, area.Point( seg.valueAt(seg.LastParameter)[0], seg.valueAt(seg.LastParameter)[1]), area.Point(seg.Curve.Center.x, seg.Curve.Center.y)) elif isinstance(seg.Curve, Part.LineSegment) or isinstance( seg.Curve, Part.Line): point1 = seg.valueAt(seg.FirstParameter)[0], seg.valueAt( seg.FirstParameter)[1] point2 = seg.valueAt(seg.LastParameter)[0], seg.valueAt( seg.LastParameter)[1] segtype = 0 # 0=line vertex = area.Point( seg.valueAt(seg.LastParameter)[0], seg.valueAt(seg.LastParameter)[1]) else: pass # print "returning vertex: area.Point(" + # str(seg.valueAt(seg.LastParameter)[0]) +"," + # str(seg.valueAt(seg.LastParameter)[1]) +")" return vertex
def make_obround(p0, p1, radius): dir = p1 - p0 d = dir.length() dir.normalize() right = area.Point(dir.y, -dir.x) obround = area.Area() c = area.Curve() vt0 = p0 + right * radius vt1 = p1 + right * radius vt2 = p1 - right * radius vt3 = p0 - right * radius c.append(area.Vertex(0, vt0, area.Point(0, 0))) c.append(area.Vertex(0, vt1, area.Point(0, 0))) c.append(area.Vertex(1, vt2, p1)) c.append(area.Vertex(0, vt3, area.Point(0, 0))) c.append(area.Vertex(1, vt0, p0)) obround.append(c) return obround
def calculate_span_cylinders(self, span, color): sz = span.p.y ez = span.v.p.y z = sz while z < ez: # make a line at this z intersection_line = area.Span( area.Point(0, z), area.Vertex(0, area.Point(300, z), area.Point(0, 0)), False) intersections = span.Intersect(intersection_line) if len(intersections): radius = intersections[0].x * toolpath.coords.voxels_per_mm self.cylinders.append( VoxelCyl(radius, z * toolpath.coords.voxels_per_mm, color)) z += 1 / toolpath.coords.voxels_per_mm
def arc(self, dir, x, y, z, i, j, k, r): self.rapid_flag = False if x == None: x = self.x if y == None: y = self.y if z == None: z = self.z area.set_units(0.05) curve = area.Curve() curve.append(area.Point(self.x, self.y)) curve.append(area.Vertex(dir, area.Point(x, y), area.Point(i, j))) curve.UnFitArcs() for span in curve.GetSpans(): self.add_line(Point(span.p.x, span.p.y, z), Point(span.v.p.x, span.v.p.y, z)) self.x = x self.y = y self.z = z
def arc(self, x=None, y=None, z=None, i=None, j=None, k=None, r=None, ccw=True): recreator.Redirector.arc(self, x, y, z, i, j, k, r, ccw) # add an arc to the path if self.path == None: self.path = area.Curve() self.path.append( area.Vertex(1 if ccw else -1, area.Point(self.x, self.y), area.Point(i, j)))
rapid_safety_space = float(2) start_depth = float(0) step_down = float(1) final_depth = float(-1) tool_diameter = float(4.7752) cutting_edge_angle = float(0) #absolute() mode roll_radius = float(2) offset_extra = 0 comment('Sketch') curve = area.Curve() #open path curve.append(area.Point(66.163292, 40.55579)) curve.append(area.Point(66.163292, -37.200397)) curve.append( area.Vertex(-1, area.Point(56.163292, -47.200397), area.Point(56.163292, -37.200397))) curve.append(area.Point(-58.730675, -47.200397)) curve.append( area.Vertex(-1, area.Point(-68.730675, -37.200397), area.Point(-58.730675, -37.200397))) curve.append(area.Point(-68.730675, 40.55579)) roll_on = 'auto' roll_off = 'auto' extend_at_start = 0 extend_at_end = 0 lead_in_line_len = 0 lead_out_line_len = 0 kurve_funcs.profile(curve, 'left', tool_diameter / 2, offset_extra, roll_radius, roll_on, roll_off, rapid_safety_space, clearance, start_depth, step_down, final_depth,
def unrotated_vertex(v): if v.type: return area.Vertex(v.type, unrotated_point(v.p), unrotated_point(v.c)) return area.Vertex(v.type, unrotated_point(v.p), area.Point(0, 0))
def load(self, nc_filepath): # this converts the G1s in an NC file into arcs with G2 or G3 pattern_main = re.compile( '([(!;].*|\s+|[a-zA-Z0-9_:](?:[+-])?\d*(?:\.\d*)?|\w\#\d+|\(.*?\)|\#\d+\=(?:[+-])?\d*(?:\.\d*)?)' ) self.lines = [] self.length = 0.0 file = open(nc_filepath, 'r') arc = 0 self.rapid = False curx = None cury = None curz = None while (True): line = file.readline().rstrip() if len(line) == 0: break move = False x = None y = None z = None i = None j = None words = pattern_main.findall(line) for word in words: word = word.upper() if word == 'G1' or word == 'G01': self.rapid = False arc = 0 elif word == 'G2' or word == 'G02': self.rapid = False arc = -1 elif word == 'G3' or word == 'G03': self.rapid = False arc = 1 elif word == 'G0' or word == 'G00': self.rapid = True arc = 0 elif word[0] == 'X': x = eval(word[1:]) move = True elif word[0] == 'Y': y = eval(word[1:]) move = True elif word[0] == 'Z': z = eval(word[1:]) move = True elif word[0] == 'I': i = float(eval(word[1:])) elif word[0] == 'J': j = float(eval(word[1:])) elif word[0] == 'T': self.current_tool = eval(word[1:]) if (curx != None) and (cury != None) and (curz != None): self.add_line(Point(curx, cury, curz), Point(curx, cury, 30.0)) curz = 30.0 elif word[0] == ';': break if move: if (curx != None) and (cury != None) and (curz != None): newx = curx newy = cury newz = curz if x != None: newx = float(x) if y != None: newy = float(y) if z != None: newz = float(z) if arc != 0: area.set_units(0.05) curve = area.Curve() curve.append(area.Point(curx, cury)) # next 4 lines were for Bridgeport. # this only works for LinuxCNC now #if (newx > curx) != (arc > 0): # j = -j #if (newy > cury) != (arc < 0): # i = -i curve.append( area.Vertex(arc, area.Point(newx, newy), area.Point(curx + i, cury + j))) curve.UnFitArcs() for span in curve.GetSpans(): self.add_line(Point(span.p.x, span.p.y, newz), Point(span.v.p.x, span.v.p.y, newz)) else: self.add_line(Point(curx, cury, curz), Point(newx, newy, newz)) if x != None: curx = float(x) if y != None: cury = float(y) if z != None: curz = float(z) for line in self.lines: self.length += line.Length() file.close() self.rewind()
def zigzag(a, a_firstoffset, stepover): if a.num_curves() == 0: return global rightward_for_zigs global curve_list_for_zigs global test_count global sin_angle_for_zigs global cos_angle_for_zigs global sin_minus_angle_for_zigs global cos_minus_angle_for_zigs a = rotated_area(a) b = area.Box() a.GetBox(b) #x0 = b.MinX() - 1.0 #x1 = b.MaxX() + 1.0 x1 = b.MinX() - 1.0 x0 = b.MaxX() + 1.0 height = b.MaxY() - b.MinY() num_steps = int(height / stepover + 1) #y = b.MinY() + 0.1 y = b.MaxY() - 0.1 null_point = area.Point(0, 0) rightward_for_zigs = True curve_list_for_zigs = [] test_count = 0 for i in range(0, num_steps): #collect vertices for a box shape from X+,Y+ toward the curve #then move the tool Y+ and then back toward the X start position # -------> # | # -------< test_count = test_count + 1 y0 = y #y = y + stepover y = y - stepover p0 = area.Point(x0, y0) p1 = area.Point(x0, y) p2 = area.Point(x1, y) p3 = area.Point(x1, y0) c = area.Curve() c.append(area.Vertex(0, p0, null_point, 0)) c.append(area.Vertex(0, p1, null_point, 0)) c.append(area.Vertex(0, p2, null_point, 1)) c.append(area.Vertex(0, p3, null_point, 0)) c.append(area.Vertex(0, p0, null_point, 1)) a2 = area.Area() a2.append(c) a2.Intersect(a) rightward_for_zigs = (rightward_for_zigs == False) y10 = y + stepover #y = y + stepover y2 = y + stepover * 2 p10 = area.Point(x0, y10) p11 = area.Point(x0, y2) p12 = area.Point(x1, y2) p13 = area.Point(x1, y10) c2 = area.Curve() c2.append(area.Vertex(0, p10, null_point, 0)) c2.append(area.Vertex(0, p11, null_point, 0)) c2.append(area.Vertex(0, p12, null_point, 1)) c2.append(area.Vertex(0, p13, null_point, 0)) c2.append(area.Vertex(0, p10, null_point, 1)) a3 = area.Area() a3.append(c2) a3.Intersect(a) make_zig(a3, y0, y) rightward_for_zigs = (rightward_for_zigs == False) reorder_zigs()
def make_zig_curve(curve, y0, y, zig_unidirectional): if rightward_for_zigs: curve.Reverse() # find a high point to start looking from high_point = None for vertex in curve.getVertices(): if high_point == None: high_point = vertex.p elif vertex.p.y > high_point.y: # use this as the new high point high_point = vertex.p elif math.fabs(vertex.p.y - high_point.y) < 0.002 * one_over_units: # equal high point if rightward_for_zigs: # use the furthest left point if vertex.p.x < high_point.x: high_point = vertex.p else: # use the furthest right point if vertex.p.x > high_point.x: high_point = vertex.p zig = area.Curve() high_point_found = False zig_started = False zag_found = False for i in range( 0, 2 ): # process the curve twice because we don't know where it will start prev_p = None for vertex in curve.getVertices(): if zag_found: break if prev_p != None: if zig_started: zig.append(unrotated_vertex(vertex)) if math.fabs(vertex.p.y - y) < 0.002 * one_over_units: zag_found = True break elif high_point_found: if math.fabs(vertex.p.y - y0) < 0.002 * one_over_units: if zig_started: zig.append(unrotated_vertex(vertex)) elif math.fabs( prev_p.y - y0 ) < 0.002 * one_over_units and vertex.type == 0: zig.append( area.Vertex(0, unrotated_point(prev_p), area.Point(0, 0))) zig.append(unrotated_vertex(vertex)) zig_started = True elif vertex.p.x == high_point.x and vertex.p.y == high_point.y: high_point_found = True prev_p = vertex.p if zig_started: if zig_unidirectional == True: # remove the last bit of zig if math.fabs(zig.LastVertex().p.y - y) < 0.002 * one_over_units: vertices = zig.getVertices() while len(vertices) > 0: v = vertices[len(vertices) - 1] if math.fabs(v.p.y - y0) < 0.002 * one_over_units: break else: vertices.pop() zig = area.Curve() for v in vertices: zig.append(v) curve_list_for_zigs.append(zig)
import centroid1 output(strippedpath[:-striptmp]+ 'tmp/test.tap') program_begin(123, 'Test program') absolute() metric() comment('') workplane(1) curve = area.Curve() #closed path curve.append(area.Point( 16.2551994324, 9.0)) curve.append(area.Vertex(1 , area.Point( 16.2552, 17.7616031082), area.Point(16.2552, 13.3808))) curve.append(area.Point( -24.0, 17.7616004944)) curve.append(area.Vertex(1 , area.Point( -24.0, 8.99999689178), area.Point(-24.0, 13.3808))) curve.append(area.Point(-24.0,17.7616031082))curve.append(area.Point(16.2551994324,9.0)) curve.Reverse() tool_diameter = float(6.35) tool_side ='left' flush_nc() clearance = float(50.0) rapid_safety_space = float(5.0) roll_on = 'auto' roll_off = 'auto' roll_radius = 2.0 lead_in_line_len= 2.0 lead_out_line_len= 2.0 extend_at_start=2.0