| 1 |
from opencamlib import ocl, camvtk |
| 2 |
import time |
| 3 |
|
| 4 |
# 2018.08: Weave not wrapped.. |
| 5 |
|
| 6 |
def generateRange(zmin,zmax,zNmax): |
| 7 |
if zNmax>1: |
| 8 |
dz = (float(zmax)-float(zmin))/(zNmax-1) |
| 9 |
else: |
| 10 |
dz = 0 |
| 11 |
zvals=[] |
| 12 |
for n in range(0,zNmax): |
| 13 |
zvals.append(zmin+n*dz) |
| 14 |
return zvals |
| 15 |
|
| 16 |
def waterline(cutter, s, zh, tol = 0.1 ): |
| 17 |
bpc = ocl.BatchPushCutter() |
| 18 |
bpc.setSTL(s) |
| 19 |
bpc.setCutter(cutter) |
| 20 |
bounds = s.getBounds() |
| 21 |
xmin= bounds[0] - 2*cutter.getRadius() |
| 22 |
xmax= bounds[1] + 2*cutter.getRadius() |
| 23 |
ymin= bounds[2] - 2*cutter.getRadius() |
| 24 |
ymax= bounds[3] + 2*cutter.getRadius() |
| 25 |
Nx= int( (xmax-xmin)/tol ) |
| 26 |
Ny= int( (ymax-ymin)/tol ) |
| 27 |
xvals = generateRange(xmin,xmax,Nx) |
| 28 |
yvals = generateRange(ymin,ymax,Ny) |
| 29 |
for y in yvals: |
| 30 |
f1 = ocl.Point(xmin,y,zh) # start point of fiber |
| 31 |
f2 = ocl.Point(xmax,y,zh) # end point of fiber |
| 32 |
f = ocl.Fiber( f1, f2) |
| 33 |
bpc.appendFiber(f) |
| 34 |
for x in xvals: |
| 35 |
f1 = ocl.Point(x,ymin,zh) # start point of fiber |
| 36 |
f2 = ocl.Point(x,ymax,zh) # end point of fiber |
| 37 |
f = ocl.Fiber( f1, f2) |
| 38 |
bpc.appendFiber(f) |
| 39 |
bpc.run() |
| 40 |
fibers = bpc.getFibers() # get fibers |
| 41 |
w = ocl.Weave() |
| 42 |
for f in fibers: |
| 43 |
w.addFiber(f) |
| 44 |
|
| 45 |
print(" build()...",) |
| 46 |
w.build() |
| 47 |
print("done") |
| 48 |
print(" split()...",) |
| 49 |
subw = w.get_components() |
| 50 |
print("done. graph has", len(subw),"sub-weaves") |
| 51 |
m=0 |
| 52 |
loops = [] |
| 53 |
for sw in subw: |
| 54 |
print(m," face_traverse...",) |
| 55 |
t_before = time.time() |
| 56 |
sw.face_traverse() |
| 57 |
t_after = time.time() |
| 58 |
calctime = t_after-t_before |
| 59 |
print("done in ", calctime," s.") |
| 60 |
sw_loops = sw.getLoops() |
| 61 |
for lop in sw_loops: |
| 62 |
loops.append(lop) |
| 63 |
m=m+1 |
| 64 |
return loops |
| 65 |
|
| 66 |
if __name__ == "__main__": |
| 67 |
print(ocl.version()) |
| 68 |
myscreen = camvtk.VTKScreen() |
| 69 |
#stl = camvtk.STLSurf("../../stl/demo.stl") |
| 70 |
stl = camvtk.STLSurf("../../../stl/gnu_tux_mod.stl") |
| 71 |
myscreen.addActor(stl) |
| 72 |
stl.SetWireframe() |
| 73 |
#stl.SetSurface() |
| 74 |
stl.SetColor(camvtk.cyan) |
| 75 |
polydata = stl.src.GetOutput() |
| 76 |
s = ocl.STLSurf() |
| 77 |
camvtk.vtkPolyData2OCLSTL(polydata, s) |
| 78 |
print("STL surface read,", s.size(), "triangles") |
| 79 |
zh=1.9 |
| 80 |
cutter_diams = generateRange(0.1, 6, 5) |
| 81 |
loops = [] |
| 82 |
length = 20 # cutter length |
| 83 |
for diam in cutter_diams: |
| 84 |
cutter = ocl.CylCutter( diam, length ) |
| 85 |
cutter_loops = waterline(cutter, s, zh, 0.05 ) |
| 86 |
for l in cutter_loops: |
| 87 |
loops.append(l) |
| 88 |
|
| 89 |
print("All waterlines done. Got", len(loops)," loops in total.") |
| 90 |
# draw the loops |
| 91 |
for lop in loops: |
| 92 |
n = 0 |
| 93 |
N = len(lop) |
| 94 |
first_point=ocl.Point(-1,-1,5) |
| 95 |
previous=ocl.Point(-1,-1,5) |
| 96 |
for p in lop: |
| 97 |
if n==0: # don't draw anything on the first iteration |
| 98 |
previous=p |
| 99 |
first_point = p |
| 100 |
elif n== (N-1): # the last point |
| 101 |
myscreen.addActor( camvtk.Line(p1=(previous.x,previous.y,previous.z),p2=(p.x,p.y,p.z),color=camvtk.yellow) ) # the normal line |
| 102 |
# and a line from p to the first point |
| 103 |
myscreen.addActor( camvtk.Line(p1=(p.x,p.y,p.z),p2=(first_point.x,first_point.y,first_point.z),color=camvtk.yellow) ) |
| 104 |
else: |
| 105 |
myscreen.addActor( camvtk.Line(p1=(previous.x,previous.y,previous.z),p2=(p.x,p.y,p.z),color=camvtk.yellow) ) |
| 106 |
previous=p |
| 107 |
n=n+1 |
| 108 |
|
| 109 |
print("done.") |
| 110 |
myscreen.camera.SetPosition(0.5, 3, 2) |
| 111 |
myscreen.camera.SetFocalPoint(0.5, 0.5, 0) |
| 112 |
camvtk.drawArrows(myscreen,center=(-0.5,-0.5,-0.5)) |
| 113 |
camvtk.drawOCLtext(myscreen) |
| 114 |
myscreen.render() |
| 115 |
myscreen.iren.Start() |
| 116 |
#raw_input("Press Enter to terminate") |