etisserant@0: #!/usr/bin/env python etisserant@0: # -*- coding: utf-8 -*- etisserant@0: etisserant@0: #This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor etisserant@0: #based on the plcopen standard. etisserant@0: # lbessard@58: #Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD etisserant@0: # etisserant@0: #See COPYING file for copyrights details. etisserant@0: # etisserant@0: #This library is free software; you can redistribute it and/or lbessard@2: #modify it under the terms of the GNU General Public etisserant@0: #License as published by the Free Software Foundation; either etisserant@0: #version 2.1 of the License, or (at your option) any later version. etisserant@0: # etisserant@0: #This library is distributed in the hope that it will be useful, etisserant@0: #but WITHOUT ANY WARRANTY; without even the implied warranty of etisserant@0: #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU lbessard@58: #General Public License for more details. etisserant@0: # lbessard@2: #You should have received a copy of the GNU General Public etisserant@0: #License along with this library; if not, write to the Free Software etisserant@0: #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA etisserant@0: lbessard@2: from xmlclass import * lbessard@225: from structures import * lbessard@67: from types import * lbessard@166: import os, re etisserant@0: etisserant@0: """ etisserant@0: Dictionary that makes the relation between var names in plcopen and displayed values etisserant@0: """ etisserant@0: VarTypes = {"Local" : "localVars", "Temp" : "tempVars", "Input" : "inputVars", etisserant@0: "Output" : "outputVars", "InOut" : "inOutVars", "External" : "externalVars", etisserant@0: "Global" : "globalVars", "Access" : "accessVars"} etisserant@0: etisserant@0: """ etisserant@0: Define in which order var types must be displayed etisserant@0: """ etisserant@0: VarOrder = ["Local","Temp","Input","Output","InOut","External","Global","Access"] etisserant@0: etisserant@0: """ etisserant@0: Define which action qualifier must be associated with a duration etisserant@0: """ etisserant@0: QualifierList = {"N" : False, "R" : False, "S" : False, "L" : True, "D" : True, etisserant@0: "P" : False, "P0" : False, "P1" : False, "SD" : True, "DS" : True, "SL" : True} etisserant@0: laurent@384: laurent@461: FILTER_ADDRESS_MODEL = "(%%[IQM](?:[XBWDL])?)(%s)((?:\.[0-9]+)*)" laurent@461: laurent@461: def update_address(address, address_model, new_leading): laurent@461: result = address_model.match(address) laurent@461: if result is None: laurent@461: return address laurent@461: groups = result.groups() laurent@461: return groups[0] + new_leading + groups[2] laurent@461: laurent@384: def _init_and_compare(function, v1, v2): laurent@384: if v1 is None: laurent@384: return v2 laurent@384: if v2 is not None: laurent@384: return function(v1, v2) laurent@384: return v1 laurent@384: laurent@384: """ laurent@384: Helper class for bounding_box calculation laurent@384: """ laurent@384: class rect: laurent@384: laurent@384: def __init__(self, x=None, y=None, width=None, height=None): laurent@384: self.x_min = x laurent@384: self.x_max = None laurent@384: self.y_min = y laurent@384: self.y_max = None laurent@384: if width is not None and x is not None: laurent@384: self.x_max = x + width laurent@384: if height is not None and y is not None: laurent@384: self.y_max = y + height laurent@384: laurent@384: def update(self, x, y): laurent@384: self.x_min = _init_and_compare(min, self.x_min, x) laurent@384: self.x_max = _init_and_compare(max, self.x_max, x) laurent@384: self.y_min = _init_and_compare(min, self.y_min, y) laurent@384: self.y_max = _init_and_compare(max, self.y_max, y) laurent@384: laurent@384: def union(self, rect): laurent@384: self.x_min = _init_and_compare(min, self.x_min, rect.x_min) laurent@384: self.x_max = _init_and_compare(max, self.x_max, rect.x_max) laurent@384: self.y_min = _init_and_compare(min, self.y_min, rect.y_min) laurent@384: self.y_max = _init_and_compare(max, self.y_max, rect.y_max) laurent@384: laurent@384: def bounding_box(self): laurent@384: width = height = None laurent@384: if self.x_min is not None and self.x_max is not None: laurent@384: width = self.x_max - self.x_min laurent@384: if self.y_min is not None and self.y_max is not None: laurent@384: height = self.y_max - self.y_min laurent@384: return self.x_min, self.y_min, width, height laurent@384: laurent@384: laurent@389: PLCOpenClasses = GenerateClassesFromXSD(os.path.join(os.path.split(__file__)[0], "tc6_xml_v201.xsd")) lbessard@2: laurent@384: ElementNameToClass = {} laurent@384: lbessard@67: cls = PLCOpenClasses.get("formattedText", None) lbessard@67: if cls: lbessard@58: def updateElementName(self, old_name, new_name): lbessard@58: index = self.text.find(old_name) lbessard@58: while index != -1: lbessard@58: if index > 0 and (self.text[index - 1].isalnum() or self.text[index - 1] == "_"): lbessard@58: index = self.text.find(old_name, index + len(old_name)) lbessard@58: elif index < len(self.text) - len(old_name) and (self.text[index + len(old_name)].isalnum() or self.text[index + len(old_name)] == "_"): lbessard@58: index = self.text.find(old_name, index + len(old_name)) lbessard@58: else: lbessard@58: self.text = self.text[:index] + new_name + self.text[index + len(old_name):] lbessard@58: index = self.text.find(old_name, index + len(new_name)) lbessard@58: setattr(cls, "updateElementName", updateElementName) lbessard@58: laurent@461: def updateElementAddress(self, address_model, new_leading): laurent@461: startpos = 0 laurent@461: result = address_model.search(self.text, startpos) laurent@461: while result is not None: laurent@461: groups = result.groups() laurent@461: new_address = groups[0] + new_leading + groups[2] laurent@461: self.text = self.text[:result.start()] + new_address + self.text[result.end():] laurent@461: startpos = result.start() + len(new_address) laurent@461: result = address_model.search(self.text, startpos) laurent@461: setattr(cls, "updateElementAddress", updateElementAddress) laurent@461: lbessard@67: cls = PLCOpenClasses.get("project", None) lbessard@67: if cls: lbessard@2: cls.singleLineAttributes = False lbessard@225: cls.EnumeratedDataTypeValues = {} lbessard@225: cls.CustomDataTypeRange = {} lbessard@225: cls.CustomTypeHierarchy = {} lbessard@225: cls.ElementUsingTree = {} lbessard@225: cls.CustomBlockTypes = [] lbessard@2: lbessard@151: def setname(self, name): lbessard@151: self.contentHeader.setname(name) lbessard@151: setattr(cls, "setname", setname) lbessard@145: lbessard@151: def getname(self): lbessard@151: return self.contentHeader.getname() lbessard@151: setattr(cls, "getname", getname) lbessard@151: lbessard@151: def getfileHeader(self): lbessard@2: fileheader = {} lbessard@151: fileheader["companyName"] = self.fileHeader.getcompanyName() lbessard@151: if self.fileHeader.getcompanyURL(): lbessard@151: fileheader["companyURL"] = self.fileHeader.getcompanyURL() lbessard@151: fileheader["productName"] = self.fileHeader.getproductName() lbessard@151: fileheader["productVersion"] = self.fileHeader.getproductVersion() lbessard@151: if self.fileHeader.getproductRelease(): lbessard@151: fileheader["productRelease"] = self.fileHeader.getproductRelease() lbessard@151: fileheader["creationDateTime"] = self.fileHeader.getcreationDateTime() lbessard@151: if self.fileHeader.getcontentDescription(): lbessard@151: fileheader["contentDescription"] = self.fileHeader.getcontentDescription() lbessard@2: return fileheader lbessard@151: setattr(cls, "getfileHeader", getfileHeader) lbessard@151: lbessard@151: def setfileHeader(self, fileheader): lbessard@151: self.fileHeader.setcompanyName(fileheader["companyName"]) laurent@379: if fileheader.has_key("companyURL"): lbessard@151: self.fileHeader.setcompanyURL(fileheader["companyURL"]) lbessard@151: self.fileHeader.setproductName(fileheader["productName"]) lbessard@151: self.fileHeader.setproductVersion(fileheader["productVersion"]) laurent@379: if fileheader.has_key("productRelease"): lbessard@151: self.fileHeader.setproductRelease(fileheader["productRelease"]) lbessard@151: self.fileHeader.setcreationDateTime(fileheader["creationDateTime"]) laurent@379: if fileheader.has_key("contentDescription"): lbessard@151: self.fileHeader.setcontentDescription(fileheader["contentDescription"]) lbessard@151: setattr(cls, "setfileHeader", setfileHeader) lbessard@151: lbessard@151: def getcontentHeader(self): lbessard@145: contentheader = {} lbessard@151: contentheader["projectName"] = self.contentHeader.getname() lbessard@151: if self.contentHeader.getversion(): lbessard@151: contentheader["projectVersion"] = self.contentHeader.getversion() lbessard@151: if self.contentHeader.getmodificationDateTime(): lbessard@151: contentheader["modificationDateTime"] = self.contentHeader.getmodificationDateTime() lbessard@151: if self.contentHeader.getorganization(): lbessard@151: contentheader["organization"] = self.contentHeader.getorganization() lbessard@151: if self.contentHeader.getauthor(): lbessard@151: contentheader["authorName"] = self.contentHeader.getauthor() lbessard@151: if self.contentHeader.getlanguage(): lbessard@151: contentheader["language"] = self.contentHeader.getlanguage() lbessard@151: contentheader["pageSize"] = self.contentHeader.getpageSize() lbessard@151: contentheader["scaling"] = self.contentHeader.getscaling() lbessard@145: return contentheader lbessard@151: setattr(cls, "getcontentHeader", getcontentHeader) lbessard@151: lbessard@151: def setcontentHeader(self, contentheader): lbessard@151: self.contentHeader.setname(contentheader["projectName"]) laurent@379: if contentheader.has_key("projectVersion"): lbessard@151: self.contentHeader.setversion(contentheader["projectVersion"]) laurent@379: if contentheader.has_key("modificationDateTime"): lbessard@151: self.contentHeader.setmodificationDateTime(contentheader["modificationDateTime"]) laurent@379: if contentheader.has_key("organization"): lbessard@151: self.contentHeader.setorganization(contentheader["organization"]) laurent@379: if contentheader.has_key("authorName"): lbessard@151: self.contentHeader.setauthor(contentheader["authorName"]) laurent@379: if contentheader.has_key("language"): lbessard@151: self.contentHeader.setlanguage(contentheader["language"]) lbessard@151: self.contentHeader.setpageSize(*contentheader["pageSize"]) lbessard@151: self.contentHeader.setscaling(contentheader["scaling"]) lbessard@151: setattr(cls, "setcontentHeader", setcontentHeader) lbessard@151: lbessard@151: def getdataTypes(self): lbessard@151: return self.types.getdataTypeElements() lbessard@151: setattr(cls, "getdataTypes", getdataTypes) lbessard@151: lbessard@151: def getdataType(self, name): lbessard@151: return self.types.getdataTypeElement(name) lbessard@151: setattr(cls, "getdataType", getdataType) lbessard@151: lbessard@151: def appenddataType(self, name): laurent@379: if self.CustomTypeHierarchy.has_key(name): lbessard@225: raise ValueError, "\"%s\" Data Type already exists !!!"%name lbessard@151: self.types.appenddataTypeElement(name) lbessard@225: self.AddCustomDataType(self.getdataType(name)) lbessard@151: setattr(cls, "appenddataType", appenddataType) lbessard@125: lbessard@151: def insertdataType(self, index, datatype): lbessard@151: self.types.insertdataTypeElement(index, datatype) lbessard@225: self.AddCustomDataType(datatype) lbessard@151: setattr(cls, "insertdataType", insertdataType) lbessard@151: lbessard@151: def removedataType(self, name): lbessard@151: self.types.removedataTypeElement(name) lbessard@225: self.RefreshDataTypeHierarchy() lbessard@225: self.RefreshElementUsingTree() lbessard@151: setattr(cls, "removedataType", removedataType) lbessard@151: lbessard@151: def getpous(self): lbessard@151: return self.types.getpouElements() lbessard@151: setattr(cls, "getpous", getpous) lbessard@151: lbessard@151: def getpou(self, name): lbessard@151: return self.types.getpouElement(name) lbessard@151: setattr(cls, "getpou", getpou) lbessard@151: lbessard@151: def appendpou(self, name, pou_type, body_type): lbessard@151: self.types.appendpouElement(name, pou_type, body_type) lbessard@225: self.AddCustomBlockType(self.getpou(name)) lbessard@151: setattr(cls, "appendpou", appendpou) etisserant@0: lbessard@151: def insertpou(self, index, pou): lbessard@151: self.types.insertpouElement(index, pou) lbessard@225: self.AddCustomBlockType(pou) lbessard@151: setattr(cls, "insertpou", insertpou) lbessard@151: lbessard@151: def removepou(self, name): lbessard@151: self.types.removepouElement(name) lbessard@225: self.RefreshCustomBlockTypes() lbessard@225: self.RefreshElementUsingTree() lbessard@151: setattr(cls, "removepou", removepou) lbessard@151: lbessard@151: def getconfigurations(self): lbessard@151: configurations = self.instances.configurations.getconfiguration() lbessard@2: if configurations: lbessard@2: return configurations lbessard@2: return [] lbessard@151: setattr(cls, "getconfigurations", getconfigurations) lbessard@151: lbessard@151: def getconfiguration(self, name): lbessard@151: for configuration in self.instances.configurations.getconfiguration(): lbessard@151: if configuration.getname() == name: lbessard@2: return configuration lbessard@2: return None lbessard@151: setattr(cls, "getconfiguration", getconfiguration) lbessard@151: lbessard@151: def addconfiguration(self, name): lbessard@151: for configuration in self.instances.configurations.getconfiguration(): lbessard@151: if configuration.getname() == name: laurent@391: raise ValueError, _("\"%s\" configuration already exists !!!")%name lbessard@2: new_configuration = PLCOpenClasses["configurations_configuration"]() lbessard@151: new_configuration.setname(name) lbessard@151: self.instances.configurations.appendconfiguration(new_configuration) lbessard@151: setattr(cls, "addconfiguration", addconfiguration) lbessard@151: lbessard@151: def removeconfiguration(self, name): lbessard@2: found = False lbessard@151: for idx, configuration in enumerate(self.instances.configurations.getconfiguration()): lbessard@151: if configuration.getname() == name: lbessard@151: self.instances.configurations.removeconfiguration(idx) lbessard@2: found = True lbessard@2: break lbessard@2: if not found: laurent@391: raise ValueError, ("\"%s\" configuration doesn't exist !!!")%name lbessard@151: setattr(cls, "removeconfiguration", removeconfiguration) lbessard@151: lbessard@151: def getconfigurationResource(self, config_name, name): lbessard@151: configuration = self.getconfiguration(config_name) lbessard@2: if configuration: lbessard@151: for resource in configuration.getresource(): lbessard@151: if resource.getname() == name: lbessard@2: return resource lbessard@2: return None lbessard@151: setattr(cls, "getconfigurationResource", getconfigurationResource) lbessard@151: lbessard@151: def addconfigurationResource(self, config_name, name): lbessard@151: configuration = self.getconfiguration(config_name) lbessard@2: if configuration: lbessard@151: for resource in configuration.getresource(): lbessard@151: if resource.getname() == name: laurent@391: raise ValueError, _("\"%s\" resource already exists in \"%s\" configuration !!!")%(name, config_name) lbessard@2: new_resource = PLCOpenClasses["configuration_resource"]() lbessard@151: new_resource.setname(name) lbessard@151: configuration.appendresource(new_resource) lbessard@151: setattr(cls, "addconfigurationResource", addconfigurationResource) lbessard@151: lbessard@151: def removeconfigurationResource(self, config_name, name): lbessard@151: configuration = self.getconfiguration(config_name) lbessard@2: if configuration: etisserant@0: found = False lbessard@151: for idx, resource in enumerate(configuration.getresource()): lbessard@151: if resource.getname() == name: lbessard@151: configuration.removeresource(idx) etisserant@0: found = True etisserant@0: break etisserant@0: if not found: laurent@391: raise ValueError, _("\"%s\" resource doesn't exist in \"%s\" configuration !!!")%(name, config_name) lbessard@151: setattr(cls, "removeconfigurationResource", removeconfigurationResource) lbessard@151: lbessard@151: def updateElementName(self, old_name, new_name): greg@348: for datatype in self.types.getdataTypeElements(): greg@348: datatype_content = datatype.baseType.getcontent() greg@348: if datatype_content["name"] == "derived" and datatype_content["value"].getname() == old_name: greg@348: datatype_content["value"].setname(new_name) greg@348: elif datatype_content["name"] in ["array", "subrangeSigned", "subrangeUnsigned"]: greg@348: basetype_content = datatype_content["value"].baseType.getcontent() greg@348: if basetype_content["name"] == "derived" and basetype_content["value"].getname() == old_name: greg@348: basetype_content["value"].setname(new_name) greg@348: elif datatype_content["name"] == "struct": greg@348: for element in datatype_content["value"].getvariable(): greg@348: element_type = element.type.getcontent() greg@348: if element_type["name"] == "derived" and element_type["value"].getname() == old_name: greg@348: element_type["value"].setname(new_name) lbessard@151: for pou in self.types.getpouElements(): lbessard@58: pou.updateElementName(old_name, new_name) lbessard@151: for configuration in self.instances.configurations.getconfiguration(): lbessard@58: configuration.updateElementName(old_name, new_name) lbessard@58: setattr(cls, "updateElementName", updateElementName) lbessard@58: laurent@461: def updateElementAddress(self, old_leading, new_leading): laurent@461: address_model = re.compile(FILTER_ADDRESS_MODEL % old_leading) laurent@461: for pou in self.types.getpouElements(): laurent@461: pou.updateElementAddress(address_model, new_leading) laurent@461: for configuration in self.instances.configurations.getconfiguration(): laurent@461: configuration.updateElementAddress(address_model, new_leading) laurent@461: setattr(cls, "updateElementAddress", updateElementAddress) laurent@461: laurent@461: def removeVariableByAddress(self, address): laurent@461: for pou in self.types.getpouElements(): laurent@461: pou.removeVariableByAddress(address) laurent@461: for configuration in self.instances.configurations.getconfiguration(): laurent@461: configuration.removeVariableByAddress(address) laurent@461: setattr(cls, "removeVariableByAddress", removeVariableByAddress) laurent@461: laurent@461: def removeVariableByFilter(self, leading): laurent@461: address_model = re.compile(FILTER_ADDRESS_MODEL % leading) laurent@461: for pou in self.types.getpouElements(): laurent@461: pou.removeVariableByFilter(address_model) laurent@461: for configuration in self.instances.configurations.getconfiguration(): laurent@461: configuration.removeVariableByFilter(address_model) laurent@461: setattr(cls, "removeVariableByFilter", removeVariableByFilter) laurent@461: lbessard@225: def RefreshDataTypeHierarchy(self): lbessard@225: self.EnumeratedDataTypeValues = {} lbessard@225: self.CustomDataTypeRange = {} lbessard@225: self.CustomTypeHierarchy = {} lbessard@225: for datatype in self.getdataTypes(): lbessard@225: self.AddCustomDataType(datatype) lbessard@225: setattr(cls, "RefreshDataTypeHierarchy", RefreshDataTypeHierarchy) lbessard@225: lbessard@225: def AddCustomDataType(self, datatype): lbessard@225: name = datatype.getname() lbessard@225: basetype_content = datatype.getbaseType().getcontent() lbessard@225: if basetype_content["value"] is None: lbessard@225: self.CustomTypeHierarchy[name] = basetype_content["name"] lbessard@225: elif basetype_content["name"] in ["string", "wstring"]: lbessard@225: self.CustomTypeHierarchy[name] = basetype_content["name"].upper() lbessard@225: elif basetype_content["name"] == "derived": lbessard@225: self.CustomTypeHierarchy[name] = basetype_content["value"].getname() lbessard@225: elif basetype_content["name"] in ["subrangeSigned", "subrangeUnsigned"]: lbessard@225: range = (basetype_content["value"].range.getlower(), lbessard@225: basetype_content["value"].range.getupper()) lbessard@225: self.CustomDataTypeRange[name] = range lbessard@225: base_type = basetype_content["value"].baseType.getcontent() lbessard@225: if base_type["value"] is None: lbessard@225: self.CustomTypeHierarchy[name] = base_type["name"] lbessard@225: else: lbessard@225: self.CustomTypeHierarchy[name] = base_type["value"].getname() lbessard@225: else: lbessard@225: if basetype_content["name"] == "enum": lbessard@225: values = [] lbessard@225: for value in basetype_content["value"].values.getvalue(): lbessard@225: values.append(value.getname()) lbessard@225: self.EnumeratedDataTypeValues[name] = values lbessard@225: self.CustomTypeHierarchy[name] = "ANY_DERIVED" lbessard@225: setattr(cls, "AddCustomDataType", AddCustomDataType) lbessard@225: lbessard@225: # Update Block types with user-defined pou added lbessard@225: def RefreshCustomBlockTypes(self): lbessard@225: # Reset the tree of user-defined pou cross-use lbessard@225: self.CustomBlockTypes = [] lbessard@225: for pou in self.getpous(): lbessard@225: self.AddCustomBlockType(pou) lbessard@225: setattr(cls, "RefreshCustomBlockTypes", RefreshCustomBlockTypes) lbessard@225: lbessard@225: def AddCustomBlockType(self, pou): lbessard@225: pou_name = pou.getname() lbessard@225: pou_type = pou.getpouType() lbessard@286: block_infos = {"name" : pou_name, "type" : pou_type, "extensible" : False, lbessard@286: "inputs" : [], "outputs" : [], "comment" : "", lbessard@286: "generate" : generate_block, "initialise" : initialise_block} lbessard@286: if pou.getinterface(): lbessard@286: return_type = pou.interface.getreturnType() lbessard@286: if return_type: lbessard@286: var_type = return_type.getcontent() lbessard@286: if var_type["name"] == "derived": lbessard@286: block_infos["outputs"].append(("", var_type["value"].getname(), "none")) lbessard@286: elif var_type["name"] in ["string", "wstring"]: lbessard@286: block_infos["outputs"].append(("", var_type["name"].upper(), "none")) lbessard@286: else: lbessard@286: block_infos["outputs"].append(("", var_type["name"], "none")) lbessard@286: for type, varlist in pou.getvars(): lbessard@286: if type == "InOut": lbessard@286: for var in varlist.getvariable(): lbessard@286: var_type = var.type.getcontent() lbessard@286: if var_type["name"] == "derived": lbessard@286: block_infos["inputs"].append((var.getname(), var_type["value"].getname(), "none")) lbessard@286: block_infos["outputs"].append((var.getname(), var_type["value"].getname(), "none")) lbessard@286: elif var_type["name"] in ["string", "wstring"]: lbessard@286: block_infos["inputs"].append((var.getname(), var_type["name"].upper(), "none")) lbessard@286: block_infos["outputs"].append((var.getname(), var_type["name"].upper(), "none")) lbessard@286: else: lbessard@286: block_infos["inputs"].append((var.getname(), var_type["name"], "none")) lbessard@286: block_infos["outputs"].append((var.getname(), var_type["name"], "none")) lbessard@286: elif type == "Input": lbessard@286: for var in varlist.getvariable(): lbessard@286: var_type = var.type.getcontent() lbessard@286: if var_type["name"] == "derived": lbessard@286: block_infos["inputs"].append((var.getname(), var_type["value"].getname(), "none")) lbessard@286: elif var_type["name"] in ["string", "wstring"]: lbessard@286: block_infos["inputs"].append((var.getname(), var_type["name"].upper(), "none")) lbessard@286: else: lbessard@286: block_infos["inputs"].append((var.getname(), var_type["name"], "none")) lbessard@286: elif type == "Output": lbessard@286: for var in varlist.getvariable(): lbessard@286: var_type = var.type.getcontent() lbessard@286: if var_type["name"] == "derived": lbessard@286: block_infos["outputs"].append((var.getname(), var_type["value"].getname(), "none")) lbessard@286: elif var_type["name"] in ["string", "wstring"]: lbessard@286: block_infos["outputs"].append((var.getname(), var_type["name"].upper(), "none")) lbessard@286: else: lbessard@286: block_infos["outputs"].append((var.getname(), var_type["name"], "none")) lbessard@286: if pou.getbodyType() in ["FBD","LD","SFC"]: lbessard@286: for instance in pou.getinstances(): lbessard@286: if isinstance(instance, PLCOpenClasses.get("commonObjects_comment", None)): lbessard@286: block_infos["comment"] = instance.getcontentText() lbessard@286: self.CustomBlockTypes.append(block_infos) lbessard@225: setattr(cls, "AddCustomBlockType", AddCustomBlockType) lbessard@225: lbessard@225: def RefreshElementUsingTree(self): lbessard@225: # Reset the tree of user-defined element cross-use lbessard@225: self.ElementUsingTree = {} lbessard@225: pous = self.getpous() lbessard@295: datatypes = self.getdataTypes() lbessard@225: # Reference all the user-defined elementu names and initialize the tree of lbessard@225: # user-defined elemnt cross-use lbessard@295: elementnames = [datatype.getname() for datatype in datatypes] + \ lbessard@295: [pou.getname() for pou in pous] lbessard@295: for name in elementnames: lbessard@225: self.ElementUsingTree[name] = [] lbessard@295: # Analyze each datatype lbessard@295: for datatype in datatypes: lbessard@295: name = datatype.getname() lbessard@295: basetype_content = datatype.baseType.getcontent() lbessard@295: if basetype_content["name"] == "derived": lbessard@295: typename = basetype_content["value"].getname() laurent@379: if name in self.ElementUsingTree[typename]: lbessard@295: self.ElementUsingTree[typename].append(name) lbessard@295: elif basetype_content["name"] in ["subrangeSigned", "subrangeUnsigned", "array"]: lbessard@295: base_type = basetype_content["value"].baseType.getcontent() lbessard@295: if base_type["name"] == "derived": lbessard@295: typename = base_type["value"].getname() laurent@379: if self.ElementUsingTree.has_key(typename) and name not in self.ElementUsingTree[typename]: lbessard@295: self.ElementUsingTree[typename].append(name) lbessard@295: elif basetype_content["name"] == "struct": lbessard@295: for element in basetype_content["value"].getvariable(): lbessard@295: type_content = element.type.getcontent() lbessard@295: if type_content["name"] == "derived": lbessard@295: typename = type_content["value"].getname() laurent@379: if self.ElementUsingTree.has_key(typename) and name not in self.ElementUsingTree[typename]: lbessard@295: self.ElementUsingTree[typename].append(name) lbessard@225: # Analyze each pou lbessard@225: for pou in pous: lbessard@225: name = pou.getname() lbessard@225: if pou.interface: lbessard@225: # Extract variables from every varLists lbessard@225: for type, varlist in pou.getvars(): lbessard@225: for var in varlist.getvariable(): lbessard@225: vartype_content = var.gettype().getcontent() lbessard@225: if vartype_content["name"] == "derived": lbessard@225: typename = vartype_content["value"].getname() laurent@379: if self.ElementUsingTree.has_key(typename) and name not in self.ElementUsingTree[typename]: lbessard@225: self.ElementUsingTree[typename].append(name) lbessard@225: setattr(cls, "RefreshElementUsingTree", RefreshElementUsingTree) lbessard@225: lbessard@225: def GetParentType(self, type): laurent@379: if self.CustomTypeHierarchy.has_key(type): lbessard@225: return self.CustomTypeHierarchy[type] laurent@379: elif TypeHierarchy.has_key(type): lbessard@225: return TypeHierarchy[type] lbessard@225: return None lbessard@225: setattr(cls, "GetParentType", GetParentType) lbessard@225: lbessard@225: def GetBaseType(self, type): lbessard@225: parent_type = self.GetParentType(type) lbessard@225: if parent_type is not None: lbessard@225: if parent_type.startswith("ANY"): lbessard@225: return type lbessard@225: else: lbessard@225: return self.GetBaseType(parent_type) lbessard@225: return None lbessard@225: setattr(cls, "GetBaseType", GetBaseType) lbessard@225: lbessard@225: def GetSubrangeBaseTypes(self, exclude): lbessard@225: derived = [] lbessard@225: for type in self.CustomTypeHierarchy.keys(): lbessard@225: for base_type in DataTypeRange.keys(): lbessard@225: if self.IsOfType(type, base_type) and not self.IsOfType(type, exclude): lbessard@225: derived.append(type) lbessard@225: break lbessard@225: return DataTypeRange.keys() + derived lbessard@225: setattr(cls, "GetSubrangeBaseTypes", GetSubrangeBaseTypes) lbessard@225: lbessard@225: """ lbessard@225: returns true if the given data type is the same that "reference" meta-type or one of its types. lbessard@225: """ lbessard@225: def IsOfType(self, type, reference): lbessard@225: if reference is None: lbessard@225: return True lbessard@225: elif type == reference: lbessard@225: return True lbessard@225: else: lbessard@225: parent_type = self.GetParentType(type) lbessard@225: if parent_type is not None: lbessard@225: return self.IsOfType(parent_type, reference) lbessard@225: return False lbessard@225: setattr(cls, "IsOfType", IsOfType) lbessard@225: lbessard@225: # Return if pou given by name is used by another pou lbessard@225: def ElementIsUsed(self, name): laurent@379: if self.ElementUsingTree.has_key(name): lbessard@225: return len(self.ElementUsingTree[name]) > 0 lbessard@225: return False lbessard@225: setattr(cls, "ElementIsUsed", ElementIsUsed) lbessard@225: lbessard@225: def DataTypeIsDerived(self, name): lbessard@225: return name in self.CustomTypeHierarchy.values() lbessard@225: setattr(cls, "DataTypeIsDerived", DataTypeIsDerived) lbessard@225: lbessard@225: # Return if pou given by name is directly or undirectly used by the reference pou lbessard@225: def ElementIsUsedBy(self, name, reference): laurent@379: if self.ElementUsingTree.has_key(name): lbessard@225: list = self.ElementUsingTree[name] lbessard@225: # Test if pou is directly used by reference lbessard@225: if reference in list: lbessard@225: return True lbessard@225: else: lbessard@225: # Test if pou is undirectly used by reference, by testing if pous lbessard@225: # that directly use pou is directly or undirectly used by reference lbessard@225: used = False lbessard@225: for element in list: lbessard@225: used |= self.ElementIsUsedBy(element, reference) lbessard@225: return used lbessard@225: return False lbessard@225: setattr(cls, "ElementIsUsedBy", ElementIsUsedBy) lbessard@225: lbessard@225: def GetDataTypeRange(self, type): laurent@379: if self.CustomDataTypeRange.has_key(type): lbessard@225: return self.CustomDataTypeRange[type] laurent@379: elif DataTypeRange.has_key(type): lbessard@225: return DataTypeRange[type] lbessard@225: else: lbessard@225: parent_type = self.GetParentType(type) lbessard@225: if parent_type is not None: lbessard@225: return self.GetDataTypeRange(parent_type) lbessard@225: return None lbessard@225: setattr(cls, "GetDataTypeRange", GetDataTypeRange) lbessard@225: lbessard@225: def GetEnumeratedDataTypeValues(self, type = None): lbessard@225: if type is None: lbessard@225: all_values = [] lbessard@225: for values in self.EnumeratedDataTypeValues.values(): lbessard@225: all_values.extend(values) lbessard@225: return all_values laurent@379: elif self.EnumeratedDataTypeValues.has_key(type): laurent@379: return self.EnumeratedDataTypeValues[type] lbessard@225: return [] lbessard@225: setattr(cls, "GetEnumeratedDataTypeValues", GetEnumeratedDataTypeValues) lbessard@225: lbessard@225: # Function that returns the block definition associated to the block type given lbessard@225: def GetCustomBlockType(self, type, inputs = None): lbessard@225: for customblocktype in self.CustomBlockTypes: lbessard@225: if inputs: lbessard@225: customblock_inputs = tuple([var_type for name, var_type, modifier in customblocktype["inputs"]]) lbessard@225: same_inputs = inputs == customblock_inputs lbessard@225: else: lbessard@225: same_inputs = True lbessard@225: if customblocktype["name"] == type and same_inputs: lbessard@225: return customblocktype lbessard@225: return None lbessard@225: setattr(cls, "GetCustomBlockType", GetCustomBlockType) lbessard@225: lbessard@225: # Return Block types checking for recursion lbessard@251: def GetCustomBlockTypes(self, exclude = "", onlyfunctions = False): lbessard@238: type = None lbessard@238: if exclude != "": lbessard@238: pou = self.getpou(exclude) lbessard@238: if pou is not None: lbessard@238: type = pou.getpouType() lbessard@225: customblocktypes = [] lbessard@225: for customblocktype in self.CustomBlockTypes: lbessard@286: if customblocktype["type"] != "program" and customblocktype["name"] != exclude and not self.ElementIsUsedBy(exclude, customblocktype["name"]) and not (onlyfunctions and customblocktype["type"] != "function"): lbessard@225: customblocktypes.append(customblocktype) lbessard@225: return customblocktypes lbessard@225: setattr(cls, "GetCustomBlockTypes", GetCustomBlockTypes) lbessard@225: lbessard@225: # Return Function Block types checking for recursion lbessard@225: def GetCustomFunctionBlockTypes(self, exclude = ""): lbessard@225: customblocktypes = [] lbessard@225: for customblocktype in self.CustomBlockTypes: lbessard@286: if customblocktype["type"] == "functionBlock" and customblocktype["name"] != exclude and not self.ElementIsUsedBy(exclude, customblocktype["name"]): lbessard@246: customblocktypes.append(customblocktype["name"]) lbessard@225: return customblocktypes lbessard@225: setattr(cls, "GetCustomFunctionBlockTypes", GetCustomFunctionBlockTypes) lbessard@225: lbessard@225: # Return Block types checking for recursion lbessard@225: def GetCustomBlockResource(self): lbessard@225: customblocktypes = [] lbessard@225: for customblocktype in self.CustomBlockTypes: lbessard@225: if customblocktype["type"] == "program": lbessard@225: customblocktypes.append(customblocktype["name"]) lbessard@225: return customblocktypes lbessard@225: setattr(cls, "GetCustomBlockResource", GetCustomBlockResource) lbessard@225: lbessard@225: # Return Data Types checking for recursion laurent@484: def GetCustomDataTypes(self, exclude = "", complextypes = True): lbessard@225: customdatatypes = [] lbessard@225: for customdatatype in self.getdataTypes(): lbessard@225: customdatatype_name = customdatatype.getname() laurent@484: if not complextypes and customdatatype.baseType.getcontent()["name"] in ["array", "struct"]: laurent@484: continue lbessard@225: if customdatatype_name != exclude and not self.ElementIsUsedBy(exclude, customdatatype_name): lbessard@225: customdatatypes.append(customdatatype_name) lbessard@225: return customdatatypes lbessard@225: setattr(cls, "GetCustomDataTypes", GetCustomDataTypes) lbessard@225: lbessard@67: cls = PLCOpenClasses.get("project_fileHeader", None) lbessard@67: if cls: lbessard@67: cls.singleLineAttributes = False lbessard@67: lbessard@145: cls = PLCOpenClasses.get("project_contentHeader", None) lbessard@145: if cls: lbessard@145: cls.singleLineAttributes = False lbessard@145: lbessard@151: def setpageSize(self, width, height): lbessard@151: self.coordinateInfo.setpageSize(width, height) lbessard@151: setattr(cls, "setpageSize", setpageSize) lbessard@151: lbessard@151: def getpageSize(self): lbessard@151: return self.coordinateInfo.getpageSize() lbessard@151: setattr(cls, "getpageSize", getpageSize) lbessard@151: lbessard@151: def setscaling(self, scaling): lbessard@145: for language, (x, y) in scaling.items(): lbessard@151: self.coordinateInfo.setscaling(language, x, y) lbessard@151: setattr(cls, "setscaling", setscaling) lbessard@151: lbessard@151: def getscaling(self): lbessard@145: scaling = {} lbessard@151: scaling["FBD"] = self.coordinateInfo.getscaling("FBD") lbessard@151: scaling["LD"] = self.coordinateInfo.getscaling("LD") lbessard@151: scaling["SFC"] = self.coordinateInfo.getscaling("SFC") lbessard@145: return scaling lbessard@151: setattr(cls, "getscaling", getscaling) lbessard@145: lbessard@145: cls = PLCOpenClasses.get("contentHeader_coordinateInfo", None) lbessard@145: if cls: lbessard@151: def setpageSize(self, width, height): lbessard@145: if width == 0 and height == 0: lbessard@151: self.deletepageSize() lbessard@145: else: lbessard@145: if self.pageSize is None: lbessard@151: self.addpageSize() lbessard@151: self.pageSize.setx(width) lbessard@151: self.pageSize.sety(height) lbessard@151: setattr(cls, "setpageSize", setpageSize) lbessard@151: lbessard@151: def getpageSize(self): lbessard@145: if self.pageSize is not None: lbessard@151: return self.pageSize.getx(), self.pageSize.gety() lbessard@145: return 0, 0 lbessard@151: setattr(cls, "getpageSize", getpageSize) lbessard@151: lbessard@151: def setscaling(self, language, x, y): lbessard@145: if language == "FBD": lbessard@151: self.fbd.scaling.setx(x) lbessard@151: self.fbd.scaling.sety(y) lbessard@145: elif language == "LD": lbessard@151: self.ld.scaling.setx(x) lbessard@151: self.ld.scaling.sety(y) lbessard@145: elif language == "SFC": lbessard@151: self.sfc.scaling.setx(x) lbessard@151: self.sfc.scaling.sety(y) lbessard@151: setattr(cls, "setscaling", setscaling) lbessard@151: lbessard@151: def getscaling(self, language): lbessard@145: if language == "FBD": lbessard@151: return self.fbd.scaling.getx(), self.fbd.scaling.gety() lbessard@145: elif language == "LD": lbessard@151: return self.ld.scaling.getx(), self.ld.scaling.gety() lbessard@145: elif language == "SFC": lbessard@151: return self.sfc.scaling.getx(), self.sfc.scaling.gety() lbessard@145: return 0, 0 lbessard@151: setattr(cls, "getscaling", getscaling) lbessard@145: lbessard@67: cls = PLCOpenClasses.get("configurations_configuration", None) lbessard@67: if cls: lbessard@58: def updateElementName(self, old_name, new_name): laurent@461: for varlist in self.getglobalVars(): laurent@461: for var in varlist.getvariable(): laurent@461: var_address = var.getaddress() laurent@461: if var_address is not None: laurent@470: if var_address == old_name: laurent@461: var.setaddress(new_name) laurent@461: if var.getname() == old_name: laurent@461: var.setname(new_name) lbessard@151: for resource in self.getresource(): lbessard@58: resource.updateElementName(old_name, new_name) lbessard@58: setattr(cls, "updateElementName", updateElementName) lbessard@58: laurent@461: def updateElementAddress(self, address_model, new_leading): laurent@461: for varlist in self.getglobalVars(): laurent@461: for var in varlist.getvariable(): laurent@461: var_address = var.getaddress() laurent@461: if var_address is not None: laurent@461: var.setaddress(update_address(var_address, address_model, new_leading)) laurent@461: for resource in self.getresource(): laurent@461: resource.updateElementAddress(address_model, new_leading) laurent@461: setattr(cls, "updateElementAddress", updateElementAddress) laurent@461: laurent@461: def removeVariableByAddress(self, address): laurent@461: for varlist in self.getglobalVars(): laurent@461: variables = varlist.getvariable() laurent@461: for i in xrange(len(variables)-1, -1, -1): laurent@461: if variables[i].getaddress() == address: laurent@461: variables.pop(i) laurent@461: setattr(cls, "removeVariableByAddress", removeVariableByAddress) laurent@461: laurent@461: def removeVariableByFilter(self, address_model): laurent@461: for varlist in self.getglobalVars(): laurent@461: variables = varlist.getvariable() laurent@461: for i in xrange(len(variables)-1, -1, -1): laurent@461: var_address = variables[i].getaddress() laurent@461: if var_address is not None: laurent@461: result = address_model.match(var_address) laurent@461: if result is not None: laurent@461: variables.pop(i) laurent@461: setattr(cls, "removeVariableByFilter", removeVariableByFilter) lbessard@58: lbessard@67: cls = PLCOpenClasses.get("configuration_resource", None) lbessard@67: if cls: lbessard@58: def updateElementName(self, old_name, new_name): laurent@461: for varlist in self.getglobalVars(): laurent@461: for var in varlist.getvariable(): laurent@461: var_address = var.getaddress() laurent@461: if var_address is not None: laurent@470: if var_address == old_name: laurent@461: var.setaddress(new_name) laurent@461: if var.getname() == old_name: laurent@461: var.setname(new_name) lbessard@187: for instance in self.getpouInstance(): lbessard@58: instance.updateElementName(old_name, new_name) lbessard@187: for task in self.gettask(): lbessard@58: task.updateElementName(old_name, new_name) lbessard@58: setattr(cls, "updateElementName", updateElementName) lbessard@58: laurent@461: def updateElementAddress(self, address_model, new_leading): laurent@461: for varlist in self.getglobalVars(): laurent@461: for var in varlist.getvariable(): laurent@461: var_address = var.getaddress() laurent@461: if var_address is not None: laurent@461: var.setaddress(update_address(var_address, address_model, new_leading)) laurent@461: for task in self.gettask(): laurent@461: task.updateElementAddress(address_model, new_leading) laurent@461: setattr(cls, "updateElementAddress", updateElementAddress) laurent@461: laurent@461: def removeVariableByAddress(self, address): laurent@461: for varlist in self.getglobalVars(): laurent@461: variables = varlist.getvariable() laurent@461: for i in xrange(len(variables)-1, -1, -1): laurent@461: if variables[i].getaddress() == address: laurent@461: variables.pop(i) laurent@461: setattr(cls, "removeVariableByAddress", removeVariableByAddress) laurent@461: laurent@461: def removeVariableByFilter(self, address_model): laurent@461: for varlist in self.getglobalVars(): laurent@461: variables = varlist.getvariable() laurent@461: for i in xrange(len(variables)-1, -1, -1): laurent@461: var_address = variables[i].getaddress() laurent@461: if var_address is not None: laurent@461: result = address_model.match(var_address) laurent@461: if result is not None: laurent@461: variables.pop(i) laurent@461: setattr(cls, "removeVariableByFilter", removeVariableByFilter) laurent@461: lbessard@67: cls = PLCOpenClasses.get("resource_task", None) lbessard@67: if cls: laurent@389: def compatibility(self, tree): laurent@389: if tree.hasAttribute("interval"): laurent@389: interval = GetAttributeValue(tree._attrs["interval"]) laurent@389: result = time_model.match(interval) laurent@389: if result is not None: laurent@389: values = result.groups() laurent@389: time_values = [int(v) for v in values[:2]] laurent@389: seconds = float(values[2]) laurent@389: time_values.extend([int(seconds), int((seconds % 1) * 1000000)]) laurent@389: text = "t#" laurent@389: if time_values[0] != 0: laurent@389: text += "%dh"%time_values[0] laurent@389: if time_values[1] != 0: laurent@389: text += "%dm"%time_values[1] laurent@389: if time_values[2] != 0: laurent@389: text += "%ds"%time_values[2] laurent@389: if time_values[3] != 0: laurent@389: if time_values[3] % 1000 != 0: laurent@389: text += "%.3fms"%(float(time_values[3]) / 1000) laurent@389: else: laurent@389: text += "%dms"%(time_values[3] / 1000) laurent@389: NodeSetAttr(tree, "interval", text) laurent@389: setattr(cls, "compatibility", compatibility) laurent@389: lbessard@58: def updateElementName(self, old_name, new_name): lbessard@58: if self.single == old_name: lbessard@58: self.single = new_name laurent@461: if self.interval == old_name: laurent@461: self.interval = new_name lbessard@187: for instance in self.getpouInstance(): lbessard@58: instance.updateElementName(old_name, new_name) lbessard@58: setattr(cls, "updateElementName", updateElementName) lbessard@58: laurent@461: def updateElementAddress(self, address_model, new_leading): laurent@461: if self.single is not None: laurent@461: self.single = update_address(self.single, address_model, new_leading) laurent@461: if self.interval is not None: laurent@461: self.interval = update_address(self.interval, address_model, new_leading) laurent@461: setattr(cls, "updateElementAddress", updateElementAddress) laurent@461: lbessard@67: cls = PLCOpenClasses.get("pouInstance", None) lbessard@67: if cls: laurent@389: def compatibility(self, tree): laurent@389: if tree.hasAttribute("type"): laurent@389: NodeRenameAttr(tree, "type", "typeName") laurent@389: setattr(cls, "compatibility", compatibility) laurent@389: lbessard@58: def updateElementName(self, old_name, new_name): b@417: if self.typeName == old_name: b@417: self.typeName = new_name lbessard@58: setattr(cls, "updateElementName", updateElementName) lbessard@58: lbessard@67: cls = PLCOpenClasses.get("project_types", None) lbessard@67: if cls: lbessard@151: def getdataTypeElements(self): lbessard@151: return self.dataTypes.getdataType() lbessard@151: setattr(cls, "getdataTypeElements", getdataTypeElements) lbessard@151: lbessard@151: def getdataTypeElement(self, name): lbessard@151: elements = self.dataTypes.getdataType() lbessard@125: for element in elements: lbessard@151: if element.getname() == name: lbessard@125: return element lbessard@125: return None lbessard@151: setattr(cls, "getdataTypeElement", getdataTypeElement) lbessard@151: lbessard@151: def appenddataTypeElement(self, name): lbessard@125: new_datatype = PLCOpenClasses["dataTypes_dataType"]() lbessard@151: new_datatype.setname(name) lbessard@151: new_datatype.baseType.setcontent({"name" : "BOOL", "value" : None}) lbessard@151: self.dataTypes.appenddataType(new_datatype) lbessard@151: setattr(cls, "appenddataTypeElement", appenddataTypeElement) lbessard@225: lbessard@151: def insertdataTypeElement(self, index, dataType): lbessard@151: self.dataTypes.insertdataType(index, dataType) lbessard@151: setattr(cls, "insertdataTypeElement", insertdataTypeElement) lbessard@151: lbessard@151: def removedataTypeElement(self, name): lbessard@125: found = False lbessard@151: for idx, element in enumerate(self.dataTypes.getdataType()): lbessard@151: if element.getname() == name: lbessard@151: self.dataTypes.removedataType(idx) lbessard@125: found = True lbessard@125: break lbessard@125: if not found: laurent@391: raise ValueError, _("\"%s\" Data Type doesn't exist !!!")%name lbessard@151: setattr(cls, "removedataTypeElement", removedataTypeElement) lbessard@151: lbessard@151: def getpouElements(self): lbessard@151: return self.pous.getpou() lbessard@151: setattr(cls, "getpouElements", getpouElements) lbessard@151: lbessard@151: def getpouElement(self, name): lbessard@151: elements = self.pous.getpou() lbessard@2: for element in elements: lbessard@151: if element.getname() == name: lbessard@2: return element lbessard@2: return None lbessard@151: setattr(cls, "getpouElement", getpouElement) lbessard@151: lbessard@151: def appendpouElement(self, name, pou_type, body_type): lbessard@151: for element in self.pous.getpou(): lbessard@151: if element.getname() == name: laurent@391: raise ValueError, _("\"%s\" POU already exists !!!")%name lbessard@2: new_pou = PLCOpenClasses["pous_pou"]() lbessard@151: new_pou.setname(name) lbessard@151: new_pou.setpouType(pou_type) laurent@389: new_pou.appendbody(PLCOpenClasses["body"]()) lbessard@151: new_pou.setbodyType(body_type) lbessard@151: self.pous.appendpou(new_pou) lbessard@151: setattr(cls, "appendpouElement", appendpouElement) lbessard@2: lbessard@151: def insertpouElement(self, index, pou): lbessard@151: self.pous.insertpou(index, pou) lbessard@151: setattr(cls, "insertpouElement", insertpouElement) lbessard@151: lbessard@151: def removepouElement(self, name): lbessard@2: found = False lbessard@151: for idx, element in enumerate(self.pous.getpou()): lbessard@151: if element.getname() == name: lbessard@151: self.pous.removepou(idx) lbessard@2: found = True lbessard@2: break lbessard@2: if not found: laurent@391: raise ValueError, _("\"%s\" POU doesn't exist !!!")%name lbessard@151: setattr(cls, "removepouElement", removepouElement) lbessard@151: lbessard@67: cls = PLCOpenClasses.get("pous_pou", None) lbessard@67: if cls: laurent@389: laurent@389: def setbodyType(self, type): laurent@389: if len(self.body) > 0: laurent@389: if type == "IL": laurent@389: self.body[0].setcontent({"name" : "IL", "value" : PLCOpenClasses["formattedText"]()}) laurent@389: elif type == "ST": laurent@389: self.body[0].setcontent({"name" : "ST", "value" : PLCOpenClasses["formattedText"]()}) laurent@389: elif type == "LD": laurent@389: self.body[0].setcontent({"name" : "LD", "value" : PLCOpenClasses["body_LD"]()}) laurent@389: elif type == "FBD": laurent@389: self.body[0].setcontent({"name" : "FBD", "value" : PLCOpenClasses["body_FBD"]()}) laurent@389: elif type == "SFC": laurent@389: self.body[0].setcontent({"name" : "SFC", "value" : PLCOpenClasses["body_SFC"]()}) laurent@389: else: laurent@389: raise ValueError, "%s isn't a valid body type!"%type lbessard@151: setattr(cls, "setbodyType", setbodyType) laurent@389: laurent@389: def getbodyType(self): laurent@389: if len(self.body) > 0: laurent@389: return self.body[0].getcontent()["name"] lbessard@151: setattr(cls, "getbodyType", getbodyType) laurent@389: laurent@389: def resetexecutionOrder(self): laurent@389: if len(self.body) > 0: laurent@389: self.body[0].resetexecutionOrder() lbessard@151: setattr(cls, "resetexecutionOrder", resetexecutionOrder) laurent@389: laurent@389: def compileexecutionOrder(self): laurent@389: if len(self.body) > 0: laurent@389: self.body[0].compileexecutionOrder() lbessard@151: setattr(cls, "compileexecutionOrder", compileexecutionOrder) laurent@389: laurent@389: def setelementExecutionOrder(self, instance, new_executionOrder): laurent@389: if len(self.body) > 0: laurent@389: self.body[0].setelementExecutionOrder(instance, new_executionOrder) lbessard@151: setattr(cls, "setelementExecutionOrder", setelementExecutionOrder) laurent@389: laurent@389: def addinstance(self, name, instance): laurent@389: if len(self.body) > 0: laurent@389: self.body[0].appendcontentInstance(name, instance) lbessard@151: setattr(cls, "addinstance", addinstance) laurent@389: laurent@389: def getinstances(self): laurent@389: if len(self.body) > 0: laurent@389: return self.body[0].getcontentInstances() laurent@389: return [] lbessard@151: setattr(cls, "getinstances", getinstances) laurent@389: laurent@389: def getinstance(self, id): laurent@389: if len(self.body) > 0: laurent@389: return self.body[0].getcontentInstance(id) laurent@389: return None lbessard@151: setattr(cls, "getinstance", getinstance) laurent@389: laurent@389: def getrandomInstance(self, exclude): laurent@389: if len(self.body) > 0: laurent@389: return self.body[0].getcontentRandomInstance(exclude) laurent@389: return None lbessard@151: setattr(cls, "getrandomInstance", getrandomInstance) laurent@389: laurent@389: def getinstanceByName(self, name): laurent@389: if len(self.body) > 0: laurent@389: return self.body[0].getcontentInstanceByName(name) laurent@389: return None lbessard@151: setattr(cls, "getinstanceByName", getinstanceByName) laurent@389: laurent@389: def removeinstance(self, id): laurent@389: if len(self.body) > 0: laurent@389: self.body[0].removecontentInstance(id) lbessard@151: setattr(cls, "removeinstance", removeinstance) laurent@389: laurent@389: def settext(self, text): laurent@389: if len(self.body) > 0: laurent@389: self.body[0].settext(text) lbessard@151: setattr(cls, "settext", settext) laurent@389: laurent@389: def gettext(self): laurent@389: if len(self.body) > 0: laurent@389: return self.body[0].gettext() laurent@389: return "" lbessard@151: setattr(cls, "gettext", gettext) lbessard@151: lbessard@151: def getvars(self): lbessard@2: vars = [] lbessard@282: if self.interface is not None: lbessard@282: reverse_types = {} lbessard@282: for name, value in VarTypes.items(): lbessard@282: reverse_types[value] = name lbessard@282: for varlist in self.interface.getcontent(): lbessard@282: vars.append((reverse_types[varlist["name"]], varlist["value"])) lbessard@2: return vars lbessard@151: setattr(cls, "getvars", getvars) lbessard@151: lbessard@151: def setvars(self, vars): lbessard@282: if self.interface is None: lbessard@282: self.interface = PLCOpenClasses["pou_interface"]() lbessard@151: self.interface.setcontent([]) lbessard@2: for vartype, varlist in vars: lbessard@151: self.interface.appendcontent({"name" : VarTypes[vartype], "value" : varlist}) lbessard@151: setattr(cls, "setvars", setvars) lbessard@151: laurent@435: def addpouVar(self, type, name, location="", description=""): lbessard@282: if self.interface is None: lbessard@282: self.interface = PLCOpenClasses["pou_interface"]() lbessard@151: content = self.interface.getcontent() lbessard@67: if len(content) == 0 or content[-1]["name"] != "localVars": lbessard@151: content.append({"name" : "localVars", "value" : PLCOpenClasses["interface_localVars"]()}) lbessard@68: else: lbessard@68: varlist = content[-1]["value"] lbessard@151: variables = varlist.getvariable() lbessard@151: if varlist.getconstant() or varlist.getretain() or len(variables) > 0 and variables[0].getaddress(): lbessard@151: content.append({"name" : "localVars", "value" : PLCOpenClasses["interface_localVars"]()}) lbessard@67: var = PLCOpenClasses["varListPlain_variable"]() lbessard@151: var.setname(name) lbessard@67: var_type = PLCOpenClasses["dataType"]() laurent@435: if type in [x for x,y in TypeHierarchy_list if not x.startswith("ANY")]: laurent@435: if type == "STRING": laurent@435: var_type.setcontent({"name" : "string", "value" : PLCOpenClasses["elementaryTypes_string"]()}) laurent@435: elif type == "WSTRING": laurent@435: var_type.setcontent({"name" : "wstring", "value" : PLCOpenClasses["elementaryTypes_wstring"]()}) laurent@435: else: laurent@435: var_type.setcontent({"name" : type, "value" : None}) laurent@435: else: laurent@435: derived_type = PLCOpenClasses["derivedTypes_derived"]() laurent@435: derived_type.setname(type) laurent@435: var_type.setcontent({"name" : "derived", "value" : derived_type}) lbessard@151: var.settype(var_type) laurent@435: if location != "": laurent@435: var.setaddress(location) laurent@435: if description != "": laurent@435: ft = PLCOpenClasses["formattedText"]() laurent@435: ft.settext(description) laurent@435: var.setdocumentation(ft) laurent@435: lbessard@151: content[-1]["value"].appendvariable(var) lbessard@151: setattr(cls, "addpouVar", addpouVar) lbessard@151: lbessard@151: def changepouVar(self, old_type, old_name, new_type, new_name): lbessard@282: if self.interface is not None: lbessard@282: content = self.interface.getcontent() lbessard@282: for varlist in content: lbessard@282: variables = varlist["value"].getvariable() lbessard@282: for var in variables: lbessard@282: if var.getname() == old_name: lbessard@282: vartype_content = var.gettype().getcontent() lbessard@282: if vartype_content["name"] == "derived" and vartype_content["value"].getname() == old_type: lbessard@282: var.setname(new_name) lbessard@282: vartype_content["value"].setname(new_type) lbessard@282: return lbessard@151: setattr(cls, "changepouVar", changepouVar) lbessard@151: lbessard@151: def removepouVar(self, type, name): lbessard@282: if self.interface is not None: lbessard@282: content = self.interface.getcontent() lbessard@282: for varlist in content: lbessard@282: variables = varlist["value"].getvariable() lbessard@282: for var in variables: lbessard@282: if var.getname() == name: lbessard@282: vartype_content = var.gettype().getcontent() lbessard@282: if vartype_content["name"] == "derived" and vartype_content["value"].getname() == type: lbessard@282: variables.remove(var) lbessard@282: break lbessard@282: if len(varlist["value"].getvariable()) == 0: lbessard@282: content.remove(varlist) lbessard@282: break lbessard@151: setattr(cls, "removepouVar", removepouVar) lbessard@151: lbessard@151: def hasblock(self, name): lbessard@151: if self.getbodyType() in ["FBD", "LD", "SFC"]: lbessard@151: for instance in self.getinstances(): lbessard@151: if isinstance(instance, PLCOpenClasses["fbdObjects_block"]) and instance.getinstanceName() == name: lbessard@68: return True lbessard@89: if self.transitions: lbessard@151: for transition in self.transitions.gettransition(): lbessard@151: result = transition.hasblock(name) lbessard@89: if result: lbessard@89: return result lbessard@89: if self.actions: lbessard@151: for action in self.actions.getaction(): lbessard@151: result = action.hasblock(name) lbessard@89: if result: lbessard@89: return result lbessard@68: return False lbessard@151: setattr(cls, "hasblock", hasblock) lbessard@151: lbessard@151: def addtransition(self, name, type): lbessard@2: if not self.transitions: lbessard@151: self.addtransitions() lbessard@151: self.transitions.settransition([]) lbessard@2: transition = PLCOpenClasses["transitions_transition"]() lbessard@151: transition.setname(name) lbessard@151: transition.setbodyType(type) lbessard@200: if type == "ST": lbessard@200: transition.settext(":= ;") lbessard@200: elif type == "IL": lbessard@200: transition.settext("\tST\t%s"%name) lbessard@151: self.transitions.appendtransition(transition) lbessard@151: setattr(cls, "addtransition", addtransition) lbessard@151: lbessard@151: def gettransition(self, name): lbessard@2: if self.transitions: lbessard@151: for transition in self.transitions.gettransition(): lbessard@151: if transition.getname() == name: lbessard@2: return transition lbessard@2: return None lbessard@151: setattr(cls, "gettransition", gettransition) lbessard@2: lbessard@151: def gettransitionList(self): lbessard@2: if self.transitions: lbessard@151: return self.transitions.gettransition() lbessard@2: return [] lbessard@151: setattr(cls, "gettransitionList", gettransitionList) lbessard@151: lbessard@151: def removetransition(self, name): lbessard@2: if self.transitions: lbessard@151: transitions = self.transitions.gettransition() lbessard@2: i = 0 lbessard@2: removed = False lbessard@2: while i < len(transitions) and not removed: lbessard@151: if transitions[i].getname() == name: lbessard@46: transitions.pop(i) lbessard@2: removed = True lbessard@2: i += 1 lbessard@2: if not removed: b@427: raise ValueError, _("Transition with name %s doesn't exist!")%name lbessard@151: setattr(cls, "removetransition", removetransition) lbessard@151: lbessard@151: def addaction(self, name, type): lbessard@2: if not self.actions: lbessard@151: self.addactions() lbessard@151: self.actions.setaction([]) lbessard@2: action = PLCOpenClasses["actions_action"]() lbessard@151: action.setname(name) lbessard@151: action.setbodyType(type) lbessard@151: self.actions.appendaction(action) lbessard@151: setattr(cls, "addaction", addaction) lbessard@151: lbessard@151: def getaction(self, name): lbessard@2: if self.actions: lbessard@151: for action in self.actions.getaction(): lbessard@151: if action.getname() == name: lbessard@2: return action lbessard@2: return None lbessard@151: setattr(cls, "getaction", getaction) lbessard@151: lbessard@151: def getactionList(self): lbessard@2: if self.actions: lbessard@151: return self.actions.getaction() lbessard@2: return [] lbessard@151: setattr(cls, "getactionList", getactionList) lbessard@2: lbessard@151: def removeaction(self, name): lbessard@2: if self.actions: lbessard@151: actions = self.actions.getaction() lbessard@2: i = 0 lbessard@2: removed = False lbessard@2: while i < len(actions) and not removed: lbessard@151: if actions[i].getname() == name: lbessard@46: actions.pop(i) lbessard@2: removed = True lbessard@2: i += 1 lbessard@2: if not removed: b@427: raise ValueError, _("Action with name %s doesn't exist!")%name lbessard@151: setattr(cls, "removeaction", removeaction) lbessard@2: lbessard@58: def updateElementName(self, old_name, new_name): greg@348: if self.interface: greg@348: for content in self.interface.getcontent(): greg@348: for var in content["value"].getvariable(): laurent@461: var_address = var.getaddress() laurent@461: if var_address is not None: laurent@470: if var_address == old_name: laurent@461: var.setaddress(new_name) laurent@461: if var.getname() == old_name: laurent@461: var.setname(new_name) greg@348: var_type_content = var.gettype().getcontent() greg@348: if var_type_content["name"] == "derived": greg@348: if var_type_content["value"].getname() == old_name: greg@348: var_type_content["value"].setname(new_name) laurent@394: self.body[0].updateElementName(old_name, new_name) lbessard@151: for action in self.getactionList(): lbessard@58: action.updateElementName(old_name, new_name) lbessard@151: for transition in self.gettransitionList(): lbessard@58: transition.updateElementName(old_name, new_name) lbessard@58: setattr(cls, "updateElementName", updateElementName) lbessard@58: laurent@461: def updateElementAddress(self, address_model, new_leading): laurent@461: if self.interface: laurent@461: for content in self.interface.getcontent(): laurent@461: for var in content["value"].getvariable(): laurent@461: var_address = var.getaddress() laurent@461: if var_address is not None: laurent@461: var.setaddress(update_address(var_address, address_model, new_leading)) laurent@461: self.body[0].updateElementAddress(address_model, new_leading) laurent@461: for action in self.getactionList(): laurent@461: action.updateElementAddress(address_model, new_leading) laurent@461: for transition in self.gettransitionList(): laurent@461: transition.updateElementAddress(address_model, new_leading) laurent@461: setattr(cls, "updateElementAddress", updateElementAddress) laurent@461: laurent@461: def removeVariableByAddress(self, address): laurent@461: if self.interface: laurent@461: for content in self.interface.getcontent(): laurent@461: variables = content["value"].getvariable() laurent@461: for i in xrange(len(variables)-1, -1, -1): laurent@461: if variables[i].getaddress() == address: laurent@461: variables.pop(i) laurent@461: setattr(cls, "removeVariableByAddress", removeVariableByAddress) laurent@461: laurent@461: def removeVariableByFilter(self, address_model): laurent@461: if self.interface: laurent@461: for content in self.interface.getcontent(): laurent@461: variables = content["value"].getvariable() laurent@461: for i in xrange(len(variables)-1, -1, -1): laurent@461: var_address = variables[i].getaddress() laurent@461: if var_address is not None: laurent@461: result = address_model.match(var_address) laurent@461: if result is not None: laurent@461: variables.pop(i) laurent@461: setattr(cls, "removeVariableByFilter", removeVariableByFilter) laurent@461: laurent@389: def setbodyType(self, type): laurent@389: if type == "IL": laurent@389: self.body.setcontent({"name" : "IL", "value" : PLCOpenClasses["formattedText"]()}) laurent@389: elif type == "ST": laurent@389: self.body.setcontent({"name" : "ST", "value" : PLCOpenClasses["formattedText"]()}) laurent@389: elif type == "LD": laurent@389: self.body.setcontent({"name" : "LD", "value" : PLCOpenClasses["body_LD"]()}) laurent@389: elif type == "FBD": laurent@389: self.body.setcontent({"name" : "FBD", "value" : PLCOpenClasses["body_FBD"]()}) laurent@389: elif type == "SFC": laurent@389: self.body.setcontent({"name" : "SFC", "value" : PLCOpenClasses["body_SFC"]()}) laurent@389: else: laurent@389: raise ValueError, "%s isn't a valid body type!"%type laurent@389: laurent@389: def getbodyType(self): laurent@389: return self.body.getcontent()["name"] laurent@389: laurent@389: def resetexecutionOrder(self): laurent@389: self.body.resetexecutionOrder() laurent@389: laurent@389: def compileexecutionOrder(self): laurent@389: self.body.compileexecutionOrder() laurent@389: laurent@389: def setelementExecutionOrder(self, instance, new_executionOrder): laurent@389: self.body.setelementExecutionOrder(instance, new_executionOrder) laurent@389: laurent@389: def addinstance(self, name, instance): laurent@389: self.body.appendcontentInstance(name, instance) laurent@389: laurent@389: def getinstances(self): laurent@389: return self.body.getcontentInstances() laurent@389: laurent@389: def getinstance(self, id): laurent@389: return self.body.getcontentInstance(id) laurent@389: laurent@389: def getrandomInstance(self, exclude): laurent@389: return self.body.getcontentRandomInstance(exclude) laurent@389: laurent@389: def getinstanceByName(self, name): laurent@389: return self.body.getcontentInstanceByName(name) laurent@389: laurent@389: def removeinstance(self, id): laurent@389: self.body.removecontentInstance(id) laurent@389: laurent@389: def settext(self, text): laurent@389: self.body.settext(text) laurent@389: laurent@389: def gettext(self): laurent@389: return self.body.gettext() laurent@389: lbessard@67: cls = PLCOpenClasses.get("transitions_transition", None) lbessard@67: if cls: lbessard@151: setattr(cls, "setbodyType", setbodyType) lbessard@151: setattr(cls, "getbodyType", getbodyType) lbessard@151: setattr(cls, "resetexecutionOrder", resetexecutionOrder) lbessard@151: setattr(cls, "compileexecutionOrder", compileexecutionOrder) lbessard@151: setattr(cls, "setelementExecutionOrder", setelementExecutionOrder) lbessard@151: setattr(cls, "addinstance", addinstance) lbessard@151: setattr(cls, "getinstances", getinstances) lbessard@151: setattr(cls, "getinstance", getinstance) lbessard@151: setattr(cls, "getrandomInstance", getrandomInstance) lbessard@151: setattr(cls, "getinstanceByName", getinstanceByName) lbessard@151: setattr(cls, "removeinstance", removeinstance) lbessard@151: setattr(cls, "settext", settext) lbessard@151: setattr(cls, "gettext", gettext) lbessard@2: lbessard@58: def updateElementName(self, old_name, new_name): lbessard@58: self.body.updateElementName(old_name, new_name) lbessard@58: setattr(cls, "updateElementName", updateElementName) lbessard@58: laurent@461: def updateElementAddress(self, address_model, new_leading): laurent@461: self.body.updateElementAddress(address_model, new_leading) laurent@461: setattr(cls, "updateElementAddress", updateElementAddress) laurent@461: lbessard@151: def hasblock(self, name): lbessard@151: if self.getbodyType() in ["FBD", "LD", "SFC"]: lbessard@151: for instance in self.getinstances(): lbessard@151: if isinstance(instance, PLCOpenClasses["fbdObjects_block"]) and instance.getinstanceName() == name: lbessard@68: return True lbessard@68: return False lbessard@151: setattr(cls, "hasblock", hasblock) lbessard@68: lbessard@67: cls = PLCOpenClasses.get("actions_action", None) lbessard@67: if cls: lbessard@151: setattr(cls, "setbodyType", setbodyType) lbessard@151: setattr(cls, "getbodyType", getbodyType) lbessard@151: setattr(cls, "resetexecutionOrder", resetexecutionOrder) lbessard@151: setattr(cls, "compileexecutionOrder", compileexecutionOrder) lbessard@151: setattr(cls, "setelementExecutionOrder", setelementExecutionOrder) lbessard@151: setattr(cls, "addinstance", addinstance) lbessard@151: setattr(cls, "getinstances", getinstances) lbessard@151: setattr(cls, "getinstance", getinstance) lbessard@151: setattr(cls, "getrandomInstance", getrandomInstance) lbessard@151: setattr(cls, "getinstanceByName", getinstanceByName) lbessard@151: setattr(cls, "removeinstance", removeinstance) lbessard@151: setattr(cls, "settext", settext) lbessard@151: setattr(cls, "gettext", gettext) lbessard@2: lbessard@58: def updateElementName(self, old_name, new_name): lbessard@58: self.body.updateElementName(old_name, new_name) lbessard@58: setattr(cls, "updateElementName", updateElementName) lbessard@58: laurent@461: def updateElementAddress(self, address_model, new_leading): laurent@461: self.body.updateElementAddress(address_model, new_leading) laurent@461: setattr(cls, "updateElementAddress", updateElementAddress) laurent@461: lbessard@151: def hasblock(self, name): lbessard@151: if self.getbodyType() in ["FBD", "LD", "SFC"]: lbessard@151: for instance in self.getinstances(): lbessard@151: if isinstance(instance, PLCOpenClasses["fbdObjects_block"]) and instance.getinstanceName() == name: lbessard@68: return True lbessard@68: return False lbessard@151: setattr(cls, "hasblock", hasblock) lbessard@68: lbessard@67: cls = PLCOpenClasses.get("body", None) lbessard@67: if cls: lbessard@118: cls.currentExecutionOrderId = 0 lbessard@118: lbessard@151: def resetcurrentExecutionOrderId(self): lbessard@200: object.__setattr__(self, "currentExecutionOrderId", 0) lbessard@151: setattr(cls, "resetcurrentExecutionOrderId", resetcurrentExecutionOrderId) lbessard@151: lbessard@151: def getnewExecutionOrderId(self): lbessard@200: object.__setattr__(self, "currentExecutionOrderId", self.currentExecutionOrderId + 1) lbessard@118: return self.currentExecutionOrderId lbessard@151: setattr(cls, "getnewExecutionOrderId", getnewExecutionOrderId) lbessard@151: lbessard@151: def resetexecutionOrder(self): lbessard@118: if self.content["name"] == "FBD": lbessard@151: for element in self.content["value"].getcontent(): lbessard@151: if not isinstance(element["value"], (PLCOpenClasses.get("commonObjects_comment", None), lbessard@151: PLCOpenClasses.get("commonObjects_connector", None), lbessard@151: PLCOpenClasses.get("commonObjects_continuation", None))): lbessard@151: element["value"].setexecutionOrderId(0) lbessard@118: else: laurent@391: raise TypeError, _("Can only generate execution order on FBD networks!") lbessard@151: setattr(cls, "resetexecutionOrder", resetexecutionOrder) lbessard@151: lbessard@151: def compileexecutionOrder(self): lbessard@118: if self.content["name"] == "FBD": lbessard@151: self.resetexecutionOrder() lbessard@151: self.resetcurrentExecutionOrderId() lbessard@151: for element in self.content["value"].getcontent(): lbessard@200: if isinstance(element["value"], PLCOpenClasses.get("fbdObjects_outVariable", None)) and element["value"].getexecutionOrderId() == 0: lbessard@151: connections = element["value"].connectionPointIn.getconnections() lbessard@118: if connections and len(connections) == 1: lbessard@151: self.compileelementExecutionOrder(connections[0]) lbessard@151: element["value"].setexecutionOrderId(self.getnewExecutionOrderId()) lbessard@118: else: laurent@391: raise TypeError, _("Can only generate execution order on FBD networks!") lbessard@151: setattr(cls, "compileexecutionOrder", compileexecutionOrder) lbessard@151: lbessard@151: def compileelementExecutionOrder(self, link): lbessard@118: if self.content["name"] == "FBD": lbessard@151: localid = link.getrefLocalId() lbessard@151: instance = self.getcontentInstance(localid) lbessard@151: if isinstance(instance, PLCOpenClasses.get("fbdObjects_block", None)) and instance.getexecutionOrderId() == 0: lbessard@151: for variable in instance.inputVariables.getvariable(): lbessard@151: connections = variable.connectionPointIn.getconnections() lbessard@118: if connections and len(connections) == 1: lbessard@151: self.compileelementExecutionOrder(connections[0]) lbessard@151: instance.setexecutionOrderId(self.getnewExecutionOrderId()) lbessard@151: elif isinstance(instance, PLCOpenClasses.get("commonObjects_continuation", None)) and instance.getexecutionOrderId() == 0: lbessard@151: name = instance.getname() lbessard@151: for tmp_instance in self.getcontentInstances(): lbessard@151: if isinstance(tmp_instance, PLCOpenClasses.get("commonObjects_connector", None)) and tmp_instance.getname() == name and tmp_instance.getexecutionOrderId() == 0: lbessard@151: connections = tmp_instance.connectionPointIn.getconnections() lbessard@118: if connections and len(connections) == 1: lbessard@151: self.compileelementExecutionOrder(connections[0]) lbessard@118: else: laurent@391: raise TypeError, _("Can only generate execution order on FBD networks!") lbessard@151: setattr(cls, "compileelementExecutionOrder", compileelementExecutionOrder) lbessard@151: lbessard@151: def setelementExecutionOrder(self, instance, new_executionOrder): lbessard@118: if self.content["name"] == "FBD": lbessard@151: old_executionOrder = instance.getexecutionOrderId() lbessard@118: if old_executionOrder is not None and old_executionOrder != 0 and new_executionOrder != 0: lbessard@151: for element in self.content["value"].getcontent(): lbessard@151: if element["value"] != instance and not isinstance(element["value"], PLCOpenClasses.get("commonObjects_comment", None)): lbessard@151: element_executionOrder = element["value"].getexecutionOrderId() lbessard@118: if old_executionOrder <= element_executionOrder <= new_executionOrder: lbessard@151: element["value"].setexecutionOrderId(element_executionOrder - 1) lbessard@118: if new_executionOrder <= element_executionOrder <= old_executionOrder: lbessard@151: element["value"].setexecutionOrderId(element_executionOrder + 1) lbessard@151: instance.setexecutionOrderId(new_executionOrder) lbessard@118: else: laurent@391: raise TypeError, _("Can only generate execution order on FBD networks!") lbessard@151: setattr(cls, "setelementExecutionOrder", setelementExecutionOrder) lbessard@151: lbessard@151: def appendcontentInstance(self, name, instance): lbessard@2: if self.content["name"] in ["LD","FBD","SFC"]: lbessard@151: self.content["value"].appendcontent({"name" : name, "value" : instance}) lbessard@2: else: laurent@391: raise TypeError, _("%s body don't have instances!")%self.content["name"] lbessard@151: setattr(cls, "appendcontentInstance", appendcontentInstance) lbessard@151: lbessard@151: def getcontentInstances(self): lbessard@2: if self.content["name"] in ["LD","FBD","SFC"]: lbessard@2: instances = [] lbessard@151: for element in self.content["value"].getcontent(): lbessard@2: instances.append(element["value"]) lbessard@2: return instances lbessard@2: else: laurent@391: raise TypeError, _("%s body don't have instances!")%self.content["name"] lbessard@151: setattr(cls, "getcontentInstances", getcontentInstances) lbessard@151: lbessard@151: def getcontentInstance(self, id): lbessard@2: if self.content["name"] in ["LD","FBD","SFC"]: lbessard@151: for element in self.content["value"].getcontent(): lbessard@151: if element["value"].getlocalId() == id: lbessard@2: return element["value"] etisserant@0: return None lbessard@2: else: laurent@391: raise TypeError, _("%s body don't have instances!")%self.content["name"] lbessard@151: setattr(cls, "getcontentInstance", getcontentInstance) lbessard@151: lbessard@151: def getcontentRandomInstance(self, exclude): lbessard@2: if self.content["name"] in ["LD","FBD","SFC"]: lbessard@151: for element in self.content["value"].getcontent(): lbessard@151: if element["value"].getlocalId() not in exclude: lbessard@2: return element["value"] lbessard@2: return None lbessard@2: else: laurent@391: raise TypeError, _("%s body don't have instances!")%self.content["name"] lbessard@151: setattr(cls, "getcontentRandomInstance", getcontentRandomInstance) lbessard@151: lbessard@151: def getcontentInstanceByName(self, name): lbessard@2: if self.content["name"] in ["LD","FBD","SFC"]: lbessard@151: for element in self.content["value"].getcontent(): lbessard@193: if isinstance(element["value"], PLCOpenClasses.get("fbdObjects_block", None)) and element["value"].getinstanceName() == name: lbessard@2: return element["value"] lbessard@2: else: laurent@391: raise TypeError, _("%s body don't have instances!")%self.content["name"] lbessard@151: setattr(cls, "getcontentInstanceByName", getcontentInstanceByName) lbessard@151: lbessard@151: def removecontentInstance(self, id): lbessard@2: if self.content["name"] in ["LD","FBD","SFC"]: lbessard@2: i = 0 lbessard@2: removed = False lbessard@151: elements = self.content["value"].getcontent() lbessard@2: while i < len(elements) and not removed: lbessard@151: if elements[i]["value"].getlocalId() == id: lbessard@151: self.content["value"].removecontent(i) lbessard@2: removed = True lbessard@2: i += 1 lbessard@2: if not removed: b@427: raise ValueError, _("Instance with id %d doesn't exist!")%id lbessard@2: else: lbessard@2: raise TypeError, "%s body don't have instances!"%self.content["name"] lbessard@151: setattr(cls, "removecontentInstance", removecontentInstance) lbessard@151: lbessard@151: def settext(self, text): lbessard@2: if self.content["name"] in ["IL","ST"]: lbessard@151: self.content["value"].settext(text) lbessard@2: else: laurent@391: raise TypeError, _("%s body don't have text!")%self.content["name"] lbessard@151: setattr(cls, "settext", settext) lbessard@151: lbessard@151: def gettext(self): lbessard@2: if self.content["name"] in ["IL","ST"]: lbessard@151: return self.content["value"].gettext() lbessard@2: else: laurent@391: raise TypeError, _("%s body don't have text!")%self.content["name"] lbessard@151: setattr(cls, "gettext", gettext) lbessard@58: lbessard@58: def updateElementName(self, old_name, new_name): lbessard@58: if self.content["name"] in ["IL", "ST"]: lbessard@58: self.content["value"].updateElementName(old_name, new_name) lbessard@58: else: lbessard@151: for element in self.content["value"].getcontent(): lbessard@58: element["value"].updateElementName(old_name, new_name) lbessard@58: setattr(cls, "updateElementName", updateElementName) lbessard@2: laurent@461: def updateElementAddress(self, address_model, new_leading): laurent@461: if self.content["name"] in ["IL", "ST"]: laurent@461: self.content["value"].updateElementAddress(address_model, new_leading) laurent@461: else: laurent@461: for element in self.content["value"].getcontent(): laurent@461: element["value"].updateElementAddress(address_model, new_leading) laurent@461: setattr(cls, "updateElementAddress", updateElementAddress) laurent@461: lbessard@151: def getx(self): lbessard@151: return self.position.getx() lbessard@151: lbessard@151: def gety(self): lbessard@151: return self.position.gety() lbessard@151: lbessard@151: def setx(self, x): lbessard@151: self.position.setx(x) lbessard@151: lbessard@151: def sety(self, y): lbessard@151: self.position.sety(y) lbessard@151: laurent@384: def _getBoundingBox(self): laurent@384: return rect(self.getx(), self.gety(), self.getwidth(), self.getheight()) laurent@384: laurent@384: def _getConnectionsBoundingBox(connectionPointIn): laurent@384: bbox = rect() laurent@384: connections = connectionPointIn.getconnections() laurent@384: if connections is not None: laurent@384: for connection in connections: laurent@384: for x, y in connection.getpoints(): laurent@384: bbox.update(x, y) laurent@384: return bbox laurent@384: laurent@384: def _getBoundingBoxSingle(self): laurent@384: bbox = _getBoundingBox(self) laurent@384: if self.connectionPointIn is not None: laurent@384: bbox.union(_getConnectionsBoundingBox(self.connectionPointIn)) laurent@384: return bbox laurent@384: laurent@384: def _getBoundingBoxMultiple(self): laurent@384: bbox = _getBoundingBox(self) laurent@384: for connectionPointIn in self.getconnectionPointIn(): laurent@384: bbox.union(_getConnectionsBoundingBox(connectionPointIn)) laurent@384: return bbox laurent@384: laurent@384: def _filterConnections(connectionPointIn, localId, connections): laurent@384: in_connections = connectionPointIn.getconnections() laurent@384: if in_connections is not None: laurent@384: to_delete = [] laurent@384: for i, connection in enumerate(in_connections): laurent@384: connected = connection.getrefLocalId() laurent@384: if not connections.has_key((localId, connected)) and \ laurent@384: not connections.has_key((connected, localId)): laurent@384: to_delete.append(i) laurent@384: to_delete.reverse() laurent@384: for i in to_delete: laurent@384: connectionPointIn.removeconnection(i) laurent@384: laurent@384: def _filterConnectionsSingle(self, connections): laurent@384: if self.connectionPointIn is not None: laurent@384: _filterConnections(self.connectionPointIn, self.localId, connections) laurent@384: laurent@384: def _filterConnectionsMultiple(self, connections): laurent@384: for connectionPointIn in self.getconnectionPointIn(): laurent@384: _filterConnections(connectionPointIn, self.localId, connections) laurent@384: laurent@384: def _getconnectionsdefinition(instance, connections_end): laurent@384: id = instance.getlocalId() laurent@384: return dict([((id, end), True) for end in connections_end]) laurent@384: laurent@384: def _updateConnectionsId(connectionPointIn, translation): laurent@384: connections_end = [] laurent@384: connections = connectionPointIn.getconnections() laurent@384: if connections is not None: laurent@384: for connection in connections: laurent@384: refLocalId = connection.getrefLocalId() laurent@384: new_reflocalId = translation.get(refLocalId, refLocalId) laurent@384: connection.setrefLocalId(new_reflocalId) laurent@384: connections_end.append(new_reflocalId) laurent@384: return connections_end laurent@384: laurent@384: def _updateConnectionsIdSingle(self, translation): laurent@384: connections_end = [] laurent@384: if self.connectionPointIn is not None: laurent@384: connections_end = _updateConnectionsId(self.connectionPointIn, translation) laurent@384: return _getconnectionsdefinition(self, connections_end) laurent@384: laurent@384: def _updateConnectionsIdMultiple(self, translation): laurent@384: connections_end = [] laurent@384: for connectionPointIn in self.getconnectionPointIn(): laurent@384: connections_end.extend(_updateConnectionsId(connectionPointIn, translation)) laurent@384: return _getconnectionsdefinition(self, connections_end) laurent@384: laurent@384: def _translate(self, dx, dy): laurent@384: self.setx(self.getx() + dx) laurent@384: self.sety(self.gety() + dy) laurent@384: laurent@384: def _translateConnections(connectionPointIn, dx, dy): laurent@384: connections = connectionPointIn.getconnections() laurent@384: if connections is not None: laurent@384: for connection in connections: laurent@384: for position in connection.getposition(): laurent@384: position.setx(position.getx() + dx) laurent@384: position.sety(position.gety() + dy) laurent@384: laurent@384: def _translateSingle(self, dx, dy): laurent@384: _translate(self, dx, dy) laurent@384: if self.connectionPointIn is not None: laurent@384: _translateConnections(self.connectionPointIn, dx, dy) laurent@384: laurent@384: def _translateMultiple(self, dx, dy): laurent@384: _translate(self, dx, dy) laurent@384: for connectionPointIn in self.getconnectionPointIn(): laurent@384: _translateConnections(connectionPointIn, dx, dy) laurent@384: laurent@383: def _updateElementName(self, old_name, new_name): laurent@383: pass laurent@383: laurent@461: def _updateElementAddress(self, address_model, new_leading): laurent@461: pass laurent@461: laurent@384: _connectionsFunctions = { laurent@384: "bbox": {"none": _getBoundingBox, laurent@384: "single": _getBoundingBoxSingle, laurent@384: "multiple": _getBoundingBoxMultiple}, laurent@384: "translate": {"none": _translate, laurent@384: "single": _translateSingle, laurent@384: "multiple": _translateMultiple}, laurent@384: "filter": {"none": lambda self, connections: None, laurent@384: "single": _filterConnectionsSingle, laurent@384: "multiple": _filterConnectionsMultiple}, laurent@387: "update": {"none": lambda self, translation: {}, laurent@384: "single": _updateConnectionsIdSingle, laurent@384: "multiple": _updateConnectionsIdMultiple} laurent@384: } laurent@384: laurent@383: def _initElementClass(name, classname, connectionPointInType="none"): laurent@384: ElementNameToClass[name] = classname laurent@383: cls = PLCOpenClasses.get(classname, None) laurent@383: if cls: laurent@383: setattr(cls, "getx", getx) laurent@383: setattr(cls, "gety", gety) laurent@383: setattr(cls, "setx", setx) laurent@383: setattr(cls, "sety", sety) laurent@383: setattr(cls, "updateElementName", _updateElementName) laurent@461: setattr(cls, "updateElementAddress", _updateElementAddress) laurent@384: setattr(cls, "getBoundingBox", _connectionsFunctions["bbox"][connectionPointInType]) laurent@384: setattr(cls, "translate", _connectionsFunctions["translate"][connectionPointInType]) laurent@384: setattr(cls, "filterConnections", _connectionsFunctions["filter"][connectionPointInType]) laurent@384: setattr(cls, "updateConnectionsId", _connectionsFunctions["update"][connectionPointInType]) laurent@383: return cls laurent@383: laurent@383: def _getexecutionOrder(instance, specific_values): laurent@383: executionOrder = instance.getexecutionOrderId() laurent@383: if executionOrder is None: laurent@383: executionOrder = 0 laurent@383: specific_values["executionOrder"] = executionOrder laurent@383: laurent@383: def _getdefaultmodifiers(instance, infos): laurent@383: infos["negated"] = instance.getnegated() laurent@383: infos["edge"] = instance.getedge() laurent@383: laurent@383: def _getinputmodifiers(instance, infos): laurent@383: infos["negated"] = instance.getnegatedIn() laurent@383: infos["edge"] = instance.getedgeIn() laurent@383: laurent@383: def _getoutputmodifiers(instance, infos): laurent@383: infos["negated"] = instance.getnegatedOut() laurent@383: infos["edge"] = instance.getedgeOut() laurent@383: laurent@383: MODIFIERS_FUNCTIONS = {"default": _getdefaultmodifiers, laurent@383: "input": _getinputmodifiers, laurent@383: "output": _getoutputmodifiers} laurent@383: laurent@383: def _getconnectioninfos(instance, connection, links=False, modifiers=None, parameter=False): laurent@383: infos = {"position": connection.getrelPositionXY()} laurent@383: if parameter: laurent@383: infos["name"] = instance.getformalParameter() laurent@383: MODIFIERS_FUNCTIONS.get(modifiers, lambda x, y: None)(instance, infos) laurent@383: if links: laurent@383: infos["links"] = [] laurent@383: connections = connection.getconnections() laurent@383: if connections is not None: laurent@383: for link in connections: laurent@383: dic = {"refLocalId": link.getrefLocalId(), laurent@383: "points": link.getpoints(), laurent@383: "formalParameter": link.getformalParameter()} laurent@383: infos["links"].append(dic) laurent@383: return infos laurent@383: laurent@383: def _getelementinfos(instance): laurent@383: return {"id": instance.getlocalId(), laurent@383: "x": instance.getx(), laurent@383: "y": instance.gety(), laurent@383: "height": instance.getheight(), laurent@383: "width": instance.getwidth(), laurent@383: "specific_values": {}, laurent@383: "inputs": [], laurent@383: "outputs": []} laurent@383: laurent@383: def _getvariableinfosFunction(type, input, output): laurent@383: def getvariableinfos(self): laurent@383: infos = _getelementinfos(self) laurent@383: infos["type"] = type laurent@383: specific_values = infos["specific_values"] laurent@383: specific_values["name"] = self.getexpression() laurent@383: _getexecutionOrder(self, specific_values) laurent@383: if input and output: laurent@383: infos["inputs"].append(_getconnectioninfos(self, self.connectionPointIn, True, "input")) laurent@383: infos["outputs"].append(_getconnectioninfos(self, self.connectionPointOut, False, "output")) laurent@383: elif input: laurent@383: infos["inputs"].append(_getconnectioninfos(self, self.connectionPointIn, True, "default")) laurent@383: elif output: laurent@383: infos["outputs"].append(_getconnectioninfos(self, self.connectionPointOut, False, "default")) laurent@383: return infos laurent@383: return getvariableinfos laurent@383: laurent@383: def _getconnectorinfosFunction(type): laurent@383: def getvariableinfos(self): laurent@383: infos = _getelementinfos(self) laurent@383: infos["type"] = type laurent@383: infos["specific_values"]["name"] = self.getname() laurent@383: if type == "connector": laurent@383: infos["inputs"].append(_getconnectioninfos(self, self.connectionPointIn, True)) laurent@383: elif type == "continuation": laurent@383: infos["outputs"].append(_getconnectioninfos(self, self.connectionPointOut)) laurent@383: return infos laurent@383: return getvariableinfos laurent@383: laurent@383: def _getpowerrailinfosFunction(type): laurent@383: def getpowerrailinfos(self): laurent@383: infos = _getelementinfos(self) laurent@383: infos["type"] = type laurent@383: if type == "rightPowerRail": laurent@383: for connectionPointIn in self.getconnectionPointIn(): laurent@383: infos["inputs"].append(_getconnectioninfos(self, connectionPointIn, True)) laurent@383: infos["specific_values"]["connectors"] = [True for input in infos["inputs"]] laurent@383: elif type == "leftPowerRail": laurent@383: for connectionPointOut in self.getconnectionPointOut(): laurent@383: infos["outputs"].append(_getconnectioninfos(self, connectionPointOut)) laurent@383: infos["specific_values"]["connectors"] = [True for output in infos["outputs"]] laurent@383: return infos laurent@383: return getpowerrailinfos laurent@383: laurent@383: def _getldelementinfosFunction(type): laurent@383: def getldelementinfos(self): laurent@383: infos = _getelementinfos(self) laurent@383: infos["type"] = type laurent@383: specific_values = infos["specific_values"] laurent@383: specific_values["name"] = self.getvariable() laurent@383: _getexecutionOrder(self, specific_values) laurent@383: specific_values["negated"] = self.getnegated() laurent@383: specific_values["edge"] = self.getedge() laurent@383: if type == "coil": laurent@383: specific_values["storage"] = self.getstorage() laurent@383: infos["inputs"].append(_getconnectioninfos(self, self.connectionPointIn, True)) laurent@383: infos["outputs"].append(_getconnectioninfos(self, self.connectionPointOut)) laurent@383: return infos laurent@383: return getldelementinfos laurent@383: laurent@383: DIVERGENCE_TYPES = {(True, True): "simultaneousDivergence", laurent@383: (True, False): "selectionDivergence", laurent@383: (False, True): "simultaneousConvergence", laurent@383: (False, False): "selectionConvergence"} laurent@383: laurent@383: def _getdivergenceinfosFunction(divergence, simultaneous): laurent@383: def getdivergenceinfos(self): laurent@383: infos = _getelementinfos(self) laurent@383: infos["type"] = DIVERGENCE_TYPES[(divergence, simultaneous)] laurent@383: if divergence: laurent@383: infos["inputs"].append(_getconnectioninfos(self, self.connectionPointIn, True)) laurent@383: for connectionPointOut in self.getconnectionPointOut(): laurent@383: infos["outputs"].append(_getconnectioninfos(self, connectionPointOut)) laurent@383: infos["specific_values"]["connectors"] = len(infos["outputs"]) laurent@383: else: laurent@383: for connectionPointIn in self.getconnectionPointIn(): laurent@383: infos["inputs"].append(_getconnectioninfos(self, connectionPointIn, True)) laurent@383: infos["outputs"].append(_getconnectioninfos(self, self.connectionPointOut)) laurent@383: infos["specific_values"]["connectors"] = len(infos["inputs"]) laurent@383: return infos laurent@383: return getdivergenceinfos laurent@383: laurent@383: cls = _initElementClass("comment", "commonObjects_comment") laurent@383: if cls: laurent@383: def getinfos(self): laurent@383: infos = _getelementinfos(self) laurent@383: infos["type"] = "comment" laurent@383: infos["specific_values"]["content"] = self.getcontentText() laurent@383: return infos laurent@383: setattr(cls, "getinfos", getinfos) laurent@383: lbessard@151: def setcontentText(self, text): lbessard@151: self.content.settext(text) lbessard@151: setattr(cls, "setcontentText", setcontentText) etisserant@0: lbessard@151: def getcontentText(self): lbessard@151: return self.content.gettext() lbessard@151: setattr(cls, "getcontentText", getcontentText) lbessard@58: lbessard@58: def updateElementName(self, old_name, new_name): lbessard@58: self.content.updateElementName(old_name, new_name) lbessard@58: setattr(cls, "updateElementName", updateElementName) lbessard@2: laurent@461: def updateElementAddress(self, address_model, new_leading): laurent@461: self.content.updateElementAddress(address_model, new_leading) laurent@461: setattr(cls, "updateElementAddress", updateElementAddress) laurent@461: laurent@383: cls = _initElementClass("block", "fbdObjects_block") laurent@383: if cls: laurent@383: def getBoundingBox(self): laurent@383: bbox = _getBoundingBox(self) laurent@383: for input in self.inputVariables.getvariable(): laurent@383: bbox.union(_getConnectionsBoundingBox(input.connectionPointIn)) laurent@383: return bbox laurent@383: setattr(cls, "getBoundingBox", getBoundingBox) laurent@383: laurent@383: def getinfos(self): laurent@383: infos = _getelementinfos(self) laurent@383: infos["type"] = self.gettypeName() laurent@383: specific_values = infos["specific_values"] laurent@383: specific_values["name"] = self.getinstanceName() laurent@383: _getexecutionOrder(self, specific_values) laurent@383: for variable in self.inputVariables.getvariable(): laurent@383: infos["inputs"].append(_getconnectioninfos(variable, variable.connectionPointIn, True, "default", True)) laurent@383: for variable in self.outputVariables.getvariable(): laurent@383: infos["outputs"].append(_getconnectioninfos(variable, variable.connectionPointOut, False, "default", True)) laurent@383: return infos laurent@383: setattr(cls, "getinfos", getinfos) lbessard@2: lbessard@58: def updateElementName(self, old_name, new_name): lbessard@58: if self.typeName == old_name: lbessard@58: self.typeName = new_name lbessard@58: setattr(cls, "updateElementName", updateElementName) lbessard@58: laurent@384: def filterConnections(self, connections): laurent@384: for input in self.inputVariables.getvariable(): laurent@384: _filterConnections(input.connectionPointIn, self.localId, connections) laurent@384: setattr(cls, "filterConnections", filterConnections) laurent@384: laurent@384: def updateConnectionsId(self, translation): laurent@384: connections_end = [] laurent@384: for input in self.inputVariables.getvariable(): laurent@384: connections_end.extend(_updateConnectionsId(input.connectionPointIn, translation)) laurent@384: return _getconnectionsdefinition(self, connections_end) laurent@384: setattr(cls, "updateConnectionsId", updateConnectionsId) laurent@384: laurent@384: def translate(self, dx, dy): laurent@384: _translate(self, dx, dy) laurent@384: for input in self.inputVariables.getvariable(): laurent@384: _translateConnections(input.connectionPointIn, dx, dy) laurent@384: setattr(cls, "translate", translate) laurent@384: laurent@383: cls = _initElementClass("leftPowerRail", "ldObjects_leftPowerRail") laurent@383: if cls: laurent@383: setattr(cls, "getinfos", _getpowerrailinfosFunction("leftPowerRail")) laurent@383: laurent@383: cls = _initElementClass("rightPowerRail", "ldObjects_rightPowerRail", "multiple") laurent@383: if cls: laurent@383: setattr(cls, "getinfos", _getpowerrailinfosFunction("rightPowerRail")) laurent@383: laurent@383: cls = _initElementClass("contact", "ldObjects_contact", "single") laurent@383: if cls: laurent@383: setattr(cls, "getinfos", _getldelementinfosFunction("contact")) laurent@383: lbessard@58: def updateElementName(self, old_name, new_name): lbessard@58: if self.variable == old_name: lbessard@58: self.variable = new_name lbessard@58: setattr(cls, "updateElementName", updateElementName) laurent@383: laurent@461: def updateElementAddress(self, address_model, new_leading): laurent@461: self.variable = update_address(self.variable, address_model, new_leading) laurent@461: setattr(cls, "updateElementAddress", updateElementAddress) laurent@461: laurent@383: cls = _initElementClass("coil", "ldObjects_coil", "single") laurent@383: if cls: laurent@383: setattr(cls, "getinfos", _getldelementinfosFunction("coil")) laurent@383: lbessard@58: def updateElementName(self, old_name, new_name): lbessard@58: if self.variable == old_name: lbessard@58: self.variable = new_name lbessard@58: setattr(cls, "updateElementName", updateElementName) lbessard@58: laurent@461: def updateElementAddress(self, address_model, new_leading): laurent@461: self.variable = update_address(self.variable, address_model, new_leading) laurent@461: setattr(cls, "updateElementAddress", updateElementAddress) laurent@461: laurent@383: cls = _initElementClass("step", "sfcObjects_step", "single") laurent@383: if cls: laurent@383: def getinfos(self): laurent@383: infos = _getelementinfos(self) laurent@383: infos["type"] = "step" laurent@383: specific_values = infos["specific_values"] laurent@383: specific_values["name"] = self.getname() laurent@383: specific_values["initial"] = self.getinitialStep() laurent@383: if self.connectionPointIn: laurent@383: infos["inputs"].append(_getconnectioninfos(self, self.connectionPointIn, True)) laurent@383: if self.connectionPointOut: laurent@383: infos["outputs"].append(_getconnectioninfos(self, self.connectionPointOut)) laurent@383: if self.connectionPointOutAction: laurent@383: specific_values["action"] = _getconnectioninfos(self, self.connectionPointOutAction) laurent@383: return infos laurent@383: setattr(cls, "getinfos", getinfos) laurent@383: laurent@389: cls = PLCOpenClasses.get("transition_condition", None) laurent@389: if cls: laurent@389: def compatibility(self, tree): laurent@389: connections = [] laurent@389: for child in tree.childNodes: laurent@389: if child.nodeName == "connection": laurent@389: connections.append(child) laurent@389: if len(connections) > 0: laurent@389: node = CreateNode("connectionPointIn") laurent@389: relPosition = CreateNode("relPosition") laurent@389: NodeSetAttr(relPosition, "x", "0") laurent@389: NodeSetAttr(relPosition, "y", "0") laurent@389: node.childNodes.append(relPosition) laurent@389: node.childNodes.extend(connections) laurent@389: tree.childNodes = [node] laurent@389: setattr(cls, "compatibility", compatibility) laurent@389: laurent@383: cls = _initElementClass("transition", "sfcObjects_transition", "single") laurent@383: if cls: laurent@383: def getinfos(self): laurent@383: infos = _getelementinfos(self) laurent@383: infos["type"] = "transition" laurent@383: specific_values = infos["specific_values"] laurent@383: priority = self.getpriority() laurent@383: if priority is None: laurent@383: priority = 0 laurent@383: specific_values["priority"] = priority laurent@383: condition = self.getconditionContent() laurent@383: specific_values["condition_type"] = condition["type"] laurent@383: if specific_values["condition_type"] == "connection": laurent@389: specific_values["connection"] = _getconnectioninfos(self, condition["value"], True) laurent@383: else: laurent@383: specific_values["condition"] = condition["value"] laurent@383: infos["inputs"].append(_getconnectioninfos(self, self.connectionPointIn, True)) laurent@383: infos["outputs"].append(_getconnectioninfos(self, self.connectionPointOut)) laurent@383: return infos laurent@383: setattr(cls, "getinfos", getinfos) lbessard@151: lbessard@151: def setconditionContent(self, type, value): lbessard@2: if not self.condition: lbessard@151: self.addcondition() lbessard@2: if type == "reference": lbessard@2: condition = PLCOpenClasses["condition_reference"]() lbessard@151: condition.setname(value) lbessard@2: elif type == "inline": lbessard@2: condition = PLCOpenClasses["condition_inline"]() lbessard@151: condition.setcontent({"name" : "ST", "value" : PLCOpenClasses["formattedText"]()}) lbessard@151: condition.settext(value) lbessard@69: elif type == "connection": laurent@389: type = "connectionPointIn" laurent@389: condition = PLCOpenClasses["connectionPointIn"]() lbessard@151: self.condition.setcontent({"name" : type, "value" : condition}) lbessard@151: setattr(cls, "setconditionContent", setconditionContent) etisserant@0: lbessard@151: def getconditionContent(self): lbessard@2: if self.condition: lbessard@151: content = self.condition.getcontent() lbessard@2: values = {"type" : content["name"]} lbessard@2: if values["type"] == "reference": lbessard@151: values["value"] = content["value"].getname() lbessard@2: elif values["type"] == "inline": lbessard@151: values["value"] = content["value"].gettext() laurent@389: elif values["type"] == "connectionPointIn": laurent@389: values["type"] = "connection" laurent@389: values["value"] = content["value"] lbessard@2: return values lbessard@2: return "" lbessard@151: setattr(cls, "getconditionContent", getconditionContent) lbessard@2: lbessard@58: def updateElementName(self, old_name, new_name): lbessard@58: if self.condition: lbessard@151: content = self.condition.getcontent() lbessard@58: if content["name"] == "reference": lbessard@151: if content["value"].getname() == old_name: lbessard@151: content["value"].setname(new_name) lbessard@58: elif content["name"] == "inline": lbessard@58: content["value"].updateElementName(old_name, new_name) lbessard@58: setattr(cls, "updateElementName", updateElementName) lbessard@58: laurent@461: def updateElementAddress(self, address_model, new_leading): laurent@461: if self.condition: laurent@461: content = self.condition.getcontent() laurent@461: if content["name"] == "reference": laurent@461: content["value"].setname(update_address(content["value"].getname(), address_model, new_leading)) laurent@461: elif content["name"] == "inline": laurent@461: content["value"].updateElementAddress(address_model, new_leading) laurent@461: setattr(cls, "updateElementAddress", updateElementAddress) laurent@461: lbessard@151: def getconnections(self): lbessard@63: if self.condition: lbessard@151: content = self.condition.getcontent() laurent@389: if content["name"] == "connectionPointIn": laurent@389: return content["value"].getconnections() lbessard@151: setattr(cls, "getconnections", getconnections) laurent@383: laurent@383: cls = _initElementClass("selectionDivergence", "sfcObjects_selectionDivergence", "single") laurent@383: if cls: laurent@383: setattr(cls, "getinfos", _getdivergenceinfosFunction(True, False)) laurent@383: laurent@383: cls = _initElementClass("selectionConvergence", "sfcObjects_selectionConvergence", "multiple") laurent@383: if cls: laurent@383: setattr(cls, "getinfos", _getdivergenceinfosFunction(False, False)) laurent@383: laurent@383: cls = _initElementClass("simultaneousDivergence", "sfcObjects_simultaneousDivergence", "single") laurent@383: if cls: laurent@383: setattr(cls, "getinfos", _getdivergenceinfosFunction(True, True)) laurent@383: laurent@383: cls = _initElementClass("simultaneousConvergence", "sfcObjects_simultaneousConvergence", "multiple") laurent@383: if cls: laurent@383: setattr(cls, "getinfos", _getdivergenceinfosFunction(False, True)) laurent@383: laurent@383: cls = _initElementClass("jumpStep", "sfcObjects_jumpStep", "single") laurent@383: if cls: laurent@383: def getinfos(self): laurent@383: infos = _getelementinfos(self) laurent@383: infos["type"] = "jump" laurent@383: infos["specific_values"]["target"] = self.gettargetName() laurent@383: infos["inputs"].append(_getconnectioninfos(self, self.connectionPointIn, True)) laurent@383: return infos laurent@383: setattr(cls, "getinfos", getinfos) lbessard@58: lbessard@67: cls = PLCOpenClasses.get("actionBlock_action", None) lbessard@67: if cls: laurent@389: def compatibility(self, tree): laurent@389: relPosition = reduce(lambda x, y: x | (y.nodeName == "relPosition"), tree.childNodes, False) laurent@389: if not tree.hasAttribute("localId"): laurent@389: NodeSetAttr(tree, "localId", "0") laurent@389: if not relPosition: laurent@389: node = CreateNode("relPosition") laurent@389: NodeSetAttr(node, "x", "0") laurent@389: NodeSetAttr(node, "y", "0") laurent@389: tree.childNodes.insert(0, node) laurent@389: setattr(cls, "compatibility", compatibility) laurent@389: lbessard@151: def setreferenceName(self, name): lbessard@151: if self.reference: lbessard@151: self.reference.setname(name) lbessard@151: setattr(cls, "setreferenceName", setreferenceName) lbessard@151: lbessard@151: def getreferenceName(self): lbessard@151: if self.reference: lbessard@151: return self.reference.getname() lbessard@2: return None lbessard@151: setattr(cls, "getreferenceName", getreferenceName) lbessard@151: lbessard@151: def setinlineContent(self, content): lbessard@151: if self.inline: lbessard@151: self.inline.setcontent({"name" : "ST", "value" : PLCOpenClasses["formattedText"]()}) lbessard@151: self.inline.settext(content) lbessard@151: setattr(cls, "setinlineContent", setinlineContent) lbessard@151: lbessard@151: def getinlineContent(self): lbessard@151: if self.inline: lbessard@151: return self.inline.gettext() lbessard@2: return None lbessard@151: setattr(cls, "getinlineContent", getinlineContent) lbessard@151: lbessard@151: def updateElementName(self, old_name, new_name): lbessard@151: if self.reference and self.reference.getname() == old_name: lbessard@151: self.reference.setname(new_name) lbessard@58: if self.inline: lbessard@58: self.inline.updateElementName(old_name, new_name) lbessard@58: setattr(cls, "updateElementName", updateElementName) lbessard@58: laurent@461: def updateElementAddress(self, address_model, new_leading): laurent@461: if self.reference: laurent@461: self.reference.setname(update_address(self.reference.getname(), address_model, new_leading)) laurent@461: if self.inline: laurent@461: self.inline.updateElementAddress(address_model, new_leading) laurent@461: setattr(cls, "updateElementAddress", updateElementAddress) laurent@461: laurent@383: cls = _initElementClass("actionBlock", "commonObjects_actionBlock", "single") laurent@383: if cls: laurent@383: def compatibility(self, tree): laurent@383: for child in tree.childNodes[:]: laurent@383: if child.nodeName == "connectionPointOut": laurent@383: tree.childNodes.remove(child) laurent@383: setattr(cls, "compatibility", compatibility) laurent@383: laurent@383: def getinfos(self): laurent@383: infos = _getelementinfos(self) laurent@383: infos["type"] = "actionBlock" laurent@383: infos["specific_values"]["actions"] = self.getactions() laurent@383: infos["inputs"].append(_getconnectioninfos(self, self.connectionPointIn, True)) laurent@383: return infos laurent@383: setattr(cls, "getinfos", getinfos) laurent@383: lbessard@151: def setactions(self, actions): lbessard@2: self.action = [] lbessard@2: for params in actions: lbessard@2: action = PLCOpenClasses["actionBlock_action"]() lbessard@151: action.setqualifier(params["qualifier"]) lbessard@2: if params["type"] == "reference": lbessard@151: action.addreference() lbessard@151: action.setreferenceName(params["value"]) etisserant@0: else: lbessard@151: action.addinline() lbessard@151: action.setinlineContent(params["value"]) laurent@379: if params.has_key("duration"): lbessard@151: action.setduration(params["duration"]) laurent@379: if params.has_key("indicator"): lbessard@151: action.setindicator(params["indicator"]) lbessard@2: self.action.append(action) lbessard@151: setattr(cls, "setactions", setactions) lbessard@151: lbessard@151: def getactions(self): lbessard@2: actions = [] lbessard@2: for action in self.action: lbessard@2: params = {} lbessard@151: params["qualifier"] = action.getqualifier() lbessard@108: if params["qualifier"] is None: lbessard@108: params["qualifier"] = "N" lbessard@151: if action.getreference(): lbessard@2: params["type"] = "reference" lbessard@151: params["value"] = action.getreferenceName() lbessard@151: elif action.getinline(): lbessard@2: params["type"] = "inline" lbessard@151: params["value"] = action.getinlineContent() lbessard@151: duration = action.getduration() lbessard@2: if duration: lbessard@2: params["duration"] = duration lbessard@151: indicator = action.getindicator() lbessard@2: if indicator: lbessard@2: params["indicator"] = indicator lbessard@2: actions.append(params) lbessard@2: return actions lbessard@151: setattr(cls, "getactions", getactions) lbessard@2: lbessard@58: def updateElementName(self, old_name, new_name): lbessard@58: for action in self.action: lbessard@58: action.updateElementName(old_name, new_name) lbessard@58: setattr(cls, "updateElementName", updateElementName) lbessard@58: laurent@461: def updateElementAddress(self, address_model, new_leading): laurent@461: for action in self.action: laurent@461: action.updateElementAddress(address_model, new_leading) laurent@461: setattr(cls, "updateElementAddress", updateElementAddress) laurent@461: laurent@383: cls = _initElementClass("inVariable", "fbdObjects_inVariable") laurent@383: if cls: laurent@383: setattr(cls, "getinfos", _getvariableinfosFunction("input", False, True)) lbessard@58: lbessard@58: def updateElementName(self, old_name, new_name): lbessard@58: if self.expression == old_name: lbessard@58: self.expression = new_name lbessard@58: setattr(cls, "updateElementName", updateElementName) lbessard@2: laurent@461: def updateElementAddress(self, address_model, new_leading): laurent@461: self.expression = update_address(self.expression, address_model, new_leading) laurent@461: setattr(cls, "updateElementAddress", updateElementAddress) laurent@461: laurent@383: cls = _initElementClass("outVariable", "fbdObjects_outVariable", "single") laurent@383: if cls: laurent@383: setattr(cls, "getinfos", _getvariableinfosFunction("output", True, False)) laurent@383: lbessard@58: def updateElementName(self, old_name, new_name): lbessard@58: if self.expression == old_name: lbessard@58: self.expression = new_name lbessard@58: setattr(cls, "updateElementName", updateElementName) lbessard@58: laurent@461: def updateElementAddress(self, address_model, new_leading): laurent@461: self.expression = update_address(self.expression, address_model, new_leading) laurent@461: setattr(cls, "updateElementAddress", updateElementAddress) laurent@461: laurent@383: cls = _initElementClass("inOutVariable", "fbdObjects_inOutVariable", "single") laurent@383: if cls: laurent@383: setattr(cls, "getinfos", _getvariableinfosFunction("inout", True, True)) laurent@383: lbessard@58: def updateElementName(self, old_name, new_name): lbessard@58: if self.expression == old_name: lbessard@58: self.expression = new_name lbessard@58: setattr(cls, "updateElementName", updateElementName) lbessard@58: laurent@461: def updateElementAddress(self, address_model, new_leading): laurent@461: self.expression = update_address(self.expression, address_model, new_leading) laurent@461: setattr(cls, "updateElementAddress", updateElementAddress) laurent@461: laurent@383: cls = _initElementClass("continuation", "commonObjects_continuation") laurent@383: if cls: laurent@383: setattr(cls, "getinfos", _getconnectorinfosFunction("continuation")) laurent@383: laurent@383: cls = _initElementClass("connector", "commonObjects_connector", "single") laurent@383: if cls: laurent@383: setattr(cls, "getinfos", _getconnectorinfosFunction("connector")) lbessard@2: lbessard@67: cls = PLCOpenClasses.get("connection", None) lbessard@67: if cls: lbessard@151: def setpoints(self, points): lbessard@2: self.position = [] lbessard@2: for point in points: lbessard@2: position = PLCOpenClasses["position"]() lbessard@151: position.setx(point.x) lbessard@151: position.sety(point.y) lbessard@2: self.position.append(position) lbessard@151: setattr(cls, "setpoints", setpoints) lbessard@151: lbessard@151: def getpoints(self): lbessard@2: points = [] lbessard@2: for position in self.position: lbessard@151: points.append((position.getx(),position.gety())) lbessard@2: return points lbessard@151: setattr(cls, "getpoints", getpoints) lbessard@2: lbessard@67: cls = PLCOpenClasses.get("connectionPointIn", None) lbessard@67: if cls: lbessard@151: def setrelPositionXY(self, x, y): lbessard@2: self.relPosition = PLCOpenClasses["position"]() lbessard@151: self.relPosition.setx(x) lbessard@151: self.relPosition.sety(y) lbessard@151: setattr(cls, "setrelPositionXY", setrelPositionXY) lbessard@151: lbessard@151: def getrelPositionXY(self): lbessard@2: if self.relPosition: lbessard@151: return self.relPosition.getx(), self.relPosition.gety() lbessard@2: else: etisserant@0: return self.relPosition lbessard@151: setattr(cls, "getrelPositionXY", getrelPositionXY) lbessard@151: lbessard@151: def addconnection(self): lbessard@2: if not self.content: lbessard@151: self.content = {"name" : "connection", "value" : [PLCOpenClasses["connection"]()]} lbessard@2: else: lbessard@2: self.content["value"].append(PLCOpenClasses["connection"]()) lbessard@151: setattr(cls, "addconnection", addconnection) lbessard@151: lbessard@151: def removeconnection(self, idx): lbessard@2: if self.content: lbessard@2: self.content["value"].pop(idx) lbessard@2: if len(self.content["value"]) == 0: lbessard@2: self.content = None lbessard@151: setattr(cls, "removeconnection", removeconnection) lbessard@151: lbessard@151: def removeconnections(self): lbessard@2: if self.content: lbessard@2: self.content = None lbessard@151: setattr(cls, "removeconnections", removeconnections) lbessard@151: lbessard@151: def getconnections(self): lbessard@2: if self.content: lbessard@2: return self.content["value"] lbessard@151: setattr(cls, "getconnections", getconnections) lbessard@151: lbessard@151: def setconnectionId(self, idx, id): lbessard@2: if self.content: lbessard@151: self.content["value"][idx].setrefLocalId(id) lbessard@151: setattr(cls, "setconnectionId", setconnectionId) lbessard@151: lbessard@151: def getconnectionId(self, idx): lbessard@2: if self.content: lbessard@151: return self.content["value"][idx].getrefLocalId() lbessard@2: return None lbessard@151: setattr(cls, "getconnectionId", getconnectionId) lbessard@151: lbessard@151: def setconnectionPoints(self, idx, points): lbessard@2: if self.content: lbessard@151: self.content["value"][idx].setpoints(points) lbessard@151: setattr(cls, "setconnectionPoints", setconnectionPoints) lbessard@151: lbessard@151: def getconnectionPoints(self, idx): lbessard@2: if self.content: lbessard@151: return self.content["value"][idx].getpoints() lbessard@2: return None lbessard@151: setattr(cls, "getconnectionPoints", getconnectionPoints) lbessard@151: lbessard@151: def setconnectionParameter(self, idx, parameter): lbessard@27: if self.content: lbessard@151: self.content["value"][idx].setformalParameter(parameter) lbessard@151: setattr(cls, "setconnectionParameter", setconnectionParameter) lbessard@151: lbessard@151: def getconnectionParameter(self, idx): lbessard@27: if self.content: lbessard@151: return self.content["value"][idx].getformalParameter() lbessard@27: return None lbessard@151: setattr(cls, "getconnectionParameter", getconnectionParameter) lbessard@27: lbessard@67: cls = PLCOpenClasses.get("connectionPointOut", None) lbessard@67: if cls: lbessard@151: def setrelPositionXY(self, x, y): lbessard@2: self.relPosition = PLCOpenClasses["position"]() lbessard@151: self.relPosition.setx(x) lbessard@151: self.relPosition.sety(y) lbessard@151: setattr(cls, "setrelPositionXY", setrelPositionXY) lbessard@151: lbessard@151: def getrelPositionXY(self): lbessard@2: if self.relPosition: lbessard@151: return self.relPosition.getx(), self.relPosition.gety() lbessard@2: return self.relPosition lbessard@151: setattr(cls, "getrelPositionXY", getrelPositionXY) lbessard@2: lbessard@67: cls = PLCOpenClasses.get("value", None) lbessard@67: if cls: lbessard@151: def setvalue(self, value): lbessard@125: if value.startswith("[") and value.endswith("]"): lbessard@2: arrayValue = PLCOpenClasses["value_arrayValue"]() lbessard@151: self.content = {"name" : "arrayValue", "value" : arrayValue} lbessard@125: elif value.startswith("(") and value.endswith(")"): lbessard@2: structValue = PLCOpenClasses["value_structValue"]() lbessard@151: self.content = {"name" : "structValue", "value" : structValue} lbessard@2: else: lbessard@2: simpleValue = PLCOpenClasses["value_simpleValue"]() lbessard@151: self.content = {"name" : "simpleValue", "value": simpleValue} lbessard@151: self.content["value"].setvalue(value) lbessard@151: setattr(cls, "setvalue", setvalue) lbessard@151: lbessard@151: def getvalue(self): lbessard@151: return self.content["value"].getvalue() lbessard@151: setattr(cls, "getvalue", getvalue) lbessard@2: lbessard@147: def extractValues(values): lbessard@145: items = values.split(",") lbessard@145: i = 1 lbessard@145: while i < len(items): lbessard@145: opened = items[i - 1].count("(") + items[i - 1].count("[") lbessard@145: closed = items[i - 1].count(")") + items[i - 1].count("]") lbessard@145: if opened > closed: lbessard@145: items[i - 1] = ','.join([items[i - 1], items.pop(i)]) lbessard@145: elif opened == closed: lbessard@145: i += 1 lbessard@145: else: laurent@391: raise ValueError, _("\"%s\" is an invalid value!")%value lbessard@145: return items lbessard@145: lbessard@67: cls = PLCOpenClasses.get("value_arrayValue", None) lbessard@67: if cls: lbessard@145: arrayValue_model = re.compile("([0-9]*)\((.*)\)$") lbessard@125: lbessard@151: def setvalue(self, value): lbessard@2: self.value = [] lbessard@145: for item in extractValues(value[1:-1]): lbessard@125: item = item.strip() lbessard@2: element = PLCOpenClasses["arrayValue_value"]() lbessard@125: result = arrayValue_model.match(item) lbessard@125: if result is not None: lbessard@125: groups = result.groups() laurent@389: element.setrepetitionValue(groups[0]) lbessard@151: element.setvalue(groups[1].strip()) lbessard@125: else: lbessard@151: element.setvalue(item) lbessard@2: self.value.append(element) lbessard@151: setattr(cls, "setvalue", setvalue) lbessard@151: lbessard@151: def getvalue(self): lbessard@125: values = [] lbessard@125: for element in self.value: lbessard@151: repetition = element.getrepetitionValue() lbessard@125: if repetition is not None and repetition > 1: laurent@389: values.append("%s(%s)"%(repetition, element.getvalue())) lbessard@125: else: lbessard@151: values.append(element.getvalue()) lbessard@125: return "[%s]"%", ".join(values) lbessard@151: setattr(cls, "getvalue", getvalue) lbessard@2: lbessard@67: cls = PLCOpenClasses.get("value_structValue", None) lbessard@67: if cls: lbessard@125: structValue_model = re.compile("(.*):=(.*)") lbessard@125: lbessard@151: def setvalue(self, value): lbessard@2: self.value = [] lbessard@145: for item in extractValues(value[1:-1]): lbessard@295: result = structValue_model.match(item) lbessard@125: if result is not None: lbessard@125: groups = result.groups() lbessard@125: element = PLCOpenClasses["structValue_value"]() lbessard@151: element.setmember(groups[0].strip()) lbessard@151: element.setvalue(groups[1].strip()) lbessard@295: self.value.append(element) lbessard@151: setattr(cls, "setvalue", setvalue) lbessard@151: lbessard@151: def getvalue(self): lbessard@125: values = [] lbessard@2: for element in self.value: lbessard@151: values.append("%s := %s"%(element.getmember(), element.getvalue())) lbessard@125: return "(%s)"%", ".join(values) lbessard@151: setattr(cls, "getvalue", getvalue)