PLCGenerator.py
author lbessard
Thu, 02 Aug 2007 16:51:58 +0200
changeset 58 39cd981ff242
parent 47 2b2f8d88e6d3
child 66 fd138fc77510
permissions -rw-r--r--
Changing file headers
Adding support for automatically modifying variable and POU names when changing
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     1
#!/usr/bin/env python
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     3
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     4
#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     5
#based on the plcopen standard. 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     6
#
58
39cd981ff242 Changing file headers
lbessard
parents: 47
diff changeset
     7
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     8
#
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     9
#See COPYING file for copyrights details.
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    10
#
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    11
#This library is free software; you can redistribute it and/or
5
f8652b073e84 GPL->LGPL
etisserant
parents: 4
diff changeset
    12
#modify it under the terms of the GNU General Public
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    13
#License as published by the Free Software Foundation; either
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    14
#version 2.1 of the License, or (at your option) any later version.
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    15
#
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    16
#This library is distributed in the hope that it will be useful,
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    17
#but WITHOUT ANY WARRANTY; without even the implied warranty of
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    18
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
58
39cd981ff242 Changing file headers
lbessard
parents: 47
diff changeset
    19
#General Public License for more details.
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    20
#
5
f8652b073e84 GPL->LGPL
etisserant
parents: 4
diff changeset
    21
#You should have received a copy of the GNU General Public
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    22
#License along with this library; if not, write to the Free Software
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    23
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    24
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    25
from plcopen import plcopen
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    26
from plcopen.structures import *
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    27
from types import *
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    28
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    29
varTypeNames = {"localVars" : "VAR", "tempVars" : "VAR_TEMP", "inputVars" : "VAR_INPUT", 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    30
                "outputVars" : "VAR_OUTPUT", "inOutVars" : "VAR_IN_OUT", "externalVars" : "VAR_EXTERNAL",
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    31
                "globalVars" : "VAR_GLOBAL", "accessVars" : "VAR_ACCESS"}
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    32
6
c8cf918ee7ea Bug corrected and improvements
lbessard
parents: 5
diff changeset
    33
pouTypeNames = {"function" : "FUNCTION", "functionBlock" : "FUNCTION_BLOCK", "program" : "PROGRAM"}
c8cf918ee7ea Bug corrected and improvements
lbessard
parents: 5
diff changeset
    34
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    35
def ReIndentText(text, nb_spaces):
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    36
    compute = ""
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    37
    lines = text.splitlines()
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    38
    if len(lines) > 0:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    39
        spaces = 0
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    40
        while lines[0][spaces] == " ":
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    41
            spaces += 1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    42
        indent = ""
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    43
        for i in xrange(spaces, nb_spaces):
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    44
            indent += " "
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    45
        for line in lines:
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
    46
            if line != "":
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
    47
                compute += "%s%s\n"%(indent, line)
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
    48
            else:
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
    49
                compute += "\n"
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    50
    return compute
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    51
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    52
"""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    53
Module implementing methods for generating PLC programs in ST or IL
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    54
"""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    55
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    56
class PouProgram:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    57
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    58
    def __init__(self, name, type):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    59
        self.Name = name
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    60
        self.Type = type
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    61
        self.ReturnType = None
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    62
        self.Interface = []
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    63
        self.InitialSteps = []
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    64
        self.SFCNetworks = {"Steps":{}, "Transitions":{}, "Actions":{}}
46
4379e98a30aa Bug on SFC generation fixed
lbessard
parents: 37
diff changeset
    65
        self.ActionNumber = 0
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    66
        self.Program = ""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    67
    
46
4379e98a30aa Bug on SFC generation fixed
lbessard
parents: 37
diff changeset
    68
    def GetActionNumber(self):
4379e98a30aa Bug on SFC generation fixed
lbessard
parents: 37
diff changeset
    69
        self.ActionNumber += 1
4379e98a30aa Bug on SFC generation fixed
lbessard
parents: 37
diff changeset
    70
        return self.ActionNumber
4379e98a30aa Bug on SFC generation fixed
lbessard
parents: 37
diff changeset
    71
    
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    72
    def IsAlreadyDefined(self, name):
33
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
    73
        for list_type, retain, constant, located, vars in self.Interface:
30
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
    74
            for var_type, var_name, var_address, var_initial in vars:
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    75
                if name == var_name:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    76
                    return True
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    77
        return False
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    78
    
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    79
    def GenerateInterface(self, interface):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    80
        if self.Type == "FUNCTION":
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    81
            self.ReturnType = interface.getReturnType().getValue()
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    82
        for varlist in interface.getContent():
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    83
            variables = []
