black reformat

black reformat for PathCommands

Black reform PathCopy

Black reform PathComment

Black reformat PathCustom/gui

Black reformat PathUtil/PathUtils

Black reformat PathHop

Black reformat PathGeom

Black reformat

Black reformat

Black reformat

Black reformat

Black reformat

black reformat

Black reformat

black
This commit is contained in:
sliptonic
2021-12-17 17:15:13 -06:00
parent c055ac4619
commit 0fae42230c
18 changed files with 1728 additions and 822 deletions

View File

@@ -35,38 +35,113 @@ __doc__ = """Path Array object and FreeCAD command"""
def translate(context, text, disambig=None):
return QtCore.QCoreApplication.translate(context, text, disambig)
class ObjectArray:
class ObjectArray:
def __init__(self, obj):
obj.addProperty("App::PropertyLinkList", "Base",
"Path", QtCore.QT_TRANSLATE_NOOP("App::Property","The path(s) to array"))
obj.addProperty("App::PropertyEnumeration", "Type",
"Path", QtCore.QT_TRANSLATE_NOOP("App::Property", "Pattern method"))
obj.addProperty("App::PropertyVectorDistance", "Offset",
"Path", QtCore.QT_TRANSLATE_NOOP("App::Property","The spacing between the array copies in Linear pattern"))
obj.addProperty("App::PropertyInteger", "CopiesX",
"Path", QtCore.QT_TRANSLATE_NOOP("App::Property","The number of copies in X direction in Linear pattern"))
obj.addProperty("App::PropertyInteger", "CopiesY",
"Path", QtCore.QT_TRANSLATE_NOOP("App::Property","The number of copies in Y direction in Linear pattern"))
obj.addProperty("App::PropertyAngle", "Angle",
"Path", QtCore.QT_TRANSLATE_NOOP("App::Property","Total angle in Polar pattern"))
obj.addProperty("App::PropertyInteger", "Copies",
"Path", QtCore.QT_TRANSLATE_NOOP("App::Property","The number of copies in Linear 1D and Polar pattern"))
obj.addProperty("App::PropertyVector", "Centre",
"Path", QtCore.QT_TRANSLATE_NOOP("App::Property","The centre of rotation in Polar pattern"))
obj.addProperty("App::PropertyBool", "SwapDirection",
"Path", QtCore.QT_TRANSLATE_NOOP("App::Property","Make copies in X direction before Y in Linear 2D pattern"))
obj.addProperty("App::PropertyInteger", "JitterPercent",
"Path", QtCore.QT_TRANSLATE_NOOP("App::Property","Percent of copies to randomly offset"))
obj.addProperty("App::PropertyVectorDistance", "JitterMagnitude",
"Path", QtCore.QT_TRANSLATE_NOOP("App::Property","Maximum random offset of copies"))
obj.addProperty("App::PropertyLink", "ToolController",
"Path", QtCore.QT_TRANSLATE_NOOP("App::Property", "The tool controller that will be used to calculate the path"))
obj.addProperty("App::PropertyBool", "Active",
"Path", QtCore.QT_TRANSLATE_NOOP("PathOp", "Make False, to prevent operation from generating code"))
obj.addProperty(
"App::PropertyLinkList",
"Base",
"Path",
QtCore.QT_TRANSLATE_NOOP("App::Property", "The path(s) to array"),
)
obj.addProperty(
"App::PropertyEnumeration",
"Type",
"Path",
QtCore.QT_TRANSLATE_NOOP("App::Property", "Pattern method"),
)
obj.addProperty(
"App::PropertyVectorDistance",
"Offset",
"Path",
QtCore.QT_TRANSLATE_NOOP(
"App::Property",
"The spacing between the array copies in Linear pattern",
),
)
obj.addProperty(
"App::PropertyInteger",
"CopiesX",
"Path",
QtCore.QT_TRANSLATE_NOOP(
"App::Property", "The number of copies in X direction in Linear pattern"
),
)
obj.addProperty(
"App::PropertyInteger",
"CopiesY",
"Path",
QtCore.QT_TRANSLATE_NOOP(
"App::Property", "The number of copies in Y direction in Linear pattern"
),
)
obj.addProperty(
"App::PropertyAngle",
"Angle",
"Path",
QtCore.QT_TRANSLATE_NOOP("App::Property", "Total angle in Polar pattern"),
)
obj.addProperty(
"App::PropertyInteger",
"Copies",
"Path",
QtCore.QT_TRANSLATE_NOOP(
"App::Property", "The number of copies in Linear 1D and Polar pattern"
),
)
obj.addProperty(
"App::PropertyVector",
"Centre",
"Path",
QtCore.QT_TRANSLATE_NOOP(
"App::Property", "The centre of rotation in Polar pattern"
),
)
obj.addProperty(
"App::PropertyBool",
"SwapDirection",
"Path",
QtCore.QT_TRANSLATE_NOOP(
"App::Property",
"Make copies in X direction before Y in Linear 2D pattern",
),
)
obj.addProperty(
"App::PropertyInteger",
"JitterPercent",
"Path",
QtCore.QT_TRANSLATE_NOOP(
"App::Property", "Percent of copies to randomly offset"
),
)
obj.addProperty(
"App::PropertyVectorDistance",
"JitterMagnitude",
"Path",
QtCore.QT_TRANSLATE_NOOP(
"App::Property", "Maximum random offset of copies"
),
)
obj.addProperty(
"App::PropertyLink",
"ToolController",
"Path",
QtCore.QT_TRANSLATE_NOOP(
"App::Property",
"The tool controller that will be used to calculate the path",
),
)
obj.addProperty(
"App::PropertyBool",
"Active",
"Path",
QtCore.QT_TRANSLATE_NOOP(
"PathOp", "Make False, to prevent operation from generating code"
),
)
obj.Active = True
obj.Type = ['Linear1D', 'Linear2D', 'Polar']
obj.Type = ["Linear1D", "Linear2D", "Polar"]
self.setEditorModes(obj)
obj.Proxy = self
@@ -78,26 +153,26 @@ class ObjectArray:
return None
def setEditorModes(self, obj):
if obj.Type == 'Linear1D':
if obj.Type == "Linear1D":
angleMode = centreMode = copiesXMode = copiesYMode = swapDirectionMode = 2
copiesMode = offsetMode = 0
elif obj.Type == 'Linear2D':
elif obj.Type == "Linear2D":
angleMode = copiesMode = centreMode = 2
copiesXMode = copiesYMode = offsetMode = swapDirectionMode = 0
elif obj.Type == 'Polar':
elif obj.Type == "Polar":
angleMode = copiesMode = centreMode = 0
copiesXMode = copiesYMode = offsetMode = swapDirectionMode = 2
obj.setEditorMode('Angle', angleMode)
obj.setEditorMode('Copies', copiesMode)
obj.setEditorMode('Centre', centreMode)
obj.setEditorMode('CopiesX', copiesXMode)
obj.setEditorMode('CopiesY', copiesYMode)
obj.setEditorMode('Offset', offsetMode)
obj.setEditorMode('SwapDirection', swapDirectionMode)
obj.setEditorMode('JitterPercent', 0)
obj.setEditorMode('JitterMagnitude', 0)
obj.setEditorMode('ToolController', 2)
obj.setEditorMode("Angle", angleMode)
obj.setEditorMode("Copies", copiesMode)
obj.setEditorMode("Centre", centreMode)
obj.setEditorMode("CopiesX", copiesXMode)
obj.setEditorMode("CopiesY", copiesYMode)
obj.setEditorMode("Offset", offsetMode)
obj.setEditorMode("SwapDirection", swapDirectionMode)
obj.setEditorMode("JitterPercent", 0)
obj.setEditorMode("JitterMagnitude", 0)
obj.setEditorMode("ToolController", 2)
def onChanged(self, obj, prop):
if prop == "Type":
@@ -107,31 +182,41 @@ class ObjectArray:
"""onDocumentRestored(obj) ... Called automatically when document is restored."""
if not hasattr(obj, "Active"):
obj.addProperty("App::PropertyBool", "Active",
"Path", QtCore.QT_TRANSLATE_NOOP("PathOp", "Make False, to prevent operation from generating code"))
obj.addProperty(
"App::PropertyBool",
"Active",
"Path",
QtCore.QT_TRANSLATE_NOOP(
"PathOp", "Make False, to prevent operation from generating code"
),
)
obj.Active = True
self.setEditorModes(obj)
def rotatePath(self, path, angle, centre):
'''
Rotates Path around given centre vector
Only X and Y is considered
'''
CmdMoveRapid = ['G0', 'G00']
CmdMoveStraight = ['G1', 'G01']
CmdMoveCW = ['G2', 'G02']
CmdMoveCCW = ['G3', 'G03']
CmdDrill = ['G81', 'G82', 'G83']
CmdMoveArc = CmdMoveCW + CmdMoveCCW
CmdMove = CmdMoveStraight + CmdMoveArc
"""
Rotates Path around given centre vector
Only X and Y is considered
"""
CmdMoveRapid = ["G0", "G00"]
CmdMoveStraight = ["G1", "G01"]
CmdMoveCW = ["G2", "G02"]
CmdMoveCCW = ["G3", "G03"]
CmdDrill = ["G81", "G82", "G83"]
CmdMoveArc = CmdMoveCW + CmdMoveCCW
CmdMove = CmdMoveStraight + CmdMoveArc
commands = []
ang = angle / 180 * math.pi
currX = 0
currY = 0
for cmd in path.Commands:
if (cmd.Name in CmdMoveRapid) or (cmd.Name in CmdMove) or (cmd.Name in CmdDrill):
if (
(cmd.Name in CmdMoveRapid)
or (cmd.Name in CmdMove)
or (cmd.Name in CmdDrill)
):
params = cmd.Parameters
x = params.get("X")
if x is None:
@@ -151,7 +236,7 @@ class ObjectArray:
ny = y * math.cos(ang) + x * math.sin(ang)
# "move" the centre back and update
params.update({'X': nx + centre.x, 'Y': ny + centre.y})
params.update({"X": nx + centre.x, "Y": ny + centre.y})
# Arcs need to have the I and J params rotated as well
if cmd.Name in CmdMoveArc:
@@ -164,7 +249,7 @@ class ObjectArray:
ni = i * math.cos(ang) - j * math.sin(ang)
nj = j * math.cos(ang) + i * math.sin(ang)
params.update({'I': ni, 'J': nj})
params.update({"I": ni, "J": nj})
cmd.Parameters = params
commands.append(cmd)
@@ -184,15 +269,26 @@ class ObjectArray:
obj.ToolController = base[0].ToolController
# Do not generate paths and clear current Path data if operation not
# Do not generate paths and clear current Path data if operation not
if not obj.Active:
if obj.Path:
obj.Path = Path.Path()
return
pa = PathArray(obj.Base, obj.Type, obj.Copies, obj.Offset,
obj.CopiesX, obj.CopiesY, obj.Angle, obj.Centre, obj.SwapDirection,
obj.JitterMagnitude, obj.JitterPercent, obj.Name)
pa = PathArray(
obj.Base,
obj.Type,
obj.Copies,
obj.Offset,
obj.CopiesX,
obj.CopiesY,
obj.Angle,
obj.Centre,
obj.SwapDirection,
obj.JitterMagnitude,
obj.JitterPercent,
obj.Name,
)
obj.Path = pa.getPath()
@@ -201,10 +297,21 @@ class PathArray:
This class receives one or more base operations and repeats those operations
at set intervals based upon array type requested and the related settings for that type."""
def __init__(self, baseList, arrayType, copies, offsetVector,
copiesX, copiesY, angle, centre, swapDirection,
jitterMagnitude=FreeCAD.Vector(0, 0, 0), jitterPercent=0,
seed='FreeCAD'):
def __init__(
self,
baseList,
arrayType,
copies,
offsetVector,
copiesX,
copiesY,
angle,
centre,
swapDirection,
jitterMagnitude=FreeCAD.Vector(0, 0, 0),
jitterPercent=0,
seed="FreeCAD",
):
self.baseList = list()
self.arrayType = arrayType # ['Linear1D', 'Linear2D', 'Polar']
self.copies = copies
@@ -230,10 +337,16 @@ class PathArray:
Returns the position argument with a random vector shift applied."""
if self.jitterPercent == 0:
pass
elif random.randint(0,100) < self.jitterPercent:
pos.x = pos.x + random.uniform(-self.jitterMagnitude.x, self.jitterMagnitude.y)
pos.y = pos.y + random.uniform(-self.jitterMagnitude.y, self.jitterMagnitude.y)
pos.z = pos.z + random.uniform(-self.jitterMagnitude.z, self.jitterMagnitude.z)
elif random.randint(0, 100) < self.jitterPercent:
pos.x = pos.x + random.uniform(
-self.jitterMagnitude.x, self.jitterMagnitude.y
)
pos.y = pos.y + random.uniform(
-self.jitterMagnitude.y, self.jitterMagnitude.y
)
pos.z = pos.z + random.uniform(
-self.jitterMagnitude.z, self.jitterMagnitude.z
)
return pos
# Public method
@@ -255,32 +368,48 @@ class PathArray:
return
if b.ToolController != base[0].ToolController:
# this may be important if Job output is split by tool controller
PathLog.warning(translate("PathArray", "Arrays of paths having different tool controllers are handled according to the tool controller of the first path."))
PathLog.warning(
translate(
"PathArray",
"Arrays of paths having different tool controllers are handled according to the tool controller of the first path.",
)
)
# build copies
output = ""
random.seed(self.seed)
if self.arrayType == 'Linear1D':
if self.arrayType == "Linear1D":
for i in range(self.copies):
pos = FreeCAD.Vector(self.offsetVector.x * (i + 1), self.offsetVector.y * (i + 1), self.offsetVector.z * (i + 1))
pos = FreeCAD.Vector(
self.offsetVector.x * (i + 1),
self.offsetVector.y * (i + 1),
self.offsetVector.z * (i + 1),
)
pos = self._calculateJitter(pos)
for b in base:
pl = FreeCAD.Placement()
pl.move(pos)
np = Path.Path([cm.transform(pl)
for cm in b.Path.Commands])
np = Path.Path([cm.transform(pl) for cm in b.Path.Commands])
output += np.toGCode()
elif self.arrayType == 'Linear2D':
elif self.arrayType == "Linear2D":
if self.swapDirection:
for i in range(self.copiesY + 1):
for j in range(self.copiesX + 1):
if (i % 2) == 0:
pos = FreeCAD.Vector(self.offsetVector.x * j, self.offsetVector.y * i, self.offsetVector.z * i)
pos = FreeCAD.Vector(
self.offsetVector.x * j,
self.offsetVector.y * i,
self.offsetVector.z * i,
)
else:
pos = FreeCAD.Vector(self.offsetVector.x * (self.copiesX - j), self.offsetVector.y * i, self.offsetVector.z * i)
pos = FreeCAD.Vector(
self.offsetVector.x * (self.copiesX - j),
self.offsetVector.y * i,
self.offsetVector.z * i,
)
pos = self._calculateJitter(pos)
for b in base:
@@ -288,15 +417,25 @@ class PathArray:
# do not process the index 0,0. It will be processed by the base Paths themselves
if not (i == 0 and j == 0):
pl.move(pos)
np = Path.Path([cm.transform(pl) for cm in b.Path.Commands])
np = Path.Path(
[cm.transform(pl) for cm in b.Path.Commands]
)
output += np.toGCode()
else:
for i in range(self.copiesX + 1):
for j in range(self.copiesY + 1):
if (i % 2) == 0:
pos = FreeCAD.Vector(self.offsetVector.x * i, self.offsetVector.y * j, self.offsetVector.z * i)
pos = FreeCAD.Vector(
self.offsetVector.x * i,
self.offsetVector.y * j,
self.offsetVector.z * i,
)
else:
pos = FreeCAD.Vector(self.offsetVector.x * i, self.offsetVector.y * (self.copiesY - j), self.offsetVector.z * i)
pos = FreeCAD.Vector(
self.offsetVector.x * i,
self.offsetVector.y * (self.copiesY - j),
self.offsetVector.z * i,
)
pos = self._calculateJitter(pos)
for b in base:
@@ -304,7 +443,9 @@ class PathArray:
# do not process the index 0,0. It will be processed by the base Paths themselves
if not (i == 0 and j == 0):
pl.move(pos)
np = Path.Path([cm.transform(pl) for cm in b.Path.Commands])
np = Path.Path(
[cm.transform(pl) for cm in b.Path.Commands]
)
output += np.toGCode()
# Eif
else:
@@ -321,7 +462,6 @@ class PathArray:
class ViewProviderArray:
def __init__(self, vobj):
self.Object = vobj.Object
vobj.Proxy = self
@@ -345,11 +485,14 @@ class ViewProviderArray:
class CommandPathArray:
def GetResources(self):
return {'Pixmap': 'Path_Array',
'MenuText': QtCore.QT_TRANSLATE_NOOP("Path_Array", "Array"),
'ToolTip': QtCore.QT_TRANSLATE_NOOP("Path_Array", "Creates an array from selected path(s)")}
return {
"Pixmap": "Path_Array",
"MenuText": QtCore.QT_TRANSLATE_NOOP("Path_Array", "Array"),
"ToolTip": QtCore.QT_TRANSLATE_NOOP(
"Path_Array", "Creates an array from selected path(s)"
),
}
def IsActive(self):
if bool(FreeCADGui.Selection.getSelection()) is False:
@@ -357,7 +500,7 @@ class CommandPathArray:
try:
obj = FreeCADGui.Selection.getSelectionEx()[0].Object
return isinstance(obj.Proxy, PathScripts.PathOp.ObjectOp)
except(IndexError, AttributeError):
except (IndexError, AttributeError):
return False
def Activated(self):
@@ -366,9 +509,13 @@ class CommandPathArray:
selection = FreeCADGui.Selection.getSelection()
for sel in selection:
if not(sel.isDerivedFrom("Path::Feature")):
if not (sel.isDerivedFrom("Path::Feature")):
FreeCAD.Console.PrintError(
translate("Path_Array", "Arrays can be created only from Path operations.")+"\n")
translate(
"Path_Array", "Arrays can be created only from Path operations."
)
+ "\n"
)
return
# if everything is ok, execute and register the transaction in the
@@ -377,19 +524,23 @@ class CommandPathArray:
FreeCADGui.addModule("PathScripts.PathArray")
FreeCADGui.addModule("PathScripts.PathUtils")
FreeCADGui.doCommand('obj = FreeCAD.ActiveDocument.addObject("Path::FeaturePython","Array")')
FreeCADGui.doCommand(
'obj = FreeCAD.ActiveDocument.addObject("Path::FeaturePython","Array")'
)
FreeCADGui.doCommand('PathScripts.PathArray.ObjectArray(obj)')
FreeCADGui.doCommand("PathScripts.PathArray.ObjectArray(obj)")
baseString = "[%s]" % ','.join(["FreeCAD.ActiveDocument.%s" % sel.Name for sel in selection])
FreeCADGui.doCommand('obj.Base = %s' % baseString)
baseString = "[%s]" % ",".join(
["FreeCAD.ActiveDocument.%s" % sel.Name for sel in selection]
)
FreeCADGui.doCommand("obj.Base = %s" % baseString)
FreeCADGui.doCommand('obj.ViewObject.Proxy = 0')
FreeCADGui.doCommand('PathScripts.PathUtils.addToJob(obj)')
FreeCADGui.doCommand("obj.ViewObject.Proxy = 0")
FreeCADGui.doCommand("PathScripts.PathUtils.addToJob(obj)")
FreeCAD.ActiveDocument.commitTransaction()
FreeCAD.ActiveDocument.recompute()
if FreeCAD.GuiUp:
# register the FreeCAD command
FreeCADGui.addCommand('Path_Array', CommandPathArray())
FreeCADGui.addCommand("Path_Array", CommandPathArray())