Advertisement
danfalck

server_send_triangles.py

Mar 1st, 2014
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.97 KB | None | 0 0
  1. import tornado.httpserver
  2. import tornado.websocket
  3. import tornado.ioloop
  4. import tornado.web
  5. import ocl
  6. import json
  7.  
  8. def filter_path(path,tol):
  9.     f = ocl.LineCLFilter()
  10.     f.setTolerance(tol)
  11.     for p in path:
  12.         p2 = ocl.CLPoint(p.x,p.y,p.z)
  13.         f.addCLPoint(p2)
  14.     f.run()
  15.     return  f.getCLPoints()
  16.  
  17. def adaptive_path_drop_cutter(s, cutter, path):
  18.     apdc = ocl.AdaptivePathDropCutter()
  19.     apdc.setSTL(s)
  20.     apdc.setCutter(cutter)
  21.     apdc.setSampling(0.04)
  22.     apdc.setMinSampling(0.0008)
  23.     apdc.setPath( path )
  24.     apdc.run()
  25.     return apdc.getCLPoints()
  26.  
  27. def line_to(x,y,z):
  28.     return "G1 X% 8.6f Y% 8.6f Z% 8.6f F%.0f\n" % (x, y, z, feed)
  29.  
  30. def xy_line_to(x,y):
  31.     return "G1 X% 8.4f Y% 8.4f\n" % (x, y)
  32.  
  33. def xy_rapid_to(x,y):
  34.     return "G0 X% 8.4f Y% 8.4f\n " % (x, y)
  35.  
  36. def pen_up():
  37.     return "G0Z% 8.4f\n" % (clearance_height)
  38.  
  39. def pen_down(z=0):
  40.     return "G0Z% 8.4f\n" % (feed_height)
  41.     plunge(z)
  42.  
  43. def plunge(z):
  44.     return "G1 Z% 8.4f F% 8.0f\n" % (z, plunge_feed)
  45.  
  46. def printCLPoints(cl_filtered_paths):
  47.     gcode = ""
  48.     for path in cl_filtered_paths:
  49.         gcode += (pen_up())
  50.         first_pt = path[0]
  51.         gcode+= (xy_rapid_to( first_pt.x, first_pt.y ))
  52.         gcode+= (pen_down( first_pt.z ))
  53.         for p in path[1:]:
  54.             gcode+= (line_to(p.x,p.y,p.z))
  55.     return gcode
  56.  
  57. def oclstuff(message):
  58.     s= ocl.STLSurf()
  59.     try:
  60.         obj = json.loads(message)
  61.     except:
  62.         print 'could not evaluate input'
  63.  
  64.     xmin= obj[0]["xmin"]
  65.     xmax= obj[0]["xmax"]
  66.     ymin= obj[0]["ymin"]
  67.     ymax= obj[0]["ymax"]
  68.     zmax= obj[0]["zmax"]
  69.  
  70.     diameter = obj[0]['cutterDia']
  71.     length = obj[0]['length']
  72.     triangles = obj[0]['triangles']
  73.  
  74.     Ny = obj[0]['Ny']
  75.  
  76.     dy = obj[0]['dy']
  77.  
  78.     global clearance_height
  79.     clearance_height= obj[0]['clearance_height']
  80.  
  81.     global feed_height
  82.     feed_height = obj[0]['feed_height']
  83.  
  84.     global feed
  85.     feed = obj[0]['feed']
  86.  
  87.     global plunge_feed
  88.     plunge_feed = obj[0]['plunge_feed']
  89.  
  90.     for tri in triangles:
  91.         p = ocl.Point(tri[0][0],tri[0][0],tri[0][2]);
  92.         q = ocl.Point(tri[1][0],tri[1][0],tri[1][2]);
  93.         r = ocl.Point(tri[2][0],tri[2][0],tri[2][2])
  94.         t = ocl.Triangle(p,q,r)
  95.         s.addTriangle(t)
  96.  
  97.     cutter = ocl.CylCutter(diameter, length)
  98. # create a simple "Zig" pattern where we cut only in one direction.
  99.     paths = []
  100. # create a list of paths
  101.     for n in xrange(0,Ny):
  102.         path = ocl.Path()
  103.         y = ymin+n*dy           # current y-coordinate
  104.         p1 = ocl.Point(xmin,y,0)   # start-point of line
  105.         p2 = ocl.Point(xmax,y,0)   # end-point of line
  106.         l = ocl.Line(p1,p2)     # line-object
  107.         path.append( l )        # add the line to the path
  108.         paths.append(path)
  109.  
  110.     cl_paths=[]
  111.  
  112. ## we now have a list of paths to run through apdc
  113.     n_aclp=0
  114.     for p in paths:
  115.         aclp = adaptive_path_drop_cutter(s,cutter,p) # the output is a list of Cutter-Locations
  116.         n_aclp = n_aclp + len(aclp)
  117.         cl_paths.append(aclp)
  118.  
  119.     tol = 0.001
  120.     cl_filtered_paths = []
  121.     n_filtered=0
  122.     for cl_path in cl_paths:
  123.         cl_filtered = filter_path(cl_path,tol)
  124.         n_filtered = n_filtered + len(cl_filtered)
  125.         cl_filtered_paths.append(cl_filtered)
  126.  
  127. #    print message
  128.     return (printCLPoints(cl_filtered_paths))
  129. #    print (printCLPoints(cl_filtered_paths))
  130.  
  131. class WSHandler(tornado.websocket.WebSocketHandler):
  132.     def open(self):
  133.         print 'new connection'
  134.         #self.write_message("Hello World")
  135.  
  136.     def on_message(self, message):
  137.         result = oclstuff(message)
  138.         self.write_message(str(result))
  139.  
  140.  
  141.     def on_close(self):
  142.       print 'connection closed'
  143.  
  144.  
  145. application = tornado.web.Application([
  146.     (r'/ws', WSHandler),
  147. ])
  148.  
  149.  
  150. if __name__ == "__main__":
  151.     http_server = tornado.httpserver.HTTPServer(application)
  152.     http_server.listen(8888)
  153.     tornado.ioloop.IOLoop.instance().start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement