Path: remove Pocket OCC algorithm

This commit is contained in:
sliptonic
2017-05-15 19:23:02 -05:00
committed by Yorik van Havre
parent 936bbedaeb
commit c97724ef30
5 changed files with 423 additions and 655 deletions

View File

@@ -27,6 +27,8 @@ import Path
from PySide import QtCore, QtGui
from PathScripts import PathUtils
import PathScripts.PathLog as PathLog
from PathScripts.PathUtils import waiting_effects, depth_params
import Part
FreeCADGui = None
if FreeCAD.GuiUp:
@@ -35,8 +37,9 @@ if FreeCAD.GuiUp:
"""Path Pocket object and FreeCAD command"""
LOG_MODULE = 'PathPocket'
PathLog.setLevel(PathLog.Level.INFO, LOG_MODULE)
# PathLog.trackModule('PathPocket')
PathLog.setLevel(PathLog.Level.DEBUG, LOG_MODULE)
PathLog.trackModule('PathPocket')
FreeCAD.setLogLevel('Path.Area',0)
# Qt tanslation handling
def translate(context, text, disambig=None):
@@ -50,9 +53,6 @@ class ObjectPocket:
obj.addProperty("App::PropertyString", "Comment", "Path", QtCore.QT_TRANSLATE_NOOP("App::Property", "An optional comment for this profile"))
obj.addProperty("App::PropertyString", "UserLabel", "Path", QtCore.QT_TRANSLATE_NOOP("App::Property", "User Assigned Label"))
obj.addProperty("App::PropertyEnumeration", "Algorithm", "Algorithm", QtCore.QT_TRANSLATE_NOOP("App::Property", "The library to use to generate the path"))
obj.Algorithm = ['OCC Native', 'libarea']
# Tool Properties
obj.addProperty("App::PropertyLink", "ToolController", "Path", QtCore.QT_TRANSLATE_NOOP("App::Property", "The tool controller that will be used to calculate the path"))
@@ -71,19 +71,10 @@ class ObjectPocket:
obj.addProperty("App::PropertyEnumeration", "StartAt", "Pocket", QtCore.QT_TRANSLATE_NOOP("App::Property", "Start pocketing at center or boundary"))
obj.StartAt = ['Center', 'Edge']
obj.addProperty("App::PropertyPercent", "StepOver", "Pocket", QtCore.QT_TRANSLATE_NOOP("App::Property", "Percent of cutter diameter to step over on each pass"))
obj.addProperty("App::PropertyBool", "KeepToolDown", "Pocket", QtCore.QT_TRANSLATE_NOOP("App::Property", "Attempts to avoid unnecessary retractions."))
obj.addProperty("App::PropertyBool", "ZigUnidirectional", "Pocket", QtCore.QT_TRANSLATE_NOOP("App::Property", "Lifts tool at the end of each pass to respect cut mode."))
obj.addProperty("App::PropertyBool", "UseZigZag", "Pocket", QtCore.QT_TRANSLATE_NOOP("App::Property", "Use Zig Zag pattern to clear area."))
obj.addProperty("App::PropertyFloat", "ZigZagAngle", "Pocket", QtCore.QT_TRANSLATE_NOOP("App::Property", "Angle of the zigzag pattern"))
obj.addProperty("App::PropertyEnumeration", "OffsetPattern", "Face", QtCore.QT_TRANSLATE_NOOP("App::Property", "clearing pattern to use"))
obj.OffsetPattern = ['ZigZag', 'Offset', 'Spiral', 'ZigZagOffset', 'Line', 'Grid', 'Triangle']
# Entry Properties
obj.addProperty("App::PropertyBool", "UseEntry", "Entry", QtCore.QT_TRANSLATE_NOOP("App::Property", "Allow Cutter enter material with a straight plunge."))
obj.addProperty("App::PropertyFloatConstraint", "RampSize", "Entry", QtCore.QT_TRANSLATE_NOOP("App::Property", "The minimum fraction of tool diameter to use for ramp length"))
obj.RampSize = (0.0, 0.01, 100.0, 0.5)
obj.addProperty("App::PropertyFloatConstraint", "HelixSize", "Entry", QtCore.QT_TRANSLATE_NOOP("App::Property", "The fraction of tool diameter to use for calculating helix size."))
obj.HelixSize = (0.0, 0.01, 100.0, 0.5)
obj.addProperty("App::PropertyFloatConstraint", "RampAngle", "Entry", QtCore.QT_TRANSLATE_NOOP("App::Property", "The Angle of the ramp entry."))
obj.RampAngle = (0.0, 0.01, 100.0, 0.5)
# Start Point Properties
obj.addProperty("App::PropertyVector", "StartPoint", "Start Point", QtCore.QT_TRANSLATE_NOOP("App::Property", "The start point of this path"))
@@ -92,15 +83,7 @@ class ObjectPocket:
obj.Proxy = self
def onChanged(self, obj, prop):
if prop == "UseEntry":
if obj.UseEntry:
obj.setEditorMode('HelixSize', 0) # make this visible
obj.setEditorMode('RampAngle', 0) # make this visible
obj.setEditorMode('RampSize', 0) # make this visible
else:
obj.setEditorMode('HelixSize', 2) # make this hidden
obj.setEditorMode('RampAngle', 2) # make this hidden
obj.setEditorMode('RampSize', 2) # make this hidden
pass
def __getstate__(self):
return None
@@ -108,7 +91,29 @@ class ObjectPocket:
def __setstate__(self, state):
return None
def setDepths(proxy, obj):
PathLog.track()
parentJob = PathUtils.findParentJob(obj)
if parentJob is None:
return
baseobject = parentJob.Base
if baseobject is None:
return
try:
bb = baseobject.Shape.BoundBox # parent boundbox
obj.StartDepth = bb.ZMax
obj.ClearanceHeight = bb.ZMax + 5.0
obj.SafeHeight = bb.ZMax + 3.0
obj.FinalDepth = bb.ZMin
except:
obj.StartDepth = 5.0
obj.ClearanceHeight = 10.0
obj.SafeHeight = 8.0
def addpocketbase(self, obj, ss, sub=""):
PathLog.track()
baselist = obj.Base
if baselist is None:
baselist = []
@@ -143,7 +148,6 @@ class ObjectPocket:
else:
baselist.append(item)
obj.Base = baselist
print("this base is: " + str(baselist))
self.execute(obj)
def getStock(self, obj):
@@ -158,191 +162,68 @@ class ObjectPocket:
return self.getStock(o)
return None
def buildpathlibarea(self, obj, a):
"""Build the pocket path using libarea algorithm"""
import PathScripts.PathAreaUtils as PathAreaUtils
from PathScripts.PathUtils import depth_params
PathLog.debug("Generating toolpath with libarea offsets.\n")
@waiting_effects
def _buildPathArea(self, obj, baseobject):
PathLog.track()
pocket = Path.Area()
pocket.setPlane(Part.makeCircle(10))
pocket.add(baseobject)
depthparams = depth_params(
obj.ClearanceHeight.Value,
obj.SafeHeight.Value,
obj.StartDepth.Value,
obj.StepDown,
obj.FinishDepth.Value,
obj.FinalDepth.Value)
extraoffset = obj.MaterialAllowance.Value
stepover = (self.radius * 2) * (float(obj.StepOver)/100)
use_zig_zag = obj.UseZigZag
zig_angle = obj.ZigZagAngle
from_center = (obj.StartAt == "Center")
keep_tool_down = obj.KeepToolDown
zig_unidirectional = obj.ZigUnidirectional
start_point = None
cut_mode = obj.CutMode
PathAreaUtils.flush_nc()
PathAreaUtils.output('mem')
PathAreaUtils.feedrate_hv(self.horizFeed, self.vertFeed)
if obj.UseStartPoint:
start_point = (obj.StartPoint.x, obj.StartPoint.y)
pocketparams = {'Fill': 0,
'Coplanar': 0,
'PocketMode': 1,
'SectionCount': -1,
'Angle': obj.ZigZagAngle,
'FromCenter': (obj.StartAt == "Center"),
'PocketStepover': stepover,
'PocketExtraOffset': obj.MaterialAllowance.Value}
# print "a," + str(self.radius) + "," + str(extraoffset) + "," + str(stepover) + ",depthparams, " + str(from_center) + "," + str(keep_tool_down) + "," + str(use_zig_zag) + "," + str(zig_angle) + "," + str(zig_unidirectional) + "," + str(start_point) + "," + str(cut_mode)
Pattern = ['ZigZag', 'Offset', 'Spiral', 'ZigZagOffset', 'Line', 'Grid', 'Triangle']
pocketparams['PocketMode'] = Pattern.index(obj.OffsetPattern) + 1
PathAreaUtils.pocket(
a,
self.radius,
extraoffset,
stepover,
depthparams,
from_center,
keep_tool_down,
use_zig_zag,
zig_angle,
zig_unidirectional,
start_point,
cut_mode)
return PathAreaUtils.retrieve_gcode()
def buildpathocc(self, obj, shape):
"""Build pocket Path using Native OCC algorithm."""
import Part
import DraftGeomUtils
from PathScripts.PathUtils import fmt, helicalPlunge, rampPlunge, depth_params
PathLog.debug("Generating toolpath with OCC native offsets.\n")
extraoffset = obj.MaterialAllowance.Value
# Build up the offset loops
output = ""
if obj.Comment != "":
output += '(' + str(obj.Comment)+')\n'
output += 'G0 Z' + fmt(obj.ClearanceHeight.Value) + "F " + PathUtils.fmt(self.vertRapid) + "\n"
offsets = []
nextradius = self.radius + extraoffset
result = DraftGeomUtils.pocket2d(shape, nextradius)
while result:
offsets.extend(result)
nextradius += (self.radius * 2) * (float(obj.StepOver)/100)
result = DraftGeomUtils.pocket2d(shape, nextradius)
# revert the list so we start with the outer wires
if obj.StartAt != 'Edge':
offsets.reverse()
plungePos = None
rampEdge = None
if obj.UseEntry:
# Try to find an entry location
toold = self.radius*2
helixBounds = DraftGeomUtils.pocket2d(shape, self.radius * (1 + obj.HelixSize))
if helixBounds:
rampD = obj.RampSize
if obj.StartAt == 'Edge':
plungePos = helixBounds[0].Edges[0].Vertexes[0].Point
else:
plungePos = offsets[0].Edges[0].Vertexes[0].Point
# If it turns out this is invalid for some reason, nuke plungePos
[perp, idx] = DraftGeomUtils.findPerpendicular(plungePos, shape.Edges)
if not perp or perp.Length < self.radius * (1 + obj.HelixSize):
plungePos = None
FreeCAD.Console.PrintError(translate("PathPocket", "Helical Entry location not found.\n"))
# FIXME: Really need to do a point-in-polygon operation to make sure this is within helixBounds
# Or some math to prove that it has to be (doubt that's true)
# Maybe reverse helixBounds and pick off that?
if plungePos is None: # If we didn't find a place to helix, how about a ramp?
FreeCAD.Console.PrintMessage(translate("PathPocket", "Attempting ramp entry.\n"))
if (offsets[0].Edges[0].Length >= toold * rampD) and not (isinstance(offsets[0].Edges[0].Curve, Part.Circle)):
rampEdge = offsets[0].Edges[0]
# The last edge also connects with the starting location- try that
elif (offsets[0].Edges[-1].Length >= toold * rampD) and not (isinstance(offsets[0].Edges[-1].Curve, Part.Circle)):
rampEdge = offsets[0].Edges[-1]
else:
FreeCAD.Console.PrintError(translate("PathPocket", "Ramp Entry location not found.\n"))
# print "Neither edge works: " + str(offsets[0].Edges[0]) + ", " + str(offsets[0].Edges[-1])
# FIXME: There's got to be a smarter way to find a place to ramp
# For helix-ing/ramping, know where we were last time
# FIXME: Can probably get this from the "machine"?
lastZ = obj.ClearanceHeight.Value
startPoint = None
pocket.setParams(**pocketparams)
PathLog.debug("Pocketing with params: {}".format(pocket.getParams()))
depthparams = depth_params(
obj.ClearanceHeight.Value,
obj.SafeHeight.Value,
obj.StartDepth.Value,
obj.StepDown,
obj.FinishDepth.Value,
obj.FinalDepth.Value)
clearance_height=obj.ClearanceHeight.Value,
rapid_safety_space=obj.SafeHeight.Value,
start_depth=obj.StartDepth.Value,
step_down=obj.StepDown.Value,
z_finish_step=0.0,
final_depth=obj.FinalDepth.Value,
user_depths=None)
for vpos in depthparams.get_depths():
sections = pocket.makeSections(mode=0, project=False, heights=depthparams.get_depths())
shapelist = [sec.getShape() for sec in sections]
first = True
# loop over successive wires
for currentWire in offsets:
last = None
for edge in currentWire.Edges:
if not last:
# we set the base GO to our fast move to our starting pos
if first:
# If we can helix, do so
if plungePos:
output += helicalPlunge(plungePos, obj.RampAngle, vpos, lastZ, self.radius*2, obj.HelixSize, self.horizFeed)
lastZ = vpos
# Otherwise, see if we can ramp
# FIXME: This could be a LOT smarter (eg, searching for a longer leg of the edge to ramp along)
elif rampEdge:
output += rampPlunge(rampEdge, obj.RampAngle, vpos, lastZ)
lastZ = vpos
# Otherwise, straight plunge... Don't want to, but sometimes you might not have a choice.
# FIXME: At least not with the lazy ramp programming above...
else:
print("WARNING: Straight-plunging... probably not good, but we didn't find a place to helix or ramp")
startPoint = edge.Vertexes[0].Point
output += "G0 Z" + fmt(obj.ClearanceHeight.Value) + "F " + PathUtils.fmt(self.vertRapid) + "\n"
output += "G0 X" + fmt(startPoint.x) + " Y" + fmt(startPoint.y) +\
" Z" + fmt(obj.ClearanceHeight.Value) + "F " + PathUtils.fmt(self.horizRapid) + "\n"
first = False
# then move slow down to our starting point for our profile
last = edge.Vertexes[0].Point
output += "G1 X" + fmt(last.x) + " Y" + fmt(last.y) + " Z" + fmt(vpos) + " F" + fmt(self.vertFeed) + "\n"
if DraftGeomUtils.geomType(edge) == "Circle":
point = edge.Vertexes[-1].Point
if point == last: # edges can come flipped
point = edge.Vertexes[0].Point
center = edge.Curve.Center
relcenter = center.sub(last)
v1 = last.sub(center)
v2 = point.sub(center)
if v1.cross(v2).z < 0:
output += "G2"
else:
output += "G3"
output += " X" + fmt(point.x) + " Y" + fmt(point.y) + " Z" + fmt(vpos)
output += " I" + fmt(relcenter.x) + " J" + fmt(relcenter.y) + " K" + fmt(relcenter.z) + " F" + fmt(self.horizFeed)
output += "\n"
last = point
else:
point = edge.Vertexes[-1].Point
if point == last: # edges can come flipped
point = edge.Vertexes[0].Point
output += "G1 X" + fmt(point.x) + " Y" + fmt(point.y) + " Z" + fmt(vpos) + " F" + fmt(self.horizFeed) + "\n"
last = point
params = {'shapes': shapelist,
'feedrate': self.horizFeed,
'feedrate_v': self.vertFeed,
'verbose': True,
'resume_height': obj.StepDown.Value,
'retraction': obj.ClearanceHeight.Value}
# if obj.UseStartPoint is True and obj.StartPoint is not None:
# params['start'] = obj.StartPoint
pp = Path.fromShapes(**params)
PathLog.debug("Generating Path with params: {}".format(params))
PathLog.debug(pp)
return pp
# move back up
output += "G0 Z" + fmt(obj.ClearanceHeight.Value) + "F " + PathUtils.fmt(self.vertRapid) + "\n"
return output
# To reload this from FreeCAD, use: import PathScripts.PathPocket; reload(PathScripts.PathPocket)
def execute(self, obj):
output = ""
PathLog.track()
commandlist = []
commandlist.append(Path.Command("(" + obj.Label + ")"))
if not obj.Active:
path = Path.Path("(inactive operation)")
obj.Path = path
obj.ViewObject.Visibility = False
return
toolLoad = obj.ToolController
if toolLoad is None or toolLoad.ToolNumber == 0:
@@ -360,51 +241,40 @@ class ObjectPocket:
self.radius = tool.Diameter/2
if obj.Base:
PathLog.debug("base items exist. Processing...")
for b in obj.Base:
PathLog.debug("Base item: {}".format(b))
for sub in b[1]:
import Part
import PathScripts.PathKurveUtils
if "Face" in sub:
shape = getattr(b[0].Shape, sub)
wire = shape.OuterWire
edges = wire.Edges
else:
edges = [getattr(b[0].Shape, sub) for sub in b[1]]
wire = Part.Wire(edges)
shape = None
shape = Part.makeFace(edges, 'Part::FaceMakerSimple')
# output = ""
if obj.Algorithm == "OCC Native":
if shape is None:
shape = wire
output += self.buildpathocc(obj, shape)
else:
try:
import area
except:
FreeCAD.Console.PrintError(translate("PathKurve", "libarea needs to be installed for this command to work.\n"))
return
env = PathUtils.getEnvelope(shape, obj.StartDepth)
try:
commandlist.extend(self._buildPathArea(obj, env.cut(b[0].Shape)).Commands)
except Exception as e:
print(e)
FreeCAD.Console.PrintError("Something unexpected happened. Unable to generate a pocket path. Check project and tool config.")
else: # process the job base object as a whole
PathLog.debug("processing the whole job base object")
parentJob = PathUtils.findParentJob(obj)
if parentJob is None:
return
baseobject = parentJob.Base
if baseobject is None:
return
env = PathUtils.getEnvelope(baseobject.Shape, obj.StartDepth)
try:
commandlist.extend(self._buildPathArea(obj, env.cut(baseobject.Shape)).Commands)
except Exception as e:
print(e)
FreeCAD.Console.PrintError("Something unexpected happened. Unable to generate a pocket path. Check project and tool config.")
a = area.Area()
if shape is None:
c = PathScripts.PathKurveUtils.makeAreaCurve(wire.Edges, 'CW')
a.append(c)
else:
for w in shape.Wires:
c = PathScripts.PathKurveUtils.makeAreaCurve(w.Edges, 'CW')
a.append(c)
a.Reorder()
output += self.buildpathlibarea(obj, a)
if obj.Active:
path = Path.Path(output)
obj.Path = path
obj.ViewObject.Visibility = True
else:
path = Path.Path("(inactive operation)")
obj.Path = path
obj.ViewObject.Visibility = False
path = Path.Path(commandlist)
obj.Path = path
obj.ViewObject.Visibility = True
class _CommandSetPocketStartPoint:
@@ -468,6 +338,7 @@ class CommandPathPocket:
return False
def Activated(self):
PathLog.track()
zbottom = 0.0
ztop = 10.0
@@ -480,22 +351,18 @@ class CommandPathPocket:
FreeCADGui.doCommand('obj.Active = True')
FreeCADGui.doCommand('PathScripts.PathPocket.ViewProviderPocket(obj.ViewObject)')
FreeCADGui.doCommand('from PathScripts import PathUtils')
FreeCADGui.doCommand('obj.Algorithm = "libarea"')
FreeCADGui.doCommand('obj.StepOver = 100')
FreeCADGui.doCommand('obj.ClearanceHeight = 10') # + str(bb.ZMax + 2.0))
FreeCADGui.doCommand('obj.StepDown = 1.0')
FreeCADGui.doCommand('obj.StartDepth = ' + str(ztop))
FreeCADGui.doCommand('obj.FinalDepth =' + str(zbottom))
FreeCADGui.doCommand('obj.ZigZagAngle = 45')
FreeCADGui.doCommand('obj.UseEntry = False')
FreeCADGui.doCommand('obj.RampAngle = 3.0')
FreeCADGui.doCommand('obj.RampSize = 0.75')
FreeCADGui.doCommand('obj.HelixSize = 0.75')
FreeCADGui.doCommand('PathScripts.PathUtils.addToJob(obj)')
FreeCADGui.doCommand('PathScripts.PathPocket.ObjectPocket.setDepths(obj.Proxy, obj)')
FreeCADGui.doCommand('obj.ToolController = PathScripts.PathUtils.findToolController(obj)')
FreeCAD.ActiveDocument.commitTransaction()
FreeCAD.ActiveDocument.recompute()
#FreeCAD.ActiveDocument.recompute()
FreeCADGui.doCommand('obj.ViewObject.startEditing()')
@@ -534,14 +401,8 @@ class TaskPanel:
self.obj.MaterialAllowance = FreeCAD.Units.Quantity(self.form.extraOffset.text()).Value
if hasattr(self.obj, "UseStartPoint"):
self.obj.UseStartPoint = self.form.useStartPoint.isChecked()
if hasattr(self.obj, "Algorithm"):
self.obj.Algorithm = str(self.form.algorithmSelect.currentText())
if hasattr(self.obj, "CutMode"):
self.obj.CutMode = str(self.form.cutMode.currentText())
if hasattr(self.obj, "UseZigZag"):
self.obj.UseZigZag = self.form.useZigZag.isChecked()
if hasattr(self.obj, "ZigUnidirectional"):
self.obj.ZigUnidirectional = self.form.zigZagUnidirectional.isChecked()
if hasattr(self.obj, "ZigZagAngle"):
self.obj.ZigZagAngle = FreeCAD.Units.Quantity(self.form.zigZagAngle.text()).Value
if hasattr(self.obj, "StepOver"):
@@ -561,16 +422,14 @@ class TaskPanel:
self.form.stepDown.setText(FreeCAD.Units.Quantity(self.obj.StepDown.Value, FreeCAD.Units.Length).UserString)
self.form.extraOffset.setText(FreeCAD.Units.Quantity(self.obj.MaterialAllowance, FreeCAD.Units.Length).UserString)
self.form.useStartPoint.setChecked(self.obj.UseStartPoint)
self.form.useZigZag.setChecked(self.obj.UseZigZag)
self.form.zigZagUnidirectional.setChecked(self.obj.ZigUnidirectional)
self.form.zigZagAngle.setText(FreeCAD.Units.Quantity(self.obj.ZigZagAngle, FreeCAD.Units.Angle).UserString)
self.form.stepOverPercent.setValue(self.obj.StepOver)
index = self.form.algorithmSelect.findText(self.obj.Algorithm, QtCore.Qt.MatchFixedString)
if index >= 0:
self.form.algorithmSelect.blockSignals(True)
self.form.algorithmSelect.setCurrentIndex(index)
self.form.algorithmSelect.blockSignals(False)
# index = self.form.algorithmSelect.findText(self.obj.Algorithm, QtCore.Qt.MatchFixedString)
# if index >= 0:
# self.form.algorithmSelect.blockSignals(True)
# self.form.algorithmSelect.setCurrentIndex(index)
# self.form.algorithmSelect.blockSignals(False)
index = self.form.cutMode.findText(
self.obj.CutMode, QtCore.Qt.MatchFixedString)
@@ -579,8 +438,6 @@ class TaskPanel:
self.form.cutMode.setCurrentIndex(index)
self.form.cutMode.blockSignals(False)
# for i in self.obj.Base:
# self.form.baseList.addItem(i[0].Name + "." + i[1][0])
self.form.baseList.blockSignals(True)
for i in self.obj.Base:
for sub in i[1]:
@@ -632,9 +489,6 @@ class TaskPanel:
for sub in i[1]:
self.form.baseList.addItem(i[0].Name + "." + sub)
# for i in self.obj.Base:
# self.form.baseList.addItem(i[0].Name + "." + i[1][0])
def deleteBase(self):
dlist = self.form.baseList.selectedItems()
newlist = []
@@ -701,15 +555,12 @@ class TaskPanel:
self.form.clearanceHeight.editingFinished.connect(self.getFields)
# operation
self.form.algorithmSelect.currentIndexChanged.connect(self.getFields)
self.form.cutMode.currentIndexChanged.connect(self.getFields)
self.form.useStartPoint.clicked.connect(self.getFields)
self.form.extraOffset.editingFinished.connect(self.getFields)
# Pattern
self.form.stepOverPercent.editingFinished.connect(self.getFields)
self.form.useZigZag.clicked.connect(self.getFields)
self.form.zigZagUnidirectional.clicked.connect(self.getFields)
self.form.zigZagAngle.editingFinished.connect(self.getFields)
self.setFields()