Refactored post processor into its own class, concentrating all post processor script operations.

This commit is contained in:
ml 2016-10-23 15:24:50 -07:00 committed by wmayer
parent f5f85eba70
commit 4865a2c39d
5 changed files with 154 additions and 99 deletions

View File

@ -47,6 +47,7 @@ SET(PathScripts_SRCS
PathScripts/PathStock.py
PathScripts/PathPlane.py
PathScripts/PathPost.py
PathScripts/PathPostProcessor.py
PathScripts/PathLoadTool.py
PathScripts/PathToolLenOffset.py
PathScripts/PathComment.py

View File

@ -27,7 +27,7 @@ import Path
from PySide import QtCore, QtGui
import os
import glob
#import PathLoadTool
from PathScripts.PathPostProcessor import PostProcessor
import Draft
@ -47,57 +47,6 @@ except AttributeError:
def translate(context, text, disambig=None):
return QtGui.QApplication.translate(context, text, disambig)
class PostProcessor:
Default = "PostProcessorDefault"
DefaultArgs = "PostProcessorDefaultArgs"
Blacklist = "PostProcessorBlacklist"
@classmethod
def all(cls):
path = FreeCAD.getHomePath() + ("Mod/Path/PathScripts/")
posts = glob.glob(path + '/*_post.py')
allposts = [ str(os.path.split(os.path.splitext(p)[0])[1][:-5]) for p in posts]
grp = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Macro")
path = grp.GetString("MacroPath", FreeCAD.getUserAppDataDir())
posts = glob.glob(path + '/*_post.py')
allposts.extend([ str(os.path.split(os.path.splitext(p)[0])[1][:-5]) for p in posts])
allposts.sort()
return allposts
@classmethod
def allEnabled(cls):
blacklist = cls.blacklist()
return [processor for processor in cls.all() if not processor in blacklist]
@classmethod
def default(cls):
preferences = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Path")
return preferences.GetString(cls.Default, "")
@classmethod
def defaultArgs(cls):
preferences = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Path")
return preferences.GetString(cls.DefaultArgs, "")
@classmethod
def blacklist(cls):
preferences = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Path")
blacklist = preferences.GetString(cls.Blacklist, "")
if not blacklist:
return []
return eval(blacklist)
@classmethod
def saveDefaults(cls, processor, args, blacklist):
preferences = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Path")
preferences.SetString(cls.Default, processor)
preferences.SetString(cls.DefaultArgs, args)
preferences.SetString(cls.Blacklist, "%s" % (blacklist))
class ObjectPathJob:
def __init__(self, obj):
@ -150,39 +99,21 @@ class ObjectPathJob:
obj.setEditorMode('Placement', mode)
if prop == "PostProcessor":
postname = obj.PostProcessor + "_post"
exec "import %s as current_post" % postname
# make sure the script is reloaded if it was previously loaded
# should the script have been imported for the first time above
# then the initialization code of the script gets executed twice
# resulting in 2 load messages if the script outputs one of those.
exec "reload(%s)" % 'current_post'
if hasattr(current_post, "UNITS"):
if current_post.UNITS == "G21":
obj.MachineUnits = "Metric"
else:
obj.MachineUnits = "Inch"
if hasattr(current_post, "MACHINE_NAME"):
obj.MachineName = current_post.MACHINE_NAME
if hasattr(current_post, "CORNER_MAX"):
obj.X_Max = current_post.CORNER_MAX['x']
obj.Y_Max = current_post.CORNER_MAX['y']
obj.Z_Max = current_post.CORNER_MAX['z']
if hasattr(current_post, "CORNER_MIN"):
obj.X_Min = current_post.CORNER_MIN['x']
obj.Y_Min = current_post.CORNER_MIN['y']
obj.Z_Min = current_post.CORNER_MIN['z']
self.tooltip = None
self.tooltipArgs = None
if hasattr(current_post, "TOOLTIP"):
self.tooltip = current_post.TOOLTIP
if hasattr(current_post, "TOOLTIP_ARGS"):
self.tooltipArgs = current_post.TOOLTIP_ARGS
processor = PostProcessor.load(obj.PostProcessor)
if processor.units:
obj.MachineUnits = processor.units
if processor.machineName:
obj.MachineName = processor.machineName
if processor.cornerMax:
obj.X_Max = processor.cornerMax['x']
obj.Y_Max = processor.cornerMax['y']
obj.Z_Max = processor.cornerMax['z']
if processor.cornerMin:
obj.X_Min = processor.cornerMin['x']
obj.Y_Min = processor.cornerMin['y']
obj.Z_Min = processor.cornerMin['z']
self.tooltip = processor.tooltip
self.tooltipArgs = processor.tooltipArgs
self.PostProcessorArgs = ''

View File

@ -24,7 +24,7 @@
''' Post Process command that will make use of the Output File and Post Processor entries in PathJob '''
import FreeCAD
import FreeCADGui
import PathScripts
from PathScripts.PathPostProcessor import PostProcessor
import os
import sys
from PySide import QtCore, QtGui
@ -90,13 +90,8 @@ class CommandPathPost:
if hasattr(postobj, "PostProcessorArgs"):
postArgs = postobj.PostProcessorArgs
postname += "_post"
try:
current_post
except NameError:
exec "import %s as current_post" % postname
reload(current_post)
current_post.export(obj, filename, postArgs)
processor = PostProcessor.load(postname)
processor.export(obj, filename, postArgs)
FreeCAD.ActiveDocument.commitTransaction()
FreeCAD.ActiveDocument.recompute()

View File

@ -0,0 +1,128 @@
# -*- coding: utf-8 -*-
# ***************************************************************************
# * *
# * Copyright (c) 2014 Yorik van Havre <yorik@uncreated.net> *
# * *
# * This program is free software; you can redistribute it and/or modify *
# * it under the terms of the GNU Lesser General Public License (LGPL) *
# * as published by the Free Software Foundation; either version 2 of *
# * the License, or (at your option) any later version. *
# * for detail see the LICENCE text file. *
# * *
# * This program is distributed in the hope that it will be useful, *
# * but WITHOUT ANY WARRANTY; without even the implied warranty of *
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
# * GNU Library General Public License for more details. *
# * *
# * You should have received a copy of the GNU Library General Public *
# * License along with this program; if not, write to the Free Software *
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
# * USA *
# * *
# ***************************************************************************
import FreeCAD
import os
import glob
class PostProcessor:
Default = "PostProcessorDefault"
DefaultArgs = "PostProcessorDefaultArgs"
Blacklist = "PostProcessorBlacklist"
@classmethod
def all(cls):
path = FreeCAD.getHomePath() + ("Mod/Path/PathScripts/")
posts = glob.glob(path + '/*_post.py')
allposts = [ str(os.path.split(os.path.splitext(p)[0])[1][:-5]) for p in posts]
grp = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Macro")
path = grp.GetString("MacroPath", FreeCAD.getUserAppDataDir())
posts = glob.glob(path + '/*_post.py')
allposts.extend([ str(os.path.split(os.path.splitext(p)[0])[1][:-5]) for p in posts])
allposts.sort()
return allposts
@classmethod
def allEnabled(cls):
blacklist = cls.blacklist()
return [processor for processor in cls.all() if not processor in blacklist]
@classmethod
def default(cls):
preferences = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Path")
return preferences.GetString(cls.Default, "")
@classmethod
def defaultArgs(cls):
preferences = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Path")
return preferences.GetString(cls.DefaultArgs, "")
@classmethod
def blacklist(cls):
preferences = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Path")
blacklist = preferences.GetString(cls.Blacklist, "")
if not blacklist:
return []
return eval(blacklist)
@classmethod
def saveDefaults(cls, processor, args, blacklist):
preferences = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Path")
preferences.SetString(cls.Default, processor)
preferences.SetString(cls.DefaultArgs, args)
preferences.SetString(cls.Blacklist, "%s" % (blacklist))
@classmethod
def load(cls, processor):
postname = processor + "_post"
exec "import %s as current_post" % postname
# make sure the script is reloaded if it was previously loaded
# should the script have been imported for the first time above
# then the initialization code of the script gets executed twice
# resulting in 2 load messages if the script outputs one of those.
exec "reload(%s)" % 'current_post'
instance = PostProcessor(current_post)
instance.units = None
if hasattr(current_post, "UNITS"):
if current_post.UNITS == "G21":
instance.units = "Metric"
else:
instance.units = "Inch"
instance.machineName = None
if hasattr(current_post, "MACHINE_NAME"):
instance.machineName = current_post.MACHINE_NAME
instance.cornerMax = None
if hasattr(current_post, "CORNER_MAX"):
instance.cornerMax = {'x': current_post.CORNER_MAX['x'],
'y': current_post.CORNER_MAX['y'],
'z': current_post.CORNER_MAX['z']}
instance.cornerMin = None
if hasattr(current_post, "CORNER_MIN"):
instance.cornerMin = {'x': current_post.CORNER_MIN['x'],
'y': current_post.CORNER_MIN['y'],
'z': current_post.CORNER_MIN['z']}
instance.tooltip = None
instance.tooltipArgs = None
if hasattr(current_post, "TOOLTIP"):
instance.tooltip = current_post.TOOLTIP
if hasattr(current_post, "TOOLTIP_ARGS"):
instance.tooltipArgs = current_post.TOOLTIP_ARGS
return instance
def __init__(self, script):
self.script = script
def export(self, obj, filename, args):
self.script.export(obj, filename, args)

View File

@ -25,7 +25,7 @@
import FreeCAD
import FreeCADGui
from PySide import QtCore, QtGui
from PathScripts import PathJob
from PathScripts.PathPostProcessor import PostProcessor
class Page:
@ -41,13 +41,13 @@ class Page:
item = self.form.postProcessorList.item(i)
if item.checkState() == QtCore.Qt.CheckState.Unchecked:
blacklist.append(item.text())
PathJob.PostProcessor.saveDefaults(processor, args, blacklist)
PostProcessor.saveDefaults(processor, args, blacklist)
def loadSettings(self):
print("loadSettings")
self.form.defaultPostProcessor.addItem("")
blacklist = PathJob.PostProcessor.blacklist()
for processor in PathJob.PostProcessor.all():
blacklist = PostProcessor.blacklist()
for processor in PostProcessor.all():
self.form.defaultPostProcessor.addItem(processor)
item = QtGui.QListWidgetItem(processor)
if processor in blacklist:
@ -57,12 +57,12 @@ class Page:
item.setFlags( QtCore.Qt.ItemFlag.ItemIsSelectable | QtCore.Qt.ItemFlag.ItemIsEnabled | QtCore.Qt.ItemFlag.ItemIsUserCheckable)
self.form.postProcessorList.addItem(item)
postindex = self.form.defaultPostProcessor.findText(PathJob.PostProcessor.default(), QtCore.Qt.MatchFixedString)
postindex = self.form.defaultPostProcessor.findText(PostProcessor.default(), QtCore.Qt.MatchFixedString)
if postindex >= 0:
self.form.defaultPostProcessor.blockSignals(True)
self.form.defaultPostProcessor.setCurrentIndex(postindex)
self.form.defaultPostProcessor.blockSignals(False)
self.form.defaultPostProcessorArgs.setText(PathJob.PostProcessor.defaultArgs())
self.form.defaultPostProcessorArgs.setText(PostProcessor.defaultArgs())