danfalck

face_edge_selection_freecad.py

Sep 9th, 2012
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.13 KB | None | 0 0
  1. from PyQt4 import QtCore, QtGui
  2. import DraftGeomUtils
  3. import Draft
  4. from FreeCAD import Vector,Base
  5.  
  6. def makeCurves(name,edges,reverse=False):
  7.     curvelist = ""
  8.  
  9.     sorted_edges = DraftGeomUtils.sortEdges(edges)
  10.  
  11.     #curvelist += '#another test after sorted_edges \n'
  12.     def isSameVertex(V1, V2):#borrowed from yorik's DraftGeomUtils.py- thanks yorik!
  13.         ''' Test if vertexes have same coordinates with precision 10E(-precision)'''
  14.         if round(V1.X-V2.X,1)==0 and round(V1.Y-V2.Y,1)==0 and round(V1.Z-V2.Z,1)==0 :
  15.             return True
  16.         else :
  17.             return False
  18.  
  19.     start=sorted_edges[0]
  20.     end=sorted_edges[-1]
  21.     startingZ = start.Vertexes[0].Z
  22.     #set starting depth to same Z as starting curve element
  23.     #form.lineEditStartDepth.setText(str(start.Vertexes[0].Z))
  24.     curvelist += str(name) +" = area.Curve()\n"
  25.     if isSameVertex(start.Vertexes[0],end.Vertexes[1]) :
  26.         curvelist += '#closed path\n'
  27.         path = 'closedpath'
  28.     else:
  29.         curvelist += '#open path\n'
  30.         path = 'openpath'
  31.  
  32.     if path ==  'openpath' :
  33.         curvelist += str(name) +".append(area.Point(" + str(start.Vertexes[0].X) + "," + str(start.Vertexes[0].Y)+ "))\n"
  34.  
  35.     for s in sorted_edges:
  36.         #edges.append(s)
  37.         if (isinstance(s.Curve,Part.Circle)):
  38.             mp = DraftGeomUtils.findMidpoint(s)
  39.             ce = s.Curve.Center
  40.             tang1 = s.Curve.tangent(s.ParameterRange[0]) ; tang2 = s.Curve.tangent(s.ParameterRange[1])
  41.             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]))
  42.             if cross1[2] > 0:
  43.                 direct = '1 ' #we seem to be working in a rh system in FreeCAD
  44.             else:
  45.                 direct = '-1 '
  46.             curvelist += str(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"
  47.  
  48.         elif (isinstance(s.Curve,Part.Line)):
  49.             curvelist += str(name) +".append(area.Point( "+str(s.Vertexes[-1].Point[0])+", " +str(s.Vertexes[-1].Point[1])+ "))\n"
  50.         else:
  51.             pass
  52.  
  53.     #export curve elements to heekscnc
  54.     #to reverse the curve:
  55.  
  56.     #curvelist += "curve.append(area.Point(" + str(end.Vertexes[0].X) + "," + str(end.Vertexes[0].Y) + "))"
  57.     if path ==  'closedpath':
  58.         curvelist += str(name) +".append(area.Point(" + str(start.Vertexes[1].X) + "," + str(start.Vertexes[1].Y)+ "))\n"
  59.         if reverse:
  60.             curvelist += str(name) +".Reverse()\n"
  61.     #form.textEditCurve.append(curvelist)
  62.     #clipboard = QtGui.QApplication.clipboard()
  63.     #clipboard.setText(curvelist)
  64.     return curvelist
  65.  
  66. def selObj():
  67.     faces = []
  68.     objs = []
  69.     sel = FreeCADGui.Selection.getSelectionEx()
  70.     for s in sel:
  71.         objs.append(s.Object)
  72.  
  73.     for e in s.SubElementNames:
  74.         if "Face" in e:
  75.             faces.append(int(e[4:])-1)
  76.  
  77.     d1 = Draft.makeShape2DView(objs[0],facenumbers=faces)
  78.     d1.ProjectionMode = "Individual Faces"
  79.     zlevel = s.SubObjects[0].BoundBox.ZMax
  80.     d1.Placement.move(Vector(0,0,zlevel))
  81.     d1.Label = "Profile"
  82.     App.activeDocument().recompute()
  83.     name = d1.Base.Name + str(d1.FaceNumbers[0])
  84.     faceEdges = d1.Shape.Edges
  85.     wires = DraftGeomUtils.findWiresOld(faceEdges)
  86.     return name, wires
  87.  
  88. def findZ(wire):
  89.     Zmax = wire[0].BoundBox.ZMax
  90.     Zmin = wire[0].BoundBox.ZMin
  91.     return Zmin, Zmax
  92.  
  93. def makeProfiles(name,wires):
  94.     curvelist = ""
  95.     if len(wires)>1:
  96.         #find outside wire
  97.         outside = wires[0]
  98.         biggest = wires[0].BoundBox.DiagonalLength
  99.  
  100.         for w in wires:
  101.             if w.BoundBox.DiagonalLength >= biggest:
  102.                 biggest = w.BoundBox.DiagonalLength
  103.                 outside = w
  104.  
  105.     #        else:
  106.     #            #return
  107.     #            print 'not biggest'
  108.         wires.remove(outside)
  109.  
  110.         inside = wires
  111.         clList = []
  112.         index = 0
  113.  
  114.         #print 'outside edge'
  115.         #print (makeCurves((name+str(index)), outside.Edges))
  116.         curvelist += str(makeCurves((name+str(index)), outside.Edges))
  117.         index +=1
  118.  
  119.         for w in inside:
  120.             #print '#pocket'
  121.             curvelist +="#inside edge\n"
  122.             #print (makeCurves((name+str(index)), w.Edges,False))
  123.             curvelist += str(makeCurves((name+str(index)), w.Edges,False))
  124.             index +=1
  125.     else:
  126.         #print (makeCurves((name+str(0)), wires[0].Edges))
  127.         curvelist +=str(makeCurves((name+str(0)), wires[0].Edges))
  128.     zmin, zmax = findZ(wires)
  129.     #curvelist += "clearance = float("  + str(zmax+1.0) + ")\n"
  130.     #curvelist += "start_depth = float("+ str(zmax)     + ")\n"
  131.     #curvelist += "final_depth = float("+ str(zmin)     + ")\n"
  132.     return curvelist
  133.  
  134. #if you change the Base sketch geometry, select the Profile and run it again:
  135. #a=FreeCAD.ActiveDocument.getObjectsByLabel(d1.Base.Name)[0]
  136.  
  137.  
  138.  
  139. name, wires = selObj()
  140. curvelist=makeProfiles(name,wires)
  141. clipboard = QtGui.QApplication.clipboard()
  142. clipboard.setText(curvelist)
Add Comment
Please, Sign In to add comment