Advertisement
danfalck

client_send_triangles.py

Mar 1st, 2014
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.17 KB | None | 0 0
  1. import FreeCAD
  2. import FreeCAD as App
  3. import FreeCADGui as Gui
  4. import Part
  5. import Mesh
  6. import MeshPart
  7. import ocl
  8. from websocket import create_connection
  9. import json
  10. import tempfile
  11. import subprocess
  12.  
  13. def encodeobj(sel):
  14. #get type of object
  15.     if sel.TypeId.startswith('Mesh'):
  16.         #it is a mesh already
  17.         print 'was already mesh'
  18.         mesh = sel
  19.     elif sel.TypeId.startswith('Part') and \
  20.         (sel.Shape.BoundBox.XLength > 0) and \
  21.         (sel.Shape.BoundBox.YLength > 0) and \
  22.         (sel.Shape.BoundBox.ZLength > 0):
  23.             print 'this is a solid Part object'
  24.             mesh = MeshPart.meshFromShape(sel.Shape,MaxLength=10)
  25.     else:
  26.         print 'we cannot work with this object'
  27.  
  28.     verts = []
  29.     for f in mesh.Facets:
  30.         p = f.Points[0];q=f.Points[1];r=f.Points[2]
  31.         verts.append((p,q,r))
  32.     xmin=sel.Shape.Boundbox.XMin
  33.     xmax=sel.Shape.BoundBox.XMax
  34.     ymin=sel.Shape.BoundBox.YMin
  35.     ymax=sel.Shape.BoundBox.YMax
  36.     zmax=sel.Shape.BoundBox.ZMax
  37.     oclobj={}
  38.     oclobj["xmin"]=xmin
  39.     oclobj["xmax"]=xmax
  40.     oclobj["ymin"]=ymin
  41.     oclobj["ymax"]=ymax
  42.     oclobj["zmax"]=zmax
  43.     cutterDia = 2.0
  44.     oclobj['cutterDia'] =cutterDia
  45.     Ny=int(sel.Shape.BoundBox.YLength/cutterDia)  # number of lines in the y-direction
  46.     oclobj['Ny']=Ny
  47.     dy = float(ymax-ymin)/Ny
  48.     oclobj['dy']=dy
  49.     clearance_height= zmax + 4
  50.     oclobj['clearance_height']=clearance_height
  51.     feed_height = 0.3
  52.     oclobj['feed_height'] = feed_height
  53.     feed = 200
  54.     oclobj['feed'] = feed
  55.     plunge_feed = 100
  56.     oclobj['plunge_feed'] = plunge_feed
  57.     length=11.0
  58.     oclobj['length'] = length
  59.     oclobj['triangles'] = verts
  60.     objlist = [oclobj]
  61.     data = json.dumps(objlist)
  62.     return data
  63.  
  64. def procsurf(data):
  65.     ws = create_connection("ws://localhost:8888/ws")
  66.     ws.send(data)
  67.     code = ws.recv_data()
  68.     gcode =  code[1]
  69.     ngcfile = tempfile.NamedTemporaryFile(suffix=".ngc",delete=False)
  70.     ngcfile.write(gcode)
  71.     ngcfile.close()
  72.     subprocess.call(["gvim", ngcfile.name])
  73.  
  74. #select an object in FreeCAD gui
  75. sel = Gui.Selection.getSelection()[0]
  76. data = encodeobj(sel)
  77. procsurf(data)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement