Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tornado.httpserver
- import tornado.websocket
- import tornado.ioloop
- import tornado.web
- import ocl
- import json
- def filter_path(path,tol):
- f = ocl.LineCLFilter()
- f.setTolerance(tol)
- for p in path:
- p2 = ocl.CLPoint(p.x,p.y,p.z)
- f.addCLPoint(p2)
- f.run()
- return f.getCLPoints()
- def adaptive_path_drop_cutter(s, cutter, path):
- apdc = ocl.AdaptivePathDropCutter()
- apdc.setSTL(s)
- apdc.setCutter(cutter)
- apdc.setSampling(0.04)
- apdc.setMinSampling(0.0008)
- apdc.setPath( path )
- apdc.run()
- return apdc.getCLPoints()
- def line_to(x,y,z):
- return "G1 X% 8.6f Y% 8.6f Z% 8.6f F%.0f\n" % (x, y, z, feed)
- def xy_line_to(x,y):
- return "G1 X% 8.4f Y% 8.4f\n" % (x, y)
- def xy_rapid_to(x,y):
- return "G0 X% 8.4f Y% 8.4f\n " % (x, y)
- def pen_up():
- return "G0Z% 8.4f\n" % (clearance_height)
- def pen_down(z=0):
- return "G0Z% 8.4f\n" % (feed_height)
- plunge(z)
- def plunge(z):
- return "G1 Z% 8.4f F% 8.0f\n" % (z, plunge_feed)
- def printCLPoints(cl_filtered_paths):
- gcode = ""
- for path in cl_filtered_paths:
- gcode += (pen_up())
- first_pt = path[0]
- gcode+= (xy_rapid_to( first_pt.x, first_pt.y ))
- gcode+= (pen_down( first_pt.z ))
- for p in path[1:]:
- gcode+= (line_to(p.x,p.y,p.z))
- return gcode
- def oclstuff(message):
- s= ocl.STLSurf()
- try:
- obj = json.loads(message)
- except:
- print 'could not evaluate input'
- xmin= obj[0]["xmin"]
- xmax= obj[0]["xmax"]
- ymin= obj[0]["ymin"]
- ymax= obj[0]["ymax"]
- zmax= obj[0]["zmax"]
- diameter = obj[0]['cutterDia']
- length = obj[0]['length']
- triangles = obj[0]['triangles']
- Ny = obj[0]['Ny']
- dy = obj[0]['dy']
- global clearance_height
- clearance_height= obj[0]['clearance_height']
- global feed_height
- feed_height = obj[0]['feed_height']
- global feed
- feed = obj[0]['feed']
- global plunge_feed
- plunge_feed = obj[0]['plunge_feed']
- for tri in triangles:
- p = ocl.Point(tri[0][0],tri[0][0],tri[0][2]);
- q = ocl.Point(tri[1][0],tri[1][0],tri[1][2]);
- r = ocl.Point(tri[2][0],tri[2][0],tri[2][2])
- t = ocl.Triangle(p,q,r)
- s.addTriangle(t)
- cutter = ocl.CylCutter(diameter, length)
- # create a simple "Zig" pattern where we cut only in one direction.
- paths = []
- # create a list of paths
- for n in xrange(0,Ny):
- path = ocl.Path()
- y = ymin+n*dy # current y-coordinate
- p1 = ocl.Point(xmin,y,0) # start-point of line
- p2 = ocl.Point(xmax,y,0) # end-point of line
- l = ocl.Line(p1,p2) # line-object
- path.append( l ) # add the line to the path
- paths.append(path)
- cl_paths=[]
- ## we now have a list of paths to run through apdc
- n_aclp=0
- for p in paths:
- aclp = adaptive_path_drop_cutter(s,cutter,p) # the output is a list of Cutter-Locations
- n_aclp = n_aclp + len(aclp)
- cl_paths.append(aclp)
- tol = 0.001
- cl_filtered_paths = []
- n_filtered=0
- for cl_path in cl_paths:
- cl_filtered = filter_path(cl_path,tol)
- n_filtered = n_filtered + len(cl_filtered)
- cl_filtered_paths.append(cl_filtered)
- # print message
- return (printCLPoints(cl_filtered_paths))
- # print (printCLPoints(cl_filtered_paths))
- class WSHandler(tornado.websocket.WebSocketHandler):
- def open(self):
- print 'new connection'
- #self.write_message("Hello World")
- def on_message(self, message):
- result = oclstuff(message)
- self.write_message(str(result))
- def on_close(self):
- print 'connection closed'
- application = tornado.web.Application([
- (r'/ws', WSHandler),
- ])
- if __name__ == "__main__":
- http_server = tornado.httpserver.HTTPServer(application)
- http_server.listen(8888)
- tornado.ioloop.IOLoop.instance().start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement