Ejemplo n.º 1
0
def OutputGCode(lev, paths, fn):
    nw.clearance_height = config.top + config.clearance_above_top
    nw.feed_height = config.top + config.engage_above_top
    nw.feed = config.feed
    nw.plunge_feed = config.plunge_feed

    nw.writer = FileWriter(fn)

    nw.comment("============ START G-CODE ===============")
    nw.preamble()
    nw.pen_up()
    pairs = zip(lev, paths)
    for lev, path in sorted(pairs, key = lambda(p): -p[0]):
        nw.comment("level=%s" % lev)
        for curve in path:
            vertices = curve.getVertices()
            current = vertices[0].p
            nw.xy_rapid_to(current.x, current.y)
            nw.pen_down(lev)
            for v in vertices[1:]:
                if v.type == 0:
                    nw.line_to(v.p.x, v.p.y, lev)
                else:
                    r = math.hypot(v.p.x - v.c.x, v.p.y - v.c.y)
                    nw.xy_arc_to(v.p.x, v.p.y, r, v.c.x, v.c.y, v.type != 1)
            nw.pen_up()
    nw.postamble()
    nw.comment("============ END G-CODE ===============")
    
    nw.writer.Close()
Ejemplo n.º 2
0
def write_zig_gcode_file(filename, n_triangles, t1,n1,tol,t2,n2, toolpath):
    ngc_writer.clearance_height= 5 # XY rapids at this height
    ngc_writer.feed_height = 3     # use z plunge-feed below this height
    ngc_writer.feed = 200          # feedrate 
    ngc_writer.plunge_feed = 100   # plunge feedrate
    ngc_writer.metric = False      # metric/inch flag
    ngc_writer.comment( " OpenCAMLib %s" % ocl.version() ) # git version-tag
    # it is probably useful to include this in all g-code output, so that bugs/problems can be tracked
    
    ngc_writer.comment( " STL surface: %s" % filename )
    ngc_writer.comment( "   triangles: %d" % n_triangles )
    ngc_writer.comment( " OpenCamLib::AdaptivePathDropCutter run took %.2f s" % t1 )
    ngc_writer.comment( " got %d raw CL-points " % n1 )
    ngc_writer.comment( " filtering to tolerance %.4f " % ( tol )  )
    ngc_writer.comment( " got %d filtered CL-points. Filter done in %.3f s " % ( n2 , t2 ) )
    ngc_writer.preamble()
    # a "Zig" or one-way parallel finish path
    # 1) lift to clearance height
    # 2) XY rapid to start of path
    # 3) plunge to correct z-depth
    # 4) feed along path until end 
    for path in toolpath:
        ngc_writer.pen_up()  
        first_pt = path[0]
        ngc_writer.xy_rapid_to( first_pt.x, first_pt.y )
        ngc_writer.pen_down( first_pt.z )
        for p in path[1:]:
            ngc_writer.line_to(p.x,p.y,p.z)
    ngc_writer.postamble() # end of program
Ejemplo n.º 3
0
def rapid_to_new_branch(myscreen, prv_tang, nxt_tang, c1, r1, c2, r2, prv, nxt):
    # rapid from prev, to nxt
    # while staying inside c1(r1) and c2(r)
    rad_default = 0.003
    rad1 = min(rad_default, 0.9 * r1)  # wrong? we get the new-branch r1 here, while we would want the old-branch r1
    rad2 = min(rad_default, 0.9 * r2)
    prv_tang.normalize()
    nxt_tang.normalize()

    prv_normal = -1 * prv_tang.xy_perp()
    nxt_normal = nxt_tang.xy_perp()

    cen1 = prv + rad1 * prv_normal  # + rad1*prv_tang

    cen2 = nxt - rad2 * nxt_normal  # rapid_tang # + rad1*prv_tang

    rapid_tang = cen2 - cen1
    rapid_tang.normalize()

    trg1 = cen1 + rad1 * prv_tang
    src2 = cen2 - rad2 * nxt_tang

    drawArc(myscreen, prv, trg1, rad1, cen1, True, ovdvtk.orange)  # lead-out arc
    ngc_writer.pen_up()
    ovdvtk.drawLine(myscreen, trg1, src2, ovdvtk.magenta)  # rapid
    ngc_writer.xy_line_to(src2.x, src2.y)
    ngc_writer.pen_down()
    drawArc(myscreen, src2, nxt, rad2, cen2, True, ovdvtk.mag2)  # lead-in arc
Ejemplo n.º 4
0
def printOffsets(ofs):
    nloop = 0
    for lop in ofs:
        n = 0
        N = len(lop)
        first_point=[]
        previous=[]
        for p in lop:
            # p[0] is the Point
            # p[1] is -1 for lines, and r for arcs
            if n==0: # don't draw anything on the first iteration
                previous=p[0]
                ngc_writer.pen_up()
                ngc_writer.xy_rapid_to( scale*previous.x, scale*previous.y)
                ngc_writer.pen_down()
            else:
                cw=p[3]  # cw or ccw arc
                cen=p[2] # center of arc
                r=p[1]   # radius of arc
                p=p[0]   # target position
                if r==-1: # -1 means line
                    ngc_writer.xy_line_to( scale*p.x, scale*p.y )
                else:
                    ngc_writer.xy_arc_to( scale*p.x, scale*p.y, scale*r, scale*cen.x, scale*cen.y, cw ) 
                previous=p
            n=n+1
        nloop = nloop+1
Ejemplo n.º 5
0
def printOffsets(ofs):
    nloop = 0
    for lop in ofs:
        n = 0
        N = len(lop)
        first_point = []
        previous = []
        for p in lop:
            # p[0] is the Point
            # p[1] is -1 for lines, and r for arcs
            if n == 0:  # don't draw anything on the first iteration
                previous = p[0]
                ngc_writer.pen_up()
                ngc_writer.xy_rapid_to(scale * previous.x, scale * previous.y)
                ngc_writer.pen_down()
            else:
                cw = p[3]  # cw or ccw arc
                cen = p[2]  # center of arc
                r = p[1]  # radius of arc
                p = p[0]  # target position
                if r == -1:  # -1 means line
                    ngc_writer.xy_line_to(scale * p.x, scale * p.y)
                else:
                    ngc_writer.xy_arc_to(scale * p.x, scale * p.y, scale * r,
                                         scale * cen.x, scale * cen.y, cw)
                previous = p
            n = n + 1
        nloop = nloop + 1
def rapid_to_new_branch(myscreen, prv_tang, nxt_tang, c1, r1, c2, r2, prv,
                        nxt):
    # rapid from prev, to nxt
    # while staying inside c1(r1) and c2(r)
    rad_default = 0.003
    rad1 = min(
        rad_default, 0.9 * r1
    )  # wrong? we get the new-branch r1 here, while we would want the old-branch r1
    rad2 = min(rad_default, 0.9 * r2)
    prv_tang.normalize()
    nxt_tang.normalize()

    prv_normal = -1 * prv_tang.xy_perp()
    nxt_normal = nxt_tang.xy_perp()

    cen1 = prv + rad1 * prv_normal  # + rad1*prv_tang

    cen2 = nxt - rad2 * nxt_normal  #rapid_tang # + rad1*prv_tang

    rapid_tang = cen2 - cen1
    rapid_tang.normalize()

    trg1 = cen1 + rad1 * prv_tang
    src2 = cen2 - rad2 * nxt_tang

    drawArc(myscreen, prv, trg1, rad1, cen1, True,
            ovdvtk.orange)  # lead-out arc
    ngc_writer.pen_up()
    ovdvtk.drawLine(myscreen, trg1, src2, ovdvtk.magenta)  # rapid
    ngc_writer.xy_line_to(src2.x, src2.y)
    ngc_writer.pen_down()
    drawArc(myscreen, src2, nxt, rad2, cen2, True, ovdvtk.mag2)  # lead-in arc
Ejemplo n.º 7
0
def write_zig_gcode_file(filename, n_triangles, t1, n1, tol, t2, n2, toolpath):
    ngc_writer.clearance_height = 5  # XY rapids at this height
    ngc_writer.feed_height = 3  # use z plunge-feed below this height
    ngc_writer.feed = 200  # feedrate
    ngc_writer.plunge_feed = 100  # plunge feedrate
    ngc_writer.metric = False  # metric/inch flag
    ngc_writer.comment(" OpenCAMLib %s" % ocl.version())  # git version-tag
    # it is probably useful to include this in all g-code output, so that bugs/problems can be tracked

    ngc_writer.comment(" STL surface: %s" % filename)
    ngc_writer.comment("   triangles: %d" % n_triangles)
    ngc_writer.comment(" OpenCamLib::AdaptivePathDropCutter run took %.2f s" %
                       t1)
    ngc_writer.comment(" got %d raw CL-points " % n1)
    ngc_writer.comment(" filtering to tolerance %.4f " % (tol))
    ngc_writer.comment(" got %d filtered CL-points. Filter done in %.3f s " %
                       (n2, t2))
    ngc_writer.preamble()
    # a "Zig" or one-way parallel finish path
    # 1) lift to clearance height
    # 2) XY rapid to start of path
    # 3) plunge to correct z-depth
    # 4) feed along path until end
    for path in toolpath:
        ngc_writer.pen_up()
        first_pt = path[0]
        ngc_writer.xy_rapid_to(first_pt.x, first_pt.y)
        ngc_writer.pen_down(first_pt.z)
        for p in path[1:]:
            ngc_writer.line_to(p.x, p.y, p.z)
    ngc_writer.postamble()  # end of program
def printCLPoints(cl_filtered_paths):
    ngc_writer.preamble()
    
    for path in cl_filtered_paths:
        ngc_writer.pen_up()
        first_pt = path[0]
        ngc_writer.xy_rapid_to( first_pt.x, first_pt.y )
        ngc_writer.pen_down( first_pt.z )
        for p in path[1:]:
            ngc_writer.line_to(p.x,p.y,p.z)

    ngc_writer.postamble()
Ejemplo n.º 9
0
def printCLPoints(cl_filtered_paths):
    ngc_writer.preamble()

    for path in cl_filtered_paths:
        ngc_writer.pen_up()
        first_pt = path[0]
        ngc_writer.xy_rapid_to(first_pt.x, first_pt.y)
        ngc_writer.pen_down(first_pt.z)
        for p in path[1:]:
            ngc_writer.line_to(p.x, p.y, p.z)

    ngc_writer.postamble()
Ejemplo n.º 10
0
def printMedial(vd, scale):
    maw = ovd.MedialAxisWalk(  vd.getGraph() )
    toolpath = maw.walk()
    for chain in toolpath:
        n = 0
        for move in chain:
            for point in move:
                if n==0: # don't draw anything on the first iteration
                    p = point[0]
                    zdepth = scale*point[1]
                    ngc_writer.pen_up();
                    ngc_writer.xy_rapid_to( scale*p.x, scale*p.y );
                    ngc_writer.pen_down( z= -zdepth )
                else:
                    p = point[0]
                    z = point[1]
                    ngc_writer.line_to( scale*p.x, scale*p.y, scale*(-z) )
                n=n+1
    return
Ejemplo n.º 11
0
def printMedial(vd, scale):
    maw = ovd.MedialAxisWalk(vd.getGraph())
    toolpath = maw.walk()
    for chain in toolpath:
        n = 0
        for move in chain:
            for point in move:
                if n == 0:  # don't draw anything on the first iteration
                    p = point[0]
                    zdepth = scale * point[1]
                    ngc_writer.pen_up()
                    ngc_writer.xy_rapid_to(scale * p.x, scale * p.y)
                    ngc_writer.pen_down(z=-zdepth)
                else:
                    p = point[0]
                    z = point[1]
                    ngc_writer.line_to(scale * p.x, scale * p.y, scale * (-z))
                n = n + 1
    return
Ejemplo n.º 12
0
def printMedial(vd):
    maw = ovd.MedialAxisWalk(  vd.getGraph() )
    toolpath = maw.walk()
    for chain in toolpath:
        n = 0
        for move in chain:
            for point in move:
                if n==0: # don't draw anything on the first iteration
                    p = point[0]
                    z = point[1]
                    ngc_writer.pen_up();
                    ngc_writer.xy_rapid_to( scale*p.x, scale*p.y );
                    ngc_writer.pen_down()
                    ngc_writer.plunge( -z ) # now we are at the correct height, at the startpoint of the first move
                else:
                    p = point[0]
                    z = point[1]
                    ngc_writer.line_to( scale*p.x, scale*p.y, scale*(-z) )
                n=n+1
    return
Ejemplo n.º 13
0
def spiral_clear(myscreen, cutwidth, out_tangent, in_tangent, c1, r1, c2, r2,
                 out1, in1):
    print "( spiral clear! )"
    ngc_writer.pen_up()

    # end spiral at in1
    # archimedean spiral
    # r = a + b theta

    in1_dir = in1 - c1
    in1_theta = math.atan2(in1_dir.y, in1_dir.x)
    # in1_theta = in1_theta
    # print "c1 =", c1
    # print "in1 = ",in1
    # print " end theta = ",in1_theta
    drawPoint(myscreen, c1, ovdvtk.red)
    # drawPoint( myscreen, in1, ovdvtk.blue, 0.006 )
    # width = 2*pi*b
    # => b = width/(2*pi)
    b = cutwidth / (2 * math.pi)
    # r = a + b in1_theta = r_max
    # =>
    # a = r_max-b*in1_theta
    a = r1 - b * in1_theta

    # figure out the start-angle
    theta_min = in1_theta
    theta_max = in1_theta
    dtheta = 0.1
    min_r = 0.001
    while True:
        r = a + b * theta_min
        if r < min_r:
            break
        else:
            theta_min = theta_min - dtheta
    # print "start_theta = ", theta_min

    Npts = (theta_max - theta_min) / dtheta
    Npts = int(Npts)
    # print "spiral has ",Npts," points"
    p = ovd.Point(c1)
    ngc_writer.xy_rapid_to(p.x, p.y)
    ngc_writer.pen_down()

    theta_end = 0
    for n in range(Npts + 1):
        theta = theta_min + n * dtheta
        r = a + b * theta
        theta = theta - 2 * abs(in1_theta - math.pi / 2)
        trg = c1 + r * ovd.Point(-math.cos(theta), math.sin(theta))
        ovdvtk.drawLine(myscreen, p, trg, ovdvtk.pink)
        ngc_writer.xy_line_to(trg.x, trg.y)
        p = trg
        theta_end = theta

    # add a complete circle after the spiral.
    print "( spiral-clear: final circle )"
    Npts = (2 * math.pi) / dtheta
    Npts = int(Npts)
    for n in range(Npts + 2):
        theta = theta_end + (n + 1) * dtheta
        # theta = theta_min + n*dtheta
        r = r1  # a + b*theta
        # theta = theta - 2* abs(in1_theta - math.pi/2 )
        trg = c1 + r * ovd.Point(-math.cos(theta), math.sin(theta))
        ovdvtk.drawLine(myscreen, p, trg, ovdvtk.pink)
        ngc_writer.xy_line_to(trg.x, trg.y)

        # if n != Npts+1:
        #    drawPoint(myscreen, trg, ovdvtk.orange)
        # else:
        #    drawPoint(myscreen, trg, ovdvtk.orange,0.004)
        p = trg
Ejemplo n.º 14
0
def spiral_clear(myscreen, out_tangent, in_tangent, c1, r1, c2, r2, out1, in1):
    print "( spiral clear! )"
    ngc_writer.pen_up()

    # end spiral at in1
    # archimedean spiral
    # r = a + b theta

    in1_dir = in1 - c1
    in1_theta = math.atan2(in1_dir.y, in1_dir.x)
    # in1_theta = in1_theta
    # print "c1 =", c1
    # print "in1 = ",in1
    # print " end theta = ",in1_theta
    drawPoint(myscreen, c1, ovdvtk.red)
    # drawPoint( myscreen, in1, ovdvtk.blue, 0.006 )
    # width = 2*pi*b
    # => b = width/(2*pi)
    b = 0.01 / (2 * math.pi)
    # r = a + b in1_theta = r_max
    # =>
    # a = r_max-b*in1_theta
    a = r1 - b * in1_theta

    # figure out the start-angle
    theta_min = in1_theta
    theta_max = in1_theta
    dtheta = 0.1
    min_r = 0.001
    while True:
        r = a + b * theta_min
        if r < min_r:
            break
        else:
            theta_min = theta_min - dtheta
    # print "start_theta = ", theta_min

    Npts = (theta_max - theta_min) / dtheta
    Npts = int(Npts)
    # print "spiral has ",Npts," points"
    p = ovd.Point(c1)
    ngc_writer.xy_rapid_to(p.x, p.y)
    ngc_writer.pen_down()

    theta_end = 0
    for n in range(Npts + 1):
        theta = theta_min + n * dtheta
        r = a + b * theta
        theta = theta - 2 * abs(in1_theta - math.pi / 2)
        trg = c1 + r * ovd.Point(-math.cos(theta), math.sin(theta))
        ovdvtk.drawLine(myscreen, p, trg, ovdvtk.pink)
        ngc_writer.xy_line_to(trg.x, trg.y)
        p = trg
        theta_end = theta

    # add a complete circle after the spiral.
    print "( spiral-clear: final circle )"
    Npts = (2 * math.pi) / dtheta
    Npts = int(Npts)
    for n in range(Npts + 2):
        theta = theta_end + (n + 1) * dtheta
        # theta = theta_min + n*dtheta
        r = r1  # a + b*theta
        # theta = theta - 2* abs(in1_theta - math.pi/2 )
        trg = c1 + r * ovd.Point(-math.cos(theta), math.sin(theta))
        ovdvtk.drawLine(myscreen, p, trg, ovdvtk.pink)
        ngc_writer.xy_line_to(trg.x, trg.y)

        # if n != Npts+1:
        #    drawPoint(myscreen, trg, ovdvtk.orange)
        # else:
        #    drawPoint(myscreen, trg, ovdvtk.orange,0.004)
        p = trg