Advertisement
danfalck

flip_edges.py

Apr 21st, 2013
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.80 KB | None | 0 0
  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.  
  23. '''
  24. Macro to reverse the list of edges in a wire and turn reverse the order of their
  25. Vertexes
  26. '''
  27. from DraftGeomUtils import sortEdges, findMidpoint,findWires, isReallyClosed, isClockwise,findWires
  28. from FreeCAD import Vector,Base,Part
  29. import FreeCADGui
  30.  
  31. def flip_wire():
  32.  
  33.     sel=Gui.Selection.getSelection()
  34.     group= sel[0]
  35.  
  36.     geom = group.Group
  37.     name = group.Name
  38.     LinesArcs = True
  39.     edges=[]
  40.    # points = []
  41.     for s in geom:
  42.         if s.Type =='Part::Vertex':
  43.             LinesArcs = False
  44. #            startpt = App.activeDocument().addObject("Part::Feature", "startpt")
  45. #            startpt.Shape = s
  46. #            group.addObject(startpt)
  47.         #points.append(s)
  48.         else:
  49.             LinesArcs = True
  50.         #objs.append(s.Object)
  51.             edges.append(s.Shape)
  52.             #group.removeObject(s.Shape)
  53.         if LinesArcs:
  54.             App.activeDocument().removeObject(s.Name)
  55.  
  56.     sorted_edges = []
  57.     sorted_edges = sortEdges(edges)
  58.     wire1 = findWires(sorted_edges)
  59.  
  60.     new_list = []
  61.  
  62.     for w in wire1[0].Edges:
  63.         new_list.append(w)
  64.  
  65.     new_list.reverse()
  66.  
  67.     def flip_verts(edgeList):
  68.         edge_list = []
  69.         shape = ''
  70.         new_edge = ''
  71.         for s in edgeList:
  72.             if (isinstance(s.Curve,Part.Line)):
  73.                 FreeCAD.Console.PrintMessage( 'Line\n')
  74.                 v2 = s.Edges[0].Vertexes[0]
  75.                 v1 = s.Edges[0].Vertexes[-1]
  76.                 shape = 'Line'
  77.                 new_edge = Part.makeLine(Vector(v1.X,v1.Y,v1.Z),Vector(v2.X,v2.Y,v2.Z))
  78.                 edge_list.append(new_edge)
  79.             elif (isinstance(s.Curve,Part.Circle)):
  80.                 if s.Closed:
  81.                     shape = 'Circle'
  82.                     center = s.Curve.Center
  83.                     radius = s.Curve.Radius
  84.                     new_edge = Part.makeCircle(radius,center,0,360)
  85.                     edge_list.append(new_edge)
  86.                 else:
  87.                     FreeCAD.Console.PrintMessage( 'Arc\n')
  88.                     shape = 'Arc'
  89.                     v3 = s.Edges[0].Vertexes[0].Point
  90.                     FreeCAD.Console.PrintMessage( str(v3)+'\n')
  91.                     v2 = findMidpoint(s)
  92.                     FreeCAD.Console.PrintMessage( str(v2)+'\n')
  93.                     v1 = s.Edges[0].Vertexes[-1].Point
  94.                     FreeCAD.Console.PrintMessage( str(v1)+'\n')
  95.                     new_arc = Part.Arc(v1,v2,v3)
  96.                     new_edge = new_arc.toShape()
  97.                     #FreeCAD.Console.PrintMessage( str(new_edge.Type)+'\n')
  98.                     edge_list.append(new_edge)
  99.             elif (isinstance(s.Curve,Part.Point)):
  100.                 shape = 'Point'
  101.             else:
  102.                 pass
  103.  
  104.         return edge_list
  105.  
  106.     flip_list = flip_verts(new_list)
  107.  
  108.     secondWire = Part.Wire(flip_list)
  109.    
  110. #    newwire=App.activeDocument().addObject("Part::Feature", "w")
  111. #    newwire.Shape = secondWire
  112. #    group.addObject(newwire)
  113.     #Part.show(secondWire)
  114.     for i in flip_list:
  115.         newobj = App.activeDocument().addObject("Part::Feature", "edge")
  116.         newobj.Shape = i
  117.         group.addObject(newobj)
  118.  
  119.     App.activeDocument().recompute()
  120.  
  121. flip_wire()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement