Advertisement
danfalck

PathKurveUtils.py

May 4th, 2015
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.74 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. #***************************************************************************
  4. #*                                                                         *
  5. #*   Copyright (c) 2015 Dan Falck <ddfalck@gmail.com>                      *
  6. #*                                                                         *
  7. #*   This program is free software; you can redistribute it and/or modify  *
  8. #*   it under the terms of the GNU Lesser General Public License (LGPL)    *
  9. #*   as published by the Free Software Foundation; either version 2 of     *
  10. #*   the License, or (at your option) any later version.                   *
  11. #*   for detail see the LICENCE text file.                                 *
  12. #*                                                                         *
  13. #*   This program is distributed in the hope that it will be useful,       *
  14. #*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
  15. #*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
  16. #*   GNU Library General Public License for more details.                  *
  17. #*                                                                         *
  18. #*   You should have received a copy of the GNU Library General Public     *
  19. #*   License along with this program; if not, write to the Free Software   *
  20. #*   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
  21. #*   USA                                                                   *
  22. #*                                                                         *
  23. #***************************************************************************
  24. '''PathKurveUtils - functions needed for using libarea (created by Dan Heeks) for making simple CNC profile paths '''
  25. import FreeCAD
  26. import FreeCADGui as Gui
  27. import Part
  28. import DraftGeomUtils
  29. import math
  30. import area
  31. import Path
  32. from PathScripts import PathUtils
  33.  
  34. def makeAreaVertex(seg):
  35.     if seg.ShapeType =='Edge':
  36.         if isinstance(seg.Curve,Part.Circle):
  37.             segtype = 1 #0=line,1=ccw arc,-1=cw arc
  38.             vertex = area.Vertex(segtype, area.Point(seg.valueAt(seg.LastParameter)[0],seg.valueAt(seg.LastParameter)[1]), area.Point(seg.Curve.Center.x, seg.Curve.Center.y))
  39.         elif isinstance(seg.Curve,Part.Line):
  40.             point1 = seg.valueAt(seg.FirstParameter)[0],seg.valueAt(seg.FirstParameter)[1]
  41.             point2 = seg.valueAt(seg.LastParameter)[0],seg.valueAt(seg.LastParameter)[1]
  42.             segtype = 0
  43.             vertex = area.Point(seg.valueAt(seg.LastParameter)[0],seg.valueAt(seg.LastParameter)[1])
  44.         else:
  45.             pass
  46.     return vertex
  47.  
  48. def makeAreaCurve(curveobj,edges,points=None):
  49.     wire = Part.Wire(edges)
  50.     seglist = DraftGeomUtils.sortEdges(edges)
  51.    
  52.     firstedge = seglist[0]
  53.     if wire.isClosed()==False:
  54.         if points:
  55.             if len(points)>=1:
  56.                 startptX = points[0].X
  57.                 startptY = points[0].Y
  58.         else:
  59.             startptX = firstedge.valueAt(firstedge.FirstParameter)[0]
  60.             startptY = firstedge.valueAt(firstedge.FirstParameter)[1]
  61.         curveobj.append(area.Point(startptX,startptY))
  62.  
  63.     if points and (len(points)>1):
  64.         endptX = points[-1].X
  65.         endptY = points[-1].Y
  66.         for s in seglist[:-1]:
  67.             curveobj.append(makeAreaVertex(s))
  68.         curveobj.append(area.Point(endptX,endptY))
  69.     else:
  70.         for s in seglist:
  71.             curveobj.append(makeAreaVertex(s))
  72.     wire = Part.Wire(seglist)
  73.     if wire.isClosed():
  74.         curveobj.append(area.Point(firstedge.valueAt(firstedge.LastParameter)[0],firstedge.valueAt(firstedge.LastParameter)[1]))
  75.  
  76. # profile command,
  77. # direction should be 'left' or 'right' or 'on'
  78. def profile(curve, direction = "on", radius = 1.0, offset_extra = 0.0,rapid_safety_space = None, clearance = None, start_depth = None, stepdown = None, final_depth = None,use_CRC=False):
  79.  
  80.     output = ""
  81.     original_curve = area.Curve(curve)
  82.     if original_curve.getNumVertices() <= 1:
  83.         raise Exception,"Sketch has no elements!"
  84.     if direction == "on":
  85.         use_CRC =False
  86.         offset_curve = original_curve
  87.     if direction != "on":
  88.         if direction != "left" and direction != "right":
  89.             raise Exception,"direction must be 'on', 'left', or 'right'"
  90.         # get tool radius
  91.         offset = radius + offset_extra
  92.         original_curve.Offset(offset)
  93.         offset_curve = original_curve
  94.         if use_CRC == False:
  95.             if direction == "right":
  96.                 offset = -offset
  97.             offset_success = original_curve.Offset(offset)
  98.             if offset_success == False:
  99.                 raise Exception, "couldn't offset kurve " + str(original_curve)
  100.     # do multiple depths
  101.     layer_count = int((start_depth - final_depth) / stepdown)
  102.     if layer_count * stepdown + 0.00001 < start_depth - final_depth:
  103.         layer_count += 1
  104.     current_start_depth = start_depth
  105.     prev_depth = start_depth
  106.     for i in range(1, layer_count+1):
  107.         if i == layer_count:
  108.             depth = final_depth
  109.         else:
  110.             depth = start_depth - i * stepdown
  111.         mat_depth = prev_depth
  112.         start_z = mat_depth
  113.         #first move
  114.         output += "G0 X"+str(PathUtils.fmt(offset_curve.GetFirstSpan().p.x))+" Y"+str(PathUtils.fmt(offset_curve.GetFirstSpan().p.y))+" Z"+str(PathUtils.fmt(mat_depth + rapid_safety_space))+"\n"
  115.         # feed down to depth
  116.         mat_depth = depth
  117.         if start_z > mat_depth:
  118.             mat_depth = start_z
  119.         # feed down in Z
  120.         output += "G1 X"+str(PathUtils.fmt(offset_curve.GetFirstSpan().p.x))+" Y"+str(PathUtils.fmt(offset_curve.GetFirstSpan().p.y))+" Z"+str(PathUtils.fmt(depth))+"\n"
  121.         if use_CRC:
  122.             if direction == 'left':
  123.                 output +="G41"+"\n"
  124.             else:
  125.                 output +="G42"+"\n"
  126.         # cut the main kurve
  127.         current_perim = 0.0
  128.         lastx=offset_curve.GetFirstSpan().p.x
  129.         lasty=offset_curve.GetFirstSpan().p.y
  130.         for span in offset_curve.GetSpans():
  131.             current_perim += span.Length()
  132.             if span.v.type == 0:#line
  133.                 #feed(span.v.p.x, span.v.p.y, ez)
  134.                 output +="G1 X"+ str(PathUtils.fmt(span.v.p.x)) +" Y"+ str(PathUtils.fmt(span.v.p.y))+" Z"+ str(PathUtils.fmt(depth))+"\n"
  135.                 lastx = span.v.p.x
  136.                 lasty = span.v.p.y
  137.             elif (span.v.type == 1) or (span.v.type == -1):
  138.                 if span.v.type == 1:# anti-clockwise arc
  139.                     command = 'G3'
  140.                 elif span.v.type == -1:#clockwise arc
  141.                     command = 'G2'
  142.                 arc_I= span.v.c.x-lastx
  143.                 arc_J= span.v.c.y-lasty
  144.                 output +=command +"X"+str(PathUtils.fmt(span.v.p.x))+" Y"+ str(PathUtils.fmt(span.v.p.y))#+" Z"+ str(PathUtils.fmt(depth))
  145.                 output +=" I"+str(PathUtils.fmt(arc_I))+ " J"+str(PathUtils.fmt(arc_J))+'\n'#" K"+str(PathUtils.fmt(depth)) +"\n"
  146.                 lastx = span.v.p.x
  147.                 lasty = span.v.p.y
  148.             else:
  149.                 raise Exception, "valid geometry identifier needed"
  150.         if use_CRC:
  151.             #end_CRC()
  152.             output +="G40"+"\n"
  153.         # rapid up to the clearance height
  154.         output +="G0 Z"+str(PathUtils.fmt(clearance))+"\n"
  155.  
  156.     del offset_curve
  157.     del original_curve
  158.     return output
  159.  
  160.  
  161. '''
  162. clearance = float(5)
  163. rapid_safety_space = float(2)
  164. start_depth = float(0)
  165. step_down = float(1)
  166. final_depth = float(-4)
  167. tool_diameter = float(3.625)
  168. cutting_edge_angle = float(0)
  169. roll_radius = float(2)
  170. offset_extra = 0
  171. roll_on = 'auto'
  172. roll_off = 'auto'
  173. #roll_on = None
  174. roll_off = None
  175. extend_at_start= 0
  176. extend_at_end= 0
  177. lead_in_line_len= 0
  178. lead_out_line_len= 0
  179. extend_at_start=1.0
  180. extend_at_end=1.0
  181.  
  182.  
  183. #edges = Gui.Selection.getSelectionEx()[0].SubObjects
  184.  
  185. sel = PathSelection.multiSelect()
  186. edges = sel['edgelist']
  187. c2= area.Curve()
  188. if  sel['pointlist']:
  189.    PathKurveUtils.makeAreaCurve(c2,edges, sel['pointlist'])
  190. else:
  191.    PathKurveUtils.makeAreaCurve(c2,edges)
  192.  
  193. c2.Reverse()
  194. import Path
  195. g2 = PathKurveUtils.profile(c2, 'left', tool_diameter/2.0, 0,rapid_safety_space, clearance, start_depth, step_down, final_depth,True )
  196. obj = App.ActiveDocument.addObject("Path::Feature","mypath2")
  197. obj.Path = Path.Path(g2)
  198.  
  199. g3 = PathKurveUtils.profile(c2, 'right', tool_diameter/2.0, 0,rapid_safety_space, clearance, start_depth, step_down, final_depth,True )
  200. obj = App.ActiveDocument.addObject("Path::Feature","mypath2")
  201. obj.Path = Path.Path(g3)
  202.  
  203. g4 = PathKurveUtils.profile(c2, 'on', tool_diameter/2.0, 0,rapid_safety_space, clearance, start_depth, step_down, final_depth,True )
  204. obj = App.ActiveDocument.addObject("Path::Feature","mypath2")
  205. obj.Path = Path.Path(g4)
  206. '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement