danfalck

stl2parallel_finish_zig_freecad2.py

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