def loadSettings(self): self.form.leDefaultFilePath.setText(PathPreferences.defaultFilePath()) self.form.leDefaultJobTemplate.setText(PathPreferences.defaultJobTemplate()) blacklist = PathPreferences.postProcessorBlacklist() for processor in PathPreferences.allAvailablePostProcessors(): item = QtGui.QListWidgetItem(processor) if processor in blacklist: item.setCheckState(QtCore.Qt.CheckState.Unchecked) else: item.setCheckState(QtCore.Qt.CheckState.Checked) item.setFlags( QtCore.Qt.ItemFlag.ItemIsSelectable | QtCore.Qt.ItemFlag.ItemIsEnabled | QtCore.Qt.ItemFlag.ItemIsUserCheckable) self.form.postProcessorList.addItem(item) self.verifyAndUpdateDefaultPostProcessorWith(PathPreferences.defaultPostProcessor()) self.form.defaultPostProcessorArgs.setText(PathPreferences.defaultPostProcessorArgs()) geomTol = Units.Quantity(PathPreferences.defaultGeometryTolerance(), Units.Length) self.form.geometryTolerance.setText(geomTol.UserString) self.form.curveAccuracy.setText(Units.Quantity(PathPreferences.defaultLibAreaCurveAccuracy(), Units.Length).UserString) self.form.leOutputFile.setText(PathPreferences.defaultOutputFile()) self.selectComboEntry(self.form.cboOutputPolicy, PathPreferences.defaultOutputPolicy()) self.form.tbDefaultFilePath.clicked.connect(self.browseDefaultFilePath) self.form.tbDefaultJobTemplate.clicked.connect(self.browseDefaultJobTemplate) self.form.postProcessorList.itemEntered.connect(self.setProcessorListTooltip) self.form.postProcessorList.itemChanged.connect(self.verifyAndUpdateDefaultPostProcessor) self.form.defaultPostProcessor.currentIndexChanged.connect(self.updateDefaultPostProcessorToolTip) self.form.tbOutputFile.clicked.connect(self.browseOutputFile) self.loadStockSettings()
def resolveFileName(self, job): path = PathPreferences.defaultOutputFile() if job.PostProcessorOutputFile: path = job.PostProcessorOutputFile filename = path if '%D' in filename: D = FreeCAD.ActiveDocument.FileName if D: D = os.path.dirname(D) # in case the document is in the current working directory if not D: D = '.' else: FreeCAD.Console.PrintError("Please save document in order to resolve output path!\n") return None filename = filename.replace('%D', D) if '%d' in filename: d = FreeCAD.ActiveDocument.Label filename = filename.replace('%d', d) if '%j' in filename: j = job.Label filename = filename.replace('%j', j) if '%M' in filename: pref = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Macro") M = pref.GetString("MacroPath", FreeCAD.getUserAppDataDir()) filename = filename.replace('%M', M) policy = PathPreferences.defaultOutputPolicy() openDialog = policy == 'Open File Dialog' if os.path.isdir(filename) or not os.path.isdir(os.path.dirname(filename)): # Either the entire filename resolves into a directory or the parent directory doesn't exist. # Either way I don't know what to do - ask for help openDialog = True if os.path.isfile(filename) and not openDialog: if policy == 'Open File Dialog on conflict': openDialog = True elif policy == 'Append Unique ID on conflict': fn, ext = os.path.splitext(filename) nr = fn[-3:] n = 1 if nr.isdigit(): n = int(nr) while os.path.isfile("%s%03d%s" % (fn, n, ext)): n = n + 1 filename = "%s%03d%s" % (fn, n, ext) if openDialog: foo = QtGui.QFileDialog.getSaveFileName(QtGui.QApplication.activeWindow(), "Output File", filename) if foo: filename = foo[0] else: filename = None return filename
def resolveFileName(job, subpartname, sequencenumber): PathLog.track(subpartname, sequencenumber) validPathSubstitutions = ["D", "d", "M", "j"] validFilenameSubstitutions = ["j", "d", "T", "t", "W", "O", "S"] # Look for preference default outputpath, filename = os.path.split(PathPreferences.defaultOutputFile()) filename, ext = os.path.splitext(filename) # Override with document default if it exists if job.PostProcessorOutputFile: matchstring = job.PostProcessorOutputFile candidateOutputPath, candidateFilename = os.path.split(matchstring) if candidateOutputPath: outputpath = candidateOutputPath if candidateFilename: filename, ext = os.path.splitext(candidateFilename) # Strip any invalid substitutions from the ouputpath for match in re.findall("%(.)", outputpath): if match not in validPathSubstitutions: outputpath = outputpath.replace(f"%{match}", "") # if nothing else, use current directory if not outputpath: outputpath = "." # Strip any invalid substitutions from the filename for match in re.findall("%(.)", filename): if match not in validFilenameSubstitutions: filename = filename.replace(f"%{match}", "") # if no filename, use the active document label if not filename: filename = FreeCAD.ActiveDocument.Label # if no extension, use something sensible if not ext: ext = ".nc" # By now we should have a sanitized path, filename and extension to work with PathLog.track(f"path: {outputpath} name: {filename} ext: {ext}") fullPath = processFileNameSubstitutions( job, subpartname, sequencenumber, outputpath, filename, ext, ) # This section determines whether user interaction is necessary policy = PathPreferences.defaultOutputPolicy() openDialog = policy == "Open File Dialog" # if os.path.isdir(filename) or not os.path.isdir(os.path.dirname(filename)): # # Either the entire filename resolves into a directory or the parent # # directory doesn't exist. # # Either way I don't know what to do - ask for help # openDialog = True if not FreeCAD.GuiUp: # if testing, or scripting, never open dialog. policy = "Append Unique ID on conflict" openDialog = False if os.path.isfile(fullPath) and not openDialog: if policy == "Open File Dialog on conflict": openDialog = True elif policy == "Append Unique ID on conflict": fn, ext = os.path.splitext(fullPath) nr = fn[-3:] n = 1 if nr.isdigit(): n = int(nr) while os.path.isfile("%s%03d%s" % (fn, n, ext)): n = n + 1 fullPath = "%s%03d%s" % (fn, n, ext) if openDialog: foo = QtGui.QFileDialog.getSaveFileName( QtGui.QApplication.activeWindow(), "Output File", filename ) if foo[0]: fullPath = foo[0] else: fullPath = None # remove any unused substitution strings: for s in validPathSubstitutions + validFilenameSubstitutions: fullPath = fullPath.replace(f"%{s}", "") fullPath = os.path.normpath(fullPath) PathLog.track(fullPath) return fullPath
def resolveFileName(job, subpartname, sequencenumber): PathLog.track(subpartname, sequencenumber) validPathSubstitutions = ["D", "d", "M", "j"] validFilenameSubstitutions = ["j", "d", "T", "t", "W", "O", "S"] # Look for preference default outputpath, filename = os.path.split(PathPreferences.defaultOutputFile()) filename, ext = os.path.splitext(filename) # Override with document default if it exists if job.PostProcessorOutputFile: matchstring = job.PostProcessorOutputFile candidateOutputPath, candidateFilename = os.path.split(matchstring) if candidateOutputPath: outputpath = candidateOutputPath if candidateFilename: filename, ext = os.path.splitext(candidateFilename) # Strip any invalid substitutions from the ouputpath for match in re.findall("%(.)", outputpath): if match not in validPathSubstitutions: outputpath = outputpath.replace(f"%{match}", "") # if nothing else, use current directory if not outputpath: outputpath = "." # Strip any invalid substitutions from the filename for match in re.findall("%(.)", filename): if match not in validFilenameSubstitutions: filename = filename.replace(f"%{match}", "") # if no filename, use the active document label if not filename: filename = FreeCAD.ActiveDocument.Label # if no extension, use something sensible if not ext: ext = ".nc" # By now we should have a sanitized path, filename and extension to work with PathLog.track(f"path: {outputpath} name: {filename} ext: {ext}") # The following section allows substitution within the path part PathLog.track(f"path before substitution: {outputpath}") if "%D" in outputpath: # Directory of active document D = FreeCAD.ActiveDocument.FileName if D: D = os.path.dirname(D) # in case the document is in the current working directory if not D: D = "." else: FreeCAD.Console.PrintError( "Please save document in order to resolve output path!\n" ) return None outputpath = outputpath.replace("%D", D) if "%M" in outputpath: M = FreeCAD.getUserMacroDir() outputpath = outputpath.replace("%M", M) # Use the file label if "%d" in outputpath: d = FreeCAD.ActiveDocument.Label outputpath = outputpath.replace("%d", d) # Use the name of the active job object if "%j" in outputpath: j = job.Label outputpath = outputpath.replace("%j", j) PathLog.track(f"path after substitution: {outputpath}") # The following section allows substitution within the filename part PathLog.track(f"filename before substitution: {filename}") # Use the file label if "%d" in filename: d = FreeCAD.ActiveDocument.Label filename = filename.replace("%d", d) # Use the name of the active job object if "%j" in filename: j = job.Label filename = filename.replace("%j", j) # Use the sequnce number if explicitly called if "%S" in filename: j = job.Label filename = filename.replace("%S", str(sequencenumber)) # This section handles unique names for splitting output if job.SplitOutput: PathLog.track() if "%T" in filename and job.OrderOutputBy == "Tool": filename = filename.replace("%T", subpartname) if "%t" in filename and job.OrderOutputBy == "Tool": filename = filename.replace("%t", subpartname) if "%W" in filename and job.OrderOutputBy == "Fixture": filename = filename.replace("%W", subpartname) if "%O" in filename and job.OrderOutputBy == "Operation": filename = filename.replace("%O", subpartname) if ( "%S" in filename ): # We always add a sequence number but the user can say where filename = filename.replace("%S", str(sequencenumber)) else: filename = f"{filename}-{sequencenumber}" PathLog.track(f"filename after substitution: {filename}") if not ext: ext = ".nc" PathLog.track(f"file extension: {ext}") fullPath = f"{outputpath}{os.path.sep}{filename}{ext}" PathLog.track(f"full filepath: {fullPath}") # This section determines whether user interaction is necessary policy = PathPreferences.defaultOutputPolicy() openDialog = policy == "Open File Dialog" # if os.path.isdir(filename) or not os.path.isdir(os.path.dirname(filename)): # # Either the entire filename resolves into a directory or the parent directory doesn't exist. # # Either way I don't know what to do - ask for help # openDialog = True if not FreeCAD.GuiUp: # if testing, or scripting, never open dialog. policy = "Append Unique ID on conflict" openDialog = False if os.path.isfile(fullPath) and not openDialog: if policy == "Open File Dialog on conflict": openDialog = True elif policy == "Append Unique ID on conflict": fn, ext = os.path.splitext(fullPath) nr = fn[-3:] n = 1 if nr.isdigit(): n = int(nr) while os.path.isfile("%s%03d%s" % (fn, n, ext)): n = n + 1 fullPath = "%s%03d%s" % (fn, n, ext) if openDialog: foo = QtGui.QFileDialog.getSaveFileName( QtGui.QApplication.activeWindow(), "Output File", filename ) if foo[0]: fullPath = foo[0] else: fullPath = None # remove any unused substitution strings: for s in validPathSubstitutions + validFilenameSubstitutions: fullPath = fullPath.replace(f"%{s}", "") fullPath = os.path.normpath(fullPath) PathLog.track(fullPath) return fullPath