Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import FreeCAD
- import FreeCAD as App
- import FreeCADGui as Gui
- import Part
- import Mesh
- import MeshPart
- import ocl
- from websocket import create_connection
- import json
- import tempfile
- import subprocess
- def encodeobj(sel):
- #get type of object
- if sel.TypeId.startswith('Mesh'):
- #it is a mesh already
- print 'was already mesh'
- mesh = sel
- elif sel.TypeId.startswith('Part') and \
- (sel.Shape.BoundBox.XLength > 0) and \
- (sel.Shape.BoundBox.YLength > 0) and \
- (sel.Shape.BoundBox.ZLength > 0):
- print 'this is a solid Part object'
- mesh = MeshPart.meshFromShape(sel.Shape,MaxLength=10)
- else:
- print 'we cannot work with this object'
- verts = []
- for f in mesh.Facets:
- p = f.Points[0];q=f.Points[1];r=f.Points[2]
- verts.append((p,q,r))
- xmin=sel.Shape.Boundbox.XMin
- xmax=sel.Shape.BoundBox.XMax
- ymin=sel.Shape.BoundBox.YMin
- ymax=sel.Shape.BoundBox.YMax
- zmax=sel.Shape.BoundBox.ZMax
- oclobj={}
- oclobj["xmin"]=xmin
- oclobj["xmax"]=xmax
- oclobj["ymin"]=ymin
- oclobj["ymax"]=ymax
- oclobj["zmax"]=zmax
- cutterDia = 2.0
- oclobj['cutterDia'] =cutterDia
- Ny=int(sel.Shape.BoundBox.YLength/cutterDia) # number of lines in the y-direction
- oclobj['Ny']=Ny
- dy = float(ymax-ymin)/Ny
- oclobj['dy']=dy
- clearance_height= zmax + 4
- oclobj['clearance_height']=clearance_height
- feed_height = 0.3
- oclobj['feed_height'] = feed_height
- feed = 200
- oclobj['feed'] = feed
- plunge_feed = 100
- oclobj['plunge_feed'] = plunge_feed
- length=11.0
- oclobj['length'] = length
- oclobj['triangles'] = verts
- objlist = [oclobj]
- data = json.dumps(objlist)
- return data
- def procsurf(data):
- ws = create_connection("ws://localhost:8888/ws")
- ws.send(data)
- code = ws.recv_data()
- gcode = code[1]
- ngcfile = tempfile.NamedTemporaryFile(suffix=".ngc",delete=False)
- ngcfile.write(gcode)
- ngcfile.close()
- subprocess.call(["gvim", ngcfile.name])
- #select an object in FreeCAD gui
- sel = Gui.Selection.getSelection()[0]
- data = encodeobj(sel)
- procsurf(data)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement