From f025636a9c697a3d59d1b96a915a2933257b5401 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lorenz=20H=C3=BCdepohl?= Date: Tue, 10 May 2016 23:23:54 +0200 Subject: [PATCH 1/9] HelixCut: A new Path command to make circular holes --- src/Mod/Path/CMakeLists.txt | 1 + src/Mod/Path/Gui/Resources/Path.qrc | 1 + .../Path/Gui/Resources/icons/Path-Helix.svg | 548 ++++++++++++++++++ src/Mod/Path/InitGui.py | 3 +- src/Mod/Path/PathScripts/PathHelix.py | 496 ++++++++++++++++ 5 files changed, 1048 insertions(+), 1 deletion(-) create mode 100644 src/Mod/Path/Gui/Resources/icons/Path-Helix.svg create mode 100644 src/Mod/Path/PathScripts/PathHelix.py diff --git a/src/Mod/Path/CMakeLists.txt b/src/Mod/Path/CMakeLists.txt index 4a0d5edfc..a130190b0 100644 --- a/src/Mod/Path/CMakeLists.txt +++ b/src/Mod/Path/CMakeLists.txt @@ -52,6 +52,7 @@ SET(PathScripts_SRCS PathScripts/PathSimpleCopy.py PathScripts/PathStock.py PathScripts/PathStop.py + PathScripts/PathHelix.py PathScripts/PathSurface.py PathScripts/PathToolLenOffset.py PathScripts/PathToolLibraryManager.py diff --git a/src/Mod/Path/Gui/Resources/Path.qrc b/src/Mod/Path/Gui/Resources/Path.qrc index bb147e140..59e8f3ce1 100644 --- a/src/Mod/Path/Gui/Resources/Path.qrc +++ b/src/Mod/Path/Gui/Resources/Path.qrc @@ -18,6 +18,7 @@ icons/Path-FaceProfile.svg icons/Path-Face.svg icons/Path-Heights.svg + icons/Path-Helix.svg icons/Path-Hop.svg icons/Path-Inspect.svg icons/Path-Job.svg diff --git a/src/Mod/Path/Gui/Resources/icons/Path-Helix.svg b/src/Mod/Path/Gui/Resources/icons/Path-Helix.svg new file mode 100644 index 000000000..48b2ed309 --- /dev/null +++ b/src/Mod/Path/Gui/Resources/icons/Path-Helix.svg @@ -0,0 +1,548 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + image/svg+xml + + + + + + + + + + diff --git a/src/Mod/Path/InitGui.py b/src/Mod/Path/InitGui.py index cc8405ef8..397c1d818 100644 --- a/src/Mod/Path/InitGui.py +++ b/src/Mod/Path/InitGui.py @@ -65,6 +65,7 @@ class PathWorkbench (Workbench): from PathScripts import PathCustom from PathScripts import PathInspect from PathScripts import PathSimpleCopy + from PathScripts import PathHelix from PathScripts import PathEngrave from PathScripts import PathSurface from PathScripts import PathSanity @@ -79,7 +80,7 @@ class PathWorkbench (Workbench): projcmdlist = ["Path_Job", "Path_Post", "Path_Inspect", "Path_Sanity"] toolcmdlist = ["Path_ToolLibraryEdit", "Path_LoadTool"] prepcmdlist = ["Path_Plane", "Path_Fixture", "Path_ToolLenOffset", "Path_Comment", "Path_Stop", "Path_FaceProfile", "Path_FacePocket", "Path_Custom", "Path_FromShape"] - twodopcmdlist = ["Path_Contour", "Path_Profile", "Path_Profile_Edges", "Path_Pocket", "Path_Drilling", "Path_Engrave", "Path_MillFace"] + twodopcmdlist = ["Path_Contour", "Path_Profile", "Path_Profile_Edges", "Path_Pocket", "Path_Drilling", "Path_Engrave", "Path_MillFace", "Path_Helix"] threedopcmdlist = ["Path_Surfacing"] modcmdlist = ["Path_Copy", "Path_CompoundExtended", "Path_Array", "Path_SimpleCopy" ] dressupcmdlist = ["PathDressup_Dogbone", "PathDressup_DragKnife"] diff --git a/src/Mod/Path/PathScripts/PathHelix.py b/src/Mod/Path/PathScripts/PathHelix.py new file mode 100644 index 000000000..2325ea16f --- /dev/null +++ b/src/Mod/Path/PathScripts/PathHelix.py @@ -0,0 +1,496 @@ +# -*- coding: utf-8 -*- + +#*************************************************************************** +#* * +#* Copyright (c) 2016 Lorenz Hüdepohl * +#* * +#* This program is free software; you can redistribute it and/or modify * +#* it under the terms of the GNU Lesser General Public License (LGPL) * +#* as published by the Free Software Foundation; either version 2 of * +#* the License, or (at your option) any later version. * +#* for detail see the LICENCE text file. * +#* * +#* This program is distributed in the hope that it will be useful, * +#* but WITHOUT ANY WARRANTY; without even the implied warranty of * +#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +#* GNU Library General Public License for more details. * +#* * +#* You should have received a copy of the GNU Library General Public * +#* License along with this program; if not, write to the Free Software * +#* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * +#* USA * +#* * +#*************************************************************************** + +import FreeCAD,Path +from PySide import QtCore,QtGui +from PathScripts import PathUtils +from PathUtils import fmt + +"""Helix Drill object and FreeCAD command""" + +try: + _encoding = QtGui.QApplication.UnicodeUTF8 + def translate(context, text, disambig=None): + return QtGui.QApplication.translate(context, text, disambig, _encoding) +except AttributeError: + def translate(context, text, disambig=None): + return QtGui.QApplication.translate(context, text, disambig) + +def hollow_cylinder(cyl): + """Test if this is a hollow cylinder""" + from Part import Circle + circle1 = None + line = None + for edge in cyl.Edges: + if isinstance(edge.Curve, Circle): + if circle1 is None: + circle1 = edge + else: + circle2 = edge + else: + line = edge + center = (circle1.Curve.Center + circle2.Curve.Center).scale(0.5, 0.5, 0.5) + p = (circle1.valueAt(circle1.ParameterRange[0]) + circle2.valueAt(circle1.ParameterRange[0])).scale(0.5, 0.5, 0.5) + to_outside = (p - center).normalize() + u, v = cyl.Surface.parameter(p) + normal = cyl.normalAt(u, v).normalize() + + cos_a = to_outside.dot(normal) + + if cos_a > 1.0 - 1e-12: + return False + elif cos_a < -1.0 + 1e-12: + return True + else: + raise Exception("Strange cylinder encountered, cannot determine if it is hollow or not") + +def z_cylinder(cyl): + """ Test if cylinder is aligned to z-Axis""" + if cyl.Surface.Axis.x != 0.0: + return False + if cyl.Surface.Axis.y != 0.0: + return False + return True + + +def connected(edge, face): + for otheredge in face.Edges: + if edge.isSame(otheredge): + return True + return False + + +def helix_cut(center, r_out, r_in, dr, zmax, zmin, dz, safe_z, tool_diameter, vfeed, hfeed, direction, startside): + """ + center: 2-tuple + (x0,y0) coordinates of center + r_out, r_in: floats + radial range, cut from outer radius r_out in layers of dr to inner radius r_in + zmax, zmin: floats + z-range, cut from zmax in layers of dz down to zmin + safe_z: float + safety layer height + tool_diameter: float + Width of tool + """ + from numpy import ceil, allclose, linspace + + out = "(helix_cut <{0}, {1}>, {2})".format(center[0], center[1], ", ".join(map(str, (r_out, r_in, dr, zmax, zmin, dz, safe_z, tool_diameter)))) + + x0, y0 = center + nz = int(ceil(2*(zmax - zmin)/dz)) + dz = (zmax - zmin) / nz + if dr > tool_diameter: + FreeCAD.Console.PrintWarning("PathHelix: Warning, shortening dr to tool diameter!\n") + dr = tool_diameter + + def xyz(x=None, y=None, z=None): + out = "" + if x is not None: + out += " X" + fmt(x) + if y is not None: + out += " Y" + fmt(y) + if z is not None: + out += " Z" + fmt(z) + return out + + def rapid(x=None, y=None, z=None): + return "G0" + xyz(x,y,z) + "\n" + + def F(f=None): + return (" F" + fmt(f) if f else "") + + def feed(x=None, y=None, z=None, f=None): + return "G1" + xyz(x,y,z) + F(f) + "\n" + + def arc(x,y,i,j,z,f): + if direction == "CW": + code = "G2" + elif direction == "CCW": + code = "G3" + return code + " I" + fmt(i) + " J" + fmt(j) + " X" + fmt(x) + " Y" + fmt(y) + " Z" + fmt(z) + F(f) + "\n" + + def helix_cut_r(r): + out = "" + out += rapid(x=x0+r,y=y0) + out += rapid(z=zmax + tool_diameter) + out += feed(z=zmax,f=vfeed) + for i in range(1,nz+2): + out += arc(x0-r, y0, i=-r, j=0.0, z = max(zmax - (i - 0.5) * dz, zmin), f=hfeed) + out += arc(x0+r, y0, i=r, j=0.0, z = max(zmax - i * dz, zmin), f=hfeed) + out += feed(z=zmax + tool_diameter, f=vfeed) + out += rapid(z=safe_z) + return out + + assert(r_out > 0.0) + assert(r_in >= 0.0) + + msg = None + if r_out < 0.0: + msg = "r_out < 0" + elif r_in > 0 and r_out - r_in < tool_diameter: + msg = "r_out - r_in = {0} is < tool diameter of {1}".format(r_out - r_in, tool_diamater) + elif r_in == 0.0 and not r_out > tool_diameter/2.: + msg = "Cannot drill a hole of diameter {0} with a tool of diameter {1}".format(2 * r_out, tool_diameter) + elif not startside in ["inside", "outside"]: + msg = "Invalid value for parameter 'startside'" + + if msg: + out += "(ERROR: Hole at {0}:".format((x0, y0, zmax)) + msg + ")\n" + FreeCAD.Console.PrintError("PathHelix: Hole at {0}:".format((x0, y0, zmax)) + msg + "\n") + return out + + if r_in > 0: + out += "(annulus mode)\n" + r_out = r_out - tool_diameter/2 + r_in = r_in + tool_diameter/2 + if abs((r_out - r_in) / dr) < 1e-5: + radii = [(r_out + r_in)/2] + else: + nr = max(int(ceil((r_out - r_in)/dr)), 2) + radii = linspace(r_out, r_in, nr) + elif r_out < dr: + out += "(single helix mode)\n" + radii = [r_out - tool_diameter/2] + assert(radii[0] > 0) + else: + out += "(full hole mode)\n" + r_out = r_out - tool_diameter/2 + r_in = dr/2 + + nr = max(1 + int(ceil((r_out - r_in)/dr)), 2) + radii = linspace(r_out, r_in, nr) + assert(all(radii > 0)) + + if startside == "inside": + radii = radii[::-1] + + for r in radii: + out += "(radius {0})\n".format(r) + out += helix_cut_r(r) + + return out + +class ObjectPathHelix: + + def __init__(self,obj): + + # Basic + obj.addProperty("App::PropertyLinkSub","Base","Path",translate("Parent Object","The base geometry of this toolpath")) + obj.addProperty("App::PropertyLinkSubList","Features","Path",translate("Features","Selected features for the drill operation")) + obj.addProperty("App::PropertyBool","Active","Path",translate("Active","Set to False to disable code generation")) + obj.addProperty("App::PropertyString","Comment","Path",translate("Comment","An optional comment for this profile, will appear in G-Code")) + + # Helix specific + obj.addProperty("App::PropertyEnumeration", "Direction", "Helix Drill", + translate("Direction", "The direction of the circular cuts, clockwise (CW), or counter clockwise (CCW)")) + obj.Direction = ['CW','CCW'] + + obj.addProperty("App::PropertyEnumeration", "StartSide", "Helix Drill", + translate("Direction", "Start cutting from the inside or outside")) + obj.StartSide = ['inside','outside'] + obj.addProperty("App::PropertyLength", "DeltaR", "Helix Drill", translate("DeltaR", "Radius increment, must be smaller than tool diameter")) + obj.addProperty("App::PropertyBool", "Recursive", "Helix Drill", translate("Recursive", "If True, drill holes also in any subsequent holes at the bottom of holes that are not fully through")) + + # Depth Properties + obj.addProperty("App::PropertyDistance", "ClearanceHeight", "Depth", translate("Clearance Height","Distance above edge to which to retract the tool")) + obj.addProperty("App::PropertyLength", "StepDown", "Depth", translate("StepDown","Incremental Step Down of Tool")) + obj.addProperty("App::PropertyBool","UseStartDepth","Depth",translate("Use Start Depth","Set to True to manually specify a start depth")) + obj.addProperty("App::PropertyDistance", "StartDepth", "Depth", translate("Start Depth","Starting Depth of Tool - first cut depth in Z")) + obj.addProperty("App::PropertyBool","UseFinalDepth","Depth", translate("Use Final Depth","Set to True to manually specify a final depth")) + obj.addProperty("App::PropertyDistance", "FinalDepth", "Depth", translate("Final Depth","Final Depth of Tool - lowest value in Z")) + obj.addProperty("App::PropertyDistance", "ThroughDepth", "Depth", translate("Through Depth","Add this amount of additional cutting depth to open holes, " + "only used if UseFinalDepth is False")) + + # Feed Properties + obj.addProperty("App::PropertySpeed", "VertFeed", "Feed", translate("Vert Feed","Feed rate for vertical moves")) + obj.addProperty("App::PropertySpeed", "HorizFeed", "Feed", translate("Horiz Feed","Feed rate for horizontal moves")) + + # The current tool number, read-only + # this is apparently used internally, to keep track of tool chagnes + obj.addProperty("App::PropertyIntegerConstraint","ToolNumber","Tool",translate("PathProfile","The current tool in use")) + obj.ToolNumber = (0,0,1000,1) + obj.setEditorMode('ToolNumber',1) #make this read only + + obj.Proxy = self + + def __getstate__(self): + return None + + def __setstate__(self,state): + return None + + def execute(self,obj): + from Part import Circle, Cylinder, Plane + if obj.Base: + if not obj.Active: + obj.Path = Path.Path("(helix cut operation inactive)") + obj.ViewObject.Visibility = False + return + + if not len(obj.InList) > 0: + FreeCAD.Console.PrintError("PathHelix: Operation is not part of a project\n") + obj.Path = Path.Path("(helix cut operation not part of any project)") + obj.ViewObject.Visibility = False + return + + project = obj.InList[0] + obj.ToolNumber = int(PathUtils.changeTool(obj,project)) + tool = PathUtils.getTool(obj,obj.ToolNumber) + + if not tool: + FreeCAD.Console.PrintError("PathHelix: No tool selected for helix cut operation, insert a tool change operation first\n") + obj.Path = Path.Path("(ERROR: no tool selected for helix cut operation)") + return + + def connected_cylinders(base, edge): + cylinders = [] + for face in base.Shape.Faces: + if isinstance(face.Surface, Cylinder): + if connected(edge, face): + if z_cylinder(face): + cylinders.append((base, face)) + return cylinders + + cylinders = [] + + for base, feature in obj.Features: + subobj = getattr(base.Shape, feature) + if subobj.ShapeType =='Face': + if isinstance(subobj.Surface, Cylinder): + if z_cylinder(subobj): + cylinders.append((base, subobj)) + else: + # brute force triple-loop as FreeCAD does not expose + # any topology information... + for edge in subobj.Edges: + cylinders.extend(filter(lambda b_c: hollow_cylinder(b_c[1]), (connected_cylinders(base, edge)))) + + if subobj.ShapeType == 'Edge': + cylinders.extend(connected_cylinders(base, subobj)) + + output = '(helix cut operation' + if obj.Comment: + output += ', '+ str(obj.Comment)+')\n' + else: + output += ')\n' + + output += "G0 Z" + fmt(obj.Base[0].Shape.BoundBox.ZMax + float(obj.ClearanceHeight)) + + drill_jobs = [] + + for base, cylinder in cylinders: + xc, yc, zc = cylinder.Surface.Center + if obj.UseStartDepth: + zmax = obj.StartDepth.Value + else: + zmax = cylinder.BoundBox.ZMax + + if obj.Recursive: + cur_z = zmax + jobs = [] + + while cylinder: + # Find other edge of current cylinder + other_edge = None + for edge in cylinder.Edges: + if isinstance(edge.Curve, Circle) and edge.Curve.Center.z != cur_z: + other_edge = edge + break + + next_z = other_edge.Curve.Center.z + dz = next_z - cur_z + r = cylinder.Surface.Radius + print cur_z, dz, r + + if dz < 0: + # This is a closed hole if the face connecting to the current cylinder at next_z has + # the cylinder's edge as its OuterWire + closed = None + for face in base.Shape.Faces: + if connected(other_edge, face) and not face.isSame(cylinder.Faces[0]): + wire = face.OuterWire + if len(wire.Edges) == 1 and wire.Edges[0].isSame(other_edge): + closed = True + else: + closed = False + + if closed is None: + raise Exception("Cannot determine if this cylinder is closed on the z = {0} side".format(next_z)) + + jobs.append(dict(xc=xc, yc=yc, zmin=next_z, zmax=cur_z, r_out=r, r_in=0.0, closed=closed)) + + elif dz > 0: + new_jobs = [] + for job in jobs: + if job["zmin"] < next_z < job["zmax"]: + # split this job + job1 = dict(job) + job2 = dict(job) + job1["zmin"] = next_z + job2["zmax"] = next_z + job2["r_in"] = r + new_jobs.append(job1) + new_jobs.append(job2) + else: + new_jobs.append(job) + jobs = new_jobs + else: + FreeCAD.Console.PrintWarning("PathHelix: Encountered cylinder with zero height\n") + break + + cur_z = next_z + cylinder = None + faces = [] + for face in base.Shape.Faces: + if connected(other_edge, face): + if isinstance(face.Surface, Plane): + faces.append(face) + face, = faces + for edge in face.Edges: + if not edge.isSame(other_edge): + for base, other_cylinder in connected_cylinders(base, edge): + if other_cylinder.Surface.Center.x == xc and other_cylinder.Surface.Center.y == yc and other_cylinder.Surface.Radius < r: + cylinder = other_cylinder + break + + if obj.UseFinalDepth: + jobs[-1]["zmin"] = obj.FinalDepth.Value + else: + if not jobs[-1]["closed"]: + jobs[-1]["zmin"] -= obj.ThroughDepth.Value + + drill_jobs.extend(jobs) + else: + if obj.UseFinalDepth: + zmin = obj.FinalDepth.Value + else: + zmin = cylinder.BoundBox.ZMin - obj.ThroughDepth.Value + drill_jobs.append(dict(xc=xc, yc=yc, zmin=zmin, zmax=zmax, r_out=cylinder.Surface.Radius, r_in=0.0)) + + for job in drill_jobs: + output += helix_cut((job["xc"], job["yc"]), job["r_out"], job["r_in"], obj.DeltaR.Value, + job["zmax"], job["zmin"], obj.StepDown.Value, + job["zmax"] + obj.ClearanceHeight.Value, tool.Diameter, + obj.VertFeed.Value, obj.HorizFeed.Value, obj.Direction, obj.StartSide) + output += '\n' + + obj.Path = Path.Path(output) + if obj.ViewObject: + obj.ViewObject.Visibility = True + + +class ViewProviderPathHelix: + def __init__(self,vobj): + vobj.Proxy = self + + def attach(self,vobj): + self.Object = vobj.Object + return + + def getIcon(self): + return ":/icons/Path-Helix.svg" + + def __getstate__(self): + return None + + def __setstate__(self,state): + return None + + +class CommandPathHelix: + def GetResources(self): + return {'Pixmap' : 'Path-Helix', + 'MenuText': QtCore.QT_TRANSLATE_NOOP("PathHelix","PathHelix"), + 'ToolTip': QtCore.QT_TRANSLATE_NOOP("PathHelix","Creates a helix cut from selected circles")} + + def IsActive(self): + return not FreeCAD.ActiveDocument is None + + def Activated(self): + import FreeCADGui + import Path + from PathScripts import PathUtils, PathHelix + + selection = FreeCADGui.Selection.getSelectionEx() + + if not len(selection) == 1: + FreeCAD.Console.PrintError("Only considering first object for PathHelix!\n") + selection = selection[0] + + if not len(selection.SubElementNames) > 0: + FreeCAD.Console.PrintError("Select a face or circles to create helix cuts\n") + + # register the transaction for the undo stack + try: + FreeCAD.ActiveDocument.openTransaction(translate("PathHelix","Create a helix cut")) + FreeCADGui.addModule("PathScripts.PathHelix") + + obj = FreeCAD.ActiveDocument.addObject("Path::FeaturePython","PathHelix") + PathHelix.ObjectPathHelix(obj) + PathHelix.ViewProviderPathHelix(obj.ViewObject) + + obj.Base = selection.Object + obj.Features = [(selection.Object, subobj) for subobj in selection.SubElementNames] + obj.DeltaR = 1.0 + + project = PathUtils.addToProject(obj) + tl = PathUtils.changeTool(obj,project) + if tl: + obj.ToolNumber = tl + tool = PathUtils.getTool(obj,obj.ToolNumber) + if tool: + # start with 25% overlap + obj.DeltaR = tool.Diameter * 0.75 + + obj.Active = True + obj.Comment = "" + + obj.Direction = "CW" + obj.StartSide = "inside" + + obj.ClearanceHeight = 10.0 + obj.StepDown = 1.0 + obj.UseStartDepth = False + obj.StartDepth = 1.0 + obj.UseFinalDepth = False + obj.FinalDepth = 0.0 + obj.ThroughDepth = 0.0 + obj.Recursive = True + + obj.VertFeed = 0.0 + obj.HorizFeed = 0.0 + + # commit + FreeCAD.ActiveDocument.commitTransaction() + + except: + FreeCAD.ActiveDocument.abortTransaction() + raise + + FreeCAD.ActiveDocument.recompute() + +if FreeCAD.GuiUp: + import FreeCADGui + FreeCADGui.addCommand('Path_Helix',CommandPathHelix()) From cd651d1a42336eb33d824094e85603ce07514662 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lorenz=20H=C3=BCdepohl?= Date: Thu, 9 Jun 2016 00:32:13 +0200 Subject: [PATCH 2/9] A TaskPanel for PathHelix --- src/Mod/Path/PathScripts/PathHelix.py | 185 ++++++++++++++++++++++---- 1 file changed, 156 insertions(+), 29 deletions(-) diff --git a/src/Mod/Path/PathScripts/PathHelix.py b/src/Mod/Path/PathScripts/PathHelix.py index 2325ea16f..970628ef1 100644 --- a/src/Mod/Path/PathScripts/PathHelix.py +++ b/src/Mod/Path/PathScripts/PathHelix.py @@ -22,10 +22,14 @@ #* * #*************************************************************************** -import FreeCAD,Path -from PySide import QtCore,QtGui -from PathScripts import PathUtils -from PathUtils import fmt +import FreeCAD, Path +if FreeCAD.GuiUp: + import FreeCADGui + from PySide import QtCore, QtGui + from DraftTools import translate + +from . import PathUtils +from .PathUtils import fmt """Helix Drill object and FreeCAD command""" @@ -192,10 +196,9 @@ def helix_cut(center, r_out, r_in, dr, zmax, zmin, dz, safe_z, tool_diameter, vf return out -class ObjectPathHelix: +class ObjectPathHelix(object): def __init__(self,obj): - # Basic obj.addProperty("App::PropertyLinkSub","Base","Path",translate("Parent Object","The base geometry of this toolpath")) obj.addProperty("App::PropertyLinkSubList","Features","Path",translate("Features","Selected features for the drill operation")) @@ -210,22 +213,33 @@ class ObjectPathHelix: obj.addProperty("App::PropertyEnumeration", "StartSide", "Helix Drill", translate("Direction", "Start cutting from the inside or outside")) obj.StartSide = ['inside','outside'] - obj.addProperty("App::PropertyLength", "DeltaR", "Helix Drill", translate("DeltaR", "Radius increment, must be smaller than tool diameter")) - obj.addProperty("App::PropertyBool", "Recursive", "Helix Drill", translate("Recursive", "If True, drill holes also in any subsequent holes at the bottom of holes that are not fully through")) + + obj.addProperty("App::PropertyLength", "DeltaR", "Helix Drill", + translate("DeltaR", "Radius increment (must be smaller than tool diameter)")) + obj.addProperty("App::PropertyBool", "Recursive", "Helix Drill", + translate("Recursive", "If True, drill holes also in any subsequent smaller holes at the bottom of a hole")) # Depth Properties - obj.addProperty("App::PropertyDistance", "ClearanceHeight", "Depth", translate("Clearance Height","Distance above edge to which to retract the tool")) - obj.addProperty("App::PropertyLength", "StepDown", "Depth", translate("StepDown","Incremental Step Down of Tool")) - obj.addProperty("App::PropertyBool","UseStartDepth","Depth",translate("Use Start Depth","Set to True to manually specify a start depth")) - obj.addProperty("App::PropertyDistance", "StartDepth", "Depth", translate("Start Depth","Starting Depth of Tool - first cut depth in Z")) - obj.addProperty("App::PropertyBool","UseFinalDepth","Depth", translate("Use Final Depth","Set to True to manually specify a final depth")) - obj.addProperty("App::PropertyDistance", "FinalDepth", "Depth", translate("Final Depth","Final Depth of Tool - lowest value in Z")) - obj.addProperty("App::PropertyDistance", "ThroughDepth", "Depth", translate("Through Depth","Add this amount of additional cutting depth to open holes, " - "only used if UseFinalDepth is False")) + obj.addProperty("App::PropertyDistance", "Clearance", "Depths", + translate("Clearance","Safe distance above the top of the hole to which to retract the tool")) + obj.addProperty("App::PropertyLength", "StepDown", "Depths", + translate("StepDown","Incremental Step Down of Tool")) + obj.addProperty("App::PropertyBool","UseStartDepth","Depths", + translate("Use Start Depth","Set to True to manually specify a start depth")) + obj.addProperty("App::PropertyDistance", "StartDepth", "Depths", + translate("Start Depth","Starting Depth of Tool - first cut depth in Z")) + obj.addProperty("App::PropertyBool","UseFinalDepth","Depths", + translate("Use Final Depth","Set to True to manually specify a final depth")) + obj.addProperty("App::PropertyDistance", "FinalDepth", "Depths", + translate("Final Depth","Final Depth of Tool - lowest value in Z")) + obj.addProperty("App::PropertyDistance", "ThroughDepth", "Depths", + translate("Through Depth","Add this amount of additional cutting depth to open-ended holes. Only used if UseFinalDepth is False")) # Feed Properties - obj.addProperty("App::PropertySpeed", "VertFeed", "Feed", translate("Vert Feed","Feed rate for vertical moves")) - obj.addProperty("App::PropertySpeed", "HorizFeed", "Feed", translate("Horiz Feed","Feed rate for horizontal moves")) + obj.addProperty("App::PropertySpeed", "VertFeed", "Feeds", + translate("Vert Feed","Feed rate for vertical mill moves, this includes the actual arcs")) + obj.addProperty("App::PropertySpeed", "HorizFeed", "Feeds", + translate("Horiz Feed","Feed rate for horizontal mill moves, these are mostly retractions to the safe distance above the object")) # The current tool number, read-only # this is apparently used internally, to keep track of tool chagnes @@ -296,7 +310,7 @@ class ObjectPathHelix: else: output += ')\n' - output += "G0 Z" + fmt(obj.Base[0].Shape.BoundBox.ZMax + float(obj.ClearanceHeight)) + output += "G0 Z" + fmt(obj.Base[0].Shape.BoundBox.ZMax + float(obj.Clearance)) drill_jobs = [] @@ -322,7 +336,6 @@ class ObjectPathHelix: next_z = other_edge.Curve.Center.z dz = next_z - cur_z r = cylinder.Surface.Radius - print cur_z, dz, r if dz < 0: # This is a closed hole if the face connecting to the current cylinder at next_z has @@ -392,7 +405,7 @@ class ObjectPathHelix: for job in drill_jobs: output += helix_cut((job["xc"], job["yc"]), job["r_out"], job["r_in"], obj.DeltaR.Value, job["zmax"], job["zmin"], obj.StepDown.Value, - job["zmax"] + obj.ClearanceHeight.Value, tool.Diameter, + job["zmax"] + obj.Clearance.Value, tool.Diameter, obj.VertFeed.Value, obj.HorizFeed.Value, obj.Direction, obj.StartSide) output += '\n' @@ -401,7 +414,7 @@ class ObjectPathHelix: obj.ViewObject.Visibility = True -class ViewProviderPathHelix: +class ViewProviderPathHelix(object): def __init__(self,vobj): vobj.Proxy = self @@ -412,14 +425,19 @@ class ViewProviderPathHelix: def getIcon(self): return ":/icons/Path-Helix.svg" + def setEdit(self, vobj, mode=0): + FreeCADGui.Control.closeDialog() + taskpanel = TaskPanel(vobj.Object) + FreeCADGui.Control.showDialog(taskpanel) + return True + def __getstate__(self): return None - def __setstate__(self,state): + def __setstate__(self, state): return None - -class CommandPathHelix: +class CommandPathHelix(object): def GetResources(self): return {'Pixmap' : 'Path-Helix', 'MenuText': QtCore.QT_TRANSLATE_NOOP("PathHelix","PathHelix"), @@ -431,7 +449,7 @@ class CommandPathHelix: def Activated(self): import FreeCADGui import Path - from PathScripts import PathUtils, PathHelix + from PathScripts import PathUtils selection = FreeCADGui.Selection.getSelectionEx() @@ -448,8 +466,8 @@ class CommandPathHelix: FreeCADGui.addModule("PathScripts.PathHelix") obj = FreeCAD.ActiveDocument.addObject("Path::FeaturePython","PathHelix") - PathHelix.ObjectPathHelix(obj) - PathHelix.ViewProviderPathHelix(obj.ViewObject) + ObjectPathHelix(obj) + ViewProviderPathHelix(obj.ViewObject) obj.Base = selection.Object obj.Features = [(selection.Object, subobj) for subobj in selection.SubElementNames] @@ -470,7 +488,7 @@ class CommandPathHelix: obj.Direction = "CW" obj.StartSide = "inside" - obj.ClearanceHeight = 10.0 + obj.Clearance = 10.0 obj.StepDown = 1.0 obj.UseStartDepth = False obj.StartDepth = 1.0 @@ -491,6 +509,115 @@ class CommandPathHelix: FreeCAD.ActiveDocument.recompute() +class TaskPanel(object): + def __init__(self, obj): + from Units import Quantity + self.obj = obj + + ui = FreeCADGui.UiLoader() + layout = QtGui.QGridLayout() + + headerStyle = "QLabel { font-weight: bold; font-size: large; }" + + def addWidget(widget): + row = layout.rowCount() + layout.addWidget(widget, row, 0, columnSpan=2) + + def addWidgets(widget1, widget2): + row = layout.rowCount() + layout.addWidget(widget1, row, 0) + layout.addWidget(widget2, row, 1) + + def heading(label): + heading = QtGui.QLabel(label) + heading.setStyleSheet(headerStyle) + addWidget(heading) + + def addQuantity(property, label, activator=None, max=None, step=None): + if activator: + label = QtGui.QCheckBox(label) + def change(state): + setattr(self.obj, activator, label.isChecked()) + self.obj.Proxy.execute(self.obj) + FreeCAD.ActiveDocument.recompute() + label.stateChanged.connect(change) + label.setChecked(getattr(self.obj, activator)) + label.setToolTip(self.obj.getDocumentationOfProperty(activator)) + else: + label = QtGui.QLabel(label) + label.setToolTip(self.obj.getDocumentationOfProperty(property)) + widget = ui.createWidget("Gui::InputField") + widget.setText(str(getattr(self.obj, property))) + widget.setToolTip(self.obj.getDocumentationOfProperty(property)) + def change(quantity): + if activator: + label.setChecked(True) + setattr(self.obj, property, quantity) + self.obj.Proxy.execute(self.obj) + FreeCAD.ActiveDocument.recompute() + QtCore.QObject.connect(widget, QtCore.SIGNAL("valueChanged(const Base::Quantity &)"), change) + addWidgets(label, widget) + + def addCheckBox(property, label): + widget = QtGui.QCheckBox(label) + widget.setToolTip(self.obj.getDocumentationOfProperty(property)) + def change(state): + setattr(self.obj, property, widget.isChecked()) + self.obj.Proxy.execute(self.obj) + FreeCAD.ActiveDocument.recompute() + widget.stateChanged.connect(change) + widget.setChecked(getattr(self.obj, property)) + addWidget(widget) + + def addEnumeration(property, label, options): + label = QtGui.QLabel(label) + label.setToolTip(self.obj.getDocumentationOfProperty(property)) + widget = QtGui.QComboBox() + widget.setToolTip(self.obj.getDocumentationOfProperty(property)) + for option_label, option_value in options: + widget.addItem(option_label) + def change(index): + setattr(self.obj, property, options[index][1]) + self.obj.Proxy.execute(self.obj) + FreeCAD.ActiveDocument.recompute() + widget.currentIndexChanged.connect(change) + addWidgets(label, widget) + + heading("Drill parameters") + addCheckBox("Active", "Operation is active") + addQuantity("DeltaR", "Step in Radius") + addQuantity("StepDown", "Step in Z") + addEnumeration("Direction", "Cut direction", [("Clockwise", "CW"), ("Counter-Clockwise", "CCW")]) + addEnumeration("StartSide", "Start Side", [("Start from inside", "inside"), ("Start from outside", "outside")]) + addCheckBox("Recursive", "Also mill subsequent holes") + + heading("Cutting Depths") + addQuantity("Clearance", "Clearance Height") + addQuantity("StartDepth", "Start Depth", "UseStartDepth") + addQuantity("FinalDepth", "Final Depth", "UseFinalDepth") + addQuantity("ThroughDepth", "Through Depth") + + heading("Feeds") + addQuantity("HorizFeed", "Horizontal Feed") + addQuantity("VertFeed", "Vertical Feed") + + widget = QtGui.QWidget() + widget.setLayout(layout) + self.form = widget + + def needsFullSpace(self): + return True + + def accept(self): + FreeCAD.Console.PrintError("accept()\n") + FreeCADGui.ActiveDocument.resetEdit() + FreeCADGui.Control.closeDialog() + + def reject(self): + FreeCAD.Console.PrintError("reject()\n") + FreeCADGui.ActiveDocument.resetEdit() + FreeCADGui.Control.closeDialog() + if FreeCAD.GuiUp: import FreeCADGui FreeCADGui.addCommand('Path_Helix',CommandPathHelix()) From 20af62cb9ea34fe9401a82a09fc481385a0268fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lorenz=20H=C3=BCdepohl?= Date: Thu, 9 Jun 2016 00:53:08 +0200 Subject: [PATCH 3/9] PathHelix: Fix StartDepth and FinalDepth for recursive holes These were previously only applied for the first and last operation, respectively. Now whole operations can be skipped if they are completely outside the specified range. --- src/Mod/Path/PathScripts/PathHelix.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/Mod/Path/PathScripts/PathHelix.py b/src/Mod/Path/PathScripts/PathHelix.py index 970628ef1..2c16aaae0 100644 --- a/src/Mod/Path/PathScripts/PathHelix.py +++ b/src/Mod/Path/PathScripts/PathHelix.py @@ -100,6 +100,9 @@ def helix_cut(center, r_out, r_in, dr, zmax, zmin, dz, safe_z, tool_diameter, vf """ from numpy import ceil, allclose, linspace + if (zmax <= zmin): + return + out = "(helix_cut <{0}, {1}>, {2})".format(center[0], center[1], ", ".join(map(str, (r_out, r_in, dr, zmax, zmin, dz, safe_z, tool_diameter)))) x0, y0 = center @@ -316,13 +319,9 @@ class ObjectPathHelix(object): for base, cylinder in cylinders: xc, yc, zc = cylinder.Surface.Center - if obj.UseStartDepth: - zmax = obj.StartDepth.Value - else: - zmax = cylinder.BoundBox.ZMax if obj.Recursive: - cur_z = zmax + cur_z = cylinder.BoundBox.ZMax jobs = [] while cylinder: @@ -388,14 +387,24 @@ class ObjectPathHelix(object): cylinder = other_cylinder break + if obj.UseStartDepth: + jobs = [job for job in jobs if job["zmin"] < obj.StartDepth.Value] + if jobs: + jobs[0]["zmax"] = obj.StartDepth.Value if obj.UseFinalDepth: - jobs[-1]["zmin"] = obj.FinalDepth.Value + jobs = [job for job in jobs if job["zmax"] > obj.FinalDepth.Value] + if jobs: + jobs[-1]["zmin"] = obj.FinalDepth.Value else: if not jobs[-1]["closed"]: jobs[-1]["zmin"] -= obj.ThroughDepth.Value drill_jobs.extend(jobs) else: + if obj.UseStartDepth: + zmax = obj.StartDepth.Value + else: + zmax = cylinder.BoundBox.ZMax if obj.UseFinalDepth: zmin = obj.FinalDepth.Value else: @@ -592,7 +601,7 @@ class TaskPanel(object): addCheckBox("Recursive", "Also mill subsequent holes") heading("Cutting Depths") - addQuantity("Clearance", "Clearance Height") + addQuantity("Clearance", "Clearance Distance") addQuantity("StartDepth", "Start Depth", "UseStartDepth") addQuantity("FinalDepth", "Final Depth", "UseFinalDepth") addQuantity("ThroughDepth", "Through Depth") From 00226a9c47289ff1f6577c5d0611b4408dd79ba9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lorenz=20H=C3=BCdepohl?= Date: Thu, 9 Jun 2016 23:48:28 +0200 Subject: [PATCH 4/9] PathHelix: Fixes for clearance, non-aligned centers The safe Z value was calculated with respect to the current hole, but the tool should better be always retracted to the safe Z value with respect to the first hole of a series of holes. Another fix concerns the detection of holes-within-holes, previously it was assumed that there the centers must align perfectly, this seems to be the case only up to some numerical precision. Fixed by just allowing any hole that is wholly contained in the first hole, even it is not centered. --- src/Mod/Path/PathScripts/PathHelix.py | 33 ++++++++++++++++++--------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/src/Mod/Path/PathScripts/PathHelix.py b/src/Mod/Path/PathScripts/PathHelix.py index 2c16aaae0..3c622f055 100644 --- a/src/Mod/Path/PathScripts/PathHelix.py +++ b/src/Mod/Path/PathScripts/PathHelix.py @@ -103,11 +103,13 @@ def helix_cut(center, r_out, r_in, dr, zmax, zmin, dz, safe_z, tool_diameter, vf if (zmax <= zmin): return - out = "(helix_cut <{0}, {1}>, {2})".format(center[0], center[1], ", ".join(map(str, (r_out, r_in, dr, zmax, zmin, dz, safe_z, tool_diameter)))) + out = "(helix_cut <{0}, {1}>, {2})".format(center[0], center[1], + ", ".join(map(str, (r_out, r_in, dr, zmax, zmin, dz, safe_z, tool_diameter, vfeed, hfeed, direction, startside)))) x0, y0 = center - nz = int(ceil(2*(zmax - zmin)/dz)) - dz = (zmax - zmin) / nz + nz = max(int(ceil((zmax - zmin)/dz)), 2) + zi = linspace(zmax, zmin, 2 * nz + 1) + if dr > tool_diameter: FreeCAD.Console.PrintWarning("PathHelix: Warning, shortening dr to tool diameter!\n") dr = tool_diameter @@ -143,9 +145,12 @@ def helix_cut(center, r_out, r_in, dr, zmax, zmin, dz, safe_z, tool_diameter, vf out += rapid(x=x0+r,y=y0) out += rapid(z=zmax + tool_diameter) out += feed(z=zmax,f=vfeed) - for i in range(1,nz+2): - out += arc(x0-r, y0, i=-r, j=0.0, z = max(zmax - (i - 0.5) * dz, zmin), f=hfeed) - out += arc(x0+r, y0, i=r, j=0.0, z = max(zmax - i * dz, zmin), f=hfeed) + z=zmin + for i in range(1,nz+1): + out += arc(x0-r, y0, i=-r, j=0.0, z = zi[2*i-1], f=hfeed) + out += arc(x0+r, y0, i= r, j=0.0, z = zi[2*i], f=hfeed) + out += arc(x0-r, y0, i=-r, j=0.0, z = zmin, f=hfeed) + out += arc(x0+r, y0, i=r, j=0.0, z = zmin, f=hfeed) out += feed(z=zmax + tool_diameter, f=vfeed) out += rapid(z=safe_z) return out @@ -260,6 +265,7 @@ class ObjectPathHelix(object): def execute(self,obj): from Part import Circle, Cylinder, Plane + from math import sqrt if obj.Base: if not obj.Active: obj.Path = Path.Path("(helix cut operation inactive)") @@ -318,6 +324,7 @@ class ObjectPathHelix(object): drill_jobs = [] for base, cylinder in cylinders: + zsafe = cylinder.BoundBox.ZMax + obj.Clearance.Value xc, yc, zc = cylinder.Surface.Center if obj.Recursive: @@ -337,7 +344,7 @@ class ObjectPathHelix(object): r = cylinder.Surface.Radius if dz < 0: - # This is a closed hole if the face connecting to the current cylinder at next_z has + # This is a closed hole if the face connected to the current cylinder at next_z has # the cylinder's edge as its OuterWire closed = None for face in base.Shape.Faces: @@ -351,7 +358,7 @@ class ObjectPathHelix(object): if closed is None: raise Exception("Cannot determine if this cylinder is closed on the z = {0} side".format(next_z)) - jobs.append(dict(xc=xc, yc=yc, zmin=next_z, zmax=cur_z, r_out=r, r_in=0.0, closed=closed)) + jobs.append(dict(xc=xc, yc=yc, zmin=next_z, zmax=cur_z, r_out=r, r_in=0.0, closed=closed, zsafe=zsafe)) elif dz > 0: new_jobs = [] @@ -379,11 +386,15 @@ class ObjectPathHelix(object): if connected(other_edge, face): if isinstance(face.Surface, Plane): faces.append(face) + # should only be one face, = faces for edge in face.Edges: if not edge.isSame(other_edge): for base, other_cylinder in connected_cylinders(base, edge): - if other_cylinder.Surface.Center.x == xc and other_cylinder.Surface.Center.y == yc and other_cylinder.Surface.Radius < r: + xo = other_cylinder.Surface.Center.x + yo = other_cylinder.Surface.Center.y + center_dist = sqrt((xo - xc)**2 + (yo - yc)**2) + if center_dist + other_cylinder.Surface.Radius < r: cylinder = other_cylinder break @@ -409,12 +420,12 @@ class ObjectPathHelix(object): zmin = obj.FinalDepth.Value else: zmin = cylinder.BoundBox.ZMin - obj.ThroughDepth.Value - drill_jobs.append(dict(xc=xc, yc=yc, zmin=zmin, zmax=zmax, r_out=cylinder.Surface.Radius, r_in=0.0)) + drill_jobs.append(dict(xc=xc, yc=yc, zmin=zmin, zmax=zmax, r_out=cylinder.Surface.Radius, r_in=0.0, zsafe=zsafe)) for job in drill_jobs: output += helix_cut((job["xc"], job["yc"]), job["r_out"], job["r_in"], obj.DeltaR.Value, job["zmax"], job["zmin"], obj.StepDown.Value, - job["zmax"] + obj.Clearance.Value, tool.Diameter, + job["zsafe"], tool.Diameter, obj.VertFeed.Value, obj.HorizFeed.Value, obj.Direction, obj.StartSide) output += '\n' From 6ad0a1f64bb1ffc9b7944a6f9a164b5404f2f35b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lorenz=20H=C3=BCdepohl?= Date: Thu, 9 Jun 2016 23:51:50 +0200 Subject: [PATCH 5/9] PathHelix: "Cancel" and some logic in task panel - "Cancel" now restores the original values as it should. - The various mutually exclusive uses of UseStartDepth, UseFinalDepth and ThroughDepth are now reflected in the GUI by grayed-out components --- src/Mod/Path/PathScripts/PathHelix.py | 71 +++++++++++++++++++++++---- 1 file changed, 61 insertions(+), 10 deletions(-) diff --git a/src/Mod/Path/PathScripts/PathHelix.py b/src/Mod/Path/PathScripts/PathHelix.py index 3c622f055..49fb32ceb 100644 --- a/src/Mod/Path/PathScripts/PathHelix.py +++ b/src/Mod/Path/PathScripts/PathHelix.py @@ -538,6 +538,9 @@ class TaskPanel(object): layout = QtGui.QGridLayout() headerStyle = "QLabel { font-weight: bold; font-size: large; }" + grayed_out = "background-color: #d0d0d0;" + + self.previous_value = {} def addWidget(widget): row = layout.rowCount() @@ -553,43 +556,67 @@ class TaskPanel(object): heading.setStyleSheet(headerStyle) addWidget(heading) - def addQuantity(property, label, activator=None, max=None, step=None): + def addQuantity(property, label, activator=None, max=None): + self.previous_value[property] = getattr(self.obj, property) + widget = ui.createWidget("Gui::InputField") + if activator: + self.previous_value[activator] = getattr(self.obj, activator) + currently_active = getattr(self.obj, activator) label = QtGui.QCheckBox(label) def change(state): setattr(self.obj, activator, label.isChecked()) + if label.isChecked(): + widget.setStyleSheet("") + else: + widget.setStyleSheet(grayed_out) self.obj.Proxy.execute(self.obj) FreeCAD.ActiveDocument.recompute() label.stateChanged.connect(change) - label.setChecked(getattr(self.obj, activator)) + label.setChecked(currently_active) + if not currently_active: + widget.setStyleSheet(grayed_out) label.setToolTip(self.obj.getDocumentationOfProperty(activator)) else: label = QtGui.QLabel(label) label.setToolTip(self.obj.getDocumentationOfProperty(property)) - widget = ui.createWidget("Gui::InputField") + widget.setText(str(getattr(self.obj, property))) widget.setToolTip(self.obj.getDocumentationOfProperty(property)) + + if max: + # cannot use widget.setMaximum() as apparently ui.createWidget() + # returns the object up-casted to QWidget. + widget.setProperty("maximum", max) + def change(quantity): if activator: label.setChecked(True) setattr(self.obj, property, quantity) self.obj.Proxy.execute(self.obj) FreeCAD.ActiveDocument.recompute() + QtCore.QObject.connect(widget, QtCore.SIGNAL("valueChanged(const Base::Quantity &)"), change) + addWidgets(label, widget) + return label, widget def addCheckBox(property, label): + self.previous_value[property] = getattr(self.obj, property) widget = QtGui.QCheckBox(label) widget.setToolTip(self.obj.getDocumentationOfProperty(property)) + def change(state): setattr(self.obj, property, widget.isChecked()) self.obj.Proxy.execute(self.obj) FreeCAD.ActiveDocument.recompute() widget.stateChanged.connect(change) + widget.setChecked(getattr(self.obj, property)) addWidget(widget) def addEnumeration(property, label, options): + self.previous_value[property] = getattr(self.obj, property) label = QtGui.QLabel(label) label.setToolTip(self.obj.getDocumentationOfProperty(property)) widget = QtGui.QComboBox() @@ -605,22 +632,44 @@ class TaskPanel(object): heading("Drill parameters") addCheckBox("Active", "Operation is active") - addQuantity("DeltaR", "Step in Radius") + addCheckBox("Recursive", "Also mill subsequent holes") + tool = PathUtils.getTool(self.obj,self.obj.ToolNumber) + if not tool: + drmax = None + else: + drmax = tool.Diameter + addQuantity("DeltaR", "Step in Radius", max=drmax) addQuantity("StepDown", "Step in Z") addEnumeration("Direction", "Cut direction", [("Clockwise", "CW"), ("Counter-Clockwise", "CCW")]) addEnumeration("StartSide", "Start Side", [("Start from inside", "inside"), ("Start from outside", "outside")]) - addCheckBox("Recursive", "Also mill subsequent holes") heading("Cutting Depths") addQuantity("Clearance", "Clearance Distance") - addQuantity("StartDepth", "Start Depth", "UseStartDepth") - addQuantity("FinalDepth", "Final Depth", "UseFinalDepth") - addQuantity("ThroughDepth", "Through Depth") + addQuantity("StartDepth", "Absolute start height", "UseStartDepth") + + fdcheckbox, fdinput = addQuantity("FinalDepth", "Absolute final height", "UseFinalDepth") + tdlabel, tdinput = addQuantity("ThroughDepth", "Extra drill depth for open holes") heading("Feeds") addQuantity("HorizFeed", "Horizontal Feed") addQuantity("VertFeed", "Vertical Feed") + # make ThroughDepth and FinalDepth mutually exclusive + def fd_change(state): + if fdcheckbox.isChecked(): + tdinput.setStyleSheet(grayed_out) + else: + tdinput.setStyleSheet("") + fdcheckbox.stateChanged.connect(fd_change) + + def td_change(quantity): + fdcheckbox.setChecked(False) + QtCore.QObject.connect(tdinput, QtCore.SIGNAL("valueChanged(const Base::Quantity &)"), td_change) + + if obj.UseFinalDepth: + tdinput.setStyleSheet(grayed_out) + + # add widget = QtGui.QWidget() widget.setLayout(layout) self.form = widget @@ -629,12 +678,14 @@ class TaskPanel(object): return True def accept(self): - FreeCAD.Console.PrintError("accept()\n") FreeCADGui.ActiveDocument.resetEdit() FreeCADGui.Control.closeDialog() def reject(self): - FreeCAD.Console.PrintError("reject()\n") + for property in self.previous_value: + setattr(self.obj, property, self.previous_value[property]) + self.obj.Proxy.execute(self.obj) + FreeCAD.ActiveDocument.recompute() FreeCADGui.ActiveDocument.resetEdit() FreeCADGui.Control.closeDialog() From 2d6ea990359e7b60ce2bb49bcce56efcb9ade343 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lorenz=20H=C3=BCdepohl?= Date: Sat, 23 Jul 2016 20:49:49 +0200 Subject: [PATCH 6/9] PathHelix: Add GUI list with selected features --- src/Mod/Path/PathScripts/PathHelix.py | 209 ++++++++++++++++++++------ 1 file changed, 164 insertions(+), 45 deletions(-) diff --git a/src/Mod/Path/PathScripts/PathHelix.py b/src/Mod/Path/PathScripts/PathHelix.py index 49fb32ceb..0628fd972 100644 --- a/src/Mod/Path/PathScripts/PathHelix.py +++ b/src/Mod/Path/PathScripts/PathHelix.py @@ -41,6 +41,17 @@ except AttributeError: def translate(context, text, disambig=None): return QtGui.QApplication.translate(context, text, disambig) +def print_exceptions(func): + from functools import wraps + @wraps(func) + def wrapper(*args, **kwargs): + try: + return func(*args, **kwargs) + except: + import traceback + raise + return wrapper + def hollow_cylinder(cyl): """Test if this is a hollow cylinder""" from Part import Circle @@ -77,6 +88,10 @@ def z_cylinder(cyl): return False return True +def full_cylinder(cyl): + p1 = cyl.valueAt(cyl.ParameterRange[0], cyl.ParameterRange[2]) + p2 = cyl.valueAt(cyl.ParameterRange[1], cyl.ParameterRange[2]) + return fmt(p1.x) == fmt(p2.x) and fmt(p1.y) == fmt(p2.y) and p1.z == p2.z def connected(edge, face): for otheredge in face.Edges: @@ -84,6 +99,50 @@ def connected(edge, face): return True return False +def connected_cylinders(base, edge): + from Part import Cylinder + cylinders = [] + for n in range(len(base.Shape.Faces)): + face = "Face{0}".format(n+1) + subobj = base.Shape.Faces[n] + if isinstance(subobj.Surface, Cylinder): + if not connected(edge, subobj): + continue + if not z_cylinder(subobj): + continue + if not full_cylinder(subobj): + continue + cylinders.append(face) + return cylinders + +def cylinders_in_selection(): + from Part import Cylinder + selections = FreeCADGui.Selection.getSelectionEx() + + cylinders = [] + + for selection in selections: + base = selection.Object + cylinders.append((base, [])) + for feature in selection.SubElementNames: + subobj = getattr(base.Shape, feature) + if subobj.ShapeType =='Face': + if isinstance(subobj.Surface, Cylinder): + if z_cylinder(subobj): + cylinders[-1][1].append(feature) + else: + # brute force triple-loop as FreeCAD does not expose + # any topology information... + for edge in subobj.Edges: + for face in connected_cylinders(base, edge): + if hollow_cylinder(getattr(base.Shape, face)): + cylinders[-1][1].append(face) + + if subobj.ShapeType == 'Edge': + cylinders.extend(connected_cylinders(base, (feature,))) + + return cylinders + def helix_cut(center, r_out, r_in, dr, zmax, zmin, dz, safe_z, tool_diameter, vfeed, hfeed, direction, startside): """ @@ -182,7 +241,7 @@ def helix_cut(center, r_out, r_in, dr, zmax, zmin, dz, safe_z, tool_diameter, vf else: nr = max(int(ceil((r_out - r_in)/dr)), 2) radii = linspace(r_out, r_in, nr) - elif r_out < dr: + elif r_out <= 2 * dr: out += "(single helix mode)\n" radii = [r_out - tool_diameter/2] assert(radii[0] > 0) @@ -208,7 +267,6 @@ class ObjectPathHelix(object): def __init__(self,obj): # Basic - obj.addProperty("App::PropertyLinkSub","Base","Path",translate("Parent Object","The base geometry of this toolpath")) obj.addProperty("App::PropertyLinkSubList","Features","Path",translate("Features","Selected features for the drill operation")) obj.addProperty("App::PropertyBool","Active","Path",translate("Active","Set to False to disable code generation")) obj.addProperty("App::PropertyString","Comment","Path",translate("Comment","An optional comment for this profile, will appear in G-Code")) @@ -264,9 +322,13 @@ class ObjectPathHelix(object): return None def execute(self,obj): + import cProfile, pstats, StringIO + pr = cProfile.Profile() + pr.enable() + from Part import Circle, Cylinder, Plane from math import sqrt - if obj.Base: + if obj.Features: if not obj.Active: obj.Path = Path.Path("(helix cut operation inactive)") obj.ViewObject.Visibility = False @@ -287,43 +349,19 @@ class ObjectPathHelix(object): obj.Path = Path.Path("(ERROR: no tool selected for helix cut operation)") return - def connected_cylinders(base, edge): - cylinders = [] - for face in base.Shape.Faces: - if isinstance(face.Surface, Cylinder): - if connected(edge, face): - if z_cylinder(face): - cylinders.append((base, face)) - return cylinders - - cylinders = [] - - for base, feature in obj.Features: - subobj = getattr(base.Shape, feature) - if subobj.ShapeType =='Face': - if isinstance(subobj.Surface, Cylinder): - if z_cylinder(subobj): - cylinders.append((base, subobj)) - else: - # brute force triple-loop as FreeCAD does not expose - # any topology information... - for edge in subobj.Edges: - cylinders.extend(filter(lambda b_c: hollow_cylinder(b_c[1]), (connected_cylinders(base, edge)))) - - if subobj.ShapeType == 'Edge': - cylinders.extend(connected_cylinders(base, subobj)) - output = '(helix cut operation' if obj.Comment: output += ', '+ str(obj.Comment)+')\n' else: output += ')\n' - output += "G0 Z" + fmt(obj.Base[0].Shape.BoundBox.ZMax + float(obj.Clearance)) + zsafe = max(baseobj.Shape.BoundBox.ZMax for baseobj, features in obj.Features) + obj.Clearance.Value + output += "G0 Z" + fmt(zsafe) drill_jobs = [] - for base, cylinder in cylinders: + for base, feature in sum((list((obj, feature) for feature in features) for obj, features in obj.Features), []): + cylinder = getattr(base.Shape, feature) zsafe = cylinder.BoundBox.ZMax + obj.Clearance.Value xc, yc, zc = cylinder.Surface.Center @@ -390,7 +428,8 @@ class ObjectPathHelix(object): face, = faces for edge in face.Edges: if not edge.isSame(other_edge): - for base, other_cylinder in connected_cylinders(base, edge): + for other_face in connected_cylinders(base, edge): + other_cylinder = getattr(base.Shape, other_face) xo = other_cylinder.Surface.Center.x yo = other_cylinder.Surface.Center.y center_dist = sqrt((xo - xc)**2 + (yo - yc)**2) @@ -433,6 +472,15 @@ class ObjectPathHelix(object): if obj.ViewObject: obj.ViewObject.Visibility = True + pr.disable() + s = StringIO.StringIO() + sortby = 'time' #cumulative' + ps = pstats.Stats(pr, stream=s).sort_stats(sortby) + ps.print_stats(10) + FreeCAD.Console.PrintError(s.getvalue() + "\n\n") + + +taskpanels = {} class ViewProviderPathHelix(object): def __init__(self,vobj): @@ -449,6 +497,7 @@ class ViewProviderPathHelix(object): FreeCADGui.Control.closeDialog() taskpanel = TaskPanel(vobj.Object) FreeCADGui.Control.showDialog(taskpanel) + taskpanels[0] = taskpanel return True def __getstate__(self): @@ -471,15 +520,6 @@ class CommandPathHelix(object): import Path from PathScripts import PathUtils - selection = FreeCADGui.Selection.getSelectionEx() - - if not len(selection) == 1: - FreeCAD.Console.PrintError("Only considering first object for PathHelix!\n") - selection = selection[0] - - if not len(selection.SubElementNames) > 0: - FreeCAD.Console.PrintError("Select a face or circles to create helix cuts\n") - # register the transaction for the undo stack try: FreeCAD.ActiveDocument.openTransaction(translate("PathHelix","Create a helix cut")) @@ -489,8 +529,7 @@ class CommandPathHelix(object): ObjectPathHelix(obj) ViewProviderPathHelix(obj.ViewObject) - obj.Base = selection.Object - obj.Features = [(selection.Object, subobj) for subobj in selection.SubElementNames] + obj.Features = cylinders_in_selection() obj.DeltaR = 1.0 project = PathUtils.addToProject(obj) @@ -520,6 +559,8 @@ class CommandPathHelix(object): obj.VertFeed = 0.0 obj.HorizFeed = 0.0 + obj.ViewObject.startEditing() + # commit FreeCAD.ActiveDocument.commitTransaction() @@ -530,6 +571,7 @@ class CommandPathHelix(object): FreeCAD.ActiveDocument.recompute() class TaskPanel(object): + def __init__(self, obj): from Units import Quantity self.obj = obj @@ -544,7 +586,7 @@ class TaskPanel(object): def addWidget(widget): row = layout.rowCount() - layout.addWidget(widget, row, 0, columnSpan=2) + layout.addWidget(widget, row, 0, 1, 2) def addWidgets(widget1, widget2): row = layout.rowCount() @@ -630,6 +672,24 @@ class TaskPanel(object): widget.currentIndexChanged.connect(change) addWidgets(label, widget) + self.featureList = QtGui.QListWidget() + self.featureList.setMinimumHeight(200) + self.featureList.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection) + self.featureList.setDragDropMode(QtGui.QAbstractItemView.DragDrop) + self.featureList.setDefaultDropAction(QtCore.Qt.MoveAction) + self.fillFeatureList() + sm = self.featureList.selectionModel() + sm.selectionChanged.connect(self.selectFeatures) + addWidget(self.featureList) + + self.addButton = QtGui.QPushButton("Add holes") + self.addButton.clicked.connect(self.addCylinders) + + self.delButton = QtGui.QPushButton("Delete") + self.delButton.clicked.connect(self.delCylinders) + + addWidgets(self.addButton, self.delButton) + heading("Drill parameters") addCheckBox("Active", "Operation is active") addCheckBox("Recursive", "Also mill subsequent holes") @@ -648,7 +708,7 @@ class TaskPanel(object): addQuantity("StartDepth", "Absolute start height", "UseStartDepth") fdcheckbox, fdinput = addQuantity("FinalDepth", "Absolute final height", "UseFinalDepth") - tdlabel, tdinput = addQuantity("ThroughDepth", "Extra drill depth for open holes") + tdlabel, tdinput = addQuantity("ThroughDepth", "Extra drill depth\nfor open holes") heading("Feeds") addQuantity("HorizFeed", "Horizontal Feed") @@ -674,6 +734,65 @@ class TaskPanel(object): widget.setLayout(layout) self.form = widget + @print_exceptions + def addCylinders(self): + features_per_base = {} + for base, features in self.obj.Features: + features_per_base[base] = list(set(features)) + + for base, features in cylinders_in_selection(): + for feature in features: + if base in features_per_base: + if not feature in features_per_base[base]: + features_per_base[base].append(feature) + else: + features_per_base[base] = [feature] + + self.obj.Features = list(features_per_base.items()) + self.featureList.clear() + self.fillFeatureList() + self.obj.Proxy.execute(self.obj) + FreeCAD.ActiveDocument.recompute() + + @print_exceptions + def delCylinders(self): + del_features = [] + for item in self.featureList.selectedItems(): + obj, feature = item.data(QtCore.Qt.UserRole) + del_features.append((obj, feature)) + self.featureList.takeItem(self.featureList.row(item)) + + new_features = [] + for obj, features in self.obj.Features: + for feature in features: + if (obj, feature) not in del_features: + new_features.append((obj, feature)) + + FreeCAD.Console.PrintError(del_features) + FreeCAD.Console.PrintError("\n") + FreeCAD.Console.PrintError(new_features) + FreeCAD.Console.PrintError("\n") + self.obj.Features = new_features + self.obj.Proxy.execute(self.obj) + FreeCAD.ActiveDocument.recompute() + + @print_exceptions + def fillFeatureList(self): + for obj, features in self.obj.Features: + for feature in features: + radius = getattr(obj.Shape, feature).Surface.Radius + item = QtGui.QListWidgetItem() + item.setText(obj.Name + "." + feature + " ({0:.2f})".format(radius)) + item.setData(QtCore.Qt.UserRole, (obj, feature)) + self.featureList.addItem(item) + + @print_exceptions + def selectFeatures(self, selected, deselected): + FreeCADGui.Selection.clearSelection() + for item in self.featureList.selectedItems(): + obj, feature = item.data(QtCore.Qt.UserRole) + FreeCADGui.Selection.addSelection(obj, feature) + def needsFullSpace(self): return True From 32bcc0a57970bbb903204729573672378047e7f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lorenz=20H=C3=BCdepohl?= Date: Fri, 30 Dec 2016 22:32:38 +0100 Subject: [PATCH 7/9] Rebase onto current master --- src/Mod/Path/PathScripts/PathHelix.py | 231 ++++++++------------------ 1 file changed, 67 insertions(+), 164 deletions(-) diff --git a/src/Mod/Path/PathScripts/PathHelix.py b/src/Mod/Path/PathScripts/PathHelix.py index 0628fd972..cd6f8ba24 100644 --- a/src/Mod/Path/PathScripts/PathHelix.py +++ b/src/Mod/Path/PathScripts/PathHelix.py @@ -33,24 +33,17 @@ from .PathUtils import fmt """Helix Drill object and FreeCAD command""" -try: - _encoding = QtGui.QApplication.UnicodeUTF8 +if FreeCAD.GuiUp: + try: + _encoding = QtGui.QApplication.UnicodeUTF8 + def translate(context, text, disambig=None): + return QtGui.QApplication.translate(context, text, disambig, _encoding) + except AttributeError: + def translate(context, text, disambig=None): + return QtGui.QApplication.translate(context, text, disambig) +else: def translate(context, text, disambig=None): - return QtGui.QApplication.translate(context, text, disambig, _encoding) -except AttributeError: - def translate(context, text, disambig=None): - return QtGui.QApplication.translate(context, text, disambig) - -def print_exceptions(func): - from functools import wraps - @wraps(func) - def wrapper(*args, **kwargs): - try: - return func(*args, **kwargs) - except: - import traceback - raise - return wrapper + return text def hollow_cylinder(cyl): """Test if this is a hollow cylinder""" @@ -99,22 +92,6 @@ def connected(edge, face): return True return False -def connected_cylinders(base, edge): - from Part import Cylinder - cylinders = [] - for n in range(len(base.Shape.Faces)): - face = "Face{0}".format(n+1) - subobj = base.Shape.Faces[n] - if isinstance(subobj.Surface, Cylinder): - if not connected(edge, subobj): - continue - if not z_cylinder(subobj): - continue - if not full_cylinder(subobj): - continue - cylinders.append(face) - return cylinders - def cylinders_in_selection(): from Part import Cylinder selections = FreeCADGui.Selection.getSelectionEx() @@ -130,16 +107,6 @@ def cylinders_in_selection(): if isinstance(subobj.Surface, Cylinder): if z_cylinder(subobj): cylinders[-1][1].append(feature) - else: - # brute force triple-loop as FreeCAD does not expose - # any topology information... - for edge in subobj.Edges: - for face in connected_cylinders(base, edge): - if hollow_cylinder(getattr(base.Shape, face)): - cylinders[-1][1].append(face) - - if subobj.ShapeType == 'Edge': - cylinders.extend(connected_cylinders(base, (feature,))) return cylinders @@ -282,8 +249,6 @@ class ObjectPathHelix(object): obj.addProperty("App::PropertyLength", "DeltaR", "Helix Drill", translate("DeltaR", "Radius increment (must be smaller than tool diameter)")) - obj.addProperty("App::PropertyBool", "Recursive", "Helix Drill", - translate("Recursive", "If True, drill holes also in any subsequent smaller holes at the bottom of a hole")) # Depth Properties obj.addProperty("App::PropertyDistance", "Clearance", "Depths", @@ -301,12 +266,6 @@ class ObjectPathHelix(object): obj.addProperty("App::PropertyDistance", "ThroughDepth", "Depths", translate("Through Depth","Add this amount of additional cutting depth to open-ended holes. Only used if UseFinalDepth is False")) - # Feed Properties - obj.addProperty("App::PropertySpeed", "VertFeed", "Feeds", - translate("Vert Feed","Feed rate for vertical mill moves, this includes the actual arcs")) - obj.addProperty("App::PropertySpeed", "HorizFeed", "Feeds", - translate("Horiz Feed","Feed rate for horizontal mill moves, these are mostly retractions to the safe distance above the object")) - # The current tool number, read-only # this is apparently used internally, to keep track of tool chagnes obj.addProperty("App::PropertyIntegerConstraint","ToolNumber","Tool",translate("PathProfile","The current tool in use")) @@ -322,33 +281,25 @@ class ObjectPathHelix(object): return None def execute(self,obj): - import cProfile, pstats, StringIO - pr = cProfile.Profile() - pr.enable() - from Part import Circle, Cylinder, Plane from math import sqrt + if obj.Features: if not obj.Active: obj.Path = Path.Path("(helix cut operation inactive)") - obj.ViewObject.Visibility = False + if obj.ViewObject: + obj.ViewObject.Visibility = False return - if not len(obj.InList) > 0: - FreeCAD.Console.PrintError("PathHelix: Operation is not part of a project\n") - obj.Path = Path.Path("(helix cut operation not part of any project)") - obj.ViewObject.Visibility = False - return + toolload = PathUtils.getLastToolLoad(obj) - project = obj.InList[0] - obj.ToolNumber = int(PathUtils.changeTool(obj,project)) - tool = PathUtils.getTool(obj,obj.ToolNumber) - - if not tool: + if toolload is None or toolload.ToolNumber == 0: FreeCAD.Console.PrintError("PathHelix: No tool selected for helix cut operation, insert a tool change operation first\n") obj.Path = Path.Path("(ERROR: no tool selected for helix cut operation)") return + tool = PathUtils.getTool(obj, toolload.ToolNumber) + output = '(helix cut operation' if obj.Comment: output += ', '+ str(obj.Comment)+')\n' @@ -360,16 +311,24 @@ class ObjectPathHelix(object): drill_jobs = [] - for base, feature in sum((list((obj, feature) for feature in features) for obj, features in obj.Features), []): - cylinder = getattr(base.Shape, feature) - zsafe = cylinder.BoundBox.ZMax + obj.Clearance.Value - xc, yc, zc = cylinder.Surface.Center + for base, features in obj.Features: + centers = {} - if obj.Recursive: - cur_z = cylinder.BoundBox.ZMax + for feature in features: + cylinder = getattr(base.Shape, feature) + xc, yc, _ = cylinder.Surface.Center + if (xc, yc) not in centers: + centers[xc, yc] = {} + centers[xc, yc][cylinder.Surface.Radius] = cylinder + + for center, by_radius in centers.items(): + cylinders = sorted(by_radius.values(), key = lambda cyl : cyl.Surface.Radius, reverse=True) + + zsafe = max(cyl.BoundBox.ZMax for cyl in cylinders) + obj.Clearance.Value + cur_z = cylinders[0].BoundBox.ZMax jobs = [] - while cylinder: + for cylinder in cylinders: # Find other edge of current cylinder other_edge = None for edge in cylinder.Edges: @@ -396,6 +355,7 @@ class ObjectPathHelix(object): if closed is None: raise Exception("Cannot determine if this cylinder is closed on the z = {0} side".format(next_z)) + xc, yc, _ = cylinder.Surface.Center jobs.append(dict(xc=xc, yc=yc, zmin=next_z, zmax=cur_z, r_out=r, r_in=0.0, closed=closed, zsafe=zsafe)) elif dz > 0: @@ -414,28 +374,10 @@ class ObjectPathHelix(object): new_jobs.append(job) jobs = new_jobs else: - FreeCAD.Console.PrintWarning("PathHelix: Encountered cylinder with zero height\n") + FreeCAD.Console.PrintError("PathHelix: Encountered cylinder with zero height\n") break cur_z = next_z - cylinder = None - faces = [] - for face in base.Shape.Faces: - if connected(other_edge, face): - if isinstance(face.Surface, Plane): - faces.append(face) - # should only be one - face, = faces - for edge in face.Edges: - if not edge.isSame(other_edge): - for other_face in connected_cylinders(base, edge): - other_cylinder = getattr(base.Shape, other_face) - xo = other_cylinder.Surface.Center.x - yo = other_cylinder.Surface.Center.y - center_dist = sqrt((xo - xc)**2 + (yo - yc)**2) - if center_dist + other_cylinder.Surface.Radius < r: - cylinder = other_cylinder - break if obj.UseStartDepth: jobs = [job for job in jobs if job["zmin"] < obj.StartDepth.Value] @@ -450,36 +392,18 @@ class ObjectPathHelix(object): jobs[-1]["zmin"] -= obj.ThroughDepth.Value drill_jobs.extend(jobs) - else: - if obj.UseStartDepth: - zmax = obj.StartDepth.Value - else: - zmax = cylinder.BoundBox.ZMax - if obj.UseFinalDepth: - zmin = obj.FinalDepth.Value - else: - zmin = cylinder.BoundBox.ZMin - obj.ThroughDepth.Value - drill_jobs.append(dict(xc=xc, yc=yc, zmin=zmin, zmax=zmax, r_out=cylinder.Surface.Radius, r_in=0.0, zsafe=zsafe)) for job in drill_jobs: output += helix_cut((job["xc"], job["yc"]), job["r_out"], job["r_in"], obj.DeltaR.Value, job["zmax"], job["zmin"], obj.StepDown.Value, job["zsafe"], tool.Diameter, - obj.VertFeed.Value, obj.HorizFeed.Value, obj.Direction, obj.StartSide) + toolload.VertFeed.Value, toolload.HorizFeed.Value, obj.Direction, obj.StartSide) output += '\n' obj.Path = Path.Path(output) if obj.ViewObject: obj.ViewObject.Visibility = True - pr.disable() - s = StringIO.StringIO() - sortby = 'time' #cumulative' - ps = pstats.Stats(pr, stream=s).sort_stats(sortby) - ps.print_stats(10) - FreeCAD.Console.PrintError(s.getvalue() + "\n\n") - - taskpanels = {} class ViewProviderPathHelix(object): @@ -513,60 +437,52 @@ class CommandPathHelix(object): 'ToolTip': QtCore.QT_TRANSLATE_NOOP("PathHelix","Creates a helix cut from selected circles")} def IsActive(self): - return not FreeCAD.ActiveDocument is None + if FreeCAD.ActiveDocument is not None: + for o in FreeCAD.ActiveDocument.Objects: + if o.Name[:3] == "Job": + return True + return False def Activated(self): import FreeCADGui import Path from PathScripts import PathUtils - # register the transaction for the undo stack - try: - FreeCAD.ActiveDocument.openTransaction(translate("PathHelix","Create a helix cut")) - FreeCADGui.addModule("PathScripts.PathHelix") + FreeCAD.ActiveDocument.openTransaction(translate("PathHelix","Create a helix cut")) + FreeCADGui.addModule("PathScripts.PathHelix") - obj = FreeCAD.ActiveDocument.addObject("Path::FeaturePython","PathHelix") - ObjectPathHelix(obj) - ViewProviderPathHelix(obj.ViewObject) + obj = FreeCAD.ActiveDocument.addObject("Path::FeaturePython","PathHelix") + ObjectPathHelix(obj) + ViewProviderPathHelix(obj.ViewObject) - obj.Features = cylinders_in_selection() - obj.DeltaR = 1.0 + obj.Features = cylinders_in_selection() + obj.DeltaR = 1.0 - project = PathUtils.addToProject(obj) - tl = PathUtils.changeTool(obj,project) - if tl: - obj.ToolNumber = tl - tool = PathUtils.getTool(obj,obj.ToolNumber) - if tool: - # start with 25% overlap - obj.DeltaR = tool.Diameter * 0.75 + toolLoad = PathUtils.getLastToolLoad(obj) + if toolLoad is not None: + obj.ToolNumber = toolLoad.ToolNumber + tool = PathUtils.getTool(obj, toolLoad.ToolNumber) + if tool: + # start with 25% overlap + obj.DeltaR = tool.Diameter * 0.75 - obj.Active = True - obj.Comment = "" + obj.Active = True + obj.Comment = "" - obj.Direction = "CW" - obj.StartSide = "inside" + obj.Direction = "CW" + obj.StartSide = "inside" - obj.Clearance = 10.0 - obj.StepDown = 1.0 - obj.UseStartDepth = False - obj.StartDepth = 1.0 - obj.UseFinalDepth = False - obj.FinalDepth = 0.0 - obj.ThroughDepth = 0.0 - obj.Recursive = True + obj.Clearance = 10.0 + obj.StepDown = 1.0 + obj.UseStartDepth = False + obj.StartDepth = 1.0 + obj.UseFinalDepth = False + obj.FinalDepth = 0.0 + obj.ThroughDepth = 0.0 - obj.VertFeed = 0.0 - obj.HorizFeed = 0.0 + PathUtils.addToJob(obj) - obj.ViewObject.startEditing() - - # commit - FreeCAD.ActiveDocument.commitTransaction() - - except: - FreeCAD.ActiveDocument.abortTransaction() - raise + obj.ViewObject.startEditing() FreeCAD.ActiveDocument.recompute() @@ -692,7 +608,6 @@ class TaskPanel(object): heading("Drill parameters") addCheckBox("Active", "Operation is active") - addCheckBox("Recursive", "Also mill subsequent holes") tool = PathUtils.getTool(self.obj,self.obj.ToolNumber) if not tool: drmax = None @@ -710,10 +625,6 @@ class TaskPanel(object): fdcheckbox, fdinput = addQuantity("FinalDepth", "Absolute final height", "UseFinalDepth") tdlabel, tdinput = addQuantity("ThroughDepth", "Extra drill depth\nfor open holes") - heading("Feeds") - addQuantity("HorizFeed", "Horizontal Feed") - addQuantity("VertFeed", "Vertical Feed") - # make ThroughDepth and FinalDepth mutually exclusive def fd_change(state): if fdcheckbox.isChecked(): @@ -734,7 +645,6 @@ class TaskPanel(object): widget.setLayout(layout) self.form = widget - @print_exceptions def addCylinders(self): features_per_base = {} for base, features in self.obj.Features: @@ -754,7 +664,6 @@ class TaskPanel(object): self.obj.Proxy.execute(self.obj) FreeCAD.ActiveDocument.recompute() - @print_exceptions def delCylinders(self): del_features = [] for item in self.featureList.selectedItems(): @@ -768,15 +677,10 @@ class TaskPanel(object): if (obj, feature) not in del_features: new_features.append((obj, feature)) - FreeCAD.Console.PrintError(del_features) - FreeCAD.Console.PrintError("\n") - FreeCAD.Console.PrintError(new_features) - FreeCAD.Console.PrintError("\n") self.obj.Features = new_features self.obj.Proxy.execute(self.obj) FreeCAD.ActiveDocument.recompute() - @print_exceptions def fillFeatureList(self): for obj, features in self.obj.Features: for feature in features: @@ -786,7 +690,6 @@ class TaskPanel(object): item.setData(QtCore.Qt.UserRole, (obj, feature)) self.featureList.addItem(item) - @print_exceptions def selectFeatures(self, selected, deselected): FreeCADGui.Selection.clearSelection() for item in self.featureList.selectedItems(): From 72f9ec67e76511727e45426185bf54413d85f9f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lorenz=20H=C3=BCdepohl?= Date: Sat, 31 Dec 2016 00:02:22 +0100 Subject: [PATCH 8/9] Path: Fix GUI race condition in helix task panel checkboxes --- src/Mod/Path/PathScripts/PathHelix.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Mod/Path/PathScripts/PathHelix.py b/src/Mod/Path/PathScripts/PathHelix.py index cd6f8ba24..bdfcd681c 100644 --- a/src/Mod/Path/PathScripts/PathHelix.py +++ b/src/Mod/Path/PathScripts/PathHelix.py @@ -514,14 +514,15 @@ class TaskPanel(object): heading.setStyleSheet(headerStyle) addWidget(heading) - def addQuantity(property, label, activator=None, max=None): + def addQuantity(property, labelstring, activator=None, max=None): self.previous_value[property] = getattr(self.obj, property) widget = ui.createWidget("Gui::InputField") if activator: self.previous_value[activator] = getattr(self.obj, activator) currently_active = getattr(self.obj, activator) - label = QtGui.QCheckBox(label) + label = QtGui.QCheckBox(labelstring) + def change(state): setattr(self.obj, activator, label.isChecked()) if label.isChecked(): @@ -530,13 +531,14 @@ class TaskPanel(object): widget.setStyleSheet(grayed_out) self.obj.Proxy.execute(self.obj) FreeCAD.ActiveDocument.recompute() + label.stateChanged.connect(change) label.setChecked(currently_active) if not currently_active: widget.setStyleSheet(grayed_out) label.setToolTip(self.obj.getDocumentationOfProperty(activator)) else: - label = QtGui.QLabel(label) + label = QtGui.QLabel(labelstring) label.setToolTip(self.obj.getDocumentationOfProperty(property)) widget.setText(str(getattr(self.obj, property))) @@ -548,8 +550,6 @@ class TaskPanel(object): widget.setProperty("maximum", max) def change(quantity): - if activator: - label.setChecked(True) setattr(self.obj, property, quantity) self.obj.Proxy.execute(self.obj) FreeCAD.ActiveDocument.recompute() From cd3c1d574eb9ecb0fc4d18ba93cc64680ce51744 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lorenz=20H=C3=BCdepohl?= Date: Wed, 4 Jan 2017 20:08:35 +0100 Subject: [PATCH 9/9] Path: Helix taskpanel shows interdependencies of holes Instead of a flat list of holes now a tree is shown, with the various machining operations for the individual holes grouped together. --- src/Mod/Path/PathScripts/PathHelix.py | 260 ++++++++++++++++++-------- 1 file changed, 181 insertions(+), 79 deletions(-) diff --git a/src/Mod/Path/PathScripts/PathHelix.py b/src/Mod/Path/PathScripts/PathHelix.py index bdfcd681c..da2e63a72 100644 --- a/src/Mod/Path/PathScripts/PathHelix.py +++ b/src/Mod/Path/PathScripts/PathHelix.py @@ -45,34 +45,6 @@ else: def translate(context, text, disambig=None): return text -def hollow_cylinder(cyl): - """Test if this is a hollow cylinder""" - from Part import Circle - circle1 = None - line = None - for edge in cyl.Edges: - if isinstance(edge.Curve, Circle): - if circle1 is None: - circle1 = edge - else: - circle2 = edge - else: - line = edge - center = (circle1.Curve.Center + circle2.Curve.Center).scale(0.5, 0.5, 0.5) - p = (circle1.valueAt(circle1.ParameterRange[0]) + circle2.valueAt(circle1.ParameterRange[0])).scale(0.5, 0.5, 0.5) - to_outside = (p - center).normalize() - u, v = cyl.Surface.parameter(p) - normal = cyl.normalAt(u, v).normalize() - - cos_a = to_outside.dot(normal) - - if cos_a > 1.0 - 1e-12: - return False - elif cos_a < -1.0 + 1e-12: - return True - else: - raise Exception("Strange cylinder encountered, cannot determine if it is hollow or not") - def z_cylinder(cyl): """ Test if cylinder is aligned to z-Axis""" if cyl.Surface.Axis.x != 0.0: @@ -81,11 +53,6 @@ def z_cylinder(cyl): return False return True -def full_cylinder(cyl): - p1 = cyl.valueAt(cyl.ParameterRange[0], cyl.ParameterRange[2]) - p2 = cyl.valueAt(cyl.ParameterRange[1], cyl.ParameterRange[2]) - return fmt(p1.x) == fmt(p2.x) and fmt(p1.y) == fmt(p2.y) and p1.z == p2.z - def connected(edge, face): for otheredge in face.Edges: if edge.isSame(otheredge): @@ -230,6 +197,35 @@ def helix_cut(center, r_out, r_in, dr, zmax, zmin, dz, safe_z, tool_diameter, vf return out +def features_by_centers(base, features): + import scipy.spatial + features = sorted(features, + key = lambda feature : getattr(base.Shape, feature).Surface.Radius, + reverse = True) + + coordinates = [(cylinder.Surface.Center.x, cylinder.Surface.Center.y) for cylinder in + [getattr(base.Shape, feature) for feature in features]] + + tree = scipy.spatial.KDTree(coordinates) + seen = {} + + by_centers = {} + for n, feature in enumerate(features): + if n in seen: + continue + seen[n] = True + + cylinder = getattr(base.Shape, feature) + xc, yc, _ = cylinder.Surface.Center + by_centers[xc, yc] = {cylinder.Surface.Radius : feature} + + for coord in tree.query_ball_point((xc, yc), cylinder.Surface.Radius): + seen[coord] = True + cylinder = getattr(base.Shape, features[coord]) + by_centers[xc, yc][cylinder.Surface.Radius] = features[coord] + + return by_centers + class ObjectPathHelix(object): def __init__(self,obj): @@ -284,6 +280,12 @@ class ObjectPathHelix(object): from Part import Circle, Cylinder, Plane from math import sqrt + output = '(helix cut operation' + if obj.Comment: + output += ', '+ str(obj.Comment)+')\n' + else: + output += ')\n' + if obj.Features: if not obj.Active: obj.Path = Path.Path("(helix cut operation inactive)") @@ -300,30 +302,15 @@ class ObjectPathHelix(object): tool = PathUtils.getTool(obj, toolload.ToolNumber) - output = '(helix cut operation' - if obj.Comment: - output += ', '+ str(obj.Comment)+')\n' - else: - output += ')\n' - zsafe = max(baseobj.Shape.BoundBox.ZMax for baseobj, features in obj.Features) + obj.Clearance.Value output += "G0 Z" + fmt(zsafe) drill_jobs = [] for base, features in obj.Features: - centers = {} - - for feature in features: - cylinder = getattr(base.Shape, feature) - xc, yc, _ = cylinder.Surface.Center - if (xc, yc) not in centers: - centers[xc, yc] = {} - centers[xc, yc][cylinder.Surface.Radius] = cylinder - - for center, by_radius in centers.items(): - cylinders = sorted(by_radius.values(), key = lambda cyl : cyl.Surface.Radius, reverse=True) - + for center, by_radius in features_by_centers(base, features).items(): + radii = sorted(by_radius.keys(), reverse=True) + cylinders = map(lambda radius: getattr(base.Shape, by_radius[radius]), radii) zsafe = max(cyl.BoundBox.ZMax for cyl in cylinders) + obj.Clearance.Value cur_z = cylinders[0].BoundBox.ZMax jobs = [] @@ -400,9 +387,9 @@ class ObjectPathHelix(object): toolload.VertFeed.Value, toolload.HorizFeed.Value, obj.Direction, obj.StartSide) output += '\n' - obj.Path = Path.Path(output) - if obj.ViewObject: - obj.ViewObject.Visibility = True + obj.Path = Path.Path(output) + if obj.ViewObject: + obj.ViewObject.Visibility = True taskpanels = {} @@ -486,6 +473,28 @@ class CommandPathHelix(object): FreeCAD.ActiveDocument.recompute() +def print_exceptions(func): + from functools import wraps + import traceback + import sys + @wraps(func) + def wrapper(*args, **kwargs): + try: + return func(*args, **kwargs) + except: + ex_type, ex, tb = sys.exc_info() + FreeCAD.Console.PrintError("".join(traceback.format_exception(ex_type, ex, tb)) + "\n") + raise + return wrapper + +def print_all_exceptions(cls): + for entry in dir(cls): + obj = getattr(cls, entry) + if not entry.startswith("__") and hasattr(obj, "__call__"): + setattr(cls, entry, print_exceptions(obj)) + return cls + +@print_all_exceptions class TaskPanel(object): def __init__(self, obj): @@ -588,15 +597,16 @@ class TaskPanel(object): widget.currentIndexChanged.connect(change) addWidgets(label, widget) - self.featureList = QtGui.QListWidget() - self.featureList.setMinimumHeight(200) - self.featureList.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection) - self.featureList.setDragDropMode(QtGui.QAbstractItemView.DragDrop) - self.featureList.setDefaultDropAction(QtCore.Qt.MoveAction) - self.fillFeatureList() - sm = self.featureList.selectionModel() + self.featureTree = QtGui.QTreeWidget() + self.featureTree.setMinimumHeight(200) + self.featureTree.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection) + #self.featureTree.setDragDropMode(QtGui.QAbstractItemView.DragDrop) + #self.featureTree.setDefaultDropAction(QtCore.Qt.MoveAction) + self.fillFeatureTree() + sm = self.featureTree.selectionModel() sm.selectionChanged.connect(self.selectFeatures) - addWidget(self.featureList) + addWidget(self.featureTree) + self.featureTree.expandAll() self.addButton = QtGui.QPushButton("Add holes") self.addButton.clicked.connect(self.addCylinders) @@ -659,17 +669,65 @@ class TaskPanel(object): features_per_base[base] = [feature] self.obj.Features = list(features_per_base.items()) - self.featureList.clear() - self.fillFeatureList() + self.featureTree.clear() + self.fillFeatureTree() + self.featureTree.expandAll() self.obj.Proxy.execute(self.obj) FreeCAD.ActiveDocument.recompute() def delCylinders(self): del_features = [] - for item in self.featureList.selectedItems(): - obj, feature = item.data(QtCore.Qt.UserRole) - del_features.append((obj, feature)) - self.featureList.takeItem(self.featureList.row(item)) + + def delete_feature(item, base=None): + kind, feature = item.data(0, QtCore.Qt.UserRole) + assert(kind == "feature") + + if base is None: + base_item = item.parent().parent() + _, base = base_item.data(0, QtCore.Qt.UserRole) + + del_features.append((base, feature)) + item.parent().takeChild(item.parent().indexOfChild(item)) + + def delete_hole(item, base=None): + kind, center = item.data(0, QtCore.Qt.UserRole) + assert(kind == "hole") + + if base is None: + base_item = item.parent() + _, base = base_item.data(0, QtCore.Qt.UserRole) + + for i in reversed(range(item.childCount())): + delete_feature(item.child(i), base=base) + item.parent().takeChild(item.parent().indexOfChild(item)) + + def delete_base(item): + kind, base = item.data(0, QtCore.Qt.UserRole) + assert(kind == "base") + for i in reversed(range(item.childCount())): + delete_hole(item.child(i), base=base) + self.featureTree.takeTopLevelItem(self.featureTree.indexOfTopLevelItem(item)) + + for item in self.featureTree.selectedItems(): + kind, info = item.data(0, QtCore.Qt.UserRole) + if kind == "base": + delete_base(item) + elif kind == "hole": + parent = item.parent() + delete_hole(item) + if parent.childCount() == 0: + self.featureTree.takeTopLevelItem(self.featureTree.indexOfTopLevelItem(parent)) + elif kind =="feature": + parent = item.parent() + delete_feature(item) + if parent.childCount() == 0: + parent.parent().takeChild(parent.parent().indexOfChild(parent)) + else: + raise Exception("No such item kind: {0}".format(kind)) + + for base, features in cylinders_in_selection(): + for feature in features: + del_features.append((base, feature)) new_features = [] for obj, features in self.obj.Features: @@ -681,20 +739,64 @@ class TaskPanel(object): self.obj.Proxy.execute(self.obj) FreeCAD.ActiveDocument.recompute() - def fillFeatureList(self): - for obj, features in self.obj.Features: - for feature in features: - radius = getattr(obj.Shape, feature).Surface.Radius - item = QtGui.QListWidgetItem() - item.setText(obj.Name + "." + feature + " ({0:.2f})".format(radius)) - item.setData(QtCore.Qt.UserRole, (obj, feature)) - self.featureList.addItem(item) + def fillFeatureTree(self): + for base, features in self.obj.Features: + base_item = QtGui.QTreeWidgetItem() + base_item.setText(0, base.Name) + base_item.setData(0, QtCore.Qt.UserRole, ("base", base)) + self.featureTree.addTopLevelItem(base_item) + for center, by_radius in features_by_centers(base, features).items(): + hole_item = QtGui.QTreeWidgetItem() + hole_item.setText(0, "Hole at ({0[0]:.2f}, {0[1]:.2f})".format(center)) + hole_item.setData(0, QtCore.Qt.UserRole, ("hole", center)) + base_item.addChild(hole_item) + for radius in sorted(by_radius.keys(), reverse=True): + feature = by_radius[radius] + cylinder = getattr(base.Shape, feature) + cyl_item = QtGui.QTreeWidgetItem() + cyl_item.setText(0, "Diameter {0:.2f}, {1}".format(2 * cylinder.Surface.Radius, feature)) + cyl_item.setData(0, QtCore.Qt.UserRole, ("feature", feature)) + hole_item.addChild(cyl_item) def selectFeatures(self, selected, deselected): FreeCADGui.Selection.clearSelection() - for item in self.featureList.selectedItems(): - obj, feature = item.data(QtCore.Qt.UserRole) - FreeCADGui.Selection.addSelection(obj, feature) + def select_feature(item, base=None): + kind, feature = item.data(0, QtCore.Qt.UserRole) + assert(kind == "feature") + + if base is None: + base_item = item.parent().parent() + _, base = base_item.data(0, QtCore.Qt.UserRole) + + FreeCADGui.Selection.addSelection(base, feature) + + def select_hole(item, base=None): + kind, center = item.data(0, QtCore.Qt.UserRole) + assert(kind == "hole") + + if base is None: + base_item = item.parent() + _, base = base_item.data(0, QtCore.Qt.UserRole) + + for i in range(item.childCount()): + select_feature(item.child(i), base=base) + + def select_base(item): + kind, base = item.data(0, QtCore.Qt.UserRole) + assert(kind == "base") + + for i in range(item.childCount()): + select_hole(item.child(i), base=base) + + for item in self.featureTree.selectedItems(): + kind, info = item.data(0, QtCore.Qt.UserRole) + + if kind == "base": + select_base(item) + elif kind == "hole": + select_hole(item) + elif kind == "feature": + select_feature(item) def needsFullSpace(self): return True