| 1 |
|
| 2 |
# simple parallel finish toolpath example |
| 3 |
# Anders Wallin 2014-02-23 |
| 4 |
|
| 5 |
import time |
| 6 |
|
| 7 |
from opencamlib import ocl, camvtk |
| 8 |
import ngc_writer # G-code output is produced by this module |
| 9 |
|
| 10 |
# create a simple "Zig" pattern where we cut only in one direction. |
| 11 |
# the first line is at ymin |
| 12 |
# the last line is at ymax |
| 13 |
def YdirectionZigPath(xmin,xmax,ymin,ymax,Ny): |
| 14 |
paths = [] |
| 15 |
dy = float(ymax-ymin)/(Ny-1) # the y step-over |
| 16 |
for n in range(0,Ny): |
| 17 |
path = ocl.Path() |
| 18 |
y = ymin+n*dy # current y-coordinate |
| 19 |
if (n==Ny-1): |
| 20 |
assert( y==ymax) |
| 21 |
elif (n==0): |
| 22 |
assert( y==ymin) |
| 23 |
p1 = ocl.Point(xmin,y,0) # start-point of line |
| 24 |
p2 = ocl.Point(xmax,y,0) # end-point of line |
| 25 |
l = ocl.Line(p1,p2) # line-object |
| 26 |
path.append( l ) # add the line to the path |
| 27 |
paths.append(path) |
| 28 |
return paths |
| 29 |
|
| 30 |
# run the actual drop-cutter algorithm |
| 31 |
def adaptive_path_drop_cutter(surface, cutter, paths): |
| 32 |
apdc = ocl.AdaptivePathDropCutter() |
| 33 |
apdc.setSTL(surface) |
| 34 |
apdc.setCutter(cutter) |
| 35 |
apdc.setSampling(0.04) # maximum sampling or "step-forward" distance |
| 36 |
# should be set so that we don't loose any detail of the STL model |
| 37 |
# i.e. this number should be similar or smaller than the smallest triangle |
| 38 |
apdc.setMinSampling(0.01) # minimum sampling or step-forward distance |
| 39 |
# the algorithm subdivides "steep" portions of the toolpath |
| 40 |
# until we reach this limit. |
| 41 |
# 0.0008 |
| 42 |
cl_paths=[] |
| 43 |
n_points=0 |
| 44 |
for path in paths: |
| 45 |
apdc.setPath( path ) |
| 46 |
apdc.run() |
| 47 |
cl_points = apdc.getCLPoints() |
| 48 |
n_points = n_points + len( cl_points ) |
| 49 |
cl_paths.append( apdc.getCLPoints() ) |
| 50 |
return (cl_paths, n_points) |
| 51 |
|
| 52 |
# this could be any source of triangles |
| 53 |
# as long as it produces an ocl.STLSurf() we can work with |
| 54 |
def STLSurfaceSource(filename): |
| 55 |
stl = camvtk.STLSurf(filename) |
| 56 |
polydata = stl.src.GetOutput() |
| 57 |
s = ocl.STLSurf() |
| 58 |
camvtk.vtkPolyData2OCLSTL(polydata, s) |
| 59 |
return s |
| 60 |
|
| 61 |
# filter a single path |
| 62 |
def filter_path(path,tol): |
| 63 |
f = ocl.LineCLFilter() |
| 64 |
f.setTolerance(tol) |
| 65 |
for p in path: |
| 66 |
p2 = ocl.CLPoint(p.x,p.y,p.z) |
| 67 |
f.addCLPoint(p2) |
| 68 |
f.run() |
| 69 |
return f.getCLPoints() |
| 70 |
|
| 71 |
# to reduce the G-code size we filter here. (this is not strictly required and could be omitted) |
| 72 |
# we could potentially detect G2/G3 arcs here, if there was a filter for that. |
| 73 |
# idea: |
| 74 |
# if a path in the raw toolpath has three points (p1,p2,p3) |
| 75 |
# and point p2 lies within tolerance of the straight line p1-p3 |
| 76 |
# then we simplify the path to (p1,p3) |
| 77 |
def filterCLPaths(cl_paths, tolerance=0.001): |
| 78 |
cl_filtered_paths = [] |
| 79 |
t_before = time.time() |
| 80 |
n_filtered=0 |
| 81 |
for cl_path in cl_paths: |
| 82 |
cl_filtered = filter_path(cl_path,tol) |
| 83 |
|
| 84 |
n_filtered = n_filtered + len(cl_filtered) |
| 85 |
cl_filtered_paths.append(cl_filtered) |
| 86 |
return (cl_filtered_paths, n_filtered) |
| 87 |
|
| 88 |
# this uses ngc_writer and writes G-code to stdout or a file |
| 89 |
def write_zig_gcode_file(filename, n_triangles, t1,n1,tol,t2,n2, toolpath): |
| 90 |
ngc_writer.clearance_height= 5 # XY rapids at this height |
| 91 |
ngc_writer.feed_height = 3 # use z plunge-feed below this height |
| 92 |
ngc_writer.feed = 200 # feedrate |
| 93 |
ngc_writer.plunge_feed = 100 # plunge feedrate |
| 94 |
ngc_writer.metric = False # metric/inch flag |
| 95 |
ngc_writer.comment( " OpenCAMLib %s" % ocl.version() ) # git version-tag |
| 96 |
# it is probably useful to include this in all g-code output, so that bugs/problems can be tracked |
| 97 |
|
| 98 |
ngc_writer.comment( " STL surface: %s" % filename ) |
| 99 |
ngc_writer.comment( " triangles: %d" % n_triangles ) |
| 100 |
ngc_writer.comment( " OpenCAMLib::AdaptivePathDropCutter run took %.2f s" % t1 ) |
| 101 |
ngc_writer.comment( " got %d raw CL-points " % n1 ) |
| 102 |
ngc_writer.comment( " filtering to tolerance %.4f " % ( tol ) ) |
| 103 |
ngc_writer.comment( " got %d filtered CL-points. Filter done in %.3f s " % ( n2 , t2 ) ) |
| 104 |
ngc_writer.preamble() |
| 105 |
# a "Zig" or one-way parallel finish path |
| 106 |
# 1) lift to clearance height |
| 107 |
# 2) XY rapid to start of path |
| 108 |
# 3) plunge to correct z-depth |
| 109 |
# 4) feed along path until end |
| 110 |
for path in toolpath: |
| 111 |
ngc_writer.pen_up() |
| 112 |
first_pt = path[0] |
| 113 |
ngc_writer.xy_rapid_to( first_pt.x, first_pt.y ) |
| 114 |
ngc_writer.pen_down( first_pt.z ) |
| 115 |
for p in path[1:]: |
| 116 |
ngc_writer.line_to(p.x,p.y,p.z) |
| 117 |
ngc_writer.postamble() # end of program |
| 118 |
|
| 119 |
def vtk_visualize_parallel_finish_zig(stlfile, toolpaths): |
| 120 |
myscreen = camvtk.VTKScreen() |
| 121 |
stl = camvtk.STLSurf(stlfile) |
| 122 |
myscreen.addActor(stl) |
| 123 |
stl.SetSurface() # try also SetWireframe() |
| 124 |
stl.SetColor(camvtk.cyan) |
| 125 |
myscreen.camera.SetPosition(15, 13, 7) |
| 126 |
myscreen.camera.SetFocalPoint(5, 5, 0) |
| 127 |
|
| 128 |
rapid_height= 5 # XY rapids at this height |
| 129 |
feed_height = 3 |
| 130 |
rapidColor = camvtk.pink |
| 131 |
XYrapidColor = camvtk.green |
| 132 |
plungeColor = camvtk.red |
| 133 |
feedColor = camvtk.yellow |
| 134 |
# zig path algorithm: |
| 135 |
# 1) lift to clearance height |
| 136 |
# 2) XY rapid to start of path |
| 137 |
# 3) plunge to correct z-depth |
| 138 |
# 4) feed along path until end |
| 139 |
pos = ocl.Point(0,0,0) # keep track of the current position of the tool |
| 140 |
first = True |
| 141 |
for path in toolpaths: |
| 142 |
first_pt = path[0] |
| 143 |
if (first == True): # green sphere at path start |
| 144 |
myscreen.addActor( camvtk.Sphere(center=(first_pt.x,first_pt.y,rapid_height) , radius=0.1, color=camvtk.green) ) |
| 145 |
pos = ocl.Point(first_pt.x,first_pt.y,first_pt.z) # at start of program, assume we have already a rapid move here |
| 146 |
first = False |
| 147 |
else: # not the very first move |
| 148 |
# retract up to rapid_height |
| 149 |
myscreen.addActor( camvtk.Line(p1=( pos.x,pos.y,pos.z),p2=(pos.x,pos.y,feed_height),color=plungeColor) ) |
| 150 |
myscreen.addActor( camvtk.Line(p1=( pos.x,pos.y,feed_height),p2=(pos.x,pos.y,rapid_height),color=rapidColor) ) |
| 151 |
# XY rapid into position |
| 152 |
myscreen.addActor( camvtk.Line(p1=( pos.x,pos.y,rapid_height),p2=( first_pt.x,first_pt.y,rapid_height),color=XYrapidColor) ) |
| 153 |
pos = ocl.Point(first_pt.x,first_pt.y,first_pt.z) |
| 154 |
|
| 155 |
# rapid down to the feed_height |
| 156 |
myscreen.addActor( camvtk.Line(p1=( pos.x,pos.y,rapid_height),p2=(pos.x,pos.y,feed_height),color=rapidColor) ) |
| 157 |
# feed down to CL |
| 158 |
myscreen.addActor( camvtk.Line(p1=( pos.x,pos.y,feed_height),p2=(pos.x,pos.y,pos.z),color=plungeColor) ) |
| 159 |
|
| 160 |
# feed along the path |
| 161 |
for p in path[1:]: |
| 162 |
myscreen.addActor( camvtk.Line(p1=( pos.x,pos.y,pos.z),p2=(p.x,p.y,p.z),color=feedColor) ) |
| 163 |
pos = ocl.Point(p.x,p.y,p.z) |
| 164 |
|
| 165 |
# END retract up to rapid_height |
| 166 |
myscreen.addActor( camvtk.Line(p1=( pos.x,pos.y,pos.z),p2=(pos.x,pos.y,feed_height),color=plungeColor) ) |
| 167 |
myscreen.addActor( camvtk.Line(p1=( pos.x,pos.y,feed_height),p2=(pos.x,pos.y,rapid_height),color=rapidColor) ) |
| 168 |
myscreen.addActor( camvtk.Sphere(center=(pos.x,pos.y,rapid_height) , radius=0.1, color=camvtk.red) ) |
| 169 |
|
| 170 |
camvtk.drawArrows(myscreen,center=(-0.5,-0.5,-0.5)) # XYZ coordinate arrows |
| 171 |
camvtk.drawOCLtext(myscreen) |
| 172 |
myscreen.render() |
| 173 |
myscreen.iren.Start() |
| 174 |
|
| 175 |
|
| 176 |
if __name__ == "__main__": |
| 177 |
stlfile = "../../../stl/gnu_tux_mod.stl" |
| 178 |
surface = STLSurfaceSource(stlfile) |
| 179 |
|
| 180 |
|
| 181 |
# choose a cutter for the operation: |
| 182 |
# http://www.anderswallin.net/2011/08/opencamlib-cutter-shapes/ |
| 183 |
diameter=0.25 |
| 184 |
length=5 |
| 185 |
#cutter = ocl.BallCutter(diameter, length) |
| 186 |
cutter = ocl.CylCutter(diameter, length) |
| 187 |
#corner_radius = 0.05 |
| 188 |
#cutter = ocl.BullCutter(diameter, corner_radius, length) |
| 189 |
#angle = math.pi/4 |
| 190 |
#cutter = ocl.ConeCutter(diameter, angle, length) |
| 191 |
#cutter = cutter.offsetCutter( 0.4 ) |
| 192 |
|
| 193 |
# toolpath is contained in this simple box |
| 194 |
ymin=0 |
| 195 |
ymax=12 |
| 196 |
xmin=0 |
| 197 |
xmax=10 |
| 198 |
Ny=40 # number of lines in the y-direction |
| 199 |
|
| 200 |
paths = YdirectionZigPath(xmin,xmax,ymin,ymax,Ny) |
| 201 |
|
| 202 |
# now project onto the STL surface |
| 203 |
t_before = time.time() |
| 204 |
(raw_toolpath, n_raw) = adaptive_path_drop_cutter(surface,cutter,paths) |
| 205 |
t1 = time.time() |
| 206 |
|
| 207 |
# filter raw toolpath to reduce size |
| 208 |
tol = 0.001 |
| 209 |
(toolpaths, n_filtered) = filterCLPaths(raw_toolpath, tolerance=0.001) |
| 210 |
t2 = time.time()-t_before |
| 211 |
|
| 212 |
# output a g-code file |
| 213 |
write_zig_gcode_file( stlfile, surface.size() , t1, n_raw ,tol,t2,n_filtered, toolpaths ) |
| 214 |
# and/or visualize with VTK |
| 215 |
vtk_visualize_parallel_finish_zig(stlfile, toolpaths) |