plcopen/plcopen.py
author lbessard
Mon, 10 Sep 2007 18:16:07 +0200
changeset 90 2245e8776086
parent 89 a6ff2b3fcc25
child 94 e7f5a251f251
permissions -rw-r--r--
Adding support support for using PLCOpenEditor with Beremiz
Modify code for wxPython 2.8.4
#!/usr/bin/env python
# -*- coding: utf-8 -*-

#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
#based on the plcopen standard. 
#
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
#
#See COPYING file for copyrights details.
#
#This library is free software; you can redistribute it and/or
#modify it under the terms of the GNU General Public
#License as published by the Free Software Foundation; either
#version 2.1 of the License, or (at your option) any later version.
#
#This library is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#General Public License for more details.
#
#You should have received a copy of the GNU General Public
#License along with this library; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

from xmlclass import *
from types import *
import os

"""
Dictionary that makes the relation between var names in plcopen and displayed values
"""
VarTypes = {"Local" : "localVars", "Temp" : "tempVars", "Input" : "inputVars",
            "Output" : "outputVars", "InOut" : "inOutVars", "External" : "externalVars",
            "Global" : "globalVars", "Access" : "accessVars"}

"""
Define in which order var types must be displayed
"""
VarOrder = ["Local","Temp","Input","Output","InOut","External","Global","Access"]

"""
Define which action qualifier must be associated with a duration 
"""
QualifierList = {"N" : False, "R" : False, "S" : False, "L" : True, "D" : True, 
    "P" : False, "P0" : False, "P1" : False, "SD" : True, "DS" : True, "SL" : True}

PLCOpenClasses, PLCOpenTypes = GenerateClassesFromXSD(os.path.join(os.path.split(__file__)[0], "TC6_XML_V10_B.xsd"))

cls = PLCOpenClasses.get("dataType", None)
if cls:
    cls.value = None
    
    def getValue(self):
        return self.value
    setattr(cls, "getValue", getValue)
    
    def setValue(self, value):
        self.value = value
    setattr(cls, "setValue", setValue)
    
    def loadXMLTree(self, tree):
        node = tree.childNodes[1]
        if node.nodeName == "derived":
            self.value = PLCOpenClasses["derived"]()
            self.value.loadXMLTree(node)
        else:
            self.value = node.nodeName
    setattr(cls, "loadXMLTree", loadXMLTree)
    
    def generateXMLText(self, name, indent, extras = {}):
        ind1, ind2 = getIndent(indent, name)
        text = ind1 + "<%s>\n"%name
        if isinstance(self.value, (StringType, UnicodeType)):
            ind3, ind4 = getIndent(indent + 1, self.value)
            text += ind3 + "<%s/>\n"%self.value
        else:
            text += self.value.generateXMLText("derived", indent + 1)
        text += ind1 + "</%s>\n"%name
        return text
    setattr(cls, "generateXMLText", generateXMLText)

cls = PLCOpenClasses.get("formattedText", None)
if cls:
    cls.text = ""
    
    def getText(self):
        return self.text
    setattr(cls, "getText", getText)
    
    def setText(self, text):
        self.text = text
    setattr(cls, "setText", setText)
    
    def loadXMLTree(self, tree):
        self.text = GetAttributeValue(tree)
        if len(self.text.splitlines()) > 1:
            self.text = self.text[1:].rstrip()
    setattr(cls, "loadXMLTree", loadXMLTree)
    
    def generateXMLText(self, name, indent, extras = {}):
        ind1, ind2 = getIndent(indent, name)
        if len(self.text.splitlines()) > 1:
            text = ind1 + "<%s>\n<![CDATA[\n"%name
            text += "%s\n"%self.text
            text += "]]>\n" + ind1 + "</%s>\n"%name
            return text
        else:
            return ind1 + "<%s><![CDATA[%s]]></%s>\n"%(name, self.text, name)
    setattr(cls, "generateXMLText", generateXMLText)
    
    def updateElementName(self, old_name, new_name):
        index = self.text.find(old_name)
        while index != -1:
            if index > 0 and (self.text[index - 1].isalnum() or self.text[index - 1] == "_"):
                index = self.text.find(old_name, index + len(old_name))
            elif index < len(self.text) - len(old_name) and (self.text[index + len(old_name)].isalnum() or self.text[index + len(old_name)] == "_"):
                index = self.text.find(old_name, index + len(old_name))
            else:
                self.text = self.text[:index] + new_name + self.text[index + len(old_name):]
                index = self.text.find(old_name, index + len(new_name))
    setattr(cls, "updateElementName", updateElementName)
    
cls = PLCOpenClasses.get("project", None)
if cls:
    cls.singleLineAttributes = False
    
    def getFileHeader(self):
        fileheader = {}
        fileheader["companyName"] = self.fileHeader.getCompanyName()
        if self.fileHeader.getCompanyURL():
            fileheader["companyURL"] = self.fileHeader.getCompanyURL()
        fileheader["productName"] = self.fileHeader.getProductName()
        fileheader["productVersion"] = self.fileHeader.getProductVersion()
        if self.fileHeader.getProductRelease():
            fileheader["productRelease"] = self.fileHeader.getProductRelease()
        fileheader["creationDateTime"] = self.fileHeader.getCreationDateTime()
        if self.fileHeader.getContentDescription():
            fileheader["contentDescription"] = self.fileHeader.getContentDescription()
        return fileheader
    setattr(cls, "getFileHeader", getFileHeader)
    
    def setFileHeader(self, fileheader):
        self.fileHeader.setCompanyName(fileheader["companyName"])
        if "companyURL" in fileheader:
            self.fileHeader.setCompanyURL(fileheader["companyURL"])
        self.fileHeader.setProductName(fileheader["productName"])
        self.fileHeader.setProductVersion(fileheader["productVersion"])
        if "productRelease" in fileheader:
            self.fileHeader.setProductRelease(fileheader["productRelease"])
        self.fileHeader.setCreationDateTime(fileheader["creationDateTime"])
        if "contentDescription" in fileheader:
            self.fileHeader.setContentDescription(fileheader["contentDescription"])
    setattr(cls, "setFileHeader", setFileHeader)
    
    def setName(self, name):
        self.contentHeader.setName(name)
    setattr(cls, "setName", setName)
        
    def getName(self):
        return self.contentHeader.getName()
    setattr(cls, "getName", getName)
    
    def getPous(self):
        return self.types.getPouElements()
    setattr(cls, "getPous", getPous)
    
    def getPou(self, name):
        return self.types.getPouElement(name)
    setattr(cls, "getPou", getPou)
    
    def appendPou(self, name, pou_type, body_type):
        self.types.appendPouElement(name, pou_type, body_type)
    setattr(cls, "appendPou", appendPou)
        
    def insertPou(self, index, pou):
        self.types.insertPouElement(index, pou)
    setattr(cls, "insertPou", insertPou)
    
    def removePou(self, name):
        self.types.removePouElement(name)
    setattr(cls, "removePou", removePou)

    def getConfigurations(self):
        configurations = self.instances.configurations.getConfiguration()
        if configurations:
            return configurations
        return []
    setattr(cls, "getConfigurations", getConfigurations)

    def getConfiguration(self, name):
        for configuration in self.instances.configurations.getConfiguration():
            if configuration.getName() == name:
                return configuration
        return None
    setattr(cls, "getConfiguration", getConfiguration)

    def addConfiguration(self, name):
        for configuration in self.instances.configurations.getConfiguration():
            if configuration.getName() == name:
                raise ValueError, "\"%s\" configuration already exists !!!"%name
        new_configuration = PLCOpenClasses["configurations_configuration"]()
        new_configuration.setName(name)
        self.instances.configurations.appendConfiguration(new_configuration)
    setattr(cls, "addConfiguration", addConfiguration)    

    def removeConfiguration(self, name):
        found = False
        for idx, configuration in enumerate(self.instances.configurations.getConfiguration()):
            if configuration.getName() == name:
                self.instances.configurations.removeConfiguration(idx)
                found = True
                break
        if not found:
            raise ValueError, "\"%s\" configuration doesn't exist !!!"%name
    setattr(cls, "removeConfiguration", removeConfiguration)

    def getConfigurationResource(self, config_name, name):
        configuration = self.getConfiguration(config_name)
        if configuration:
            for resource in configuration.getResource():
                if resource.getName() == name:
                    return resource
        return None
    setattr(cls, "getConfigurationResource", getConfigurationResource)

    def addConfigurationResource(self, config_name, name):
        configuration = self.getConfiguration(config_name)
        if configuration:
            for resource in configuration.getResource():
                if resource.getName() == name:
                    raise ValueError, "\"%s\" resource already exists in \"%s\" configuration !!!"%(name, config_name)
            new_resource = PLCOpenClasses["configuration_resource"]()
            new_resource.setName(name)
            configuration.appendResource(new_resource)
    setattr(cls, "addConfigurationResource", addConfigurationResource)    

    def removeConfigurationResource(self, config_name, name):
        configuration = self.getConfiguration(config_name)
        if configuration:
            found = False
            for idx, resource in enumerate(configuration.getResource()):
                if resource.getName() == name:
                    configuration.removeResource(idx)
                    found = True
                    break
            if not found:
                raise ValueError, "\"%s\" resource doesn't exist in \"%s\" configuration !!!"%(name, config_name)
    setattr(cls, "removeConfigurationResource", removeConfigurationResource)

    def updateElementName(self, old_name, new_name):
        for pou in self.types.getPouElements():
            pou.updateElementName(old_name, new_name)
        for configuration in self.instances.configurations.getConfiguration():
            configuration.updateElementName(old_name, new_name)
    setattr(cls, "updateElementName", updateElementName)

cls = PLCOpenClasses.get("project_fileHeader", None)
if cls:
    cls.singleLineAttributes = False

cls = PLCOpenClasses.get("configurations_configuration", None)
if cls:
    def updateElementName(self, old_name, new_name):
        for resource in self.getResource():
            resource.updateElementName(old_name, new_name)
    setattr(cls, "updateElementName", updateElementName)


cls = PLCOpenClasses.get("configuration_resource", None)
if cls:
    def updateElementName(self, old_name, new_name):
        for instance in self.getPouInstance():
            instance.updateElementName(old_name, new_name)
        for task in self.getTask():
            task.updateElementName(old_name, new_name)
    setattr(cls, "updateElementName", updateElementName)

cls = PLCOpenClasses.get("resource_task", None)
if cls:
    def updateElementName(self, old_name, new_name):
        if self.single == old_name:
            self.single = new_name
        for instance in self.getPouInstance():
            instance.updateElementName(old_name, new_name)
    setattr(cls, "updateElementName", updateElementName)

cls = PLCOpenClasses.get("pouInstance", None)
if cls:
    def updateElementName(self, old_name, new_name):
        if self.type == old_name:
            self.type = new_name
    setattr(cls, "updateElementName", updateElementName)

cls = PLCOpenClasses.get("project_types", None)
if cls:
    def getPouElements(self):
        return self.pous.getPou()
    setattr(cls, "getPouElements", getPouElements)
    
    def getPouElement(self, name):
        elements = self.pous.getPou()
        for element in elements:
            if element.getName() == name:
                return element
        return None
    setattr(cls, "getPouElement", getPouElement)

    def appendPouElement(self, name, pou_type, body_type):
        for element in self.pous.getPou():
            if element.getName() == name:
                raise ValueError, "\"%s\" POU already exists !!!"%name
        new_pou = PLCOpenClasses["pous_pou"]()
        new_pou.setName(name)
        new_pou.pouType.setValue(pou_type)
        new_pou.setBody(PLCOpenClasses["body"]())
        new_pou.setBodyType(body_type)
        self.pous.appendPou(new_pou)
    setattr(cls, "appendPouElement", appendPouElement)
        
    def insertPouElement(self, index, pou):
        self.pous.insertPou(index, pou)
    setattr(cls, "insertPouElement", insertPouElement)
    
    def removePouElement(self, name):
        found = False
        for idx, element in enumerate(self.pous.getPou()):
            if element.getName() == name:
                self.pous.removePou(idx)
                found = True
                break
        if not found:
            raise ValueError, "\"%s\" POU doesn't exist !!!"%name
    setattr(cls, "removePouElement", removePouElement)

def setBodyType(self, type):
    if type == "IL":
        self.body.setContent("IL", PLCOpenClasses["formattedText"]())
    elif type == "ST":
        self.body.setContent("ST", PLCOpenClasses["formattedText"]())
    elif type == "LD":
        self.body.setContent("LD", PLCOpenClasses["body_LD"]())
    elif type == "FBD":
        self.body.setContent("FBD", PLCOpenClasses["body_FBD"]())
    elif type == "SFC":
        self.body.setContent("SFC", PLCOpenClasses["body_SFC"]())
    else:
        raise ValueError, "%s isn't a valid body type!"%type
        
def getBodyType(self):
    return self.body.getContent()["name"]

def addInstance(self, name, instance):
    self.body.appendContentInstance(name, instance)

def getInstances(self):
    return self.body.getContentInstances()

def getInstance(self, id):
    return self.body.getContentInstance(id)

def getRandomInstance(self, exclude):
    return self.body.getContentRandomInstance(exclude)

def getInstanceByName(self, name):
    return self.body.getContentInstanceByName(name)

def removeInstance(self, id):
    self.body.removeContentInstance(id)

def setText(self, text):
    self.body.setText(text)

def getText(self):
    return self.body.getText()
setattr(cls, "getText", getText)

cls = PLCOpenClasses.get("pous_pou", None)
if cls:
    setattr(cls, "setBodyType", setBodyType)
    setattr(cls, "getBodyType", getBodyType)
    setattr(cls, "addInstance", addInstance)
    setattr(cls, "getInstances", getInstances)
    setattr(cls, "getInstance", getInstance)
    setattr(cls, "getRandomInstance", getRandomInstance)
    setattr(cls, "getInstanceByName", getInstanceByName)
    setattr(cls, "removeInstance", removeInstance)
    setattr(cls, "setText", setText)
    setattr(cls, "getText", getText)

    def getVars(self):
        vars = []
        reverse_types = {}
        for name, value in VarTypes.items():
            reverse_types[value] = name
        for varlist in self.interface.getContent():
            vars.append((reverse_types[varlist["name"]], varlist["value"]))
        return vars
    setattr(cls, "getVars", getVars)
    
    def setVars(self, vars):
        self.interface.setContent([])
        for vartype, varlist in vars:
            self.interface.appendContent(VarTypes[vartype], varlist)
    setattr(cls, "setVars", setVars)
    
    def addPouVar(self, type, name):
        content = self.interface.getContent()
        if len(content) == 0 or content[-1]["name"] != "localVars":
            content.append({"name" : "localVars", "value" : PLCOpenClasses["varList"]()})
        else:
            varlist = content[-1]["value"]
            variables = varlist.getVariable()
            if varlist.getConstant() or varlist.getRetain() or len(variables) > 0 and variables[0].getAddress():
                content.append({"name" : "localVars", "value" : PLCOpenClasses["varList"]()})
        var = PLCOpenClasses["varListPlain_variable"]()
        var.setName(name)
        var_type = PLCOpenClasses["dataType"]()
        derived_type = PLCOpenClasses["derived"]()
        derived_type.setName(type)
        var_type.setValue(derived_type)
        var.setType(var_type)
        content[-1]["value"].appendVariable(var)
    setattr(cls, "addPouVar", addPouVar)
    
    def removePouVar(self, type, name):
        content = self.interface.getContent()
        for varlist in content:
            variables = varlist["value"].getVariable()
            for var in variables:
                if var.getName() == name:
                    var_type = var.getType().getValue()
                    if isinstance(var_type, PLCOpenClasses["derived"]) and var_type.getName() == type:
                        variables.remove(var)
                        break
            if len(varlist["value"].getVariable()) == 0:
                content.remove(varlist)
                break
    setattr(cls, "removePouVar", removePouVar)
    
    def hasBlock(self, name):
        if self.getBodyType() in ["FBD", "LD", "SFC"]:
            for instance in self.getInstances():
                if isinstance(instance, PLCOpenClasses["block"]) and instance.getInstanceName() == name:
                    return True
            if self.transitions:
                for transition in self.transitions.getTransition():
                    result = transition.hasBlock(name)
                    if result:
                        return result
            if self.actions:
                for action in self.actions.getAction():
                    result = action.hasBlock(name)
                    if result:
                        return result
        return False
    setattr(cls, "hasBlock", hasBlock)
    
    def addTransition(self, name, type):
        if not self.transitions:
            self.addTransitions()
            self.transitions.setTransition([])
        transition = PLCOpenClasses["transitions_transition"]()
        transition.setName(name)
        transition.setBodyType(type)
        self.transitions.appendTransition(transition)
    setattr(cls, "addTransition", addTransition)
    
    def getTransition(self, name):
        if self.transitions:
            for transition in self.transitions.getTransition():
                if transition.getName() == name:
                    return transition
        return None
    setattr(cls, "getTransition", getTransition)
        
    def getTransitionList(self):
        if self.transitions:
            return self.transitions.getTransition()
        return []
    setattr(cls, "getTransitionList", getTransitionList)
    
    def removeTransition(self, name):
        if self.transitions:
            transitions = self.transitions.getTransition()
            i = 0
            removed = False
            while i < len(transitions) and not removed:
                if transitions[i].getName() == name:
                    transitions.pop(i)
                    removed = True
                i += 1
            if not removed:
                raise ValueError, "Transition with name %s doesn't exists!"%name
    setattr(cls, "removeTransition", removeTransition)

    def addAction(self, name, type):
        if not self.actions:
            self.addActions()
            self.actions.setAction([])
        action = PLCOpenClasses["actions_action"]()
        action.setName(name)
        action.setBodyType(type)
        self.actions.appendAction(action)
    setattr(cls, "addAction", addAction)
    
    def getAction(self, name):
        if self.actions:
            for action in self.actions.getAction():
                if action.getName() == name:
                    return action
        return None
    setattr(cls, "getAction", getAction)
    
    def getActionList(self):
        if self.actions:
            return self.actions.getAction()
        return []
    setattr(cls, "getActionList", getActionList)
        
    def removeAction(self, name):
        if self.actions:
            actions = self.actions.getAction()
            i = 0
            removed = False
            while i < len(actions) and not removed:
                if actions[i].getName() == name:
                    actions.pop(i)
                    removed = True
                i += 1
            if not removed:
                raise ValueError, "Action with name %s doesn't exists!"%name
    setattr(cls, "removeAction", removeAction)

    def updateElementName(self, old_name, new_name):
        self.body.updateElementName(old_name, new_name)
        for action in self.getActionList():
            action.updateElementName(old_name, new_name)
        for transition in self.getTransitionList():
            transition.updateElementName(old_name, new_name)
    setattr(cls, "updateElementName", updateElementName)

cls = PLCOpenClasses.get("transitions_transition", None)
if cls:
    setattr(cls, "setBodyType", setBodyType)
    setattr(cls, "getBodyType", getBodyType)
    setattr(cls, "addInstance", addInstance)
    setattr(cls, "getInstances", getInstances)
    setattr(cls, "getInstance", getInstance)
    setattr(cls, "getRandomInstance", getRandomInstance)
    setattr(cls, "getInstanceByName", getInstanceByName)
    setattr(cls, "removeInstance", removeInstance)
    setattr(cls, "setText", setText)
    setattr(cls, "getText", getText)

    def updateElementName(self, old_name, new_name):
        self.body.updateElementName(old_name, new_name)
    setattr(cls, "updateElementName", updateElementName)

    def hasBlock(self, name):
        if self.getBodyType() in ["FBD", "LD", "SFC"]:
            for instance in self.getInstances():
                if isinstance(instance, PLCOpenClasses["block"]) and instance.getInstanceName() == name:
                    return True
        return False
    setattr(cls, "hasBlock", hasBlock)

cls = PLCOpenClasses.get("actions_action", None)
if cls:
    setattr(cls, "setBodyType", setBodyType)
    setattr(cls, "getBodyType", getBodyType)
    setattr(cls, "addInstance", addInstance)
    setattr(cls, "getInstances", getInstances)
    setattr(cls, "getInstance", getInstance)
    setattr(cls, "getRandomInstance", getRandomInstance)
    setattr(cls, "getInstanceByName", getInstanceByName)
    setattr(cls, "removeInstance", removeInstance)
    setattr(cls, "setText", setText)
    setattr(cls, "getText", getText)

    def updateElementName(self, old_name, new_name):
        self.body.updateElementName(old_name, new_name)
    setattr(cls, "updateElementName", updateElementName)

    def hasBlock(self, name):
        if self.getBodyType() in ["FBD", "LD", "SFC"]:
            for instance in self.getInstances():
                if isinstance(instance, PLCOpenClasses["block"]) and instance.getInstanceName() == name:
                    return True
        return False
    setattr(cls, "hasBlock", hasBlock)

cls = PLCOpenClasses.get("body", None)
if cls:
    def appendContentInstance(self, name, instance):
        if self.content["name"] in ["LD","FBD","SFC"]:
            self.content["value"].appendContent(name, instance)
        else:
            raise TypeError, "%s body don't have instances!"%self.content["name"]
    setattr(cls, "appendContentInstance", appendContentInstance)
    
    def getContentInstances(self):
        if self.content["name"] in ["LD","FBD","SFC"]:
            instances = []
            for element in self.content["value"].getContent():
                instances.append(element["value"])
            return instances
        else:
            raise TypeError, "%s body don't have instances!"%self.content["name"]
    setattr(cls, "getContentInstances", getContentInstances)

    def getContentInstance(self, id):
        if self.content["name"] in ["LD","FBD","SFC"]:
            for element in self.content["value"].getContent():
                if element["value"].getLocalId() == id:
                    return element["value"]
            return None
        else:
            raise TypeError, "%s body don't have instances!"%self.content["name"]
    setattr(cls, "getContentInstance", getContentInstance)
    
    def getContentRandomInstance(self, exclude):
        if self.content["name"] in ["LD","FBD","SFC"]:
            for element in self.content["value"].getContent():
                if element["value"].getLocalId() not in exclude:
                    return element["value"]
            return None
        else:
            raise TypeError, "%s body don't have instances!"%self.content["name"]
    setattr(cls, "getContentRandomInstance", getContentRandomInstance)
    
    def getContentInstanceByName(self, name):
        if self.content["name"] in ["LD","FBD","SFC"]:
            for element in self.content["value"].getContent():
                if element["value"].getLocalId() == name:
                    return element["value"]
        else:
            raise TypeError, "%s body don't have instances!"%self.content["name"]
    setattr(cls, "getContentInstanceByName", getContentInstanceByName)
    
    def removeContentInstance(self, id):
        if self.content["name"] in ["LD","FBD","SFC"]:
            i = 0
            removed = False
            elements = self.content["value"].getContent()
            while i < len(elements) and not removed:
                if elements[i]["value"].getLocalId() == id:
                    self.content["value"].removeContent(i)
                    removed = True
                i += 1
            if not removed:
                raise ValueError, "Instance with id %d doesn't exists!"%id
        else:
            raise TypeError, "%s body don't have instances!"%self.content["name"]
    setattr(cls, "removeContentInstance", removeContentInstance)
    
    def setText(self, text):
        if self.content["name"] in ["IL","ST"]:
            self.content["value"].setText(text)
        else:
            raise TypeError, "%s body don't have text!"%self.content["name"]
    setattr(cls, "setText", setText)

    def getText(self):
        if self.content["name"] in ["IL","ST"]:
            return self.content["value"].getText()
        else:
            raise TypeError, "%s body don't have text!"%self.content["name"]
    setattr(cls, "getText", getText)
    
    def updateElementName(self, old_name, new_name):
        if self.content["name"] in ["IL", "ST"]:
            self.content["value"].updateElementName(old_name, new_name)
        else:
            for element in self.content["value"].getContent():
                element["value"].updateElementName(old_name, new_name)
    setattr(cls, "updateElementName", updateElementName)

def getX(self):
    return self.position.getX()

def getY(self):
    return self.position.getY()

def setX(self, x):
    self.position.setX(x)
    
def setY(self, y):
    self.position.setY(y)

cls = PLCOpenClasses.get("comment", None)
if cls:
    setattr(cls, "getX", getX)
    setattr(cls, "getY", getY)
    setattr(cls, "setX", setX)
    setattr(cls, "setY", setY)

    def setContentText(self, text):
        self.content.setText(text)
    setattr(cls, "setContentText", setContentText)
        
    def getContentText(self):
        return self.content.getText()
    setattr(cls, "getContentText", getContentText)
    
    def updateElementName(self, old_name, new_name):
        self.content.updateElementName(old_name, new_name)
    setattr(cls, "updateElementName", updateElementName)

cls = PLCOpenClasses.get("block", None)
if cls:
    setattr(cls, "getX", getX)
    setattr(cls, "getY", getY)
    setattr(cls, "setX", setX)
    setattr(cls, "setY", setY)

    def updateElementName(self, old_name, new_name):
        if self.typeName == old_name:
            self.typeName = new_name
    setattr(cls, "updateElementName", updateElementName)

cls = PLCOpenClasses.get("inputVariables_variable", None)
if cls:
    def setConnectorEdge(self, edge):
        if not self.edge:
            self.edge = PLCOpenClasses["edgeModifierType"]()
        self.edge.setValue(edge)
    setattr(cls, "setConnectorEdge", setConnectorEdge)
    
    def getConnectorEdge(self):
        if self.edge:
            return self.edge.getValue()
        return None
    setattr(cls, "getConnectorEdge", getConnectorEdge)

cls = PLCOpenClasses.get("outputVariables_variable", None)
if cls:
    def setConnectorEdge(self, edge):
        if not self.edge:
            self.edge = PLCOpenClasses["edgeModifierType"]()
        self.edge.setValue(edge)
    setattr(cls, "setConnectorEdge", setConnectorEdge)
    
    def getConnectorEdge(self):
        if self.edge:
            return self.edge.getValue()
        return None
    setattr(cls, "getConnectorEdge", getConnectorEdge)

cls = PLCOpenClasses.get("leftPowerRail", None)
if cls:
    setattr(cls, "getX", getX)
    setattr(cls, "getY", getY)
    setattr(cls, "setX", setX)
    setattr(cls, "setY", setY)

    def updateElementName(self, old_name, new_name):
        pass
    setattr(cls, "updateElementName", updateElementName)

cls = PLCOpenClasses.get("rightPowerRail", None)
if cls:
    setattr(cls, "getX", getX)
    setattr(cls, "getY", getY)
    setattr(cls, "setX", setX)
    setattr(cls, "setY", setY)
    
    def updateElementName(self, old_name, new_name):
        pass
    setattr(cls, "updateElementName", updateElementName)

cls = PLCOpenClasses.get("contact", None)
if cls:
    setattr(cls, "getX", getX)
    setattr(cls, "getY", getY)
    setattr(cls, "setX", setX)
    setattr(cls, "setY", setY)

    def setContactEdge(self, edge):
        if not self.edge:
            self.edge = PLCOpenClasses["edgeModifierType"]()
        self.edge.setValue(edge)
    setattr(cls, "setContactEdge", setContactEdge)
    
    def getContactEdge(self):
        if self.edge:
            return self.edge.getValue()
        return None
    setattr(cls, "getContactEdge", getContactEdge)

    def updateElementName(self, old_name, new_name):
        if self.variable == old_name:
            self.variable = new_name
    setattr(cls, "updateElementName", updateElementName)

cls = PLCOpenClasses.get("coil", None)
if cls:
    setattr(cls, "getX", getX)
    setattr(cls, "getY", getY)
    setattr(cls, "setX", setX)
    setattr(cls, "setY", setY)
    
    def setCoilStorage(self, edge):
        if not self.storage:
            self.storage = PLCOpenClasses["storageModifierType"]()
        self.storage.setValue(edge)
    setattr(cls, "setCoilStorage", setCoilStorage)
    
    def getCoilStorage(self):
        if self.storage:
            return self.storage.getValue()
        return None
    setattr(cls, "getCoilStorage", getCoilStorage)

    def updateElementName(self, old_name, new_name):
        if self.variable == old_name:
            self.variable = new_name
    setattr(cls, "updateElementName", updateElementName)

cls = PLCOpenClasses.get("step", None)
if cls:
    setattr(cls, "getX", getX)
    setattr(cls, "getY", getY)
    setattr(cls, "setX", setX)
    setattr(cls, "setY", setY)

    def updateElementName(self, old_name, new_name):
        pass
    setattr(cls, "updateElementName", updateElementName)

cls = PLCOpenClasses.get("transition", None)
if cls:
    setattr(cls, "getX", getX)
    setattr(cls, "getY", getY)
    setattr(cls, "setX", setX)
    setattr(cls, "setY", setY)

    def setConditionContent(self, type, value):
        if not self.condition:
            self.addCondition()
        if type == "reference":
            condition = PLCOpenClasses["condition_reference"]()
            condition.setName(value)
        elif type == "inline":
            condition = PLCOpenClasses["condition_inline"]()
            condition.setContent("ST", PLCOpenClasses["formattedText"]())
            condition.setText(value)
        elif type == "connection":
            condition = []
        self.condition.setContent(type, condition)
    setattr(cls, "setConditionContent", setConditionContent)
        
    def getConditionContent(self):
        if self.condition:
            content = self.condition.getContent()
            values = {"type" : content["name"]}
            if values["type"] == "reference":
                values["value"] = content["value"].getName()
            elif values["type"] == "inline":
                values["value"] = content["value"].getText()
            return values
        return ""
    setattr(cls, "getConditionContent", getConditionContent)

    def updateElementName(self, old_name, new_name):
        if self.condition:
            content = self.condition.getContent()
            if content["name"] == "reference":
                if content["value"].getName() == old_name:
                    content["value"].setName(new_name)
            elif content["name"] == "inline":
                content["value"].updateElementName(old_name, new_name)
    setattr(cls, "updateElementName", updateElementName)

    def addConnection(self):
        if self.condition:
            content = self.condition.getContent()
            if content["name"] != "connection":
                self.condition.setContent("connection", [])
                content = self.condition.getContent()
            content["value"].append(PLCOpenClasses["connection"]())
    setattr(cls, "addConnection", addConnection)
    
    def removeConnection(self, idx):
        if self.condition:
            content = self.condition.getContent()
            if content["name"] == "connection":
                content["value"].pop(idx)
        setattr(cls, "removeConnection", removeConnection)

    def removeConnections(self):
        if self.condition:
            content = self.condition.getContent()
            if content["name"] == "connection":
                content["value"] = []
    setattr(cls, "removeConnections", removeConnections)
    
    def getConnections(self):
        if self.condition:
            content = self.condition.getContent()
            if content["name"] == "connection":
                return content["value"]
    setattr(cls, "getConnections", getConnections)
    
    def setConnectionId(self, idx, id):
        if self.condition:
            content = self.condition.getContent()
            if content["name"] == "connection":
                content["value"][idx].setRefLocalId(id)
    setattr(cls, "setConnectionId", setConnectionId)
    
    def getConnectionId(self, idx):
        if self.condition:
            content = self.condition.getContent()
            if content["name"] == "connection":
                return content["value"][idx].getRefLocalId()
        return None
    setattr(cls, "getConnectionId", getConnectionId)
    
    def setConnectionPoints(self, idx, points):
        if self.condition:
            content = self.condition.getContent()
            if content["name"] == "connection":
                content["value"][idx].setPoints(points)
    setattr(cls, "setConnectionPoints", setConnectionPoints)

    def getConnectionPoints(self, idx):
        if self.condition:
            content = self.condition.getContent()
            if content["name"] == "connection":
                return content["value"][idx].getPoints()
        return None
    setattr(cls, "getConnectionPoints", getConnectionPoints)

    def setConnectionParameter(self, idx, parameter):
        if self.condition:
            content = self.condition.getContent()
            if content["name"] == "connection":
                content["value"][idx].setFormalParameter(parameter)
    setattr(cls, "setConnectionParameter", setConnectionParameter)
    
    def getConnectionParameter(self, idx):
        if self.condition:
            content = self.condition.getContent()
            if content["name"] == "connection":
                return content["value"][idx].getFormalParameter()
        return None
    setattr(cls, "getConnectionParameter", getConnectionParameter)
    
    setattr(cls, "addConnection", addConnection)    

cls = PLCOpenClasses.get("selectionDivergence", None)
if cls:
    setattr(cls, "getX", getX)
    setattr(cls, "getY", getY)
    setattr(cls, "setX", setX)
    setattr(cls, "setY", setY)

    def updateElementName(self, old_name, new_name):
        pass
    setattr(cls, "updateElementName", updateElementName)

cls = PLCOpenClasses.get("selectionConvergence", None)
if cls:
    setattr(cls, "getX", getX)
    setattr(cls, "getY", getY)
    setattr(cls, "setX", setX)
    setattr(cls, "setY", setY)
    
    def updateElementName(self, old_name, new_name):
        pass
    setattr(cls, "updateElementName", updateElementName)
    
cls = PLCOpenClasses.get("simultaneousDivergence", None)
if cls:
    setattr(cls, "getX", getX)
    setattr(cls, "getY", getY)
    setattr(cls, "setX", setX)
    setattr(cls, "setY", setY)
    
    def updateElementName(self, old_name, new_name):
        pass
    setattr(cls, "updateElementName", updateElementName)
    
cls = PLCOpenClasses.get("simultaneousConvergence", None)
if cls:
    setattr(cls, "getX", getX)
    setattr(cls, "getY", getY)
    setattr(cls, "setX", setX)
    setattr(cls, "setY", setY)
    
    def updateElementName(self, old_name, new_name):
        pass
    setattr(cls, "updateElementName", updateElementName)

cls = PLCOpenClasses.get("jumpStep", None)
if cls:
    setattr(cls, "getX", getX)
    setattr(cls, "getY", getY)
    setattr(cls, "setX", setX)
    setattr(cls, "setY", setY)
    
    def updateElementName(self, old_name, new_name):
        pass
    setattr(cls, "updateElementName", updateElementName)

cls = PLCOpenClasses.get("actionBlock_action", None)
if cls:
    def setQualifierValue(self, value):
        if self.qualifier:
            self.qualifier.setValue(value)
    setattr(cls, "setQualifierValue", setQualifierValue)
    
    def getQualifierValue(self):
        if self.qualifier:
            return self.qualifier.getValue()
        return None
    setattr(cls, "getQualifierValue", getQualifierValue)

    def setReferenceName(self, name):
        if self.reference:
            self.reference.setName(name)
    setattr(cls, "setReferenceName", setReferenceName)
    
    def getReferenceName(self):
        if self.reference:
            return self.reference.getName()
        return None
    setattr(cls, "getReferenceName", getReferenceName)

    def setInlineContent(self, content):
        if self.inline:
            self.inline.setContent("ST", PLCOpenClasses["formattedText"]())
            self.inline.setText(content)
    setattr(cls, "setInlineContent", setInlineContent)
    
    def getInlineContent(self):
        if self.inline:
            return self.inline.getText()
        return None
    setattr(cls, "getInlineContent", getInlineContent)

    def updateElementName(self, old_name, new_name):
        if self.reference and self.reference.getName() == old_name:
            self.reference.setName(new_name)
        if self.inline:
            self.inline.updateElementName(old_name, new_name)
    setattr(cls, "updateElementName", updateElementName)

cls = PLCOpenClasses.get("actionBlock", None)
if cls:
    setattr(cls, "getX", getX)
    setattr(cls, "getY", getY)
    setattr(cls, "setX", setX)
    setattr(cls, "setY", setY)

    def setActions(self, actions):
        self.action = []
        for params in actions:
            action = PLCOpenClasses["actionBlock_action"]()
            action.addQualifier()
            action.setQualifierValue(params["qualifier"])
            if params["type"] == "reference":
                action.addReference()
                action.setReferenceName(params["value"])
            else:
                action.addInline()
                action.setInlineContent(params["value"])
            if "duration" in params:
                action.setDuration(params["duration"])
            if "indicator" in params:
                action.setIndicator(params["indicator"])
            self.action.append(action)
    setattr(cls, "setActions", setActions)

    def getActions(self):
        actions = []
        for action in self.action:
            params = {}
            params["qualifier"] = action.getQualifierValue()
            if action.getReference():
                params["type"] = "reference"
                params["value"] = action.getReferenceName()
            elif action.getInline():
                params["type"] = "inline"
                params["value"] = action.getInlineContent()
            duration = action.getDuration()
            if duration:
                params["duration"] = duration
            indicator = action.getIndicator()
            if indicator:
                params["indicator"] = indicator
            actions.append(params)
        return actions
    setattr(cls, "getActions", getActions)

    def updateElementName(self, old_name, new_name):
        for action in self.action:
            action.updateElementName(old_name, new_name)
    setattr(cls, "updateElementName", updateElementName)

cls = PLCOpenClasses.get("inVariable", None)
if cls:
    setattr(cls, "getX", getX)
    setattr(cls, "getY", getY)
    setattr(cls, "setX", setX)
    setattr(cls, "setY", setY)

    def setConnectorEdge(self, edge):
        if not self.edge:
            self.edge = PLCOpenClasses["edgeModifierType"]()
        self.edge.setValue(edge)
    setattr(cls, "setConnectorEdge", setConnectorEdge)
    
    def getConnectorEdge(self):
        if self.edge:
            return self.edge.getValue()
        return None
    setattr(cls, "getConnectorEdge", getConnectorEdge)
    
    def updateElementName(self, old_name, new_name):
        if self.expression == old_name:
            self.expression = new_name
    setattr(cls, "updateElementName", updateElementName)

cls = PLCOpenClasses.get("outVariable", None)
if cls:
    setattr(cls, "getX", getX)
    setattr(cls, "getY", getY)
    setattr(cls, "setX", setX)
    setattr(cls, "setY", setY)

    def setConnectorEdge(self, edge):
        if not self.edge:
            self.edge = PLCOpenClasses["edgeModifierType"]()
        self.edge.setValue(edge)
    setattr(cls, "setConnectorEdge", setConnectorEdge)
    
    def getConnectorEdge(self):
        if self.edge:
            return self.edge.getValue()
        return None
    setattr(cls, "getConnectorEdge", getConnectorEdge)

    def updateElementName(self, old_name, new_name):
        if self.expression == old_name:
            self.expression = new_name
    setattr(cls, "updateElementName", updateElementName)

cls = PLCOpenClasses.get("inOutVariable", None)
if cls:
    setattr(cls, "getX", getX)
    setattr(cls, "getY", getY)
    setattr(cls, "setX", setX)
    setattr(cls, "setY", setY)

    def setInputEdge(self, edge):
        if not self.edgeIn:
            self.edgeIn = PLCOpenClasses["edgeModifierType"]()
        self.edgeIn.setValue(edge)
    setattr(cls, "setInputEdge", setInputEdge)
    
    def getInputEdge(self):
        if self.edgeIn:
            return self.edgeIn.getValue()
        return None
    setattr(cls, "getInputEdge", getInputEdge)

    def setOutputEdge(self, edge):
        if not self.edgeOut:
            self.edgeOut = PLCOpenClasses["edgeModifierType"]()
        self.edgeOut.setValue(edge)
    setattr(cls, "setInputEdge", setInputEdge)
    
    def getOutputEdge(self):
        if self.edgeOut:
            return self.edgeOut.getValue()
        return None
    setattr(cls, "getOutputEdge", getOutputEdge)

    def updateElementName(self, old_name, new_name):
        if self.expression == old_name:
            self.expression = new_name
    setattr(cls, "updateElementName", updateElementName)

cls = PLCOpenClasses.get("continuation", None)
if cls:
    setattr(cls, "getX", getX)
    setattr(cls, "getY", getY)
    setattr(cls, "setX", setX)
    setattr(cls, "setY", setY)
    
    def updateElementName(self, old_name, new_name):
        pass
    setattr(cls, "updateElementName", updateElementName)

cls = PLCOpenClasses.get("connector", None)
if cls:
    setattr(cls, "getX", getX)
    setattr(cls, "getY", getY)
    setattr(cls, "setX", setX)
    setattr(cls, "setY", setY)
    
    def updateElementName(self, old_name, new_name):
        pass
    setattr(cls, "updateElementName", updateElementName)

cls = PLCOpenClasses.get("connection", None)
if cls:
    def setPoints(self, points):
        self.position = []
        for point in points:
            position = PLCOpenClasses["position"]()
            position.setX(point.x)
            position.setY(point.y)
            self.position.append(position)
    setattr(cls, "setPoints", setPoints)

    def getPoints(self):
        points = []
        for position in self.position:
            points.append((position.getX(),position.getY()))
        return points
    setattr(cls, "getPoints", getPoints)

cls = PLCOpenClasses.get("connectionPointIn", None)
if cls:
    def setRelPosition(self, x, y):
        self.relPosition = PLCOpenClasses["position"]()
        self.relPosition.setX(x)
        self.relPosition.setY(y)
    setattr(cls, "setRelPosition", setRelPosition)

    def getRelPosition(self):
        if self.relPosition:
            return self.relPosition.getX(), self.relPosition.getY()
        else:
            return self.relPosition
    setattr(cls, "getRelPosition", getRelPosition)

    def addConnection(self):
        if not self.content:
            self.content = {"name":"connection","value":[PLCOpenClasses["connection"]()]}
        else:
            self.content["value"].append(PLCOpenClasses["connection"]())
    setattr(cls, "addConnection", addConnection)

    def removeConnection(self, idx):
        if self.content:
            self.content["value"].pop(idx)
        if len(self.content["value"]) == 0:
            self.content = None
    setattr(cls, "removeConnection", removeConnection)

    def removeConnections(self):
        if self.content:
            self.content = None
    setattr(cls, "removeConnections", removeConnections)
    
    def getConnections(self):
        if self.content:
            return self.content["value"]
    setattr(cls, "getConnections", getConnections)
    
    def setConnectionId(self, idx, id):
        if self.content:
            self.content["value"][idx].setRefLocalId(id)
    setattr(cls, "setConnectionId", setConnectionId)
    
    def getConnectionId(self, idx):
        if self.content:
            return self.content["value"][idx].getRefLocalId()
        return None
    setattr(cls, "getConnectionId", getConnectionId)
    
    def setConnectionPoints(self, idx, points):
        if self.content:
            self.content["value"][idx].setPoints(points)
    setattr(cls, "setConnectionPoints", setConnectionPoints)

    def getConnectionPoints(self, idx):
        if self.content:
            return self.content["value"][idx].getPoints()
        return None
    setattr(cls, "getConnectionPoints", getConnectionPoints)

    def setConnectionParameter(self, idx, parameter):
        if self.content:
            self.content["value"][idx].setFormalParameter(parameter)
    setattr(cls, "setConnectionParameter", setConnectionParameter)
    
    def getConnectionParameter(self, idx):
        if self.content:
            return self.content["value"][idx].getFormalParameter()
        return None
    setattr(cls, "getConnectionParameter", getConnectionParameter)

cls = PLCOpenClasses.get("connectionPointOut", None)
if cls:
    def setRelPosition(self, x, y):
        self.relPosition = PLCOpenClasses["position"]()
        self.relPosition.setX(x)
        self.relPosition.setY(y)
    setattr(cls, "setRelPosition", setRelPosition)

    def getRelPosition(self):
        if self.relPosition:
            return self.relPosition.getX(), self.relPosition.getY()
        return self.relPosition
    setattr(cls, "getRelPosition", getRelPosition)

cls = PLCOpenClasses.get("value", None)
if cls:
    def setValue(self, value):
        try:
            value = eval(value)
        except:
            pass
        if type(value) == ListType:
            arrayValue = PLCOpenClasses["value_arrayValue"]()
            arrayValue.setValue(value)
            self.content = {"name":"arrayValue","value":arrayValue}
        elif type(value) == DictType:
            structValue = PLCOpenClasses["value_structValue"]()
            structValue.setValue(value)
            self.content = {"name":"structValue","value":structValue}
        else:
            simpleValue = PLCOpenClasses["value_simpleValue"]()
            simpleValue.setValue(str(value))
            self.content = {"name":"simpleValue","value":simpleValue}
    setattr(cls, "setValue", setValue)
    
    def getValue(self):
        value = self.content["value"].getValue()
        try:
            value = eval(value)
        except:
            pass
        return value
    setattr(cls, "getValue", getValue)

cls = PLCOpenClasses.get("value_arrayValue", None)
if cls:
    def setValue(self, array):
        self.value = []
        for value in array:
            element = PLCOpenClasses["arrayValue_value"]()
            element.setValue(value)
            self.value.append(element)
    setattr(cls, "setValue", setValue)
    
    def getValue(self):
        return [element.getValue() for element in self.value]
    setattr(cls, "getValue", getValue)

cls = PLCOpenClasses.get("value_structValue", None)
if cls:
    def setValue(self, dict):
        self.value = []
        for name,value in dict.items():
            element = PLCOpenClasses["structValue_value"]()
            element.setMember(name)
            element.setValue(value)
            self.value.append(element)
    setattr(cls, "setValue", setValue)
    
    def getValue(self):
        value = {}
        for element in self.value:
            value[element.getMember()] = element.getValue()
        return value
    setattr(cls, "getValue", getValue)