33
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
    84
            located = False
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    85
            for var in varlist["value"].getVariable():
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    86
                type = var.getType().getValue()
28
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 6
diff changeset
    87
                initial = var.getInitialValue()
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 6
diff changeset
    88
                if initial:
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 6
diff changeset
    89
                    initial_value = initial.getValue()
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 6
diff changeset
    90
                else:
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 6
diff changeset
    91
                    initial_value = None
30
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
    92
                address = var.getAddress()
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
    93
                if address == "":
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
    94
                    address = None
33
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
    95
                else:
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
    96
                    located = True
30
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
    97
                variables.append((type, var.getName(), address, initial_value))
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    98
            self.Interface.append((varTypeNames[varlist["name"]], varlist["value"].getRetain(), 
33
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
    99
                            varlist["value"].getConstant(), located, variables))
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   100
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   101
    def GenerateProgram(self, pou):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   102
        body = pou.getBody()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   103
        body_content = body.getContent()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   104
        body_type = body_content["name"]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   105
        if body_type in ["IL","ST"]:
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   106
            self.Program = ReIndentText(body_content["value"].getText(), 2)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   107
        elif body_type == "FBD":
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   108
            for instance in body.getContentInstances():
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   109
                if isinstance(instance, plcopen.outVariable):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   110
                    var = instance.getExpression()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   111
                    connections = instance.connectionPointIn.getConnections()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   112
                    if connections and len(connections) == 1:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   113
                        expression = self.ComputeFBDExpression(body, connections[0])
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   114
                        self.Program += "  %s := %s;\n"%(var, expression)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   115
        elif body_type == "LD":
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   116
            for instance in body.getContentInstances():
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   117
                if isinstance(instance, plcopen.coil):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   118
                    paths = self.GenerateLDPaths(instance, body)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   119
                    variable = self.ExtractModifier(instance, instance.getVariable())
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   120
                    expression = self.ComputeLDExpression(paths, True)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   121
                    self.Program += "  %s := %s;\n"%(variable, expression)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   122
        elif body_type == "SFC":
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   123
            for instance in body.getContentInstances():
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   124
                if isinstance(instance, plcopen.step):
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   125
                    self.GenerateSFCStep(instance, pou)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   126
                elif isinstance(instance, plcopen.actionBlock):
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   127
                    self.GenerateSFCStepActions(instance, pou)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   128
                elif isinstance(instance, plcopen.transition):
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   129
                    self.GenerateSFCTransition(instance, pou)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   130
                elif isinstance(instance, plcopen.jumpStep):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   131
                    self.GenerateSFCJump(instance, pou)
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   132
            for initialstep in self.InitialSteps:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   133
                self.ComputeSFCStep(initialstep)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   134
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   135
    def ComputeFBDExpression(self, body, link):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   136
        localid = link.getRefLocalId()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   137
        instance = body.getContentInstance(localid)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   138
        if isinstance(instance, plcopen.inVariable):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   139
            return instance.getExpression()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   140
        elif isinstance(instance, plcopen.block):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   141
            name = instance.getInstanceName()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   142
            type = instance.getTypeName()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   143
            block_infos = GetBlockType(type)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   144
            if block_infos["type"] == "function":
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   145
                vars = []
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   146
                for variable in instance.inputVariables.getVariable():
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   147
                    connections = variable.connectionPointIn.getConnections()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   148
                    if connections and len(connections) == 1:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   149
                        value = self.ComputeFBDExpression(body, connections[0])
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   150
                        vars.append(self.ExtractModifier(variable, value))
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   151
                variable = instance.outputVariables.getVariable()[0]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   152
                return self.ExtractModifier(variable, "%s(%s)"%(type, ", ".join(vars)))
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   153
            elif block_infos["type"] == "functionBlock":
33
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
   154
                if self.Interface[-1][0] != "VAR" or self.Interface[-1][1] or self.Interface[-1][2] or self.Interface[-1][3]:
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
   155
                    self.Interface.append(("VAR", False, False, False, []))
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   156
                if not self.IsAlreadyDefined(name):
33
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
   157
                    self.Interface[-1][4].append((type, name, None, None))
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   158
                    vars = []
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   159
                    for variable in instance.inputVariables.getVariable():
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   160
                        connections = variable.connectionPointIn.getConnections()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   161
                        if connections and len(connections) == 1:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   162
                            parameter = variable.getFormalParameter()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   163
                            value = self.ComputeFBDExpression(body, connections[0])
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   164
                            vars.append(self.ExtractModifier(variable, "%s := %s"%(parameter, value)))
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   165
                    self.Program += "  %s(%s);\n"%(name, ", ".join(vars))
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   166
                connectionPoint = link.getPosition()[-1]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   167
                for variable in instance.outputVariables.getVariable():
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   168
                    blockPointx, blockPointy = variable.connectionPointOut.getRelPosition()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   169
                    if instance.getX() + blockPointx == connectionPoint.getX() and instance.getY() + blockPointy == connectionPoint.getY():
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   170
                        return self.ExtractModifier(variable, "%s.%s"%(name, variable.getFormalParameter()))
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   171
                raise ValueError, "No output variable found"
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   172
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   173
    def GenerateLDPaths(self, instance, body):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   174
        paths = []
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   175
        variable = self.ExtractModifier(instance, instance.getVariable())
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   176
        connections = instance.connectionPointIn.getConnections()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   177
        for connection in connections:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   178
            localId = connection.getRefLocalId()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   179
            next = body.getContentInstance(localId)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   180
            if isinstance(next, plcopen.leftPowerRail):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   181
                paths.append(None)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   182
            else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   183
                paths.append(self.GenerateLDPaths(next, body))
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   184
        if isinstance(instance, plcopen.coil):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   185
            if len(paths) > 1:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   186
                return tuple(paths)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   187
            else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   188
                return paths
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   189
        else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   190
            if len(paths) > 1:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   191
                return [variable, tuple(paths)]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   192
            elif type(paths[0]) == ListType:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   193
                return [variable] + paths[0]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   194
            elif paths[0]:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   195
                return [variable, paths[0]]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   196
            else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   197
                return variable
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   198
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   199
    def ExtractDivergenceInput(self, divergence, pou):
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   200
        connectionPointIn = divergence.getConnectionPointIn()
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   201
        if connectionPointIn:
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   202
            connections = connectionPointIn.getConnections()
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   203
            if len(connections) == 1:
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   204
                instanceLocalId = connections[0].getRefLocalId()
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   205
                return pou.body.getContentInstance(instanceLocalId)
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   206
        return None
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   207
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   208
    def ExtractConvergenceInputs(self, convergence, pou):
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   209
        instances = []
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   210
        for connectionPointIn in convergence.getConnectionPointIn():
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   211
            connections = connectionPointIn.getConnections()
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   212
            if len(connections) == 1:
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   213
                instanceLocalId = connections[0].getRefLocalId()
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   214
                instances.append(pou.body.getContentInstance(instanceLocalId))
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   215
        return instances
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   216
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   217
    def GenerateSFCStep(self, step, pou):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   218
        step_name = step.getName()
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   219
        if step_name not in self.SFCNetworks["Steps"].keys():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   220
            if step.getInitialStep():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   221
                self.InitialSteps.append(step_name)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   222
            step_infos = {"initial" : step.getInitialStep(), "transitions" : [], "actions" : []}
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   223
            if step.connectionPointIn:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   224
                instances = []
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   225
                connections = step.connectionPointIn.getConnections()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   226
                if len(connections) == 1:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   227
                    instanceLocalId = connections[0].getRefLocalId()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   228
                    instance = pou.body.getContentInstance(instanceLocalId)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   229
                    if isinstance(instance, plcopen.transition):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   230
                        instances.append(instance)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   231
                    elif isinstance(instance, plcopen.selectionConvergence):
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   232
                        instances.extend(self.ExtractConvergenceInputs(instance, pou))
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   233
                    elif isinstance(instance, plcopen.simultaneousDivergence):
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   234
                        transition = self.ExtractDivergenceInput(instance, pou)
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   235
                        if transition:
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   236
                            if isinstance(transition, plcopen.transition):
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   237
                                instances.append(transition)
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   238
                            elif isinstance(transition, plcopen.selectionConvergence):
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   239
                                instances.extend(self.ExtractConvergenceInputs(transition, pou))
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   240
                for instance in instances:
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   241
                    self.GenerateSFCTransition(instance, pou)
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   242
                    if instance in self.SFCNetworks["Transitions"].keys():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   243
                        self.SFCNetworks["Transitions"][instance]["to"].append(step_name)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   244
            self.SFCNetworks["Steps"][step_name] = step_infos
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   245
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   246
    def GenerateSFCJump(self, jump, pou):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   247
        jump_target = jump.getTargetName()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   248
        if jump.connectionPointIn:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   249
            instances = []
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   250
            connections = jump.connectionPointIn.getConnections()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   251
            if len(connections) == 1:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   252
                instanceLocalId = connections[0].getRefLocalId()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   253
                instance = pou.body.getContentInstance(instanceLocalId)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   254
                if isinstance(instance, plcopen.transition):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   255
                    instances.append(instance)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   256
                elif isinstance(instance, plcopen.selectionConvergence):
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   257
                    instances.extend(self.ExtractConvergenceInputs(instance, pou))
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   258
                elif isinstance(instance, plcopen.simultaneousDivergence):
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   259
                    transition = self.ExtractDivergenceInput(instance, pou)
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   260
                    if transition:
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   261
                        if isinstance(transition, plcopen.transition):
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   262
                            instances.append(transition)
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   263
                        elif isinstance(transition, plcopen.selectionConvergence):
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   264
                            instances.extend(self.ExtractConvergenceInputs(transition, pou))
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   265
            for instance in instances:
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   266
                self.GenerateSFCTransition(instance, pou)
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   267
                if instance in self.SFCNetworks["Transitions"].keys():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   268
                    self.SFCNetworks["Transitions"][instance]["to"].append(jump_target)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   269
    
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   270
    def GenerateSFCStepActions(self, actionBlock, pou):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   271
        connections = actionBlock.connectionPointIn.getConnections()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   272
        if len(connections) == 1:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   273
            stepLocalId = connections[0].getRefLocalId()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   274
            step = pou.body.getContentInstance(stepLocalId)
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   275
            self.GenerateSFCStep(step, pou)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   276
            step_name = step.getName()
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   277
            if step_name in self.SFCNetworks["Steps"].keys():
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   278
                actions = actionBlock.getActions()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   279
                for action in actions:
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   280
                    action_infos = {"qualifier" : action["qualifier"], "content" : action["value"]}
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   281
                    if "duration" in action:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   282
                        action_infos["duration"] = action["duration"]
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   283
                    if "indicator" in action:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   284
                        action_infos["indicator"] = action["indicator"]
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   285
                    if action["type"] == "reference":
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   286
                        self.GenerateSFCAction(action["value"], pou)
46
4379e98a30aa Bug on SFC generation fixed
lbessard
parents: 37
diff changeset
   287
                    else:
4379e98a30aa Bug on SFC generation fixed
lbessard
parents: 37
diff changeset
   288
                        action_name = "INLINE%d"%self.GetActionNumber()
4379e98a30aa Bug on SFC generation fixed
lbessard
parents: 37
diff changeset
   289
                        self.SFCNetworks["Actions"][action_name] = "    %s\n"%action["value"]
4379e98a30aa Bug on SFC generation fixed
lbessard
parents: 37
diff changeset
   290
                        action_infos["content"] = action_name
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   291
                    self.SFCNetworks["Steps"][step_name]["actions"].append(action_infos)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   292
    
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   293
    def GenerateSFCAction(self, action_name, pou):
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   294
        if action_name not in self.SFCNetworks["Actions"].keys():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   295
            actionContent = pou.getAction(action_name)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   296
            if actionContent:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   297
                actionType = actionContent.getBodyType()
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   298
                actionBody = actionContent.getBody()
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   299
                if actionType in ["ST", "IL"]:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   300
                    self.SFCNetworks["Actions"][action_name] = ReIndentText(actionContent.getText(), 4)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   301
                elif actionType == "FBD":
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   302
                    for instance in actionBody.getContentInstances():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   303
                        if isinstance(instance, plcopen.outVariable):
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   304
                            var = instance.getExpression()
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   305
                            connections = instance.connectionPointIn.getConnections()
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   306
                            if connections and len(connections) == 1:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   307
                                expression = self.ComputeFBDExpression(actionBody, connections[0])
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   308
                                self.SFCNetworks["Actions"][action_name] = self.Program + "  %s := %s;\n"%(var, expression)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   309
                                self.Program = ""
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   310
                elif actionType == "LD":
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   311
                    for instance in actionbody.getContentInstances():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   312
                        if isinstance(instance, plcopen.coil):
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   313
                            paths = self.GenerateLDPaths(instance, actionBody)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   314
                            variable = self.ExtractModifier(instance, instance.getVariable())
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   315
                            expression = self.ComputeLDExpression(paths, True)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   316
                            self.SFCNetworks["Actions"][action_name] = self.Program + "  %s := %s;\n"%(variable, expression)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   317
                            self.Program = ""
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   318
    
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   319
    def GenerateSFCTransition(self, transition, pou):
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   320
        if transition not in self.SFCNetworks["Transitions"].keys():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   321
            steps = []
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   322
            connections = transition.connectionPointIn.getConnections()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   323
            if len(connections) == 1:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   324
                instanceLocalId = connections[0].getRefLocalId()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   325
                instance = pou.body.getContentInstance(instanceLocalId)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   326
                if isinstance(instance, plcopen.step):
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   327
                    steps.append(instance)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   328
                elif isinstance(instance, plcopen.selectionDivergence):
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   329
                    step = self.ExtractDivergenceInput(instance, pou)
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   330
                    if step:
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   331
                        if isinstance(step, plcopen.step):
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   332
                            steps.append(step)
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   333
                        elif isinstance(step, plcopen.simultaneousConvergence):
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   334
                            steps.extend(self.ExtractConvergenceInputs(step, pou))
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   335
                elif isinstance(instance, plcopen.simultaneousConvergence):
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   336
                    steps.extend(self.ExtractConvergenceInputs(instance, pou))
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   337
            transition_infos = {"from": [], "to" : []}
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   338
            transitionValues = transition.getConditionContent()
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   339
            if transitionValues["type"] == "inline":
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   340
                transition_infos["content"] = "\n    := %s;\n"%transitionValues["value"]
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   341
            else:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   342
                transitionContent = pou.getTransition(transitionValues["value"])
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   343
                transitionType = transitionContent.getBodyType()
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   344
                transitionBody = transitionContent.getBody()
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   345
                if transitionType == "IL":
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   346
                    transition_infos["content"] = ":\n%s"%ReIndentText(transitionBody.getText(), 4)
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   347
                elif transitionType == "ST":
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   348
                    transition_infos["content"] = "\n%s"%ReIndentText(transitionBody.getText(), 4)
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   349
                elif conditionType == "FBD":
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   350
                    for instance in transitionBody.getContentInstances():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   351
                        if isinstance(instance, plcopen.outVariable):
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   352
                            connections = instance.connectionPointIn.getConnections()
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   353
                            if connections and len(connections) == 1:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   354
                                expression = self.ComputeFBDExpression(actionBody, connections[0])
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   355
                                transition_infos["content"] = "\n    := %s;\n"%(var, expression)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   356
                elif actionType == "LD":
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   357
                    for instance in transitionbody.getContentInstances():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   358
                        if isinstance(instance, plcopen.coil):
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   359
                            paths = self.GenerateLDPaths(instance, conditionBody)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   360
                            expression = self.ComputeLDExpression(paths, True)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   361
                            transition_infos["content"] = "\n    := %s;\n"%expression
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   362
            for step in steps:
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   363
                self.GenerateSFCStep(step, pou)
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   364
                step_name = step.getName()
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   365
                if step_name in self.SFCNetworks["Steps"].keys():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   366
                    transition_infos["from"].append(step_name)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   367
                    self.SFCNetworks["Steps"][step_name]["transitions"].append(transition)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   368
            self.SFCNetworks["Transitions"][transition] = transition_infos
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   369
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   370
    def ComputeSFCStep(self, step_name):
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   371
        if step_name in self.SFCNetworks["Steps"].keys():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   372
            step_infos = self.SFCNetworks["Steps"].pop(step_name)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   373
            if step_infos["initial"]:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   374
                self.Program += "  INITIAL_STEP %s:\n"%step_name
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   375
            else:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   376
                self.Program += "  STEP %s:\n"%step_name
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   377
            actions = []
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   378
            for action_infos in step_infos["actions"]:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   379
                actions.append(action_infos["content"])
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   380
                self.Program += "    %(content)s(%(qualifier)s"%action_infos
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   381
                if "duration" in action_infos:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   382
                    self.Program += ", %(duration)s"%action_infos
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   383
                if "indicator" in action_infos:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   384
                    self.Program += ", %(indicator)s"%action_infos
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   385
                self.Program += ");\n"
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   386
            self.Program += "  END_STEP\n\n"
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   387
            for action in actions:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   388
                self.ComputeSFCAction(action)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   389
            for transition in step_infos["transitions"]:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   390
                self.ComputeSFCTransition(transition)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   391
                
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   392
    def ComputeSFCAction(self, action_name):
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   393
        if action_name in self.SFCNetworks["Actions"].keys():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   394
            action_content = self.SFCNetworks["Actions"].pop(action_name)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   395
            self.Program += "  ACTION %s:\n%s  END_ACTION\n\n"%(action_name, action_content)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   396
    
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   397
    def ComputeSFCTransition(self, transition):
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   398
        if transition in self.SFCNetworks["Transitions"].keys():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   399
            transition_infos = self.SFCNetworks["Transitions"].pop(transition)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   400
            self.Program += "  TRANSITION FROM "
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   401
            if len(transition_infos["from"]) > 1:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   402
                self.Program += "(%s)"%", ".join(transition_infos["from"])
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   403
            else:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   404
                self.Program += "%s"%transition_infos["from"][0]
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   405
            self.Program += " TO "
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   406
            if len(transition_infos["to"]) > 1:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   407
                self.Program += "(%s)"%", ".join(transition_infos["to"])
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   408
            else:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   409
                self.Program += "%s"%transition_infos["to"][0]
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   410
            self.Program += transition_infos["content"]
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   411
            self.Program += "  END_TRANSITION\n\n"
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   412
            for step_name in transition_infos["to"]:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   413
                self.ComputeSFCStep(step_name)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   414
        
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   415
    def ComputeLDExpression(self, paths, first = False):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   416
        if type(paths) == TupleType:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   417
            if None in paths:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   418
                return "TRUE"
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   419
            else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   420
                var = [self.ComputeLDExpression(path) for path in paths]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   421
                if first:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   422
                    return " OR ".join(var)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   423
                else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   424
                    return "(%s)"%" OR ".join(var)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   425
        elif type(paths) == ListType:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   426
            var = [self.ComputeLDExpression(path) for path in paths]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   427
            return " AND ".join(var)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   428
        else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   429
            return paths
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   430
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   431
    def ExtractModifier(self, variable, text):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   432
        if variable.getNegated():
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   433
            return "NOT(%s)"%text
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   434
        else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   435
            edge = variable.getEdge()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   436
            if edge:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   437
                if edge.getValue() == "rising":
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   438
                    return self.AddTrigger("R_TRIG", text)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   439
                elif edge.getValue() == "falling":
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   440
                    return self.AddTrigger("F_TRIG", text)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   441
        return text
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   442
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   443
    def AddTrigger(self, edge, text):
33
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
   444
        if self.Interface[-1][0] != "VAR" or self.Interface[-1][1] or self.Interface[-1][2] or self.Interface[-1][3]:
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
   445
            self.Interface.append(("VAR", False, False, False, []))
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   446
        i = 1
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   447
        name = "%s%d"%(edge, i)
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   448
        while self.IsAlreadyDefined(name):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   449
            i += 1
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   450
            name = "%s%d"%(edge, i)
33
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
   451
        self.Interface[-1][4].append((edge, name, None, None))
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   452
        self.Program += "  %s(CLK := %s);\n"%(name, text)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   453
        return "%s.Q"%name
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   454
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   455
    def GenerateSTProgram(self):
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   456
        if self.ReturnType:
29
3b7e23a323a6 Adding support for configuration generation
lbessard
parents: 28
diff changeset
   457
            program = "%s %s : %s\n"%(self.Type, self.Name, self.ReturnType)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   458
        else:
29
3b7e23a323a6 Adding support for configuration generation
lbessard
parents: 28
diff changeset
   459
            program = "%s %s\n"%(self.Type, self.Name)
33
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
   460
        for list_type, retain, constant, located, variables in self.Interface:
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   461
            program += "  %s"%list_type
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   462
            if retain:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   463
                program += " RETAIN"
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   464
            if constant:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   465
                program += " CONSTANT"
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   466
            program += "\n"
30
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   467
            for var_type, var_name, var_address, var_initial in variables:
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   468
                program += "    %s "%var_name
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   469
                if var_address != None:
31
d833bf7567b1 Bug on variable location fixed
lbessard
parents: 30
diff changeset
   470
                    program += "AT %s "%var_address
30
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   471
                program += ": %s"%var_type
28
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 6
diff changeset
   472
                if var_initial != None:
33
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
   473
                    value = {"TRUE":"0","FALSE":"1"}.get(str(var_initial).upper(), str(var_initial))
37
256eedd275d0 Adding support for disable interface variable change if a POU is used
lbessard
parents: 33
diff changeset
   474
                    if var_type == "STRING":
256eedd275d0 Adding support for disable interface variable change if a POU is used
lbessard
parents: 33
diff changeset
   475
                        program += " := '%s'"%value
256eedd275d0 Adding support for disable interface variable change if a POU is used
lbessard
parents: 33
diff changeset
   476
                    elif var_type == "WSTRING":
33
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
   477
                        program += " := \"%s\""%value
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
   478
                    else:
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
   479
                        program += " := %s"%value
30
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   480
                program += ";\n"
4
2de7fd952fdd Adding File Dialog for choosing path to generated program
lbessard
parents: 2
diff changeset
   481
            program += "  END_VAR\n"
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   482
        program += "\n"
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   483
        program += self.Program
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   484
        program += "END_%s\n\n"%self.Type
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   485
        return program
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   486
    
29
3b7e23a323a6 Adding support for configuration generation
lbessard
parents: 28
diff changeset
   487
    def GenerateConfiguration(self, configuration):
3b7e23a323a6 Adding support for configuration generation
lbessard
parents: 28
diff changeset
   488
        config = "\nCONFIGURATION %s\n"%configuration.getName()
3b7e23a323a6 Adding support for configuration generation
lbessard
parents: 28
diff changeset
   489
        for varlist in configuration.getGlobalVars():
3b7e23a323a6 Adding support for configuration generation
lbessard
parents: 28
diff changeset
   490
            config += "  VAR_GLOBAL"
3b7e23a323a6 Adding support for configuration generation
lbessard
parents: 28
diff changeset
   491
            if varlist.getRetain():
3b7e23a323a6 Adding support for configuration generation
lbessard
parents: 28
diff changeset
   492
                config += " RETAIN"
3b7e23a323a6 Adding support for configuration generation
lbessard
parents: 28
diff changeset
   493
            if varlist.getConstant():
3b7e23a323a6 Adding support for configuration generation
lbessard
parents: 28
diff changeset
   494
                config += " CONSTANT"
3b7e23a323a6 Adding support for configuration generation
lbessard
parents: 28
diff changeset
   495
            config += "\n"
3b7e23a323a6 Adding support for configuration generation
lbessard
parents: 28
diff changeset
   496
            for var in varlist.getVariable():
33
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
   497
                var_type = var.getType().getValue()
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
   498
                config += "    %s "%var.getName()
30
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   499
                address = var.getAddress()
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   500
                if address:
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   501
                    config += "AT %s "%address
33
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
   502
                config += ": %s"%var_type
29
3b7e23a323a6 Adding support for configuration generation
lbessard
parents: 28
diff changeset
   503
                initial = var.getInitialValue()
3b7e23a323a6 Adding support for configuration generation
lbessard
parents: 28
diff changeset
   504
                if initial:
30
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   505
                    value = str(initial.getValue())
33
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
   506
                    value = {"TRUE":"0","FALSE":"1"}.get(value.upper(), value)
37
256eedd275d0 Adding support for disable interface variable change if a POU is used
lbessard
parents: 33
diff changeset
   507
                    if var_type == "STRING":
256eedd275d0 Adding support for disable interface variable change if a POU is used
lbessard
parents: 33
diff changeset
   508
                        config += " := '%s'"%value
256eedd275d0 Adding support for disable interface variable change if a POU is used
lbessard
parents: 33
diff changeset
   509
                    elif var_type == "WSTRING":
33
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
   510
                        config += " := \"%s\""%value
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
   511
                    else:
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
   512
                        config += " := %s"%value
30
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   513
                config += ";\n"
29
3b7e23a323a6 Adding support for configuration generation
lbessard
parents: 28
diff changeset
   514
            config += "  END_VAR\n"
3b7e23a323a6 Adding support for configuration generation
lbessard
parents: 28
diff changeset
   515
        for resource in configuration.getResource():
3b7e23a323a6 Adding support for configuration generation
lbessard
parents: 28
diff changeset
   516
            config += self.GenerateResource(resource)
3b7e23a323a6 Adding support for configuration generation
lbessard
parents: 28
diff changeset
   517
        config += "END_CONFIGURATION\n"
3b7e23a323a6 Adding support for configuration generation
lbessard
parents: 28
diff changeset
   518
        return config
3b7e23a323a6 Adding support for configuration generation
lbessard
parents: 28
diff changeset
   519
        
3b7e23a323a6 Adding support for configuration generation
lbessard
parents: 28
diff changeset
   520
    def GenerateResource(self, resource):
33
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
   521
        resrce = "\n  RESOURCE %s ON BEREMIZ\n"%resource.getName()
29
3b7e23a323a6 Adding support for configuration generation
lbessard
parents: 28
diff changeset
   522
        for varlist in resource.getGlobalVars():
3b7e23a323a6 Adding support for configuration generation
lbessard
parents: 28
diff changeset
   523
            resrce += "    VAR_GLOBAL"
3b7e23a323a6 Adding support for configuration generation
lbessard
parents: 28
diff changeset
   524
            if varlist.getRetain():
3b7e23a323a6 Adding support for configuration generation
lbessard
parents: 28
diff changeset
   525
                resrce += " RETAIN"
3b7e23a323a6 Adding support for configuration generation
lbessard
parents: 28
diff changeset
   526
            if varlist.getConstant():
3b7e23a323a6 Adding support for configuration generation
lbessard
parents: 28
diff changeset
   527
                resrce += " CONSTANT"
3b7e23a323a6 Adding support for configuration generation
lbessard
parents: 28
diff changeset
   528
            resrce += "\n"
3b7e23a323a6 Adding support for configuration generation
lbessard
parents: 28
diff changeset
   529
            for var in varlist.getVariable():
33
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
   530
                var_type = var.getType().getValue()
30
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   531
                resrce += "      %s "%var.getName()
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   532
                address = var.getAddress()
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   533
                if address:
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   534
                    resrce += "AT %s "%address
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   535
                resrce += ": %s"%var.getType().getValue()
29
3b7e23a323a6 Adding support for configuration generation
lbessard
parents: 28
diff changeset
   536
                initial = var.getInitialValue()
3b7e23a323a6 Adding support for configuration generation
lbessard
parents: 28
diff changeset
   537
                if initial:
30
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   538
                    value = str(initial.getValue())
33
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
   539
                    value = {"TRUE":"0","FALSE":"1"}.get(value.upper(), value)
37
256eedd275d0 Adding support for disable interface variable change if a POU is used
lbessard
parents: 33
diff changeset
   540
                    if var_type == "STRING":
256eedd275d0 Adding support for disable interface variable change if a POU is used
lbessard
parents: 33
diff changeset
   541
                        resrce += " := '%s'"%value
256eedd275d0 Adding support for disable interface variable change if a POU is used
lbessard
parents: 33
diff changeset
   542
                    elif var_type == "WSTRING":
33
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
   543
                        resrce += " := \"%s\""%value
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
   544
                    else:
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
   545
                        resrce += " := %s"%value
30
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   546
                resrce += ";\n"
29
3b7e23a323a6 Adding support for configuration generation
lbessard
parents: 28
diff changeset
   547
            resrce += "    END_VAR\n"
30
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   548
        tasks = resource.getTask()
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   549
        for task in tasks:
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   550
            resrce += "    TASK %s("%task.getName()
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   551
            args = []
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   552
            single = task.getSingle()
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   553
            if single:
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   554
                args.append("SINGLE := %s"%single)
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   555
            interval = task.getInterval()
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   556
            if interval:
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   557
                text = "t#"
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   558
                if interval.hour != 0:
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   559
                    text += "%dh"%interval.hour
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   560
                if interval.minute != 0:
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   561
                    text += "%dm"%interval.minute
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   562
                if interval.second != 0:
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   563
                    text += "%ds"%interval.second
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   564
                if interval.microsecond != 0:
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   565
                    text += "%dms"%(interval.microsecond / 1000)
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   566
                args.append("INTERVAL := %s"%text)
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   567
            args.append("PRIORITY := %s"%str(task.priority.getValue()))
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   568
            resrce += ",".join(args) + ");\n"
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   569
        for task in tasks:
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   570
            for instance in task.getPouInstance():
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   571
                resrce += "    PROGRAM %s WITH %s : %s;\n"%(instance.getName(), task.getName(), instance.getType())
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   572
        for instance in resource.getPouInstance():
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   573
            resrce += "    PROGRAM %s : %s;\n"%(instance.getName(), instance.getType())
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   574
        resrce += "  END_RESOURCE\n"
29
3b7e23a323a6 Adding support for configuration generation
lbessard
parents: 28
diff changeset
   575
        return resrce
3b7e23a323a6 Adding support for configuration generation
lbessard
parents: 28
diff changeset
   576
        
3b7e23a323a6 Adding support for configuration generation
lbessard
parents: 28
diff changeset
   577
            
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   578
def GenerateCurrentProgram(project):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   579
    program = ""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   580
    for pou in project.getPous():
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   581
        pou_type = pou.getPouType().getValue()
6
c8cf918ee7ea Bug corrected and improvements
lbessard
parents: 5
diff changeset
   582
        if pou_type in pouTypeNames:
c8cf918ee7ea Bug corrected and improvements
lbessard
parents: 5
diff changeset
   583
            pou_program = PouProgram(pou.getName(), pouTypeNames[pou_type])
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   584
        else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   585
            raise ValueError, "Undefined pou type"
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   586
        pou_program.GenerateInterface(pou.getInterface())
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   587
        pou_program.GenerateProgram(pou)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   588
        program += pou_program.GenerateSTProgram()
29
3b7e23a323a6 Adding support for configuration generation
lbessard
parents: 28
diff changeset
   589
    for config in project.getConfigurations():
3b7e23a323a6 Adding support for configuration generation
lbessard
parents: 28
diff changeset
   590
        program += pou_program.GenerateConfiguration(config)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   591
    return program
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   592