PLCGenerator.py
author lbessard
Wed, 02 Jan 2008 18:16:12 +0100
changeset 141 c0242a51774c
parent 128 d16a8df4d322
child 151 aaa80b48bead
permissions -rw-r--r--
Bug with String not recognized fixed
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
71
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 70
diff changeset
    35
currentProject = None
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 70
diff changeset
    36
currentProgram = ""
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    37
datatypeComputed = {}
71
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 70
diff changeset
    38
pouComputed = {}
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 70
diff changeset
    39
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    40
def ReIndentText(text, nb_spaces):
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    41
    compute = ""
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    42
    lines = text.splitlines()
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    43
    if len(lines) > 0:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    44
        spaces = 0
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    45
        while lines[0][spaces] == " ":
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    46
            spaces += 1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    47
        indent = ""
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    48
        for i in xrange(spaces, nb_spaces):
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    49
            indent += " "
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    50
        for line in lines:
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
    51
            if line != "":
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
    52
                compute += "%s%s\n"%(indent, line)
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
    53
            else:
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
    54
                compute += "\n"
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    55
    return compute
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
    56
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    57
def GenerateDataType(datatype_name):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    58
    if not datatypeComputed.get(datatype_name, True):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    59
        datatypeComputed[datatype_name] = True
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    60
        global currentProject, currentProgram
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    61
        datatype = currentProject.getDataType(datatype_name)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    62
        datatype_def = "  %s :"%datatype.getName()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    63
        basetype_content = datatype.baseType.getContent()
141
c0242a51774c Bug with String not recognized fixed
lbessard
parents: 128
diff changeset
    64
        if basetype_content["name"] in ["string", "wstring"]:
c0242a51774c Bug with String not recognized fixed
lbessard
parents: 128
diff changeset
    65
            datatype_def += " %s"%basetype_content["name"].upper()
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    66
        elif basetype_content["name"] == "derived":
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    67
            basetype_name = basetype_content["value"].getName()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    68
            GenerateDataType(basetype_name)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    69
            datatype_def += " %s"%basetype_name
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    70
        elif basetype_content["name"] in ["subrangeSigned", "subrangeUnsigned"]:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    71
            base_type = basetype_content["value"].baseType.getContent()
141
c0242a51774c Bug with String not recognized fixed
lbessard
parents: 128
diff changeset
    72
            if base_type["name"] == "derived":
c0242a51774c Bug with String not recognized fixed
lbessard
parents: 128
diff changeset
    73
                basetype_name = base_type["value"].getName()
c0242a51774c Bug with String not recognized fixed
lbessard
parents: 128
diff changeset
    74
            else:
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    75
                basetype_name = base_type["name"]
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    76
                GenerateDataType(basetype_name)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    77
            min_value = basetype_content["value"].range.getLower()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    78
            max_value = basetype_content["value"].range.getUpper()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    79
            datatype_def += " %s (%d..%d)"%(basetype_name, min_value, max_value)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    80
        elif basetype_content["name"] == "enum":
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    81
            values = []
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    82
            for value in basetype_content["value"].values.getValue():
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    83
                values.append(value.getName())
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    84
            datatype_def += " (%s)"%", ".join(values)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    85
        elif basetype_content["name"] == "array":
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    86
            base_type = basetype_content["value"].baseType.getContent()
141
c0242a51774c Bug with String not recognized fixed
lbessard
parents: 128
diff changeset
    87
            if base_type["name"] == "derived":
c0242a51774c Bug with String not recognized fixed
lbessard
parents: 128
diff changeset
    88
                basetype_name = base_type["value"].getName()
c0242a51774c Bug with String not recognized fixed
lbessard
parents: 128
diff changeset
    89
            elif base_type["name"] in ["string", "wstring"]:
c0242a51774c Bug with String not recognized fixed
lbessard
parents: 128
diff changeset
    90
                basetype_name = base_type["name"].upper()
c0242a51774c Bug with String not recognized fixed
lbessard
parents: 128
diff changeset
    91
            else:
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    92
                basetype_name = base_type["name"]
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    93
                GenerateDataType(basetype_name)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    94
            dimensions = []
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    95
            for dimension in basetype_content["value"].getDimension():
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    96
                dimensions.append("0..%d"%(dimension.getUpper() - 1))
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
    97
            datatype_def += " ARRAY [%s] OF %s"%(",".join(dimensions), basetype_name)
141
c0242a51774c Bug with String not recognized fixed
lbessard
parents: 128
diff changeset
    98
        else:
c0242a51774c Bug with String not recognized fixed
lbessard
parents: 128
diff changeset
    99
            datatype_def += " %s"%basetype_content["name"]
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   100
        if datatype.initialValue is not None:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   101
            datatype_def += " := %s"%str(datatype.initialValue.getValue())
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   102
        currentProgram += "%s;\n"%datatype_def
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   103
78
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   104
def GeneratePouProgram(pou_name):
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   105
    if not pouComputed.get(pou_name, True):
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   106
        pouComputed[pou_name] = True
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   107
        global currentProject, currentProgram
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   108
        pou = currentProject.getPou(pou_name)
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   109
        pou_type = pou.getPouType().getValue()
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   110
        if pou_type in pouTypeNames:
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   111
            pou_program = PouProgram(pou.getName(), pouTypeNames[pou_type])
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   112
        else:
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   113
            raise ValueError, "Undefined pou type"
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   114
        pou_program.GenerateInterface(pou.getInterface())
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   115
        pou_program.GenerateConnectionTypes(pou)
78
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   116
        pou_program.GenerateProgram(pou)
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   117
        currentProgram += pou_program.GenerateSTProgram()
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   118
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   119
def GenerateConfiguration(configuration):
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   120
    config = "\nCONFIGURATION %s\n"%configuration.getName()
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   121
    for varlist in configuration.getGlobalVars():
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   122
        config += "  VAR_GLOBAL"
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   123
        if varlist.getRetain():
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   124
            config += " RETAIN"
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   125
        if varlist.getConstant():
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   126
            config += " CONSTANT"
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   127
        config += "\n"
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   128
        for var in varlist.getVariable():
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   129
            vartype_content = var.getType().getContent()
141
c0242a51774c Bug with String not recognized fixed
lbessard
parents: 128
diff changeset
   130
            if vartype_content["name"] == "derived":
c0242a51774c Bug with String not recognized fixed
lbessard
parents: 128
diff changeset
   131
                var_type = vartype_content["value"].getName()
c0242a51774c Bug with String not recognized fixed
lbessard
parents: 128
diff changeset
   132
            elif vartype_content["name"] in ["string", "wstring"]:
c0242a51774c Bug with String not recognized fixed
lbessard
parents: 128
diff changeset
   133
                var_type = vartype_content["name"].upper()
c0242a51774c Bug with String not recognized fixed
lbessard
parents: 128
diff changeset
   134
            else:
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   135
                var_type = vartype_content["name"]
78
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   136
            config += "    %s "%var.getName()
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   137
            address = var.getAddress()
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   138
            if address:
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   139
                config += "AT %s "%address
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   140
            config += ": %s"%var_type
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   141
            initial = var.getInitialValue()
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   142
            if initial:
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   143
                value = str(initial.getValue())
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   144
                value = {"TRUE":"0","FALSE":"1"}.get(value.upper(), value)
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   145
                if var_type == "STRING":
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   146
                    config += " := '%s'"%value
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   147
                elif var_type == "WSTRING":
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   148
                    config += " := \"%s\""%value
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   149
                else:
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   150
                    config += " := %s"%value
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   151
            config += ";\n"
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   152
        config += "  END_VAR\n"
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   153
    for resource in configuration.getResource():
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   154
        config += GenerateResource(resource)
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   155
    config += "END_CONFIGURATION\n"
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   156
    return config
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   157
    
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   158
def GenerateResource(resource):
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   159
    resrce = "\n  RESOURCE %s ON BEREMIZ\n"%resource.getName()
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   160
    for varlist in resource.getGlobalVars():
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   161
        resrce += "    VAR_GLOBAL"
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   162
        if varlist.getRetain():
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   163
            resrce += " RETAIN"
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   164
        if varlist.getConstant():
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   165
            resrce += " CONSTANT"
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   166
        resrce += "\n"
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   167
        for var in varlist.getVariable():
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   168
            vartype_content = var.getType().getContent()
141
c0242a51774c Bug with String not recognized fixed
lbessard
parents: 128
diff changeset
   169
            if vartype_content["name"] == "derived":
c0242a51774c Bug with String not recognized fixed
lbessard
parents: 128
diff changeset
   170
                var_type = vartype_content["value"].getName()
c0242a51774c Bug with String not recognized fixed
lbessard
parents: 128
diff changeset
   171
            elif vartype_content["name"] in ["string", "wstring"]:
c0242a51774c Bug with String not recognized fixed
lbessard
parents: 128
diff changeset
   172
                var_type = vartype_content["name"].upper()
c0242a51774c Bug with String not recognized fixed
lbessard
parents: 128
diff changeset
   173
            else:
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   174
                var_type = vartype_content["name"]
78
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   175
            resrce += "      %s "%var.getName()
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   176
            address = var.getAddress()
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   177
            if address:
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   178
                resrce += "AT %s "%address
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   179
            resrce += ": %s"%var_type
78
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   180
            initial = var.getInitialValue()
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   181
            if initial:
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   182
                value = str(initial.getValue())
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   183
                value = {"TRUE":"0","FALSE":"1"}.get(value.upper(), value)
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   184
                if var_type == "STRING":
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   185
                    resrce += " := '%s'"%value
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   186
                elif var_type == "WSTRING":
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   187
                    resrce += " := \"%s\""%value
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   188
                else:
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   189
                    resrce += " := %s"%value
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   190
            resrce += ";\n"
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   191
        resrce += "    END_VAR\n"
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   192
    tasks = resource.getTask()
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   193
    for task in tasks:
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   194
        resrce += "    TASK %s("%task.getName()
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   195
        args = []
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   196
        single = task.getSingle()
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   197
        if single:
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   198
            args.append("SINGLE := %s"%single)
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   199
        interval = task.getInterval()
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   200
        if interval:
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   201
            text = "t#"
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   202
            if interval.hour != 0:
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   203
                text += "%dh"%interval.hour
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   204
            if interval.minute != 0:
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   205
                text += "%dm"%interval.minute
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   206
            if interval.second != 0:
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   207
                text += "%ds"%interval.second
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   208
            if interval.microsecond != 0:
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   209
                text += "%dms"%(interval.microsecond / 1000)
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   210
            args.append("INTERVAL := %s"%text)
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   211
        args.append("PRIORITY := %s"%str(task.priority.getValue()))
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   212
        resrce += ",".join(args) + ");\n"
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   213
    for task in tasks:
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   214
        for instance in task.getPouInstance():
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   215
            resrce += "    PROGRAM %s WITH %s : %s;\n"%(instance.getName(), task.getName(), instance.getType())
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   216
    for instance in resource.getPouInstance():
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   217
        resrce += "    PROGRAM %s : %s;\n"%(instance.getName(), instance.getType())
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   218
    resrce += "  END_RESOURCE\n"
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   219
    return resrce
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   220
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   221
"""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   222
Module implementing methods for generating PLC programs in ST or IL
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   223
"""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   224
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   225
class PouProgram:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   226
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   227
    def __init__(self, name, type):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   228
        self.Name = name
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   229
        self.Type = type
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   230
        self.ReturnType = None
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   231
        self.Interface = []
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   232
        self.InitialSteps = []
72
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   233
        self.ComputedBlocks = {}
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   234
        self.ComputedConnectors = {}
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   235
        self.ConnectionTypes = {}
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   236
        self.RelatedConnections = []
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   237
        self.SFCNetworks = {"Steps":{}, "Transitions":{}, "Actions":{}}
72
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   238
        self.SFCComputedBlocks = ""
46
4379e98a30aa Bug on SFC generation fixed
lbessard
parents: 37
diff changeset
   239
        self.ActionNumber = 0
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   240
        self.Program = ""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   241
    
46
4379e98a30aa Bug on SFC generation fixed
lbessard
parents: 37
diff changeset
   242
    def GetActionNumber(self):
4379e98a30aa Bug on SFC generation fixed
lbessard
parents: 37
diff changeset
   243
        self.ActionNumber += 1
4379e98a30aa Bug on SFC generation fixed
lbessard
parents: 37
diff changeset
   244
        return self.ActionNumber
4379e98a30aa Bug on SFC generation fixed
lbessard
parents: 37
diff changeset
   245
    
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   246
    def IsAlreadyDefined(self, name):
33
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
   247
        for list_type, retain, constant, located, vars in self.Interface:
30
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   248
            for var_type, var_name, var_address, var_initial in vars:
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   249
                if name == var_name:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   250
                    return True
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   251
        return False
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   252
    
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   253
    def GetVariableType(self, name):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   254
        for list_type, retain, constant, located, vars in self.Interface:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   255
            for var_type, var_name, var_address, var_initial in vars:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   256
                if name == var_name:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   257
                    return var_type
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   258
        return None
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   259
    
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   260
    def GetConnectedConnection(self, connection, body):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   261
        links = connection.getConnections()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   262
        if links and len(links) == 1:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   263
            return self.GetLinkedConnection(links[0], body)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   264
        return None
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   265
        
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   266
    def GetLinkedConnection(self, link, body):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   267
        parameter = link.getFormalParameter()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   268
        instance = body.getContentInstance(link.getRefLocalId())
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   269
        if isinstance(instance, (plcopen.inVariable, plcopen.inOutVariable, plcopen.continuation, plcopen.contact, plcopen.coil)):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   270
            return instance.connectionPointOut
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   271
        elif isinstance(instance, plcopen.block):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   272
            outputvariables = instance.outputVariables.getVariable()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   273
            if len(outputvariables) == 1:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   274
                return outputvariables[0].connectionPointOut
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   275
            elif parameter:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   276
                for variable in outputvariables:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   277
                    if variable.getFormalParameter() == parameter:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   278
                        return variable.connectionPointOut
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   279
            else:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   280
                point = link.getPosition()[-1]
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   281
                for variable in outputvariables:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   282
                    relposition = variable.connectionPointOut.getRelPosition()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   283
                    blockposition = instance.getPosition()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   284
                    if point.x == blockposition.x + relposition[0] and point.y == blockposition.y + relposition[1]:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   285
                        return variable.connectionPointOut
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   286
        elif isinstance(instance, plcopen.leftPowerRail):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   287
            outputconnections = instance.getConnectionPointOut()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   288
            if len(outputconnections) == 1:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   289
                return outputconnections[0]
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   290
            else:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   291
                point = link.getPosition()[-1]
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   292
                for outputconnection in outputconnections:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   293
                    relposition = outputconnection.getRelPosition()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   294
                    powerrailposition = instance.getPosition()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   295
                    if point.x == powerrailposition.x + relposition[0] and point.y == powerrailposition.y + relposition[1]:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   296
                        return outputconnection
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   297
        return None
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   298
        
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   299
    def ExtractRelatedConnections(self, connection):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   300
        for i, related in enumerate(self.RelatedConnections):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   301
            if connection in related:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   302
                return self.RelatedConnections.pop(i)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   303
        return [connection]
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   304
    
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   305
    def GenerateInterface(self, interface):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   306
        if self.Type == "FUNCTION":
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   307
            returntype_content = interface.getReturnType().getContent()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   308
            if returntype_content["value"] is None:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   309
                self.ReturnType = returntype_content["name"]
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   310
            else:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   311
                self.ReturnType = returntype_content["value"].getName()
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   312
        for varlist in interface.getContent():
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   313
            variables = []
33
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
   314
            located = False
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   315
            for var in varlist["value"].getVariable():
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   316
                vartype_content = var.getType().getContent()
141
c0242a51774c Bug with String not recognized fixed
lbessard
parents: 128
diff changeset
   317
                if vartype_content["name"] == "derived":
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   318
                    var_type = vartype_content["value"].getName()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   319
                    GeneratePouProgram(var_type)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   320
                    blocktype = GetBlockType(var_type)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   321
                    if blocktype is not None:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   322
                        variables.extend(blocktype["initialise"](var_type, var.getName()))
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   323
                        located = False
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   324
                    else:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   325
                        initial = var.getInitialValue()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   326
                        if initial:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   327
                            initial_value = initial.getValue()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   328
                        else:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   329
                            initial_value = None
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   330
                        address = var.getAddress()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   331
                        if address:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   332
                            located = True
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   333
                        variables.append((vartype_content["value"].getName(), var.getName(), address, initial_value))
141
c0242a51774c Bug with String not recognized fixed
lbessard
parents: 128
diff changeset
   334
                else:
c0242a51774c Bug with String not recognized fixed
lbessard
parents: 128
diff changeset
   335
                    initial = var.getInitialValue()
c0242a51774c Bug with String not recognized fixed
lbessard
parents: 128
diff changeset
   336
                    if initial:
c0242a51774c Bug with String not recognized fixed
lbessard
parents: 128
diff changeset
   337
                        initial_value = initial.getValue()
c0242a51774c Bug with String not recognized fixed
lbessard
parents: 128
diff changeset
   338
                    else:
c0242a51774c Bug with String not recognized fixed
lbessard
parents: 128
diff changeset
   339
                        initial_value = None
c0242a51774c Bug with String not recognized fixed
lbessard
parents: 128
diff changeset
   340
                    address = var.getAddress()
c0242a51774c Bug with String not recognized fixed
lbessard
parents: 128
diff changeset
   341
                    if address:
c0242a51774c Bug with String not recognized fixed
lbessard
parents: 128
diff changeset
   342
                        located = True
c0242a51774c Bug with String not recognized fixed
lbessard
parents: 128
diff changeset
   343
                    if vartype_content["name"] in ["string", "wstring"]:
c0242a51774c Bug with String not recognized fixed
lbessard
parents: 128
diff changeset
   344
                        variables.append((vartype_content["name"].upper(), var.getName(), address, initial_value))
c0242a51774c Bug with String not recognized fixed
lbessard
parents: 128
diff changeset
   345
                    else:
c0242a51774c Bug with String not recognized fixed
lbessard
parents: 128
diff changeset
   346
                        variables.append((vartype_content["name"], var.getName(), address, initial_value))
93
c3c24b979a4d Add support for custom block declaration
lbessard
parents: 80
diff changeset
   347
            if len(variables) > 0:
c3c24b979a4d Add support for custom block declaration
lbessard
parents: 80
diff changeset
   348
                self.Interface.append((varTypeNames[varlist["name"]], varlist["value"].getRetain(), 
33
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
   349
                            varlist["value"].getConstant(), located, variables))
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   350
    
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   351
    def GenerateConnectionTypes(self, pou):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   352
        body = pou.getBody()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   353
        body_content = body.getContent()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   354
        body_type = body_content["name"]
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   355
        if body_type in ["FBD", "LD", "SFC"]:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   356
            for instance in body.getContentInstances():
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   357
                if isinstance(instance, (plcopen.inVariable, plcopen.outVariable, plcopen.inOutVariable)):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   358
                    expression = instance.getExpression()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   359
                    var_type = self.GetVariableType(expression)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   360
                    if expression == pou.getName():
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   361
                        returntype_content = pou.interface.getReturnType().getContent()
141
c0242a51774c Bug with String not recognized fixed
lbessard
parents: 128
diff changeset
   362
                        if returntype_content["name"] == "derived":
c0242a51774c Bug with String not recognized fixed
lbessard
parents: 128
diff changeset
   363
                            var_type = returntype_content["value"].getName()
c0242a51774c Bug with String not recognized fixed
lbessard
parents: 128
diff changeset
   364
                        elif returntype_content["name"] in ["string", "wstring"]:
c0242a51774c Bug with String not recognized fixed
lbessard
parents: 128
diff changeset
   365
                            var_type = returntype_content["name"].upper()
