PathHelix: fix most of the PEP8 issues

Dealt with most issues expect for overly long lines
This commit is contained in:
Lorenz Hüdepohl 2017-01-06 15:50:21 +01:00
parent ec5faafd86
commit 0de0d8f930

View File

@ -22,29 +22,35 @@
# * *
# ***************************************************************************
import FreeCAD, Path
from . import PathUtils
from .PathUtils import fmt
import FreeCAD
import 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"""
if FreeCAD.GuiUp:
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def translate(context, text, disambig=None):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
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 text
def z_cylinder(cyl):
""" Test if cylinder is aligned to z-Axis"""
if cyl.Surface.Axis.x != 0.0:
@ -53,12 +59,14 @@ def z_cylinder(cyl):
return False
return True
def connected(edge, face):
for otheredge in face.Edges:
if edge.isSame(otheredge):
return True
return False
def cylinders_in_selection():
from Part import Cylinder
selections = FreeCADGui.Selection.getSelectionEx()
@ -158,7 +166,7 @@ def helix_cut(center, r_out, r_in, dr, zmax, zmin, dz, safe_z, tool_diameter, vf
msg = "r_out - r_in = {0} is < tool diameter of {1}".format(r_out - r_in, tool_diameter)
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"]:
elif startside not in ["inside", "outside"]:
msg = "Invalid value for parameter 'startside'"
if msg:
@ -197,6 +205,7 @@ 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):
try:
from scipy.spatial import KDTree
@ -230,13 +239,17 @@ def features_by_centers(base, features):
return by_centers
class ObjectPathHelix(object):
def __init__(self, obj):
# Basic
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"))
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",
@ -264,11 +277,13 @@ class ObjectPathHelix(object):
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"))
translate("Through Depth", "Add this amount of additional cutting depth "
"to open-ended holes. Only used if UseFinalDepth is False"))
# 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.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
@ -332,8 +347,9 @@ class ObjectPathHelix(object):
r = cylinder.Surface.Radius
if dz < 0:
# This is a closed hole if the face connected to the current cylinder at next_z has
# the cylinder's edge as its OuterWire
# 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:
if connected(other_edge, face) and not face.isSame(cylinder.Faces[0]):
@ -347,7 +363,9 @@ class ObjectPathHelix(object):
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))
jobs.append(dict(xc=xc, yc=yc,
zmin=next_z, zmax=cur_z, zsafe=zsafe,
r_out=r, r_in=0.0, closed=closed))
elif dz > 0:
new_jobs = []
@ -388,14 +406,14 @@ class ObjectPathHelix(object):
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,
toolload.VertFeed.Value, toolload.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
taskpanels = {}
class ViewProviderPathHelix(object):
def __init__(self, vobj):
@ -412,7 +430,6 @@ class ViewProviderPathHelix(object):
FreeCADGui.Control.closeDialog()
taskpanel = TaskPanel(vobj.Object)
FreeCADGui.Control.showDialog(taskpanel)
taskpanels[0] = taskpanel
return True
def __getstate__(self):
@ -421,6 +438,7 @@ class ViewProviderPathHelix(object):
def __setstate__(self, state):
return None
class CommandPathHelix(object):
def GetResources(self):
return {'Pixmap': 'Path-Helix',
@ -477,10 +495,12 @@ 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:
@ -489,8 +509,10 @@ def print_exceptions(func):
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)
@ -498,6 +520,7 @@ def print_all_exceptions(cls):
setattr(cls, entry, print_exceptions(obj))
return cls
@print_all_exceptions
class TaskPanel(object):
@ -594,10 +617,12 @@ class TaskPanel(object):
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)
@ -629,8 +654,10 @@ class TaskPanel(object):
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")])
addEnumeration("Direction", "Cut direction",
[("Clockwise", "CW"), ("Counter-Clockwise", "CCW")])
addEnumeration("StartSide", "Start Side",
[("Start from inside", "inside"), ("Start from outside", "outside")])
heading("Cutting Depths")
addQuantity("Clearance", "Clearance Distance")
@ -667,7 +694,7 @@ class TaskPanel(object):
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]:
if feature not in features_per_base[base]:
features_per_base[base].append(feature)
else:
features_per_base[base] = [feature]
@ -758,12 +785,14 @@ class TaskPanel(object):
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.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()
def select_feature(item, base=None):
kind, feature = item.data(0, QtCore.Qt.UserRole)
assert(kind == "feature")