Advertisement
danfalck

curves_from_groups.py

Nov 12th, 2014
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.46 KB | None | 0 0
  1. #***************************************************************************
  2. #*                                                                         *
  3. #*   Copyright (c) 2014 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.  
  23. '''
  24. Module to extract useable curve geometry from FreeCAD to CAM scripts such as HeeksCNC
  25. rev2 - added way of extracting edges from faces and wires.
  26. 11/09/14
  27. '''
  28.  
  29. from DraftGeomUtils import sortEdges, findMidpoint,findWires, isReallyClosed, isClockwise
  30. from FreeCAD import Vector,Base,Part
  31. import FreeCADGui
  32. import FreeCAD
  33. import FreeCAD as App
  34. import FreeCADGui as Gui
  35. #from PyQt4 import QtGui,QtCore
  36. from PySide import QtGui,QtCore
  37.  
  38. #cw ccw functions
  39. def segments(poly):
  40.     """A sequence of (x,y) numeric coordinates pairs """
  41.     return zip(poly, poly[1:] + [poly[0]])
  42.  
  43. def check_clockwise(poly):
  44.     clockwise = False
  45.     if (sum(x0*y1 - x1*y0 for ((x0, y0), (x1, y1)) in segments(poly))) < 0:
  46.         clockwise = not clockwise
  47.     return clockwise
  48. #'''
  49. #poly = [(2,2),(6,2),(6,6),(2,6)]
  50. #check_clockwise(poly)
  51. #False
  52. # l1 = []
  53. #for v in wire1.Vertexes:
  54. #    l1.append((v.X,v.Y))
  55. #check_clockwise(l1)
  56. #'''
  57.  
  58. #doc = App.activeDocument()
  59. #grp1 = doc.getObject("outer_profile")
  60. #sel = grp1.OutList
  61.  
  62. def getGeom():
  63.  
  64.     sel=Gui.Selection.getSelection()
  65.     group= sel[0]
  66.  
  67.     geom = group.Group
  68.     name = group.Label
  69.    
  70.     objs = []
  71.     item = "# ***** "+name+" *****\n"
  72.     edges=[]
  73.     points = []
  74.     for s in geom:
  75.         if s.Shape.ShapeType =='Vertex':
  76.             points.append(s)
  77.         elif s.Shape.ShapeType =='Wire':
  78.             edges.extend(s.Shape.Edges)
  79.         elif s.Shape.ShapeType =='Face':
  80.             edges.extend(s.Shape.OuterWire.Edges)
  81.         else:
  82.         #objs.append(s.Object)
  83.             edges.append(s.Shape)
  84.        
  85.     sorted_edges = []
  86.     sorted_edges = sortEdges(edges)
  87.     wire1 = findWires(sorted_edges)
  88.  
  89. #find direction of wire for milling purposes (inside profile, outside profile etc)
  90.  
  91.     w1 = wire1[0]
  92.  
  93.     l1 = []
  94.     for v in w1.Vertexes:
  95.         l1.append((v.X,v.Y))
  96.  
  97.  
  98.     if check_clockwise(l1):
  99.         FreeCAD.Console.PrintMessage('clockwise path')
  100.     else:
  101.         FreeCAD.Console.PrintMessage('counter clockwise path')
  102.  
  103.  
  104. #    def isSameVertex(V1, V2):#borrowed from yorik's fcgeo.py- thanks yorik!
  105. #        ''' Test if vertexes have same coordinates with precision 10E(-precision)'''
  106. #        if round(V1.X-V2.X,1)==0 and round(V1.Y-V2.Y,1)==0 and round(V1.Z-V2.Z,1)==0 :
  107. #            return True
  108. #        else :
  109. #            return False
  110.  
  111.     start=sorted_edges[0]
  112.     end=sorted_edges[-1]
  113.     startingZ = start.Vertexes[0].Z
  114.     #set starting depth to same Z as starting curve element
  115.     #self.form.lineEditStartDepth.setText(str(start.Vertexes[0].Z))
  116.     item += name+" = area.Curve()\n"
  117.    
  118.     if isReallyClosed(wire1[0]):
  119.         item += '#closed path\n'
  120.         path = 'closedpath'
  121.     else:
  122.         item += '#open path\n'
  123.         path = 'openpath'
  124.    
  125. #    if isSameVertex(start.Vertexes[0],end.Vertexes[1]) :
  126. #        item += '#closed path\n'
  127. #        path = 'closedpath'
  128. #    else:
  129. #        item += '#open path\n'
  130. #        path = 'openpath'
  131.  
  132.     if path ==  'openpath' :
  133.         item += name+".append(area.Point(" + str(start.Vertexes[0].X) + "," + str(start.Vertexes[0].Y)+ "))\n"
  134.  
  135.     for s in sorted_edges:
  136.         #edges.append(s)
  137.         if (isinstance(s.Curve,Part.Circle)):
  138.             mp = findMidpoint(s)
  139.             ce = s.Curve.Center
  140. #            tang1 = s.Curve.tangent(s.ParameterRange[0]) ; tang2 = s.Curve.tangent(s.ParameterRange[1])
  141. #            cross1 = Vector.cross(Base.Vector(tang1[0][0],tang1[0][1],tang1[0][2]),Base.Vector(tang2[0][0],tang2[0][1],tang2[0][2]))
  142.             #look at isClockwise in DraftGeomUtils.py
  143. #            if cross1[2] > 0:
  144.             if isClockwise(s):
  145.                 direct = '1 ' #we seem to be working in a rh system in FreeCAD
  146.             else:
  147.                 direct = '-1 '
  148.             item += name+".append(area.Vertex("+str(direct)+ ", area.Point( "+ str(s.Vertexes[-1].Point[0])+", "+str(s.Vertexes[-1].Point[1])+ "), area.Point("+str(s.Curve.Center [0])+ ", " + str(s.Curve.Center[1])+ ")))\n"
  149.  
  150.         elif (isinstance(s.Curve,Part.Line)):
  151.             item += name+".append(area.Point( "+str(s.Vertexes[-1].Point[0])+", " +str(s.Vertexes[-1].Point[1])+ "))\n"
  152.         else:
  153.             pass
  154.  
  155.     if path ==  'closedpath':
  156.         item += name+".append(area.Point(" + str(start.Vertexes[1].X) + "," + str(start.Vertexes[1].Y)+ "))\n"
  157.  
  158.  
  159.  
  160.     item+= name+".Reverse()\n"
  161.     #return item
  162.  
  163.     if points:
  164.         item+= "kurve_funcs.make_smaller( "+ name+ ", start = area.Point(" + str(points[0].Shape.X)+","+str(points[0].Shape.Y)+"))\n"
  165.  
  166.         item+= name+"_startparams = {'key':'value'}\n"
  167.         item+= name+"_startparams['startpt'] = True\n"
  168.         item+= name+"_startparams['startptX'] = "+str(points[0].Shape.X)+"\n"
  169.         item+= name+"_startparams['startptY'] = "+str(points[0].Shape.Y)+"\n"
  170.  
  171.  
  172.     clipboard = QtGui.QApplication.clipboard()
  173.     clipboard.setText(item)
  174.  
  175.  
  176. getGeom()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement