From f5f2f9558798d181b95cf2812cd0ad4e231434d7 Mon Sep 17 00:00:00 2001 From: Russell Johnson <47639332+Russ4262@users.noreply.github.com> Date: Thu, 10 Feb 2022 23:30:28 -0600 Subject: [PATCH] Path: Fix translation issues for Slot operation --- src/Mod/Path/PathScripts/PathSlot.py | 928 +++++++++++++++--------- src/Mod/Path/PathScripts/PathSlotGui.py | 166 +++-- 2 files changed, 682 insertions(+), 412 deletions(-) diff --git a/src/Mod/Path/PathScripts/PathSlot.py b/src/Mod/Path/PathScripts/PathSlot.py index bbf99d345b..fac268d809 100644 --- a/src/Mod/Path/PathScripts/PathSlot.py +++ b/src/Mod/Path/PathScripts/PathSlot.py @@ -39,48 +39,57 @@ import math # lazily loaded modules from lazy_loader.lazy_loader import LazyLoader -Part = LazyLoader('Part', globals(), 'Part') -Arcs = LazyLoader('draftgeoutils.arcs', globals(), 'draftgeoutils.arcs') -PathGeom = LazyLoader('PathScripts.PathGeom', globals(), 'PathScripts.PathGeom') + +Part = LazyLoader("Part", globals(), "Part") +Arcs = LazyLoader("draftgeoutils.arcs", globals(), "draftgeoutils.arcs") +PathGeom = LazyLoader("PathScripts.PathGeom", globals(), "PathScripts.PathGeom") if FreeCAD.GuiUp: - FreeCADGui = LazyLoader('FreeCADGui', globals(), 'FreeCADGui') + FreeCADGui = LazyLoader("FreeCADGui", globals(), "FreeCADGui") -# Qt translation handling -def translate(context, text, disambig=None): - return QtCore.QCoreApplication.translate(context, text, disambig) +translate = FreeCAD.Qt.translate -PathLog.setLevel(PathLog.Level.INFO, PathLog.thisModule()) -# PathLog.trackModule(PathLog.thisModule()) +if False: + PathLog.setLevel(PathLog.Level.DEBUG, PathLog.thisModule()) + PathLog.trackModule(PathLog.thisModule()) +else: + PathLog.setLevel(PathLog.Level.INFO, PathLog.thisModule()) class ObjectSlot(PathOp.ObjectOp): - '''Proxy object for Slot operation.''' + """Proxy object for Slot operation.""" def opFeatures(self, obj): - '''opFeatures(obj) ... return all standard features''' - return PathOp.FeatureTool | PathOp.FeatureDepths \ - | PathOp.FeatureHeights | PathOp.FeatureStepDown \ - | PathOp.FeatureCoolant | PathOp.FeatureBaseVertexes \ - | PathOp.FeatureBaseEdges | PathOp.FeatureBaseFaces + """opFeatures(obj) ... return all standard features""" + return ( + PathOp.FeatureTool + | PathOp.FeatureDepths + | PathOp.FeatureHeights + | PathOp.FeatureStepDown + | PathOp.FeatureCoolant + | PathOp.FeatureBaseVertexes + | PathOp.FeatureBaseEdges + | PathOp.FeatureBaseFaces + ) def initOperation(self, obj): - '''initOperation(obj) ... Initialize the operation by - managing property creation and property editor status.''' + """initOperation(obj) ... Initialize the operation by + managing property creation and property editor status.""" self.propertiesReady = False self.initOpProperties(obj) # Initialize operation-specific properties # For debugging if PathLog.getLevel(PathLog.thisModule()) != 4: - obj.setEditorMode('ShowTempObjects', 2) # hide + obj.setEditorMode("ShowTempObjects", 2) # hide - if not hasattr(obj, 'DoNotSetDefaultValues'): + if not hasattr(obj, "DoNotSetDefaultValues"): self.opSetEditorModes(obj) def initOpProperties(self, obj, warn=False): - '''initOpProperties(obj) ... create operation specific properties''' + """initOpProperties(obj) ... create operation specific properties""" + PathLog.track() self.addNewProps = list() for (prtyp, nm, grp, tt) in self.opPropertyDefinitions(): @@ -90,87 +99,228 @@ class ObjectSlot(PathOp.ObjectOp): # Set enumeration lists for enumeration properties if len(self.addNewProps) > 0: - ENUMS = self.opPropertyEnumerations() - # ENUMS = self.getActiveEnumerations(obj) - for n in ENUMS: - if n in self.addNewProps: - setattr(obj, n, ENUMS[n]) + enumDict = ObjectSlot.propertyEnumerations(dataType="raw") + for k, tupList in enumDict.items(): + if k in self.addNewProps: + setattr(obj, k, [t[1] for t in tupList]) if warn: - newPropMsg = translate('PathSlot', 'New property added to') - newPropMsg += ' "{}": {}'.format(obj.Label, self.addNewProps) + '. ' - newPropMsg += translate('PathSlot', 'Check default value(s).') - FreeCAD.Console.PrintWarning(newPropMsg + '\n') + newPropMsg = translate("Path_Slot", "New property added to") + newPropMsg += ' "{}": {}'.format(obj.Label, self.addNewProps) + ". " + newPropMsg += translate("Path_Slot", "Check default value(s).") + FreeCAD.Console.PrintWarning(newPropMsg + "\n") self.propertiesReady = True def opPropertyDefinitions(self): - '''opPropertyDefinitions(obj) ... Store operation specific properties''' + """opPropertyDefinitions(obj) ... Store operation specific properties""" return [ - ("App::PropertyBool", "ShowTempObjects", "Debug", - QtCore.QT_TRANSLATE_NOOP("App::Property", "Show the temporary path construction objects when module is in DEBUG mode.")), - - ("App::PropertyVectorDistance", "CustomPoint1", "Slot", - QtCore.QT_TRANSLATE_NOOP("App::Property", "Enter custom start point for slot path.")), - ("App::PropertyVectorDistance", "CustomPoint2", "Slot", - QtCore.QT_TRANSLATE_NOOP("App::Property", "Enter custom end point for slot path.")), - ("App::PropertyEnumeration", "CutPattern", "Slot", - QtCore.QT_TRANSLATE_NOOP("App::Property", "Set the geometric clearing pattern to use for the operation.")), - ("App::PropertyDistance", "ExtendPathStart", "Slot", - QtCore.QT_TRANSLATE_NOOP("App::Property", "Positive extends the beginning of the path, negative shortens.")), - ("App::PropertyDistance", "ExtendPathEnd", "Slot", - QtCore.QT_TRANSLATE_NOOP("App::Property", "Positive extends the end of the path, negative shortens.")), - ("App::PropertyEnumeration", "LayerMode", "Slot", - QtCore.QT_TRANSLATE_NOOP("App::Property", "Complete the operation in a single pass at depth, or mulitiple passes to final depth.")), - ("App::PropertyEnumeration", "PathOrientation", "Slot", - QtCore.QT_TRANSLATE_NOOP("App::Property", "Choose the path orientation with regard to the feature(s) selected.")), - ("App::PropertyEnumeration", "Reference1", "Slot", - QtCore.QT_TRANSLATE_NOOP("App::Property", "Choose what point to use on the first selected feature.")), - ("App::PropertyEnumeration", "Reference2", "Slot", - QtCore.QT_TRANSLATE_NOOP("App::Property", "Choose what point to use on the second selected feature.")), - ("App::PropertyDistance", "ExtendRadius", "Slot", - QtCore.QT_TRANSLATE_NOOP("App::Property", "For arcs/circlular edges, offset the radius for the path.")), - ("App::PropertyBool", "ReverseDirection", "Slot", - QtCore.QT_TRANSLATE_NOOP("App::Property", "Enable to reverse the cut direction of the slot path.")), - - ("App::PropertyVectorDistance", "StartPoint", "Start Point", - QtCore.QT_TRANSLATE_NOOP("App::Property", "The custom start point for the path of this operation")), - ("App::PropertyBool", "UseStartPoint", "Start Point", - QtCore.QT_TRANSLATE_NOOP("App::Property", "Make True, if specifying a Start Point")) + ( + "App::PropertyBool", + "ShowTempObjects", + "Debug", + QtCore.QT_TRANSLATE_NOOP( + "App::Property", + "Show the temporary path construction objects when module is in DEBUG mode.", + ), + ), + ( + "App::PropertyVectorDistance", + "CustomPoint1", + "Slot", + QtCore.QT_TRANSLATE_NOOP( + "App::Property", "Enter custom start point for slot path." + ), + ), + ( + "App::PropertyVectorDistance", + "CustomPoint2", + "Slot", + QtCore.QT_TRANSLATE_NOOP( + "App::Property", "Enter custom end point for slot path." + ), + ), + ( + "App::PropertyEnumeration", + "CutPattern", + "Slot", + QtCore.QT_TRANSLATE_NOOP( + "App::Property", + "Set the geometric clearing pattern to use for the operation.", + ), + ), + ( + "App::PropertyDistance", + "ExtendPathStart", + "Slot", + QtCore.QT_TRANSLATE_NOOP( + "App::Property", + "Positive extends the beginning of the path, negative shortens.", + ), + ), + ( + "App::PropertyDistance", + "ExtendPathEnd", + "Slot", + QtCore.QT_TRANSLATE_NOOP( + "App::Property", + "Positive extends the end of the path, negative shortens.", + ), + ), + ( + "App::PropertyEnumeration", + "LayerMode", + "Slot", + QtCore.QT_TRANSLATE_NOOP( + "App::Property", + "Complete the operation in a single pass at depth, or mulitiple passes to final depth.", + ), + ), + ( + "App::PropertyEnumeration", + "PathOrientation", + "Slot", + QtCore.QT_TRANSLATE_NOOP( + "App::Property", + "Choose the path orientation with regard to the feature(s) selected.", + ), + ), + ( + "App::PropertyEnumeration", + "Reference1", + "Slot", + QtCore.QT_TRANSLATE_NOOP( + "App::Property", + "Choose what point to use on the first selected feature.", + ), + ), + ( + "App::PropertyEnumeration", + "Reference2", + "Slot", + QtCore.QT_TRANSLATE_NOOP( + "App::Property", + "Choose what point to use on the second selected feature.", + ), + ), + ( + "App::PropertyDistance", + "ExtendRadius", + "Slot", + QtCore.QT_TRANSLATE_NOOP( + "App::Property", + "For arcs/circlular edges, offset the radius for the path.", + ), + ), + ( + "App::PropertyBool", + "ReverseDirection", + "Slot", + QtCore.QT_TRANSLATE_NOOP( + "App::Property", + "Enable to reverse the cut direction of the slot path.", + ), + ), + ( + "App::PropertyVectorDistance", + "StartPoint", + "Start Point", + QtCore.QT_TRANSLATE_NOOP( + "App::Property", + "The custom start point for the path of this operation", + ), + ), + ( + "App::PropertyBool", + "UseStartPoint", + "Start Point", + QtCore.QT_TRANSLATE_NOOP( + "App::Property", "Make True, if specifying a Start Point" + ), + ), ] - def opPropertyEnumerations(self): - # Enumeration lists for App::PropertyEnumeration properties - return { - 'CutPattern': ['Line', 'ZigZag'], - 'LayerMode': ['Single-pass', 'Multi-pass'], - 'PathOrientation': ['Start to End', 'Perpendicular'], - 'Reference1': ['Center of Mass', 'Center of BoundBox', - 'Lowest Point', 'Highest Point', 'Long Edge', - 'Short Edge', 'Vertex'], - 'Reference2': ['Center of Mass', 'Center of BoundBox', - 'Lowest Point', 'Highest Point', 'Vertex'] + @classmethod + def propertyEnumerations(self, dataType="data"): + """propertyEnumerations(dataType="data")... return property enumeration lists of specified dataType. + Args: + dataType = 'data', 'raw', 'translated' + Notes: + 'data' is list of internal string literals used in code + 'raw' is list of (translated_text, data_string) tuples + 'translated' is list of translated string literals + """ + PathLog.track() + + enums = { + "CutPattern": [ + (translate("Path_Slot", "Line"), "Line"), + (translate("Path_Slot", "ZigZag"), "ZigZag"), + ], + "LayerMode": [ + (translate("Path_Slot", "Single-pass"), "Single-pass"), + (translate("Path_Slot", "Multi-pass"), "Multi-pass"), + ], + "PathOrientation": [ + (translate("Path_Slot", "Start to End"), "Start to End"), + (translate("Path_Slot", "Perpendicular"), "Perpendicular"), + ], + "Reference1": [ + (translate("Path_Slot", "Center of Mass"), "Center of Mass"), + ( + translate("Path_Slot", "Center of BoundBox"), + "Center of BoundBox", + ), + (translate("Path_Slot", "Lowest Point"), "Lowest Point"), + (translate("Path_Slot", "Highest Point"), "Highest Point"), + (translate("Path_Slot", "Long Edge"), "Long Edge"), + (translate("Path_Slot", "Short Edge"), "Short Edge"), + (translate("Path_Slot", "Vertex"), "Vertex"), + ], + "Reference2": [ + (translate("Path_Slot", "Center of Mass"), "Center of Mass"), + ( + translate("Path_Slot", "Center of BoundBox"), + "Center of BoundBox", + ), + (translate("Path_Slot", "Lowest Point"), "Lowest Point"), + (translate("Path_Slot", "Highest Point"), "Highest Point"), + (translate("Path_Slot", "Vertex"), "Vertex"), + ], } - def opPropertyDefaults(self, obj, job): - '''opPropertyDefaults(obj, job) ... returns a dictionary of default values - for the operation's properties.''' - defaults = { - 'CustomPoint1': FreeCAD.Vector(0.0, 0.0, 0.0), - 'ExtendPathStart': 0.0, - 'Reference1': 'Center of Mass', - 'CustomPoint2': FreeCAD.Vector(0.0, 0.0, 0.0), - 'ExtendPathEnd': 0.0, - 'Reference2': 'Center of Mass', - 'LayerMode': 'Multi-pass', - 'CutPattern': 'ZigZag', - 'PathOrientation': 'Start to End', - 'ExtendRadius': 0.0, - 'ReverseDirection': False, + if dataType == "raw": + return enums + data = list() + idx = 0 if dataType == "translated" else 1 + + PathLog.debug(enums) + + for k, v in enumerate(enums): + data.append((v, [tup[idx] for tup in enums[v]])) + PathLog.debug(data) + + return data + + def opPropertyDefaults(self, obj, job): + """opPropertyDefaults(obj, job) ... returns a dictionary of default values + for the operation's properties.""" + defaults = { + "CustomPoint1": FreeCAD.Vector(0.0, 0.0, 0.0), + "ExtendPathStart": 0.0, + "Reference1": "Center of Mass", + "CustomPoint2": FreeCAD.Vector(0.0, 0.0, 0.0), + "ExtendPathEnd": 0.0, + "Reference2": "Center of Mass", + "LayerMode": "Multi-pass", + "CutPattern": "ZigZag", + "PathOrientation": "Start to End", + "ExtendRadius": 0.0, + "ReverseDirection": False, # For debugging - 'ShowTempObjects': False + "ShowTempObjects": False, } return defaults @@ -179,19 +329,23 @@ class ObjectSlot(PathOp.ObjectOp): """getActiveEnumerations(obj) ... Method returns dictionary of property enumerations based on active conditions in the operation.""" - ENUMS = self.opPropertyEnumerations() - if hasattr(obj, 'Base'): + ENUMS = dict() + for prop, data in ObjectSlot.propertyEnumerations(): + ENUMS[prop] = data + if hasattr(obj, "Base"): if obj.Base: # (base, subsList) = obj.Base[0] subsList = obj.Base[0][1] subCnt = len(subsList) if subCnt == 1: # Adjust available enumerations - ENUMS['Reference1'] = self._makeReference1Enumerations(subsList[0], True) + ENUMS["Reference1"] = self._makeReference1Enumerations( + subsList[0], True + ) elif subCnt == 2: # Adjust available enumerations - ENUMS['Reference1'] = self._makeReference1Enumerations(subsList[0]) - ENUMS['Reference2'] = self._makeReference2Enumerations(subsList[1]) + ENUMS["Reference1"] = self._makeReference1Enumerations(subsList[0]) + ENUMS["Reference2"] = self._makeReference2Enumerations(subsList[1]) return ENUMS def updateEnumerations(self, obj): @@ -200,26 +354,26 @@ class ObjectSlot(PathOp.ObjectOp): in the operation. Returns the updated enumerations dictionary. Existing property values must be stored, and then restored after the assignment of updated enumerations.""" - PathLog.debug('updateEnumerations()') + PathLog.debug("updateEnumerations()") # Save existing values pre_Ref1 = obj.Reference1 pre_Ref2 = obj.Reference2 # Update enumerations ENUMS = self.getActiveEnumerations(obj) - obj.Reference1 = ENUMS['Reference1'] - obj.Reference2 = ENUMS['Reference2'] + obj.Reference1 = ENUMS["Reference1"] + obj.Reference2 = ENUMS["Reference2"] # Restore pre-existing values if available with active enumerations. # If not, set to first element in active enumeration list. - if pre_Ref1 in ENUMS['Reference1']: + if pre_Ref1 in ENUMS["Reference1"]: obj.Reference1 = pre_Ref1 else: - obj.Reference1 = ENUMS['Reference1'][0] - if pre_Ref2 in ENUMS['Reference2']: + obj.Reference1 = ENUMS["Reference1"][0] + if pre_Ref2 in ENUMS["Reference2"]: obj.Reference2 = pre_Ref2 else: - obj.Reference2 = ENUMS['Reference2'][0] + obj.Reference2 = ENUMS["Reference2"][0] return ENUMS @@ -227,7 +381,7 @@ class ObjectSlot(PathOp.ObjectOp): # Used to hide inputs in properties list A = B = 2 C = 0 - if hasattr(obj, 'Base'): + if hasattr(obj, "Base"): if obj.Base: # (base, subsList) = obj.Base[0] subsList = obj.Base[0][1] @@ -238,14 +392,14 @@ class ObjectSlot(PathOp.ObjectOp): A = B = 0 C = 2 - obj.setEditorMode('Reference1', A) - obj.setEditorMode('Reference2', B) - obj.setEditorMode('ExtendRadius', C) + obj.setEditorMode("Reference1", A) + obj.setEditorMode("Reference2", B) + obj.setEditorMode("ExtendRadius", C) def onChanged(self, obj, prop): - if hasattr(self, 'propertiesReady'): + if hasattr(self, "propertiesReady"): if self.propertiesReady: - if prop in ['Base']: + if prop in ["Base"]: self.updateEnumerations(obj) self.opSetEditorModes(obj) @@ -257,7 +411,7 @@ class ObjectSlot(PathOp.ObjectOp): self.opApplyPropertyDefaults(obj, job, self.addNewProps) mode = 2 if PathLog.getLevel(PathLog.thisModule()) != 4 else 0 - obj.setEditorMode('ShowTempObjects', mode) + obj.setEditorMode("ShowTempObjects", mode) # Repopulate enumerations in case of changes ENUMS = self.updateEnumerations(obj) @@ -280,17 +434,17 @@ class ObjectSlot(PathOp.ObjectOp): prop = getattr(obj, n) val = PROP_DFLTS[n] setVal = False - if hasattr(prop, 'Value'): + if hasattr(prop, "Value"): if isinstance(val, int) or isinstance(val, float): setVal = True if setVal: # propVal = getattr(prop, 'Value') - setattr(prop, 'Value', val) + setattr(prop, "Value", val) else: setattr(obj, n, val) def opSetDefaultValues(self, obj, job): - '''opSetDefaultValues(obj, job) ... initialize defaults''' + """opSetDefaultValues(obj, job) ... initialize defaults""" job = PathUtils.findParentJob(obj) self.opApplyPropertyDefaults(obj, job, self.addNewProps) @@ -313,15 +467,15 @@ class ObjectSlot(PathOp.ObjectOp): obj.OpFinalDepth.Value = -10 obj.OpStartDepth.Value = 10 - PathLog.debug('Default OpFinalDepth: {}'.format(obj.OpFinalDepth.Value)) - PathLog.debug('Default OpStartDepth: {}'.format(obj.OpStartDepth.Value)) + PathLog.debug("Default OpFinalDepth: {}".format(obj.OpFinalDepth.Value)) + PathLog.debug("Default OpStartDepth: {}".format(obj.OpStartDepth.Value)) def opApplyPropertyLimits(self, obj): - '''opApplyPropertyLimits(obj) ... Apply necessary limits to user input property values before performing main operation.''' + """opApplyPropertyLimits(obj) ... Apply necessary limits to user input property values before performing main operation.""" pass def opUpdateDepths(self, obj): - if hasattr(obj, 'Base') and obj.Base: + if hasattr(obj, "Base") and obj.Base: base, sublist = obj.Base[0] fbb = base.Shape.getElement(sublist[0]).BoundBox zmin = fbb.ZMax @@ -335,7 +489,7 @@ class ObjectSlot(PathOp.ObjectOp): obj.OpFinalDepth = zmin def opExecute(self, obj): - '''opExecute(obj) ... process surface operation''' + """opExecute(obj) ... process surface operation""" PathLog.track() self.base = None @@ -361,7 +515,7 @@ class ObjectSlot(PathOp.ObjectOp): CMDS = list() try: - dotIdx = __name__.index('.') + 1 + dotIdx = __name__.index(".") + 1 except Exception: dotIdx = 0 self.module = __name__[dotIdx:] @@ -371,33 +525,55 @@ class ObjectSlot(PathOp.ObjectOp): self.showDebugObjects = obj.ShowTempObjects if self.showDebugObjects: FCAD = FreeCAD.ActiveDocument - for grpNm in ['tmpDebugGrp', 'tmpDebugGrp001']: + for grpNm in ["tmpDebugGrp", "tmpDebugGrp001"]: if hasattr(FCAD, grpNm): for go in FCAD.getObject(grpNm).Group: FCAD.removeObject(go.Name) FCAD.removeObject(grpNm) - self.tmpGrp = FCAD.addObject('App::DocumentObjectGroup', 'tmpDebugGrp') + self.tmpGrp = FCAD.addObject("App::DocumentObjectGroup", "tmpDebugGrp") # Begin GCode for operation with basic information # ... and move cutter to clearance height and startpoint tool = obj.ToolController.Tool - toolType = tool.ToolType if hasattr(tool, 'ToolType') else tool.ShapeName - output = '' - if obj.Comment != '': - self.commandlist.append(Path.Command('N ({})'.format(obj.Comment), {})) - self.commandlist.append(Path.Command('N ({})'.format(obj.Label), {})) - self.commandlist.append(Path.Command('N (Tool type: {})'.format(toolType), {})) - self.commandlist.append(Path.Command('N (Compensated Tool Path. Diameter: {})'.format(tool.Diameter), {})) - self.commandlist.append(Path.Command('N ({})'.format(output), {})) - self.commandlist.append(Path.Command('G0', {'Z': obj.ClearanceHeight.Value, 'F': self.vertRapid})) + toolType = tool.ToolType if hasattr(tool, "ToolType") else tool.ShapeName + output = "" + if obj.Comment != "": + self.commandlist.append(Path.Command("N ({})".format(obj.Comment), {})) + self.commandlist.append(Path.Command("N ({})".format(obj.Label), {})) + self.commandlist.append(Path.Command("N (Tool type: {})".format(toolType), {})) + self.commandlist.append( + Path.Command( + "N (Compensated Tool Path. Diameter: {})".format(tool.Diameter), {} + ) + ) + self.commandlist.append(Path.Command("N ({})".format(output), {})) + self.commandlist.append( + Path.Command("G0", {"Z": obj.ClearanceHeight.Value, "F": self.vertRapid}) + ) if obj.UseStartPoint is True: - self.commandlist.append(Path.Command('G0', {'X': obj.StartPoint.x, 'Y': obj.StartPoint.y, 'F': self.horizRapid})) + self.commandlist.append( + Path.Command( + "G0", + { + "X": obj.StartPoint.x, + "Y": obj.StartPoint.y, + "F": self.horizRapid, + }, + ) + ) # Impose property limits self.opApplyPropertyLimits(obj) # Calculate default depthparams for operation - self.depthParams = PathUtils.depth_params(obj.ClearanceHeight.Value, obj.SafeHeight.Value, obj.StartDepth.Value, obj.StepDown.Value, 0.0, obj.FinalDepth.Value) + self.depthParams = PathUtils.depth_params( + obj.ClearanceHeight.Value, + obj.SafeHeight.Value, + obj.StartDepth.Value, + obj.StepDown.Value, + 0.0, + obj.FinalDepth.Value, + ) # ###### MAIN COMMANDS FOR OPERATION ###### @@ -406,7 +582,9 @@ class ObjectSlot(PathOp.ObjectOp): CMDS.extend(cmds) # Save gcode produced - CMDS.append(Path.Command('G0', {'Z': obj.ClearanceHeight.Value, 'F': self.vertRapid})) + CMDS.append( + Path.Command("G0", {"Z": obj.ClearanceHeight.Value, "F": self.vertRapid}) + ) self.commandlist.extend(CMDS) # ###### CLOSING COMMANDS FOR OPERATION ###### @@ -425,10 +603,9 @@ class ObjectSlot(PathOp.ObjectOp): pnts = False featureCount = 0 - if not hasattr(obj, 'Base'): - msg = translate('PathSlot', - 'No Base Geometry object in the operation.') - FreeCAD.Console.PrintError(msg + '\n') + if not hasattr(obj, "Base"): + msg = translate("Path_Slot", "No Base Geometry object in the operation.") + FreeCAD.Console.PrintError(msg + "\n") return False if not obj.Base: @@ -436,17 +613,15 @@ class ObjectSlot(PathOp.ObjectOp): p1 = obj.CustomPoint1 p2 = obj.CustomPoint2 if p1 == p2: - msg = translate('PathSlot', - 'Custom points are identical.') - FreeCAD.Console.PrintError(msg + '\n') + msg = translate("Path_Slot", "Custom points are identical.") + FreeCAD.Console.PrintError(msg + "\n") return False elif p1.z == p2.z: pnts = (p1, p2) featureCount = 2 else: - msg = translate('PathSlot', - 'Custom points not at same Z height.') - FreeCAD.Console.PrintError(msg + '\n') + msg = translate("Path_Slot", "Custom points not at same Z height.") + FreeCAD.Console.PrintError(msg + "\n") return False else: baseGeom = obj.Base[0] @@ -454,15 +629,15 @@ class ObjectSlot(PathOp.ObjectOp): self.base = base featureCount = len(subsList) - if featureCount == 1: - PathLog.debug('Reference 1: {}'.format(obj.Reference1)) + if featureCount == 1: + PathLog.debug("Reference 1: {}".format(obj.Reference1)) sub1 = subsList[0] shape_1 = getattr(base.Shape, sub1) self.shape1 = shape_1 pnts = self._processSingle(obj, shape_1, sub1) else: - PathLog.debug('Reference 1: {}'.format(obj.Reference1)) - PathLog.debug('Reference 2: {}'.format(obj.Reference2)) + PathLog.debug("Reference 1: {}".format(obj.Reference1)) + PathLog.debug("Reference 2: {}".format(obj.Reference2)) sub1 = subsList[0] sub2 = subsList[1] shape_1 = getattr(base.Shape, sub1) @@ -487,36 +662,42 @@ class ObjectSlot(PathOp.ObjectOp): def _finishArc(self, obj, pnts, featureCnt): """This method finishes an Arc Slot operation. It returns the gcode for the slot operation.""" - PathLog.debug('arc center: {}'.format(self.arcCenter)) - self._addDebugObject(Part.makeLine(self.arcCenter, self.arcMidPnt), 'CentToMidPnt') + PathLog.debug("arc center: {}".format(self.arcCenter)) + self._addDebugObject( + Part.makeLine(self.arcCenter, self.arcMidPnt), "CentToMidPnt" + ) # PathLog.debug('Pre-offset points are:\np1 = {}\np2 = {}'.format(p1, p2)) if obj.ExtendRadius.Value != 0: # verify offset does not force radius < 0 newRadius = self.arcRadius + obj.ExtendRadius.Value - PathLog.debug('arc radius: {}; offset radius: {}'.format(self.arcRadius, newRadius)) + PathLog.debug( + "arc radius: {}; offset radius: {}".format(self.arcRadius, newRadius) + ) if newRadius <= 0: - msg = translate('PathSlot', - 'Current Extend Radius value produces negative arc radius.') - FreeCAD.Console.PrintError(msg + '\n') + msg = translate( + "Path_Slot", + "Current Extend Radius value produces negative arc radius.", + ) + FreeCAD.Console.PrintError(msg + "\n") return False else: (p1, p2) = pnts pnts = self._makeOffsetArc(p1, p2, self.arcCenter, newRadius) self.newRadius = newRadius else: - PathLog.debug('arc radius: {}'.format(self.arcRadius)) + PathLog.debug("arc radius: {}".format(self.arcRadius)) self.newRadius = self.arcRadius # Apply path extension for arcs # PathLog.debug('Pre-extension points are:\np1 = {}\np2 = {}'.format(p1, p2)) if self.isArc == 1: # Complete circle - if (obj.ExtendPathStart.Value != 0 or - obj.ExtendPathEnd.Value != 0): - msg = translate('PathSlot', - 'No path extensions available for full circles.') - FreeCAD.Console.PrintWarning(msg + '\n') + if obj.ExtendPathStart.Value != 0 or obj.ExtendPathEnd.Value != 0: + msg = translate( + "Path_Slot", "No path extensions available for full circles." + ) + FreeCAD.Console.PrintWarning(msg + "\n") else: # Arc segment # Apply extensions to slot path @@ -533,19 +714,18 @@ class ObjectSlot(PathOp.ObjectOp): (p1, p2) = pnts # PathLog.error('Post-offset points are:\np1 = {}\np2 = {}'.format(p1, p2)) if self.isDebug: - PathLog.debug('Path Points are:\np1 = {}\np2 = {}'.format(p1, p2)) + PathLog.debug("Path Points are:\np1 = {}\np2 = {}".format(p1, p2)) if p1.sub(p2).Length != 0: - self._addDebugObject(Part.makeLine(p1, p2), 'Path') + self._addDebugObject(Part.makeLine(p1, p2), "Path") if featureCnt: obj.CustomPoint1 = p1 obj.CustomPoint2 = p2 if self._arcCollisionCheck(obj, p1, p2, self.arcCenter, self.newRadius): - msg = obj.Label + ' ' - msg += translate('PathSlot', - 'operation collides with model.') - FreeCAD.Console.PrintError(msg + '\n') + msg = obj.Label + " " + msg += translate("Path_Slot", "operation collides with model.") + FreeCAD.Console.PrintError(msg + "\n") # PathLog.warning('Unable to create G-code. _makeArcGCode() is incomplete.') cmds = self._makeArcGCode(obj, p1, p2) @@ -556,34 +736,47 @@ class ObjectSlot(PathOp.ObjectOp): It accepts the operation object and two end points for the path. It returns the gcode for the slot operation.""" CMDS = list() - PATHS = [(p2, p1, 'G2'), (p1, p2, 'G3')] - if obj.ReverseDirection : + PATHS = [(p2, p1, "G2"), (p1, p2, "G3")] + if obj.ReverseDirection: path_index = 1 - else : + else: path_index = 0 def arcPass(POINTS, depth): cmds = list() (st_pt, end_pt, arcCmd) = POINTS # cmds.append(Path.Command('N (Tool type: {})'.format(toolType), {})) - cmds.append(Path.Command('G0', {'X': st_pt.x, 'Y': st_pt.y, 'F': self.horizRapid})) - cmds.append(Path.Command('G1', {'Z': depth, 'F': self.vertFeed})) + cmds.append( + Path.Command("G0", {"X": st_pt.x, "Y": st_pt.y, "F": self.horizRapid}) + ) + cmds.append(Path.Command("G1", {"Z": depth, "F": self.vertFeed})) vtc = self.arcCenter.sub(st_pt) # vector to center cmds.append( - Path.Command(arcCmd, - {'X': end_pt.x, 'Y': end_pt.y, 'I': vtc.x, - 'J': vtc.y, 'F': self.horizFeed - })) + Path.Command( + arcCmd, + { + "X": end_pt.x, + "Y": end_pt.y, + "I": vtc.x, + "J": vtc.y, + "F": self.horizFeed, + }, + ) + ) return cmds - if obj.LayerMode == 'Single-pass': + if obj.LayerMode == "Single-pass": CMDS.extend(arcPass(PATHS[path_index], obj.FinalDepth.Value)) else: - if obj.CutPattern == 'Line': + if obj.CutPattern == "Line": for depth in self.depthParams: CMDS.extend(arcPass(PATHS[path_index], depth)) - CMDS.append(Path.Command('G0', {'Z': obj.SafeHeight.Value, 'F': self.vertRapid})) - elif obj.CutPattern == 'ZigZag': + CMDS.append( + Path.Command( + "G0", {"Z": obj.SafeHeight.Value, "F": self.vertRapid} + ) + ) + elif obj.CutPattern == "ZigZag": i = 0 for depth in self.depthParams: if i % 2.0 == 0: # even @@ -592,10 +785,12 @@ class ObjectSlot(PathOp.ObjectOp): CMDS.extend(arcPass(PATHS[not path_index], depth)) i += 1 # Raise to SafeHeight when finished - CMDS.append(Path.Command('G0', {'Z': obj.SafeHeight.Value, 'F': self.vertRapid})) + CMDS.append( + Path.Command("G0", {"Z": obj.SafeHeight.Value, "F": self.vertRapid}) + ) if self.isDebug: - PathLog.debug('G-code arc command is: {}'.format(PATHS[path_index][2])) + PathLog.debug("G-code arc command is: {}".format(PATHS[path_index][2])) return CMDS @@ -604,38 +799,46 @@ class ObjectSlot(PathOp.ObjectOp): It returns the gcode for the line slot operation.""" # Apply perpendicular rotation if requested perpZero = True - if obj.PathOrientation == 'Perpendicular': + if obj.PathOrientation == "Perpendicular": if featureCnt == 2: - if self.shapeType1 == 'Face' and self.shapeType2 == 'Face': + if self.shapeType1 == "Face" and self.shapeType2 == "Face": if self.bottomEdges: self.bottomEdges.sort(key=lambda edg: edg.Length, reverse=True) BE = self.bottomEdges[0] pnts = self._processSingleVertFace(obj, BE) perpZero = False - elif self.shapeType1 == 'Edge' and self.shapeType2 == 'Edge': - PathLog.debug('_finishLine() Perp, featureCnt == 2') + elif self.shapeType1 == "Edge" and self.shapeType2 == "Edge": + PathLog.debug("_finishLine() Perp, featureCnt == 2") if perpZero: (p1, p2) = pnts initPerpDist = p1.sub(p2).Length - pnts = self._makePerpendicular(p1, p2, initPerpDist) # 10.0 offset below + pnts = self._makePerpendicular( + p1, p2, initPerpDist + ) # 10.0 offset below else: # Modify path points if user selected two parallel edges - if (featureCnt == 2 and self.shapeType1 == 'Edge' and - self.shapeType2 == 'Edge'): + if ( + featureCnt == 2 + and self.shapeType1 == "Edge" + and self.shapeType2 == "Edge" + ): if self.featureDetails[0] == "arc" and self.featureDetails[1] == "arc": perpZero = False elif self._isParallel(self.dYdX1, self.dYdX2): - PathLog.debug('_finishLine() StE, featureCnt == 2 // edges') + PathLog.debug("_finishLine() StE, featureCnt == 2 // edges") (p1, p2) = pnts edg1_len = self.shape1.Length edg2_len = self.shape2.Length set_length = max(edg1_len, edg2_len) - pnts = self._makePerpendicular(p1, p2, 10.0 + set_length) # 10.0 offset below + pnts = self._makePerpendicular( + p1, p2, 10.0 + set_length + ) # 10.0 offset below if edg1_len != edg2_len: - msg = obj.Label + ' ' - msg += translate('PathSlot', - 'Verify slot path start and end points.') - FreeCAD.Console.PrintWarning(msg + '\n') + msg = obj.Label + " " + msg += translate( + "Path_Slot", "Verify slot path start and end points." + ) + FreeCAD.Console.PrintWarning(msg + "\n") else: perpZero = False @@ -659,19 +862,18 @@ class ObjectSlot(PathOp.ObjectOp): (p1, p2) = pnts if self.isDebug: - PathLog.debug('Path Points are:\np1 = {}\np2 = {}'.format(p1, p2)) + PathLog.debug("Path Points are:\np1 = {}\np2 = {}".format(p1, p2)) if p1.sub(p2).Length != 0: - self._addDebugObject(Part.makeLine(p1, p2), 'Path') + self._addDebugObject(Part.makeLine(p1, p2), "Path") if featureCnt: obj.CustomPoint1 = p1 obj.CustomPoint2 = p2 if self._lineCollisionCheck(obj, p1, p2): - msg = obj.Label + ' ' - msg += translate('PathSlot', - 'operation collides with model.') - FreeCAD.Console.PrintWarning(msg + '\n') + msg = obj.Label + " " + msg += translate("Path_Slot", "operation collides with model.") + FreeCAD.Console.PrintWarning(msg + "\n") cmds = self._makeLineGCode(obj, p1, p2) return cmds @@ -685,32 +887,52 @@ class ObjectSlot(PathOp.ObjectOp): def linePass(p1, p2, depth): cmds = list() # cmds.append(Path.Command('N (Tool type: {})'.format(toolType), {})) - cmds.append(Path.Command('G0', {'X': p1.x, 'Y': p1.y, 'F': self.horizRapid})) - cmds.append(Path.Command('G1', {'Z': depth, 'F': self.vertFeed})) - cmds.append(Path.Command('G1', {'X': p2.x, 'Y': p2.y, 'F': self.horizFeed})) + cmds.append( + Path.Command("G0", {"X": p1.x, "Y": p1.y, "F": self.horizRapid}) + ) + cmds.append(Path.Command("G1", {"Z": depth, "F": self.vertFeed})) + cmds.append(Path.Command("G1", {"X": p2.x, "Y": p2.y, "F": self.horizFeed})) return cmds # CMDS.append(Path.Command('N (Tool type: {})'.format(toolType), {})) - if obj.LayerMode == 'Single-pass': + if obj.LayerMode == "Single-pass": CMDS.extend(linePass(p1, p2, obj.FinalDepth.Value)) - CMDS.append(Path.Command('G0', {'Z': obj.SafeHeight.Value, 'F': self.vertRapid})) + CMDS.append( + Path.Command("G0", {"Z": obj.SafeHeight.Value, "F": self.vertRapid}) + ) else: - if obj.CutPattern == 'Line': + if obj.CutPattern == "Line": for dep in self.depthParams: CMDS.extend(linePass(p1, p2, dep)) - CMDS.append(Path.Command('G0', {'Z': obj.SafeHeight.Value, 'F': self.vertRapid})) - elif obj.CutPattern == 'ZigZag': - CMDS.append(Path.Command('G0', {'X': p1.x, 'Y': p1.y, 'F': self.horizRapid})) + CMDS.append( + Path.Command( + "G0", {"Z": obj.SafeHeight.Value, "F": self.vertRapid} + ) + ) + elif obj.CutPattern == "ZigZag": + CMDS.append( + Path.Command("G0", {"X": p1.x, "Y": p1.y, "F": self.horizRapid}) + ) i = 0 for dep in self.depthParams: if i % 2.0 == 0: # even - CMDS.append(Path.Command('G1', {'Z': dep, 'F': self.vertFeed})) - CMDS.append(Path.Command('G1', {'X': p2.x, 'Y': p2.y, 'F': self.horizFeed})) + CMDS.append(Path.Command("G1", {"Z": dep, "F": self.vertFeed})) + CMDS.append( + Path.Command( + "G1", {"X": p2.x, "Y": p2.y, "F": self.horizFeed} + ) + ) else: # odd - CMDS.append(Path.Command('G1', {'Z': dep, 'F': self.vertFeed})) - CMDS.append(Path.Command('G1', {'X': p1.x, 'Y': p1.y, 'F': self.horizFeed})) + CMDS.append(Path.Command("G1", {"Z": dep, "F": self.vertFeed})) + CMDS.append( + Path.Command( + "G1", {"X": p1.x, "Y": p1.y, "F": self.horizFeed} + ) + ) i += 1 - CMDS.append(Path.Command('G0', {'Z': obj.SafeHeight.Value, 'F': self.vertRapid})) + CMDS.append( + Path.Command("G0", {"Z": obj.SafeHeight.Value, "F": self.vertRapid}) + ) return CMDS @@ -721,10 +943,10 @@ class ObjectSlot(PathOp.ObjectOp): done = False cat1 = sub1[:4] - if cat1 == 'Face': + if cat1 == "Face": pnts = False norm = shape_1.normalAt(0.0, 0.0) - PathLog.debug('{}.normalAt(): {}'.format(sub1, norm)) + PathLog.debug("{}.normalAt(): {}".format(sub1, norm)) if PathGeom.isRoughly(shape_1.BoundBox.ZMax, shape_1.BoundBox.ZMin): # Horizontal face @@ -734,11 +956,11 @@ class ObjectSlot(PathOp.ObjectOp): faceType = self._getVertFaceType(shape_1) if faceType: (geo, shp) = faceType - if geo == 'Face': + if geo == "Face": pnts = self._processSingleComplexFace(obj, shp) - if geo == 'Wire': + if geo == "Wire": pnts = self._processSingleVertFace(obj, shp) - if geo == 'Edge': + if geo == "Edge": pnts = self._processSingleVertFace(obj, shp) else: if len(shape_1.Edges) == 4: @@ -747,26 +969,27 @@ class ObjectSlot(PathOp.ObjectOp): pnts = self._processSingleComplexFace(obj, shape_1) if not pnts: - msg = translate('PathSlot', - 'The selected face is inaccessible.') - FreeCAD.Console.PrintError(msg + '\n') + msg = translate("Path_Slot", "The selected face is inaccessible.") + FreeCAD.Console.PrintError(msg + "\n") return False if pnts: (p1, p2) = pnts done = True - elif cat1 == 'Edge': - PathLog.debug('Single edge') + elif cat1 == "Edge": + PathLog.debug("Single edge") pnts = self._processSingleEdge(obj, shape_1) if pnts: (p1, p2) = pnts done = True - elif cat1 == 'Vert': - msg = translate('PathSlot', - 'Only a vertex selected. Add another feature to the Base Geometry.') - FreeCAD.Console.PrintError(msg + '\n') + elif cat1 == "Vert": + msg = translate( + "Path_Slot", + "Only a vertex selected. Add another feature to the Base Geometry.", + ) + FreeCAD.Console.PrintError(msg + "\n") if done: return (p1, p2) @@ -775,8 +998,8 @@ class ObjectSlot(PathOp.ObjectOp): def _processSingleHorizFace(self, obj, shape): """Determine slot path endpoints from a single horizontally oriented face.""" - PathLog.debug('_processSingleHorizFace()') - lineTypes = ['Part::GeomLine'] + PathLog.debug("_processSingleHorizFace()") + lineTypes = ["Part::GeomLine"] def getRadians(self, E): vect = self._dXdYdZ(E) @@ -789,18 +1012,16 @@ class ObjectSlot(PathOp.ObjectOp): # Reject triangular faces if len(shape.Edges) < 4: - msg = translate('PathSlot', - 'A single selected face must have four edges minimum.') - FreeCAD.Console.PrintError(msg + '\n') + msg = translate( + "Path_Slot", "A single selected face must have four edges minimum." + ) + FreeCAD.Console.PrintError(msg + "\n") return False # Create tuples as (edge index, length, angle) eTups = list() for i in range(0, 4): - eTups.append((i, - shape.Edges[i].Length, - getRadians(self, shape.Edges[i])) - ) + eTups.append((i, shape.Edges[i].Length, getRadians(self, shape.Edges[i]))) # Sort tuples by edge angle eTups.sort(key=lambda tup: tup[2]) @@ -834,7 +1055,7 @@ class ObjectSlot(PathOp.ObjectOp): parallel_edge_flags[B[0]] = flag flag += 1 if debug: - msg = 'Erroneous Curve.TypeId: {}'.format(debug) + msg = "Erroneous Curve.TypeId: {}".format(debug) PathLog.debug(msg) pairCnt = len(parallel_edge_pairs) @@ -842,15 +1063,16 @@ class ObjectSlot(PathOp.ObjectOp): parallel_edge_pairs.sort(key=lambda tup: tup[0].Length, reverse=True) if self.isDebug: - PathLog.debug(' -pairCnt: {}'.format(pairCnt)) + PathLog.debug(" -pairCnt: {}".format(pairCnt)) for (a, b) in parallel_edge_pairs: - PathLog.debug(' -pair: {}, {}'.format(round(a.Length, 4), round(b.Length,4))) - PathLog.debug(' -parallel_edge_flags: {}'.format(parallel_edge_flags)) + PathLog.debug( + " -pair: {}, {}".format(round(a.Length, 4), round(b.Length, 4)) + ) + PathLog.debug(" -parallel_edge_flags: {}".format(parallel_edge_flags)) if pairCnt == 0: - msg = translate('PathSlot', - 'No parallel edges identified.') - FreeCAD.Console.PrintError(msg + '\n') + msg = translate("Path_Slot", "No parallel edges identified.") + FreeCAD.Console.PrintError(msg + "\n") return False elif pairCnt == 1: # One pair of parallel edges identified @@ -867,15 +1089,14 @@ class ObjectSlot(PathOp.ObjectOp): else: same = parallel_edge_pairs[0] else: - if obj.Reference1 == 'Long Edge': + if obj.Reference1 == "Long Edge": same = parallel_edge_pairs[1] - elif obj.Reference1 == 'Short Edge': + elif obj.Reference1 == "Short Edge": same = parallel_edge_pairs[0] else: - msg = 'Reference1 ' - msg += translate('PathSlot', - 'value error.') - FreeCAD.Console.PrintError(msg + '\n') + msg = "Reference1 " + msg += translate("Path_Slot", "value error.") + FreeCAD.Console.PrintError(msg + "\n") return False (p1, p2) = self._getOppMidPoints(same) @@ -883,7 +1104,7 @@ class ObjectSlot(PathOp.ObjectOp): def _processSingleComplexFace(self, obj, shape): """Determine slot path endpoints from a single complex face.""" - PathLog.debug('_processSingleComplexFace()') + PathLog.debug("_processSingleComplexFace()") pnts = list() def zVal(p): @@ -898,7 +1119,7 @@ class ObjectSlot(PathOp.ObjectOp): def _processSingleVertFace(self, obj, shape): """Determine slot path endpoints from a single vertically oriented face with no single bottom edge.""" - PathLog.debug('_processSingleVertFace()') + PathLog.debug("_processSingleVertFace()") eCnt = len(shape.Edges) V0 = shape.Edges[0].Vertexes[0] V1 = shape.Edges[eCnt - 1].Vertexes[1] @@ -920,33 +1141,30 @@ class ObjectSlot(PathOp.ObjectOp): b2 = v1.sub(perpVect) (p1, p2) = self._getCutSidePoints(obj, v0, v1, a1, a2, b1, b2) - msg = obj.Label + ' ' - msg += translate('PathSlot', - 'Verify slot path start and end points.') - FreeCAD.Console.PrintWarning(msg + '\n') + msg = obj.Label + " " + msg += translate("Path_Slot", "Verify slot path start and end points.") + FreeCAD.Console.PrintWarning(msg + "\n") return (p1, p2) def _processSingleEdge(self, obj, edge): """Determine slot path endpoints from a single horizontally oriented edge.""" - PathLog.debug('_processSingleEdge()') + PathLog.debug("_processSingleEdge()") tolrnc = 0.0000001 - lineTypes = ['Part::GeomLine'] - curveTypes = ['Part::GeomCircle'] + lineTypes = ["Part::GeomLine"] + curveTypes = ["Part::GeomCircle"] def oversizedTool(holeDiam): # Test if tool larger than opening if self.tool.Diameter > holeDiam: - msg = translate('PathSlot', - 'Current tool larger than arc diameter.') - FreeCAD.Console.PrintError(msg + '\n') + msg = translate("Path_Slot", "Current tool larger than arc diameter.") + FreeCAD.Console.PrintError(msg + "\n") return True return False def isHorizontal(z1, z2, z3): # Check that all Z values are equal (isRoughly same) - if (abs(z1 - z2) > tolrnc or - abs(z1 - z3) > tolrnc ): + if abs(z1 - z2) > tolrnc or abs(z1 - z3) > tolrnc: # abs(z2 - z3) > tolrnc): 3rd test redundant. return False return True @@ -954,9 +1172,9 @@ class ObjectSlot(PathOp.ObjectOp): def circumCircleFrom3Points(P1, P2, P3): # Source code for this function copied from (with modifications): # https://wiki.freecadweb.org/Macro_Draft_Circle_3_Points_3D - vP2P1 = (P2 - P1) - vP3P2 = (P3 - P2) - vP1P3 = (P1 - P3) + vP2P1 = P2 - P1 + vP3P2 = P3 - P2 + vP1P3 = P1 - P3 L = vP2P1.cross(vP3P2).Length # Circle radius (not used) @@ -966,17 +1184,17 @@ class ObjectSlot(PathOp.ObjectOp): return False # Sphere center. - twolsqr= 2*L*L + twolsqr = 2 * L * L a = -vP3P2.dot(vP3P2) * vP2P1.dot(vP1P3) / twolsqr b = -vP1P3.dot(vP1P3) * vP3P2.dot(vP2P1) / twolsqr c = -vP2P1.dot(vP2P1) * vP1P3.dot(vP3P2) / twolsqr - return P1*a + P2*b + P3*c + return P1 * a + P2 * b + P3 * c V1 = edge.Vertexes[0] p1 = FreeCAD.Vector(V1.X, V1.Y, 0.0) - if len(edge.Vertexes) == 1: # circle has one virtex + if len(edge.Vertexes) == 1: # circle has one virtex p2 = FreeCAD.Vector(p1) - else : + else: V2 = edge.Vertexes[1] p2 = FreeCAD.Vector(V2.X, V2.Y, 0.0) @@ -987,7 +1205,7 @@ class ObjectSlot(PathOp.ObjectOp): elif edge.Curve.TypeId in curveTypes: if len(edge.Vertexes) == 1: # Circle edge - PathLog.debug('Arc with single vertex.') + PathLog.debug("Arc with single vertex.") if oversizedTool(edge.BoundBox.XLength): return False @@ -1004,7 +1222,7 @@ class ObjectSlot(PathOp.ObjectOp): self.arcRadius = edge.BoundBox.XLength / 2.0 else: # Arc edge - PathLog.debug('Arc with multiple vertices.') + PathLog.debug("Arc with multiple vertices.") self.isArc = 2 midPnt = edge.valueAt(edge.getParameterByLength(edge.Length / 2.0)) if not isHorizontal(V1.Z, V2.Z, midPnt.z): @@ -1022,9 +1240,12 @@ class ObjectSlot(PathOp.ObjectOp): return False return (p1, p2) - else : - msg = translate('PathSlot','Failed, slot from edge only accepts lines, arcs and circles.') - FreeCAD.Console.PrintError(msg + '\n') + else: + msg = translate( + "Path_Slot", + "Failed, slot from edge only accepts lines, arcs and circles.", + ) + FreeCAD.Console.PrintError(msg + "\n") return False # not line , not circle @@ -1032,7 +1253,7 @@ class ObjectSlot(PathOp.ObjectOp): def _processDouble(self, obj, shape_1, sub1, shape_2, sub2): """This is the control method for slots based on a two Base Geometry features.""" - PathLog.debug('_processDouble()') + PathLog.debug("_processDouble()") p1 = None p2 = None @@ -1042,9 +1263,8 @@ class ObjectSlot(PathOp.ObjectOp): feature1 = self._processFeature(obj, shape_1, sub1, 1) if not feature1: - msg = translate('PathSlot', - 'Failed to determine point 1 from') - FreeCAD.Console.PrintError(msg + ' {}.\n'.format(sub1)) + msg = translate("Path_Slot", "Failed to determine point 1 from") + FreeCAD.Console.PrintError(msg + " {}.\n".format(sub1)) return False (p1, dYdX1, shpType) = feature1 self.shapeType1 = shpType @@ -1053,9 +1273,8 @@ class ObjectSlot(PathOp.ObjectOp): feature2 = self._processFeature(obj, shape_2, sub2, 2) if not feature2: - msg = translate('PathSlot', - 'Failed to determine point 2 from') - FreeCAD.Console.PrintError(msg + ' {}.\n'.format(sub2)) + msg = translate("Path_Slot", "Failed to determine point 2 from") + FreeCAD.Console.PrintError(msg + " {}.\n".format(sub2)) return False (p2, dYdX2, shpType) = feature2 self.shapeType2 = shpType @@ -1064,12 +1283,11 @@ class ObjectSlot(PathOp.ObjectOp): # Parallel check for twin face, and face-edge cases if dYdX1 and dYdX2: - PathLog.debug('dYdX1, dYdX2: {}, {}'.format(dYdX1, dYdX2)) + PathLog.debug("dYdX1, dYdX2: {}, {}".format(dYdX1, dYdX2)) if not self._isParallel(dYdX1, dYdX2): - if self.shapeType1 != 'Edge' or self.shapeType2 != 'Edge': - msg = translate('PathSlot', - 'Selected geometry not parallel.') - FreeCAD.Console.PrintError(msg + '\n') + if self.shapeType1 != "Edge" or self.shapeType2 != "Edge": + msg = translate("Path_Slot", "Selected geometry not parallel.") + FreeCAD.Console.PrintError(msg + "\n") return False if p2: @@ -1171,9 +1389,9 @@ class ObjectSlot(PathOp.ObjectOp): p = None dYdX = None cat = sub[:4] - PathLog.debug('sub-feature is {}'.format(cat)) - Ref = getattr(obj, 'Reference' + str(pNum)) - if cat == 'Face': + PathLog.debug("sub-feature is {}".format(cat)) + Ref = getattr(obj, "Reference" + str(pNum)) + if cat == "Face": BE = self._getBottomEdge(shape) if BE: self.bottomEdges.append(BE) @@ -1187,25 +1405,26 @@ class ObjectSlot(PathOp.ObjectOp): norm = shape.normalAt(0.0, 0.0) # FreeCAD.Console.PrintMessage('{} normal {}.\n'.format(sub, norm)) if norm.z != 0: - msg = translate('PathSlot', - 'The selected face is not oriented vertically:') - FreeCAD.Console.PrintError(msg + ' {}.\n'.format(sub)) + msg = translate( + "Path_Slot", "The selected face is not oriented vertically:" + ) + FreeCAD.Console.PrintError(msg + " {}.\n".format(sub)) return False - if Ref == 'Center of Mass': + if Ref == "Center of Mass": comS = shape.CenterOfMass p = FreeCAD.Vector(comS.x, comS.y, 0.0) - elif Ref == 'Center of BoundBox': + elif Ref == "Center of BoundBox": comS = shape.BoundBox.Center p = FreeCAD.Vector(comS.x, comS.y, 0.0) - elif Ref == 'Lowest Point': + elif Ref == "Lowest Point": p = self._getLowestPoint(shape) - elif Ref == 'Highest Point': + elif Ref == "Highest Point": p = self._getHighestPoint(shape) - elif cat == 'Edge': + elif cat == "Edge": featDetIdx = pNum - 1 - if shape.Curve.TypeId == 'Part::GeomCircle': + if shape.Curve.TypeId == "Part::GeomCircle": self.featureDetails[featDetIdx] = "arc" # calculate slope between end vertexes v0 = shape.Edges[0].Vertexes[0] @@ -1213,18 +1432,18 @@ class ObjectSlot(PathOp.ObjectOp): temp = FreeCAD.Vector(v1.X - v0.X, v1.Y - v0.Y, 0.0) dYdX = self._normalizeVector(temp) - if Ref == 'Center of Mass': + if Ref == "Center of Mass": comS = shape.CenterOfMass p = FreeCAD.Vector(comS.x, comS.y, 0.0) - elif Ref == 'Center of BoundBox': + elif Ref == "Center of BoundBox": comS = shape.BoundBox.Center p = FreeCAD.Vector(comS.x, comS.y, 0.0) - elif Ref == 'Lowest Point': + elif Ref == "Lowest Point": p = self._findLowestPointOnEdge(shape) - elif Ref == 'Highest Point': + elif Ref == "Highest Point": p = self._findHighestPointOnEdge(shape) - elif cat == 'Vert': + elif cat == "Vert": V = shape.Vertexes[0] p = FreeCAD.Vector(V.X, V.Y, 0.0) @@ -1239,7 +1458,7 @@ class ObjectSlot(PathOp.ObjectOp): The arc is extended along the circumference with begExt and endExt values. The function returns the new end points as tuple (n1, n2) to replace p1 and p2.""" cancel = True - if not begExt and not endExt : + if not begExt and not endExt: return (p1, p2) n1 = p1 @@ -1253,7 +1472,6 @@ class ObjectSlot(PathOp.ObjectOp): b = FreeCAD.Vector(x, y, 0.0) return Part.makeLine(a, b) - # Convert extension to radians; make a generic chord ( line ) on XY plane from the x axis # rotate and shift into place so it has same vertices as the required arc extension # adjust rotation angle to provide +ve or -ve extension as needed @@ -1265,14 +1483,16 @@ class ObjectSlot(PathOp.ObjectOp): beginRadians = self._getVectorAngle(p1.sub(self.arcCenter)) if begExt < 0: beginRadians += 0 # negative Ext shortens slot so chord endpoint is slot start point - else : - beginRadians -= 2*ExtRadians # positive Ext lengthens slot so decrease start point angle + else: + beginRadians -= ( + 2 * ExtRadians + ) # positive Ext lengthens slot so decrease start point angle # PathLog.debug('begExt angles are: {}, {}'.format(beginRadians, math.degrees(beginRadians))) chord.rotate(origin, FreeCAD.Vector(0, 0, 1), math.degrees(beginRadians)) chord.translate(self.arcCenter) - self._addDebugObject(chord, 'ExtendStart') + self._addDebugObject(chord, "ExtendStart") v1 = chord.Vertexes[1] n1 = FreeCAD.Vector(v1.X, v1.Y, 0.0) @@ -1284,14 +1504,16 @@ class ObjectSlot(PathOp.ObjectOp): endRadians = self._getVectorAngle(p2.sub(self.arcCenter)) if endExt > 0: endRadians += 0 # positive Ext lengthens slot so chord endpoint is good - else : - endRadians -= 2*ExtRadians # negative Ext shortens slot so decrease end point angle + else: + endRadians -= ( + 2 * ExtRadians + ) # negative Ext shortens slot so decrease end point angle # PathLog.debug('endExt angles are: {}, {}'.format(endRadians, math.degrees(endRadians))) chord.rotate(origin, FreeCAD.Vector(0, 0, 1), math.degrees(endRadians)) chord.translate(self.arcCenter) - self._addDebugObject(chord, 'ExtendEnd') + self._addDebugObject(chord, "ExtendEnd") v1 = chord.Vertexes[1] n2 = FreeCAD.Vector(v1.X, v1.Y, 0.0) @@ -1303,8 +1525,8 @@ class ObjectSlot(PathOp.ObjectOp): This function offsets an arc defined by endpoints, p1 and p2, and the center. New end points are returned at the radius passed by newRadius. The angle of the original arc is maintained.""" - n1 = p1.sub(center).normalize()*newRadius - n2 = p2.sub(center).normalize()*newRadius + n1 = p1.sub(center).normalize() * newRadius + n2 = p2.sub(center).normalize() * newRadius return (n1.add(center), n2.add(center)) def _extendLineSlot(self, p1, p2, begExt, endExt): @@ -1313,12 +1535,12 @@ class ObjectSlot(PathOp.ObjectOp): The beginning is extended by begExt value and the end by endExt value.""" if begExt: beg = p1.sub(p2) - n1 = p1.add(beg.normalize()*begExt) + n1 = p1.add(beg.normalize() * begExt) else: n1 = p1 if endExt: - end = p2.sub(p1) - n2 = p2.add(end.normalize()*endExt) + end = p2.sub(p1) + n2 = p2.add(end.normalize() * endExt) else: n2 = p2 return (n1, n2) @@ -1340,7 +1562,7 @@ class ObjectSlot(PathOp.ObjectOp): # (dYdX1.y + dYdX2.y) / 2.0 == dYdX1.y): # return True # return False - return (dYdX1.cross(dYdX2) == FreeCAD.Vector(0,0,0) ) + return dYdX1.cross(dYdX2) == FreeCAD.Vector(0, 0, 0) def _makePerpendicular(self, p1, p2, length): """_makePerpendicular(p1, p2, length)... @@ -1478,7 +1700,7 @@ class ObjectSlot(PathOp.ObjectOp): x = abs(v.x) y = abs(v.y) - rads = math.atan(y/x) + rads = math.atan(y / x) if v.x > 0: if v.y > 0: return rads @@ -1527,7 +1749,7 @@ class ObjectSlot(PathOp.ObjectOp): bottomEdge = self._getBottomEdge(shape) if bottomEdge: - return ('Edge', bottomEdge) + return ("Edge", bottomEdge) # Extract cross-section of face extFwd = (shape.BoundBox.ZLength * 2.2) + 10 @@ -1540,36 +1762,38 @@ class ObjectSlot(PathOp.ObjectOp): if wires[0].isClosed(): face = Part.Face(wires[0]) if face.Area > 0: - face.translate(FreeCAD.Vector(0.0, 0.0, shape.BoundBox.ZMin - face.BoundBox.ZMin)) - return ('Face', face) - return ('Wire', wires[0]) + face.translate( + FreeCAD.Vector( + 0.0, 0.0, shape.BoundBox.ZMin - face.BoundBox.ZMin + ) + ) + return ("Face", face) + return ("Wire", wires[0]) return False def _makeReference1Enumerations(self, sub, single=False): """Customize Reference1 enumerations based on feature type.""" - PathLog.debug('_makeReference1Enumerations()') + PathLog.debug("_makeReference1Enumerations()") cat = sub[:4] if single: - if cat == 'Face': - return ['Long Edge', 'Short Edge'] - elif cat == 'Edge': - return ['Long Edge'] - elif cat == 'Vert': - return ['Vertex'] - elif cat == 'Vert': - return ['Vertex'] + if cat == "Face": + return ["Long Edge", "Short Edge"] + elif cat == "Edge": + return ["Long Edge"] + elif cat == "Vert": + return ["Vertex"] + elif cat == "Vert": + return ["Vertex"] - return ['Center of Mass', 'Center of BoundBox', - 'Lowest Point', 'Highest Point'] + return ["Center of Mass", "Center of BoundBox", "Lowest Point", "Highest Point"] def _makeReference2Enumerations(self, sub): """Customize Reference2 enumerations based on feature type.""" - PathLog.debug('_makeReference2Enumerations()') + PathLog.debug("_makeReference2Enumerations()") cat = sub[:4] - if cat == 'Vert': - return ['Vertex'] - return ['Center of Mass', 'Center of BoundBox', - 'Lowest Point', 'Highest Point'] + if cat == "Vert": + return ["Vertex"] + return ["Center of Mass", "Center of BoundBox", "Lowest Point", "Highest Point"] def _lineCollisionCheck(self, obj, p1, p2): """Make simple circle with diameter of tool, at start point. @@ -1627,7 +1851,7 @@ class ObjectSlot(PathOp.ObjectOp): else: pathTravel = startShp - self._addDebugObject(pathTravel, 'PathTravel') + self._addDebugObject(pathTravel, "PathTravel") # Check for collision with model try: @@ -1635,7 +1859,7 @@ class ObjectSlot(PathOp.ObjectOp): if cmn.Volume > 0.000001: return True except Exception: - PathLog.debug('Failed to complete path collision check.') + PathLog.debug("Failed to complete path collision check.") return False @@ -1644,7 +1868,7 @@ class ObjectSlot(PathOp.ObjectOp): Make arch face between circles. Fuse and extrude it vertically. Check for collision with model.""" # Make path travel of tool as 3D solid. - if hasattr(self.tool.Diameter, 'Value'): + if hasattr(self.tool.Diameter, "Value"): rad = self.tool.Diameter.Value / 2.0 else: rad = self.tool.Diameter / 2.0 @@ -1695,9 +1919,10 @@ class ObjectSlot(PathOp.ObjectOp): newRadius = arcRadius - rad # PathLog.debug('arcRadius, newRadius: {}, {}'.format(arcRadius, newRadius)) if newRadius <= 0: - msg = translate('PathSlot', - 'Current offset value produces negative radius.') - FreeCAD.Console.PrintError(msg + '\n') + msg = translate( + "Path_Slot", "Current offset value produces negative radius." + ) + FreeCAD.Console.PrintError(msg + "\n") return False else: (pA, pB) = self._makeOffsetArc(p1, p2, arcCenter, newRadius) @@ -1708,9 +1933,10 @@ class ObjectSlot(PathOp.ObjectOp): newRadius = arcRadius + rad # PathLog.debug('arcRadius, newRadius: {}, {}'.format(arcRadius, newRadius)) if newRadius <= 0: - msg = translate('PathSlot', - 'Current offset value produces negative radius.') - FreeCAD.Console.PrintError(msg + '\n') + msg = translate( + "Path_Slot", "Current offset value produces negative radius." + ) + FreeCAD.Console.PrintError(msg + "\n") return False else: (pC, pD) = self._makeOffsetArc(p1, p2, arcCenter, newRadius) @@ -1740,35 +1966,37 @@ class ObjectSlot(PathOp.ObjectOp): part1 = startShp.fuse(boxShp) pathTravel = part1.fuse(endShp) - self._addDebugObject(pathTravel, 'PathTravel') + self._addDebugObject(pathTravel, "PathTravel") # Check for collision with model try: cmn = self.base.Shape.common(pathTravel) if cmn.Volume > 0.000001: - print ("volume=",cmn.Volume) + # print("volume=", cmn.Volume) return True except Exception: - PathLog.debug('Failed to complete path collision check.') + PathLog.debug("Failed to complete path collision check.") return False def _addDebugObject(self, objShape, objName): if self.showDebugObjects: - do = FreeCAD.ActiveDocument.addObject('Part::Feature', 'tmp_' + objName) + do = FreeCAD.ActiveDocument.addObject("Part::Feature", "tmp_" + objName) do.Shape = objShape do.purgeTouched() self.tmpGrp.addObject(do) + + # Eclass def SetupProperties(): - ''' SetupProperties() ... Return list of properties required for operation.''' + """SetupProperties() ... Return list of properties required for operation.""" return [tup[1] for tup in ObjectSlot.opPropertyDefinitions(False)] def Create(name, obj=None, parentJob=None): - '''Create(name) ... Creates and returns a Slot operation.''' + """Create(name) ... Creates and returns a Slot operation.""" if obj is None: obj = FreeCAD.ActiveDocument.addObject("Path::FeaturePython", name) obj.Proxy = ObjectSlot(obj, name, parentJob) diff --git a/src/Mod/Path/PathScripts/PathSlotGui.py b/src/Mod/Path/PathScripts/PathSlotGui.py index 518209e562..1c620d4c57 100644 --- a/src/Mod/Path/PathScripts/PathSlotGui.py +++ b/src/Mod/Path/PathScripts/PathSlotGui.py @@ -22,7 +22,7 @@ import FreeCAD import FreeCADGui -import PathGui as PGui # ensure Path/Gui/Resources are loaded +import PathGui as PGui # ensure Path/Gui/Resources are loaded import PathScripts.PathSlot as PathSlot import PathScripts.PathGui as PathGui import PathScripts.PathOpGui as PathOpGui @@ -38,37 +38,42 @@ __contributors__ = "" DEBUG = False + def debugMsg(msg): global DEBUG if DEBUG: - FreeCAD.Console.PrintMessage('PathSlotGui:: ' + msg + '\n') + FreeCAD.Console.PrintMessage("PathSlotGui:: " + msg + "\n") class TaskPanelOpPage(PathOpGui.TaskPanelPage): - '''Page controller class for the Slot operation.''' + """Page controller class for the Slot operation.""" def getForm(self): - '''getForm() ... returns UI''' - debugMsg('getForm()') + """getForm() ... returns UI""" + debugMsg("getForm()") return FreeCADGui.PySideUic.loadUi(":/panels/PageOpSlotEdit.ui") def initPage(self, obj): - '''initPage(obj) ... Is called after getForm() to initiate the task panel.''' - debugMsg('initPage()') + """initPage(obj) ... Is called after getForm() to initiate the task panel.""" + debugMsg("initPage()") # pylint: disable=attribute-defined-outside-init self.CATS = [None, None] - self.propEnums = PathSlot.ObjectSlot.opPropertyEnumerations(False) + self.propEnums = PathSlot.ObjectSlot.propertyEnumerations(dataType="raw") self.ENUMS = dict() self.setTitle("Slot - " + obj.Label) # retrieve property enumerations # Requirements due to Gui::QuantitySpinBox class use in UI panel - self.geo1Extension = PathGui.QuantitySpinBox(self.form.geo1Extension, obj, 'ExtendPathStart') - self.geo2Extension = PathGui.QuantitySpinBox(self.form.geo2Extension, obj, 'ExtendPathEnd') + self.geo1Extension = PathGui.QuantitySpinBox( + self.form.geo1Extension, obj, "ExtendPathStart" + ) + self.geo2Extension = PathGui.QuantitySpinBox( + self.form.geo2Extension, obj, "ExtendPathEnd" + ) def setFields(self, obj): - '''setFields(obj) ... transfers obj's property values to UI''' - debugMsg('setFields()') - debugMsg('... calling updateVisibility()') + """setFields(obj) ... transfers obj's property values to UI""" + debugMsg("setFields()") + debugMsg("... calling updateVisibility()") self.updateVisibility() self.updateQuantitySpinBoxes() @@ -76,18 +81,22 @@ class TaskPanelOpPage(PathOpGui.TaskPanelPage): self.setupToolController(obj, self.form.toolController) self.setupCoolant(obj, self.form.coolantController) - enums = self.propEnums['Reference1'] - if 'Reference1' in self.ENUMS: - enums = self.ENUMS['Reference1'] - debugMsg(' -enums1: {}'.format(enums)) - idx = enums.index(obj.Reference1) + enums = [t[1] for t in self.propEnums["Reference1"]] + if "Reference1" in self.ENUMS: + enums = self.ENUMS["Reference1"] + debugMsg(" -enums1: {}".format(enums)) + idx = 0 + if obj.Reference1 in enums: + idx = enums.index(obj.Reference1) self.form.geo1Reference.setCurrentIndex(idx) - enums = self.propEnums['Reference2'] - if 'Reference2' in self.ENUMS: - enums = self.ENUMS['Reference2'] - debugMsg(' -enums2: {}'.format(enums)) - idx = enums.index(obj.Reference2) + enums = [t[1] for t in self.propEnums["Reference2"]] + if "Reference2" in self.ENUMS: + enums = self.ENUMS["Reference2"] + debugMsg(" -enums2: {}".format(enums)) + idx = 0 + if obj.Reference2 in enums: + idx = enums.index(obj.Reference2) self.form.geo2Reference.setCurrentIndex(idx) self.selectInComboBox(obj.LayerMode, self.form.layerMode) @@ -103,8 +112,8 @@ class TaskPanelOpPage(PathOpGui.TaskPanelPage): self.geo2Extension.updateSpinBox() def getFields(self, obj): - '''getFields(obj) ... transfers values from UI to obj's proprties''' - debugMsg('getFields()') + """getFields(obj) ... transfers values from UI to obj's proprties""" + debugMsg("getFields()") self.updateToolController(obj, self.form.toolController) self.updateCoolant(obj, self.form.coolantController) @@ -114,17 +123,19 @@ class TaskPanelOpPage(PathOpGui.TaskPanelPage): obj.Reference2 = str(self.form.geo2Reference.currentText()) self.geo2Extension.updateProperty() - val = self.propEnums['LayerMode'][self.form.layerMode.currentIndex()] + val = self.propEnums["LayerMode"][self.form.layerMode.currentIndex()][1] obj.LayerMode = val - val = self.propEnums['PathOrientation'][self.form.pathOrientation.currentIndex()] + val = self.propEnums["PathOrientation"][ + self.form.pathOrientation.currentIndex() + ][1] obj.PathOrientation = val obj.ReverseDirection = self.form.reverseDirection.isChecked() def getSignalsForUpdate(self, obj): - '''getSignalsForUpdate(obj) ... return list of signals for updating obj''' - debugMsg('getSignalsForUpdate()') + """getSignalsForUpdate(obj) ... return list of signals for updating obj""" + debugMsg("getSignalsForUpdate()") signals = [] signals.append(self.form.toolController.currentIndexChanged) signals.append(self.form.coolantController.currentIndexChanged) @@ -138,10 +149,10 @@ class TaskPanelOpPage(PathOpGui.TaskPanelPage): return signals def updateVisibility(self, sentObj=None): - '''updateVisibility(sentObj=None)... Updates visibility of Tasks panel objects.''' + """updateVisibility(sentObj=None)... Updates visibility of Tasks panel objects.""" # debugMsg('updateVisibility()') hideFeatures = True - if hasattr(self.obj, 'Base'): + if hasattr(self.obj, "Base"): if self.obj.Base: self.form.customPoints.hide() self.form.featureReferences.show() @@ -152,36 +163,40 @@ class TaskPanelOpPage(PathOpGui.TaskPanelPage): subCnt = len(sublist) if subCnt == 1: - debugMsg(' -subCnt == 1') + debugMsg(" -subCnt == 1") # Save value, then reset choices n1 = sublist[0] - s1 = getattr(base.Shape, n1) + # s1 = getattr(base.Shape, n1) # Show Reference1 and cusomize options within self.form.geo1Reference.show() self.form.geo1Reference_label.show() - self.form.geo1Reference_label.setText('Reference: {}'.format(n1)) + self.form.geo1Reference_label.setText("Reference: {}".format(n1)) self.customizeReference_1(n1, single=True) # Hide Reference2 self.form.geo2Reference.hide() self.form.geo2Reference_label.hide() - self.form.geo2Reference_label.setText('End Reference') + self.form.geo2Reference_label.setText("End Reference") if self.CATS[1]: self.CATS[1] = None elif subCnt == 2: - debugMsg(' -subCnt == 2') + debugMsg(" -subCnt == 2") n1 = sublist[0] n2 = sublist[1] - s1 = getattr(base.Shape, n1) - s2 = getattr(base.Shape, n2) + # s1 = getattr(base.Shape, n1) + # s2 = getattr(base.Shape, n2) # Show Reference1 and cusomize options within self.form.geo1Reference.show() self.form.geo1Reference_label.show() - self.form.geo1Reference_label.setText('Start Reference: {}'.format(n1)) + self.form.geo1Reference_label.setText( + "Start Reference: {}".format(n1) + ) self.customizeReference_1(n1) # Show Reference2 and cusomize options within self.form.geo2Reference.show() self.form.geo2Reference_label.show() - self.form.geo2Reference_label.setText('End Reference: {}'.format(n2)) + self.form.geo2Reference_label.setText( + "End Reference: {}".format(n2) + ) self.customizeReference_2(n2) else: self.form.pathOrientation_label.hide() @@ -190,13 +205,13 @@ class TaskPanelOpPage(PathOpGui.TaskPanelPage): if hideFeatures: # reset values self.CATS = [None, None] - self.selectInComboBox('Start to End', self.form.pathOrientation) + self.selectInComboBox("Start to End", self.form.pathOrientation) # hide inputs and show message self.form.featureReferences.hide() self.form.customPoints.show() def customizeReference_1(self, sub, single=False): - debugMsg('customizeReference_1()') + debugMsg("customizeReference_1()") # Customize Reference1 combobox options # by removing unavailable choices cat = sub[:4] @@ -204,14 +219,14 @@ class TaskPanelOpPage(PathOpGui.TaskPanelPage): self.CATS[0] = cat slot = PathSlot.ObjectSlot enums = slot._makeReference1Enumerations(slot, sub, single) - self.ENUMS['Reference1'] = enums - debugMsg('Ref1: {}'.format(enums)) - self._updateComboBox(self.form.geo1Reference, enums) - # self.form.geo1Reference.setCurrentIndex(0) - # self.form.geo1Reference.setCurrentText(enums[0]) + self.ENUMS["Reference1"] = enums + debugMsg("Ref1: {}".format(enums)) + rawEnums = slot.propertyEnumerations(dataType="raw")["Reference1"] + enumTups = [(t, d) for t, d in rawEnums if d in enums] + self._updateComboBox(self.form.geo1Reference, enumTups) def customizeReference_2(self, sub): - debugMsg('customizeReference_2()') + debugMsg("customizeReference_2()") # Customize Reference2 combobox options # by removing unavailable choices cat = sub[:4] @@ -219,29 +234,56 @@ class TaskPanelOpPage(PathOpGui.TaskPanelPage): self.CATS[1] = cat slot = PathSlot.ObjectSlot enums = slot._makeReference2Enumerations(slot, sub) - self.ENUMS['Reference2'] = enums - debugMsg('Ref2: {}'.format(enums)) - self._updateComboBox(self.form.geo2Reference, enums) - # self.form.geo2Reference.setCurrentIndex(0) - # self.form.geo2Reference.setCurrentText(enums[0]) + self.ENUMS["Reference2"] = enums + debugMsg("Ref2: {}".format(enums)) + rawEnums = slot.propertyEnumerations(dataType="raw")["Reference2"] + enumTups = [(t, d) for t, d in rawEnums if d in enums] + self._updateComboBox(self.form.geo2Reference, enumTups) def registerSignalHandlers(self, obj): # debugMsg('registerSignalHandlers()') # self.form.pathOrientation.currentIndexChanged.connect(self.updateVisibility) pass - def _updateComboBox(self, cBox, enums): + def _updateComboBox(self, cBox, enumTups): cBox.blockSignals(True) cBox.clear() - cBox.addItems(enums) + for text, data in enumTups: # load enumerations + cBox.addItem(text, data) + self.selectInSlotComboBox(data, cBox) cBox.blockSignals(False) -Command = PathOpGui.SetupOperation('Slot', - PathSlot.Create, - TaskPanelOpPage, - 'Path_Slot', - QtCore.QT_TRANSLATE_NOOP("Path_Slot", "Slot"), - QtCore.QT_TRANSLATE_NOOP("Path_Slot", "Create a Slot operation from selected geometry or custom points."), - PathSlot.SetupProperties) + def selectInSlotComboBox(self, name, combo): + """selectInSlotComboBox(name, combo) ... + helper function to select a specific value in a combo box.""" + + # Search using currentData and return if found + newindex = combo.findData(name) + if newindex >= 0: + combo.setCurrentIndex(newindex) + return + + # if not found, search using current text + newindex = combo.findText(name, QtCore.Qt.MatchFixedString) + if newindex >= 0: + combo.setCurrentIndex(newindex) + return + + # not found, return unchanged + combo.setCurrentIndex(0) + return + + +Command = PathOpGui.SetupOperation( + "Slot", + PathSlot.Create, + TaskPanelOpPage, + "Path_Slot", + QtCore.QT_TRANSLATE_NOOP("Path_Slot", "Slot"), + QtCore.QT_TRANSLATE_NOOP( + "Path_Slot", "Create a Slot operation from selected geometry or custom points." + ), + PathSlot.SetupProperties, +) FreeCAD.Console.PrintLog("Loading PathSlotGui... done\n")