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