c0242a51774c Bug with String not recognized fixed
lbessard
parents: 128
diff changeset
   366
                        else:
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   367
                            var_type = returntype_content["name"]
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   368
                    elif var_type is None:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   369
                        var_type = expression.split("#")[0]
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   370
                    if isinstance(instance, (plcopen.inVariable, plcopen.inOutVariable)):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   371
                        self.ConnectionTypes[instance.connectionPointOut] = var_type
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   372
                    if isinstance(instance, (plcopen.outVariable, plcopen.inOutVariable)):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   373
                        self.ConnectionTypes[instance.connectionPointIn] = var_type
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   374
                        connected = self.GetConnectedConnection(instance.connectionPointIn, body)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   375
                        if connected and connected not in self.ConnectionTypes:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   376
                            for connection in self.ExtractRelatedConnections(connected):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   377
                                self.ConnectionTypes[connection] = var_type
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   378
                elif isinstance(instance, (plcopen.contact, plcopen.coil)):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   379
                    self.ConnectionTypes[instance.connectionPointOut] = "BOOL"
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   380
                    self.ConnectionTypes[instance.connectionPointIn] = "BOOL"
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   381
                    connected = self.GetConnectedConnection(instance.connectionPointIn, body)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   382
                    if connected and connected not in self.ConnectionTypes:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   383
                        for connection in self.ExtractRelatedConnections(connected):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   384
                            self.ConnectionTypes[connection] = "BOOL"
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   385
                elif isinstance(instance, plcopen.leftPowerRail):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   386
                    for connection in instance.getConnectionPointOut():
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   387
                        self.ConnectionTypes[connection] = "BOOL"
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   388
                elif isinstance(instance, plcopen.rightPowerRail):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   389
                    for connection in instance.getConnectionPointIn():
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   390
                        self.ConnectionTypes[connection] = "BOOL"
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   391
                        connected = self.GetConnectedConnection(connection, body)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   392
                        if connected and connected not in self.ConnectionTypes:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   393
                            for connection in self.ExtractRelatedConnections(connected):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   394
                                self.ConnectionTypes[connection] = "BOOL"
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   395
                elif isinstance(instance, plcopen.transition):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   396
                    content = instance.condition.getContent()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   397
                    if content["name"] == "connection" and len(content["value"]) == 1:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   398
                        connected = self.GetLinkedConnection(content["value"][0], body)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   399
                        if connected and connected not in self.ConnectionTypes:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   400
                            for connection in self.ExtractRelatedConnections(connected):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   401
                                self.ConnectionTypes[connection] = "BOOL"
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   402
                elif isinstance(instance, plcopen.block):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   403
                    block_infos = GetBlockType(instance.getTypeName())
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   404
                    undefined = {}
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   405
                    for variable in instance.outputVariables.getVariable():
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   406
                        output_name = variable.getFormalParameter()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   407
                        for oname, otype, oqualifier in block_infos["outputs"]:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   408
                            if output_name == oname and variable.connectionPointOut not in self.ConnectionTypes:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   409
                                if otype.startswith("ANY"):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   410
                                    if otype not in undefined:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   411
                                        undefined[otype] = []
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   412
                                    undefined[otype].append(variable.connectionPointOut)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   413
                                else:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   414
                                    for connection in self.ExtractRelatedConnections(variable.connectionPointOut):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   415
                                        self.ConnectionTypes[connection] = otype
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   416
                    for variable in instance.inputVariables.getVariable():
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   417
                        input_name = variable.getFormalParameter()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   418
                        for iname, itype, iqualifier in block_infos["inputs"]:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   419
                            if input_name == iname:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   420
                                connected = self.GetConnectedConnection(variable.connectionPointIn, body)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   421
                                if itype.startswith("ANY"):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   422
                                    if itype not in undefined:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   423
                                        undefined[itype] = []
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   424
                                    undefined[itype].append(variable.connectionPointIn)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   425
                                    if connected:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   426
                                        undefined[itype].append(connected)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   427
                                else:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   428
                                    self.ConnectionTypes[variable.connectionPointIn] = itype
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   429
                                    if connected and connected not in self.ConnectionTypes:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   430
                                        for connection in self.ExtractRelatedConnections(connected):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   431
                                            self.ConnectionTypes[connection] = itype
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   432
                    for var_type, connections in undefined.items():
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   433
                        related = []
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   434
                        for connection in connections:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   435
                            if connection in self.ConnectionTypes:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   436
                                var_type = self.ConnectionTypes[connection]
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   437
                            else:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   438
                                related.extend(self.ExtractRelatedConnections(connection))
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   439
                        if var_type.startswith("ANY") and len(related) > 0:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   440
                            self.RelatedConnections.append(related)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   441
                        else:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   442
                            for connection in related:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   443
                                self.ConnectionTypes[connection] = var_type
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   444
                                    
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   445
    def GenerateProgram(self, pou):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   446
        body = pou.getBody()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   447
        body_content = body.getContent()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   448
        body_type = body_content["name"]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   449
        if body_type in ["IL","ST"]:
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   450
            self.Program = ReIndentText(body_content["value"].getText(), 2)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   451
        elif body_type == "FBD":
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   452
            orderedInstances = []
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   453
            otherInstances = []
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   454
            for instance in body.getContentInstances():
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   455
                if isinstance(instance, (plcopen.outVariable, plcopen.inOutVariable, plcopen.block)):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   456
                    executionOrderId = instance.getExecutionOrderId()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   457
                    if executionOrderId > 0:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   458
                        orderedInstances.append((executionOrderId, instance))
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   459
                    else:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   460
                        otherInstances.append(instance)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   461
                elif isinstance(instance, plcopen.connector):
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   462
                    otherInstances.append(instance)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   463
            orderedInstances.sort()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   464
            instances = [instance for (executionOrderId, instance) in orderedInstances] + otherInstances
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   465
            for instance in instances:
72
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   466
                if isinstance(instance, (plcopen.outVariable, plcopen.inOutVariable)):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   467
                    var = instance.getExpression()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   468
                    connections = instance.connectionPointIn.getConnections()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   469
                    if connections and len(connections) == 1:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   470
                        expression = self.ComputeFBDExpression(body, connections[0])
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   471
                        self.Program += "  %s := %s;\n"%(var, expression)
78
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   472
                elif isinstance(instance, plcopen.block):
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   473
                    block_type = instance.getTypeName()
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   474
                    self.GeneratePouProgram(block_type)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   475
                    block_infos = GetBlockType(block_type)
78
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   476
                    block_infos["generate"](self, instance, body, None)
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   477
                elif isinstance(instance, plcopen.connector):
72
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   478
                    connector = instance.getName()
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   479
                    if self.ComputedConnectors.get(connector, None):
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   480
                        continue 
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   481
                    connections = instance.connectionPointIn.getConnections()
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   482
                    if connections and len(connections) == 1:
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   483
                        self.ComputedConnectors[connector] = self.ComputeFBDExpression(body, connections[0])
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   484
        elif body_type == "LD":
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   485
            for instance in body.getContentInstances():
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   486
                if isinstance(instance, plcopen.coil):
66
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   487
                    paths = self.GenerateLDPaths(instance.connectionPointIn.getConnections(), body)
104
a9b8916d906d Adding support for generation of blocks in LD
lbessard
parents: 93
diff changeset
   488
                    if len(paths) > 0:
a9b8916d906d Adding support for generation of blocks in LD
lbessard
parents: 93
diff changeset
   489
                        paths = tuple(paths)
a9b8916d906d Adding support for generation of blocks in LD
lbessard
parents: 93
diff changeset
   490
                    else:
a9b8916d906d Adding support for generation of blocks in LD
lbessard
parents: 93
diff changeset
   491
                        paths = paths[0] 
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   492
                    variable = self.ExtractModifier(instance, instance.getVariable())
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   493
                    expression = self.ComputeLDExpression(paths, True)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   494
                    self.Program += "  %s := %s;\n"%(variable, expression)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   495
        elif body_type == "SFC":
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   496
            for instance in body.getContentInstances():
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   497
                if isinstance(instance, plcopen.step):
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   498
                    self.GenerateSFCStep(instance, pou)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   499
                elif isinstance(instance, plcopen.actionBlock):
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   500
                    self.GenerateSFCStepActions(instance, pou)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   501
                elif isinstance(instance, plcopen.transition):
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   502
                    self.GenerateSFCTransition(instance, pou)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   503
                elif isinstance(instance, plcopen.jumpStep):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   504
                    self.GenerateSFCJump(instance, pou)
72
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   505
            if len(self.InitialSteps) > 0 and self.SFCComputedBlocks != "":
66
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   506
                action_name = "COMPUTE_FUNCTION_BLOCKS"
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   507
                action_infos = {"qualifier" : "S", "content" : action_name}
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   508
                self.SFCNetworks["Steps"][self.InitialSteps[0]]["actions"].append(action_infos)
72
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   509
                self.SFCNetworks["Actions"][action_name] = ReIndentText(self.SFCComputedBlocks, 4)
66
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   510
                self.Program = ""
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   511
            for initialstep in self.InitialSteps:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   512
                self.ComputeSFCStep(initialstep)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   513
    
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   514
    def ComputeFBDExpression(self, body, link, order = False):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   515
        localid = link.getRefLocalId()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   516
        instance = body.getContentInstance(localid)
72
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   517
        if isinstance(instance, (plcopen.inVariable, plcopen.inOutVariable)):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   518
            return instance.getExpression()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   519
        elif isinstance(instance, plcopen.block):
104
a9b8916d906d Adding support for generation of blocks in LD
lbessard
parents: 93
diff changeset
   520
            block_type = instance.getTypeName()
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   521
            self.GeneratePouProgram(block_type)
104
a9b8916d906d Adding support for generation of blocks in LD
lbessard
parents: 93
diff changeset
   522
            block_infos = GetBlockType(block_type)
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   523
            return block_infos["generate"](self, instance, body, link, order)
72
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   524
        elif isinstance(instance, plcopen.continuation):
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   525
            name = instance.getName()
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   526
            computed_value = self.ComputedConnectors.get(name, None)
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   527
            if computed_value != None:
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   528
                return computed_value
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   529
            for tmp_instance in body.getContentInstances():
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   530
                if isinstance(tmp_instance, plcopen.connector):
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   531
                    if tmp_instance.getName() == name:
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   532
                        connections = tmp_instance.connectionPointIn.getConnections()
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   533
                        if connections and len(connections) == 1:
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   534
                            expression = self.ComputeFBDExpression(body, connections[0], order)
72
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   535
                            self.ComputedConnectors[name] = expression
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   536
                            return expression
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   537
            raise ValueError, "No connector found"
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   538
66
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   539
    def GenerateLDPaths(self, connections, body):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   540
        paths = []
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   541
        for connection in connections:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   542
            localId = connection.getRefLocalId()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   543
            next = body.getContentInstance(localId)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   544
            if isinstance(next, plcopen.leftPowerRail):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   545
                paths.append(None)
104
a9b8916d906d Adding support for generation of blocks in LD
lbessard
parents: 93
diff changeset
   546
            elif isinstance(next, plcopen.block):
a9b8916d906d Adding support for generation of blocks in LD
lbessard
parents: 93
diff changeset
   547
                block_type = next.getTypeName()
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   548
                self.GeneratePouProgram(block_type)
104
a9b8916d906d Adding support for generation of blocks in LD
lbessard
parents: 93
diff changeset
   549
                block_infos = GetBlockType(block_type)
a9b8916d906d Adding support for generation of blocks in LD
lbessard
parents: 93
diff changeset
   550
                paths.append(block_infos["generate"](self, next, body, connection))
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   551
            else:
66
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   552
                variable = self.ExtractModifier(next, next.getVariable())
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   553
                result = self.GenerateLDPaths(next.connectionPointIn.getConnections(), body)
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   554
                if len(result) > 1:
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   555
                    paths.append([variable, tuple(result)])
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   556
                elif type(result[0]) == ListType:
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   557
                    paths.append([variable] + result[0])
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   558
                elif result[0]:
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   559
                    paths.append([variable, result[0]])
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   560
                else:
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   561
                    paths.append(variable)
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   562
        return paths
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   563
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   564
    def GetNetworkType(self, connections, body):
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   565
        network_type = "FBD"
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   566
        for connection in connections:
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   567
            localId = connection.getRefLocalId()
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   568
            next = body.getContentInstance(localId)
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   569
            if isinstance(next, plcopen.leftPowerRail) or isinstance(next, plcopen.contact):
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   570
                return "LD"
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   571
            elif isinstance(next, plcopen.block):
70
0e48629c1e6d Adding support for avoiding name conflicts
lbessard
parents: 68
diff changeset
   572
                 for variable in next.inputVariables.getVariable():
66
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   573
                     result = self.GetNetworkType(variable.connectionPointIn.getConnections(), body)
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   574
                     if result != "FBD":
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   575
                         return result
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   576
            elif isinstance(next, plcopen.inVariable):
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   577
                return "FBD"
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   578
            elif isinstance(next, plcopen.inOutVariable):
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   579
                return self.GetNetworkType(next.connectionPointIn.getConnections(), body)
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   580
            else:
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   581
                return None
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   582
        return "FBD"
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   583
    
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   584
    def ExtractDivergenceInput(self, divergence, pou):
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   585
        connectionPointIn = divergence.getConnectionPointIn()
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   586
        if connectionPointIn:
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   587
            connections = connectionPointIn.getConnections()
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   588
            if len(connections) == 1:
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   589
                instanceLocalId = connections[0].getRefLocalId()
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   590
                return pou.body.getContentInstance(instanceLocalId)
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   591
        return None
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   592
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   593
    def ExtractConvergenceInputs(self, convergence, pou):
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   594
        instances = []
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   595
        for connectionPointIn in convergence.getConnectionPointIn():
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   596
            connections = connectionPointIn.getConnections()
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   597
            if len(connections) == 1:
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   598
                instanceLocalId = connections[0].getRefLocalId()
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   599
                instances.append(pou.body.getContentInstance(instanceLocalId))
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   600
        return instances
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   601
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   602
    def GenerateSFCStep(self, step, pou):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   603
        step_name = step.getName()
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   604
        if step_name not in self.SFCNetworks["Steps"].keys():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   605
            if step.getInitialStep():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   606
                self.InitialSteps.append(step_name)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   607
            step_infos = {"initial" : step.getInitialStep(), "transitions" : [], "actions" : []}
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   608
            if step.connectionPointIn:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   609
                instances = []
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   610
                connections = step.connectionPointIn.getConnections()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   611
                if len(connections) == 1:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   612
                    instanceLocalId = connections[0].getRefLocalId()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   613
                    instance = pou.body.getContentInstance(instanceLocalId)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   614
                    if isinstance(instance, plcopen.transition):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   615
                        instances.append(instance)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   616
                    elif isinstance(instance, plcopen.selectionConvergence):
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   617
                        instances.extend(self.ExtractConvergenceInputs(instance, pou))
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   618
                    elif isinstance(instance, plcopen.simultaneousDivergence):
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   619
                        transition = self.ExtractDivergenceInput(instance, pou)
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   620
                        if transition:
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   621
                            if isinstance(transition, plcopen.transition):
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   622
                                instances.append(transition)
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   623
                            elif isinstance(transition, plcopen.selectionConvergence):
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   624
                                instances.extend(self.ExtractConvergenceInputs(transition, pou))
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   625
                for instance in instances:
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   626
                    self.GenerateSFCTransition(instance, pou)
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   627
                    if instance in self.SFCNetworks["Transitions"].keys():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   628
                        self.SFCNetworks["Transitions"][instance]["to"].append(step_name)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   629
            self.SFCNetworks["Steps"][step_name] = step_infos
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   630
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   631
    def GenerateSFCJump(self, jump, pou):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   632
        jump_target = jump.getTargetName()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   633
        if jump.connectionPointIn:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   634
            instances = []
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   635
            connections = jump.connectionPointIn.getConnections()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   636
            if len(connections) == 1:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   637
                instanceLocalId = connections[0].getRefLocalId()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   638
                instance = pou.body.getContentInstance(instanceLocalId)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   639
                if isinstance(instance, plcopen.transition):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   640
                    instances.append(instance)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   641
                elif isinstance(instance, plcopen.selectionConvergence):
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   642
                    instances.extend(self.ExtractConvergenceInputs(instance, pou))
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   643
                elif isinstance(instance, plcopen.simultaneousDivergence):
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   644
                    transition = self.ExtractDivergenceInput(instance, pou)
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   645
                    if transition:
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   646
                        if isinstance(transition, plcopen.transition):
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   647
                            instances.append(transition)
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   648
                        elif isinstance(transition, plcopen.selectionConvergence):
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   649
                            instances.extend(self.ExtractConvergenceInputs(transition, pou))
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   650
            for instance in instances:
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   651
                self.GenerateSFCTransition(instance, pou)
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   652
                if instance in self.SFCNetworks["Transitions"].keys():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   653
                    self.SFCNetworks["Transitions"][instance]["to"].append(jump_target)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   654
    
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   655
    def GenerateSFCStepActions(self, actionBlock, pou):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   656
        connections = actionBlock.connectionPointIn.getConnections()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   657
        if len(connections) == 1:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   658
            stepLocalId = connections[0].getRefLocalId()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   659
            step = pou.body.getContentInstance(stepLocalId)
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   660
            self.GenerateSFCStep(step, pou)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   661
            step_name = step.getName()
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   662
            if step_name in self.SFCNetworks["Steps"].keys():
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   663
                actions = actionBlock.getActions()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   664
                for action in actions:
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   665
                    action_infos = {"qualifier" : action["qualifier"], "content" : action["value"]}
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   666
                    if "duration" in action:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   667
                        action_infos["duration"] = action["duration"]
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   668
                    if "indicator" in action:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   669
                        action_infos["indicator"] = action["indicator"]
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   670
                    if action["type"] == "reference":
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   671
                        self.GenerateSFCAction(action["value"], pou)
46
4379e98a30aa Bug on SFC generation fixed
lbessard
parents: 37
diff changeset
   672
                    else:
4379e98a30aa Bug on SFC generation fixed
lbessard
parents: 37
diff changeset
   673
                        action_name = "INLINE%d"%self.GetActionNumber()
4379e98a30aa Bug on SFC generation fixed
lbessard
parents: 37
diff changeset
   674
                        self.SFCNetworks["Actions"][action_name] = "    %s\n"%action["value"]
4379e98a30aa Bug on SFC generation fixed
lbessard
parents: 37
diff changeset
   675
                        action_infos["content"] = action_name
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   676
                    self.SFCNetworks["Steps"][step_name]["actions"].append(action_infos)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   677
    
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   678
    def GenerateSFCAction(self, action_name, pou):
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   679
        if action_name not in self.SFCNetworks["Actions"].keys():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   680
            actionContent = pou.getAction(action_name)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   681
            if actionContent:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   682
                actionType = actionContent.getBodyType()
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   683
                actionBody = actionContent.getBody()
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   684
                if actionType in ["ST", "IL"]:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   685
                    self.SFCNetworks["Actions"][action_name] = ReIndentText(actionContent.getText(), 4)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   686
                elif actionType == "FBD":
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   687
                    for instance in actionBody.getContentInstances():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   688
                        if isinstance(instance, plcopen.outVariable):
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   689
                            var = instance.getExpression()
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   690
                            connections = instance.connectionPointIn.getConnections()
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   691
                            if connections and len(connections) == 1:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   692
                                expression = self.ComputeFBDExpression(actionBody, connections[0])
70
0e48629c1e6d Adding support for avoiding name conflicts
lbessard
parents: 68
diff changeset
   693
                                action_content = self.Program + "  %s := %s;\n"%(var, expression)
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   694
                                self.Program = ""
70
0e48629c1e6d Adding support for avoiding name conflicts
lbessard
parents: 68
diff changeset
   695
                                self.SFCNetworks["Actions"][action_name] = ReIndentText(action_content, 4)
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   696
                elif actionType == "LD":
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   697
                    for instance in actionbody.getContentInstances():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   698
                        if isinstance(instance, plcopen.coil):
66
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   699
                            paths = self.GenerateLDPaths(instance.connectionPointIn.getConnections(), actionBody)
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   700
                            variable = self.ExtractModifier(instance, instance.getVariable())
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   701
                            expression = self.ComputeLDExpression(paths, True)
70
0e48629c1e6d Adding support for avoiding name conflicts
lbessard
parents: 68
diff changeset
   702
                            action_content = self.Program + "  %s := %s;\n"%(variable, expression)
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   703
                            self.Program = ""
70
0e48629c1e6d Adding support for avoiding name conflicts
lbessard
parents: 68
diff changeset
   704
                            self.SFCNetworks["Actions"][action_name] = ReIndentText(action_content, 4)
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   705
    
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   706
    def GenerateSFCTransition(self, transition, pou):
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   707
        if transition not in self.SFCNetworks["Transitions"].keys():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   708
            steps = []
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   709
            connections = transition.connectionPointIn.getConnections()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   710
            if len(connections) == 1:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   711
                instanceLocalId = connections[0].getRefLocalId()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   712
                instance = pou.body.getContentInstance(instanceLocalId)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   713
                if isinstance(instance, plcopen.step):
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   714
                    steps.append(instance)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   715
                elif isinstance(instance, plcopen.selectionDivergence):
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   716
                    step = self.ExtractDivergenceInput(instance, pou)
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   717
                    if step:
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   718
                        if isinstance(step, plcopen.step):
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   719
                            steps.append(step)
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   720
                        elif isinstance(step, plcopen.simultaneousConvergence):
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   721
                            steps.extend(self.ExtractConvergenceInputs(step, pou))
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   722
                elif isinstance(instance, plcopen.simultaneousConvergence):
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   723
                    steps.extend(self.ExtractConvergenceInputs(instance, pou))
80
c798a68c5560 Lots of bugs fixed
lbessard
parents: 78
diff changeset
   724
            transition_infos = {"priority": transition.getPriority(), "from": [], "to" : []}
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   725
            transitionValues = transition.getConditionContent()
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   726
            if transitionValues["type"] == "inline":
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   727
                transition_infos["content"] = "\n    := %s;\n"%transitionValues["value"]
66
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   728
            elif transitionValues["type"] == "reference":
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   729
                transitionContent = pou.getTransition(transitionValues["value"])
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   730
                transitionType = transitionContent.getBodyType()
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   731
                transitionBody = transitionContent.getBody()
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   732
                if transitionType == "IL":
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   733
                    transition_infos["content"] = ":\n%s"%ReIndentText(transitionBody.getText(), 4)
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   734
                elif transitionType == "ST":
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   735
                    transition_infos["content"] = "\n%s"%ReIndentText(transitionBody.getText(), 4)
66
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   736
                elif transitionType == "FBD":
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   737
                    for instance in transitionBody.getContentInstances():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   738
                        if isinstance(instance, plcopen.outVariable):
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   739
                            connections = instance.connectionPointIn.getConnections()
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   740
                            if connections and len(connections) == 1:
66
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   741
                                expression = self.ComputeFBDExpression(transitionBody, connections[0])
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   742
                                transition_infos["content"] = "\n    := %s;\n"%expression
72
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   743
                                self.SFCComputedBlocks += self.Program
70
0e48629c1e6d Adding support for avoiding name conflicts
lbessard
parents: 68
diff changeset
   744
                                self.Program = ""
66
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   745
                elif transitionType == "LD":
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   746
                    for instance in transitionBody.getContentInstances():
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   747
                        if isinstance(instance, plcopen.coil):
66
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   748
                            paths = self.GenerateLDPaths(instance.connectionPointIn.getConnections(), transitionBody)
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   749
                            expression = self.ComputeLDExpression(paths, True)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   750
                            transition_infos["content"] = "\n    := %s;\n"%expression
72
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   751
                            self.SFCComputedBlocks += self.Program
70
0e48629c1e6d Adding support for avoiding name conflicts
lbessard
parents: 68
diff changeset
   752
                            self.Program = ""
66
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   753
            elif transitionValues["type"] == "connection":
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   754
                body = pou.getBody()
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   755
                connections = transition.getConnections()
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   756
                network_type = self.GetNetworkType(connections, body)
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   757
                if network_type == None:
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   758
                    raise Exception
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   759
                if len(connections) > 1 or network_type == "LD":
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   760
                    paths = self.GenerateLDPaths(connections, body)
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   761
                    expression = self.ComputeLDExpression(paths, True)
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   762
                    transition_infos["content"] = "\n    := %s;\n"%expression
72
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   763
                    self.SFCComputedBlocks += self.Program
70
0e48629c1e6d Adding support for avoiding name conflicts
lbessard
parents: 68
diff changeset
   764
                    self.Program = ""
66
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   765
                else:
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   766
                    expression = self.ComputeFBDExpression(body, connections[0])
fd138fc77510 Adding support for generating network and rung connected to transitions into SFC
lbessard
parents: 58
diff changeset
   767
                    transition_infos["content"] = "\n    := %s;\n"%expression
72
73212220ad22 Adding support for generating FBD with connectors and continuations
lbessard
parents: 71
diff changeset
   768
                    self.SFCComputedBlocks += self.Program
70
0e48629c1e6d Adding support for avoiding name conflicts
lbessard
parents: 68
diff changeset
   769
                    self.Program = ""
2
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   770
            for step in steps:
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   771
                self.GenerateSFCStep(step, pou)
93bc4c2cf376 PLCGenerator finished
lbessard
parents: 1
diff changeset
   772
                step_name = step.getName()
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   773
                if step_name in self.SFCNetworks["Steps"].keys():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   774
                    transition_infos["from"].append(step_name)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   775
                    self.SFCNetworks["Steps"][step_name]["transitions"].append(transition)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   776
            self.SFCNetworks["Transitions"][transition] = transition_infos
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   777
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   778
    def ComputeSFCStep(self, step_name):
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   779
        if step_name in self.SFCNetworks["Steps"].keys():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   780
            step_infos = self.SFCNetworks["Steps"].pop(step_name)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   781
            if step_infos["initial"]:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   782
                self.Program += "  INITIAL_STEP %s:\n"%step_name
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   783
            else:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   784
                self.Program += "  STEP %s:\n"%step_name
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   785
            actions = []
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   786
            for action_infos in step_infos["actions"]:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   787
                actions.append(action_infos["content"])
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   788
                self.Program += "    %(content)s(%(qualifier)s"%action_infos
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   789
                if "duration" in action_infos:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   790
                    self.Program += ", %(duration)s"%action_infos
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   791
                if "indicator" in action_infos:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   792
                    self.Program += ", %(indicator)s"%action_infos
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   793
                self.Program += ");\n"
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   794
            self.Program += "  END_STEP\n\n"
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   795
            for action in actions:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   796
                self.ComputeSFCAction(action)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   797
            for transition in step_infos["transitions"]:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   798
                self.ComputeSFCTransition(transition)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   799
                
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   800
    def ComputeSFCAction(self, action_name):
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   801
        if action_name in self.SFCNetworks["Actions"].keys():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   802
            action_content = self.SFCNetworks["Actions"].pop(action_name)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   803
            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
   804
    
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   805
    def ComputeSFCTransition(self, transition):
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   806
        if transition in self.SFCNetworks["Transitions"].keys():
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   807
            transition_infos = self.SFCNetworks["Transitions"].pop(transition)
80
c798a68c5560 Lots of bugs fixed
lbessard
parents: 78
diff changeset
   808
            self.Program += "  TRANSITION"
c798a68c5560 Lots of bugs fixed
lbessard
parents: 78
diff changeset
   809
            if transition_infos["priority"] != None:
c798a68c5560 Lots of bugs fixed
lbessard
parents: 78
diff changeset
   810
                self.Program += " (PRIORITY := %d)"%transition_infos["priority"]
c798a68c5560 Lots of bugs fixed
lbessard
parents: 78
diff changeset
   811
            self.Program += " FROM "
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   812
            if len(transition_infos["from"]) > 1:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   813
                self.Program += "(%s)"%", ".join(transition_infos["from"])
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   814
            else:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   815
                self.Program += "%s"%transition_infos["from"][0]
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   816
            self.Program += " TO "
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   817
            if len(transition_infos["to"]) > 1:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   818
                self.Program += "(%s)"%", ".join(transition_infos["to"])
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   819
            else:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   820
                self.Program += "%s"%transition_infos["to"][0]
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   821
            self.Program += transition_infos["content"]
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   822
            self.Program += "  END_TRANSITION\n\n"
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   823
            for step_name in transition_infos["to"]:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   824
                self.ComputeSFCStep(step_name)
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   825
        
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   826
    def ComputeLDExpression(self, paths, first = False):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   827
        if type(paths) == TupleType:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   828
            if None in paths:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   829
                return "TRUE"
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   830
            else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   831
                var = [self.ComputeLDExpression(path) for path in paths]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   832
                if first:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   833
                    return " OR ".join(var)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   834
                else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   835
                    return "(%s)"%" OR ".join(var)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   836
        elif type(paths) == ListType:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   837
            var = [self.ComputeLDExpression(path) for path in paths]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   838
            return " AND ".join(var)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   839
        else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   840
            return paths
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   841
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   842
    def ExtractModifier(self, variable, text):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   843
        if variable.getNegated():
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   844
            return "NOT(%s)"%text
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   845
        else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   846
            edge = variable.getEdge()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   847
            if edge:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   848
                if edge.getValue() == "rising":
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   849
                    return self.AddTrigger("R_TRIG", text)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   850
                elif edge.getValue() == "falling":
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   851
                    return self.AddTrigger("F_TRIG", text)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   852
        return text
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   853
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   854
    def AddTrigger(self, edge, text):
33
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
   855
        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
   856
            self.Interface.append(("VAR", False, False, False, []))
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   857
        i = 1
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   858
        name = "%s%d"%(edge, i)
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   859
        while self.IsAlreadyDefined(name):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   860
            i += 1
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   861
            name = "%s%d"%(edge, i)
33
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
   862
        self.Interface[-1][4].append((edge, name, None, None))
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   863
        self.Program += "  %s(CLK := %s);\n"%(name, text)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   864
        return "%s.Q"%name
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   865
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   866
    def GenerateSTProgram(self):
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   867
        if self.ReturnType:
29
3b7e23a323a6 Adding support for configuration generation
lbessard
parents: 28
diff changeset
   868
            program = "%s %s : %s\n"%(self.Type, self.Name, self.ReturnType)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   869
        else:
29
3b7e23a323a6 Adding support for configuration generation
lbessard
parents: 28
diff changeset
   870
            program = "%s %s\n"%(self.Type, self.Name)
33
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
   871
        for list_type, retain, constant, located, variables in self.Interface:
1
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   872
            program += "  %s"%list_type
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   873
            if retain:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   874
                program += " RETAIN"
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   875
            if constant:
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   876
                program += " CONSTANT"
e9d01d824086 SFC textual programs generation completed
lbessard
parents: 0
diff changeset
   877
            program += "\n"
30
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   878
            for var_type, var_name, var_address, var_initial in variables:
93
c3c24b979a4d Add support for custom block declaration
lbessard
parents: 80
diff changeset
   879
                program += "    "
c3c24b979a4d Add support for custom block declaration
lbessard
parents: 80
diff changeset
   880
                if var_name:
c3c24b979a4d Add support for custom block declaration
lbessard
parents: 80
diff changeset
   881
                    program += "%s "%var_name
30
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   882
                if var_address != None:
31
d833bf7567b1 Bug on variable location fixed
lbessard
parents: 30
diff changeset
   883
                    program += "AT %s "%var_address
30
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   884
                program += ": %s"%var_type
28
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 6
diff changeset
   885
                if var_initial != None:
33
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
   886
                    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
   887
                    if var_type == "STRING":
256eedd275d0 Adding support for disable interface variable change if a POU is used
lbessard
parents: 33
diff changeset
   888
                        program += " := '%s'"%value
256eedd275d0 Adding support for disable interface variable change if a POU is used
lbessard
parents: 33
diff changeset
   889
                    elif var_type == "WSTRING":
33
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
   890
                        program += " := \"%s\""%value
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
   891
                    else:
0dd4a876392f Bugs on ST generation fixed
lbessard
parents: 31
diff changeset
   892
                        program += " := %s"%value
30
768cf2a6b0b1 Bug on ressource generation fixed
lbessard
parents: 29
diff changeset
   893
                program += ";\n"
4
2de7fd952fdd Adding File Dialog for choosing path to generated program
lbessard
parents: 2
diff changeset
   894
            program += "  END_VAR\n"
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   895
        program += "\n"
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   896
        program += self.Program
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   897
        program += "END_%s\n\n"%self.Type
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   898
        return program
71
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 70
diff changeset
   899
78
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   900
    def GeneratePouProgram(self, pou_name):
049f2e7090a2 Adding support for adding block types with particular behaviour
lbessard
parents: 72
diff changeset
   901
        GeneratePouProgram(pou_name)
71
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 70
diff changeset
   902
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 70
diff changeset
   903
def GenerateCurrentProgram(project):
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 70
diff changeset
   904
    global currentProject, currentProgram
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 70
diff changeset
   905
    currentProject = project
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 70
diff changeset
   906
    currentProgram = ""
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   907
    for datatype in project.getDataTypes():
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   908
        datatypeComputed[datatype.getName()] = False
71
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 70
diff changeset
   909
    for pou in project.getPous():
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 70
diff changeset
   910
        pouComputed[pou.getName()] = False
125
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   911
    if len(datatypeComputed) > 0:
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   912
        currentProgram += "TYPE\n"
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   913
        for datatype_name in datatypeComputed.keys():
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   914
            GenerateDataType(datatype_name)
394d9f168258 Adding support for execution order in PLCGenerator
lbessard
parents: 108
diff changeset
   915
        currentProgram += "END_TYPE\n\n"
71
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 70
diff changeset
   916
    for pou_name in pouComputed.keys():
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 70
diff changeset
   917
        GeneratePouProgram(pou_name)
29
3b7e23a323a6 Adding support for configuration generation
lbessard
parents: 28
diff changeset
   918
    for config in project.getConfigurations():
71
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 70
diff changeset
   919
        currentProgram += GenerateConfiguration(config)
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 70
diff changeset
   920
    return currentProgram
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 70
diff changeset
   921