| 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 |
|
| 9 |
# this could be any source of triangles |
| 10 |
# as long as it produces an ocl.STLSurf() we can work with |
| 11 |
def STLSurfaceSource(filename): |
| 12 |
stl = camvtk.STLSurf(filename) |
| 13 |
polydata = stl.src.GetOutput() |
| 14 |
s = ocl.STLSurf() |
| 15 |
camvtk.vtkPolyData2OCLSTL(polydata, s) |
| 16 |
return s |
| 17 |
|
| 18 |
|
| 19 |
# this does not show a toolpath, just the waterlines |
| 20 |
# to make a toolpath one would need to: |
| 21 |
# 1) decide on an order in which to machine |
| 22 |
# the different z-heights (from lowest to highest, or from highest to lowest) |
| 23 |
# 2) within one z-height waterline there can be many loops |
| 24 |
# so decide in what order to machine these |
| 25 |
# 3) create plunge/retract moves at the start and end of each loop |
| 26 |
# optionally some fancier lead-in lead-out moves |
| 27 |
def vtk_visualize_waterlines(stlfile, waterlines): |
| 28 |
myscreen = camvtk.VTKScreen() |
| 29 |
stl = camvtk.STLSurf(stlfile) |
| 30 |
myscreen.addActor(stl) |
| 31 |
stl.SetSurface() # try also SetWireframe() |
| 32 |
stl.SetColor(camvtk.cyan) |
| 33 |
myscreen.camera.SetPosition(15, 13, 7) |
| 34 |
myscreen.camera.SetFocalPoint(5, 5, 0) |
| 35 |
print("Rendering waterlines at ", len(waterlines), " different z-heights") |
| 36 |
n=0 |
| 37 |
for loops in waterlines: # at each z-height, we may get many loops |
| 38 |
print(" %d/%d:" % (n,len(waterlines))) |
| 39 |
drawLoops(myscreen,loops,camvtk.yellow) |
| 40 |
n=n+1 |
| 41 |
camvtk.drawArrows(myscreen,center=(-0.5,-0.5,-0.5)) # XYZ coordinate arrows |
| 42 |
camvtk.drawOCLtext(myscreen) |
| 43 |
myscreen.render() |
| 44 |
myscreen.iren.Start() |
| 45 |
|
| 46 |
def drawLoops(myscreen,loops,loopColor): |
| 47 |
# draw the loops |
| 48 |
nloop = 0 |
| 49 |
for lop in loops: |
| 50 |
n = 0 |
| 51 |
N = len(lop) |
| 52 |
first_point=ocl.Point(-1,-1,5) |
| 53 |
previous=ocl.Point(-1,-1,5) |
| 54 |
for p in lop: |
| 55 |
if n==0: # don't draw anything on the first iteration |
| 56 |
previous=p |
| 57 |
first_point = p |
| 58 |
elif n== (N-1): # the last point |
| 59 |
myscreen.addActor( camvtk.Line(p1=(previous.x,previous.y,previous.z),p2=(p.x,p.y,p.z),color=loopColor) ) # the normal line |
| 60 |
# and a line from p to the first point |
| 61 |
myscreen.addActor( camvtk.Line(p1=(p.x,p.y,p.z),p2=(first_point.x,first_point.y,first_point.z),color=loopColor) ) |
| 62 |
else: |
| 63 |
myscreen.addActor( camvtk.Line(p1=(previous.x,previous.y,previous.z),p2=(p.x,p.y,p.z),color=loopColor) ) |
| 64 |
previous=p |
| 65 |
n=n+1 |
| 66 |
print(" loop ",nloop, " with ", len(lop), " points") |
| 67 |
nloop = nloop+1 |
| 68 |
|
| 69 |
if __name__ == "__main__": |
| 70 |
stlfile = "../../../stl/gnu_tux_mod.stl" |
| 71 |
surface = STLSurfaceSource(stlfile) |
| 72 |
|
| 73 |
|
| 74 |
t_before = time.time() |
| 75 |
|
| 76 |
zheights=[0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6, 1.8, 2.0, 2.2, 2.4, 2.6] |
| 77 |
#zheights=[float(1.0)] # for faster computation, calculate only one waterline |
| 78 |
|
| 79 |
wl = ocl.Waterline() # total time 14 seconds on i7 CPU |
| 80 |
#wl = ocl.AdaptiveWaterline() # this is slower, ca 60 seconds on i7 CPU |
| 81 |
wl.setSTL(surface) |
| 82 |
diam = 0.5 |
| 83 |
length= 10 |
| 84 |
cutter = ocl.BallCutter( diam , length ) # any ocl MillingCutter class should work here |
| 85 |
wl.setCutter(cutter) |
| 86 |
wl.setSampling(0.0314) # this should be smaller than the smallest details in the STL file |
| 87 |
# AdaptiveWaterline() also has settings for minimum sampling interval (see c++ code) |
| 88 |
all_loops=[] |
| 89 |
for zh in zheights: |
| 90 |
print("calculating Waterline at z= ", zh) |
| 91 |
wl.reset() |
| 92 |
wl.setZ(zh) # height for this waterline |
| 93 |
wl.run() |
| 94 |
all_loops.append( wl.getLoops() ) |
| 95 |
t_after = time.time() |
| 96 |
calctime = t_after-t_before |
| 97 |
print(" TOTAL Waterline time is: ", calctime," s") |
| 98 |
|
| 99 |
# waterlines around sharp features tend to be circular. |
| 100 |
# however the algorithm outputs only cutter-location points |
| 101 |
# it would be helpful to find a circular-arc filter so we could reduce |
| 102 |
# the amount of data and output G2/G3 arcs instead. |
| 103 |
|
| 104 |
# output a g-code file FIXME: not done yet |
| 105 |
# write_zig_gcode_file( stlfile, surface.size() , t1, n_raw ,tol,t2,n_filtered, toolpaths ) |
| 106 |
# and/or visualize with VTK |
| 107 |
vtk_visualize_waterlines(stlfile, all_loops) |