import PathScripts.PathOp as PathOp import Path import FreeCAD import FreeCADGui from FreeCAD import Console import time import json import math import area from pivy import coin __doc__ = "Class and implementation of the Adaptive path operation." def discretize(edge, flipDirection=False): pts=edge.discretize(Deflection=0.01) if flipDirection: pts.reverse() return pts def convertTo2d(pathArray): output = [] for path in pathArray: pth2 = [] for edge in path: for pt in edge: pth2.append([pt[0],pt[1]]) output.append(pth2) return output sceneGraph = None scenePathNodes = [] #for scene cleanup aftewards topZ = 10 def sceneDrawPath(path, color=(0, 0, 1)): global sceneGraph global scenePathNodes coPoint = coin.SoCoordinate3() pts = [] for pt in path: pts.append([pt[0], pt[1], topZ]) coPoint.point.setValues(0, len(pts), pts) ma = coin.SoBaseColor() ma.rgb = color li = coin.SoLineSet() li.numVertices.setValue(len(pts)) pathNode = coin.SoSeparator() pathNode.addChild(coPoint) pathNode.addChild(ma) pathNode.addChild(li) sceneGraph.addChild(pathNode) scenePathNodes.append(pathNode) #for scene cleanup afterwards def sceneClean(): global scenePathNodes for n in scenePathNodes: sceneGraph.removeChild(n) del scenePathNodes[:] def GenerateGCode(op,obj,adaptiveResults, helixDiameter): if len(adaptiveResults)==0 or len(adaptiveResults[0]["AdaptivePaths"])==0: return minLiftDistance = op.tool.Diameter p1 = adaptiveResults[0]["HelixCenterPoint"] p2 = adaptiveResults[0]["StartPoint"] helixRadius =math.sqrt((p1[0]-p2[0]) * (p1[0]-p2[0]) + (p1[1]-p2[1]) * (p1[1]-p2[1])) stepDown = obj.StepDown.Value passStartDepth=obj.StartDepth.Value if stepDown<0.1 : stepDown=0.1 length = 2*math.pi * helixRadius if float(obj.HelixAngle)<1: obj.HelixAngle=1 helixAngleRad = math.pi * float(obj.HelixAngle)/180.0 depthPerOneCircle=length * math.tan(helixAngleRad) stepUp = obj.LiftDistance.Value if stepUp<0: stepUp=0 lx=adaptiveResults[0]["HelixCenterPoint"][0] ly=adaptiveResults[0]["HelixCenterPoint"][1] lz=passStartDepth step=0 while passStartDepth>obj.FinalDepth.Value and step<1000: step=step+1 passEndDepth=passStartDepth-stepDown if passEndDepth0.0001: r = helixRadius - 0.01 maxfi = passDepth / depthPerOneCircle * 2 * math.pi fi = 0 offsetFi =-maxfi + startAngle-math.pi/16 helixStart = [region["HelixCenterPoint"][0] + r * math.cos(offsetFi), region["HelixCenterPoint"][1] + r * math.sin(offsetFi)] op.commandlist.append(Path.Command("(helix to depth: %f)"%passEndDepth)) #if step == 1: #rapid move to start point op.commandlist.append(Path.Command( "G0", {"X": helixStart[0], "Y": helixStart[1], "Z": obj.ClearanceHeight.Value})) #rapid move to safe height op.commandlist.append(Path.Command( "G0", {"X": helixStart[0], "Y": helixStart[1], "Z": obj.SafeHeight.Value})) op.commandlist.append(Path.Command("G1", { "X": helixStart[0], "Y": helixStart[1], "Z": passStartDepth, "F": op.vertFeed})) while fi