PLCGenerator.py
author Andrey Skvortsov <andrej.skvortzov@gmail.com>
Thu, 11 Oct 2018 12:00:40 +0300
changeset 2450 5024c19ca8f0
parent 2447 1c04a50dc7ff
child 2456 7373e3048167
permissions -rw-r--r--
python3 support: pylint, W1652 # (deprecated-types-field) Accessing a deprecated fields on the types module
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     1
#!/usr/bin/env python
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     3
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1450
diff changeset
     4
# This file is part of Beremiz, a Integrated Development Environment for
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1450
diff changeset
     5
# programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     6
#
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1450
diff changeset
     7
# Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     8
#
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1450
diff changeset
     9
# See COPYING file for copyrights details.
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    10
#
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1450
diff changeset
    11
# This program is free software; you can redistribute it and/or
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1450
diff changeset
    12
# modify it under the terms of the GNU General Public License
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1450
diff changeset
    13
# as published by the Free Software Foundation; either version 2
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1450
diff changeset
    14
# of the License, or (at your option) any later version.
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    15
#
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1450
diff changeset
    16
# This program is distributed in the hope that it will be useful,
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1450
diff changeset
    17
# but WITHOUT ANY WARRANTY; without even the implied warranty of
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1450
diff changeset
    18
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1450
diff changeset
    19
# GNU General Public License for more details.
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    20
#
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1450
diff changeset
    21
# You should have received a copy of the GNU General Public License
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1450
diff changeset
    22
# along with this program; if not, write to the Free Software
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1450
diff changeset
    23
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    24
1832
0f1081928d65 fix wrong-import-order. first standard modules are imported, then others
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1831
diff changeset
    25
1881
091005ec69c4 fix pylint py3k conversion warning: "(no-absolute-import) import missing `from __future__ import absolute_import`"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1878
diff changeset
    26
from __future__ import absolute_import
1832
0f1081928d65 fix wrong-import-order. first standard modules are imported, then others
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1831
diff changeset
    27
import re
2432
dbc065a2f7a5 python3 support: pylint, W1613 # (xrange-builtin) xrange built-in referenced
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2418
diff changeset
    28
from six.moves import xrange
dbc065a2f7a5 python3 support: pylint, W1613 # (xrange-builtin) xrange built-in referenced
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2418
diff changeset
    29
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
    30
from plcopen import PLCOpenParser
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    31
from plcopen.structures import *
1948
b9a3f771aaab Moved some definitions away from controller class, and adaped references them through all code.
Edouard Tisserant
parents: 1881
diff changeset
    32
from plcopen.types_enums import *
1832
0f1081928d65 fix wrong-import-order. first standard modules are imported, then others
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1831
diff changeset
    33
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    34
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
    35
# Dictionary associating PLCOpen variable categories to the corresponding
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    36
# IEC 61131-3 variable categories
1739
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
    37
varTypeNames = {"localVars": "VAR", "tempVars": "VAR_TEMP", "inputVars": "VAR_INPUT",
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
    38
                "outputVars": "VAR_OUTPUT", "inOutVars": "VAR_IN_OUT", "externalVars": "VAR_EXTERNAL",
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
    39
                "globalVars": "VAR_GLOBAL", "accessVars": "VAR_ACCESS"}
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    40
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    41
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
    42
# Dictionary associating PLCOpen POU categories to the corresponding
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    43
# IEC 61131-3 POU categories
1739
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
    44
pouTypeNames = {"function": "FUNCTION", "functionBlock": "FUNCTION_BLOCK", "program": "PROGRAM"}
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    45
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    46
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    47
errorVarTypes = {
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    48
    "VAR_INPUT": "var_input",
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    49
    "VAR_OUTPUT": "var_output",
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    50
    "VAR_INOUT": "var_inout",
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    51
}
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    52
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1734
diff changeset
    53
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    54
def ReIndentText(text, nb_spaces):
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1734
diff changeset
    55
    """ Helper function for reindenting text """
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    56
    compute = ""
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    57
    lines = text.splitlines()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    58
    if len(lines) > 0:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    59
        line_num = 0
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    60
        while line_num < len(lines) and len(lines[line_num].strip()) == 0:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    61
            line_num += 1
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    62
        if line_num < len(lines):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    63
            spaces = 0
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    64
            while lines[line_num][spaces] == " ":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    65
                spaces += 1
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    66
            indent = ""
1847
6198190bc121 explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1846
diff changeset
    67
            for dummy in xrange(spaces, nb_spaces):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    68
                indent += " "
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    69
            for line in lines:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    70
                if line != "":
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
    71
                    compute += "%s%s\n" % (indent, line)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    72
                else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    73
                    compute += "\n"
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    74
    return compute
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    75
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1734
diff changeset
    76
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    77
def SortInstances(a, b):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    78
    ax, ay = int(a.getx()), int(a.gety())
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    79
    bx, by = int(b.getx()), int(b.gety())
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    80
    if abs(ay - by) < 10:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    81
        return cmp(ax, bx)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    82
    else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    83
        return cmp(ay, by)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    84
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1734
diff changeset
    85
1310
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
    86
def JoinList(separator, mylist):
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1734
diff changeset
    87
    """ Helper for emulate join on element list """
1739
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
    88
    if len(mylist) > 0:
1310
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
    89
        return reduce(lambda x, y: x + separator + y, mylist)
1739
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
    90
    else:
1310
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
    91
        return mylist
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
    92
1782
5b6ad7a7fd9d clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1775
diff changeset
    93
# -------------------------------------------------------------------------------
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    94
#                  Specific exception for PLC generating errors
1782
5b6ad7a7fd9d clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1775
diff changeset
    95
# -------------------------------------------------------------------------------
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    96
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    97
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    98
class PLCGenException(Exception):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
    99
    pass
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   100
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   101
1782
5b6ad7a7fd9d clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1775
diff changeset
   102
# -------------------------------------------------------------------------------
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   103
#                           Generator of PLC program
1782
5b6ad7a7fd9d clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1775
diff changeset
   104
# -------------------------------------------------------------------------------
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   105
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   106
1831
56b48961cc68 fix (old-style-class) Old-style class defined error for most parts of
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
   107
class ProgramGenerator(object):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   108
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   109
    # Create a new PCL program generator
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   110
    def __init__(self, controler, project, errors, warnings):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   111
        # Keep reference of the controler and project
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   112
        self.Controler = controler
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   113
        self.Project = project
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   114
        # Reset the internal variables used to generate PLC programs
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   115
        self.Program = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   116
        self.DatatypeComputed = {}
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   117
        self.PouComputed = {}
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   118
        self.Errors = errors
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   119
        self.Warnings = warnings
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   120
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   121
    # Compute value according to type given
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   122
    def ComputeValue(self, value, var_type):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   123
        base_type = self.Controler.GetBaseType(var_type)
1032
c4989e53f9c3 Fix bug defining string initial value using quotes
Laurent Bessard
parents: 893
diff changeset
   124
        if base_type == "STRING" and not value.startswith("'") and not value.endswith("'"):
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   125
            return "'%s'" % value
1032
c4989e53f9c3 Fix bug defining string initial value using quotes
Laurent Bessard
parents: 893
diff changeset
   126
        elif base_type == "WSTRING" and not value.startswith('"') and not value.endswith('"'):
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   127
            return "\"%s\"" % value
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   128
        return value
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   129
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   130
    # Generate a data type from its name
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   131
    def GenerateDataType(self, datatype_name):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   132
        # Verify that data type hasn't been generated yet
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   133
        if not self.DatatypeComputed.get(datatype_name, True):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   134
            # If not mark data type as computed
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   135
            self.DatatypeComputed[datatype_name] = True
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
   136
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   137
            # Getting datatype model from project
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   138
            datatype = self.Project.getdataType(datatype_name)
1948
b9a3f771aaab Moved some definitions away from controller class, and adaped references them through all code.
Edouard Tisserant
parents: 1881
diff changeset
   139
            tagname = ComputeDataTypeName(datatype.getname())
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
   140
            datatype_def = [("  ", ()),
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   141
                            (datatype.getname(), (tagname, "name")),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   142
                            (" : ", ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   143
            basetype_content = datatype.baseType.getcontent()
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   144
            basetype_content_type = basetype_content.getLocalTag()
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
   145
            # Data type derived directly from a user defined type
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   146
            if basetype_content_type == "derived":
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   147
                basetype_name = basetype_content.getname()
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   148
                self.GenerateDataType(basetype_name)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   149
                datatype_def += [(basetype_name, (tagname, "base"))]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   150
            # Data type is a subrange
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   151
            elif basetype_content_type in ["subrangeSigned", "subrangeUnsigned"]:
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   152
                base_type = basetype_content.baseType.getcontent()
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   153
                base_type_type = base_type.getLocalTag()
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
   154
                # Subrange derived directly from a user defined type
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   155
                if base_type_type == "derived":
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   156
                    basetype_name = base_type_type.getname()
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   157
                    self.GenerateDataType(basetype_name)
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
   158
                # Subrange derived directly from an elementary type
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   159
                else:
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   160
                    basetype_name = base_type_type
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   161
                min_value = basetype_content.range.getlower()
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   162
                max_value = basetype_content.range.getupper()
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   163
                datatype_def += [(basetype_name, (tagname, "base")),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   164
                                 (" (", ()),
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   165
                                 ("%s" % min_value, (tagname, "lower")),
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   166
                                 ("..", ()),
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   167
                                 ("%s" % max_value, (tagname, "upper")),
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
   168
                                 (")", ())]
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   169
            # Data type is an enumerated type
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   170
            elif basetype_content_type == "enum":
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   171
                values = [[(value.getname(), (tagname, "value", i))]
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   172
                          for i, value in enumerate(
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
   173
                              basetype_content.xpath("ppx:values/ppx:value",
1768
691083b5682a clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1767
diff changeset
   174
                                                     namespaces=PLCOpenParser.NSMAP))]
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   175
                datatype_def += [("(", ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   176
                datatype_def += JoinList([(", ", ())], values)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   177
                datatype_def += [(")", ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   178
            # Data type is an array
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   179
            elif basetype_content_type == "array":
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   180
                base_type = basetype_content.baseType.getcontent()
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   181
                base_type_type = base_type.getLocalTag()
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
   182
                # Array derived directly from a user defined type
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   183
                if base_type_type == "derived":
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   184
                    basetype_name = base_type.getname()
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   185
                    self.GenerateDataType(basetype_name)
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
   186
                # Array derived directly from an elementary type
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   187
                else:
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   188
                    basetype_name = base_type_type.upper()
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   189
                dimensions = [[("%s" % dimension.getlower(), (tagname, "range", i, "lower")),
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   190
                               ("..", ()),
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   191
                               ("%s" % dimension.getupper(), (tagname, "range", i, "upper"))]
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   192
                              for i, dimension in enumerate(basetype_content.getdimension())]
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   193
                datatype_def += [("ARRAY [", ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   194
                datatype_def += JoinList([(",", ())], dimensions)
1739
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   195
                datatype_def += [("] OF ", ()),
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   196
                                 (basetype_name, (tagname, "base"))]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   197
            # Data type is a structure
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   198
            elif basetype_content_type == "struct":
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   199
                elements = []
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   200
                for i, element in enumerate(basetype_content.getvariable()):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   201
                    element_type = element.type.getcontent()
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   202
                    element_type_type = element_type.getLocalTag()
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
   203
                    # Structure element derived directly from a user defined type
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   204
                    if element_type_type == "derived":
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   205
                        elementtype_name = element_type.getname()
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   206
                        self.GenerateDataType(elementtype_name)
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   207
                    elif element_type_type == "array":
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   208
                        base_type = element_type.baseType.getcontent()
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   209
                        base_type_type = base_type.getLocalTag()
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
   210
                        # Array derived directly from a user defined type
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   211
                        if base_type_type == "derived":
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   212
                            basetype_name = base_type.getname()
864
bf4f7f0801b9 Adding support for direct array declaration in structure element declaration
Laurent Bessard
parents: 854
diff changeset
   213
                            self.GenerateDataType(basetype_name)
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
   214
                        # Array derived directly from an elementary type
864
bf4f7f0801b9 Adding support for direct array declaration in structure element declaration
Laurent Bessard
parents: 854
diff changeset
   215
                        else:
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   216
                            basetype_name = base_type_type.upper()
864
bf4f7f0801b9 Adding support for direct array declaration in structure element declaration
Laurent Bessard
parents: 854
diff changeset
   217
                        dimensions = ["%s..%s" % (dimension.getlower(), dimension.getupper())
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   218
                                      for dimension in element_type.getdimension()]
864
bf4f7f0801b9 Adding support for direct array declaration in structure element declaration
Laurent Bessard
parents: 854
diff changeset
   219
                        elementtype_name = "ARRAY [%s] OF %s" % (",".join(dimensions), basetype_name)
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
   220
                    # Structure element derived directly from an elementary type
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   221
                    else:
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   222
                        elementtype_name = element_type_type.upper()
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   223
                    element_text = [("\n    ", ()),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   224
                                    (element.getname(), (tagname, "struct", i, "name")),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   225
                                    (" : ", ()),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   226
                                    (elementtype_name, (tagname, "struct", i, "type"))]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   227
                    if element.initialValue is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   228
                        element_text.extend([(" := ", ()),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   229
                                             (self.ComputeValue(element.initialValue.getvalue(), elementtype_name), (tagname, "struct", i, "initial value"))])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   230
                    element_text.append((";", ()))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   231
                    elements.append(element_text)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   232
                datatype_def += [("STRUCT", ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   233
                datatype_def += JoinList([("", ())], elements)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   234
                datatype_def += [("\n  END_STRUCT", ())]
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
   235
            # Data type derived directly from a elementary type
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   236
            else:
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   237
                datatype_def += [(basetype_content_type.upper(), (tagname, "base"))]
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   238
            # Data type has an initial value
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   239
            if datatype.initialValue is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   240
                datatype_def += [(" := ", ()),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   241
                                 (self.ComputeValue(datatype.initialValue.getvalue(), datatype_name), (tagname, "initial value"))]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   242
            datatype_def += [(";\n", ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   243
            self.Program += datatype_def
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   244
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   245
    # Generate a POU from its name
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   246
    def GeneratePouProgram(self, pou_name):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   247
        # Verify that POU hasn't been generated yet
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   248
        if not self.PouComputed.get(pou_name, True):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   249
            # If not mark POU as computed
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   250
            self.PouComputed[pou_name] = True
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
   251
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   252
            # Getting POU model from project
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   253
            pou = self.Project.getpou(pou_name)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   254
            pou_type = pou.getpouType()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   255
            # Verify that POU type exists
1763
bcc07ff2362c clean-up: fix PEP8 W601 .has_key() is deprecated, use 'in'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1753
diff changeset
   256
            if pou_type in pouTypeNames:
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   257
                # Create a POU program generator
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   258
                pou_program = PouProgramGenerator(self, pou.getname(), pouTypeNames[pou_type], self.Errors, self.Warnings)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   259
                program = pou_program.GenerateProgram(pou)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   260
                self.Program += program
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   261
            else:
1765
ccf59c1f0b45 clean-up: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1763
diff changeset
   262
                raise PLCGenException(_("Undefined pou type \"%s\"") % pou_type)
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
   263
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   264
    # Generate a POU defined and used in text
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   265
    def GeneratePouProgramInText(self, text):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   266
        for pou_name in self.PouComputed.keys():
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   267
            model = re.compile("(?:^|[^0-9^A-Z])%s(?:$|[^0-9^A-Z])" % pou_name.upper())
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   268
            if model.search(text) is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   269
                self.GeneratePouProgram(pou_name)
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
   270
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   271
    # Generate a configuration from its model
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   272
    def GenerateConfiguration(self, configuration):
1948
b9a3f771aaab Moved some definitions away from controller class, and adaped references them through all code.
Edouard Tisserant
parents: 1881
diff changeset
   273
        tagname = ComputeConfigurationName(configuration.getname())
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   274
        config = [("\nCONFIGURATION ", ()),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   275
                  (configuration.getname(), (tagname, "name")),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   276
                  ("\n", ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   277
        var_number = 0
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
   278
883
235a9ec83b95 Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents: 864
diff changeset
   279
        varlists = [(varlist, varlist.getvariable()[:]) for varlist in configuration.getglobalVars()]
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
   280
883
235a9ec83b95 Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents: 864
diff changeset
   281
        extra_variables = self.Controler.GetConfigurationExtraVariables()
1315
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1310
diff changeset
   282
        extra_global_vars = None
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1310
diff changeset
   283
        if len(extra_variables) > 0 and len(varlists) == 0:
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1310
diff changeset
   284
            extra_global_vars = PLCOpenParser.CreateElement("globalVars", "interface")
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1310
diff changeset
   285
            configuration.setglobalVars([extra_global_vars])
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1310
diff changeset
   286
            varlists = [(extra_global_vars, [])]
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
   287
1315
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1310
diff changeset
   288
        for variable in extra_variables:
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1310
diff changeset
   289
            varlists[-1][0].appendvariable(variable)
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1310
diff changeset
   290
            varlists[-1][1].append(variable)
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
   291
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   292
        # Generate any global variable in configuration
883
235a9ec83b95 Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents: 864
diff changeset
   293
        for varlist, varlist_variables in varlists:
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   294
            variable_type = errorVarTypes.get("VAR_GLOBAL", "var_local")
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   295
            # Generate variable block with modifier
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   296
            config += [("  VAR_GLOBAL", ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   297
            if varlist.getconstant():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   298
                config += [(" CONSTANT", (tagname, variable_type, (var_number, var_number + len(varlist.getvariable())), "constant"))]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   299
            elif varlist.getretain():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   300
                config += [(" RETAIN", (tagname, variable_type, (var_number, var_number + len(varlist.getvariable())), "retain"))]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   301
            elif varlist.getnonretain():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   302
                config += [(" NON_RETAIN", (tagname, variable_type, (var_number, var_number + len(varlist.getvariable())), "non_retain"))]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   303
            config += [("\n", ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   304
            # Generate any variable of this block
883
235a9ec83b95 Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents: 864
diff changeset
   305
            for var in varlist_variables:
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   306
                vartype_content = var.gettype().getcontent()
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   307
                if vartype_content.getLocalTag() == "derived":
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   308
                    var_type = vartype_content.getname()
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   309
                    self.GenerateDataType(var_type)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   310
                else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   311
                    var_type = var.gettypeAsText()
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
   312
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   313
                config += [("    ", ()),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   314
                           (var.getname(), (tagname, variable_type, var_number, "name")),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   315
                           (" ", ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   316
                # Generate variable address if exists
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   317
                address = var.getaddress()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   318
                if address:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   319
                    config += [("AT ", ()),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   320
                               (address, (tagname, variable_type, var_number, "location")),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   321
                               (" ", ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   322
                config += [(": ", ()),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   323
                           (var.gettypeAsText(), (tagname, variable_type, var_number, "type"))]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   324
                # Generate variable initial value if exists
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   325
                initial = var.getinitialValue()
1315
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1310
diff changeset
   326
                if initial is not None:
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   327
                    config += [(" := ", ()),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   328
                               (self.ComputeValue(initial.getvalue(), var_type), (tagname, variable_type, var_number, "initial value"))]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   329
                config += [(";\n", ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   330
                var_number += 1
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   331
            config += [("  END_VAR\n", ())]
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
   332
1315
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1310
diff changeset
   333
        if extra_global_vars is not None:
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1310
diff changeset
   334
            configuration.remove(extra_global_vars)
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1310
diff changeset
   335
        else:
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1310
diff changeset
   336
            for variable in extra_variables:
1358
fe7770a30527 Fixed bug when building project with configuration extra variables
Laurent Bessard
parents: 1322
diff changeset
   337
                varlists[-1][0].remove(variable)
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
   338
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   339
        # Generate any resource in the configuration
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   340
        for resource in configuration.getresource():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   341
            config += self.GenerateResource(resource, configuration.getname())
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   342
        config += [("END_CONFIGURATION\n", ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   343
        return config
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
   344
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   345
    # Generate a resource from its model
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   346
    def GenerateResource(self, resource, config_name):
1948
b9a3f771aaab Moved some definitions away from controller class, and adaped references them through all code.
Edouard Tisserant
parents: 1881
diff changeset
   347
        tagname = ComputeConfigurationResourceName(config_name, resource.getname())
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   348
        resrce = [("\n  RESOURCE ", ()),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   349
                  (resource.getname(), (tagname, "name")),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   350
                  (" ON PLC\n", ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   351
        var_number = 0
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   352
        # Generate any global variable in configuration
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   353
        for varlist in resource.getglobalVars():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   354
            variable_type = errorVarTypes.get("VAR_GLOBAL", "var_local")
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   355
            # Generate variable block with modifier
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   356
            resrce += [("    VAR_GLOBAL", ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   357
            if varlist.getconstant():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   358
                resrce += [(" CONSTANT", (tagname, variable_type, (var_number, var_number + len(varlist.getvariable())), "constant"))]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   359
            elif varlist.getretain():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   360
                resrce += [(" RETAIN", (tagname, variable_type, (var_number, var_number + len(varlist.getvariable())), "retain"))]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   361
            elif varlist.getnonretain():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   362
                resrce += [(" NON_RETAIN", (tagname, variable_type, (var_number, var_number + len(varlist.getvariable())), "non_retain"))]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   363
            resrce += [("\n", ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   364
            # Generate any variable of this block
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   365
            for var in varlist.getvariable():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   366
                vartype_content = var.gettype().getcontent()
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   367
                if vartype_content.getLocalTag() == "derived":
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   368
                    var_type = vartype_content.getname()
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   369
                    self.GenerateDataType(var_type)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   370
                else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   371
                    var_type = var.gettypeAsText()
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
   372
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   373
                resrce += [("      ", ()),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   374
                           (var.getname(), (tagname, variable_type, var_number, "name")),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   375
                           (" ", ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   376
                address = var.getaddress()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   377
                # Generate variable address if exists
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   378
                if address:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   379
                    resrce += [("AT ", ()),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   380
                               (address, (tagname, variable_type, var_number, "location")),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   381
                               (" ", ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   382
                resrce += [(": ", ()),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   383
                           (var.gettypeAsText(), (tagname, variable_type, var_number, "type"))]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   384
                # Generate variable initial value if exists
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   385
                initial = var.getinitialValue()
1315
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1310
diff changeset
   386
                if initial is not None:
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   387
                    resrce += [(" := ", ()),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   388
                               (self.ComputeValue(initial.getvalue(), var_type), (tagname, variable_type, var_number, "initial value"))]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   389
                resrce += [(";\n", ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   390
                var_number += 1
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   391
            resrce += [("    END_VAR\n", ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   392
        # Generate any task in the resource
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   393
        tasks = resource.gettask()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   394
        task_number = 0
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   395
        for task in tasks:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   396
            # Task declaration
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   397
            resrce += [("    TASK ", ()),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   398
                       (task.getname(), (tagname, "task", task_number, "name")),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   399
                       ("(", ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   400
            single = task.getsingle()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   401
            # Single argument if exists
1315
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1310
diff changeset
   402
            if single is not None:
1614
f8f05f849831 check during program generation whether source signal is defined for single task
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1603
diff changeset
   403
                if len(single) == 0:
1765
ccf59c1f0b45 clean-up: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1763
diff changeset
   404
                    raise PLCGenException(
ccf59c1f0b45 clean-up: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1763
diff changeset
   405
                        _("Source signal has to be defined for single task '{a1}' in resource '{a2}.{a3}'.").
ccf59c1f0b45 clean-up: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1763
diff changeset
   406
                        format(a1=task.getname(), a2=config_name, a3=resource.getname()))
1614
f8f05f849831 check during program generation whether source signal is defined for single task
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1603
diff changeset
   407
1742
92932cd370a4 clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1740
diff changeset
   408
                if single[0] == '[' and single[-1] == ']':
1420
71b1545d746f Enable overloading of task triggerring source cell editor (SINGLE) in resource editor. PLCGenerator now generates MULTI keywork instead of SINGLE when task's activation is surroundes with square brackets
Edouard Tisserant
parents: 1418
diff changeset
   409
                    SNGLKW = "MULTI"
71b1545d746f Enable overloading of task triggerring source cell editor (SINGLE) in resource editor. PLCGenerator now generates MULTI keywork instead of SINGLE when task's activation is surroundes with square brackets
Edouard Tisserant
parents: 1418
diff changeset
   410
                else:
71b1545d746f Enable overloading of task triggerring source cell editor (SINGLE) in resource editor. PLCGenerator now generates MULTI keywork instead of SINGLE when task's activation is surroundes with square brackets
Edouard Tisserant
parents: 1418
diff changeset
   411
                    SNGLKW = "SINGLE"
71b1545d746f Enable overloading of task triggerring source cell editor (SINGLE) in resource editor. PLCGenerator now generates MULTI keywork instead of SINGLE when task's activation is surroundes with square brackets
Edouard Tisserant
parents: 1418
diff changeset
   412
                resrce += [(SNGLKW + " := ", ()),
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   413
                           (single, (tagname, "task", task_number, "single")),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   414
                           (",", ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   415
            # Interval argument if exists
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   416
            interval = task.getinterval()
1315
ff14a66bbd12 Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents: 1310
diff changeset
   417
            if interval is not None:
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   418
                resrce += [("INTERVAL := ", ()),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   419
                           (interval, (tagname, "task", task_number, "interval")),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   420
                           (",", ())]
1753
19f19c66b67e clean-up: fix most PEP8 E266 too many leading '#' for block comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1751
diff changeset
   421
#                resrce += [("INTERVAL := t#", ())]
19f19c66b67e clean-up: fix most PEP8 E266 too many leading '#' for block comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1751
diff changeset
   422
#                if interval.hour != 0:
19f19c66b67e clean-up: fix most PEP8 E266 too many leading '#' for block comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1751
diff changeset
   423
#                    resrce += [("%dh"%interval.hour, (tagname, "task", task_number, "interval", "hour"))]
19f19c66b67e clean-up: fix most PEP8 E266 too many leading '#' for block comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1751
diff changeset
   424
#                if interval.minute != 0:
19f19c66b67e clean-up: fix most PEP8 E266 too many leading '#' for block comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1751
diff changeset
   425
#                    resrce += [("%dm"%interval.minute, (tagname, "task", task_number, "interval", "minute"))]
19f19c66b67e clean-up: fix most PEP8 E266 too many leading '#' for block comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1751
diff changeset
   426
#                if interval.second != 0:
19f19c66b67e clean-up: fix most PEP8 E266 too many leading '#' for block comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1751
diff changeset
   427
#                    resrce += [("%ds"%interval.second, (tagname, "task", task_number, "interval", "second"))]
19f19c66b67e clean-up: fix most PEP8 E266 too many leading '#' for block comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1751
diff changeset
   428
#                if interval.microsecond != 0:
19f19c66b67e clean-up: fix most PEP8 E266 too many leading '#' for block comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1751
diff changeset
   429
#                    resrce += [("%dms"%(interval.microsecond / 1000), (tagname, "task", task_number, "interval", "millisecond"))]
19f19c66b67e clean-up: fix most PEP8 E266 too many leading '#' for block comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1751
diff changeset
   430
#                resrce += [(",", ())]
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   431
            # Priority argument
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
   432
            resrce += [("PRIORITY := ", ()),
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   433
                       ("%d" % task.getpriority(), (tagname, "task", task_number, "priority")),
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   434
                       (");\n", ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   435
            task_number += 1
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   436
        instance_number = 0
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   437
        # Generate any program assign to each task
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   438
        for task in tasks:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   439
            for instance in task.getpouInstance():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   440
                resrce += [("    PROGRAM ", ()),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   441
                           (instance.getname(), (tagname, "instance", instance_number, "name")),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   442
                           (" WITH ", ()),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   443
                           (task.getname(), (tagname, "instance", instance_number, "task")),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   444
                           (" : ", ()),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   445
                           (instance.gettypeName(), (tagname, "instance", instance_number, "type")),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   446
                           (";\n", ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   447
                instance_number += 1
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   448
        # Generate any program assign to no task
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   449
        for instance in resource.getpouInstance():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   450
            resrce += [("    PROGRAM ", ()),
1767
c74815729afd clean-up: fix PEP8 E127 continuation line over-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1766
diff changeset
   451
                       (instance.getname(),     (tagname, "instance", instance_number, "name")),
c74815729afd clean-up: fix PEP8 E127 continuation line over-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1766
diff changeset
   452
                       (" : ", ()),
c74815729afd clean-up: fix PEP8 E127 continuation line over-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1766
diff changeset
   453
                       (instance.gettypeName(), (tagname, "instance", instance_number, "type")),
c74815729afd clean-up: fix PEP8 E127 continuation line over-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1766
diff changeset
   454
                       (";\n", ())]
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   455
            instance_number += 1
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   456
        resrce += [("  END_RESOURCE\n", ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   457
        return resrce
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
   458
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   459
    # Generate the entire program for current project
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
   460
    def GenerateProgram(self):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   461
        # Find all data types defined
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   462
        for datatype in self.Project.getdataTypes():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   463
            self.DatatypeComputed[datatype.getname()] = False
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   464
        # Find all data types defined
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   465
        for pou in self.Project.getpous():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   466
            self.PouComputed[pou.getname()] = False
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
   467
        # Generate data type declaration structure if there is at least one data
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   468
        # type defined
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   469
        if len(self.DatatypeComputed) > 0:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   470
            self.Program += [("TYPE\n", ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   471
            # Generate every data types defined
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   472
            for datatype_name in self.DatatypeComputed.keys():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   473
                self.GenerateDataType(datatype_name)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   474
            self.Program += [("END_TYPE\n\n", ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   475
        # Generate every POUs defined
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   476
        for pou_name in self.PouComputed.keys():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   477
            self.GeneratePouProgram(pou_name)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   478
        # Generate every configurations defined
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   479
        for config in self.Project.getconfigurations():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   480
            self.Program += self.GenerateConfiguration(config)
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
   481
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   482
    # Return generated program
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   483
    def GetGeneratedProgram(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   484
        return self.Program
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   485
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   486
1782
5b6ad7a7fd9d clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1775
diff changeset
   487
# -------------------------------------------------------------------------------
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   488
#                           Generator of POU programs
1782
5b6ad7a7fd9d clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1775
diff changeset
   489
# -------------------------------------------------------------------------------
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   490
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   491
[ConnectorClass, ContinuationClass, ActionBlockClass] = [
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   492
    PLCOpenParser.GetElementClass(instance_name, "commonObjects")
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   493
    for instance_name in ["connector", "continuation", "actionBlock"]]
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   494
[InVariableClass, InOutVariableClass, OutVariableClass, BlockClass] = [
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   495
    PLCOpenParser.GetElementClass(instance_name, "fbdObjects")
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   496
    for instance_name in ["inVariable", "inOutVariable", "outVariable", "block"]]
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   497
[ContactClass, CoilClass, LeftPowerRailClass, RightPowerRailClass] = [
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   498
    PLCOpenParser.GetElementClass(instance_name, "ldObjects")
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   499
    for instance_name in ["contact", "coil", "leftPowerRail", "rightPowerRail"]]
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
   500
[StepClass, TransitionClass, JumpStepClass,
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   501
 SelectionConvergenceClass, SelectionDivergenceClass,
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   502
 SimultaneousConvergenceClass, SimultaneousDivergenceClass] = [
1878
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1864
diff changeset
   503
     PLCOpenParser.GetElementClass(instance_name, "sfcObjects")
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1864
diff changeset
   504
     for instance_name in ["step",
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1864
diff changeset
   505
                           "transition",
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1864
diff changeset
   506
                           "jumpStep",
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1864
diff changeset
   507
                           "selectionConvergence",
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1864
diff changeset
   508
                           "selectionDivergence",
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1864
diff changeset
   509
                           "simultaneousConvergence",
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1864
diff changeset
   510
                           "simultaneousDivergence"]]
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   511
TransitionObjClass = PLCOpenParser.GetElementClass("transition", "transitions")
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   512
ActionObjClass = PLCOpenParser.GetElementClass("action", "actions")
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   513
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1734
diff changeset
   514
1831
56b48961cc68 fix (old-style-class) Old-style class defined error for most parts of
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
   515
class PouProgramGenerator(object):
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
   516
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   517
    # Create a new POU program generator
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   518
    def __init__(self, parent, name, type, errors, warnings):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   519
        # Keep Reference to the parent generator
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   520
        self.ParentGenerator = parent
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   521
        self.Name = name
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   522
        self.Type = type
1948
b9a3f771aaab Moved some definitions away from controller class, and adaped references them through all code.
Edouard Tisserant
parents: 1881
diff changeset
   523
        self.TagName = ComputePouName(name)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   524
        self.CurrentIndent = "  "
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   525
        self.ReturnType = None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   526
        self.Interface = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   527
        self.InitialSteps = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   528
        self.ComputedBlocks = {}
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   529
        self.ComputedConnectors = {}
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   530
        self.ConnectionTypes = {}
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   531
        self.RelatedConnections = []
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
   532
        self.SFCNetworks = {"Steps": {}, "Transitions": {}, "Actions": {}}
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   533
        self.SFCComputedBlocks = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   534
        self.ActionNumber = 0
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   535
        self.Program = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   536
        self.Errors = errors
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   537
        self.Warnings = warnings
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
   538
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   539
    def GetBlockType(self, type, inputs=None):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   540
        return self.ParentGenerator.Controler.GetBlockType(type, inputs)
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
   541
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   542
    def IndentLeft(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   543
        if len(self.CurrentIndent) >= 2:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   544
            self.CurrentIndent = self.CurrentIndent[:-2]
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
   545
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   546
    def IndentRight(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   547
        self.CurrentIndent += "  "
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
   548
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   549
    # Generator of unique ID for inline actions
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   550
    def GetActionNumber(self):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   551
        self.ActionNumber += 1
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   552
        return self.ActionNumber
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
   553
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   554
    # Test if a variable has already been defined
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   555
    def IsAlreadyDefined(self, name):
1847
6198190bc121 explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1846
diff changeset
   556
        for _list_type, _option, _located, vars in self.Interface:
6198190bc121 explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1846
diff changeset
   557
            for _var_type, var_name, _var_address, _var_initial in vars:
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   558
                if name == var_name:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   559
                    return True
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   560
        return False
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
   561
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   562
    # Return the type of a variable defined in interface
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   563
    def GetVariableType(self, name):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   564
        parts = name.split('.')
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   565
        current_type = None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   566
        if len(parts) > 0:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   567
            name = parts.pop(0)
1847
6198190bc121 explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1846
diff changeset
   568
            for _list_type, _option, _located, vars in self.Interface:
6198190bc121 explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1846
diff changeset
   569
                for var_type, var_name, _var_address, _var_initial in vars:
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   570
                    if name == var_name:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   571
                        current_type = var_type
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   572
                        break
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   573
            while current_type is not None and len(parts) > 0:
883
235a9ec83b95 Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents: 864
diff changeset
   574
                blocktype = self.ParentGenerator.Controler.GetBlockType(current_type)
235a9ec83b95 Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents: 864
diff changeset
   575
                if blocktype is not None:
235a9ec83b95 Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents: 864
diff changeset
   576
                    name = parts.pop(0)
235a9ec83b95 Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents: 864
diff changeset
   577
                    current_type = None
1847
6198190bc121 explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1846
diff changeset
   578
                    for var_name, var_type, _var_modifier in blocktype["inputs"] + blocktype["outputs"]:
883
235a9ec83b95 Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents: 864
diff changeset
   579
                        if var_name == name:
235a9ec83b95 Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents: 864
diff changeset
   580
                            current_type = var_type
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   581
                            break
883
235a9ec83b95 Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents: 864
diff changeset
   582
                else:
1948
b9a3f771aaab Moved some definitions away from controller class, and adaped references them through all code.
Edouard Tisserant
parents: 1881
diff changeset
   583
                    tagname = ComputeDataTypeName(current_type)
883
235a9ec83b95 Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents: 864
diff changeset
   584
                    infos = self.ParentGenerator.Controler.GetDataTypeInfos(tagname)
235a9ec83b95 Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents: 864
diff changeset
   585
                    if infos is not None and infos["type"] == "Structure":
235a9ec83b95 Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents: 864
diff changeset
   586
                        name = parts.pop(0)
235a9ec83b95 Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents: 864
diff changeset
   587
                        current_type = None
235a9ec83b95 Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents: 864
diff changeset
   588
                        for element in infos["elements"]:
235a9ec83b95 Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents: 864
diff changeset
   589
                            if element["Name"] == name:
235a9ec83b95 Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents: 864
diff changeset
   590
                                current_type = element["Type"]
235a9ec83b95 Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents: 864
diff changeset
   591
                                break
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   592
        return current_type
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
   593
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   594
    # Return connectors linked by a connection to the given connector
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   595
    def GetConnectedConnector(self, connector, body):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   596
        links = connector.getconnections()
1298
f034fb2b1aab Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents: 1297
diff changeset
   597
        if links is not None and len(links) == 1:
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   598
            return self.GetLinkedConnector(links[0], body)
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
   599
        return None
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   600
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   601
    def GetLinkedConnector(self, link, body):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   602
        parameter = link.getformalParameter()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   603
        instance = body.getcontentInstance(link.getrefLocalId())
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
   604
        if isinstance(instance, (InVariableClass, InOutVariableClass,
1768
691083b5682a clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1767
diff changeset
   605
                                 ContinuationClass, ContactClass, CoilClass)):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   606
            return instance.connectionPointOut
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   607
        elif isinstance(instance, BlockClass):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   608
            outputvariables = instance.outputVariables.getvariable()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   609
            if len(outputvariables) == 1:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   610
                return outputvariables[0].connectionPointOut
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   611
            elif parameter:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   612
                for variable in outputvariables:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   613
                    if variable.getformalParameter() == parameter:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   614
                        return variable.connectionPointOut
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   615
            else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   616
                point = link.getposition()[-1]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   617
                for variable in outputvariables:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   618
                    relposition = variable.connectionPointOut.getrelPositionXY()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   619
                    blockposition = instance.getposition()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   620
                    if point.x == blockposition.x + relposition[0] and point.y == blockposition.y + relposition[1]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   621
                        return variable.connectionPointOut
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   622
        elif isinstance(instance, LeftPowerRailClass):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   623
            outputconnections = instance.getconnectionPointOut()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   624
            if len(outputconnections) == 1:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   625
                return outputconnections[0]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   626
            else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   627
                point = link.getposition()[-1]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   628
                for outputconnection in outputconnections:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   629
                    relposition = outputconnection.getrelPositionXY()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   630
                    powerrailposition = instance.getposition()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   631
                    if point.x == powerrailposition.x + relposition[0] and point.y == powerrailposition.y + relposition[1]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   632
                        return outputconnection
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   633
        return None
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
   634
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   635
    def ExtractRelatedConnections(self, connection):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   636
        for i, related in enumerate(self.RelatedConnections):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   637
            if connection in related:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   638
                return self.RelatedConnections.pop(i)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   639
        return [connection]
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
   640
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   641
    def ComputeInterface(self, pou):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   642
        interface = pou.getinterface()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   643
        if interface is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   644
            if self.Type == "FUNCTION":
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
   645
                returntype_content = interface.getreturnType()[0]
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   646
                returntype_content_type = returntype_content.getLocalTag()
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   647
                if returntype_content_type == "derived":
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   648
                    self.ReturnType = returntype_content.getname()
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   649
                else:
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   650
                    self.ReturnType = returntype_content_type.upper()
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   651
            for varlist in interface.getcontent():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   652
                variables = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   653
                located = []
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   654
                varlist_type = varlist.getLocalTag()
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   655
                for var in varlist.getvariable():
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   656
                    vartype_content = var.gettype().getcontent()
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   657
                    if vartype_content.getLocalTag() == "derived":
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   658
                        var_type = vartype_content.getname()
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   659
                        blocktype = self.GetBlockType(var_type)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   660
                        if blocktype is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   661
                            self.ParentGenerator.GeneratePouProgram(var_type)
1310
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
   662
                            variables.append((var_type, var.getname(), None, None))
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   663
                        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   664
                            self.ParentGenerator.GenerateDataType(var_type)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   665
                            initial = var.getinitialValue()
1310
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
   666
                            if initial is not None:
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   667
                                initial_value = initial.getvalue()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   668
                            else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   669
                                initial_value = None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   670
                            address = var.getaddress()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   671
                            if address is not None:
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   672
                                located.append((vartype_content.getname(), var.getname(), address, initial_value))
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   673
                            else:
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   674
                                variables.append((vartype_content.getname(), var.getname(), None, initial_value))
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   675
                    else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   676
                        var_type = var.gettypeAsText()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   677
                        initial = var.getinitialValue()
1310
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
   678
                        if initial is not None:
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   679
                            initial_value = initial.getvalue()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   680
                        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   681
                            initial_value = None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   682
                        address = var.getaddress()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   683
                        if address is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   684
                            located.append((var_type, var.getname(), address, initial_value))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   685
                        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   686
                            variables.append((var_type, var.getname(), None, initial_value))
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   687
                if varlist.getconstant():
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   688
                    option = "CONSTANT"
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   689
                elif varlist.getretain():
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   690
                    option = "RETAIN"
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   691
                elif varlist.getnonretain():
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   692
                    option = "NON_RETAIN"
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   693
                else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   694
                    option = None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   695
                if len(variables) > 0:
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   696
                    self.Interface.append((varTypeNames[varlist_type], option, False, variables))
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   697
                if len(located) > 0:
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   698
                    self.Interface.append((varTypeNames[varlist_type], option, True, located))
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
   699
1181
21e6db77eb29 Fixed bug in PLC code generated with binary, octal and hexadecimal literals
Laurent Bessard
parents: 1134
diff changeset
   700
    LITERAL_TYPES = {
1751
c28db6f7616b clean-up: fix PEP8 E301 expected 1 blank line, found 0
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1747
diff changeset
   701
        "T":   "TIME",
c28db6f7616b clean-up: fix PEP8 E301 expected 1 blank line, found 0
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1747
diff changeset
   702
        "D":   "DATE",
1181
21e6db77eb29 Fixed bug in PLC code generated with binary, octal and hexadecimal literals
Laurent Bessard
parents: 1134
diff changeset
   703
        "TOD": "TIME_OF_DAY",
1751
c28db6f7616b clean-up: fix PEP8 E301 expected 1 blank line, found 0
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1747
diff changeset
   704
        "DT":  "DATE_AND_TIME",
c28db6f7616b clean-up: fix PEP8 E301 expected 1 blank line, found 0
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1747
diff changeset
   705
        "2":    None,
c28db6f7616b clean-up: fix PEP8 E301 expected 1 blank line, found 0
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1747
diff changeset
   706
        "8":    None,
c28db6f7616b clean-up: fix PEP8 E301 expected 1 blank line, found 0
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1747
diff changeset
   707
        "16":   None,
1181
21e6db77eb29 Fixed bug in PLC code generated with binary, octal and hexadecimal literals
Laurent Bessard
parents: 1134
diff changeset
   708
    }
1751
c28db6f7616b clean-up: fix PEP8 E301 expected 1 blank line, found 0
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1747
diff changeset
   709
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   710
    def ComputeConnectionTypes(self, pou):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   711
        body = pou.getbody()
2450
5024c19ca8f0 python3 support: pylint, W1652 # (deprecated-types-field) Accessing a deprecated fields on the types module
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2447
diff changeset
   712
        if isinstance(body, list):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   713
            body = body[0]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   714
        body_content = body.getcontent()
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   715
        body_type = body_content.getLocalTag()
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   716
        if body_type in ["FBD", "LD", "SFC"]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   717
            undefined_blocks = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   718
            for instance in body.getcontentInstances():
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
   719
                if isinstance(instance, (InVariableClass, OutVariableClass,
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   720
                                         InOutVariableClass)):
1322
0a9227f743b3 Fixed xmlclass for working with included files, adding support for SimpleType elements and solving ambiguity in extension class when different elements share the same name and parent name
Laurent Bessard
parents: 1315
diff changeset
   721
                    expression = instance.getexpression()
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   722
                    var_type = self.GetVariableType(expression)
1766
c1e5b9f19483 clean-up: fix PEP8 E129 visually indented line with same indent as next logical line
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1765
diff changeset
   723
                    if isinstance(pou, TransitionObjClass) and expression == pou.getname():
822
050045c32d98 Fix bug in PLCGenerator connection types not computed for SFC actions and transitions body
laurent
parents: 814
diff changeset
   724
                        var_type = "BOOL"
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   725
                    elif (not isinstance(pou, (TransitionObjClass, ActionObjClass)) and
822
050045c32d98 Fix bug in PLCGenerator connection types not computed for SFC actions and transitions body
laurent
parents: 814
diff changeset
   726
                          pou.getpouType() == "function" and expression == pou.getname()):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   727
                        returntype_content = pou.interface.getreturnType().getcontent()
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   728
                        returntype_content_type = returntype_content.getLocalTag()
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   729
                        if returntype_content_type == "derived":
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   730
                            var_type = returntype_content.getname()
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   731
                        else:
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   732
                            var_type = returntype_content_type.upper()
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   733
                    elif var_type is None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   734
                        parts = expression.split("#")
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   735
                        if len(parts) > 1:
1181
21e6db77eb29 Fixed bug in PLC code generated with binary, octal and hexadecimal literals
Laurent Bessard
parents: 1134
diff changeset
   736
                            literal_prefix = parts[0].upper()
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
   737
                            var_type = self.LITERAL_TYPES.get(literal_prefix,
1181
21e6db77eb29 Fixed bug in PLC code generated with binary, octal and hexadecimal literals
Laurent Bessard
parents: 1134
diff changeset
   738
                                                              literal_prefix)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   739
                        elif expression.startswith("'"):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   740
                            var_type = "STRING"
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   741
                        elif expression.startswith('"'):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   742
                            var_type = "WSTRING"
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   743
                    if var_type is not None:
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   744
                        if isinstance(instance, (InVariableClass, InOutVariableClass)):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   745
                            for connection in self.ExtractRelatedConnections(instance.connectionPointOut):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   746
                                self.ConnectionTypes[connection] = var_type
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   747
                        if isinstance(instance, (OutVariableClass, InOutVariableClass)):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   748
                            self.ConnectionTypes[instance.connectionPointIn] = var_type
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   749
                            connected = self.GetConnectedConnector(instance.connectionPointIn, body)
1775
b45f2768fab1 clean-up: fix PEP8 E713 test for membership should be 'not in'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1769
diff changeset
   750
                            if connected is not None and connected not in self.ConnectionTypes:
1298
f034fb2b1aab Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents: 1297
diff changeset
   751
                                for related in self.ExtractRelatedConnections(connected):
f034fb2b1aab Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents: 1297
diff changeset
   752
                                    self.ConnectionTypes[related] = var_type
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   753
                elif isinstance(instance, (ContactClass, CoilClass)):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   754
                    for connection in self.ExtractRelatedConnections(instance.connectionPointOut):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   755
                        self.ConnectionTypes[connection] = "BOOL"
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   756
                    self.ConnectionTypes[instance.connectionPointIn] = "BOOL"
1298
f034fb2b1aab Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents: 1297
diff changeset
   757
                    for link in instance.connectionPointIn.getconnections():
f034fb2b1aab Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents: 1297
diff changeset
   758
                        connected = self.GetLinkedConnector(link, body)
1775
b45f2768fab1 clean-up: fix PEP8 E713 test for membership should be 'not in'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1769
diff changeset
   759
                        if connected is not None and connected not in self.ConnectionTypes:
1298
f034fb2b1aab Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents: 1297
diff changeset
   760
                            for related in self.ExtractRelatedConnections(connected):
f034fb2b1aab Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents: 1297
diff changeset
   761
                                self.ConnectionTypes[related] = "BOOL"
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   762
                elif isinstance(instance, LeftPowerRailClass):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   763
                    for connection in instance.getconnectionPointOut():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   764
                        for related in self.ExtractRelatedConnections(connection):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   765
                            self.ConnectionTypes[related] = "BOOL"
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   766
                elif isinstance(instance, RightPowerRailClass):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   767
                    for connection in instance.getconnectionPointIn():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   768
                        self.ConnectionTypes[connection] = "BOOL"
1298
f034fb2b1aab Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents: 1297
diff changeset
   769
                        for link in connection.getconnections():
f034fb2b1aab Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents: 1297
diff changeset
   770
                            connected = self.GetLinkedConnector(link, body)
1775
b45f2768fab1 clean-up: fix PEP8 E713 test for membership should be 'not in'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1769
diff changeset
   771
                            if connected is not None and connected not in self.ConnectionTypes:
1298
f034fb2b1aab Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents: 1297
diff changeset
   772
                                for related in self.ExtractRelatedConnections(connected):
f034fb2b1aab Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents: 1297
diff changeset
   773
                                    self.ConnectionTypes[related] = "BOOL"
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   774
                elif isinstance(instance, TransitionClass):
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   775
                    content = instance.getconditionContent()
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   776
                    if content["type"] == "connection":
1298
f034fb2b1aab Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents: 1297
diff changeset
   777
                        self.ConnectionTypes[content["value"]] = "BOOL"
1603
18279f12a6be fix issue with compiling SFC POU with non-connected transition (type "connection")
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1581
diff changeset
   778
                        connections = content["value"].getconnections()
18279f12a6be fix issue with compiling SFC POU with non-connected transition (type "connection")
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1581
diff changeset
   779
                        if not connections:
1765
ccf59c1f0b45 clean-up: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1763
diff changeset
   780
                            raise PLCGenException(
ccf59c1f0b45 clean-up: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1763
diff changeset
   781
                                _("SFC transition in POU \"%s\" must be connected.") % self.Name)
ccf59c1f0b45 clean-up: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1763
diff changeset
   782
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1626
diff changeset
   783
                        for link in connections:
1298
f034fb2b1aab Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents: 1297
diff changeset
   784
                            connected = self.GetLinkedConnector(link, body)
1775
b45f2768fab1 clean-up: fix PEP8 E713 test for membership should be 'not in'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1769
diff changeset
   785
                            if connected is not None and connected not in self.ConnectionTypes:
1298
f034fb2b1aab Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents: 1297
diff changeset
   786
                                for related in self.ExtractRelatedConnections(connected):
f034fb2b1aab Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents: 1297
diff changeset
   787
                                    self.ConnectionTypes[related] = "BOOL"
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   788
                elif isinstance(instance, ContinuationClass):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   789
                    name = instance.getname()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   790
                    connector = None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   791
                    var_type = "ANY"
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   792
                    for element in body.getcontentInstances():
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   793
                        if isinstance(element, ConnectorClass) and element.getname() == name:
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   794
                            if connector is not None:
1765
ccf59c1f0b45 clean-up: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1763
diff changeset
   795
                                raise PLCGenException(
ccf59c1f0b45 clean-up: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1763
diff changeset
   796
                                    _("More than one connector found corresponding to \"{a1}\" continuation in \"{a2}\" POU").
ccf59c1f0b45 clean-up: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1763
diff changeset
   797
                                    format(a1=name, a2=self.Name))
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   798
                            connector = element
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   799
                    if connector is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   800
                        undefined = [instance.connectionPointOut, connector.connectionPointIn]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   801
                        connected = self.GetConnectedConnector(connector.connectionPointIn, body)
1298
f034fb2b1aab Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents: 1297
diff changeset
   802
                        if connected is not None:
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   803
                            undefined.append(connected)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   804
                        related = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   805
                        for connection in undefined:
1763
bcc07ff2362c clean-up: fix PEP8 W601 .has_key() is deprecated, use 'in'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1753
diff changeset
   806
                            if connection in self.ConnectionTypes:
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   807
                                var_type = self.ConnectionTypes[connection]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   808
                            else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   809
                                related.extend(self.ExtractRelatedConnections(connection))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   810
                        if var_type.startswith("ANY") and len(related) > 0:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   811
                            self.RelatedConnections.append(related)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   812
                        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   813
                            for connection in related:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   814
                                self.ConnectionTypes[connection] = var_type
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   815
                    else:
1765
ccf59c1f0b45 clean-up: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1763
diff changeset
   816
                        raise PLCGenException(
ccf59c1f0b45 clean-up: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1763
diff changeset
   817
                            _("No connector found corresponding to \"{a1}\" continuation in \"{a2}\" POU").
ccf59c1f0b45 clean-up: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1763
diff changeset
   818
                            format(a1=name, a2=self.Name))
ccf59c1f0b45 clean-up: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1763
diff changeset
   819
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   820
                elif isinstance(instance, BlockClass):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   821
                    block_infos = self.GetBlockType(instance.gettypeName(), "undefined")
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   822
                    if block_infos is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   823
                        self.ComputeBlockInputTypes(instance, block_infos, body)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   824
                    else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   825
                        for variable in instance.inputVariables.getvariable():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   826
                            connected = self.GetConnectedConnector(variable.connectionPointIn, body)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   827
                            if connected is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   828
                                var_type = self.ConnectionTypes.get(connected, None)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   829
                                if var_type is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   830
                                    self.ConnectionTypes[variable.connectionPointIn] = var_type
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   831
                                else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   832
                                    related = self.ExtractRelatedConnections(connected)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   833
                                    related.append(variable.connectionPointIn)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   834
                                    self.RelatedConnections.append(related)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   835
                        undefined_blocks.append(instance)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   836
            for instance in undefined_blocks:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   837
                block_infos = self.GetBlockType(instance.gettypeName(), tuple([self.ConnectionTypes.get(variable.connectionPointIn, "ANY") for variable in instance.inputVariables.getvariable() if variable.getformalParameter() != "EN"]))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   838
                if block_infos is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   839
                    self.ComputeBlockInputTypes(instance, block_infos, body)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   840
                else:
1765
ccf59c1f0b45 clean-up: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1763
diff changeset
   841
                    raise PLCGenException(
ccf59c1f0b45 clean-up: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1763
diff changeset
   842
                        _("No informations found for \"%s\" block") % (instance.gettypeName()))
822
050045c32d98 Fix bug in PLCGenerator connection types not computed for SFC actions and transitions body
laurent
parents: 814
diff changeset
   843
            if body_type == "SFC":
050045c32d98 Fix bug in PLCGenerator connection types not computed for SFC actions and transitions body
laurent
parents: 814
diff changeset
   844
                previous_tagname = self.TagName
050045c32d98 Fix bug in PLCGenerator connection types not computed for SFC actions and transitions body
laurent
parents: 814
diff changeset
   845
                for action in pou.getactionList():
1948
b9a3f771aaab Moved some definitions away from controller class, and adaped references them through all code.
Edouard Tisserant
parents: 1881
diff changeset
   846
                    self.TagName = ComputePouActionName(self.Name, action.getname())
822
050045c32d98 Fix bug in PLCGenerator connection types not computed for SFC actions and transitions body
laurent
parents: 814
diff changeset
   847
                    self.ComputeConnectionTypes(action)
050045c32d98 Fix bug in PLCGenerator connection types not computed for SFC actions and transitions body
laurent
parents: 814
diff changeset
   848
                for transition in pou.gettransitionList():
1948
b9a3f771aaab Moved some definitions away from controller class, and adaped references them through all code.
Edouard Tisserant
parents: 1881
diff changeset
   849
                    self.TagName = ComputePouTransitionName(self.Name, transition.getname())
822
050045c32d98 Fix bug in PLCGenerator connection types not computed for SFC actions and transitions body
laurent
parents: 814
diff changeset
   850
                    self.ComputeConnectionTypes(transition)
050045c32d98 Fix bug in PLCGenerator connection types not computed for SFC actions and transitions body
laurent
parents: 814
diff changeset
   851
                self.TagName = previous_tagname
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
   852
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   853
    def ComputeBlockInputTypes(self, instance, block_infos, body):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   854
        undefined = {}
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   855
        for variable in instance.outputVariables.getvariable():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   856
            output_name = variable.getformalParameter()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   857
            if output_name == "ENO":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   858
                for connection in self.ExtractRelatedConnections(variable.connectionPointOut):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   859
                    self.ConnectionTypes[connection] = "BOOL"
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   860
            else:
1847
6198190bc121 explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1846
diff changeset
   861
                for oname, otype, _oqualifier in block_infos["outputs"]:
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   862
                    if output_name == oname:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   863
                        if otype.startswith("ANY"):
1775
b45f2768fab1 clean-up: fix PEP8 E713 test for membership should be 'not in'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1769
diff changeset
   864
                            if otype not in undefined:
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   865
                                undefined[otype] = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   866
                            undefined[otype].append(variable.connectionPointOut)
1775
b45f2768fab1 clean-up: fix PEP8 E713 test for membership should be 'not in'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1769
diff changeset
   867
                        elif variable.connectionPointOut not in self.ConnectionTypes:
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   868
                            for connection in self.ExtractRelatedConnections(variable.connectionPointOut):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   869
                                self.ConnectionTypes[connection] = otype
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   870
        for variable in instance.inputVariables.getvariable():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   871
            input_name = variable.getformalParameter()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   872
            if input_name == "EN":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   873
                for connection in self.ExtractRelatedConnections(variable.connectionPointIn):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   874
                    self.ConnectionTypes[connection] = "BOOL"
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   875
            else:
1847
6198190bc121 explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1846
diff changeset
   876
                for iname, itype, _iqualifier in block_infos["inputs"]:
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   877
                    if input_name == iname:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   878
                        connected = self.GetConnectedConnector(variable.connectionPointIn, body)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   879
                        if itype.startswith("ANY"):
1775
b45f2768fab1 clean-up: fix PEP8 E713 test for membership should be 'not in'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1769
diff changeset
   880
                            if itype not in undefined:
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   881
                                undefined[itype] = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   882
                            undefined[itype].append(variable.connectionPointIn)
1298
f034fb2b1aab Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents: 1297
diff changeset
   883
                            if connected is not None:
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   884
                                undefined[itype].append(connected)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   885
                        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   886
                            self.ConnectionTypes[variable.connectionPointIn] = itype
1775
b45f2768fab1 clean-up: fix PEP8 E713 test for membership should be 'not in'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1769
diff changeset
   887
                            if connected is not None and connected not in self.ConnectionTypes:
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   888
                                for connection in self.ExtractRelatedConnections(connected):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   889
                                    self.ConnectionTypes[connection] = itype
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   890
        for var_type, connections in undefined.items():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   891
            related = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   892
            for connection in connections:
854
c10f2092c43a Fixing bug in PLCGenerator with user defined functions and standard overloaded function
Laurent Bessard
parents: 822
diff changeset
   893
                connection_type = self.ConnectionTypes.get(connection)
c10f2092c43a Fixing bug in PLCGenerator with user defined functions and standard overloaded function
Laurent Bessard
parents: 822
diff changeset
   894
                if connection_type and not connection_type.startswith("ANY"):
c10f2092c43a Fixing bug in PLCGenerator with user defined functions and standard overloaded function
Laurent Bessard
parents: 822
diff changeset
   895
                    var_type = connection_type
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   896
                else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   897
                    related.extend(self.ExtractRelatedConnections(connection))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   898
            if var_type.startswith("ANY") and len(related) > 0:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   899
                self.RelatedConnections.append(related)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   900
            else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   901
                for connection in related:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   902
                    self.ConnectionTypes[connection] = var_type
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   903
2258
c9915bc620cd Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1948
diff changeset
   904
    def GetUsedEno(self, body, connections):
c9915bc620cd Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1948
diff changeset
   905
        """
c9915bc620cd Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1948
diff changeset
   906
            Function checks whether value on given connection
c9915bc620cd Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1948
diff changeset
   907
            comes from block, that has used EN input and
c9915bc620cd Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1948
diff changeset
   908
            returns variable name for ENO output.
c9915bc620cd Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1948
diff changeset
   909
c9915bc620cd Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1948
diff changeset
   910
            This is needed to avoid value propagation from blocks
c9915bc620cd Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1948
diff changeset
   911
            with false signal on EN input.
c9915bc620cd Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1948
diff changeset
   912
c9915bc620cd Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1948
diff changeset
   913
        :param body:
c9915bc620cd Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1948
diff changeset
   914
            body of the block for that program is currently generated
c9915bc620cd Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1948
diff changeset
   915
        :param connections:
c9915bc620cd Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1948
diff changeset
   916
            connection, that's source is checked for EN/ENO usage
c9915bc620cd Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1948
diff changeset
   917
        :return:
c9915bc620cd Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1948
diff changeset
   918
            if EN/ENO are not used, then None is returned
c9915bc620cd Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1948
diff changeset
   919
            Otherwise BOOL variable corresponding to ENO
c9915bc620cd Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1948
diff changeset
   920
            output is returned.
c9915bc620cd Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1948
diff changeset
   921
        """
c9915bc620cd Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1948
diff changeset
   922
c9915bc620cd Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1948
diff changeset
   923
        if len(connections) != 1:
c9915bc620cd Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1948
diff changeset
   924
            return None
c9915bc620cd Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1948
diff changeset
   925
        ref_local_id = connections[0].getrefLocalId()
c9915bc620cd Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1948
diff changeset
   926
        blk = body.getcontentInstance(ref_local_id)
c9915bc620cd Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1948
diff changeset
   927
        if blk is None:
c9915bc620cd Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1948
diff changeset
   928
            return None
c9915bc620cd Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1948
diff changeset
   929
2275
1bb8afa02409 Improve fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2258
diff changeset
   930
        if not hasattr(blk, "inputVariables"):
1bb8afa02409 Improve fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2258
diff changeset
   931
            return None
1bb8afa02409 Improve fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2258
diff changeset
   932
2258
c9915bc620cd Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1948
diff changeset
   933
        for invar in blk.inputVariables.getvariable():
c9915bc620cd Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1948
diff changeset
   934
            if invar.getformalParameter() == "EN":
c9915bc620cd Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1948
diff changeset
   935
                if len(invar.getconnectionPointIn().getconnections()) > 0:
c9915bc620cd Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1948
diff changeset
   936
                    if blk.getinstanceName() is None:
c9915bc620cd Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1948
diff changeset
   937
                        var_name = "%s%d_ENO" % (blk.gettypeName(), blk.getlocalId())
c9915bc620cd Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1948
diff changeset
   938
                    else:
c9915bc620cd Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1948
diff changeset
   939
                        var_name = "%s.ENO" % blk.getinstanceName()
c9915bc620cd Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1948
diff changeset
   940
                    return var_name
c9915bc620cd Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1948
diff changeset
   941
        return None
c9915bc620cd Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1948
diff changeset
   942
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   943
    def ComputeProgram(self, pou):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   944
        body = pou.getbody()
2450
5024c19ca8f0 python3 support: pylint, W1652 # (deprecated-types-field) Accessing a deprecated fields on the types module
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2447
diff changeset
   945
        if isinstance(body, list):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   946
            body = body[0]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   947
        body_content = body.getcontent()
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   948
        body_type = body_content.getLocalTag()
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
   949
        if body_type in ["IL", "ST"]:
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   950
            text = body_content.getanyText()
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   951
            self.ParentGenerator.GeneratePouProgramInText(text.upper())
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
   952
            self.Program = [(ReIndentText(text, len(self.CurrentIndent)),
1878
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1864
diff changeset
   953
                             (self.TagName, "body", len(self.CurrentIndent)))]
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   954
        elif body_type == "SFC":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   955
            self.IndentRight()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   956
            for instance in body.getcontentInstances():
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   957
                if isinstance(instance, StepClass):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   958
                    self.GenerateSFCStep(instance, pou)
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   959
                elif isinstance(instance, ActionBlockClass):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   960
                    self.GenerateSFCStepActions(instance, pou)
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   961
                elif isinstance(instance, TransitionClass):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   962
                    self.GenerateSFCTransition(instance, pou)
1298
f034fb2b1aab Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents: 1297
diff changeset
   963
                elif isinstance(instance, JumpStepClass):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   964
                    self.GenerateSFCJump(instance, pou)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   965
            if len(self.InitialSteps) > 0 and len(self.SFCComputedBlocks) > 0:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   966
                action_name = "COMPUTE_FUNCTION_BLOCKS"
1739
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   967
                action_infos = {"qualifier": "S", "content": action_name}
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   968
                self.SFCNetworks["Steps"][self.InitialSteps[0]]["actions"].append(action_infos)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   969
                self.SFCNetworks["Actions"][action_name] = (self.SFCComputedBlocks, ())
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   970
                self.Program = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   971
            self.IndentLeft()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   972
            for initialstep in self.InitialSteps:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   973
                self.ComputeSFCStep(initialstep)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   974
        else:
1739
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   975
            otherInstances = {"outVariables&coils": [], "blocks": [], "connectors": []}
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   976
            orderedInstances = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   977
            for instance in body.getcontentInstances():
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   978
                if isinstance(instance, (OutVariableClass, InOutVariableClass, BlockClass)):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   979
                    executionOrderId = instance.getexecutionOrderId()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   980
                    if executionOrderId > 0:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   981
                        orderedInstances.append((executionOrderId, instance))
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   982
                    elif isinstance(instance, (OutVariableClass, InOutVariableClass)):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   983
                        otherInstances["outVariables&coils"].append(instance)
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   984
                    elif isinstance(instance, BlockClass):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   985
                        otherInstances["blocks"].append(instance)
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   986
                elif isinstance(instance, ConnectorClass):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   987
                    otherInstances["connectors"].append(instance)
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   988
                elif isinstance(instance, CoilClass):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   989
                    otherInstances["outVariables&coils"].append(instance)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   990
            orderedInstances.sort()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   991
            otherInstances["outVariables&coils"].sort(SortInstances)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   992
            otherInstances["blocks"].sort(SortInstances)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   993
            instances = [instance for (executionOrderId, instance) in orderedInstances]
1048
b450202605ab Fixed bug in program elements computation order in PLCGenerator
Laurent Bessard
parents: 1032
diff changeset
   994
            instances.extend(otherInstances["outVariables&coils"] + otherInstances["blocks"] + otherInstances["connectors"])
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   995
            for instance in instances:
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
   996
                if isinstance(instance, (OutVariableClass, InOutVariableClass)):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   997
                    connections = instance.connectionPointIn.getconnections()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   998
                    if connections is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
   999
                        expression = self.ComputeExpression(body, connections)
1239
d1f6ea56555d Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents: 1183
diff changeset
  1000
                        if expression is not None:
2258
c9915bc620cd Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1948
diff changeset
  1001
                            eno_var = self.GetUsedEno(body, connections)
c9915bc620cd Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1948
diff changeset
  1002
                            if eno_var is not None:
c9915bc620cd Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1948
diff changeset
  1003
                                self.Program += [(self.CurrentIndent + "IF %s" % eno_var, ())]
c9915bc620cd Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1948
diff changeset
  1004
                                self.Program += [(" THEN\n  ", ())]
c9915bc620cd Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1948
diff changeset
  1005
                                self.IndentRight()
c9915bc620cd Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1948
diff changeset
  1006
1239
d1f6ea56555d Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents: 1183
diff changeset
  1007
                            self.Program += [(self.CurrentIndent, ()),
1322
0a9227f743b3 Fixed xmlclass for working with included files, adding support for SimpleType elements and solving ambiguity in extension class when different elements share the same name and parent name
Laurent Bessard
parents: 1315
diff changeset
  1008
                                             (instance.getexpression(), (self.TagName, "io_variable", instance.getlocalId(), "expression")),
1239
d1f6ea56555d Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents: 1183
diff changeset
  1009
                                             (" := ", ())]
d1f6ea56555d Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents: 1183
diff changeset
  1010
                            self.Program += expression
d1f6ea56555d Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents: 1183
diff changeset
  1011
                            self.Program += [(";\n", ())]
2258
c9915bc620cd Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1948
diff changeset
  1012
c9915bc620cd Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1948
diff changeset
  1013
                            if eno_var is not None:
c9915bc620cd Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1948
diff changeset
  1014
                                self.IndentLeft()
c9915bc620cd Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1948
diff changeset
  1015
                                self.Program += [(self.CurrentIndent + "END_IF;\n", ())]
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
  1016
                elif isinstance(instance, BlockClass):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1017
                    block_type = instance.gettypeName()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1018
                    self.ParentGenerator.GeneratePouProgram(block_type)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1019
                    block_infos = self.GetBlockType(block_type, tuple([self.ConnectionTypes.get(variable.connectionPointIn, "ANY") for variable in instance.inputVariables.getvariable() if variable.getformalParameter() != "EN"]))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1020
                    if block_infos is None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1021
                        block_infos = self.GetBlockType(block_type)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1022
                    if block_infos is None:
1765
ccf59c1f0b45 clean-up: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1763
diff changeset
  1023
                        raise PLCGenException(
ccf59c1f0b45 clean-up: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1763
diff changeset
  1024
                            _("Undefined block type \"{a1}\" in \"{a2}\" POU").
ccf59c1f0b45 clean-up: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1763
diff changeset
  1025
                            format(a1=block_type, a2=self.Name))
1134
1c7a4ad86aa1 Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents: 1048
diff changeset
  1026
                    try:
1310
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1027
                        self.GenerateBlock(instance, block_infos, body, None)
2418
5587c490a070 Use python 3 compatible exception syntax everywhere
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2277
diff changeset
  1028
                    except ValueError as e:
2447
1c04a50dc7ff python3 support: pylint, W1645 # (exception-message-attribute) Exception.message removed in Python 3
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2432
diff changeset
  1029
                        raise PLCGenException(str(e))
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
  1030
                elif isinstance(instance, ConnectorClass):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1031
                    connector = instance.getname()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1032
                    if self.ComputedConnectors.get(connector, None):
1239
d1f6ea56555d Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents: 1183
diff changeset
  1033
                        continue
d1f6ea56555d Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents: 1183
diff changeset
  1034
                    expression = self.ComputeExpression(body, instance.connectionPointIn.getconnections())
d1f6ea56555d Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents: 1183
diff changeset
  1035
                    if expression is not None:
d1f6ea56555d Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents: 1183
diff changeset
  1036
                        self.ComputedConnectors[connector] = expression
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
  1037
                elif isinstance(instance, CoilClass):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1038
                    connections = instance.connectionPointIn.getconnections()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1039
                    if connections is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1040
                        coil_info = (self.TagName, "coil", instance.getlocalId())
1239
d1f6ea56555d Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents: 1183
diff changeset
  1041
                        expression = self.ComputeExpression(body, connections)
d1f6ea56555d Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents: 1183
diff changeset
  1042
                        if expression is not None:
d1f6ea56555d Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents: 1183
diff changeset
  1043
                            expression = self.ExtractModifier(instance, expression, coil_info)
d1f6ea56555d Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents: 1183
diff changeset
  1044
                            self.Program += [(self.CurrentIndent, ())]
1322
0a9227f743b3 Fixed xmlclass for working with included files, adding support for SimpleType elements and solving ambiguity in extension class when different elements share the same name and parent name
Laurent Bessard
parents: 1315
diff changeset
  1045
                            self.Program += [(instance.getvariable(), coil_info + ("reference",))]
1239
d1f6ea56555d Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents: 1183
diff changeset
  1046
                            self.Program += [(" := ", ())] + expression + [(";\n", ())]
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
  1047
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1048
    def FactorizePaths(self, paths):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1049
        same_paths = {}
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1050
        uncomputed_index = range(len(paths))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1051
        factorized_paths = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1052
        for num, path in enumerate(paths):
2450
5024c19ca8f0 python3 support: pylint, W1652 # (deprecated-types-field) Accessing a deprecated fields on the types module
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2447
diff changeset
  1053
            if isinstance(path, list):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1054
                if len(path) > 1:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1055
                    str_path = str(path[-1:])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1056
                    same_paths.setdefault(str_path, [])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1057
                    same_paths[str_path].append((path[:-1], num))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1058
            else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1059
                factorized_paths.append(path)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1060
                uncomputed_index.remove(num)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1061
        for same_path, elements in same_paths.items():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1062
            if len(elements) > 1:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1063
                elements_paths = self.FactorizePaths([path for path, num in elements])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1064
                if len(elements_paths) > 1:
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
  1065
                    factorized_paths.append([tuple(elements_paths)] + eval(same_path))
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1066
                else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1067
                    factorized_paths.append(elements_paths + eval(same_path))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1068
                for path, num in elements:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1069
                    uncomputed_index.remove(num)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1070
        for num in uncomputed_index:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1071
            factorized_paths.append(paths[num])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1072
        factorized_paths.sort()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1073
        return factorized_paths
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1074
1310
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1075
    def GenerateBlock(self, block, block_infos, body, link, order=False, to_inout=False):
1864
9e64afb38963 fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1863
diff changeset
  1076
9e64afb38963 fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1863
diff changeset
  1077
        def _GetBlockName(name, type):
9e64afb38963 fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1863
diff changeset
  1078
            """function returns name of function or function block instance"""
9e64afb38963 fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1863
diff changeset
  1079
            if name:
9e64afb38963 fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1863
diff changeset
  1080
                # function blocks
9e64afb38963 fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1863
diff changeset
  1081
                blockname = "{a1}({a2})".format(a1=name, a2=type)
9e64afb38963 fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1863
diff changeset
  1082
            else:
9e64afb38963 fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1863
diff changeset
  1083
                # functions
9e64afb38963 fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1863
diff changeset
  1084
                blockname = type
9e64afb38963 fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1863
diff changeset
  1085
            return blockname
9e64afb38963 fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1863
diff changeset
  1086
9e64afb38963 fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1863
diff changeset
  1087
        def _RaiseUnconnectedInOutError(name, type, parameter, place):
9e64afb38963 fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1863
diff changeset
  1088
            blockname = _GetBlockName(name, type)
9e64afb38963 fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1863
diff changeset
  1089
            raise ValueError(
9e64afb38963 fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1863
diff changeset
  1090
                _("InOut variable {a1} in block {a2} in POU {a3} must be connected.").
9e64afb38963 fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1863
diff changeset
  1091
                format(a1=parameter, a2=blockname, a3=place))
9e64afb38963 fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1863
diff changeset
  1092
1310
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1093
        name = block.getinstanceName()
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1094
        type = block.gettypeName()
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1095
        executionOrderId = block.getexecutionOrderId()
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1096
        input_variables = block.inputVariables.getvariable()
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1097
        output_variables = block.outputVariables.getvariable()
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1098
        inout_variables = {}
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1099
        for input_variable in input_variables:
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1100
            for output_variable in output_variables:
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1101
                if input_variable.getformalParameter() == output_variable.getformalParameter():
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1102
                    inout_variables[input_variable.getformalParameter()] = ""
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1103
        input_names = [input[0] for input in block_infos["inputs"]]
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1104
        output_names = [output[0] for output in block_infos["outputs"]]
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1105
        if block_infos["type"] == "function":
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1106
            if not self.ComputedBlocks.get(block, False) and not order:
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1107
                self.ComputedBlocks[block] = True
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1108
                connected_vars = []
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1109
                if not block_infos["extensible"]:
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
  1110
                    input_connected = dict([("EN", None)] +
1310
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1111
                                           [(input_name, None) for input_name in input_names])
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1112
                    for variable in input_variables:
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1113
                        parameter = variable.getformalParameter()
1763
bcc07ff2362c clean-up: fix PEP8 W601 .has_key() is deprecated, use 'in'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1753
diff changeset
  1114
                        if parameter in input_connected:
1310
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1115
                            input_connected[parameter] = variable
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1116
                    if input_connected["EN"] is None:
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1117
                        input_connected.pop("EN")
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1118
                        input_parameters = input_names
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1119
                    else:
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1120
                        input_parameters = ["EN"] + input_names
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1121
                else:
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1122
                    input_connected = dict([(variable.getformalParameter(), variable)
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1123
                                            for variable in input_variables])
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1124
                    input_parameters = [variable.getformalParameter()
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1125
                                        for variable in input_variables]
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1126
                one_input_connected = False
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1127
                all_input_connected = True
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1128
                for i, parameter in enumerate(input_parameters):
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1129
                    variable = input_connected.get(parameter)
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1130
                    if variable is not None:
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1131
                        input_info = (self.TagName, "block", block.getlocalId(), "input", i)
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1132
                        connections = variable.connectionPointIn.getconnections()
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1133
                        if connections is not None:
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1134
                            if parameter != "EN":
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1135
                                one_input_connected = True
1763
bcc07ff2362c clean-up: fix PEP8 W601 .has_key() is deprecated, use 'in'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1753
diff changeset
  1136
                            if parameter in inout_variables:
1310
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1137
                                expression = self.ComputeExpression(body, connections, executionOrderId > 0, True)
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1138
                                if expression is not None:
1863
46d9955e1101 fix error for functions with VAR_IN_OUT
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1862
diff changeset
  1139
                                    inout_variables[parameter] = expression
1864
9e64afb38963 fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1863
diff changeset
  1140
                                else:
9e64afb38963 fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1863
diff changeset
  1141
                                    _RaiseUnconnectedInOutError(name, type, parameter, self.Name)
1310
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1142
                            else:
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1143
                                expression = self.ComputeExpression(body, connections, executionOrderId > 0)
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1144
                            if expression is not None:
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1145
                                connected_vars.append(([(parameter, input_info), (" := ", ())],
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1146
                                                       self.ExtractModifier(variable, expression, input_info)))
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1147
                        else:
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1148
                            all_input_connected = False
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1149
                    else:
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1150
                        all_input_connected = False
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1151
                if len(output_variables) > 1 or not all_input_connected:
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1152
                    vars = [name + value for name, value in connected_vars]
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1153
                else:
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1154
                    vars = [value for name, value in connected_vars]
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1155
                if one_input_connected:
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1156
                    for i, variable in enumerate(output_variables):
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1157
                        parameter = variable.getformalParameter()
1775
b45f2768fab1 clean-up: fix PEP8 E713 test for membership should be 'not in'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1769
diff changeset
  1158
                        if parameter not in inout_variables and parameter in output_names + ["", "ENO"]:
1310
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1159
                            if variable.getformalParameter() == "":
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1160
                                variable_name = "%s%d" % (type, block.getlocalId())
1310
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1161
                            else:
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1162
                                variable_name = "%s%d_%s" % (type, block.getlocalId(), parameter)
1310
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1163
                            if self.Interface[-1][0] != "VAR" or self.Interface[-1][1] is not None or self.Interface[-1][2]:
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1164
                                self.Interface.append(("VAR", None, False, []))
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1165
                            if variable.connectionPointOut in self.ConnectionTypes:
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1166
                                self.Interface[-1][3].append((self.ConnectionTypes[variable.connectionPointOut], variable_name, None, None))
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1167
                            else:
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1168
                                self.Interface[-1][3].append(("ANY", variable_name, None, None))
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1169
                            if len(output_variables) > 1 and parameter not in ["", "OUT"]:
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
  1170
                                vars.append([(parameter, (self.TagName, "block", block.getlocalId(), "output", i)),
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1171
                                             (" => %s" % variable_name, ())])
1310
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1172
                            else:
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1173
                                output_info = (self.TagName, "block", block.getlocalId(), "output", i)
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1174
                                output_name = variable_name
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1175
                    self.Program += [(self.CurrentIndent, ()),
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1176
                                     (output_name, output_info),
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1177
                                     (" := ", ()),
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1178
                                     (type, (self.TagName, "block", block.getlocalId(), "type")),
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1179
                                     ("(", ())]
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1180
                    self.Program += JoinList([(", ", ())], vars)
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1181
                    self.Program += [(");\n", ())]
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1182
                else:
1744
69dfdb26f600 clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1743
diff changeset
  1183
                    msg = _("\"{a1}\" function cancelled in \"{a2}\" POU: No input connected").format(a1=type, a2=self.TagName.split("::")[-1])
1581
2295fdc5c271 fix translation strings with multiple parameters
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1571
diff changeset
  1184
                    self.Warnings.append(msg)
1310
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1185
        elif block_infos["type"] == "functionBlock":
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1186
            if not self.ComputedBlocks.get(block, False) and not order:
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1187
                self.ComputedBlocks[block] = True
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1188
                vars = []
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1189
                offset_idx = 0
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1190
                for variable in input_variables:
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1191
                    parameter = variable.getformalParameter()
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1192
                    if parameter in input_names or parameter == "EN":
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1193
                        if parameter == "EN":
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1194
                            input_idx = 0
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1195
                            offset_idx = 1
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1196
                        else:
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1197
                            input_idx = offset_idx + input_names.index(parameter)
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1198
                        input_info = (self.TagName, "block", block.getlocalId(), "input", input_idx)
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1199
                        connections = variable.connectionPointIn.getconnections()
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1200
                        if connections is not None:
1763
bcc07ff2362c clean-up: fix PEP8 W601 .has_key() is deprecated, use 'in'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1753
diff changeset
  1201
                            expression = self.ComputeExpression(body, connections, executionOrderId > 0, parameter in inout_variables)
1310
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1202
                            if expression is not None:
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1203
                                vars.append([(parameter, input_info),
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1204
                                             (" := ", ())] + self.ExtractModifier(variable, expression, input_info))
1864
9e64afb38963 fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1863
diff changeset
  1205
                            elif parameter in inout_variables:
9e64afb38963 fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1863
diff changeset
  1206
                                _RaiseUnconnectedInOutError(name, type, parameter, self.Name)
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
  1207
                self.Program += [(self.CurrentIndent, ()),
1310
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1208
                                 (name, (self.TagName, "block", block.getlocalId(), "name")),
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1209
                                 ("(", ())]
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1210
                self.Program += JoinList([(", ", ())], vars)
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1211
                self.Program += [(");\n", ())]
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
  1212
1310
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1213
        if link is not None:
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1214
            connectionPoint = link.getposition()[-1]
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1215
            output_parameter = link.getformalParameter()
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1216
        else:
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1217
            connectionPoint = None
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1218
            output_parameter = None
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
  1219
1310
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1220
        output_variable = None
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1221
        output_idx = 0
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1222
        if output_parameter is not None:
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1223
            if output_parameter in output_names or output_parameter == "ENO":
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1224
                for variable in output_variables:
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1225
                    if variable.getformalParameter() == output_parameter:
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1226
                        output_variable = variable
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1227
                        if output_parameter != "ENO":
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1228
                            output_idx = output_names.index(output_parameter)
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1229
        else:
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1230
            for i, variable in enumerate(output_variables):
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1231
                blockPointx, blockPointy = variable.connectionPointOut.getrelPositionXY()
1766
c1e5b9f19483 clean-up: fix PEP8 E129 visually indented line with same indent as next logical line
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1765
diff changeset
  1232
                if connectionPoint is None or \
c1e5b9f19483 clean-up: fix PEP8 E129 visually indented line with same indent as next logical line
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1765
diff changeset
  1233
                   block.getx() + blockPointx == connectionPoint.getx() and \
c1e5b9f19483 clean-up: fix PEP8 E129 visually indented line with same indent as next logical line
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1765
diff changeset
  1234
                   block.gety() + blockPointy == connectionPoint.gety():
1310
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1235
                    output_variable = variable
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1236
                    output_parameter = variable.getformalParameter()
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1237
                    output_idx = i
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
  1238
1310
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1239
        if output_variable is not None:
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1240
            if block_infos["type"] == "function":
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1241
                output_info = (self.TagName, "block", block.getlocalId(), "output", output_idx)
1763
bcc07ff2362c clean-up: fix PEP8 W601 .has_key() is deprecated, use 'in'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1753
diff changeset
  1242
                if output_parameter in inout_variables:
1864
9e64afb38963 fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1863
diff changeset
  1243
                    for variable in input_variables:
9e64afb38963 fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1863
diff changeset
  1244
                        if variable.getformalParameter() == output_parameter:
9e64afb38963 fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1863
diff changeset
  1245
                            connections = variable.connectionPointIn.getconnections()
9e64afb38963 fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1863
diff changeset
  1246
                            if connections is not None:
9e64afb38963 fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1863
diff changeset
  1247
                                expression = self.ComputeExpression(
9e64afb38963 fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1863
diff changeset
  1248
                                    body, connections, executionOrderId > 0, True)
9e64afb38963 fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1863
diff changeset
  1249
                                output_value = expression
9e64afb38963 fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1863
diff changeset
  1250
                                break
1310
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1251
                else:
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1252
                    if output_parameter == "":
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1253
                        output_name = "%s%d" % (type, block.getlocalId())
1310
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1254
                    else:
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1255
                        output_name = "%s%d_%s" % (type, block.getlocalId(), output_parameter)
1310
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1256
                    output_value = [(output_name, output_info)]
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1257
                return self.ExtractModifier(output_variable, output_value, output_info)
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1258
            if block_infos["type"] == "functionBlock":
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1259
                output_info = (self.TagName, "block", block.getlocalId(), "output", output_idx)
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1260
                output_name = self.ExtractModifier(output_variable, [("%s.%s" % (name, output_parameter), output_info)], output_info)
1310
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1261
                if to_inout:
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1262
                    variable_name = "%s_%s" % (name, output_parameter)
1310
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1263
                    if not self.IsAlreadyDefined(variable_name):
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1264
                        if self.Interface[-1][0] != "VAR" or self.Interface[-1][1] is not None or self.Interface[-1][2]:
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1265
                            self.Interface.append(("VAR", None, False, []))
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1266
                        if variable.connectionPointOut in self.ConnectionTypes:
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1267
                            self.Interface[-1][3].append(
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1268
                                (self.ConnectionTypes[output_variable.connectionPointOut], variable_name, None, None))
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1269
                        else:
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1270
                            self.Interface[-1][3].append(("ANY", variable_name, None, None))
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1271
                        self.Program += [(self.CurrentIndent, ()),
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1272
                                         ("%s := " % variable_name, ())]
1310
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1273
                        self.Program += output_name
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1274
                        self.Program += [(";\n", ())]
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1275
                    return [(variable_name, ())]
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
  1276
                return output_name
1310
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1277
        if link is not None:
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1278
            if output_parameter is None:
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1279
                output_parameter = ""
1864
9e64afb38963 fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1863
diff changeset
  1280
            blockname = _GetBlockName(name, type)
1765
ccf59c1f0b45 clean-up: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1763
diff changeset
  1281
            raise ValueError(
ccf59c1f0b45 clean-up: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1763
diff changeset
  1282
                _("No output {a1} variable found in block {a2} in POU {a3}. Connection must be broken").
ccf59c1f0b45 clean-up: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1763
diff changeset
  1283
                format(a1=output_parameter, a2=blockname, a3=self.Name))
1310
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1284
1744
69dfdb26f600 clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1743
diff changeset
  1285
    def GeneratePaths(self, connections, body, order=False, to_inout=False):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1286
        paths = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1287
        for connection in connections:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1288
            localId = connection.getrefLocalId()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1289
            next = body.getcontentInstance(localId)
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
  1290
            if isinstance(next, LeftPowerRailClass):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1291
                paths.append(None)
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
  1292
            elif isinstance(next, (InVariableClass, InOutVariableClass)):
1322
0a9227f743b3 Fixed xmlclass for working with included files, adding support for SimpleType elements and solving ambiguity in extension class when different elements share the same name and parent name
Laurent Bessard
parents: 1315
diff changeset
  1293
                paths.append(str([(next.getexpression(), (self.TagName, "io_variable", localId, "expression"))]))
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
  1294
            elif isinstance(next, BlockClass):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1295
                block_type = next.gettypeName()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1296
                self.ParentGenerator.GeneratePouProgram(block_type)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1297
                block_infos = self.GetBlockType(block_type, tuple([self.ConnectionTypes.get(variable.connectionPointIn, "ANY") for variable in next.inputVariables.getvariable() if variable.getformalParameter() != "EN"]))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1298
                if block_infos is None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1299
                    block_infos = self.GetBlockType(block_type)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1300
                if block_infos is None:
1765
ccf59c1f0b45 clean-up: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1763
diff changeset
  1301
                    raise PLCGenException(
ccf59c1f0b45 clean-up: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1763
diff changeset
  1302
                        _("Undefined block type \"{a1}\" in \"{a2}\" POU").
ccf59c1f0b45 clean-up: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1763
diff changeset
  1303
                        format(a1=block_type, a2=self.Name))
1134
1c7a4ad86aa1 Fixed PLC code generator when interface of an already used POU has changed
Laurent Bessard
parents: 1048
diff changeset
  1304
                try:
1310
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1305
                    paths.append(str(self.GenerateBlock(next, block_infos, body, connection, order, to_inout)))
2418
5587c490a070 Use python 3 compatible exception syntax everywhere
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2277
diff changeset
  1306
                except ValueError as e:
2447
1c04a50dc7ff python3 support: pylint, W1645 # (exception-message-attribute) Exception.message removed in Python 3
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2432
diff changeset
  1307
                    raise PLCGenException(str(e))
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
  1308
            elif isinstance(next, ContinuationClass):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1309
                name = next.getname()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1310
                computed_value = self.ComputedConnectors.get(name, None)
1743
c3c3d1318130 clean-up: fix PEP8 E711 comparison to None should be 'if cond is not None:'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1742
diff changeset
  1311
                if computed_value is not None:
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1312
                    paths.append(str(computed_value))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1313
                else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1314
                    connector = None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1315
                    for instance in body.getcontentInstances():
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
  1316
                        if isinstance(instance, ConnectorClass) and instance.getname() == name:
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1317
                            if connector is not None:
1765
ccf59c1f0b45 clean-up: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1763
diff changeset
  1318
                                raise PLCGenException(
ccf59c1f0b45 clean-up: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1763
diff changeset
  1319
                                    _("More than one connector found corresponding to \"{a1}\" continuation in \"{a2}\" POU").
ccf59c1f0b45 clean-up: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1763
diff changeset
  1320
                                    format(a1=name, a2=self.Name))
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1321
                            connector = instance
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1322
                    if connector is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1323
                        connections = connector.connectionPointIn.getconnections()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1324
                        if connections is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1325
                            expression = self.ComputeExpression(body, connections, order)
1239
d1f6ea56555d Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents: 1183
diff changeset
  1326
                            if expression is not None:
d1f6ea56555d Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents: 1183
diff changeset
  1327
                                self.ComputedConnectors[name] = expression
d1f6ea56555d Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents: 1183
diff changeset
  1328
                                paths.append(str(expression))
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1329
                    else:
1765
ccf59c1f0b45 clean-up: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1763
diff changeset
  1330
                        raise PLCGenException(
ccf59c1f0b45 clean-up: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1763
diff changeset
  1331
                            _("No connector found corresponding to \"{a1}\" continuation in \"{a2}\" POU").
ccf59c1f0b45 clean-up: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1763
diff changeset
  1332
                            format(a1=name, a2=self.Name))
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
  1333
            elif isinstance(next, ContactClass):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1334
                contact_info = (self.TagName, "contact", next.getlocalId())
1322
0a9227f743b3 Fixed xmlclass for working with included files, adding support for SimpleType elements and solving ambiguity in extension class when different elements share the same name and parent name
Laurent Bessard
parents: 1315
diff changeset
  1335
                variable = str(self.ExtractModifier(next, [(next.getvariable(), contact_info + ("reference",))], contact_info))
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1336
                result = self.GeneratePaths(next.connectionPointIn.getconnections(), body, order)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1337
                if len(result) > 1:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1338
                    factorized_paths = self.FactorizePaths(result)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1339
                    if len(factorized_paths) > 1:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1340
                        paths.append([variable, tuple(factorized_paths)])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1341
                    else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1342
                        paths.append([variable] + factorized_paths)
2450
5024c19ca8f0 python3 support: pylint, W1652 # (deprecated-types-field) Accessing a deprecated fields on the types module
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2447
diff changeset
  1343
                elif isinstance(result[0], list):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1344
                    paths.append([variable] + result[0])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1345
                elif result[0] is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1346
                    paths.append([variable, result[0]])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1347
                else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1348
                    paths.append(variable)
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
  1349
            elif isinstance(next, CoilClass):
2277
068ccc02f5f2 Fix wrong ST code generation in case of several coils connected to each other.
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2275
diff changeset
  1350
                paths.append(self.GeneratePaths(next.connectionPointIn.getconnections(), body, order))
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1351
        return paths
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1352
1744
69dfdb26f600 clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1743
diff changeset
  1353
    def ComputePaths(self, paths, first=False):
2450
5024c19ca8f0 python3 support: pylint, W1652 # (deprecated-types-field) Accessing a deprecated fields on the types module
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2447
diff changeset
  1354
        if isinstance(paths, tuple):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1355
            if None in paths:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1356
                return [("TRUE", ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1357
            else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1358
                vars = [self.ComputePaths(path) for path in paths]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1359
                if first:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1360
                    return JoinList([(" OR ", ())], vars)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1361
                else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1362
                    return [("(", ())] + JoinList([(" OR ", ())], vars) + [(")", ())]
2450
5024c19ca8f0 python3 support: pylint, W1652 # (deprecated-types-field) Accessing a deprecated fields on the types module
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2447
diff changeset
  1363
        elif isinstance(paths, list):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1364
            vars = [self.ComputePaths(path) for path in paths]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1365
            return JoinList([(" AND ", ())], vars)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1366
        elif paths is None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1367
            return [("TRUE", ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1368
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1369
            return eval(paths)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1370
1744
69dfdb26f600 clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1743
diff changeset
  1371
    def ComputeExpression(self, body, connections, order=False, to_inout=False):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1372
        paths = self.GeneratePaths(connections, body, order, to_inout)
1239
d1f6ea56555d Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents: 1183
diff changeset
  1373
        if len(paths) == 0:
d1f6ea56555d Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents: 1183
diff changeset
  1374
            return None
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1375
        if len(paths) > 1:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1376
            factorized_paths = self.FactorizePaths(paths)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1377
            if len(factorized_paths) > 1:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1378
                paths = tuple(factorized_paths)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1379
            else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1380
                paths = factorized_paths[0]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1381
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1382
            paths = paths[0]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1383
        return self.ComputePaths(paths, True)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1384
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1385
    def ExtractModifier(self, variable, expression, var_info):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1386
        if variable.getnegated():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1387
            return [("NOT(", var_info + ("negated",))] + expression + [(")", ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1388
        else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1389
            storage = variable.getstorage()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1390
            if storage in ["set", "reset"]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1391
                self.Program += [(self.CurrentIndent + "IF ", var_info + (storage,))] + expression
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1392
                self.Program += [(" THEN\n  ", ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1393
                if storage == "set":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1394
                    return [("TRUE; (*set*)\n" + self.CurrentIndent + "END_IF", ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1395
                else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1396
                    return [("FALSE; (*reset*)\n" + self.CurrentIndent + "END_IF", ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1397
            edge = variable.getedge()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1398
            if edge == "rising":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1399
                return self.AddTrigger("R_TRIG", expression, var_info + ("rising",))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1400
            elif edge == "falling":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1401
                return self.AddTrigger("F_TRIG", expression, var_info + ("falling",))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1402
        return expression
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
  1403
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1404
    def AddTrigger(self, edge, expression, var_info):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1405
        if self.Interface[-1][0] != "VAR" or self.Interface[-1][1] is not None or self.Interface[-1][2]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1406
            self.Interface.append(("VAR", None, False, []))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1407
        i = 1
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1408
        name = "%s%d" % (edge, i)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1409
        while self.IsAlreadyDefined(name):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1410
            i += 1
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1411
            name = "%s%d" % (edge, i)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1412
        self.Interface[-1][3].append((edge, name, None, None))
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
  1413
        self.Program += [(self.CurrentIndent, ()), (name, var_info), ("(CLK := ", ())]
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1414
        self.Program += expression
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1415
        self.Program += [(");\n", ())]
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1416
        return [("%s.Q" % name, var_info)]
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
  1417
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1418
    def ExtractDivergenceInput(self, divergence, pou):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1419
        connectionPointIn = divergence.getconnectionPointIn()
1298
f034fb2b1aab Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents: 1297
diff changeset
  1420
        if connectionPointIn is not None:
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1421
            connections = connectionPointIn.getconnections()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1422
            if connections is not None and len(connections) == 1:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1423
                instanceLocalId = connections[0].getrefLocalId()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1424
                body = pou.getbody()
2450
5024c19ca8f0 python3 support: pylint, W1652 # (deprecated-types-field) Accessing a deprecated fields on the types module
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2447
diff changeset
  1425
                if isinstance(body, list):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1426
                    body = body[0]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1427
                return body.getcontentInstance(instanceLocalId)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1428
        return None
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1429
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1430
    def ExtractConvergenceInputs(self, convergence, pou):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1431
        instances = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1432
        for connectionPointIn in convergence.getconnectionPointIn():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1433
            connections = connectionPointIn.getconnections()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1434
            if connections is not None and len(connections) == 1:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1435
                instanceLocalId = connections[0].getrefLocalId()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1436
                body = pou.getbody()
2450
5024c19ca8f0 python3 support: pylint, W1652 # (deprecated-types-field) Accessing a deprecated fields on the types module
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2447
diff changeset
  1437
                if isinstance(body, list):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1438
                    body = body[0]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1439
                instances.append(body.getcontentInstance(instanceLocalId))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1440
        return instances
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1441
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1442
    def GenerateSFCStep(self, step, pou):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1443
        step_name = step.getname()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1444
        if step_name not in self.SFCNetworks["Steps"].keys():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1445
            if step.getinitialStep():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1446
                self.InitialSteps.append(step_name)
1739
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
  1447
            step_infos = {"id":          step.getlocalId(),
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
  1448
                          "initial":     step.getinitialStep(),
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
  1449
                          "transitions": [],
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
  1450
                          "actions":     []}
889
ac18acb6917f Fix bug when using feedback loop in SFC program instead of jump
Laurent Bessard
parents: 883
diff changeset
  1451
            self.SFCNetworks["Steps"][step_name] = step_infos
1298
f034fb2b1aab Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents: 1297
diff changeset
  1452
            if step.connectionPointIn is not None:
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1453
                instances = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1454
                connections = step.connectionPointIn.getconnections()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1455
                if connections is not None and len(connections) == 1:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1456
                    instanceLocalId = connections[0].getrefLocalId()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1457
                    body = pou.getbody()
2450
5024c19ca8f0 python3 support: pylint, W1652 # (deprecated-types-field) Accessing a deprecated fields on the types module
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2447
diff changeset
  1458
                    if isinstance(body, list):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1459
                        body = body[0]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1460
                    instance = body.getcontentInstance(instanceLocalId)
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
  1461
                    if isinstance(instance, TransitionClass):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1462
                        instances.append(instance)
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
  1463
                    elif isinstance(instance, SelectionConvergenceClass):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1464
                        instances.extend(self.ExtractConvergenceInputs(instance, pou))
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
  1465
                    elif isinstance(instance, SimultaneousDivergenceClass):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1466
                        transition = self.ExtractDivergenceInput(instance, pou)
1298
f034fb2b1aab Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents: 1297
diff changeset
  1467
                        if transition is not None:
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
  1468
                            if isinstance(transition, TransitionClass):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1469
                                instances.append(transition)
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
  1470
                            elif isinstance(transition, SelectionConvergenceClass):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1471
                                instances.extend(self.ExtractConvergenceInputs(transition, pou))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1472
                for instance in instances:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1473
                    self.GenerateSFCTransition(instance, pou)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1474
                    if instance in self.SFCNetworks["Transitions"].keys():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1475
                        target_info = (self.TagName, "transition", instance.getlocalId(), "to", step_infos["id"])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1476
                        self.SFCNetworks["Transitions"][instance]["to"].append([(step_name, target_info)])
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
  1477
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1478
    def GenerateSFCJump(self, jump, pou):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1479
        jump_target = jump.gettargetName()
1626
0779c1b18c01 check before program generation whether SFC step, that is referenced
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1614
diff changeset
  1480
        if not pou.hasstep(jump_target):
0779c1b18c01 check before program generation whether SFC step, that is referenced
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1614
diff changeset
  1481
            pname = pou.getname()
1765
ccf59c1f0b45 clean-up: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1763
diff changeset
  1482
            raise PLCGenException(
ccf59c1f0b45 clean-up: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1763
diff changeset
  1483
                _("SFC jump in pou \"{a1}\" refers to non-existent SFC step \"{a2}\"").
ccf59c1f0b45 clean-up: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1763
diff changeset
  1484
                format(a1=pname, a2=jump_target))
ccf59c1f0b45 clean-up: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1763
diff changeset
  1485
1298
f034fb2b1aab Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents: 1297
diff changeset
  1486
        if jump.connectionPointIn is not None:
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1487
            instances = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1488
            connections = jump.connectionPointIn.getconnections()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1489
            if connections is not None and len(connections) == 1:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1490
                instanceLocalId = connections[0].getrefLocalId()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1491
                body = pou.getbody()
2450
5024c19ca8f0 python3 support: pylint, W1652 # (deprecated-types-field) Accessing a deprecated fields on the types module
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2447
diff changeset
  1492
                if isinstance(body, list):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1493
                    body = body[0]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1494
                instance = body.getcontentInstance(instanceLocalId)
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
  1495
                if isinstance(instance, TransitionClass):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1496
                    instances.append(instance)
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
  1497
                elif isinstance(instance, SelectionConvergenceClass):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1498
                    instances.extend(self.ExtractConvergenceInputs(instance, pou))
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
  1499
                elif isinstance(instance, SimultaneousDivergenceClass):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1500
                    transition = self.ExtractDivergenceInput(instance, pou)
1298
f034fb2b1aab Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents: 1297
diff changeset
  1501
                    if transition is not None:
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
  1502
                        if isinstance(transition, TransitionClass):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1503
                            instances.append(transition)
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
  1504
                        elif isinstance(transition, SelectionConvergenceClass):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1505
                            instances.extend(self.ExtractConvergenceInputs(transition, pou))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1506
            for instance in instances:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1507
                self.GenerateSFCTransition(instance, pou)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1508
                if instance in self.SFCNetworks["Transitions"].keys():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1509
                    target_info = (self.TagName, "jump", jump.getlocalId(), "target")
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1510
                    self.SFCNetworks["Transitions"][instance]["to"].append([(jump_target, target_info)])
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
  1511
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1512
    def GenerateSFCStepActions(self, actionBlock, pou):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1513
        connections = actionBlock.connectionPointIn.getconnections()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1514
        if connections is not None and len(connections) == 1:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1515
            stepLocalId = connections[0].getrefLocalId()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1516
            body = pou.getbody()
2450
5024c19ca8f0 python3 support: pylint, W1652 # (deprecated-types-field) Accessing a deprecated fields on the types module
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2447
diff changeset
  1517
            if isinstance(body, list):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1518
                body = body[0]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1519
            step = body.getcontentInstance(stepLocalId)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1520
            self.GenerateSFCStep(step, pou)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1521
            step_name = step.getname()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1522
            if step_name in self.SFCNetworks["Steps"].keys():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1523
                actions = actionBlock.getactions()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1524
                for i, action in enumerate(actions):
1739
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
  1525
                    action_infos = {"id":        actionBlock.getlocalId(),
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
  1526
                                    "qualifier": action["qualifier"],
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
  1527
                                    "content":   action["value"],
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
  1528
                                    "num":       i}
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1529
                    if "duration" in action:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1530
                        action_infos["duration"] = action["duration"]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1531
                    if "indicator" in action:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1532
                        action_infos["indicator"] = action["indicator"]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1533
                    if action["type"] == "reference":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1534
                        self.GenerateSFCAction(action["value"], pou)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1535
                    else:
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1536
                        action_name = "%s_INLINE%d" % (step_name.upper(), self.GetActionNumber())
1768
691083b5682a clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1767
diff changeset
  1537
                        self.SFCNetworks["Actions"][action_name] = ([
691083b5682a clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1767
diff changeset
  1538
                            (self.CurrentIndent, ()),
691083b5682a clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1767
diff changeset
  1539
                            (action["value"], (
691083b5682a clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1767
diff changeset
  1540
                                self.TagName, "action_block", action_infos["id"],
691083b5682a clean-up: fix PEP8 E128 continuation line under-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1767
diff changeset
  1541
                                "action", i, "inline")),
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1542
                            ("\n", ())], ())
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1543
                        action_infos["content"] = action_name
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1544
                    self.SFCNetworks["Steps"][step_name]["actions"].append(action_infos)
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
  1545
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1546
    def GenerateSFCAction(self, action_name, pou):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1547
        if action_name not in self.SFCNetworks["Actions"].keys():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1548
            actionContent = pou.getaction(action_name)
1298
f034fb2b1aab Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents: 1297
diff changeset
  1549
            if actionContent is not None:
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1550
                previous_tagname = self.TagName
1948
b9a3f771aaab Moved some definitions away from controller class, and adaped references them through all code.
Edouard Tisserant
parents: 1881
diff changeset
  1551
                self.TagName = ComputePouActionName(self.Name, action_name)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1552
                self.ComputeProgram(actionContent)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1553
                self.SFCNetworks["Actions"][action_name] = (self.Program, (self.TagName, "name"))
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1554
                self.Program = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1555
                self.TagName = previous_tagname
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
  1556
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1557
    def GenerateSFCTransition(self, transition, pou):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1558
        if transition not in self.SFCNetworks["Transitions"].keys():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1559
            steps = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1560
            connections = transition.connectionPointIn.getconnections()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1561
            if connections is not None and len(connections) == 1:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1562
                instanceLocalId = connections[0].getrefLocalId()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1563
                body = pou.getbody()
2450
5024c19ca8f0 python3 support: pylint, W1652 # (deprecated-types-field) Accessing a deprecated fields on the types module
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2447
diff changeset
  1564
                if isinstance(body, list):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1565
                    body = body[0]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1566
                instance = body.getcontentInstance(instanceLocalId)
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
  1567
                if isinstance(instance, StepClass):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1568
                    steps.append(instance)
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
  1569
                elif isinstance(instance, SelectionDivergenceClass):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1570
                    step = self.ExtractDivergenceInput(instance, pou)
1298
f034fb2b1aab Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents: 1297
diff changeset
  1571
                    if step is not None:
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
  1572
                        if isinstance(step, StepClass):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1573
                            steps.append(step)
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
  1574
                        elif isinstance(step, SimultaneousConvergenceClass):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1575
                            steps.extend(self.ExtractConvergenceInputs(step, pou))
1297
cd639725fba5 Fixed ST program generating module
Laurent Bessard
parents: 1239
diff changeset
  1576
                elif isinstance(instance, SimultaneousConvergenceClass):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1577
                    steps.extend(self.ExtractConvergenceInputs(instance, pou))
1739
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
  1578
            transition_infos = {"id":       transition.getlocalId(),
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
  1579
                                "priority": transition.getpriority(),
1739
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
  1580
                                "from":     [],
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
  1581
                                "to":       [],
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
  1582
                                "content":  []}
889
ac18acb6917f Fix bug when using feedback loop in SFC program instead of jump
Laurent Bessard
parents: 883
diff changeset
  1583
            self.SFCNetworks["Transitions"][transition] = transition_infos
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1584
            transitionValues = transition.getconditionContent()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1585
            if transitionValues["type"] == "inline":
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1586
                transition_infos["content"] = [("\n%s:= " % self.CurrentIndent, ()),
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1587
                                               (transitionValues["value"], (self.TagName, "transition", transition.getlocalId(), "inline")),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1588
                                               (";\n", ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1589
            elif transitionValues["type"] == "reference":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1590
                transitionContent = pou.gettransition(transitionValues["value"])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1591
                transitionType = transitionContent.getbodyType()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1592
                transitionBody = transitionContent.getbody()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1593
                previous_tagname = self.TagName
1948
b9a3f771aaab Moved some definitions away from controller class, and adaped references them through all code.
Edouard Tisserant
parents: 1881
diff changeset
  1594
                self.TagName = ComputePouTransitionName(self.Name, transitionValues["value"])
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1595
                if transitionType == "IL":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1596
                    transition_infos["content"] = [(":\n", ()),
1450
44bf0ba866e9 Fixed SFC code generation bug detected by Mario
Edouard Tisserant
parents: 1420
diff changeset
  1597
                                                   (ReIndentText(transitionBody.getcontent().getanyText(), len(self.CurrentIndent)), (self.TagName, "body", len(self.CurrentIndent)))]
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1598
                elif transitionType == "ST":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1599
                    transition_infos["content"] = [("\n", ()),
1450
44bf0ba866e9 Fixed SFC code generation bug detected by Mario
Edouard Tisserant
parents: 1420
diff changeset
  1600
                                                   (ReIndentText(transitionBody.getcontent().getanyText(), len(self.CurrentIndent)), (self.TagName, "body", len(self.CurrentIndent)))]
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1601
                else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1602
                    for instance in transitionBody.getcontentInstances():
1769
4665ba25a0ba clean-up: fix PEP8 E125 continuation line with same indent as next logical line
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1768
diff changeset
  1603
                        if isinstance(instance, OutVariableClass) and instance.getexpression() == transitionValues["value"] or \
4665ba25a0ba clean-up: fix PEP8 E125 continuation line with same indent as next logical line
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1768
diff changeset
  1604
                           isinstance(instance, CoilClass) and instance.getvariable() == transitionValues["value"]:
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1605
                            connections = instance.connectionPointIn.getconnections()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1606
                            if connections is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1607
                                expression = self.ComputeExpression(transitionBody, connections)
1239
d1f6ea56555d Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents: 1183
diff changeset
  1608
                                if expression is not None:
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1609
                                    transition_infos["content"] = [("\n%s:= " % self.CurrentIndent, ())] + expression + [(";\n", ())]
1239
d1f6ea56555d Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents: 1183
diff changeset
  1610
                                    self.SFCComputedBlocks += self.Program
d1f6ea56555d Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents: 1183
diff changeset
  1611
                                    self.Program = []
1775
b45f2768fab1 clean-up: fix PEP8 E713 test for membership should be 'not in'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1769
diff changeset
  1612
                    if "content" not in transition_infos:
1765
ccf59c1f0b45 clean-up: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1763
diff changeset
  1613
                        raise PLCGenException(
ccf59c1f0b45 clean-up: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1763
diff changeset
  1614
                            _("Transition \"%s\" body must contain an output variable or coil referring to its name")
ccf59c1f0b45 clean-up: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1763
diff changeset
  1615
                            % transitionValues["value"])
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1616
                self.TagName = previous_tagname
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1617
            elif transitionValues["type"] == "connection":
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1618
                body = pou.getbody()
2450
5024c19ca8f0 python3 support: pylint, W1652 # (deprecated-types-field) Accessing a deprecated fields on the types module
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2447
diff changeset
  1619
                if isinstance(body, list):
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1620
                    body = body[0]
1298
f034fb2b1aab Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents: 1297
diff changeset
  1621
                connections = transitionValues["value"].getconnections()
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1622
                if connections is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1623
                    expression = self.ComputeExpression(body, connections)
1239
d1f6ea56555d Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents: 1183
diff changeset
  1624
                    if expression is not None:
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1625
                        transition_infos["content"] = [("\n%s:= " % self.CurrentIndent, ())] + expression + [(";\n", ())]
1239
d1f6ea56555d Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents: 1183
diff changeset
  1626
                        self.SFCComputedBlocks += self.Program
d1f6ea56555d Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents: 1183
diff changeset
  1627
                        self.Program = []
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1628
            for step in steps:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1629
                self.GenerateSFCStep(step, pou)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1630
                step_name = step.getname()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1631
                if step_name in self.SFCNetworks["Steps"].keys():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1632
                    transition_infos["from"].append([(step_name, (self.TagName, "transition", transition.getlocalId(), "from", step.getlocalId()))])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1633
                    self.SFCNetworks["Steps"][step_name]["transitions"].append(transition)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1634
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1635
    def ComputeSFCStep(self, step_name):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1636
        if step_name in self.SFCNetworks["Steps"].keys():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1637
            step_infos = self.SFCNetworks["Steps"].pop(step_name)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1638
            self.Program += [(self.CurrentIndent, ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1639
            if step_infos["initial"]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1640
                self.Program += [("INITIAL_", ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1641
            self.Program += [("STEP ", ()),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1642
                             (step_name, (self.TagName, "step", step_infos["id"], "name")),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1643
                             (":\n", ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1644
            actions = []
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1645
            self.IndentRight()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1646
            for action_infos in step_infos["actions"]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1647
                if action_infos.get("id", None) is not None:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1648
                    action_info = (self.TagName, "action_block", action_infos["id"], "action", action_infos["num"])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1649
                else:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1650
                    action_info = ()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1651
                actions.append(action_infos["content"])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1652
                self.Program += [(self.CurrentIndent, ()),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1653
                                 (action_infos["content"], action_info + ("reference",)),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1654
                                 ("(", ()),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1655
                                 (action_infos["qualifier"], action_info + ("qualifier",))]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1656
                if "duration" in action_infos:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1657
                    self.Program += [(", ", ()),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1658
                                     (action_infos["duration"], action_info + ("duration",))]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1659
                if "indicator" in action_infos:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1660
                    self.Program += [(", ", ()),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1661
                                     (action_infos["indicator"], action_info + ("indicator",))]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1662
                self.Program += [(");\n", ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1663
            self.IndentLeft()
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1664
            self.Program += [("%sEND_STEP\n\n" % self.CurrentIndent, ())]
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1665
            for action in actions:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1666
                self.ComputeSFCAction(action)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1667
            for transition in step_infos["transitions"]:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1668
                self.ComputeSFCTransition(transition)
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
  1669
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1670
    def ComputeSFCAction(self, action_name):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1671
        if action_name in self.SFCNetworks["Actions"].keys():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1672
            action_content, action_info = self.SFCNetworks["Actions"].pop(action_name)
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1673
            self.Program += [("%sACTION " % self.CurrentIndent, ()),
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1674
                             (action_name, action_info),
1298
f034fb2b1aab Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents: 1297
diff changeset
  1675
                             (":\n", ())]
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1676
            self.Program += action_content
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1677
            self.Program += [("%sEND_ACTION\n\n" % self.CurrentIndent, ())]
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
  1678
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1679
    def ComputeSFCTransition(self, transition):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1680
        if transition in self.SFCNetworks["Transitions"].keys():
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1681
            transition_infos = self.SFCNetworks["Transitions"].pop(transition)
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1682
            self.Program += [("%sTRANSITION" % self.CurrentIndent, ())]
1743
c3c3d1318130 clean-up: fix PEP8 E711 comparison to None should be 'if cond is not None:'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1742
diff changeset
  1683
            if transition_infos["priority"] is not None:
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1684
                self.Program += [(" (PRIORITY := ", ()),
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1685
                                 ("%d" % transition_infos["priority"], (self.TagName, "transition", transition_infos["id"], "priority")),
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1686
                                 (")", ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1687
            self.Program += [(" FROM ", ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1688
            if len(transition_infos["from"]) > 1:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1689
                self.Program += [("(", ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1690
                self.Program += JoinList([(", ", ())], transition_infos["from"])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1691
                self.Program += [(")", ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1692
            elif len(transition_infos["from"]) == 1:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1693
                self.Program += transition_infos["from"][0]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1694
            else:
1765
ccf59c1f0b45 clean-up: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1763
diff changeset
  1695
                raise PLCGenException(
ccf59c1f0b45 clean-up: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1763
diff changeset
  1696
                    _("Transition with content \"{a1}\" not connected to a previous step in \"{a2}\" POU").
ccf59c1f0b45 clean-up: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1763
diff changeset
  1697
                    format(a1=transition_infos["content"], a2=self.Name))
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1698
            self.Program += [(" TO ", ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1699
            if len(transition_infos["to"]) > 1:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1700
                self.Program += [("(", ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1701
                self.Program += JoinList([(", ", ())], transition_infos["to"])
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1702
                self.Program += [(")", ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1703
            elif len(transition_infos["to"]) == 1:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1704
                self.Program += transition_infos["to"][0]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1705
            else:
1765
ccf59c1f0b45 clean-up: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1763
diff changeset
  1706
                raise PLCGenException(
ccf59c1f0b45 clean-up: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1763
diff changeset
  1707
                    _("Transition with content \"{a1}\" not connected to a next step in \"{a2}\" POU").
ccf59c1f0b45 clean-up: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1763
diff changeset
  1708
                    format(a1=transition_infos["content"], a2=self.Name))
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1709
            self.Program += transition_infos["content"]
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1710
            self.Program += [("%sEND_TRANSITION\n\n" % self.CurrentIndent, ())]
1847
6198190bc121 explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1846
diff changeset
  1711
            for [(step_name, _step_infos)] in transition_infos["to"]:
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1712
                self.ComputeSFCStep(step_name)
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
  1713
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1714
    def GenerateProgram(self, pou):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1715
        self.ComputeInterface(pou)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1716
        self.ComputeConnectionTypes(pou)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1717
        self.ComputeProgram(pou)
1418
c97dc5281419 Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents: 1358
diff changeset
  1718
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1719
        program = [("%s " % self.Type, ()),
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1720
                   (self.Name, (self.TagName, "name"))]
1310
3d7fa2257b24 Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents: 1298
diff changeset
  1721
        if self.ReturnType is not None:
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1722
            program += [(" : ", ()),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1723
                        (self.ReturnType, (self.TagName, "return"))]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1724
        program += [("\n", ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1725
        if len(self.Interface) == 0:
1765
ccf59c1f0b45 clean-up: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1763
diff changeset
  1726
            raise PLCGenException(_("No variable defined in \"%s\" POU") % self.Name)
1739
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
  1727
        if len(self.Program) == 0:
1765
ccf59c1f0b45 clean-up: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1763
diff changeset
  1728
            raise PLCGenException(_("No body defined in \"%s\" POU") % self.Name)
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1729
        var_number = 0
1847
6198190bc121 explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1846
diff changeset
  1730
        for list_type, option, _located, variables in self.Interface:
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1731
            variable_type = errorVarTypes.get(list_type, "var_local")
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1732
            program += [("  %s" % list_type, ())]
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1733
            if option is not None:
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1734
                program += [(" %s" % option, (self.TagName, variable_type, (var_number, var_number + len(variables)), option.lower()))]
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1735
            program += [("\n", ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1736
            for var_type, var_name, var_address, var_initial in variables:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1737
                program += [("    ", ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1738
                if var_name:
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1739
                    program += [(var_name, (self.TagName, variable_type, var_number, "name")),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1740
                                (" ", ())]
1743
c3c3d1318130 clean-up: fix PEP8 E711 comparison to None should be 'if cond is not None:'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1742
diff changeset
  1741
                if var_address is not None:
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1742
                    program += [("AT ", ()),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1743
                                (var_address, (self.TagName, variable_type, var_number, "location")),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1744
                                (" ", ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1745
                program += [(": ", ()),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1746
                            (var_type, (self.TagName, variable_type, var_number, "type"))]
1743
c3c3d1318130 clean-up: fix PEP8 E711 comparison to None should be 'if cond is not None:'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1742
diff changeset
  1747
                if var_initial is not None:
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1748
                    program += [(" := ", ()),
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1749
                                (self.ParentGenerator.ComputeValue(var_initial, var_type), (self.TagName, variable_type, var_number, "initial value"))]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1750
                program += [(";\n", ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1751
                var_number += 1
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1752
            program += [("  END_VAR\n", ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1753
        program += [("\n", ())]
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1754
        program += self.Program
1734
750eeb7230a1 clean-up: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
  1755
        program += [("END_%s\n\n" % self.Type, ())]
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1756
        return program
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1757
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1734
diff changeset
  1758
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1759
def GenerateCurrentProgram(controler, project, errors, warnings):
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1760
    generator = ProgramGenerator(controler, project, errors, warnings)
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1761
    generator.GenerateProgram()
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
  1762
    return generator.GetGeneratedProgram()