danfalck

pathdropcutter_test1_freecad.py

Feb 23rd, 2014
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.64 KB | None | 0 0
  1. import time
  2. import FreeCAD
  3. import FreeCAD as App
  4. import FreeCADGui as Gui
  5. import Part
  6. import Mesh
  7. import MeshPart
  8. import ocl
  9.  
  10. #select an object in FreeCAD gui
  11.  
  12. sel = Gui.Selection.getSelection()[0]
  13.  
  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=2)
  25.  
  26. else:
  27.     print 'we cannot work with this object'
  28.  
  29. s= ocl.STLSurf()
  30.  
  31. for f in mesh.Facets:
  32.     p = f.Points[0];q=f.Points[1];r=f.Points[2]
  33.     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]))
  34.     s.addTriangle(t)
  35.  
  36. # define a cutter
  37. cutter = ocl.CylCutter(0.6, 5)
  38.  
  39. print "creating PathDropCutter()"
  40. pdc = ocl.PathDropCutter()   # create a pdc
  41. print "set STL surface"
  42. pdc.setSTL(s)
  43. print "set cutter"
  44. pdc.setCutter(cutter)               # set the cutter
  45. print "set minimumZ"
  46. pdc.minimumZ = -1                   # set the minimum Z-coordinate, or "floor" for drop-cutter
  47. print "set the sampling interval"
  48. pdc.setSampling(0.0123)
  49.  
  50. # some parameters for this "zigzig" pattern    
  51. xmin=sel.Shape.BoundBox.XMin - cutter.getDiameter()
  52. xmax=sel.Shape.BoundBox.XMax + cutter.getDiameter()
  53. ymin=sel.Shape.BoundBox.YMin - cutter.getDiameter()
  54. ymax=sel.Shape.BoundBox.YMax + cutter.getDiameter()
  55. zmax=sel.Shape.BoundBox.ZMax + cutter.getDiameter()
  56.  
  57. Ny=int(sel.Shape.BoundBox.YLength/cutter.getDiameter())  # number of lines in the y-direction
  58. dy = float(ymax-ymin)/Ny  # the y step-over
  59.  
  60. path = ocl.Path()                   # create an empty path object
  61. # add Line objects to the path in this loop
  62. for n in xrange(0,Ny):
  63.     y = ymin+n*dy
  64.     p1 = ocl.Point(xmin,y,0)   # start-point of line
  65.     p2 = ocl.Point(xmax,y,0)   # end-point of line
  66.     l = ocl.Line(p1,p2)     # line-object
  67.     path.append( l )        # add the line to the path
  68.  
  69.  
  70. print " set the path for pdf "
  71. pdc.setPath( path )
  72.  
  73. print " run the calculation "
  74. t_before = time.time()
  75. pdc.run()                   # run drop-cutter on the path
  76. t_after = time.time()
  77. print "run took ", t_after-t_before," s"
  78.  
  79. print "get the results "
  80. clp = pdc.getCLPoints()     # get
  81.  
  82. edge = Part.Edge
  83. stpt = (clp[0].x, clp[0].y, clp[0].z)
  84. edgelist= []
  85. for c in clp[1:]:
  86.     endpt = (c.x, c.y, c.z)
  87.     edge = Part.makeLine(stpt,endpt)
  88.     edgelist.append(edge)
  89.     stpt = (c.x, c.y, c.z)
  90.  
  91. comp = Part.Compound(edgelist)
  92. Part.show(comp)
Add Comment
Please, Sign In to add comment