Laurent@814: #!/usr/bin/env python Laurent@814: # -*- coding: utf-8 -*- Laurent@814: Laurent@814: #This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor Laurent@814: #based on the plcopen standard. Laurent@814: # Laurent@814: #Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD Laurent@814: # Laurent@814: #See COPYING file for copyrights details. Laurent@814: # Laurent@814: #This library is free software; you can redistribute it and/or Laurent@814: #modify it under the terms of the GNU General Public Laurent@814: #License as published by the Free Software Foundation; either Laurent@814: #version 2.1 of the License, or (at your option) any later version. Laurent@814: # Laurent@814: #This library is distributed in the hope that it will be useful, Laurent@814: #but WITHOUT ANY WARRANTY; without even the implied warranty of Laurent@814: #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Laurent@814: #General Public License for more details. Laurent@814: # Laurent@814: #You should have received a copy of the GNU General Public Laurent@814: #License along with this library; if not, write to the Free Software Laurent@814: #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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@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@814: QualifierList = {"N" : False, "R" : False, "S" : False, "L" : True, "D" : True, Laurent@814: "P" : False, "P0" : False, "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: Laurent@814: def TestTextElement(text, criteria): Laurent@814: lines = text.splitlines() Laurent@814: if not criteria["case_sensitive"]: Laurent@814: text = text.upper() 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: Laurent@1290: PLCOpenParser = GenerateParserFromXSD(os.path.join(os.path.split(__file__)[0], "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: Laurent@1290: def LoadProject(filepath): Laurent@1290: project_file = open(filepath) Laurent@1294: project_xml = project_file.read().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@1290: project_file.close() Laurent@1290: Laurent@1290: return etree.fromstring(project_xml, PLCOpenParser) Laurent@1290: Laurent@1305: project_pou_xpath = PLCOpen_XPath("/ppx:project/ppx:types/ppx:pous/ppx:pou") Laurent@1299: def LoadPou(xml_string): Laurent@1299: root = etree.fromstring( Laurent@1299: LOAD_POU_PROJECT_TEMPLATE % xml_string, Laurent@1299: PLCOpenParser) Laurent@1305: return project_pou_xpath(root)[0] 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@1299: root = etree.fromstring( Laurent@1299: LOAD_POU_INSTANCES_PROJECT_TEMPLATE(body_type) % xml_string, Laurent@1299: PLCOpenParser) Laurent@1305: return project_pou_instances_xpath[body_type](root) 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() Laurent@814: index = text.find(old_name) Laurent@814: while index != -1: Laurent@814: if index > 0 and (text[index - 1].isalnum() or text[index - 1] == "_"): Laurent@814: index = text.find(old_name, index + len(old_name)) Laurent@814: elif index < len(text) - len(old_name) and (text[index + len(old_name)].isalnum() or text[index + len(old_name)] == "_"): Laurent@814: index = text.find(old_name, index + len(old_name)) Laurent@814: else: Laurent@814: text = text[:index] + new_name + text[index + len(old_name):] Laurent@814: index = text.find(old_name, index + len(new_name)) 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): Laurent@1294: text = self.getanyText().upper() Laurent@1142: index = text.find(block_type.upper()) Laurent@1142: while index != -1: Laurent@1142: if (not (index > 0 and (text[index - 1].isalnum() or text[index - 1] == "_")) and Laurent@1142: not (index < len(text) - len(block_type) and text[index + len(block_type)] != "(")): Laurent@1142: return True Laurent@1142: index = text.find(block_type.upper(), index + len(block_type)) Laurent@1142: return False 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: Laurent@1301: raise ValueError, _("\"%s\" resource already exists in \"%s\" configuration !!!") % (name, config_name) 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: Laurent@1301: raise ValueError, _("\"%s\" resource doesn't exist in \"%s\" configuration !!!")%(name, config_name) 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: Laurent@814: if var_address == old_name: Laurent@814: var.setaddress(new_name) Laurent@814: if 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 compatibility(self, tree): Laurent@814: if tree.hasAttribute("interval"): Laurent@814: interval = GetAttributeValue(tree._attrs["interval"]) Laurent@814: result = time_model.match(interval) Laurent@814: if result is not None: Laurent@814: values = result.groups() Laurent@814: time_values = [int(v) for v in values[:2]] Laurent@814: seconds = float(values[2]) Laurent@814: time_values.extend([int(seconds), int((seconds % 1) * 1000000)]) Laurent@814: text = "t#" Laurent@814: if time_values[0] != 0: Laurent@814: text += "%dh"%time_values[0] Laurent@814: if time_values[1] != 0: Laurent@814: text += "%dm"%time_values[1] Laurent@814: if time_values[2] != 0: Laurent@814: text += "%ds"%time_values[2] Laurent@814: if time_values[3] != 0: Laurent@814: if time_values[3] % 1000 != 0: Laurent@814: text += "%.3fms"%(float(time_values[3]) / 1000) Laurent@814: else: Laurent@814: text += "%dms"%(time_values[3] / 1000) Laurent@814: NodeSetAttr(tree, "interval", text) Laurent@814: setattr(cls, "compatibility", compatibility) Laurent@814: Laurent@814: def updateElementName(self, old_name, new_name): Laurent@814: if self.single == old_name: Laurent@814: self.single = new_name Laurent@814: if 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 compatibility(self, tree): Laurent@814: if tree.hasAttribute("type"): Laurent@814: NodeRenameAttr(tree, "type", "typeName") Laurent@814: setattr(cls, "compatibility", compatibility) Laurent@814: Laurent@814: def updateElementName(self, old_name, new_name): Laurent@814: if 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: Laurent@814: if 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(): Laurent@814: if 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: Laurent@814: if 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(): Laurent@814: if 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(): Laurent@814: if 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): Laurent@1294: if 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@814: def getrandomInstance(self, exclude): Laurent@814: if len(self.body) > 0: Laurent@814: return self.body[0].getcontentRandomInstance(exclude) Laurent@814: return None Laurent@814: setattr(cls, "getrandomInstance", getrandomInstance) 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 addpouLocalVar(self, var_type, name, location="", description=""): Laurent@1290: self.addpouVar(var_type, name, location=location, description=description) Laurent@814: setattr(cls, "addpouLocalVar", addpouLocalVar) Laurent@814: Laurent@1290: def addpouExternalVar(self, var_type, name): Laurent@814: self.addpouVar(type, name, "externalVars") Laurent@814: setattr(cls, "addpouExternalVar", addpouExternalVar) Laurent@814: Laurent@1290: def addpouVar(self, var_type, name, var_class="localVars", location="", description=""): 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@1294: elif content[-1] != 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) Laurent@814: Laurent@1294: varlist.appendvariable(var) Laurent@814: setattr(cls, "addpouVar", 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: Laurent@814: if var.getname() == old_name: Laurent@814: vartype_content = var.gettype().getcontent() Laurent@1290: if vartype_content.getLocalTag() == "derived" and 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(): Laurent@814: if var.getname() == name: Laurent@814: vartype_content = var.gettype().getcontent() Laurent@1290: if vartype_content.getLocalTag() == "derived" and vartype_content.getname() == var_type: Laurent@1290: varlist.remove(var) Laurent@814: break Laurent@1290: if len(varlist.getvariable()) == 0: Laurent@814: content.remove(varlist) Laurent@814: break Laurent@814: setattr(cls, "removepouVar", removepouVar) 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 Laurent@1142: (name and instance.getinstanceName() == name or Laurent@1142: block_type and 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": Laurent@1291: transition.setanyText(":= ;") Laurent@1290: elif body_type == "IL": Laurent@1291: transition.setanyText("\tST\t%s"%name) 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(): Laurent@814: if 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(): Laurent@1290: if 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(): Laurent@814: if 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@814: if self.actions: 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(): Laurent@1290: if 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: Laurent@814: if var_address == old_name: Laurent@814: var.setaddress(new_name) Laurent@814: if 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": Laurent@1290: if 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(): Laurent@1290: if 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: Laurent@814: 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 Laurent@1142: (name and instance.getinstanceName() == name or Laurent@1142: block_type and 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 Laurent@814: 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) 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) 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: Laurent@814: self.compileelementExecutionOrder(connections[0]) Laurent@814: instance.setexecutionOrderId(self.getnewExecutionOrderId()) Laurent@1290: elif isinstance(instance, PLCOpenParser.GetElementClass("continuation", "commonObjects")) and instance.getexecutionOrderId() == 0: Laurent@814: name = instance.getname() Laurent@814: for tmp_instance in self.getcontentInstances(): Laurent@1290: if isinstance(tmp_instance, PLCOpenParser.GetElementClass("connector", "commonObjects")) and tmp_instance.getname() == name 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@814: def getcontentRandomInstance(self, exclude): Laurent@1290: if self.content.getLocalTag() in ["LD","FBD","SFC"]: Laurent@1291: instance = self.content.xpath("*%s[position()=1]" % Laurent@1291: ("[not(%s)]" % " or ".join( Laurent@1291: map(lambda x: "@localId=%d" % x, exclude)) Laurent@1291: if len(exclude) > 0 else "")) 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, "getcontentRandomInstance", getcontentRandomInstance) 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@814: def _getexecutionOrder(instance, specific_values): Laurent@814: executionOrder = instance.getexecutionOrderId() Laurent@814: if executionOrder is None: Laurent@814: executionOrder = 0 Laurent@814: specific_values["executionOrder"] = executionOrder Laurent@814: Laurent@814: def _getdefaultmodifiers(instance, infos): Laurent@814: infos["negated"] = instance.getnegated() Laurent@814: infos["edge"] = instance.getedge() Laurent@814: Laurent@814: def _getinputmodifiers(instance, infos): Laurent@814: infos["negated"] = instance.getnegatedIn() Laurent@814: infos["edge"] = instance.getedgeIn() Laurent@814: Laurent@814: def _getoutputmodifiers(instance, infos): Laurent@814: infos["negated"] = instance.getnegatedOut() Laurent@814: infos["edge"] = instance.getedgeOut() Laurent@814: Laurent@814: MODIFIERS_FUNCTIONS = {"default": _getdefaultmodifiers, Laurent@814: "input": _getinputmodifiers, Laurent@814: "output": _getoutputmodifiers} Laurent@814: Laurent@814: def _getconnectioninfos(instance, connection, links=False, modifiers=None, parameter=False): Laurent@814: infos = {"position": connection.getrelPositionXY()} Laurent@814: if parameter: Laurent@814: infos["name"] = instance.getformalParameter() Laurent@814: MODIFIERS_FUNCTIONS.get(modifiers, lambda x, y: None)(instance, infos) Laurent@814: if links: Laurent@814: infos["links"] = [] Laurent@814: connections = connection.getconnections() Laurent@814: if connections is not None: Laurent@814: for link in connections: Laurent@814: dic = {"refLocalId": link.getrefLocalId(), Laurent@814: "points": link.getpoints(), Laurent@814: "formalParameter": link.getformalParameter()} Laurent@814: infos["links"].append(dic) Laurent@814: return infos Laurent@814: Laurent@814: def _getelementinfos(instance): Laurent@814: return {"id": instance.getlocalId(), Laurent@814: "x": instance.getx(), Laurent@814: "y": instance.gety(), Laurent@814: "height": instance.getheight(), Laurent@814: "width": instance.getwidth(), Laurent@814: "specific_values": {}, Laurent@814: "inputs": [], Laurent@814: "outputs": []} Laurent@814: Laurent@814: def _getvariableinfosFunction(type, input, output): Laurent@814: def getvariableinfos(self): Laurent@814: infos = _getelementinfos(self) Laurent@814: infos["type"] = type Laurent@814: specific_values = infos["specific_values"] Laurent@1322: specific_values["name"] = self.getexpression() Laurent@814: _getexecutionOrder(self, specific_values) Laurent@814: if input and output: Laurent@814: infos["inputs"].append(_getconnectioninfos(self, self.connectionPointIn, True, "input")) Laurent@814: infos["outputs"].append(_getconnectioninfos(self, self.connectionPointOut, False, "output")) Laurent@814: elif input: Laurent@814: infos["inputs"].append(_getconnectioninfos(self, self.connectionPointIn, True, "default")) Laurent@814: elif output: Laurent@814: infos["outputs"].append(_getconnectioninfos(self, self.connectionPointOut, False, "default")) Laurent@814: return infos Laurent@814: return getvariableinfos Laurent@814: Laurent@814: def _getconnectorinfosFunction(type): Laurent@1291: def getconnectorinfos(self): Laurent@814: infos = _getelementinfos(self) Laurent@814: infos["type"] = type Laurent@814: infos["specific_values"]["name"] = self.getname() Laurent@814: if type == "connector": Laurent@814: infos["inputs"].append(_getconnectioninfos(self, self.connectionPointIn, True)) Laurent@814: elif type == "continuation": Laurent@814: infos["outputs"].append(_getconnectioninfos(self, self.connectionPointOut)) Laurent@814: return infos Laurent@1291: return getconnectorinfos Laurent@814: Laurent@814: def _getpowerrailinfosFunction(type): Laurent@814: def getpowerrailinfos(self): Laurent@814: infos = _getelementinfos(self) Laurent@814: infos["type"] = type Laurent@814: if type == "rightPowerRail": Laurent@814: for connectionPointIn in self.getconnectionPointIn(): Laurent@814: infos["inputs"].append(_getconnectioninfos(self, connectionPointIn, True)) Laurent@814: infos["specific_values"]["connectors"] = len(infos["inputs"]) Laurent@814: elif type == "leftPowerRail": Laurent@814: for connectionPointOut in self.getconnectionPointOut(): Laurent@814: infos["outputs"].append(_getconnectioninfos(self, connectionPointOut)) Laurent@814: infos["specific_values"]["connectors"] = len(infos["outputs"]) Laurent@814: return infos Laurent@814: return getpowerrailinfos Laurent@814: Laurent@1293: def _getldelementinfosFunction(ld_element_type): Laurent@814: def getldelementinfos(self): Laurent@814: infos = _getelementinfos(self) Laurent@1293: infos["type"] = ld_element_type Laurent@814: specific_values = infos["specific_values"] Laurent@1322: specific_values["name"] = self.getvariable() Laurent@814: _getexecutionOrder(self, specific_values) Laurent@814: specific_values["negated"] = self.getnegated() Laurent@814: specific_values["edge"] = self.getedge() Laurent@1294: if ld_element_type == "coil": Laurent@814: specific_values["storage"] = self.getstorage() Laurent@814: infos["inputs"].append(_getconnectioninfos(self, self.connectionPointIn, True)) Laurent@814: infos["outputs"].append(_getconnectioninfos(self, self.connectionPointOut)) Laurent@814: return infos Laurent@814: return getldelementinfos Laurent@814: Laurent@814: DIVERGENCE_TYPES = {(True, True): "simultaneousDivergence", Laurent@814: (True, False): "selectionDivergence", Laurent@814: (False, True): "simultaneousConvergence", Laurent@814: (False, False): "selectionConvergence"} Laurent@814: Laurent@814: def _getdivergenceinfosFunction(divergence, simultaneous): Laurent@814: def getdivergenceinfos(self): Laurent@814: infos = _getelementinfos(self) Laurent@814: infos["type"] = DIVERGENCE_TYPES[(divergence, simultaneous)] Laurent@814: if divergence: Laurent@814: infos["inputs"].append(_getconnectioninfos(self, self.connectionPointIn, True)) Laurent@814: for connectionPointOut in self.getconnectionPointOut(): Laurent@814: infos["outputs"].append(_getconnectioninfos(self, connectionPointOut)) Laurent@814: infos["specific_values"]["connectors"] = len(infos["outputs"]) Laurent@814: else: Laurent@814: for connectionPointIn in self.getconnectionPointIn(): Laurent@814: infos["inputs"].append(_getconnectioninfos(self, connectionPointIn, True)) Laurent@814: infos["outputs"].append(_getconnectioninfos(self, self.connectionPointOut)) Laurent@814: infos["specific_values"]["connectors"] = len(infos["inputs"]) Laurent@814: return infos Laurent@814: return getdivergenceinfos Laurent@814: Laurent@1290: cls = _initElementClass("comment", "commonObjects") Laurent@814: if cls: Laurent@814: def getinfos(self): Laurent@814: infos = _getelementinfos(self) Laurent@814: infos["type"] = "comment" Laurent@814: infos["specific_values"]["content"] = self.getcontentText() Laurent@814: return infos Laurent@814: setattr(cls, "getinfos", getinfos) Laurent@814: 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 getinfos(self): Laurent@814: infos = _getelementinfos(self) Laurent@814: infos["type"] = self.gettypeName() Laurent@814: specific_values = infos["specific_values"] Laurent@814: specific_values["name"] = self.getinstanceName() Laurent@814: _getexecutionOrder(self, specific_values) Laurent@814: for variable in self.inputVariables.getvariable(): Laurent@814: infos["inputs"].append(_getconnectioninfos(variable, variable.connectionPointIn, True, "default", True)) Laurent@814: for variable in self.outputVariables.getvariable(): Laurent@814: infos["outputs"].append(_getconnectioninfos(variable, variable.connectionPointOut, False, "default", True)) Laurent@814: return infos Laurent@814: setattr(cls, "getinfos", getinfos) Laurent@814: Laurent@814: def updateElementName(self, old_name, new_name): Laurent@814: if 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@1290: cls = _initElementClass("leftPowerRail", "ldObjects") Laurent@814: if cls: Laurent@814: setattr(cls, "getinfos", _getpowerrailinfosFunction("leftPowerRail")) Laurent@814: Laurent@1290: cls = _initElementClass("rightPowerRail", "ldObjects", "multiple") Laurent@814: if cls: Laurent@814: setattr(cls, "getinfos", _getpowerrailinfosFunction("rightPowerRail")) Laurent@814: Laurent@1293: def _UpdateLDElementName(self, old_name, new_name): Laurent@1322: if 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@814: setattr(cls, "getinfos", _getldelementinfosFunction("contact")) 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@814: setattr(cls, "getinfos", _getldelementinfosFunction("coil")) 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 getinfos(self): Laurent@814: infos = _getelementinfos(self) Laurent@814: infos["type"] = "step" Laurent@814: specific_values = infos["specific_values"] Laurent@814: specific_values["name"] = self.getname() Laurent@814: specific_values["initial"] = self.getinitialStep() Laurent@1298: if self.connectionPointIn is not None: Laurent@814: infos["inputs"].append(_getconnectioninfos(self, self.connectionPointIn, True)) Laurent@1298: if self.connectionPointOut is not None: Laurent@814: infos["outputs"].append(_getconnectioninfos(self, self.connectionPointOut)) Laurent@1298: if self.connectionPointOutAction is not None: Laurent@814: specific_values["action"] = _getconnectioninfos(self, self.connectionPointOutAction) Laurent@814: return infos Laurent@814: setattr(cls, "getinfos", getinfos) Laurent@814: 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 = PLCOpenParser.GetElementClass("condition", "transition") Laurent@814: if cls: Laurent@814: def compatibility(self, tree): Laurent@814: connections = [] Laurent@814: for child in tree.childNodes: Laurent@814: if child.nodeName == "connection": Laurent@814: connections.append(child) Laurent@814: if len(connections) > 0: Laurent@814: node = CreateNode("connectionPointIn") Laurent@814: relPosition = CreateNode("relPosition") Laurent@814: NodeSetAttr(relPosition, "x", "0") Laurent@814: NodeSetAttr(relPosition, "y", "0") Laurent@814: node.childNodes.append(relPosition) Laurent@814: node.childNodes.extend(connections) Laurent@814: tree.childNodes = [node] Laurent@814: setattr(cls, "compatibility", compatibility) Laurent@814: Laurent@1290: cls = _initElementClass("transition", "sfcObjects") Laurent@814: if cls: Laurent@814: def getinfos(self): Laurent@814: infos = _getelementinfos(self) Laurent@814: infos["type"] = "transition" Laurent@814: specific_values = infos["specific_values"] Laurent@814: priority = self.getpriority() Laurent@814: if priority is None: Laurent@814: priority = 0 Laurent@814: specific_values["priority"] = priority Laurent@814: condition = self.getconditionContent() Laurent@814: specific_values["condition_type"] = condition["type"] Laurent@814: if specific_values["condition_type"] == "connection": Laurent@1298: specific_values["connection"] = _getconnectioninfos(self, condition["value"], True) Laurent@814: else: Laurent@1298: specific_values["condition"] = condition["value"] Laurent@814: infos["inputs"].append(_getconnectioninfos(self, self.connectionPointIn, True)) Laurent@814: infos["outputs"].append(_getconnectioninfos(self, self.connectionPointOut)) Laurent@814: return infos Laurent@814: setattr(cls, "getinfos", getinfos) Laurent@814: 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": Laurent@1290: if 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@814: Laurent@1291: cls = _initElementClass("selectionDivergence", "sfcObjects", "single") Laurent@814: if cls: Laurent@814: setattr(cls, "getinfos", _getdivergenceinfosFunction(True, False)) Laurent@814: Laurent@1291: cls = _initElementClass("selectionConvergence", "sfcObjects", "multiple") Laurent@814: if cls: Laurent@814: setattr(cls, "getinfos", _getdivergenceinfosFunction(False, False)) Laurent@814: Laurent@1291: cls = _initElementClass("simultaneousDivergence", "sfcObjects", "single") Laurent@814: if cls: Laurent@814: setattr(cls, "getinfos", _getdivergenceinfosFunction(True, True)) Laurent@814: Laurent@1291: cls = _initElementClass("simultaneousConvergence", "sfcObjects", "multiple") Laurent@814: if cls: Laurent@814: setattr(cls, "getinfos", _getdivergenceinfosFunction(False, True)) Laurent@814: Laurent@1291: cls = _initElementClass("jumpStep", "sfcObjects", "single") Laurent@814: if cls: Laurent@814: def getinfos(self): Laurent@814: infos = _getelementinfos(self) Laurent@814: infos["type"] = "jump" Laurent@814: infos["specific_values"]["target"] = self.gettargetName() Laurent@814: infos["inputs"].append(_getconnectioninfos(self, self.connectionPointIn, True)) Laurent@814: return infos Laurent@814: setattr(cls, "getinfos", getinfos) Laurent@814: 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 compatibility(self, tree): Laurent@814: relPosition = reduce(lambda x, y: x | (y.nodeName == "relPosition"), tree.childNodes, False) Laurent@814: if not tree.hasAttribute("localId"): Laurent@814: NodeSetAttr(tree, "localId", "0") Laurent@814: if not relPosition: Laurent@814: node = CreateNode("relPosition") Laurent@814: NodeSetAttr(node, "x", "0") Laurent@814: NodeSetAttr(node, "y", "0") Laurent@814: tree.childNodes.insert(0, node) Laurent@814: setattr(cls, "compatibility", compatibility) Laurent@814: 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): Laurent@1298: if self.reference is not None and 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 compatibility(self, tree): Laurent@814: for child in tree.childNodes[:]: Laurent@814: if child.nodeName == "connectionPointOut": Laurent@814: tree.childNodes.remove(child) Laurent@814: setattr(cls, "compatibility", compatibility) Laurent@814: Laurent@814: def getinfos(self): Laurent@814: infos = _getelementinfos(self) Laurent@814: infos["type"] = "actionBlock" Laurent@814: infos["specific_values"]["actions"] = self.getactions() Laurent@814: infos["inputs"].append(_getconnectioninfos(self, self.connectionPointIn, True)) Laurent@814: return infos Laurent@814: setattr(cls, "getinfos", getinfos) Laurent@814: 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@814: action.setqualifier(params["qualifier"]) Laurent@814: if params["type"] == "reference": Laurent@814: action.addreference() Laurent@814: action.setreferenceName(params["value"]) Laurent@814: else: Laurent@814: action.addinline() Laurent@814: action.setinlineContent(params["value"]) Laurent@814: if params.has_key("duration"): Laurent@814: action.setduration(params["duration"]) Laurent@814: if params.has_key("indicator"): Laurent@814: 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): Laurent@1322: if self.expression == old_name: Laurent@1322: self.expression = new_name Laurent@1293: Laurent@1293: def _UpdateIOElementAddress(self, old_name, new_name): 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@814: setattr(cls, "getinfos", _getvariableinfosFunction("input", False, True)) 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@814: setattr(cls, "getinfos", _getvariableinfosFunction("output", True, False)) 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@814: setattr(cls, "getinfos", _getvariableinfosFunction("inout", True, True)) 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, "getinfos", _getconnectorinfosFunction("continuation")) Laurent@814: setattr(cls, "Search", _SearchInConnector) Laurent@814: Laurent@814: def updateElementName(self, old_name, new_name): Laurent@814: if 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, "getinfos", _getconnectorinfosFunction("connector")) Laurent@814: setattr(cls, "Search", _SearchInConnector) Laurent@814: Laurent@814: def updateElementName(self, old_name, new_name): Laurent@814: if 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: