danfalck

stl2parallel_finish_zig_freecad.py

Feb 23rd, 2014
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.59 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 sys
  8. import ocl
  9. import math
  10. import os
  11. import StringIO
  12. import subprocess
  13. import tempfile
  14. import ngc_writer
  15.  
  16. nfile = StringIO.StringIO()
  17.  
  18. clearance_height= 4
  19. feed_height = 0.3
  20. feed = 200
  21. plunge_feed = 100
  22. metric = False
  23.  
  24. def line_to(x,y,z):
  25.     return "G1 X% 8.6f Y% 8.6f Z% 8.6f F%.0f\n" % (x, y, z, feed)
  26.  
  27. def xy_line_to(x,y):
  28.     return "G1 X% 8.4f Y% 8.4f\n" % (x, y)
  29.  
  30. def xy_rapid_to(x,y):
  31.     return "G0 X% 8.4f Y% 8.4f\n " % (x, y)
  32.  
  33. def pen_up():
  34.     return "G0Z% 8.4f\n" % (clearance_height)
  35.  
  36. def pen_down(z=0):
  37.     return "G0Z% 8.4f\n" % (feed_height)
  38.     plunge(z)
  39.  
  40. def plunge(z):
  41.     return "G1 Z% 8.4f F% 8.0f\n" % (z, plunge_feed)
  42.  
  43. def printCLPoints(cl_filtered_paths):
  44.     gcode = ""
  45.     for path in cl_filtered_paths:
  46.         gcode += (pen_up())
  47.         first_pt = path[0]
  48.         gcode+= (xy_rapid_to( first_pt.x, first_pt.y ))
  49.         gcode+= (pen_down( first_pt.z ))
  50.         for p in path[1:]:
  51.             gcode+= (line_to(p.x,p.y,p.z))
  52.     return gcode
  53.  
  54. def filter_path(path,tol):
  55.     f = ocl.LineCLFilter()
  56.     f.setTolerance(tol)
  57.     for p in path:
  58.         p2 = ocl.CLPoint(p.x,p.y,p.z)
  59.         f.addCLPoint(p2)
  60.     f.run()
  61.     return  f.getCLPoints()
  62.  
  63. def adaptive_path_drop_cutter(s, cutter, path):
  64.     apdc = ocl.AdaptivePathDropCutter()
  65.     apdc.setSTL(s)
  66.     apdc.setCutter(cutter)
  67.     apdc.setSampling(0.04)
  68.     apdc.setMinSampling(0.0008)
  69.     apdc.setPath( path )
  70.     apdc.run()
  71.     return apdc.getCLPoints()
  72.  
  73. #select an object in FreeCAD gui
  74. sel = Gui.Selection.getSelection()[0]
  75. #get type of object
  76. if sel.TypeId.startswith('Mesh'):
  77.     #it is a mesh already
  78.     mesh = sel
  79. elif sel.TypeId.startswith('Part') and \
  80.     (sel.Shape.BoundBox.XLength > 0) and \
  81.     (sel.Shape.BoundBox.YLength > 0) and \
  82.     (sel.Shape.BoundBox.ZLength > 0):
  83.         mesh = MeshPart.meshFromShape(sel.Shape,MaxLength=2)
  84. else:
  85.     print 'we cannot work with this object'
  86.  
  87. s = ocl.STLSurf()
  88.  
  89. for f in mesh.Facets:
  90.     p = f.Points[0];q=f.Points[1];r=f.Points[2]
  91.     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]))
  92.     s.addTriangle(t)
  93.  
  94. angle = math.pi/4
  95. diameter=0.25
  96. length=5
  97. # choose a cutter for the operation:
  98. cutter = ocl.CylCutter(diameter, length)
  99.  
  100. ymin=0
  101. ymax=12
  102. Ny=40  # number of lines in the y-direction
  103. dy = float(ymax-ymin)/(Ny-1)  # the y step-over
  104.  
  105. # create a simple "Zig" pattern where we cut only in one direction.
  106. paths = []
  107. # create a list of paths
  108. for n in xrange(0,Ny):
  109.     path = ocl.Path()
  110.     y = ymin+n*dy           # current y-coordinate
  111.     p1 = ocl.Point(0,y,0)   # start-point of line
  112.     p2 = ocl.Point(10,y,0)   # end-point of line
  113.     l = ocl.Line(p1,p2)     # line-object
  114.     path.append( l )        # add the line to the path
  115.     paths.append(path)
  116.  
  117. cl_paths=[]
  118.  
  119. # we now have a list of paths to run through apdc
  120. n_aclp=0
  121. for p in paths:
  122.     aclp = adaptive_path_drop_cutter(s,cutter,p) # the output is a list of Cutter-Locations
  123.     n_aclp = n_aclp + len(aclp)
  124.     cl_paths.append(aclp)
  125.  
  126. tol = 0.001
  127. cl_filtered_paths = []
  128. n_filtered=0
  129. for cl_path in cl_paths:
  130.     cl_filtered = filter_path(cl_path,tol)
  131.     n_filtered = n_filtered + len(cl_filtered)
  132.     cl_filtered_paths.append(cl_filtered)
  133.  
  134. nfile.write(printCLPoints(cl_filtered_paths))
  135. print nfile.getvalue()
  136.  
  137. ngcfile = tempfile.NamedTemporaryFile(suffix=".ngc",delete=False)
  138. ngcfile.write(nfile.getvalue() )
  139. ngcfile.close()
  140.  
  141. subprocess.call(["gvim", ngcfile.name])
  142.  
  143. nfile.close()
Add Comment
Please, Sign In to add comment