--- a/PLCControler.py Mon Sep 09 00:48:34 2013 +0200
+++ b/PLCControler.py Mon Sep 09 23:36:12 2013 +0200
@@ -24,6 +24,7 @@
from xml.dom import minidom
from types import StringType, UnicodeType, TupleType
from copy import deepcopy
@@ -99,6 +100,66 @@
RESOURCES, PROPERTIES] = UNEDITABLE_NAMES
#-------------------------------------------------------------------------------
+# Helpers object for generating pou var list +#------------------------------------------------------------------------------- +def compute_dimensions(el): + (dimension.get("lower"), dimension.get("upper")) + for dimension in el.findall("dimension")] + if el.tag == "Type" and el.text is None: + array = el.find("array") + return ('array', array.text, compute_dimensions(array)) + return generate_var_tree(el) +def generate_var_tree(tree): + (var.get("name"), var.text, generate_var_tree(var)) + for var in tree.findall("var")], + compute_dimensions(tree)) +class AddVariable(etree.XSLTExtension): + def __init__(self, variables): + etree.XSLTExtension.__init__(self) + self.Variables = variables + def execute(self, context, self_node, input_node, output_parent): + infos = etree.Element('var_infos') + self.process_children(context, infos) + {el.tag.replace("_", " "): extract_param(el) for el in infos}) +class VarTree(etree.XSLTExtension): + def __init__(self, controller): + etree.XSLTExtension.__init__(self) + self.Controller = controller + def execute(self, context, self_node, input_node, output_parent): + typename = input_node.get("name") + pou_infos = self.Controller.GetPou(typename) + if pou_infos is not None: + self.apply_templates(context, pou_infos, output_parent) + datatype_infos = self.Controller.GetDataType(typename) + if datatype_infos is not None: + self.apply_templates(context, datatype_infos, output_parent) +variables_infos_xslt = etree.parse( + os.path.join(ScriptDirectory, "plcopen", "variables_infos.xslt")) +#------------------------------------------------------------------------------- # Undo Buffer for PLCOpenEditor
#-------------------------------------------------------------------------------
@@ -1224,63 +1285,17 @@
current_varlist.appendvariable(tempvar)
- def GetVariableDictionary(self, varlist, var):
- convert a PLC variable to the dictionary representation
- tempvar = {"Name": var.getname()}
- vartype_content = var.gettype().getcontent()
- vartype_content_type = vartype_content.getLocalTag()
- if vartype_content_type == "derived":
- tempvar["Type"] = vartype_content.getname()
- elif vartype_content_type == "array":
- for dimension in vartype_content.getdimension():
- dimensions.append((dimension.getlower(), dimension.getupper()))
- base_type = vartype_content.baseType.getcontent()
- base_type_type = base_type.getLocalTag()
- if base_type_type == "derived":
- base_type_name = base_type.getname()
- base_type_name = base_type_type.upper()
- tempvar["Type"] = ("array", base_type_name, dimensions)
- tempvar["Type"] = vartype_content_type.upper()
+ def GetVariableDictionary(self, object_with_vars): - initial = var.getinitialValue()
- if initial is not None:
- tempvar["Initial Value"] = initial.getvalue()
- tempvar["Initial Value"] = ""
- address = var.getaddress()
- tempvar["Location"] = address
- tempvar["Location"] = ""
- if varlist.getconstant():
- tempvar["Option"] = "Constant"
- elif varlist.getretain():
- tempvar["Option"] = "Retain"
- elif varlist.getnonretain():
- tempvar["Option"] = "Non-Retain"
- doc = var.getdocumentation()
- tempvar["Documentation"] = doc.getanyText()
- tempvar["Documentation"] = ""
+ variables_infos_xslt_tree = etree.XSLT( + variables_infos_xslt, extensions = { + ("var_infos_ns", "add_variable"): AddVariable(variables), + ("var_infos_ns", "var_tree"): VarTree(self)}) + variables_infos_xslt_tree(object_with_vars) # Add a global var to configuration to configuration
def AddConfigurationGlobalVar(self, config_name, type, var_name,
location="", description=""):
@@ -1304,19 +1319,15 @@
# Return the configuration globalvars
def GetConfigurationGlobalVars(self, name, debug = False):
project = self.GetProject(debug)
# Found the configuration corresponding to name
configuration = project.getconfiguration(name)
if configuration is not None:
- # Extract variables from every varLists
- for varlist in configuration.getglobalVars():
- for var in varlist.getvariable():
- tempvar = self.GetVariableDictionary(varlist, var)
- tempvar["Class"] = "Global"
+ # Extract variables defined in configuration + return self.GetVariableDictionary(configuration) # Return configuration variable names
def GetConfigurationVariableNames(self, config_name = None, debug = False):
@@ -1345,19 +1356,15 @@
# Return the resource globalvars
def GetConfigurationResourceGlobalVars(self, config_name, name, debug = False):
project = self.GetProject(debug)
# Found the resource corresponding to name
resource = project.getconfigurationResource(config_name, name)
- # Extract variables from every varLists
- for varlist in resource.getglobalVars():
- for var in varlist.getvariable():
- tempvar = self.GetVariableDictionary(varlist, var)
- tempvar["Class"] = "Global"
+ # Extract variables defined in configuration + return self.GetVariableDictionary(resource) # Return resource variable names
def GetConfigurationResourceVariableNames(self,
@@ -1375,73 +1382,15 @@
for varlist in resource.globalVars],
- # Recursively generate element name tree for a structured variable
- def GenerateVarTree(self, typename, debug = False):
- project = self.GetProject(debug)
- if project is not None:
- blocktype = self.GetBlockType(typename, debug = debug)
- if blocktype is not None:
- for var_name, var_type, var_modifier in blocktype["inputs"] + blocktype["outputs"]:
- en |= var_name.upper() == "EN"
- eno |= var_name.upper() == "ENO"
- tree.append((var_name, var_type, self.GenerateVarTree(var_type, debug)))
- tree.insert(0, ("ENO", "BOOL", ([], [])))
- tree.insert(0, ("EN", "BOOL", ([], [])))
- datatype = self.GetDataType(typename)
- if datatype is not None:
- basetype_content = datatype.baseType.getcontent()
- basetype_content_type = basetype_content.getLocalTag()
- if basetype_content_type == "derived":
- return self.GenerateVarTree(basetype_content.getname())
- elif basetype_content_type == "array":
- base_type = basetype_content.baseType.getcontent()
- if base_type.getLocalTag() == "derived":
- tree = self.GenerateVarTree(base_type.getname())
- for dimension in basetype_content.getdimension():
- dimensions.append((dimension.getlower(), dimension.getupper()))
- return tree, dimensions
- elif basetype_content_type == "struct":
- for element in basetype_content.getvariable():
- element_type = element.type.getcontent()
- element_type_type = element_type.getLocalTag()
- if element_type_type == "derived":
- tree.append((element.getname(), element_type.getname(), self.GenerateVarTree(element_type.getname())))
- tree.append((element.getname(), element_type_type, ([], [])))
# Return the interface for the given pou
def GetPouInterfaceVars(self, pou, debug = False):
+ interface = pou.interface # Verify that the pou has an interface
- if pou.interface is not None:
- # Extract variables from every varLists
- for type, varlist in pou.getvars():
- for var in varlist.getvariable():
- tempvar = self.GetVariableDictionary(varlist, var)
- tempvar["Class"] = type
- tempvar["Tree"] = ([], [])
- vartype_content = var.gettype().getcontent()
- if vartype_content.getLocalTag() == "derived":
- tempvar["Edit"] = not pou.hasblock(tempvar["Name"])
- tempvar["Tree"] = self.GenerateVarTree(tempvar["Type"], debug)
+ if interface is not None: + # Extract variables defined in interface + return self.GetVariableDictionary(interface) # Replace the Pou interface by the one given
def SetPouInterfaceVars(self, name, vars):
@@ -1503,13 +1452,13 @@
# Return the return type if there is one
return_type = pou.interface.getreturnType()
if return_type is not None:
- returntype_content = return_type.getcontent()
- returntype_content_type = returntype_content.getLocalTag()
- if returntype_content_type == "derived":
- return returntype_content.getname()
- return returntype_content_type.upper()
+ return_type_infos_xslt_tree = etree.XSLT( + variables_infos_xslt, extensions = { + ("var_infos_ns", "var_tree"): VarTree(self)}) + return [extract_param(el) + for el in return_type_infos_xslt_tree(return_type).getroot()] + return [None, ([], [])] # Function that add a new confnode to the confnode list
def AddConfNodeTypesList(self, typeslist):
@@ -1680,6 +1629,20 @@
# Return Data Type Object
+ def GetPou(self, typename, debug = False): + project = self.GetProject(debug) + if project is not None: + result = project.getpou(typename) + for confnodetype in self.ConfNodeTypes: + result = confnodetype["types"].getpou(typename) + # Return Data Type Object def GetDataType(self, typename, debug = False):
project = self.GetProject(debug)
--- a/controls/VariablePanel.py Mon Sep 09 00:48:34 2013 +0200
+++ b/controls/VariablePanel.py Mon Sep 09 23:36:12 2013 +0200
@@ -173,7 +173,7 @@
if var_class not in ["External", "InOut"]:
if self.Parent.Controler.IsEnumeratedType(var_type):
editor = wx.grid.GridCellChoiceEditor()
- editor.SetParameters(",".join(self.Parent.Controler.GetEnumeratedDataValues(var_type)))
+ editor.SetParameters(",".join([""] + self.Parent.Controler.GetEnumeratedDataValues(var_type))) editor = wx.grid.GridCellTextEditor()
renderer = wx.grid.GridCellStringRenderer()
@@ -635,7 +635,7 @@
for data_type in self.Controler.GetDataTypes(self.TagName, debug=self.Debug):
self.ReturnType.Append(data_type)
- returnType = self.Controler.GetEditedElementInterfaceReturnType(self.TagName)
+ returnType, (var_tree, dimensions) = self.Controler.GetEditedElementInterfaceReturnType(self.TagName) description = self.Controler.GetPouDescription(words[1])
self.Values = self.Controler.GetEditedElementInterfaceVars(self.TagName, self.Debug)
--- a/editors/TextViewer.py Mon Sep 09 00:48:34 2013 +0200
+++ b/editors/TextViewer.py Mon Sep 09 23:36:12 2013 +0200
@@ -460,9 +460,8 @@
words = self.TagName.split("::")
self.Variables = self.GenerateVariableTree([(variable["Name"], variable["Type"], variable["Tree"]) for variable in self.Controler.GetEditedElementInterfaceVars(self.TagName, self.Debug)])
if self.Controler.GetEditedElementType(self.TagName, self.Debug)[1] == "function" or words[0] == "T" and self.TextSyntax == "IL":
- return_type = self.Controler.GetEditedElementInterfaceReturnType(self.TagName, self.Debug)
+ return_type, (var_tree, var_dimension) = self.Controler.GetEditedElementInterfaceReturnType(self.TagName, self.Debug) if return_type is not None:
- var_tree, var_dimension = self.Controler.GenerateVarTree(return_type, self.Debug)
self.Variables[words[-1].upper()] = self.GenerateVariableTree(var_tree)
self.Variables[words[-1].upper()] = {}