SHARE
TWEET

FreeCADholefinder.py

danfalck Nov 30th, 2013 274 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #***************************************************************************
  2. #*                                                                         *
  3. #*   Copyright (c) 2013 Daniel Falck  <ddfalck@gmail.com>                  *  
  4. #*                                                                         *
  5. #*   This program is free software; you can redistribute it and/or modify  *
  6. #*   it under the terms of the GNU Lesser General Public License (LGPL)    *
  7. #*   as published by the Free Software Foundation; either version 2 of     *
  8. #*   the License, or (at your option) any later version.                   *
  9. #*   for detail see the LICENCE text file.                                 *
  10. #*                                                                         *
  11. #*   This program is distributed in the hope that it will be useful,       *
  12. #*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
  13. #*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
  14. #*   GNU Library General Public License for more details.                  *
  15. #*                                                                         *
  16. #*   You should have received a copy of the GNU Library General Public     *
  17. #*   License along with this program; if not, write to the Free Software   *
  18. #*   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
  19. #*   USA                                                                   *
  20. #*                                                                         *
  21. #***************************************************************************
  22. from FreeCAD import Part,Base
  23. from PyQt4 import QtGui,QtCore
  24. from math import pi
  25. '''
  26. This macro makes a list of holes for drilling  from a solid
  27. 1. Select a solid object that has holes in it and run the macro
  28. 2. It only collects info on holes that are parallel to the Z axis- I don't have a 4 or 5 axis mill at the moment
  29. 3. It pulls the center of the hole bounding box and the XLength for it's diameter
  30. 4. It will place a list of the holes on the clipboard
  31. 5. Uncomment the line that starts with '#Draft.makeLine' and manipulate it, if you want to see lines down the center of each hole.
  32. 6. Manipulate the line that starts with  'holelist.append' to make the list fit your own needs- I've put the ZMax at the ZMax of the solid's bounding box
  33. because I want to make sure that my drill tip doesn't collide with anything on the top of the part. YMMV.
  34. '''
  35. def findholes(obj):
  36.     facelist = []
  37.     holelist =[]
  38.     vz = Base.Vector(0,0,1)
  39.     for f in obj.Faces:
  40.         if ( round(f.ParameterRange[0], 8)==0.0 ) and ( round(f.ParameterRange[1],8) == round(pi*2, 8)  ) : #eliminate flat faces
  41.             facelist.append(f)
  42.  
  43.     for h in facelist:
  44.         for w in h.Wires:
  45.             for c in w.Edges:
  46.                 if ( isinstance(c.Curve,Part.Line)):
  47.                     v0=Base.Vector(c.Vertexes[0].X, c.Vertexes[0].Y, c.Vertexes[0].Z); v1=Base.Vector(c.Vertexes[1].X,c.Vertexes[1].Y, c.Vertexes[1].Z)
  48.                     if (v1.sub(v0).x == 0) and (v1.sub(v0).y == 0):
  49.                         lsp = Base.Vector(h.BoundBox.Center.x,h.BoundBox.Center.y,h.BoundBox.ZMax)
  50.                         lep = Base.Vector(h.BoundBox.Center.x,h.BoundBox.Center.y,h.BoundBox.ZMin)
  51.                         if obj.isInside(lsp, 0,False) or obj.isInside(lep, 0,False):
  52.                             pass
  53.                         else:
  54.                             Draft.makeLine((h.BoundBox.Center.x,h.BoundBox.Center.y,obj.BoundBox.ZMax ),(h.BoundBox.Center.x,h.BoundBox.Center.y,h.BoundBox.ZMin  ))
  55.                             x =h.BoundBox.Center.x;y=h.BoundBox.Center.y;zmax=obj.BoundBox.ZMax;zmin=h.BoundBox.ZMin;diameter=h.BoundBox.XLength
  56.                             holelist.append((diameter, x,y,zmax,zmin))
  57.     clipboard = QtGui.QApplication.clipboard()
  58.     clipboard.setText(str(holelist))
  59.  
  60. sel=Gui.Selection.getSelection()
  61. obj = sel[0].Shape
  62. findholes(obj)
RAW Paste Data
Want to get better at Python?
Learn to code Python in 2017
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top