From d5068ffc5cfe7524ee72e3bc6ffd17208dfdd18a Mon Sep 17 00:00:00 2001 From: Markus Lampert Date: Fri, 9 Jun 2017 21:14:22 -0700 Subject: [PATCH] Some cleanup and refactoring. --- src/Mod/Path/PathScripts/PathJob.py | 69 ++++++++++-------- src/Mod/Path/PathScripts/PathPost.py | 2 +- src/Mod/Path/PathScripts/PathPreferences.py | 2 +- .../Path/PathScripts/PathToolController.py | 72 +++++++++++++------ 4 files changed, 90 insertions(+), 55 deletions(-) diff --git a/src/Mod/Path/PathScripts/PathJob.py b/src/Mod/Path/PathScripts/PathJob.py index 2d5c6c2218..6c0660bcb4 100644 --- a/src/Mod/Path/PathScripts/PathJob.py +++ b/src/Mod/Path/PathScripts/PathJob.py @@ -41,7 +41,7 @@ if sys.version_info.major >= 3: xrange = range PathLog.setLevel(PathLog.Level.INFO, PathLog.thisModule()) -PathLog.trackModule() +#PathLog.trackModule() FreeCADGui = None if FreeCAD.GuiUp: @@ -54,12 +54,14 @@ def translate(context, text, disambig=None): return QtCore.QCoreApplication.translate(context, text, disambig) class JobTemplate: + '''Attribute and sub element strings for template export/import.''' Job = 'Job' PostProcessor = 'post' PostProcessorArgs = 'post_args' PostProcessorOutputFile = 'output' GeometryTolerance = 'tol' Description = 'desc' + ToolController = 'ToolController' class ObjectPathJob: @@ -95,6 +97,8 @@ class ObjectPathJob: self.assignTemplate(obj, template) def assignTemplate(self, obj, template): + '''assignTemplate(obj, template) ... extract the properties from the given template file and assign to receiver. + This will also create any TCs stored in the template.''' if template: tree = xml.parse(template) for job in tree.getroot().iter(JobTemplate.Job): @@ -110,11 +114,23 @@ class ObjectPathJob: obj.PostProcessorOutputFile = job.get(JobTemplate.PostProcessorOutputFile) if job.get(JobTemplate.Description): obj.Description = job.get(JobTemplate.Description) - for tc in tree.getroot().iter('ToolController'): + for tc in tree.getroot().iter(JobTemplate.ToolController): PathToolController.CommandPathToolController.FromTemplate(obj, tc) else: PathToolController.CommandPathToolController.Create(obj.Name) + def templateAttrs(self, obj): + '''templateAttrs(obj) ... answer a dictionary with all properties of the receiver that should be stored in a template file.''' + attrs = {} + if obj.PostProcessor: + attrs[JobTemplate.PostProcessor] = obj.PostProcessor + attrs[JobTemplate.PostProcessorArgs] = obj.PostProcessorArgs + if obj.PostProcessorOutputFile: + attrs[JobTemplate.PostProcessorOutputFile] = obj.PostProcessorOutputFile + attrs[JobTemplate.GeometryTolerance] = str(obj.GeometryTolerance.Value) + if obj.Description: + attrs[JobTemplate.Description] = obj.Description + def __getstate__(self): return None @@ -362,20 +378,29 @@ class DlgJobCreate: self.dialog.cbTemplate.setCurrentIndex(index) def templateFilesIn(self, path): + '''templateFilesIn(path) ... answer all file in the given directory which fit the job template naming convention. + PathJob template files are name job_*.xml''' PathLog.track(path) return glob.glob(path + '/job_*.xml') def getModel(self): + '''answer the base model selected for the job''' label = self.dialog.cbModel.currentText() return filter(lambda obj: obj.Label == label, FreeCAD.ActiveDocument.Objects)[0] def getTemplate(self): + '''answer the file name of the template to be assigned''' return self.dialog.cbTemplate.itemData(self.dialog.cbTemplate.currentIndex()) def exec_(self): return self.dialog.exec_() class CommandJobCreate: + ''' + Command used to creat a command. + When activated the command opens a dialog allowing the user to select a base object (has to be a solid) + and a template to be used for the initial creation. + ''' def GetResources(self): return {'Pixmap': 'Path-Job', @@ -389,11 +414,11 @@ class CommandJobCreate: def Activated(self): dialog = DlgJobCreate() if dialog.exec_() == 1: - self.Create(dialog.getModel(), dialog.getTemplate()) + self.Execute(dialog.getModel(), dialog.getTemplate()) FreeCAD.ActiveDocument.recompute() @classmethod - def Create(cls, base, template): + def Execute(cls, base, template): FreeCADGui.addModule('PathScripts.PathJob') FreeCAD.ActiveDocument.openTransaction(translate("Path_Job", "Create Job")) snippet = '''App.ActiveDocument.addObject("Path::FeatureCompoundPython", "Job") @@ -406,6 +431,12 @@ PathScripts.PathJob.ObjectPathJob(App.ActiveDocument.ActiveObject, App.ActiveDoc FreeCAD.ActiveDocument.abortTransaction() class CommandJobExportTemplate: + ''' + Command to export a template of a given job. + Opens a dialog to select the file to store the template in. If the template is stored in Path's + file path (see preferences) and named in accordance with job_*.xml it will automatically be found + on Job creation and be available for selection. + ''' def GetResources(self): return {'Pixmap': 'Path-Job', @@ -422,41 +453,17 @@ class CommandJobExportTemplate: "Path - Job Template", PathPreferences.filePath(), "job_*.xml")[0] - if foo: + if foo: self.Execute(job, foo) @classmethod def Execute(cls, job, path): root = xml.Element('PathJobTemplate') - - # first collect all attributes of a job to be stored - attrs = {} - if job.PostProcessor: - attrs[JobTemplate.PostProcessor] = job.PostProcessor - attrs[JobTemplate.PostProcessorArgs] = job.PostProcessorArgs - if job.PostProcessorOutputFile: - attrs[JobTemplate.PostProcessorOutputFile] = job.PostProcessorOutputFile - attrs[JobTemplate.GeometryTolerance] = str(job.GeometryTolerance.Value) - if job.Description: - attrs[JobTemplate.Description] = job.Description - xml.SubElement(root, JobTemplate.Job, attrs) - - # then store all tool controllers + xml.SubElement(root, JobTemplate.Job, job.Proxy.templateAttrs(job)) for obj in job.Group: if hasattr(obj, 'Tool') and hasattr(obj, 'SpindleDir'): - attrs = {} - attrs['label'] = ("%s" % (obj.Label)) - attrs['nr'] = ("%d" % (obj.ToolNumber)) - attrs['vfeed'] = ("%s" % (obj.VertFeed)) - attrs['hfeed'] = ("%s" % (obj.HorizFeed)) - attrs['vrapid'] = ("%s" % (obj.VertRapid)) - attrs['hrapid'] = ("%s" % (obj.HorizRapid)) - attrs['speed'] = ("%f" % (obj.SpindleSpeed)) - attrs['dir'] = ("%s" % (obj.SpindleDir)) - - tc = xml.SubElement(root, 'ToolController', attrs) + tc = xml.SubElement(root, JobTemplate.ToolController, obj.Proxy.templateAttrs(obj)) tc.append(xml.fromstring(obj.Tool.Content)) - xml.ElementTree(root).write(path, pretty_print=True) if FreeCAD.GuiUp: diff --git a/src/Mod/Path/PathScripts/PathPost.py b/src/Mod/Path/PathScripts/PathPost.py index 8a8075f58e..240cdd36dc 100644 --- a/src/Mod/Path/PathScripts/PathPost.py +++ b/src/Mod/Path/PathScripts/PathPost.py @@ -41,7 +41,7 @@ from PySide import QtCore, QtGui LOG_MODULE = PathLog.thisModule() PathLog.setLevel(PathLog.Level.DEBUG, LOG_MODULE) -PathLog.trackModule(LOG_MODULE) +#PathLog.trackModule(LOG_MODULE) # Qt tanslation handling def translate(context, text, disambig=None): diff --git a/src/Mod/Path/PathScripts/PathPreferences.py b/src/Mod/Path/PathScripts/PathPreferences.py index 4dbbc01fc4..719dff0d20 100644 --- a/src/Mod/Path/PathScripts/PathPreferences.py +++ b/src/Mod/Path/PathScripts/PathPreferences.py @@ -28,7 +28,7 @@ import os import PathScripts.PathLog as PathLog PathLog.setLevel(PathLog.Level.INFO, PathLog.thisModule()) -PathLog.trackModule() +#PathLog.trackModule() class PathPreferences: DefaultFilePath = "DefaultFilePath" diff --git a/src/Mod/Path/PathScripts/PathToolController.py b/src/Mod/Path/PathScripts/PathToolController.py index 494f620a2e..0988283fd8 100644 --- a/src/Mod/Path/PathScripts/PathToolController.py +++ b/src/Mod/Path/PathScripts/PathToolController.py @@ -45,8 +45,19 @@ else: def translate(context, text, disambig=None): return QtCore.QCoreApplication.translate(context, text, disambig) +class ToolControllerTemplate: + '''Attribute and sub element strings for template export/import.''' + Label = 'label' + ToolNumber = 'nr' + VertFeed = 'vfeed' + HorizFeed = 'hfeed' + VertRapid = 'vrapid' + HorizRapid = 'hrapid' + SpindleSpeed = 'speed' + SpindleDir = 'dir' + Tool = 'Tool' -class ToolController(): +class ToolController: def __init__(self, obj, tool=1): PathLog.track('tool: {}'.format(tool)) @@ -65,6 +76,41 @@ class ToolController(): mode = 2 obj.setEditorMode('Placement', mode) + def assignTemplate(self, obj, template): + '''assignTemplate(obj, xmlItem) ... extract properties from xmlItem and assign to receiver.''' + if template.get(ToolControllerTemplate.VertFeed): + obj.HorizFeed = template.get(ToolControllerTemplate.VertFeed) + if template.get(ToolControllerTemplate.HorizFeed): + obj.HorizFeed = template.get(ToolControllerTemplate.HorizFeed) + if template.get(ToolControllerTemplate.VertRapid): + obj.HorizRapid = template.get(ToolControllerTemplate.VertRapid) + if template.get(ToolControllerTemplate.HorizRapid): + obj.HorizRapid = template.get(ToolControllerTemplate.HorizRapid) + if template.get(ToolControllerTemplate.SpindleSpeed): + obj.SpindleSpeed = float(template.get(ToolControllerTemplate.SpindleSpeed)) + if template.get(ToolControllerTemplate.SpindleDir): + obj.SpindleDir = template.get(ToolControllerTemplate.SpindleDir) + if template.get(ToolControllerTemplate.ToolNumber): + obj.ToolNumber = int(template.get(ToolControllerTemplate.ToolNumber)) + + for t in template.iter(ToolControllerTemplate.Tool): + tool = Path.Tool() + tool.fromTemplate(xml.tostring(t)) + obj.Tool = tool + + def templateAttrs(self, obj): + '''templateAttrs(obj) ... answer a dictionary with all properties that should be stored for a template.''' + attrs = {} + attrs[ToolControllerTemplate.Label] = ("%s" % (obj.Label)) + attrs[ToolControllerTemplate.ToolNumber] = ("%d" % (obj.ToolNumber)) + attrs[ToolControllerTemplate.VertFeed] = ("%s" % (obj.VertFeed)) + attrs[ToolControllerTemplate.HorizFeed] = ("%s" % (obj.HorizFeed)) + attrs[ToolControllerTemplate.VertRapid] = ("%s" % (obj.VertRapid)) + attrs[ToolControllerTemplate.HorizRapid] = ("%s" % (obj.HorizRapid)) + attrs[ToolControllerTemplate.SpindleSpeed] = ("%f" % (obj.SpindleSpeed)) + attrs[ToolControllerTemplate.SpindleDir] = ("%s" % (obj.SpindleDir)) + return attrs + def execute(self, obj): PathLog.track() @@ -208,30 +254,12 @@ class CommandPathToolController: def FromTemplate(job, template, assignViewProvider=True): PathLog.track() - obj = FreeCAD.ActiveDocument.addObject("Path::FeaturePython", template.get('label')) - PathScripts.PathToolController.ToolController(obj) + obj = FreeCAD.ActiveDocument.addObject("Path::FeaturePython", template.get(ToolControllerTemplate.Label)) + tc = PathScripts.PathToolController.ToolController(obj) if assignViewProvider: PathScripts.PathToolController._ViewProviderToolController(obj.ViewObject) - if template.get('vfeed'): - obj.HorizFeed = template.get('vfeed') - if template.get('hfeed'): - obj.HorizFeed = template.get('hfeed') - if template.get('vrapid'): - obj.HorizRapid = template.get('vrapid') - if template.get('hrapid'): - obj.HorizRapid = template.get('hrapid') - if template.get('speed'): - obj.SpindleSpeed = float(template.get('speed')) - if template.get('dir'): - obj.SpindleDir = template.get('dir') - if template.get('nr'): - obj.ToolNumber = int(template.get('nr')) - - for t in template.iter('Tool'): - tool = Path.Tool() - tool.fromTemplate(xml.tostring(t)) - obj.Tool = tool + tc.assignTemplate(obj, template) PathUtils.addToJob(obj, job.Name)