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