Laurent@814: #!/usr/bin/env python Laurent@814: # -*- coding: utf-8 -*- Laurent@814: andrej@1571: # This file is part of Beremiz, a Integrated Development Environment for andrej@1571: # programming IEC 61131-3 automates supporting plcopen standard and CanFestival. Laurent@814: # andrej@1571: # Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD andrej@1680: # Copyright (C) 2017: Andrey Skvortsov Laurent@814: # andrej@1571: # See COPYING file for copyrights details. Laurent@814: # andrej@1571: # This program is free software; you can redistribute it and/or andrej@1571: # modify it under the terms of the GNU General Public License andrej@1571: # as published by the Free Software Foundation; either version 2 andrej@1571: # of the License, or (at your option) any later version. Laurent@814: # andrej@1571: # This program is distributed in the hope that it will be useful, andrej@1571: # but WITHOUT ANY WARRANTY; without even the implied warranty of andrej@1571: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the andrej@1571: # GNU General Public License for more details. Laurent@814: # andrej@1571: # You should have received a copy of the GNU General Public License andrej@1571: # along with this program; if not, write to the Free Software andrej@1571: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Laurent@814: Laurent@814: from xmlclass import * Laurent@814: from types import * Laurent@814: import os, re Laurent@1290: from lxml import etree Laurent@1331: from collections import OrderedDict andrej@1680: import util.paths as paths Laurent@1331: Laurent@814: """ Laurent@814: Dictionary that makes the relation between var names in plcopen and displayed values Laurent@814: """ Laurent@814: VarTypes = {"Local" : "localVars", "Temp" : "tempVars", "Input" : "inputVars", Laurent@814: "Output" : "outputVars", "InOut" : "inOutVars", "External" : "externalVars", Laurent@814: "Global" : "globalVars", "Access" : "accessVars"} Laurent@814: Laurent@814: searchResultVarTypes = { Laurent@814: "inputVars": "var_input", Laurent@814: "outputVars": "var_output", Laurent@814: "inOutVars": "var_inout" Laurent@814: } Laurent@814: Laurent@814: """ Laurent@814: Define in which order var types must be displayed Laurent@814: """ Laurent@814: VarOrder = ["Local","Temp","Input","Output","InOut","External","Global","Access"] Laurent@814: Laurent@814: """ Laurent@814: Define which action qualifier must be associated with a duration Laurent@814: """ Laurent@1339: QualifierList = OrderedDict([("N", False), ("R", False), ("S", False), Laurent@1339: ("L", True), ("D", True), ("P", False), ("P0", False), Laurent@1339: ("P1", False), ("SD", True), ("DS", True), ("SL", True)]) Laurent@814: Laurent@814: Laurent@814: FILTER_ADDRESS_MODEL = "(%%[IQM](?:[XBWDL])?)(%s)((?:\.[0-9]+)*)" Laurent@814: Laurent@814: def update_address(address, address_model, new_leading): Laurent@814: result = address_model.match(address) Laurent@814: if result is None: Laurent@814: return address Laurent@814: groups = result.groups() Laurent@814: return groups[0] + new_leading + groups[2] Laurent@814: Laurent@814: def _init_and_compare(function, v1, v2): Laurent@814: if v1 is None: Laurent@814: return v2 Laurent@814: if v2 is not None: Laurent@814: return function(v1, v2) Laurent@814: return v1 Laurent@814: Laurent@814: """ Laurent@814: Helper class for bounding_box calculation Laurent@814: """ Laurent@814: class rect: Laurent@814: Laurent@814: def __init__(self, x=None, y=None, width=None, height=None): Laurent@814: self.x_min = x Laurent@814: self.x_max = None Laurent@814: self.y_min = y Laurent@814: self.y_max = None Laurent@814: if width is not None and x is not None: Laurent@814: self.x_max = x + width Laurent@814: if height is not None and y is not None: Laurent@814: self.y_max = y + height Laurent@814: Laurent@814: def update(self, x, y): Laurent@814: self.x_min = _init_and_compare(min, self.x_min, x) Laurent@814: self.x_max = _init_and_compare(max, self.x_max, x) Laurent@814: self.y_min = _init_and_compare(min, self.y_min, y) Laurent@814: self.y_max = _init_and_compare(max, self.y_max, y) Laurent@814: Laurent@814: def union(self, rect): Laurent@814: self.x_min = _init_and_compare(min, self.x_min, rect.x_min) Laurent@814: self.x_max = _init_and_compare(max, self.x_max, rect.x_max) Laurent@814: self.y_min = _init_and_compare(min, self.y_min, rect.y_min) Laurent@814: self.y_max = _init_and_compare(max, self.y_max, rect.y_max) Laurent@814: Laurent@814: def bounding_box(self): Laurent@814: width = height = None Laurent@814: if self.x_min is not None and self.x_max is not None: Laurent@814: width = self.x_max - self.x_min Laurent@814: if self.y_min is not None and self.y_max is not None: Laurent@814: height = self.y_max - self.y_min Laurent@814: return self.x_min, self.y_min, width, height Laurent@814: Laurent@814: def TextLenInRowColumn(text): Laurent@814: if text == "": Laurent@814: return (0, 0) Laurent@814: lines = text.split("\n") Laurent@814: return len(lines) - 1, len(lines[-1]) Laurent@814: surkovsv93@1556: def CompilePattern(criteria): surkovsv93@1556: flag = 0 if criteria["case_sensitive"] else re.IGNORECASE surkovsv93@1556: find_pattern = criteria["find_pattern"] surkovsv93@1556: if not criteria["regular_expression"]: surkovsv93@1556: find_pattern = re.escape(find_pattern) surkovsv93@1556: criteria["pattern"] = re.compile(find_pattern, flag) surkovsv93@1556: Laurent@814: def TestTextElement(text, criteria): Laurent@814: lines = text.splitlines() Laurent@814: test_result = [] Laurent@814: result = criteria["pattern"].search(text) Laurent@814: while result is not None: Laurent@814: start = TextLenInRowColumn(text[:result.start()]) Laurent@814: end = TextLenInRowColumn(text[:result.end() - 1]) Laurent@814: test_result.append((start, end, "\n".join(lines[start[0]:end[0] + 1]))) Laurent@814: result = criteria["pattern"].search(text, result.end()) Laurent@814: return test_result Laurent@814: andrej@1611: def TextMatched(str1, str2): andrej@1611: return str1 and str2 and (str1.upper() == str2.upper()) andrej@1611: andrej@1680: PLCOpenParser = GenerateParserFromXSD(paths.AbsNeighbourFile(__file__, "tc6_xml_v201.xsd")) Laurent@1305: PLCOpen_XPath = lambda xpath: etree.XPath(xpath, namespaces=PLCOpenParser.NSMAP) Laurent@1290: Laurent@1299: LOAD_POU_PROJECT_TEMPLATE = """ Laurent@1299: Laurent@1299: Laurent@1299: Laurent@1299: Laurent@1299: Laurent@1299: Laurent@1299: Laurent@1299: Laurent@1299: Laurent@1299: Laurent@1299: Laurent@1299: %s Laurent@1299: Laurent@1299: Laurent@1299: Laurent@1299: Laurent@1299: Laurent@1299: """ Laurent@1299: Laurent@1299: def LOAD_POU_INSTANCES_PROJECT_TEMPLATE(body_type): Laurent@1299: return LOAD_POU_PROJECT_TEMPLATE % """ Laurent@1299: Laurent@1299: Laurent@1299: <%(body_type)s>%%s Laurent@1299: Laurent@1299: """ % locals() Laurent@1299: andrej@1680: PLCOpen_v1_file = open(paths.AbsNeighbourFile(__file__, "TC6_XML_V10_B.xsd")) Laurent@1334: PLCOpen_v1_xml = PLCOpen_v1_file.read() Laurent@1334: PLCOpen_v1_file.close() Laurent@1334: PLCOpen_v1_xml = PLCOpen_v1_xml.replace( Laurent@1334: "http://www.plcopen.org/xml/tc6.xsd", Laurent@1334: "http://www.plcopen.org/xml/tc6_0201") Laurent@1334: PLCOpen_v1_xsd = etree.XMLSchema(etree.fromstring(PLCOpen_v1_xml)) Laurent@1334: Laurent@1334: # XPath for file compatibility process Laurent@1334: ProjectResourcesXPath = PLCOpen_XPath("ppx:instances/ppx:configurations/ppx:configuration/ppx:resource") Laurent@1334: ResourceInstancesXpath = PLCOpen_XPath("ppx:pouInstance | ppx:task/ppx:pouInstance") Laurent@1334: TransitionsConditionXPath = PLCOpen_XPath("ppx:types/ppx:pous/ppx:pou/ppx:body/*/ppx:transition/ppx:condition") Laurent@1334: ConditionConnectionsXPath = PLCOpen_XPath("ppx:connection") Laurent@1334: ActionBlocksXPath = PLCOpen_XPath("ppx:types/ppx:pous/ppx:pou/ppx:body/*/ppx:actionBlock") Laurent@1334: ActionBlocksConnectionPointOutXPath = PLCOpen_XPath("ppx:connectionPointOut") Laurent@1334: Laurent@1330: def LoadProjectXML(project_xml): Laurent@1330: project_xml = project_xml.replace( Laurent@1294: "http://www.plcopen.org/xml/tc6.xsd", Laurent@1294: "http://www.plcopen.org/xml/tc6_0201") Laurent@1294: for cre, repl in [ Laurent@1294: (re.compile("(?)(?:)(?!)"), "]]>")]: Laurent@1294: project_xml = cre.sub(repl, project_xml) Laurent@1330: Laurent@1330: try: Laurent@1330: tree, error = PLCOpenParser.LoadXMLString(project_xml) Laurent@1334: if error is None: Laurent@1334: return tree, None Laurent@1334: Laurent@1334: if PLCOpen_v1_xsd.validate(tree): Laurent@1334: # Make file compatible with PLCOpen v2 Laurent@1334: Laurent@1334: # Update resource interval value Laurent@1334: for resource in ProjectResourcesXPath(tree): Laurent@1334: for task in resource.gettask(): Laurent@1334: interval = task.get("interval") Laurent@1334: if interval is not None: Laurent@1334: result = time_model.match(interval) Laurent@1334: if result is not None: Laurent@1334: values = result.groups() Laurent@1334: time_values = [int(v) for v in values[:2]] Laurent@1334: seconds = float(values[2]) Laurent@1334: time_values.extend([int(seconds), int((seconds % 1) * 1000000)]) Laurent@1334: text = "T#" Laurent@1334: if time_values[0] != 0: Laurent@1334: text += "%dh"%time_values[0] Laurent@1334: if time_values[1] != 0: Laurent@1334: text += "%dm"%time_values[1] Laurent@1334: if time_values[2] != 0: Laurent@1334: text += "%ds"%time_values[2] Laurent@1334: if time_values[3] != 0: Laurent@1334: if time_values[3] % 1000 != 0: Laurent@1334: text += "%.3fms"%(float(time_values[3]) / 1000) Laurent@1334: else: Laurent@1334: text += "%dms"%(time_values[3] / 1000) Laurent@1334: task.set("interval", text) Laurent@1334: Laurent@1334: # Update resources pou instance attributes Laurent@1334: for pouInstance in ResourceInstancesXpath(resource): Laurent@1339: type_name = pouInstance.attrib.pop("type") Laurent@1334: if type_name is not None: Laurent@1334: pouInstance.set("typeName", type_name) Laurent@1334: Laurent@1334: # Update transitions condition Laurent@1334: for transition_condition in TransitionsConditionXPath(tree): Laurent@1334: connections = ConditionConnectionsXPath(transition_condition) Laurent@1334: if len(connections) > 0: Laurent@1334: connectionPointIn = PLCOpenParser.CreateElement("connectionPointIn", "condition") Laurent@1334: transition_condition.setcontent(connectionPointIn) Laurent@1334: connectionPointIn.setrelPositionXY(0, 0) Laurent@1334: for connection in connections: Laurent@1334: connectionPointIn.append(connection) Laurent@1334: Laurent@1334: # Update actionBlocks Laurent@1334: for actionBlock in ActionBlocksXPath(tree): Laurent@1334: for connectionPointOut in ActionBlocksConnectionPointOutXPath(actionBlock): Laurent@1334: actionBlock.remove(connectionPointOut) Laurent@1334: Laurent@1334: for action in actionBlock.getaction(): Laurent@1334: action.set("localId", "0") Laurent@1334: relPosition = PLCOpenParser.CreateElement("relPosition", "action") Laurent@1334: relPosition.set("x", "0") Laurent@1334: relPosition.set("y", "0") Laurent@1334: action.setrelPosition(relPosition) Laurent@1334: Laurent@1334: return tree, None Laurent@1334: Laurent@1334: return tree, error Laurent@1334: Laurent@1330: except Exception, e: Laurent@1330: return None, e.message Laurent@1330: Laurent@1330: def LoadProject(filepath): Laurent@1330: project_file = open(filepath) Laurent@1330: project_xml = project_file.read() Laurent@1290: project_file.close() Laurent@1330: return LoadProjectXML(project_xml) Laurent@1290: Laurent@1305: project_pou_xpath = PLCOpen_XPath("/ppx:project/ppx:types/ppx:pous/ppx:pou") Laurent@1299: def LoadPou(xml_string): Laurent@1330: root, error = LoadProjectXML(LOAD_POU_PROJECT_TEMPLATE % xml_string) Laurent@1330: return project_pou_xpath(root)[0], error Laurent@1305: Laurent@1305: project_pou_instances_xpath = { Laurent@1305: body_type: PLCOpen_XPath( Laurent@1305: "/ppx:project/ppx:types/ppx:pous/ppx:pou[@name='paste_pou']/ppx:body/ppx:%s/*" % body_type) Laurent@1305: for body_type in ["FBD", "LD", "SFC"]} Laurent@1299: def LoadPouInstances(xml_string, body_type): Laurent@1330: root, error = LoadProjectXML( Laurent@1330: LOAD_POU_INSTANCES_PROJECT_TEMPLATE(body_type) % xml_string) Laurent@1330: return project_pou_instances_xpath[body_type](root), error Laurent@1299: Laurent@1290: def SaveProject(project, filepath): Laurent@1290: project_file = open(filepath, 'w') Laurent@1290: project_file.write(etree.tostring( Laurent@1290: project, Laurent@1290: pretty_print=True, Laurent@1290: xml_declaration=True, Laurent@1290: encoding='utf-8')) Laurent@1290: project_file.close() Laurent@1290: Laurent@1290: cls = PLCOpenParser.GetElementClass("formattedText") Laurent@814: if cls: Laurent@814: def updateElementName(self, old_name, new_name): Laurent@1294: text = self.getanyText() andrej@1611: pattern = re.compile('\\b' + old_name + '\\b', re.IGNORECASE) andrej@1611: text = pattern.sub(new_name, text) Laurent@1294: self.setanyText(text) Laurent@814: setattr(cls, "updateElementName", updateElementName) Laurent@814: Laurent@814: def updateElementAddress(self, address_model, new_leading): Laurent@1294: text = self.getanyText() Laurent@814: startpos = 0 Laurent@814: result = address_model.search(text, startpos) Laurent@814: while result is not None: Laurent@814: groups = result.groups() Laurent@814: new_address = groups[0] + new_leading + groups[2] Laurent@814: text = text[:result.start()] + new_address + text[result.end():] Laurent@814: startpos = result.start() + len(new_address) Laurent@1322: result = address_model.search(text, startpos) Laurent@1294: self.setanyText(text) Laurent@814: setattr(cls, "updateElementAddress", updateElementAddress) Laurent@814: Laurent@1142: def hasblock(self, block_type): andrej@1611: text = self.getanyText() andrej@1611: pattern = re.compile('\\b' + block_type + '\\b', re.IGNORECASE) andrej@1611: return pattern.search(text) is not None Laurent@1142: setattr(cls, "hasblock", hasblock) Laurent@1142: Laurent@814: def Search(self, criteria, parent_infos): Laurent@1291: return [(tuple(parent_infos),) + result for result in TestTextElement(self.getanyText(), criteria)] Laurent@814: setattr(cls, "Search", Search) Laurent@814: Laurent@1290: cls = PLCOpenParser.GetElementClass("project") Laurent@1290: if cls: Laurent@814: Laurent@814: def setname(self, name): Laurent@814: self.contentHeader.setname(name) Laurent@814: setattr(cls, "setname", setname) Laurent@814: Laurent@814: def getname(self): Laurent@814: return self.contentHeader.getname() Laurent@814: setattr(cls, "getname", getname) Laurent@814: Laurent@814: def getfileHeader(self): Laurent@1301: fileheader_obj = self.fileHeader Laurent@1301: return { Laurent@1301: attr: value if value is not None else "" Laurent@1301: for attr, value in [ Laurent@1301: ("companyName", fileheader_obj.getcompanyName()), Laurent@1301: ("companyURL", fileheader_obj.getcompanyURL()), Laurent@1301: ("productName", fileheader_obj.getproductName()), Laurent@1301: ("productVersion", fileheader_obj.getproductVersion()), Laurent@1301: ("productRelease", fileheader_obj.getproductRelease()), Laurent@1301: ("creationDateTime", fileheader_obj.getcreationDateTime()), Laurent@1301: ("contentDescription", fileheader_obj.getcontentDescription())] Laurent@1301: } Laurent@814: setattr(cls, "getfileHeader", getfileHeader) Laurent@814: Laurent@814: def setfileHeader(self, fileheader): Laurent@1301: fileheader_obj = self.fileHeader Laurent@1309: for attr in ["companyName", "companyURL", "productName", Laurent@1309: "productVersion", "productRelease", "creationDateTime", Laurent@1309: "contentDescription"]: Laurent@1309: value = fileheader.get(attr) Laurent@1309: if value is not None: Laurent@1309: setattr(fileheader_obj, attr, value) Laurent@814: setattr(cls, "setfileHeader", setfileHeader) Laurent@814: Laurent@814: def getcontentHeader(self): Laurent@1301: contentheader_obj = self.contentHeader Laurent@1301: contentheader = { Laurent@1301: attr: value if value is not None else "" Laurent@1301: for attr, value in [ Laurent@1301: ("projectName", contentheader_obj.getname()), Laurent@1301: ("projectVersion", contentheader_obj.getversion()), Laurent@1301: ("modificationDateTime", contentheader_obj.getmodificationDateTime()), Laurent@1301: ("organization", contentheader_obj.getorganization()), Laurent@1301: ("authorName", contentheader_obj.getauthor()), Laurent@1301: ("language", contentheader_obj.getlanguage())] Laurent@1301: } Laurent@814: contentheader["pageSize"] = self.contentHeader.getpageSize() Laurent@814: contentheader["scaling"] = self.contentHeader.getscaling() Laurent@814: return contentheader Laurent@814: setattr(cls, "getcontentHeader", getcontentHeader) Laurent@814: Laurent@814: def setcontentHeader(self, contentheader): Laurent@1301: contentheader_obj = self.contentHeader Laurent@1301: for attr, value in contentheader.iteritems(): Laurent@1313: func = {"projectName": contentheader_obj.setname, Laurent@1313: "projectVersion": contentheader_obj.setversion, Laurent@1313: "authorName": contentheader_obj.setauthor, Laurent@1313: "pageSize": lambda v: contentheader_obj.setpageSize(*v), Laurent@1313: "scaling": contentheader_obj.setscaling}.get(attr) Laurent@1313: if func is not None: Laurent@1313: func(value) Laurent@1309: elif attr in ["modificationDateTime", "organization", "language"]: Laurent@1301: setattr(contentheader_obj, attr, value) Laurent@814: setattr(cls, "setcontentHeader", setcontentHeader) Laurent@814: Laurent@1305: def gettypeElementFunc(element_type): Laurent@1305: elements_xpath = PLCOpen_XPath( Laurent@1305: "ppx:types/ppx:%(element_type)ss/ppx:%(element_type)s[@name=$name]" % locals()) Laurent@1305: def gettypeElement(self, name): Laurent@1305: elements = elements_xpath(self, name=name) Laurent@1305: if len(elements) == 1: Laurent@1305: return elements[0] Laurent@1305: return None Laurent@1305: return gettypeElement Laurent@1305: Laurent@1305: datatypes_xpath = PLCOpen_XPath("ppx:types/ppx:dataTypes/ppx:dataType") Laurent@1305: filtered_datatypes_xpath = PLCOpen_XPath( Laurent@1305: "ppx:types/ppx:dataTypes/ppx:dataType[@name!=$exclude]") Laurent@1302: def getdataTypes(self, exclude=None): Laurent@1305: if exclude is not None: Laurent@1305: return filtered_datatypes_xpath(self, exclude=exclude) Laurent@1305: return datatypes_xpath(self) Laurent@814: setattr(cls, "getdataTypes", getdataTypes) Laurent@814: Laurent@1305: setattr(cls, "getdataType", gettypeElementFunc("dataType")) Laurent@814: Laurent@814: def appenddataType(self, name): Laurent@1301: if self.getdataType(name) is not None: Laurent@814: raise ValueError, "\"%s\" Data Type already exists !!!"%name Laurent@814: self.types.appenddataTypeElement(name) Laurent@814: setattr(cls, "appenddataType", appenddataType) Laurent@814: Laurent@814: def insertdataType(self, index, datatype): Laurent@814: self.types.insertdataTypeElement(index, datatype) Laurent@814: setattr(cls, "insertdataType", insertdataType) Laurent@814: Laurent@814: def removedataType(self, name): Laurent@814: self.types.removedataTypeElement(name) Laurent@814: setattr(cls, "removedataType", removedataType) Laurent@814: Laurent@1305: def getpous(self, exclude=None, filter=[]): Laurent@1302: return self.xpath( Laurent@1302: "ppx:types/ppx:pous/ppx:pou%s%s" % Laurent@1302: (("[@name!='%s']" % exclude) if exclude is not None else '', Laurent@1302: ("[%s]" % " or ".join( Laurent@1302: map(lambda x: "@pouType='%s'" % x, filter))) Laurent@1305: if len(filter) > 0 else ""), Laurent@1302: namespaces=PLCOpenParser.NSMAP) Laurent@814: setattr(cls, "getpous", getpous) Laurent@814: Laurent@1305: setattr(cls, "getpou", gettypeElementFunc("pou")) Laurent@814: Laurent@814: def appendpou(self, name, pou_type, body_type): Laurent@814: self.types.appendpouElement(name, pou_type, body_type) Laurent@814: setattr(cls, "appendpou", appendpou) Laurent@814: Laurent@814: def insertpou(self, index, pou): Laurent@814: self.types.insertpouElement(index, pou) Laurent@814: setattr(cls, "insertpou", insertpou) Laurent@814: Laurent@814: def removepou(self, name): Laurent@814: self.types.removepouElement(name) Laurent@814: setattr(cls, "removepou", removepou) Laurent@814: Laurent@1305: configurations_xpath = PLCOpen_XPath( Laurent@1305: "ppx:instances/ppx:configurations/ppx:configuration") Laurent@814: def getconfigurations(self): Laurent@1305: return configurations_xpath(self) Laurent@1301: setattr(cls, "getconfigurations", getconfigurations) Laurent@1301: Laurent@1305: configuration_xpath = PLCOpen_XPath( Laurent@1305: "ppx:instances/ppx:configurations/ppx:configuration[@name=$name]") Laurent@1305: def getconfiguration(self, name): Laurent@1305: configurations = configuration_xpath(self, name=name) Laurent@1305: if len(configurations) == 1: Laurent@1301: return configurations[0] Laurent@814: return None Laurent@814: setattr(cls, "getconfiguration", getconfiguration) Laurent@814: Laurent@814: def addconfiguration(self, name): Laurent@1301: if self.getconfiguration(name) is not None: Laurent@1301: raise ValueError, _("\"%s\" configuration already exists !!!") % name Laurent@1290: new_configuration = PLCOpenParser.CreateElement("configuration", "configurations") Laurent@814: new_configuration.setname(name) Laurent@814: self.instances.configurations.appendconfiguration(new_configuration) Laurent@814: setattr(cls, "addconfiguration", addconfiguration) Laurent@814: Laurent@814: def removeconfiguration(self, name): Laurent@1301: configuration = self.getconfiguration(name) Laurent@1301: if configuration is None: Laurent@1301: raise ValueError, ("\"%s\" configuration doesn't exist !!!") % name Laurent@1301: self.instances.configurations.remove(configuration) Laurent@814: setattr(cls, "removeconfiguration", removeconfiguration) Laurent@1305: Laurent@1305: resources_xpath = PLCOpen_XPath( Laurent@1305: "ppx:instances/ppx:configurations/ppx:configuration[@name=$configname]/ppx:resource[@name=$name]") Laurent@814: def getconfigurationResource(self, config_name, name): Laurent@1305: resources = resources_xpath(self, configname=config_name, name=name) Laurent@1301: if len(resources) == 1: Laurent@1301: return resources[0] Laurent@1301: return None Laurent@1301: setattr(cls, "getconfigurationResource", getconfigurationResource) Laurent@1301: Laurent@1301: def addconfigurationResource(self, config_name, name): Laurent@1301: if self.getconfigurationResource(config_name, name) is not None: andrej@1581: msg = _("\"{a1}\" resource already exists in \"{a2}\" configuration !!!").format(a1 = name, a2 = config_name) andrej@1581: raise ValueError, msg Laurent@814: configuration = self.getconfiguration(config_name) Laurent@1294: if configuration is not None: Laurent@1290: new_resource = PLCOpenParser.CreateElement("resource", "configuration") Laurent@814: new_resource.setname(name) Laurent@814: configuration.appendresource(new_resource) Laurent@814: setattr(cls, "addconfigurationResource", addconfigurationResource) Laurent@814: Laurent@814: def removeconfigurationResource(self, config_name, name): Laurent@814: configuration = self.getconfiguration(config_name) Laurent@1301: found = False Laurent@1294: if configuration is not None: Laurent@1301: resource = self.getconfigurationResource(config_name, name) Laurent@1301: if resource is not None: Laurent@1301: configuration.remove(resource) Laurent@1301: found = True Laurent@1301: if not found: andrej@1581: msg = _("\"{a1}\" resource doesn't exist in \"{a2}\" configuration !!!").format(a1 = name, a2 = config_name) andrej@1581: raise ValueError, msg Laurent@814: setattr(cls, "removeconfigurationResource", removeconfigurationResource) Laurent@814: Laurent@814: def updateElementName(self, old_name, new_name): Laurent@1301: for datatype in self.getdataTypes(): Laurent@814: datatype.updateElementName(old_name, new_name) Laurent@1301: for pou in self.getpous(): Laurent@814: pou.updateElementName(old_name, new_name) Laurent@1301: for configuration in self.getconfigurations(): Laurent@814: configuration.updateElementName(old_name, new_name) Laurent@814: setattr(cls, "updateElementName", updateElementName) Laurent@814: Laurent@814: def updateElementAddress(self, old_leading, new_leading): Laurent@814: address_model = re.compile(FILTER_ADDRESS_MODEL % old_leading) Laurent@1301: for pou in self.getpous(): Laurent@814: pou.updateElementAddress(address_model, new_leading) Laurent@1301: for configuration in self.getconfigurations(): Laurent@814: configuration.updateElementAddress(address_model, new_leading) Laurent@814: setattr(cls, "updateElementAddress", updateElementAddress) Laurent@814: Laurent@814: def removeVariableByAddress(self, address): Laurent@1301: for pou in self.getpous(): Laurent@814: pou.removeVariableByAddress(address) Laurent@1301: for configuration in self.getconfigurations(): Laurent@814: configuration.removeVariableByAddress(address) Laurent@814: setattr(cls, "removeVariableByAddress", removeVariableByAddress) Laurent@814: Laurent@814: def removeVariableByFilter(self, leading): Laurent@814: address_model = re.compile(FILTER_ADDRESS_MODEL % leading) Laurent@1301: for pou in self.getpous(): Laurent@814: pou.removeVariableByFilter(address_model) Laurent@1301: for configuration in self.getconfigurations(): Laurent@814: configuration.removeVariableByFilter(address_model) Laurent@814: setattr(cls, "removeVariableByFilter", removeVariableByFilter) Laurent@814: Laurent@1305: enumerated_values_xpath = PLCOpen_XPath( Laurent@1305: "ppx:types/ppx:dataTypes/ppx:dataType/ppx:baseType/ppx:enum/ppx:values/ppx:value") Laurent@1301: def GetEnumeratedDataTypeValues(self): Laurent@1305: return [value.getname() for value in enumerated_values_xpath(self)] Laurent@814: setattr(cls, "GetEnumeratedDataTypeValues", GetEnumeratedDataTypeValues) Laurent@814: Laurent@814: def Search(self, criteria, parent_infos=[]): Laurent@814: result = self.types.Search(criteria, parent_infos) Laurent@814: for configuration in self.instances.configurations.getconfiguration(): Laurent@814: result.extend(configuration.Search(criteria, parent_infos)) Laurent@814: return result Laurent@814: setattr(cls, "Search", Search) Laurent@814: Laurent@1290: cls = PLCOpenParser.GetElementClass("contentHeader", "project") Laurent@1290: if cls: Laurent@814: Laurent@814: def setpageSize(self, width, height): Laurent@814: self.coordinateInfo.setpageSize(width, height) Laurent@814: setattr(cls, "setpageSize", setpageSize) Laurent@814: Laurent@814: def getpageSize(self): Laurent@814: return self.coordinateInfo.getpageSize() Laurent@814: setattr(cls, "getpageSize", getpageSize) Laurent@814: Laurent@814: def setscaling(self, scaling): Laurent@814: for language, (x, y) in scaling.items(): Laurent@814: self.coordinateInfo.setscaling(language, x, y) Laurent@814: setattr(cls, "setscaling", setscaling) Laurent@814: Laurent@814: def getscaling(self): Laurent@814: scaling = {} Laurent@814: scaling["FBD"] = self.coordinateInfo.getscaling("FBD") Laurent@814: scaling["LD"] = self.coordinateInfo.getscaling("LD") Laurent@814: scaling["SFC"] = self.coordinateInfo.getscaling("SFC") Laurent@814: return scaling Laurent@814: setattr(cls, "getscaling", getscaling) Laurent@814: Laurent@1290: cls = PLCOpenParser.GetElementClass("coordinateInfo", "contentHeader") Laurent@814: if cls: Laurent@814: def setpageSize(self, width, height): Laurent@814: if width == 0 and height == 0: Laurent@814: self.deletepageSize() Laurent@814: else: Laurent@814: if self.pageSize is None: Laurent@814: self.addpageSize() Laurent@814: self.pageSize.setx(width) Laurent@814: self.pageSize.sety(height) Laurent@814: setattr(cls, "setpageSize", setpageSize) Laurent@814: Laurent@814: def getpageSize(self): Laurent@814: if self.pageSize is not None: Laurent@814: return self.pageSize.getx(), self.pageSize.gety() Laurent@814: return 0, 0 Laurent@814: setattr(cls, "getpageSize", getpageSize) Laurent@814: Laurent@814: def setscaling(self, language, x, y): Laurent@814: if language == "FBD": Laurent@814: self.fbd.scaling.setx(x) Laurent@814: self.fbd.scaling.sety(y) Laurent@814: elif language == "LD": Laurent@814: self.ld.scaling.setx(x) Laurent@814: self.ld.scaling.sety(y) Laurent@814: elif language == "SFC": Laurent@814: self.sfc.scaling.setx(x) Laurent@814: self.sfc.scaling.sety(y) Laurent@814: setattr(cls, "setscaling", setscaling) Laurent@814: Laurent@814: def getscaling(self, language): Laurent@814: if language == "FBD": Laurent@814: return self.fbd.scaling.getx(), self.fbd.scaling.gety() Laurent@814: elif language == "LD": Laurent@814: return self.ld.scaling.getx(), self.ld.scaling.gety() Laurent@814: elif language == "SFC": Laurent@814: return self.sfc.scaling.getx(), self.sfc.scaling.gety() Laurent@814: return 0, 0 Laurent@814: setattr(cls, "getscaling", getscaling) Laurent@814: Laurent@814: def _Search(attributes, criteria, parent_infos): Laurent@814: search_result = [] Laurent@814: for attr, value in attributes: Laurent@814: if value is not None: Laurent@814: search_result.extend([(tuple(parent_infos + [attr]),) + result for result in TestTextElement(value, criteria)]) Laurent@814: return search_result Laurent@814: Laurent@814: def _updateConfigurationResourceElementName(self, old_name, new_name): Laurent@814: for varlist in self.getglobalVars(): Laurent@814: for var in varlist.getvariable(): Laurent@814: var_address = var.getaddress() Laurent@814: if var_address is not None: andrej@1611: if TextMatched(var_address, old_name): Laurent@814: var.setaddress(new_name) andrej@1611: if TextMatched(var.getname(), old_name): Laurent@814: var.setname(new_name) Laurent@814: Laurent@814: def _updateConfigurationResourceElementAddress(self, address_model, new_leading): Laurent@814: for varlist in self.getglobalVars(): Laurent@814: for var in varlist.getvariable(): Laurent@814: var_address = var.getaddress() Laurent@814: if var_address is not None: Laurent@814: var.setaddress(update_address(var_address, address_model, new_leading)) Laurent@814: Laurent@814: def _removeConfigurationResourceVariableByAddress(self, address): Laurent@814: for varlist in self.getglobalVars(): Laurent@814: variables = varlist.getvariable() Laurent@814: for i in xrange(len(variables)-1, -1, -1): Laurent@814: if variables[i].getaddress() == address: Laurent@1290: variables.remove(variables[i]) Laurent@814: Laurent@814: def _removeConfigurationResourceVariableByFilter(self, address_model): Laurent@814: for varlist in self.getglobalVars(): Laurent@814: variables = varlist.getvariable() Laurent@814: for i in xrange(len(variables)-1, -1, -1): Laurent@814: var_address = variables[i].getaddress() Laurent@814: if var_address is not None: Laurent@814: result = address_model.match(var_address) Laurent@814: if result is not None: Laurent@1290: variables.remove(variables[i]) Laurent@814: Laurent@814: def _SearchInConfigurationResource(self, criteria, parent_infos=[]): Laurent@814: search_result = _Search([("name", self.getname())], criteria, parent_infos) Laurent@814: var_number = 0 Laurent@814: for varlist in self.getglobalVars(): Laurent@814: variable_type = searchResultVarTypes.get("globalVars", "var_local") Laurent@814: variables = varlist.getvariable() Laurent@814: for modifier, has_modifier in [("constant", varlist.getconstant()), Laurent@814: ("retain", varlist.getretain()), Laurent@814: ("non_retain", varlist.getnonretain())]: Laurent@814: if has_modifier: Laurent@814: for result in TestTextElement(modifier, criteria): Laurent@814: search_result.append((tuple(parent_infos + [variable_type, (var_number, var_number + len(variables)), modifier]),) + result) Laurent@814: break Laurent@814: for variable in variables: Laurent@814: search_result.extend(variable.Search(criteria, parent_infos + [variable_type, var_number])) Laurent@814: var_number += 1 Laurent@814: return search_result Laurent@814: Laurent@1290: cls = PLCOpenParser.GetElementClass("configuration", "configurations") Laurent@1290: if cls: Laurent@1290: Laurent@1290: def addglobalVar(self, var_type, name, location="", description=""): Laurent@1171: globalvars = self.getglobalVars() Laurent@1171: if len(globalvars) == 0: Laurent@1290: globalvars.append(PLCOpenParser.CreateElement("varList")) Laurent@1290: var = PLCOpenParser.CreateElement("variable", "varListPlain") Laurent@1171: var.setname(name) Laurent@1313: var.settype(var_type) Laurent@1171: if location != "": Laurent@1171: var.setaddress(location) Laurent@1171: if description != "": Laurent@1294: ft = PLCOpenParser.CreateElement("documentation", "variable") Laurent@1291: ft.setanyText(description) Laurent@1171: var.setdocumentation(ft) Laurent@1171: globalvars[-1].appendvariable(var) Laurent@1171: setattr(cls, "addglobalVar", addglobalVar) Laurent@1171: Laurent@814: def updateElementName(self, old_name, new_name): Laurent@814: _updateConfigurationResourceElementName(self, old_name, new_name) Laurent@814: for resource in self.getresource(): Laurent@814: resource.updateElementName(old_name, new_name) Laurent@814: setattr(cls, "updateElementName", updateElementName) Laurent@814: Laurent@814: def updateElementAddress(self, address_model, new_leading): Laurent@814: _updateConfigurationResourceElementAddress(self, address_model, new_leading) Laurent@814: for resource in self.getresource(): Laurent@814: resource.updateElementAddress(address_model, new_leading) Laurent@814: setattr(cls, "updateElementAddress", updateElementAddress) Laurent@814: Laurent@814: setattr(cls, "removeVariableByAddress", _removeConfigurationResourceVariableByAddress) Laurent@814: setattr(cls, "removeVariableByFilter", _removeConfigurationResourceVariableByFilter) Laurent@814: Laurent@814: def Search(self, criteria, parent_infos=[]): Laurent@814: search_result = [] Laurent@814: parent_infos = parent_infos + ["C::%s" % self.getname()] Laurent@814: filter = criteria["filter"] Laurent@814: if filter == "all" or "configuration" in filter: Laurent@814: search_result = _SearchInConfigurationResource(self, criteria, parent_infos) Laurent@814: for resource in self.getresource(): Laurent@814: search_result.extend(resource.Search(criteria, parent_infos)) Laurent@814: return search_result Laurent@814: setattr(cls, "Search", Search) Laurent@814: Laurent@1290: cls = PLCOpenParser.GetElementClass("resource", "configuration") Laurent@814: if cls: Laurent@814: def updateElementName(self, old_name, new_name): Laurent@814: _updateConfigurationResourceElementName(self, old_name, new_name) Laurent@814: for instance in self.getpouInstance(): Laurent@814: instance.updateElementName(old_name, new_name) Laurent@814: for task in self.gettask(): Laurent@814: task.updateElementName(old_name, new_name) Laurent@814: setattr(cls, "updateElementName", updateElementName) Laurent@814: Laurent@814: def updateElementAddress(self, address_model, new_leading): Laurent@814: _updateConfigurationResourceElementAddress(self, address_model, new_leading) Laurent@814: for task in self.gettask(): Laurent@814: task.updateElementAddress(address_model, new_leading) Laurent@814: setattr(cls, "updateElementAddress", updateElementAddress) Laurent@814: Laurent@814: setattr(cls, "removeVariableByAddress", _removeConfigurationResourceVariableByAddress) Laurent@814: setattr(cls, "removeVariableByFilter", _removeConfigurationResourceVariableByFilter) Laurent@814: Laurent@814: def Search(self, criteria, parent_infos=[]): Laurent@814: parent_infos = parent_infos[:-1] + ["R::%s::%s" % (parent_infos[-1].split("::")[1], self.getname())] Laurent@814: search_result = _SearchInConfigurationResource(self, criteria, parent_infos) Laurent@814: task_number = 0 Laurent@814: instance_number = 0 Laurent@814: for task in self.gettask(): Laurent@814: results = TestTextElement(task.getname(), criteria) Laurent@814: for result in results: Laurent@814: search_result.append((tuple(parent_infos + ["task", task_number, "name"]),) + result) Laurent@814: search_result.extend(task.Search(criteria, parent_infos + ["task", task_number])) Laurent@814: task_number += 1 Laurent@814: for instance in task.getpouInstance(): Laurent@814: search_result.extend(task.Search(criteria, parent_infos + ["instance", instance_number])) Laurent@814: for result in results: Laurent@814: search_result.append((tuple(parent_infos + ["instance", instance_number, "task"]),) + result) Laurent@814: instance_number += 1 Laurent@814: for instance in self.getpouInstance(): Laurent@814: search_result.extend(instance.Search(criteria, parent_infos + ["instance", instance_number])) Laurent@814: instance_number += 1 Laurent@814: return search_result Laurent@814: setattr(cls, "Search", Search) Laurent@814: Laurent@1290: cls = PLCOpenParser.GetElementClass("task", "resource") Laurent@814: if cls: Laurent@814: def updateElementName(self, old_name, new_name): andrej@1611: if TextMatched(self.single, old_name): Laurent@814: self.single = new_name andrej@1611: if TextMatched(self.interval, old_name): Laurent@814: self.interval = new_name Laurent@814: for instance in self.getpouInstance(): Laurent@814: instance.updateElementName(old_name, new_name) Laurent@814: setattr(cls, "updateElementName", updateElementName) Laurent@814: Laurent@814: def updateElementAddress(self, address_model, new_leading): Laurent@814: if self.single is not None: Laurent@814: self.single = update_address(self.single, address_model, new_leading) Laurent@814: if self.interval is not None: Laurent@814: self.interval = update_address(self.interval, address_model, new_leading) Laurent@814: setattr(cls, "updateElementAddress", updateElementAddress) Laurent@814: Laurent@814: def Search(self, criteria, parent_infos=[]): Laurent@814: return _Search([("single", self.getsingle()), Laurent@814: ("interval", self.getinterval()), Laurent@814: ("priority", str(self.getpriority()))], Laurent@814: criteria, parent_infos) Laurent@814: setattr(cls, "Search", Search) Laurent@814: Laurent@1290: cls = PLCOpenParser.GetElementClass("pouInstance") Laurent@814: if cls: Laurent@814: def updateElementName(self, old_name, new_name): andrej@1611: if TextMatched(self.typeName, old_name): Laurent@814: self.typeName = new_name Laurent@814: setattr(cls, "updateElementName", updateElementName) Laurent@814: Laurent@814: def Search(self, criteria, parent_infos=[]): Laurent@814: return _Search([("name", self.getname()), Laurent@814: ("type", self.gettypeName())], Laurent@814: criteria, parent_infos) Laurent@814: setattr(cls, "Search", Search) Laurent@814: Laurent@1290: cls = PLCOpenParser.GetElementClass("variable", "varListPlain") Laurent@814: if cls: Laurent@814: def gettypeAsText(self): Laurent@814: vartype_content = self.gettype().getcontent() Laurent@1290: vartype_content_name = vartype_content.getLocalTag() Laurent@814: # Variable type is a user data type Laurent@1290: if vartype_content_name == "derived": Laurent@1290: return vartype_content.getname() Laurent@814: # Variable type is a string type Laurent@1290: elif vartype_content_name in ["string", "wstring"]: Laurent@1290: return vartype_content_name.upper() Laurent@814: # Variable type is an array Laurent@1290: elif vartype_content_name == "array": Laurent@1290: base_type = vartype_content.baseType.getcontent() Laurent@1290: base_type_name = base_type.getLocalTag() Laurent@814: # Array derived directly from a user defined type Laurent@1290: if base_type_name == "derived": Laurent@1290: basetype_name = base_type.getname() Laurent@814: # Array derived directly from a string type Laurent@1290: elif base_type_name in ["string", "wstring"]: Laurent@1290: basetype_name = base_type_name.upper() Laurent@814: # Array derived directly from an elementary type Laurent@814: else: Laurent@1290: basetype_name = base_type_name Laurent@1290: return "ARRAY [%s] OF %s" % (",".join(map(lambda x : "%s..%s" % (x.getlower(), x.getupper()), vartype_content.getdimension())), basetype_name) Laurent@814: # Variable type is an elementary type Laurent@1290: return vartype_content_name Laurent@814: setattr(cls, "gettypeAsText", gettypeAsText) Laurent@814: Laurent@814: def Search(self, criteria, parent_infos=[]): Laurent@814: search_result = _Search([("name", self.getname()), Laurent@814: ("type", self.gettypeAsText()), Laurent@814: ("location", self.getaddress())], Laurent@814: criteria, parent_infos) Laurent@814: initial = self.getinitialValue() Laurent@814: if initial is not None: Laurent@814: search_result.extend(_Search([("initial value", initial.getvalue())], criteria, parent_infos)) Laurent@814: doc = self.getdocumentation() Laurent@814: if doc is not None: Laurent@814: search_result.extend(doc.Search(criteria, parent_infos + ["documentation"])) Laurent@814: return search_result Laurent@814: setattr(cls, "Search", Search) Laurent@814: Laurent@1290: cls = PLCOpenParser.GetElementClass("types", "project") Laurent@814: if cls: Laurent@814: def getdataTypeElements(self): Laurent@814: return self.dataTypes.getdataType() Laurent@814: setattr(cls, "getdataTypeElements", getdataTypeElements) Laurent@814: Laurent@814: def getdataTypeElement(self, name): Laurent@814: elements = self.dataTypes.getdataType() Laurent@814: for element in elements: andrej@1611: if TextMatched(element.getname(), name): Laurent@814: return element Laurent@814: return None Laurent@814: setattr(cls, "getdataTypeElement", getdataTypeElement) Laurent@814: Laurent@814: def appenddataTypeElement(self, name): Laurent@1290: new_datatype = PLCOpenParser.CreateElement("dataType", "dataTypes") Laurent@1293: self.dataTypes.appenddataType(new_datatype) Laurent@814: new_datatype.setname(name) Laurent@1294: new_datatype.baseType.setcontent(PLCOpenParser.CreateElement("BOOL", "dataType")) Laurent@814: setattr(cls, "appenddataTypeElement", appenddataTypeElement) Laurent@814: Laurent@814: def insertdataTypeElement(self, index, dataType): Laurent@814: self.dataTypes.insertdataType(index, dataType) Laurent@814: setattr(cls, "insertdataTypeElement", insertdataTypeElement) Laurent@814: Laurent@814: def removedataTypeElement(self, name): Laurent@814: found = False Laurent@1290: for element in self.dataTypes.getdataType(): andrej@1611: if TextMatched(element.getname(), name): Laurent@1290: self.dataTypes.remove(element) Laurent@814: found = True Laurent@814: break Laurent@814: if not found: Laurent@814: raise ValueError, _("\"%s\" Data Type doesn't exist !!!")%name Laurent@814: setattr(cls, "removedataTypeElement", removedataTypeElement) Laurent@814: Laurent@814: def getpouElements(self): Laurent@814: return self.pous.getpou() Laurent@814: setattr(cls, "getpouElements", getpouElements) Laurent@814: Laurent@814: def getpouElement(self, name): Laurent@814: elements = self.pous.getpou() Laurent@814: for element in elements: andrej@1611: if TextMatched(element.getname(), name): Laurent@814: return element Laurent@814: return None Laurent@814: setattr(cls, "getpouElement", getpouElement) Laurent@814: Laurent@814: def appendpouElement(self, name, pou_type, body_type): Laurent@814: for element in self.pous.getpou(): andrej@1611: if TextMatched(element.getname(), name): Laurent@814: raise ValueError, _("\"%s\" POU already exists !!!")%name Laurent@1290: new_pou = PLCOpenParser.CreateElement("pou", "pous") Laurent@1293: self.pous.appendpou(new_pou) Laurent@814: new_pou.setname(name) Laurent@814: new_pou.setpouType(pou_type) Laurent@1293: new_pou.appendbody(PLCOpenParser.CreateElement("body", "pou")) Laurent@814: new_pou.setbodyType(body_type) Laurent@814: setattr(cls, "appendpouElement", appendpouElement) Laurent@814: Laurent@814: def insertpouElement(self, index, pou): Laurent@814: self.pous.insertpou(index, pou) Laurent@814: setattr(cls, "insertpouElement", insertpouElement) Laurent@814: Laurent@814: def removepouElement(self, name): Laurent@814: found = False Laurent@1290: for element in self.pous.getpou(): andrej@1611: if TextMatched(element.getname(), name): Laurent@1290: self.pous.remove(element) Laurent@814: found = True Laurent@814: break Laurent@814: if not found: Laurent@814: raise ValueError, _("\"%s\" POU doesn't exist !!!")%name Laurent@814: setattr(cls, "removepouElement", removepouElement) Laurent@814: Laurent@814: def Search(self, criteria, parent_infos=[]): Laurent@814: search_result = [] Laurent@814: filter = criteria["filter"] Laurent@814: for datatype in self.dataTypes.getdataType(): Laurent@814: search_result.extend(datatype.Search(criteria, parent_infos)) Laurent@814: for pou in self.pous.getpou(): Laurent@814: search_result.extend(pou.Search(criteria, parent_infos)) Laurent@814: return search_result Laurent@814: setattr(cls, "Search", Search) Laurent@814: Laurent@814: def _updateBaseTypeElementName(self, old_name, new_name): Laurent@814: self.baseType.updateElementName(old_name, new_name) Laurent@814: Laurent@1290: cls = PLCOpenParser.GetElementClass("dataType", "dataTypes") Laurent@814: if cls: Laurent@814: setattr(cls, "updateElementName", _updateBaseTypeElementName) Laurent@814: Laurent@814: def Search(self, criteria, parent_infos=[]): Laurent@814: search_result = [] Laurent@814: filter = criteria["filter"] Laurent@814: if filter == "all" or "datatype" in filter: Laurent@814: parent_infos = parent_infos + ["D::%s" % self.getname()] Laurent@814: search_result.extend(_Search([("name", self.getname())], criteria, parent_infos)) Laurent@814: search_result.extend(self.baseType.Search(criteria, parent_infos)) Laurent@814: if self.initialValue is not None: Laurent@814: search_result.extend(_Search([("initial", self.initialValue.getvalue())], criteria, parent_infos)) Laurent@814: return search_result Laurent@814: setattr(cls, "Search", Search) Laurent@814: Laurent@1290: cls = PLCOpenParser.GetElementClass("dataType") Laurent@814: if cls: Laurent@814: Laurent@814: def updateElementName(self, old_name, new_name): Laurent@1290: content_name = self.content.getLocalTag() Laurent@1290: if content_name in ["derived", "array", "subrangeSigned", "subrangeUnsigned"]: Laurent@1290: self.content.updateElementName(old_name, new_name) Laurent@1290: elif content_name == "struct": Laurent@1290: for element in self.content.getvariable(): Laurent@814: element_type = element.type.updateElementName(old_name, new_name) Laurent@814: setattr(cls, "updateElementName", updateElementName) Laurent@814: Laurent@814: def Search(self, criteria, parent_infos=[]): Laurent@814: search_result = [] Laurent@1290: content_name = self.content.getLocalTag() Laurent@1290: if content_name in ["derived", "array", "enum", "subrangeSigned", "subrangeUnsigned"]: Laurent@1294: search_result.extend(self.content.Search(criteria, parent_infos + ["base"])) Laurent@1290: elif content_name == "struct": Laurent@1294: for i, element in enumerate(self.content.getvariable()): Laurent@814: search_result.extend(element.Search(criteria, parent_infos + ["struct", i])) Laurent@814: else: Laurent@1290: if content_name in ["string", "wstring"]: Laurent@1290: content_name = content_name.upper() Laurent@1290: search_result.extend(_Search([("base", content_name)], criteria, parent_infos)) Laurent@814: return search_result Laurent@814: setattr(cls, "Search", Search) Laurent@814: Laurent@1294: cls = PLCOpenParser.GetElementClass("derived", "dataType") Laurent@1294: if cls: Laurent@1294: def updateElementName(self, old_name, new_name): andrej@1611: if TextMatched(self.name, old_name): Laurent@1294: self.name = new_name Laurent@1294: setattr(cls, "updateElementName", updateElementName) Laurent@1294: Laurent@1294: def Search(self, criteria, parent_infos=[]): Laurent@1294: return [(tuple(parent_infos),) + result for result in TestTextElement(self.name, criteria)] Laurent@1294: setattr(cls, "Search", Search) Laurent@1294: Laurent@1291: cls = PLCOpenParser.GetElementClass("array", "dataType") Laurent@814: if cls: Laurent@814: setattr(cls, "updateElementName", _updateBaseTypeElementName) Laurent@814: Laurent@814: def Search(self, criteria, parent_infos=[]): Laurent@814: search_result = self.baseType.Search(criteria, parent_infos) Laurent@814: for i, dimension in enumerate(self.getdimension()): Laurent@814: search_result.extend(_Search([("lower", dimension.getlower()), Laurent@814: ("upper", dimension.getupper())], Laurent@814: criteria, parent_infos + ["range", i])) Laurent@814: return search_result Laurent@814: setattr(cls, "Search", Search) Laurent@814: Laurent@814: def _SearchInSubrange(self, criteria, parent_infos=[]): Laurent@814: search_result = self.baseType.Search(criteria, parent_infos) Laurent@814: search_result.extend(_Search([("lower", self.range.getlower()), Laurent@814: ("upper", self.range.getupper())], Laurent@814: criteria, parent_infos)) Laurent@814: return search_result Laurent@814: Laurent@1291: cls = PLCOpenParser.GetElementClass("subrangeSigned", "dataType") Laurent@814: if cls: Laurent@814: setattr(cls, "updateElementName", _updateBaseTypeElementName) Laurent@814: setattr(cls, "Search", _SearchInSubrange) Laurent@814: Laurent@1291: cls = PLCOpenParser.GetElementClass("subrangeUnsigned", "dataType") Laurent@814: if cls: Laurent@814: setattr(cls, "updateElementName", _updateBaseTypeElementName) Laurent@814: setattr(cls, "Search", _SearchInSubrange) Laurent@814: Laurent@1291: cls = PLCOpenParser.GetElementClass("enum", "dataType") Laurent@814: if cls: Laurent@814: Laurent@814: def updateElementName(self, old_name, new_name): Laurent@814: pass Laurent@814: setattr(cls, "updateElementName", updateElementName) Laurent@814: Laurent@1305: enumerated_datatype_values_xpath = PLCOpen_XPath("ppx:values/ppx:value") Laurent@814: def Search(self, criteria, parent_infos=[]): Laurent@814: search_result = [] Laurent@1305: for i, value in enumerate(enumerated_datatype_values_xpath(self)): Laurent@814: for result in TestTextElement(value.getname(), criteria): Laurent@814: search_result.append((tuple(parent_infos + ["value", i]),) + result) Laurent@814: return search_result Laurent@814: setattr(cls, "Search", Search) Laurent@814: Laurent@1302: def _getvariableTypeinfos(variable_type): Laurent@1302: type_content = variable_type.getcontent() Laurent@1302: type_content_type = type_content.getLocalTag() Laurent@1302: if type_content_type == "derived": Laurent@1302: return type_content.getname() Laurent@1302: return type_content_type.upper() Laurent@1302: Laurent@1290: cls = PLCOpenParser.GetElementClass("pou", "pous") Laurent@814: if cls: Laurent@814: Laurent@1305: block_inputs_xpath = PLCOpen_XPath( Laurent@1305: "ppx:interface/*[self::ppx:inputVars or self::ppx:inOutVars]/ppx:variable") Laurent@1305: block_outputs_xpath = PLCOpen_XPath( Laurent@1305: "ppx:interface/*[self::ppx:outputVars or self::ppx:inOutVars]/ppx:variable") Laurent@1302: def getblockInfos(self): Laurent@1302: block_infos = { Laurent@1302: "name" : self.getname(), Laurent@1302: "type" : self.getpouType(), Laurent@1302: "extensible" : False, Laurent@1302: "inputs" : [], Laurent@1302: "outputs" : [], Laurent@1310: "comment" : self.getdescription()} Laurent@1302: if self.interface is not None: Laurent@1302: return_type = self.interface.getreturnType() Laurent@1302: if return_type is not None: Laurent@1302: block_infos["outputs"].append( Laurent@1302: ("OUT", _getvariableTypeinfos(return_type), "none")) Laurent@1305: block_infos["inputs"].extend( Laurent@1305: [(var.getname(), _getvariableTypeinfos(var.type), "none") Laurent@1305: for var in block_inputs_xpath(self)]) Laurent@1305: block_infos["outputs"].extend( Laurent@1305: [(var.getname(), _getvariableTypeinfos(var.type), "none") Laurent@1305: for var in block_outputs_xpath(self)]) Laurent@1302: Laurent@1302: block_infos["usage"] = ("\n (%s) => (%s)" % Laurent@1302: (", ".join(["%s:%s" % (input[1], input[0]) Laurent@1302: for input in block_infos["inputs"]]), Laurent@1302: ", ".join(["%s:%s" % (output[1], output[0]) Laurent@1302: for output in block_infos["outputs"]]))) Laurent@1302: return block_infos Laurent@1302: setattr(cls, "getblockInfos", getblockInfos) Laurent@1302: Laurent@814: def setdescription(self, description): Laurent@814: doc = self.getdocumentation() Laurent@814: if doc is None: Laurent@1294: doc = PLCOpenParser.CreateElement("documentation", "pou") Laurent@814: self.setdocumentation(doc) Laurent@1291: doc.setanyText(description) Laurent@814: setattr(cls, "setdescription", setdescription) Laurent@814: Laurent@814: def getdescription(self): Laurent@814: doc = self.getdocumentation() Laurent@814: if doc is not None: Laurent@1291: return doc.getanyText() Laurent@814: return "" Laurent@814: setattr(cls, "getdescription", getdescription) Laurent@814: Laurent@1290: def setbodyType(self, body_type): Laurent@814: if len(self.body) > 0: Laurent@1290: if body_type in ["IL", "ST", "LD", "FBD", "SFC"]: Laurent@1293: self.body[0].setcontent(PLCOpenParser.CreateElement(body_type, "body")) Laurent@814: else: Laurent@814: raise ValueError, "%s isn't a valid body type!"%type Laurent@814: setattr(cls, "setbodyType", setbodyType) Laurent@814: Laurent@814: def getbodyType(self): Laurent@814: if len(self.body) > 0: Laurent@1290: return self.body[0].getcontent().getLocalTag() Laurent@814: setattr(cls, "getbodyType", getbodyType) Laurent@814: Laurent@814: def resetexecutionOrder(self): Laurent@814: if len(self.body) > 0: Laurent@814: self.body[0].resetexecutionOrder() Laurent@814: setattr(cls, "resetexecutionOrder", resetexecutionOrder) Laurent@814: Laurent@814: def compileexecutionOrder(self): Laurent@814: if len(self.body) > 0: Laurent@814: self.body[0].compileexecutionOrder() Laurent@814: setattr(cls, "compileexecutionOrder", compileexecutionOrder) Laurent@814: Laurent@814: def setelementExecutionOrder(self, instance, new_executionOrder): Laurent@814: if len(self.body) > 0: Laurent@814: self.body[0].setelementExecutionOrder(instance, new_executionOrder) Laurent@814: setattr(cls, "setelementExecutionOrder", setelementExecutionOrder) Laurent@814: Laurent@1293: def addinstance(self, instance): Laurent@814: if len(self.body) > 0: Laurent@1293: self.body[0].appendcontentInstance(instance) Laurent@814: setattr(cls, "addinstance", addinstance) Laurent@814: Laurent@814: def getinstances(self): Laurent@814: if len(self.body) > 0: Laurent@814: return self.body[0].getcontentInstances() Laurent@814: return [] Laurent@814: setattr(cls, "getinstances", getinstances) Laurent@814: Laurent@814: def getinstance(self, id): Laurent@814: if len(self.body) > 0: Laurent@814: return self.body[0].getcontentInstance(id) Laurent@814: return None Laurent@814: setattr(cls, "getinstance", getinstance) Laurent@814: Laurent@1331: def getinstancesIds(self): Laurent@814: if len(self.body) > 0: Laurent@1331: return self.body[0].getcontentInstancesIds() Laurent@1331: return [] Laurent@1331: setattr(cls, "getinstancesIds", getinstancesIds) Laurent@814: Laurent@814: def getinstanceByName(self, name): Laurent@814: if len(self.body) > 0: Laurent@814: return self.body[0].getcontentInstanceByName(name) Laurent@814: return None Laurent@814: setattr(cls, "getinstanceByName", getinstanceByName) Laurent@814: Laurent@814: def removeinstance(self, id): Laurent@814: if len(self.body) > 0: Laurent@814: self.body[0].removecontentInstance(id) Laurent@814: setattr(cls, "removeinstance", removeinstance) Laurent@814: Laurent@814: def settext(self, text): Laurent@814: if len(self.body) > 0: Laurent@814: self.body[0].settext(text) Laurent@814: setattr(cls, "settext", settext) Laurent@814: Laurent@814: def gettext(self): Laurent@814: if len(self.body) > 0: Laurent@814: return self.body[0].gettext() Laurent@814: return "" Laurent@814: setattr(cls, "gettext", gettext) Laurent@814: Laurent@814: def getvars(self): Laurent@814: vars = [] Laurent@814: if self.interface is not None: Laurent@814: reverse_types = {} Laurent@814: for name, value in VarTypes.items(): Laurent@814: reverse_types[value] = name Laurent@814: for varlist in self.interface.getcontent(): Laurent@1290: vars.append((reverse_types[varlist.getLocalTag()], varlist)) Laurent@814: return vars Laurent@814: setattr(cls, "getvars", getvars) Laurent@814: Laurent@814: def setvars(self, vars): Laurent@814: if self.interface is None: Laurent@1290: self.interface = PLCOpenParser.CreateElement("interface", "pou") Laurent@1290: self.interface.setcontent(vars) Laurent@814: setattr(cls, "setvars", setvars) Laurent@814: Laurent@1290: def addpouExternalVar(self, var_type, name): Edouard@1406: self.addpouVar(var_type, name, "externalVars") Laurent@814: setattr(cls, "addpouExternalVar", addpouExternalVar) Laurent@814: Edouard@1406: def addpouVar(self, var_type, name, var_class="localVars", location="", description="", initval=""): Laurent@814: if self.interface is None: Laurent@1290: self.interface = PLCOpenParser.CreateElement("interface", "pou") Laurent@814: content = self.interface.getcontent() Laurent@1294: if len(content) == 0: Laurent@1294: varlist = PLCOpenParser.CreateElement(var_class, "interface") Laurent@1294: self.interface.setcontent([varlist]) Laurent@1371: elif content[-1].getLocalTag() != var_class: Laurent@1294: varlist = PLCOpenParser.CreateElement(var_class, "interface") Laurent@1294: content[-1].addnext(varlist) Laurent@814: else: Laurent@1294: varlist = content[-1] Laurent@814: variables = varlist.getvariable() Laurent@814: if varlist.getconstant() or varlist.getretain() or len(variables) > 0 and variables[0].getaddress(): Laurent@1294: varlist = PLCOpenParser.CreateElement(var_class, "interface") Laurent@1294: content[-1].addnext(varlist) Laurent@1293: var = PLCOpenParser.CreateElement("variable", "varListPlain") Laurent@814: var.setname(name) Laurent@1313: var.settype(var_type) Laurent@814: if location != "": Laurent@814: var.setaddress(location) Laurent@814: if description != "": Laurent@1294: ft = PLCOpenParser.CreateElement("documentation", "variable") Laurent@1291: ft.setanyText(description) Laurent@814: var.setdocumentation(ft) Edouard@1406: if initval != "": Edouard@1406: el = PLCOpenParser.CreateElement("initialValue", "variable") Edouard@1406: el.setvalue(initval) Edouard@1406: var.setinitialValue(el) Laurent@814: Laurent@1294: varlist.appendvariable(var) Laurent@814: setattr(cls, "addpouVar", addpouVar) Edouard@1406: setattr(cls, "addpouLocalVar", addpouVar) Laurent@814: Laurent@814: def changepouVar(self, old_type, old_name, new_type, new_name): Laurent@814: if self.interface is not None: Laurent@814: content = self.interface.getcontent() Laurent@814: for varlist in content: Laurent@1294: variables = varlist.getvariable() Laurent@814: for var in variables: andrej@1611: if TextMatched(var.getname(), old_name): Laurent@814: vartype_content = var.gettype().getcontent() andrej@1611: if vartype_content.getLocalTag() == "derived" and TextMatched(vartype_content.getname(), old_type): Laurent@814: var.setname(new_name) Laurent@1290: vartype_content.setname(new_type) Laurent@814: return Laurent@814: setattr(cls, "changepouVar", changepouVar) Laurent@814: Laurent@1290: def removepouVar(self, var_type, name): Laurent@814: if self.interface is not None: Laurent@814: content = self.interface.getcontent() Laurent@814: for varlist in content: Laurent@1290: for var in varlist.getvariable(): andrej@1611: if TextMatched(var.getname(), name): Laurent@814: vartype_content = var.gettype().getcontent() andrej@1611: if vartype_content.getLocalTag() == "derived" and TextMatched(vartype_content.getname(), var_type): Laurent@1290: varlist.remove(var) Laurent@1371: if len(varlist.getvariable()) == 0: Laurent@1371: self.interface.remove(varlist) Laurent@814: break Laurent@814: setattr(cls, "removepouVar", removepouVar) andrej@1626: andrej@1626: def hasstep(self, name=None): andrej@1626: if self.getbodyType() in ["SFC"]: andrej@1626: for instance in self.getinstances(): andrej@1626: if isinstance(instance, PLCOpenParser.GetElementClass("step", "sfcObjects")) and TextMatched(instance.getname(), name): andrej@1626: return True andrej@1626: return False andrej@1626: setattr(cls, "hasstep", hasstep) Laurent@814: Laurent@1142: def hasblock(self, name=None, block_type=None): Laurent@1142: if self.getbodyType() in ["FBD", "LD", "SFC"]: Laurent@814: for instance in self.getinstances(): Laurent@1290: if (isinstance(instance, PLCOpenParser.GetElementClass("block", "fbdObjects")) and andrej@1611: (TextMatched(instance.getinstanceName(), name) or TextMatched(instance.gettypeName(), block_type))): Laurent@814: return True Laurent@814: if self.transitions: Laurent@814: for transition in self.transitions.gettransition(): Laurent@1142: result = transition.hasblock(name, block_type) Laurent@814: if result: Laurent@814: return result Laurent@814: if self.actions: Laurent@814: for action in self.actions.getaction(): Laurent@1142: result = action.hasblock(name, block_type) Laurent@814: if result: Laurent@814: return result Laurent@1142: elif block_type is not None and len(self.body) > 0: Laurent@1142: return self.body[0].hasblock(block_type) Laurent@814: return False Laurent@814: setattr(cls, "hasblock", hasblock) Laurent@814: Laurent@1290: def addtransition(self, name, body_type): Laurent@1293: if self.transitions is None: Laurent@814: self.addtransitions() Laurent@814: self.transitions.settransition([]) Laurent@1290: transition = PLCOpenParser.CreateElement("transition", "transitions") Laurent@1293: self.transitions.appendtransition(transition) Laurent@814: transition.setname(name) Laurent@1290: transition.setbodyType(body_type) Laurent@1290: if body_type == "ST": andrej@1616: transition.settext(":= ;") Laurent@814: setattr(cls, "addtransition", addtransition) Laurent@814: Laurent@814: def gettransition(self, name): Laurent@1293: if self.transitions is not None: Laurent@814: for transition in self.transitions.gettransition(): andrej@1611: if TextMatched(transition.getname(), name): Laurent@814: return transition Laurent@814: return None Laurent@814: setattr(cls, "gettransition", gettransition) Laurent@814: Laurent@814: def gettransitionList(self): Laurent@1293: if self.transitions is not None: Laurent@814: return self.transitions.gettransition() Laurent@814: return [] Laurent@814: setattr(cls, "gettransitionList", gettransitionList) Laurent@814: Laurent@814: def removetransition(self, name): Laurent@1293: if self.transitions is not None: Laurent@814: removed = False Laurent@1290: for transition in self.transitions.gettransition(): andrej@1611: if TextMatched(transition.getname(), name): Laurent@1290: if transition.getbodyType() in ["FBD", "LD", "SFC"]: Laurent@1290: for instance in transition.getinstances(): Laurent@1290: if isinstance(instance, PLCOpenParser.GetElementClass("block", "fbdObjects")): laurent@824: self.removepouVar(instance.gettypeName(), laurent@824: instance.getinstanceName()) Laurent@1290: self.transitions.remove(transition) Laurent@814: removed = True Laurent@1290: break Laurent@814: if not removed: Laurent@814: raise ValueError, _("Transition with name %s doesn't exist!")%name Laurent@814: setattr(cls, "removetransition", removetransition) Laurent@814: Laurent@1290: def addaction(self, name, body_type): Laurent@1293: if self.actions is None: Laurent@814: self.addactions() Laurent@814: self.actions.setaction([]) Laurent@1290: action = PLCOpenParser.CreateElement("action", "actions") Laurent@1293: self.actions.appendaction(action) Laurent@814: action.setname(name) Laurent@1290: action.setbodyType(body_type) Laurent@814: setattr(cls, "addaction", addaction) Laurent@814: Laurent@814: def getaction(self, name): Laurent@1293: if self.actions is not None: Laurent@814: for action in self.actions.getaction(): andrej@1611: if TextMatched(action.getname(), name): Laurent@814: return action Laurent@814: return None Laurent@814: setattr(cls, "getaction", getaction) Laurent@814: Laurent@814: def getactionList(self): Laurent@1382: if self.actions is not None: Laurent@814: return self.actions.getaction() Laurent@814: return [] Laurent@814: setattr(cls, "getactionList", getactionList) laurent@824: Laurent@814: def removeaction(self, name): Laurent@1293: if self.actions is not None: Laurent@814: removed = False Laurent@1290: for action in self.actions.getaction(): andrej@1611: if TextMatched(action.getname(), name): Laurent@1290: if action.getbodyType() in ["FBD", "LD", "SFC"]: Laurent@1290: for instance in action.getinstances(): Laurent@1290: if isinstance(instance, PLCOpenParser.GetElementClass("block", "fbdObjects")): laurent@824: self.removepouVar(instance.gettypeName(), laurent@824: instance.getinstanceName()) Laurent@1290: self.actions.remove(action) Laurent@814: removed = True Laurent@1290: break Laurent@814: if not removed: Laurent@814: raise ValueError, _("Action with name %s doesn't exist!")%name Laurent@814: setattr(cls, "removeaction", removeaction) Laurent@814: Laurent@814: def updateElementName(self, old_name, new_name): Laurent@1293: if self.interface is not None: Laurent@814: for content in self.interface.getcontent(): Laurent@1290: for var in content.getvariable(): Laurent@814: var_address = var.getaddress() Laurent@814: if var_address is not None: andrej@1611: if TextMatched(var_address, old_name): Laurent@814: var.setaddress(new_name) andrej@1611: if TextMatched(var.getname(), old_name): Laurent@814: var.setname(new_name) Laurent@814: var_type_content = var.gettype().getcontent() Laurent@1290: if var_type_content.getLocalTag() == "derived": andrej@1611: if TextMatched(var_type_content.getname(), old_name): Laurent@1290: var_type_content.setname(new_name) Laurent@814: self.body[0].updateElementName(old_name, new_name) Laurent@814: for action in self.getactionList(): Laurent@814: action.updateElementName(old_name, new_name) Laurent@814: for transition in self.gettransitionList(): Laurent@814: transition.updateElementName(old_name, new_name) Laurent@814: setattr(cls, "updateElementName", updateElementName) Laurent@814: Laurent@814: def updateElementAddress(self, address_model, new_leading): Laurent@1293: if self.interface is not None: Laurent@814: for content in self.interface.getcontent(): Laurent@1290: for var in content.getvariable(): Laurent@814: var_address = var.getaddress() Laurent@814: if var_address is not None: Laurent@814: var.setaddress(update_address(var_address, address_model, new_leading)) Laurent@814: self.body[0].updateElementAddress(address_model, new_leading) Laurent@814: for action in self.getactionList(): Laurent@814: action.updateElementAddress(address_model, new_leading) Laurent@814: for transition in self.gettransitionList(): Laurent@814: transition.updateElementAddress(address_model, new_leading) Laurent@814: setattr(cls, "updateElementAddress", updateElementAddress) Laurent@814: Laurent@814: def removeVariableByAddress(self, address): Laurent@1293: if self.interface is not None: Laurent@814: for content in self.interface.getcontent(): Laurent@1290: for variable in content.getvariable(): andrej@1611: if TextMatched(variable.getaddress(), address): Laurent@1290: content.remove(variable) Laurent@814: setattr(cls, "removeVariableByAddress", removeVariableByAddress) Laurent@814: Laurent@814: def removeVariableByFilter(self, address_model): Laurent@1293: if self.interface is not None: Laurent@814: for content in self.interface.getcontent(): Laurent@1290: for variable in content.getvariable(): Laurent@1290: var_address = variable.getaddress() Laurent@814: if var_address is not None: Laurent@814: result = address_model.match(var_address) Laurent@814: if result is not None: Laurent@1290: content.remove(variable) Laurent@814: setattr(cls, "removeVariableByFilter", removeVariableByFilter) Laurent@814: Laurent@814: def Search(self, criteria, parent_infos=[]): Laurent@814: search_result = [] Laurent@814: filter = criteria["filter"] Laurent@814: if filter == "all" or self.getpouType() in filter: surkovsv93@1619: if parent_infos == []: surkovsv93@1619: parent_infos = parent_infos + ["P::%s" % self.getname()] Laurent@814: search_result.extend(_Search([("name", self.getname())], criteria, parent_infos)) Laurent@814: if self.interface is not None: Laurent@814: var_number = 0 Laurent@814: for content in self.interface.getcontent(): Laurent@1290: variable_type = searchResultVarTypes.get(content, "var_local") Laurent@1290: variables = content.getvariable() Laurent@1290: for modifier, has_modifier in [("constant", content.getconstant()), Laurent@1290: ("retain", content.getretain()), Laurent@1290: ("non_retain", content.getnonretain())]: Laurent@814: if has_modifier: Laurent@814: for result in TestTextElement(modifier, criteria): Laurent@814: search_result.append((tuple(parent_infos + [variable_type, (var_number, var_number + len(variables)), modifier]),) + result) Laurent@814: break Laurent@814: for variable in variables: Laurent@814: search_result.extend(variable.Search(criteria, parent_infos + [variable_type, var_number])) Laurent@814: var_number += 1 Laurent@814: if len(self.body) > 0: Laurent@814: search_result.extend(self.body[0].Search(criteria, parent_infos)) Laurent@814: for action in self.getactionList(): Laurent@814: search_result.extend(action.Search(criteria, parent_infos)) Laurent@814: for transition in self.gettransitionList(): Laurent@814: search_result.extend(transition.Search(criteria, parent_infos)) Laurent@814: return search_result Laurent@814: setattr(cls, "Search", Search) Laurent@814: Laurent@1290: def setbodyType(self, body_type): Laurent@1290: if body_type in ["IL", "ST", "LD", "FBD", "SFC"]: Laurent@1290: self.body.setcontent(PLCOpenParser.CreateElement(body_type, "body")) Laurent@814: else: Laurent@814: raise ValueError, "%s isn't a valid body type!"%type Laurent@814: Laurent@814: def getbodyType(self): Laurent@1290: return self.body.getcontent().getLocalTag() Laurent@814: Laurent@814: def resetexecutionOrder(self): Laurent@814: self.body.resetexecutionOrder() Laurent@814: Laurent@814: def compileexecutionOrder(self): Laurent@814: self.body.compileexecutionOrder() Laurent@814: Laurent@814: def setelementExecutionOrder(self, instance, new_executionOrder): Laurent@814: self.body.setelementExecutionOrder(instance, new_executionOrder) Laurent@814: Laurent@1293: def addinstance(self, instance): Laurent@1293: self.body.appendcontentInstance(instance) Laurent@814: Laurent@814: def getinstances(self): Laurent@814: return self.body.getcontentInstances() Laurent@814: Laurent@814: def getinstance(self, id): Laurent@814: return self.body.getcontentInstance(id) Laurent@814: Laurent@814: def getrandomInstance(self, exclude): Laurent@814: return self.body.getcontentRandomInstance(exclude) Laurent@814: Laurent@814: def getinstanceByName(self, name): Laurent@814: return self.body.getcontentInstanceByName(name) Laurent@814: Laurent@814: def removeinstance(self, id): Laurent@814: self.body.removecontentInstance(id) Laurent@814: Laurent@814: def settext(self, text): Laurent@814: self.body.settext(text) Laurent@814: Laurent@814: def gettext(self): Laurent@814: return self.body.gettext() Laurent@814: Laurent@1142: def hasblock(self, name=None, block_type=None): Laurent@1142: if self.getbodyType() in ["FBD", "LD", "SFC"]: Laurent@1142: for instance in self.getinstances(): Laurent@1290: if (isinstance(instance, PLCOpenParser.GetElementClass("block", "fbdObjects")) and andrej@1611: (TextMatched(instance.getinstanceName(), name) or TextMatched(instance.gettypeName(), block_type))): Laurent@1142: return True Laurent@1142: elif block_type is not None: Laurent@1142: return self.body.hasblock(block_type) Laurent@1142: return False Laurent@1142: Laurent@1142: def updateElementName(self, old_name, new_name): Laurent@1142: self.body.updateElementName(old_name, new_name) Laurent@1142: Laurent@1142: def updateElementAddress(self, address_model, new_leading): Laurent@1142: self.body.updateElementAddress(address_model, new_leading) Laurent@1142: Laurent@1142: Laurent@1290: cls = PLCOpenParser.GetElementClass("transition", "transitions") Laurent@814: if cls: Laurent@814: setattr(cls, "setbodyType", setbodyType) Laurent@814: setattr(cls, "getbodyType", getbodyType) Laurent@814: setattr(cls, "resetexecutionOrder", resetexecutionOrder) Laurent@814: setattr(cls, "compileexecutionOrder", compileexecutionOrder) Laurent@814: setattr(cls, "setelementExecutionOrder", setelementExecutionOrder) Laurent@814: setattr(cls, "addinstance", addinstance) Laurent@814: setattr(cls, "getinstances", getinstances) Laurent@814: setattr(cls, "getinstance", getinstance) Laurent@814: setattr(cls, "getrandomInstance", getrandomInstance) Laurent@814: setattr(cls, "getinstanceByName", getinstanceByName) Laurent@814: setattr(cls, "removeinstance", removeinstance) Laurent@814: setattr(cls, "settext", settext) Laurent@814: setattr(cls, "gettext", gettext) Laurent@1142: setattr(cls, "hasblock", hasblock) Laurent@814: setattr(cls, "updateElementName", updateElementName) Laurent@814: setattr(cls, "updateElementAddress", updateElementAddress) Laurent@1142: Laurent@814: def Search(self, criteria, parent_infos): Laurent@814: search_result = [] Laurent@814: parent_infos = parent_infos[:-1] + ["T::%s::%s" % (parent_infos[-1].split("::")[1], self.getname())] Laurent@814: for result in TestTextElement(self.getname(), criteria): Laurent@814: search_result.append((tuple(parent_infos + ["name"]),) + result) Laurent@814: search_result.extend(self.body.Search(criteria, parent_infos)) Laurent@814: return search_result Laurent@814: setattr(cls, "Search", Search) Laurent@814: Laurent@1290: cls = PLCOpenParser.GetElementClass("action", "actions") Laurent@814: if cls: Laurent@814: setattr(cls, "setbodyType", setbodyType) Laurent@814: setattr(cls, "getbodyType", getbodyType) Laurent@814: setattr(cls, "resetexecutionOrder", resetexecutionOrder) Laurent@814: setattr(cls, "compileexecutionOrder", compileexecutionOrder) Laurent@814: setattr(cls, "setelementExecutionOrder", setelementExecutionOrder) Laurent@814: setattr(cls, "addinstance", addinstance) Laurent@814: setattr(cls, "getinstances", getinstances) Laurent@814: setattr(cls, "getinstance", getinstance) Laurent@814: setattr(cls, "getrandomInstance", getrandomInstance) Laurent@814: setattr(cls, "getinstanceByName", getinstanceByName) Laurent@814: setattr(cls, "removeinstance", removeinstance) Laurent@814: setattr(cls, "settext", settext) Laurent@814: setattr(cls, "gettext", gettext) Laurent@1142: setattr(cls, "hasblock", hasblock) Laurent@814: setattr(cls, "updateElementName", updateElementName) Laurent@814: setattr(cls, "updateElementAddress", updateElementAddress) Laurent@1142: Laurent@814: def Search(self, criteria, parent_infos): Laurent@814: search_result = [] Laurent@814: parent_infos = parent_infos[:-1] + ["A::%s::%s" % (parent_infos[-1].split("::")[1], self.getname())] Laurent@814: for result in TestTextElement(self.getname(), criteria): Laurent@814: search_result.append((tuple(parent_infos + ["name"]),) + result) Laurent@814: search_result.extend(self.body.Search(criteria, parent_infos)) Laurent@814: return search_result Laurent@814: setattr(cls, "Search", Search) Laurent@814: Laurent@1290: cls = PLCOpenParser.GetElementClass("body") Laurent@814: if cls: Laurent@814: cls.currentExecutionOrderId = 0 andrej@1629: cls.checkedBlocksDict = {} Laurent@814: def resetcurrentExecutionOrderId(self): Laurent@814: object.__setattr__(self, "currentExecutionOrderId", 0) Laurent@814: setattr(cls, "resetcurrentExecutionOrderId", resetcurrentExecutionOrderId) Laurent@814: Laurent@814: def getnewExecutionOrderId(self): Laurent@814: object.__setattr__(self, "currentExecutionOrderId", self.currentExecutionOrderId + 1) Laurent@814: return self.currentExecutionOrderId Laurent@814: setattr(cls, "getnewExecutionOrderId", getnewExecutionOrderId) Laurent@814: Laurent@814: def resetexecutionOrder(self): Laurent@1290: if self.content.getLocalTag() == "FBD": Laurent@1290: for element in self.content.getcontent(): Laurent@1290: if not isinstance(element, (PLCOpenParser.GetElementClass("comment", "commonObjects"), Laurent@1290: PLCOpenParser.GetElementClass("connector", "commonObjects"), Laurent@1290: PLCOpenParser.GetElementClass("continuation", "commonObjects"))): Laurent@1290: element.setexecutionOrderId(0) andrej@1629: self.checkedBlocksDict.clear() Laurent@814: else: Laurent@814: raise TypeError, _("Can only generate execution order on FBD networks!") Laurent@814: setattr(cls, "resetexecutionOrder", resetexecutionOrder) Laurent@814: Laurent@814: def compileexecutionOrder(self): Laurent@1290: if self.content.getLocalTag() == "FBD": Laurent@814: self.resetexecutionOrder() Laurent@814: self.resetcurrentExecutionOrderId() Laurent@1290: for element in self.content.getcontent(): Laurent@1290: if isinstance(element, PLCOpenParser.GetElementClass("outVariable", "fbdObjects")) and element.getexecutionOrderId() == 0: Laurent@1290: connections = element.connectionPointIn.getconnections() Laurent@814: if connections and len(connections) == 1: Laurent@814: self.compileelementExecutionOrder(connections[0]) Laurent@1290: element.setexecutionOrderId(self.getnewExecutionOrderId()) Laurent@814: else: Laurent@814: raise TypeError, _("Can only generate execution order on FBD networks!") Laurent@814: setattr(cls, "compileexecutionOrder", compileexecutionOrder) Laurent@814: Laurent@814: def compileelementExecutionOrder(self, link): Laurent@1290: if self.content.getLocalTag() == "FBD": Laurent@814: localid = link.getrefLocalId() Laurent@814: instance = self.getcontentInstance(localid) andrej@1629: self.checkedBlocksDict[localid] = True Laurent@1290: if isinstance(instance, PLCOpenParser.GetElementClass("block", "fbdObjects")) and instance.getexecutionOrderId() == 0: Laurent@814: for variable in instance.inputVariables.getvariable(): Laurent@814: connections = variable.connectionPointIn.getconnections() Laurent@814: if connections and len(connections) == 1: andrej@1629: if (self.checkedBlocksDict.has_key(connections[0].getrefLocalId()) == False): andrej@1534: self.compileelementExecutionOrder(connections[0]) andrej@1534: if instance.getexecutionOrderId() == 0: andrej@1534: instance.setexecutionOrderId(self.getnewExecutionOrderId()) Laurent@1290: elif isinstance(instance, PLCOpenParser.GetElementClass("continuation", "commonObjects")) and instance.getexecutionOrderId() == 0: Laurent@814: for tmp_instance in self.getcontentInstances(): andrej@1611: if (isinstance(tmp_instance, PLCOpenParser.GetElementClass("connector", "commonObjects")) and andrej@1611: TextMatched(tmp_instance.getname(), instance.getname()) and tmp_instance.getexecutionOrderId() == 0): Laurent@814: connections = tmp_instance.connectionPointIn.getconnections() Laurent@814: if connections and len(connections) == 1: Laurent@814: self.compileelementExecutionOrder(connections[0]) Laurent@814: else: Laurent@814: raise TypeError, _("Can only generate execution order on FBD networks!") Laurent@814: setattr(cls, "compileelementExecutionOrder", compileelementExecutionOrder) Laurent@814: Laurent@814: def setelementExecutionOrder(self, instance, new_executionOrder): Laurent@1294: if self.content.getLocalTag() == "FBD": Laurent@814: old_executionOrder = instance.getexecutionOrderId() Laurent@814: if old_executionOrder is not None and old_executionOrder != 0 and new_executionOrder != 0: Laurent@1290: for element in self.content.getcontent(): Laurent@1290: if element != instance and not isinstance(element, PLCOpenParser.GetElementClass("comment", "commonObjects")): Laurent@1290: element_executionOrder = element.getexecutionOrderId() Laurent@814: if old_executionOrder <= element_executionOrder <= new_executionOrder: Laurent@1290: element.setexecutionOrderId(element_executionOrder - 1) Laurent@814: if new_executionOrder <= element_executionOrder <= old_executionOrder: Laurent@1290: element.setexecutionOrderId(element_executionOrder + 1) Laurent@814: instance.setexecutionOrderId(new_executionOrder) Laurent@814: else: Laurent@814: raise TypeError, _("Can only generate execution order on FBD networks!") Laurent@814: setattr(cls, "setelementExecutionOrder", setelementExecutionOrder) Laurent@814: Laurent@1293: def appendcontentInstance(self, instance): Laurent@1290: if self.content.getLocalTag() in ["LD","FBD","SFC"]: Laurent@1290: self.content.appendcontent(instance) Laurent@814: else: Laurent@1293: raise TypeError, _("%s body don't have instances!")%self.content.getLocalTag() Laurent@814: setattr(cls, "appendcontentInstance", appendcontentInstance) Laurent@814: Laurent@814: def getcontentInstances(self): Laurent@1290: if self.content.getLocalTag() in ["LD","FBD","SFC"]: Laurent@1290: return self.content.getcontent() Laurent@814: else: Laurent@1293: raise TypeError, _("%s body don't have instances!")%self.content.getLocalTag() Laurent@814: setattr(cls, "getcontentInstances", getcontentInstances) Laurent@1305: Laurent@1305: instance_by_id_xpath = PLCOpen_XPath("*[@localId=$localId]") Laurent@1305: instance_by_name_xpath = PLCOpen_XPath("ppx:block[@instanceName=$name]") Laurent@1290: def getcontentInstance(self, local_id): Laurent@1290: if self.content.getLocalTag() in ["LD","FBD","SFC"]: Laurent@1305: instance = instance_by_id_xpath(self.content, localId=local_id) Laurent@1290: if len(instance) > 0: Laurent@1290: return instance[0] Laurent@814: return None Laurent@814: else: Laurent@1293: raise TypeError, _("%s body don't have instances!")%self.content.getLocalTag() Laurent@814: setattr(cls, "getcontentInstance", getcontentInstance) Laurent@814: Laurent@1331: def getcontentInstancesIds(self): Laurent@1290: if self.content.getLocalTag() in ["LD","FBD","SFC"]: Laurent@1331: return OrderedDict([(instance.getlocalId(), True) Laurent@1331: for instance in self.content]) Laurent@814: else: Laurent@1293: raise TypeError, _("%s body don't have instances!")%self.content.getLocalTag() Laurent@1331: setattr(cls, "getcontentInstancesIds", getcontentInstancesIds) Laurent@814: Laurent@814: def getcontentInstanceByName(self, name): Laurent@1290: if self.content.getLocalTag() in ["LD","FBD","SFC"]: Laurent@1305: instance = instance_by_name_xpath(self.content) Laurent@1290: if len(instance) > 0: Laurent@1290: return instance[0] Laurent@1290: return None Laurent@814: else: Laurent@1293: raise TypeError, _("%s body don't have instances!")%self.content.getLocalTag() Laurent@814: setattr(cls, "getcontentInstanceByName", getcontentInstanceByName) Laurent@814: Laurent@1290: def removecontentInstance(self, local_id): Laurent@1290: if self.content.getLocalTag() in ["LD","FBD","SFC"]: Laurent@1318: instance = instance_by_id_xpath(self.content, localId=local_id) Laurent@1290: if len(instance) > 0: Laurent@1290: self.content.remove(instance[0]) Laurent@1232: else: Laurent@814: raise ValueError, _("Instance with id %d doesn't exist!")%id Laurent@814: else: Laurent@1293: raise TypeError, "%s body don't have instances!"%self.content.getLocalTag() Laurent@814: setattr(cls, "removecontentInstance", removecontentInstance) Laurent@814: Laurent@814: def settext(self, text): Laurent@1290: if self.content.getLocalTag() in ["IL","ST"]: Laurent@1291: self.content.setanyText(text) Laurent@814: else: Laurent@1293: raise TypeError, _("%s body don't have text!")%self.content.getLocalTag() Laurent@814: setattr(cls, "settext", settext) Laurent@814: Laurent@814: def gettext(self): Laurent@1290: if self.content.getLocalTag() in ["IL","ST"]: Laurent@1291: return self.content.getanyText() Laurent@814: else: Laurent@1293: raise TypeError, _("%s body don't have text!")%self.content.getLocalTag() Laurent@814: setattr(cls, "gettext", gettext) Laurent@814: Laurent@1142: def hasblock(self, block_type): Laurent@1290: if self.content.getLocalTag() in ["IL","ST"]: Laurent@1290: return self.content.hasblock(block_type) Laurent@1142: else: Laurent@1293: raise TypeError, _("%s body don't have text!")%self.content.getLocalTag() Laurent@1142: setattr(cls, "hasblock", hasblock) Laurent@1142: Laurent@814: def updateElementName(self, old_name, new_name): Laurent@1290: if self.content.getLocalTag() in ["IL", "ST"]: Laurent@1290: self.content.updateElementName(old_name, new_name) Laurent@814: else: Laurent@1290: for element in self.content.getcontent(): Laurent@1290: element.updateElementName(old_name, new_name) Laurent@814: setattr(cls, "updateElementName", updateElementName) Laurent@814: Laurent@814: def updateElementAddress(self, address_model, new_leading): Laurent@1290: if self.content.getLocalTag() in ["IL", "ST"]: Laurent@1290: self.content.updateElementAddress(address_model, new_leading) Laurent@814: else: Laurent@1290: for element in self.content.getcontent(): Laurent@1290: element.updateElementAddress(address_model, new_leading) Laurent@814: setattr(cls, "updateElementAddress", updateElementAddress) Laurent@814: Laurent@814: def Search(self, criteria, parent_infos=[]): Laurent@1290: if self.content.getLocalTag() in ["IL", "ST"]: Laurent@1290: search_result = self.content.Search(criteria, parent_infos + ["body", 0]) Laurent@814: else: Laurent@814: search_result = [] Laurent@1290: for element in self.content.getcontent(): Laurent@1290: search_result.extend(element.Search(criteria, parent_infos)) Laurent@814: return search_result Laurent@814: setattr(cls, "Search", Search) Laurent@814: Laurent@814: def getx(self): Laurent@814: return self.position.getx() Laurent@814: Laurent@814: def gety(self): Laurent@814: return self.position.gety() Laurent@814: Laurent@814: def setx(self, x): Laurent@814: self.position.setx(x) Laurent@814: Laurent@814: def sety(self, y): Laurent@814: self.position.sety(y) Laurent@814: Laurent@814: def _getBoundingBox(self): Laurent@814: return rect(self.getx(), self.gety(), self.getwidth(), self.getheight()) Laurent@814: Laurent@814: def _getConnectionsBoundingBox(connectionPointIn): Laurent@814: bbox = rect() Laurent@814: connections = connectionPointIn.getconnections() Laurent@814: if connections is not None: Laurent@814: for connection in connections: Laurent@814: for x, y in connection.getpoints(): Laurent@814: bbox.update(x, y) Laurent@814: return bbox Laurent@814: Laurent@814: def _getBoundingBoxSingle(self): Laurent@814: bbox = _getBoundingBox(self) Laurent@814: if self.connectionPointIn is not None: Laurent@814: bbox.union(_getConnectionsBoundingBox(self.connectionPointIn)) Laurent@814: return bbox Laurent@814: Laurent@814: def _getBoundingBoxMultiple(self): Laurent@814: bbox = _getBoundingBox(self) Laurent@814: for connectionPointIn in self.getconnectionPointIn(): Laurent@814: bbox.union(_getConnectionsBoundingBox(connectionPointIn)) Laurent@814: return bbox Laurent@814: Laurent@814: def _filterConnections(connectionPointIn, localId, connections): Laurent@814: in_connections = connectionPointIn.getconnections() Laurent@814: if in_connections is not None: Laurent@1290: for connection in in_connections: Laurent@814: connected = connection.getrefLocalId() Laurent@814: if not connections.has_key((localId, connected)) and \ Laurent@814: not connections.has_key((connected, localId)): Laurent@1290: connectionPointIn.remove(connection) Laurent@814: Laurent@814: def _filterConnectionsSingle(self, connections): Laurent@814: if self.connectionPointIn is not None: Laurent@814: _filterConnections(self.connectionPointIn, self.localId, connections) Laurent@814: Laurent@814: def _filterConnectionsMultiple(self, connections): Laurent@814: for connectionPointIn in self.getconnectionPointIn(): Laurent@814: _filterConnections(connectionPointIn, self.localId, connections) Laurent@814: Laurent@814: def _getconnectionsdefinition(instance, connections_end): Laurent@1290: local_id = instance.getlocalId() Laurent@1290: return dict([((local_id, end), True) for end in connections_end]) Laurent@814: Laurent@814: def _updateConnectionsId(connectionPointIn, translation): Laurent@814: connections_end = [] Laurent@814: connections = connectionPointIn.getconnections() Laurent@814: if connections is not None: Laurent@814: for connection in connections: Laurent@814: refLocalId = connection.getrefLocalId() Laurent@814: new_reflocalId = translation.get(refLocalId, refLocalId) Laurent@814: connection.setrefLocalId(new_reflocalId) Laurent@814: connections_end.append(new_reflocalId) Laurent@814: return connections_end Laurent@814: Laurent@814: def _updateConnectionsIdSingle(self, translation): Laurent@814: connections_end = [] Laurent@814: if self.connectionPointIn is not None: Laurent@814: connections_end = _updateConnectionsId(self.connectionPointIn, translation) Laurent@814: return _getconnectionsdefinition(self, connections_end) Laurent@814: Laurent@814: def _updateConnectionsIdMultiple(self, translation): Laurent@814: connections_end = [] Laurent@814: for connectionPointIn in self.getconnectionPointIn(): Laurent@814: connections_end.extend(_updateConnectionsId(connectionPointIn, translation)) Laurent@814: return _getconnectionsdefinition(self, connections_end) Laurent@814: Laurent@814: def _translate(self, dx, dy): Laurent@814: self.setx(self.getx() + dx) Laurent@814: self.sety(self.gety() + dy) Laurent@814: Laurent@814: def _translateConnections(connectionPointIn, dx, dy): Laurent@814: connections = connectionPointIn.getconnections() Laurent@814: if connections is not None: Laurent@814: for connection in connections: Laurent@814: for position in connection.getposition(): Laurent@814: position.setx(position.getx() + dx) Laurent@814: position.sety(position.gety() + dy) Laurent@814: Laurent@814: def _translateSingle(self, dx, dy): Laurent@814: _translate(self, dx, dy) Laurent@814: if self.connectionPointIn is not None: Laurent@814: _translateConnections(self.connectionPointIn, dx, dy) Laurent@814: Laurent@814: def _translateMultiple(self, dx, dy): Laurent@814: _translate(self, dx, dy) Laurent@814: for connectionPointIn in self.getconnectionPointIn(): Laurent@814: _translateConnections(connectionPointIn, dx, dy) Laurent@814: Laurent@814: def _updateElementName(self, old_name, new_name): Laurent@814: pass Laurent@814: Laurent@814: def _updateElementAddress(self, address_model, new_leading): Laurent@814: pass Laurent@814: Laurent@814: def _SearchInElement(self, criteria, parent_infos=[]): Laurent@814: return [] Laurent@814: Laurent@814: _connectionsFunctions = { Laurent@814: "bbox": {"none": _getBoundingBox, Laurent@814: "single": _getBoundingBoxSingle, Laurent@814: "multiple": _getBoundingBoxMultiple}, Laurent@814: "translate": {"none": _translate, Laurent@814: "single": _translateSingle, Laurent@814: "multiple": _translateMultiple}, Laurent@814: "filter": {"none": lambda self, connections: None, Laurent@814: "single": _filterConnectionsSingle, Laurent@814: "multiple": _filterConnectionsMultiple}, Laurent@814: "update": {"none": lambda self, translation: {}, Laurent@814: "single": _updateConnectionsIdSingle, Laurent@814: "multiple": _updateConnectionsIdMultiple}, Laurent@814: } Laurent@814: Laurent@1290: def _initElementClass(name, parent, connectionPointInType="none"): Laurent@1290: cls = PLCOpenParser.GetElementClass(name, parent) Laurent@814: if cls: Laurent@814: setattr(cls, "getx", getx) Laurent@814: setattr(cls, "gety", gety) Laurent@814: setattr(cls, "setx", setx) Laurent@814: setattr(cls, "sety", sety) Laurent@814: setattr(cls, "updateElementName", _updateElementName) Laurent@814: setattr(cls, "updateElementAddress", _updateElementAddress) Laurent@814: setattr(cls, "getBoundingBox", _connectionsFunctions["bbox"][connectionPointInType]) Laurent@814: setattr(cls, "translate", _connectionsFunctions["translate"][connectionPointInType]) Laurent@814: setattr(cls, "filterConnections", _connectionsFunctions["filter"][connectionPointInType]) Laurent@814: setattr(cls, "updateConnectionsId", _connectionsFunctions["update"][connectionPointInType]) Laurent@814: setattr(cls, "Search", _SearchInElement) Laurent@814: return cls Laurent@814: Laurent@1290: cls = _initElementClass("comment", "commonObjects") Laurent@814: if cls: Laurent@814: def setcontentText(self, text): Laurent@1291: self.content.setanyText(text) Laurent@814: setattr(cls, "setcontentText", setcontentText) Laurent@814: Laurent@814: def getcontentText(self): Laurent@1291: return self.content.getanyText() Laurent@814: setattr(cls, "getcontentText", getcontentText) Laurent@814: Laurent@814: def updateElementName(self, old_name, new_name): Laurent@814: self.content.updateElementName(old_name, new_name) Laurent@814: setattr(cls, "updateElementName", updateElementName) Laurent@814: Laurent@814: def updateElementAddress(self, address_model, new_leading): Laurent@814: self.content.updateElementAddress(address_model, new_leading) Laurent@814: setattr(cls, "updateElementAddress", updateElementAddress) Laurent@814: Laurent@814: def Search(self, criteria, parent_infos=[]): Laurent@814: return self.content.Search(criteria, parent_infos + ["comment", self.getlocalId(), "content"]) Laurent@814: setattr(cls, "Search", Search) Laurent@814: Laurent@1290: cls = _initElementClass("block", "fbdObjects") Laurent@814: if cls: Laurent@814: def getBoundingBox(self): Laurent@814: bbox = _getBoundingBox(self) Laurent@814: for input in self.inputVariables.getvariable(): Laurent@814: bbox.union(_getConnectionsBoundingBox(input.connectionPointIn)) Laurent@814: return bbox Laurent@814: setattr(cls, "getBoundingBox", getBoundingBox) Laurent@814: Laurent@814: def updateElementName(self, old_name, new_name): andrej@1611: if TextMatched(self.typeName, old_name): Laurent@814: self.typeName = new_name Laurent@814: setattr(cls, "updateElementName", updateElementName) Laurent@814: Laurent@814: def filterConnections(self, connections): Laurent@814: for input in self.inputVariables.getvariable(): Laurent@814: _filterConnections(input.connectionPointIn, self.localId, connections) Laurent@814: setattr(cls, "filterConnections", filterConnections) Laurent@814: Laurent@814: def updateConnectionsId(self, translation): Laurent@814: connections_end = [] Laurent@814: for input in self.inputVariables.getvariable(): Laurent@814: connections_end.extend(_updateConnectionsId(input.connectionPointIn, translation)) Laurent@814: return _getconnectionsdefinition(self, connections_end) Laurent@814: setattr(cls, "updateConnectionsId", updateConnectionsId) Laurent@814: Laurent@814: def translate(self, dx, dy): Laurent@814: _translate(self, dx, dy) Laurent@814: for input in self.inputVariables.getvariable(): Laurent@814: _translateConnections(input.connectionPointIn, dx, dy) Laurent@814: setattr(cls, "translate", translate) Laurent@814: Laurent@814: def Search(self, criteria, parent_infos=[]): Laurent@814: parent_infos = parent_infos + ["block", self.getlocalId()] Laurent@814: search_result = _Search([("name", self.getinstanceName()), Laurent@814: ("type", self.gettypeName())], Laurent@814: criteria, parent_infos) Laurent@814: for i, variable in enumerate(self.inputVariables.getvariable()): Laurent@814: for result in TestTextElement(variable.getformalParameter(), criteria): Laurent@814: search_result.append((tuple(parent_infos + ["input", i]),) + result) Laurent@814: for i, variable in enumerate(self.outputVariables.getvariable()): Laurent@814: for result in TestTextElement(variable.getformalParameter(), criteria): Laurent@814: search_result.append((tuple(parent_infos + ["output", i]),) + result) Laurent@814: return search_result Laurent@814: setattr(cls, "Search", Search) Laurent@814: Laurent@1355: _initElementClass("leftPowerRail", "ldObjects") Laurent@1355: _initElementClass("rightPowerRail", "ldObjects", "multiple") Laurent@1355: Laurent@1293: def _UpdateLDElementName(self, old_name, new_name): andrej@1611: if TextMatched(self.variable, old_name): Laurent@1322: self.variable = new_name Laurent@1293: Laurent@1293: def _UpdateLDElementAddress(self, address_model, new_leading): Laurent@1322: self.variable = update_address(self.variable, address_model, new_leading) Laurent@1293: Laurent@1293: def _getSearchInLDElement(ld_element_type): Laurent@1293: def SearchInLDElement(self, criteria, parent_infos=[]): Laurent@1322: return _Search([("reference", self.variable)], criteria, parent_infos + [ld_element_type, self.getlocalId()]) Laurent@1293: return SearchInLDElement Laurent@1293: Laurent@1290: cls = _initElementClass("contact", "ldObjects", "single") Laurent@814: if cls: Laurent@1293: setattr(cls, "updateElementName", _UpdateLDElementName) Laurent@1293: setattr(cls, "updateElementAddress", _UpdateLDElementAddress) Laurent@1293: setattr(cls, "Search", _getSearchInLDElement("contact")) Laurent@814: Laurent@1290: cls = _initElementClass("coil", "ldObjects", "single") Laurent@814: if cls: Laurent@1293: setattr(cls, "updateElementName", _UpdateLDElementName) Laurent@1293: setattr(cls, "updateElementAddress", _UpdateLDElementAddress) Laurent@1293: setattr(cls, "Search", _getSearchInLDElement("coil")) Laurent@814: Laurent@1290: cls = _initElementClass("step", "sfcObjects", "single") Laurent@814: if cls: Laurent@814: def Search(self, criteria, parent_infos=[]): Laurent@814: return _Search([("name", self.getname())], criteria, parent_infos + ["step", self.getlocalId()]) Laurent@814: setattr(cls, "Search", Search) Laurent@814: Laurent@1290: cls = _initElementClass("transition", "sfcObjects") Laurent@814: if cls: Laurent@1290: def setconditionContent(self, condition_type, value): Laurent@1298: if self.condition is None: Laurent@814: self.addcondition() Laurent@1293: if condition_type == "connection": Laurent@1293: condition = PLCOpenParser.CreateElement("connectionPointIn", "condition") Laurent@1293: else: Laurent@1293: condition = PLCOpenParser.CreateElement(condition_type, "condition") Laurent@1293: self.condition.setcontent(condition) Laurent@1290: if condition_type == "reference": Laurent@814: condition.setname(value) Laurent@1290: elif condition_type == "inline": Laurent@1293: condition.setcontent(PLCOpenParser.CreateElement("ST", "inline")) Laurent@1293: condition.settext(value) Laurent@814: setattr(cls, "setconditionContent", setconditionContent) Laurent@814: Laurent@814: def getconditionContent(self): Laurent@1298: if self.condition is not None: Laurent@814: content = self.condition.getcontent() Laurent@1290: values = {"type" : content.getLocalTag()} Laurent@814: if values["type"] == "reference": Laurent@1290: values["value"] = content.getname() Laurent@814: elif values["type"] == "inline": Laurent@1293: values["value"] = content.gettext() Laurent@814: elif values["type"] == "connectionPointIn": Laurent@814: values["type"] = "connection" Laurent@1290: values["value"] = content Laurent@814: return values Laurent@814: return "" Laurent@814: setattr(cls, "getconditionContent", getconditionContent) Laurent@814: Laurent@891: def getconditionConnection(self): Laurent@1298: if self.condition is not None: Laurent@891: content = self.condition.getcontent() Laurent@1299: if content.getLocalTag() == "connectionPointIn": Laurent@1290: return content Laurent@891: return None Laurent@891: setattr(cls, "getconditionConnection", getconditionConnection) Laurent@891: Laurent@891: def getBoundingBox(self): Laurent@891: bbox = _getBoundingBoxSingle(self) Laurent@891: condition_connection = self.getconditionConnection() Laurent@1302: if condition_connection is not None: Laurent@891: bbox.union(_getConnectionsBoundingBox(condition_connection)) Laurent@891: return bbox Laurent@891: setattr(cls, "getBoundingBox", getBoundingBox) Laurent@891: Laurent@891: def translate(self, dx, dy): Laurent@891: _translateSingle(self, dx, dy) Laurent@891: condition_connection = self.getconditionConnection() Laurent@1299: if condition_connection is not None: Laurent@891: _translateConnections(condition_connection, dx, dy) Laurent@891: setattr(cls, "translate", translate) Laurent@891: Laurent@891: def filterConnections(self, connections): Laurent@891: _filterConnectionsSingle(self, connections) Laurent@891: condition_connection = self.getconditionConnection() Laurent@1301: if condition_connection is not None: Laurent@891: _filterConnections(condition_connection, self.localId, connections) Laurent@891: setattr(cls, "filterConnections", filterConnections) Laurent@891: Laurent@891: def updateConnectionsId(self, translation): Laurent@891: connections_end = [] Laurent@891: if self.connectionPointIn is not None: Laurent@891: connections_end = _updateConnectionsId(self.connectionPointIn, translation) Laurent@891: condition_connection = self.getconditionConnection() Laurent@1301: if condition_connection is not None: Laurent@891: connections_end.extend(_updateConnectionsId(condition_connection, translation)) Laurent@891: return _getconnectionsdefinition(self, connections_end) Laurent@891: setattr(cls, "updateConnectionsId", updateConnectionsId) Laurent@891: Laurent@814: def updateElementName(self, old_name, new_name): Laurent@1301: if self.condition is not None: Laurent@814: content = self.condition.getcontent() Laurent@1290: content_name = content.getLocalTag() Laurent@1290: if content_name == "reference": andrej@1611: if TextMatched(content.getname(), old_name): Laurent@1290: content.setname(new_name) Laurent@1290: elif content_name == "inline": Laurent@1290: content.updateElementName(old_name, new_name) Laurent@814: setattr(cls, "updateElementName", updateElementName) Laurent@814: Laurent@814: def updateElementAddress(self, address_model, new_leading): Laurent@1301: if self.condition is not None: Laurent@814: content = self.condition.getcontent() Laurent@1290: content_name = content.getLocalTag() Laurent@1290: if content_name == "reference": Laurent@1290: content.setname(update_address(content.getname(), address_model, new_leading)) Laurent@1290: elif content_name == "inline": Laurent@1290: content.updateElementAddress(address_model, new_leading) Laurent@814: setattr(cls, "updateElementAddress", updateElementAddress) Laurent@814: Laurent@814: def getconnections(self): Laurent@891: condition_connection = self.getconditionConnection() Laurent@1301: if condition_connection is not None: Laurent@891: return condition_connection.getconnections() Laurent@891: return None Laurent@814: setattr(cls, "getconnections", getconnections) Laurent@814: Laurent@814: def Search(self, criteria, parent_infos=[]): Laurent@814: parent_infos = parent_infos + ["transition", self.getlocalId()] Laurent@814: search_result = [] Laurent@814: content = self.condition.getcontent() Laurent@1290: content_name = content.getLocalTag() Laurent@1290: if content_name == "reference": Laurent@1290: search_result.extend(_Search([("reference", content.getname())], criteria, parent_infos)) Laurent@1290: elif content_name == "inline": Laurent@1290: search_result.extend(content.Search(criteria, parent_infos + ["inline"])) Laurent@814: return search_result Laurent@814: setattr(cls, "Search", Search) Laurent@1346: Laurent@1346: _initElementClass("selectionDivergence", "sfcObjects", "single") Laurent@1346: _initElementClass("selectionConvergence", "sfcObjects", "multiple") Laurent@1346: _initElementClass("simultaneousDivergence", "sfcObjects", "single") Laurent@1346: _initElementClass("simultaneousConvergence", "sfcObjects", "multiple") Laurent@814: Laurent@1291: cls = _initElementClass("jumpStep", "sfcObjects", "single") Laurent@814: if cls: Laurent@814: def Search(self, criteria, parent_infos): Laurent@814: return _Search([("target", self.gettargetName())], criteria, parent_infos + ["jump", self.getlocalId()]) Laurent@814: setattr(cls, "Search", Search) Laurent@814: Laurent@1290: cls = PLCOpenParser.GetElementClass("action", "actionBlock") Laurent@814: if cls: Laurent@814: def setreferenceName(self, name): Laurent@1298: if self.reference is not None: Laurent@814: self.reference.setname(name) Laurent@814: setattr(cls, "setreferenceName", setreferenceName) Laurent@814: Laurent@814: def getreferenceName(self): Laurent@1298: if self.reference is not None: Laurent@814: return self.reference.getname() Laurent@814: return None Laurent@814: setattr(cls, "getreferenceName", getreferenceName) Laurent@814: Laurent@814: def setinlineContent(self, content): Laurent@1298: if self.inline is not None: Laurent@1298: self.inline.setcontent(PLCOpenParser.CreateElement("ST", "inline")) Laurent@1298: self.inline.settext(content) Laurent@814: setattr(cls, "setinlineContent", setinlineContent) Laurent@814: Laurent@814: def getinlineContent(self): Laurent@1298: if self.inline is not None: Laurent@1298: return self.inline.gettext() Laurent@814: return None Laurent@814: setattr(cls, "getinlineContent", getinlineContent) Laurent@814: Laurent@814: def updateElementName(self, old_name, new_name): andrej@1611: if self.reference is not None and TextMatched(self.reference.getname(), old_name): Laurent@814: self.reference.setname(new_name) Laurent@1298: if self.inline is not None: Laurent@814: self.inline.updateElementName(old_name, new_name) Laurent@814: setattr(cls, "updateElementName", updateElementName) Laurent@814: Laurent@814: def updateElementAddress(self, address_model, new_leading): Laurent@1298: if self.reference is not None: Laurent@814: self.reference.setname(update_address(self.reference.getname(), address_model, new_leading)) Laurent@1298: if self.inline is not None: Laurent@814: self.inline.updateElementAddress(address_model, new_leading) Laurent@814: setattr(cls, "updateElementAddress", updateElementAddress) Laurent@814: Laurent@814: def Search(self, criteria, parent_infos=[]): Laurent@814: qualifier = self.getqualifier() Laurent@814: if qualifier is None: Laurent@814: qualifier = "N" Laurent@814: return _Search([("inline", self.getinlineContent()), Laurent@814: ("reference", self.getreferenceName()), Laurent@814: ("qualifier", qualifier), Laurent@814: ("duration", self.getduration()), Laurent@814: ("indicator", self.getindicator())], Laurent@814: criteria, parent_infos) Laurent@814: setattr(cls, "Search", Search) Laurent@814: Laurent@1291: cls = _initElementClass("actionBlock", "commonObjects", "single") Laurent@814: if cls: Laurent@814: def setactions(self, actions): Laurent@814: self.action = [] Laurent@814: for params in actions: Laurent@1298: action = PLCOpenParser.CreateElement("action", "actionBlock") Laurent@1298: self.appendaction(action) Laurent@1339: action.setqualifier(params.qualifier) Laurent@1339: if params.type == "reference": Laurent@814: action.addreference() Laurent@1339: action.setreferenceName(params.value) Laurent@814: else: Laurent@814: action.addinline() Laurent@1339: action.setinlineContent(params.value) Laurent@1339: if params.duration != "": Laurent@1339: action.setduration(params.duration) Laurent@1339: if params.indicator != "": Laurent@1339: action.setindicator(params.indicator) Laurent@814: setattr(cls, "setactions", setactions) Laurent@814: Laurent@814: def getactions(self): Laurent@814: actions = [] Laurent@814: for action in self.action: Laurent@814: params = {} Laurent@814: params["qualifier"] = action.getqualifier() Laurent@814: if params["qualifier"] is None: Laurent@814: params["qualifier"] = "N" Laurent@1298: if action.getreference() is not None: Laurent@814: params["type"] = "reference" Laurent@814: params["value"] = action.getreferenceName() Laurent@1298: elif action.getinline() is not None: Laurent@814: params["type"] = "inline" Laurent@814: params["value"] = action.getinlineContent() Laurent@814: duration = action.getduration() Laurent@814: if duration: Laurent@814: params["duration"] = duration Laurent@814: indicator = action.getindicator() Laurent@1298: if indicator is not None: Laurent@814: params["indicator"] = indicator Laurent@814: actions.append(params) Laurent@814: return actions Laurent@814: setattr(cls, "getactions", getactions) Laurent@814: Laurent@814: def updateElementName(self, old_name, new_name): Laurent@814: for action in self.action: Laurent@814: action.updateElementName(old_name, new_name) Laurent@814: setattr(cls, "updateElementName", updateElementName) Laurent@814: Laurent@814: def updateElementAddress(self, address_model, new_leading): Laurent@814: for action in self.action: Laurent@814: action.updateElementAddress(address_model, new_leading) Laurent@814: setattr(cls, "updateElementAddress", updateElementAddress) Laurent@814: Laurent@814: def Search(self, criteria, parent_infos=[]): Laurent@814: parent_infos = parent_infos + ["action_block", self.getlocalId()] Laurent@814: search_result = [] Laurent@814: for idx, action in enumerate(self.action): Laurent@814: search_result.extend(action.Search(criteria, parent_infos + ["action", idx])) Laurent@814: return search_result Laurent@814: setattr(cls, "Search", Search) Laurent@814: Laurent@814: def _SearchInIOVariable(self, criteria, parent_infos=[]): Laurent@1322: return _Search([("expression", self.expression)], criteria, parent_infos + ["io_variable", self.getlocalId()]) Laurent@1293: Laurent@1293: def _UpdateIOElementName(self, old_name, new_name): andrej@1611: if TextMatched(self.expression, old_name): Laurent@1322: self.expression = new_name Laurent@1293: Edouard@1400: def _UpdateIOElementAddress(self, address_model, new_leading): Laurent@1322: self.expression = update_address(self.expression, address_model, new_leading) Laurent@814: Laurent@1291: cls = _initElementClass("inVariable", "fbdObjects") Laurent@814: if cls: Laurent@1293: setattr(cls, "updateElementName", _UpdateIOElementName) Laurent@1293: setattr(cls, "updateElementAddress", _UpdateIOElementAddress) Laurent@814: setattr(cls, "Search", _SearchInIOVariable) Laurent@814: Laurent@1291: cls = _initElementClass("outVariable", "fbdObjects", "single") Laurent@814: if cls: Laurent@1293: setattr(cls, "updateElementName", _UpdateIOElementName) Laurent@1293: setattr(cls, "updateElementAddress", _UpdateIOElementAddress) Laurent@814: setattr(cls, "Search", _SearchInIOVariable) Laurent@814: Laurent@1291: cls = _initElementClass("inOutVariable", "fbdObjects", "single") Laurent@814: if cls: Laurent@1293: setattr(cls, "updateElementName", _UpdateIOElementName) Laurent@1293: setattr(cls, "updateElementAddress", _UpdateIOElementAddress) Laurent@814: setattr(cls, "Search", _SearchInIOVariable) Laurent@814: Laurent@814: Laurent@814: def _SearchInConnector(self, criteria, parent_infos=[]): Laurent@814: return _Search([("name", self.getname())], criteria, parent_infos + ["connector", self.getlocalId()]) Laurent@814: Laurent@1291: cls = _initElementClass("continuation", "commonObjects") Laurent@814: if cls: Laurent@814: setattr(cls, "Search", _SearchInConnector) Laurent@814: Laurent@814: def updateElementName(self, old_name, new_name): andrej@1611: if TextMatched(self.name, old_name): Laurent@814: self.name = new_name Laurent@814: setattr(cls, "updateElementName", updateElementName) Laurent@814: Laurent@1291: cls = _initElementClass("connector", "commonObjects", "single") Laurent@814: if cls: Laurent@814: setattr(cls, "Search", _SearchInConnector) Laurent@814: Laurent@814: def updateElementName(self, old_name, new_name): andrej@1611: if TextMatched(self.name, old_name): Laurent@814: self.name = new_name Laurent@814: setattr(cls, "updateElementName", updateElementName) Laurent@814: Laurent@1290: cls = PLCOpenParser.GetElementClass("connection") Laurent@814: if cls: Laurent@814: def setpoints(self, points): Laurent@1290: positions = [] Laurent@814: for point in points: Laurent@1293: position = PLCOpenParser.CreateElement("position", "connection") Laurent@814: position.setx(point.x) Laurent@814: position.sety(point.y) Laurent@1290: positions.append(position) Laurent@1293: self.position = positions Laurent@814: setattr(cls, "setpoints", setpoints) Laurent@814: Laurent@814: def getpoints(self): Laurent@814: points = [] Laurent@814: for position in self.position: Laurent@814: points.append((position.getx(),position.gety())) Laurent@814: return points Laurent@814: setattr(cls, "getpoints", getpoints) Laurent@814: Laurent@1290: cls = PLCOpenParser.GetElementClass("connectionPointIn") Laurent@814: if cls: Laurent@814: def setrelPositionXY(self, x, y): Laurent@1293: self.relPosition = PLCOpenParser.CreateElement("relPosition", "connectionPointIn") Laurent@814: self.relPosition.setx(x) Laurent@814: self.relPosition.sety(y) Laurent@814: setattr(cls, "setrelPositionXY", setrelPositionXY) Laurent@814: Laurent@814: def getrelPositionXY(self): Laurent@1291: if self.relPosition is not None: Laurent@814: return self.relPosition.getx(), self.relPosition.gety() Laurent@1291: return self.relPosition Laurent@814: setattr(cls, "getrelPositionXY", getrelPositionXY) Laurent@814: Laurent@814: def addconnection(self): Laurent@1293: self.append(PLCOpenParser.CreateElement("connection", "connectionPointIn")) Laurent@814: setattr(cls, "addconnection", addconnection) Laurent@814: Laurent@814: def removeconnection(self, idx): Laurent@1290: if len(self.content) > idx: Laurent@1290: self.remove(self.content[idx]) Laurent@814: setattr(cls, "removeconnection", removeconnection) Laurent@814: Laurent@814: def removeconnections(self): Laurent@1290: self.content = None Laurent@814: setattr(cls, "removeconnections", removeconnections) Laurent@814: Laurent@1305: connection_xpath = PLCOpen_XPath("ppx:connection") Laurent@1305: connection_by_position_xpath = PLCOpen_XPath("ppx:connection[position()=$pos]") Laurent@814: def getconnections(self): Laurent@1305: return connection_xpath(self) Laurent@814: setattr(cls, "getconnections", getconnections) Laurent@814: Laurent@1293: def getconnection(self, idx): Laurent@1305: connection = connection_by_position_xpath(self, pos=idx+1) Laurent@1293: if len(connection) > 0: Laurent@1293: return connection[0] Laurent@1293: return None Laurent@1293: setattr(cls, "getconnection", getconnection) Laurent@1293: Laurent@1290: def setconnectionId(self, idx, local_id): Laurent@1293: connection = self.getconnection(idx) Laurent@1293: if connection is not None: Laurent@1293: connection.setrefLocalId(local_id) Laurent@814: setattr(cls, "setconnectionId", setconnectionId) Laurent@814: Laurent@814: def getconnectionId(self, idx): Laurent@1293: connection = self.getconnection(idx) Laurent@1293: if connection is not None: Laurent@1293: return connection.getrefLocalId() Laurent@814: return None Laurent@814: setattr(cls, "getconnectionId", getconnectionId) Laurent@814: Laurent@814: def setconnectionPoints(self, idx, points): Laurent@1293: connection = self.getconnection(idx) Laurent@1293: if connection is not None: Laurent@1293: connection.setpoints(points) Laurent@814: setattr(cls, "setconnectionPoints", setconnectionPoints) Laurent@814: Laurent@814: def getconnectionPoints(self, idx): Laurent@1293: connection = self.getconnection(idx) Laurent@1293: if connection is not None: Laurent@1293: return connection.getpoints() Laurent@1285: return [] Laurent@814: setattr(cls, "getconnectionPoints", getconnectionPoints) Laurent@814: Laurent@814: def setconnectionParameter(self, idx, parameter): Laurent@1293: connection = self.getconnection(idx) Laurent@1293: if connection is not None: Laurent@1293: connection.setformalParameter(parameter) Laurent@814: setattr(cls, "setconnectionParameter", setconnectionParameter) Laurent@814: Laurent@814: def getconnectionParameter(self, idx): Laurent@1293: connection = self.getconnection(idx) Laurent@1293: if connection is not None: Laurent@1293: return connection.getformalParameter() Laurent@814: return None Laurent@814: setattr(cls, "getconnectionParameter", getconnectionParameter) Laurent@814: Laurent@1290: cls = PLCOpenParser.GetElementClass("connectionPointOut") Laurent@814: if cls: Laurent@814: def setrelPositionXY(self, x, y): Laurent@1293: self.relPosition = PLCOpenParser.CreateElement("relPosition", "connectionPointOut") Laurent@814: self.relPosition.setx(x) Laurent@814: self.relPosition.sety(y) Laurent@814: setattr(cls, "setrelPositionXY", setrelPositionXY) Laurent@814: Laurent@814: def getrelPositionXY(self): Laurent@1291: if self.relPosition is not None: Laurent@814: return self.relPosition.getx(), self.relPosition.gety() Laurent@814: return self.relPosition Laurent@814: setattr(cls, "getrelPositionXY", getrelPositionXY) Laurent@814: Laurent@1290: cls = PLCOpenParser.GetElementClass("value") Laurent@814: if cls: Laurent@814: def setvalue(self, value): Laurent@814: value = value.strip() Laurent@814: if value.startswith("[") and value.endswith("]"): Laurent@1291: content = PLCOpenParser.CreateElement("arrayValue", "value") Laurent@814: elif value.startswith("(") and value.endswith(")"): Laurent@1291: content = PLCOpenParser.CreateElement("structValue", "value") Laurent@814: else: Laurent@1291: content = PLCOpenParser.CreateElement("simpleValue", "value") Laurent@1291: content.setvalue(value) Laurent@1291: self.setcontent(content) Laurent@814: setattr(cls, "setvalue", setvalue) Laurent@814: Laurent@814: def getvalue(self): Laurent@1290: return self.content.getvalue() Laurent@814: setattr(cls, "getvalue", getvalue) Laurent@814: Laurent@814: def extractValues(values): Laurent@814: items = values.split(",") Laurent@814: i = 1 Laurent@814: while i < len(items): Laurent@814: opened = items[i - 1].count("(") + items[i - 1].count("[") Laurent@814: closed = items[i - 1].count(")") + items[i - 1].count("]") Laurent@814: if opened > closed: Laurent@814: items[i - 1] = ','.join([items[i - 1], items.pop(i)]) Laurent@814: elif opened == closed: Laurent@814: i += 1 Laurent@814: else: Laurent@814: raise ValueError, _("\"%s\" is an invalid value!")%value Laurent@814: return items Laurent@814: Laurent@1290: cls = PLCOpenParser.GetElementClass("arrayValue", "value") Laurent@814: if cls: Laurent@814: arrayValue_model = re.compile("([0-9]*)\((.*)\)$") Laurent@814: Laurent@814: def setvalue(self, value): Laurent@1290: elements = [] Laurent@814: for item in extractValues(value[1:-1]): Laurent@814: item = item.strip() Laurent@1290: element = PLCOpenParser.CreateElement("value", "arrayValue") Laurent@814: result = arrayValue_model.match(item) Laurent@814: if result is not None: Laurent@814: groups = result.groups() Laurent@814: element.setrepetitionValue(groups[0]) Laurent@814: element.setvalue(groups[1].strip()) Laurent@814: else: Laurent@814: element.setvalue(item) Laurent@1290: elements.append(element) Laurent@1290: self.value = elements Laurent@814: setattr(cls, "setvalue", setvalue) Laurent@814: Laurent@814: def getvalue(self): Laurent@814: values = [] Laurent@814: for element in self.value: Laurent@1293: try: Laurent@1293: repetition = int(element.getrepetitionValue()) Laurent@1293: except: Laurent@1293: repetition = 1 Laurent@1293: if repetition > 1: Laurent@814: value = element.getvalue() Laurent@814: if value is None: Laurent@814: value = "" Laurent@814: values.append("%s(%s)"%(repetition, value)) Laurent@814: else: Laurent@814: values.append(element.getvalue()) Laurent@814: return "[%s]"%", ".join(values) Laurent@814: setattr(cls, "getvalue", getvalue) Laurent@814: Laurent@1290: cls = PLCOpenParser.GetElementClass("structValue", "value") Laurent@814: if cls: Laurent@814: structValue_model = re.compile("(.*):=(.*)") Laurent@814: Laurent@814: def setvalue(self, value): Laurent@1290: elements = [] Laurent@814: for item in extractValues(value[1:-1]): Laurent@814: result = structValue_model.match(item) Laurent@814: if result is not None: Laurent@814: groups = result.groups() Laurent@1290: element = PLCOpenParser.CreateElement("value", "structValue") Laurent@814: element.setmember(groups[0].strip()) Laurent@814: element.setvalue(groups[1].strip()) Laurent@1290: elements.append(element) Laurent@1290: self.value = elements Laurent@814: setattr(cls, "setvalue", setvalue) Laurent@814: Laurent@814: def getvalue(self): Laurent@814: values = [] Laurent@814: for element in self.value: Laurent@814: values.append("%s := %s"%(element.getmember(), element.getvalue())) Laurent@814: return "(%s)"%", ".join(values) Laurent@814: setattr(cls, "getvalue", getvalue) Laurent@1290: