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 sys
- import ocl
- import math
- import os
- import StringIO
- import subprocess
- import tempfile
- import ngc_writer
- nfile = StringIO.StringIO()
- clearance_height= 4
- feed_height = 0.3
- feed = 200
- plunge_feed = 100
- metric = False
- 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 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()
- #select an object in FreeCAD gui
- sel = Gui.Selection.getSelection()[0]
- #get type of object
- if sel.TypeId.startswith('Mesh'):
- #it is a mesh already
- 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):
- mesh = MeshPart.meshFromShape(sel.Shape,MaxLength=2)
- else:
- print 'we cannot work with this object'
- s = ocl.STLSurf()
- for f in mesh.Facets:
- p = f.Points[0];q=f.Points[1];r=f.Points[2]
- t= ocl.Triangle(ocl.Point(p[0],p[1],p[2]),ocl.Point(q[0],q[1],q[2]),ocl.Point(r[0],r[1],r[2]))
- s.addTriangle(t)
- angle = math.pi/4
- diameter=0.25
- length=5
- # choose a cutter for the operation:
- cutter = ocl.CylCutter(diameter, length)
- ymin=0
- ymax=12
- Ny=40 # number of lines in the y-direction
- dy = float(ymax-ymin)/(Ny-1) # the y step-over
- # 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(0,y,0) # start-point of line
- p2 = ocl.Point(10,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)
- nfile.write(printCLPoints(cl_filtered_paths))
- print nfile.getvalue()
- ngcfile = tempfile.NamedTemporaryFile(suffix=".ngc",delete=False)
- ngcfile.write(nfile.getvalue() )
- ngcfile.close()
- subprocess.call(["gvim", ngcfile.name])
- nfile.close()
Add Comment
Please, Sign In to add comment