author  Edouard Tisserant 
Tue, 16 Jun 2020 10:12:21 +0200  
changeset 2675  3ca5611d6308 
parent 2633  fecdb71e7514 
child 2727  6330e6bb345d 
permissions  rwrr 
814  1 
#!/usr/bin/env python 
2 
# * coding: utf8 * 

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 611313 automates supporting plcopen standard and CanFestival. 
814  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  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  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  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  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 021101301, USA. 
814  24 

1832
0f1081928d65
fix wrongimportorder. first standard modules are imported, then others
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1831
diff
changeset

25 

1881
091005ec69c4
fix pylint py3k conversion warning: "(noabsoluteimport) import missing `from __future__ import absolute_import`"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1878
diff
changeset

26 
from __future__ import absolute_import 
1832
0f1081928d65
fix wrongimportorder. first standard modules are imported, then others
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1831
diff
changeset

27 
import re 
2456
7373e3048167
python3 support: pylint,W1610 # (reducebuiltin) reduce builtin referenced
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2450
diff
changeset

28 
from functools import reduce 
2432
dbc065a2f7a5
python3 support: pylint, W1613 # (xrangebuiltin) xrange builtin referenced
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2418
diff
changeset

29 
from six.moves import xrange 
dbc065a2f7a5
python3 support: pylint, W1613 # (xrangebuiltin) xrange builtin referenced
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2418
diff
changeset

30 

1297  31 
from plcopen import PLCOpenParser 
814  32 
from plcopen.structures import * 
1948
b9a3f771aaab
Moved some definitions away from controller class, and adaped references them through all code.
Edouard Tisserant
parents:
1881
diff
changeset

33 
from plcopen.types_enums import * 
1832
0f1081928d65
fix wrongimportorder. first standard modules are imported, then others
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1831
diff
changeset

34 

814  35 

1418
c97dc5281419
Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents:
1358
diff
changeset

36 
# Dictionary associating PLCOpen variable categories to the corresponding 
814  37 
# IEC 611313 variable categories 
1739
ec153828ded2
cleanup: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset

38 
varTypeNames = {"localVars": "VAR", "tempVars": "VAR_TEMP", "inputVars": "VAR_INPUT", 
ec153828ded2
cleanup: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset

39 
"outputVars": "VAR_OUTPUT", "inOutVars": "VAR_IN_OUT", "externalVars": "VAR_EXTERNAL", 
ec153828ded2
cleanup: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset

40 
"globalVars": "VAR_GLOBAL", "accessVars": "VAR_ACCESS"} 
814  41 

42 

1418
c97dc5281419
Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents:
1358
diff
changeset

43 
# Dictionary associating PLCOpen POU categories to the corresponding 
814  44 
# IEC 611313 POU categories 
1739
ec153828ded2
cleanup: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset

45 
pouTypeNames = {"function": "FUNCTION", "functionBlock": "FUNCTION_BLOCK", "program": "PROGRAM"} 
814  46 

47 

48 
errorVarTypes = { 

49 
"VAR_INPUT": "var_input", 

50 
"VAR_OUTPUT": "var_output", 

51 
"VAR_INOUT": "var_inout", 

52 
} 

53 

1736
7e61baa047f0
cleanup: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset

54 

814  55 
def ReIndentText(text, nb_spaces): 
1736
7e61baa047f0
cleanup: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset

56 
""" Helper function for reindenting text """ 
814  57 
compute = "" 
58 
lines = text.splitlines() 

59 
if len(lines) > 0: 

60 
line_num = 0 

61 
while line_num < len(lines) and len(lines[line_num].strip()) == 0: 

62 
line_num += 1 

63 
if line_num < len(lines): 

64 
spaces = 0 

65 
while lines[line_num][spaces] == " ": 

66 
spaces += 1 

67 
indent = "" 

1847
6198190bc121
explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1846
diff
changeset

68 
for dummy in xrange(spaces, nb_spaces): 
814  69 
indent += " " 
70 
for line in lines: 

71 
if line != "": 

1734
750eeb7230a1
cleanup: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset

72 
compute += "%s%s\n" % (indent, line) 
814  73 
else: 
74 
compute += "\n" 

75 
return compute 

76 

1736
7e61baa047f0
cleanup: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset

77 

814  78 
def SortInstances(a, b): 
79 
ax, ay = int(a.getx()), int(a.gety()) 

80 
bx, by = int(b.getx()), int(b.gety()) 

81 
if abs(ay  by) < 10: 

82 
return cmp(ax, bx) 

83 
else: 

84 
return cmp(ay, by) 

85 

1736
7e61baa047f0
cleanup: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset

86 

1310
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

87 
def JoinList(separator, mylist): 
1736
7e61baa047f0
cleanup: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset

88 
""" Helper for emulate join on element list """ 
1739
ec153828ded2
cleanup: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset

89 
if len(mylist) > 0: 
1310
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

90 
return reduce(lambda x, y: x + separator + y, mylist) 
1739
ec153828ded2
cleanup: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset

91 
else: 
1310
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

92 
return mylist 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

93 

1782
5b6ad7a7fd9d
cleanup: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1775
diff
changeset

94 
#  
814  95 
# Specific exception for PLC generating errors 
1782
5b6ad7a7fd9d
cleanup: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1775
diff
changeset

96 
#  
814  97 

98 

99 
class PLCGenException(Exception): 

100 
pass 

101 

102 

1782
5b6ad7a7fd9d
cleanup: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1775
diff
changeset

103 
#  
814  104 
# Generator of PLC program 
1782
5b6ad7a7fd9d
cleanup: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1775
diff
changeset

105 
#  
814  106 

107 

1831
56b48961cc68
fix (oldstyleclass) Oldstyle class defined error for most parts of
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1782
diff
changeset

108 
class ProgramGenerator(object): 
814  109 

110 
# Create a new PCL program generator 

111 
def __init__(self, controler, project, errors, warnings): 

112 
# Keep reference of the controler and project 

113 
self.Controler = controler 

114 
self.Project = project 

115 
# Reset the internal variables used to generate PLC programs 

116 
self.Program = [] 

117 
self.DatatypeComputed = {} 

118 
self.PouComputed = {} 

119 
self.Errors = errors 

120 
self.Warnings = warnings 

121 

122 
# Compute value according to type given 

123 
def ComputeValue(self, value, var_type): 

124 
base_type = self.Controler.GetBaseType(var_type) 

1032
c4989e53f9c3
Fix bug defining string initial value using quotes
Laurent Bessard
parents:
893
diff
changeset

125 
if base_type == "STRING" and not value.startswith("'") and not value.endswith("'"): 
1734
750eeb7230a1
cleanup: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset

126 
return "'%s'" % value 
1032
c4989e53f9c3
Fix bug defining string initial value using quotes
Laurent Bessard
parents:
893
diff
changeset

127 
elif base_type == "WSTRING" and not value.startswith('"') and not value.endswith('"'): 
1734
750eeb7230a1
cleanup: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset

128 
return "\"%s\"" % value 
814  129 
return value 
130 

131 
# Generate a data type from its name 

132 
def GenerateDataType(self, datatype_name): 

133 
# Verify that data type hasn't been generated yet 

134 
if not self.DatatypeComputed.get(datatype_name, True): 

135 
# If not mark data type as computed 

136 
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

137 

814  138 
# Getting datatype model from project 
139 
datatype = self.Project.getdataType(datatype_name) 

1948
b9a3f771aaab
Moved some definitions away from controller class, and adaped references them through all code.
Edouard Tisserant
parents:
1881
diff
changeset

140 
tagname = ComputeDataTypeName(datatype.getname()) 
1418
c97dc5281419
Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents:
1358
diff
changeset

141 
datatype_def = [(" ", ()), 
814  142 
(datatype.getname(), (tagname, "name")), 
143 
(" : ", ())] 

144 
basetype_content = datatype.baseType.getcontent() 

1297  145 
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

146 
# Data type derived directly from a user defined type 
1297  147 
if basetype_content_type == "derived": 
148 
basetype_name = basetype_content.getname() 

814  149 
self.GenerateDataType(basetype_name) 
150 
datatype_def += [(basetype_name, (tagname, "base"))] 

151 
# Data type is a subrange 

1297  152 
elif basetype_content_type in ["subrangeSigned", "subrangeUnsigned"]: 
153 
base_type = basetype_content.baseType.getcontent() 

154 
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

155 
# Subrange derived directly from a user defined type 
1297  156 
if base_type_type == "derived": 
157 
basetype_name = base_type_type.getname() 

814  158 
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

159 
# Subrange derived directly from an elementary type 
814  160 
else: 
1297  161 
basetype_name = base_type_type 
162 
min_value = basetype_content.range.getlower() 

163 
max_value = basetype_content.range.getupper() 

814  164 
datatype_def += [(basetype_name, (tagname, "base")), 
165 
(" (", ()), 

1734
750eeb7230a1
cleanup: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset

166 
("%s" % min_value, (tagname, "lower")), 
814  167 
("..", ()), 
1734
750eeb7230a1
cleanup: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset

168 
("%s" % max_value, (tagname, "upper")), 
1740
b789b695b5c6
cleanup: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1739
diff
changeset

169 
(")", ())] 
814  170 
# Data type is an enumerated type 
1297  171 
elif basetype_content_type == "enum": 
814  172 
values = [[(value.getname(), (tagname, "value", i))] 
1297  173 
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

174 
basetype_content.xpath("ppx:values/ppx:value", 
1768
691083b5682a
cleanup: fix PEP8 E128 continuation line underindented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset

175 
namespaces=PLCOpenParser.NSMAP))] 
814  176 
datatype_def += [("(", ())] 
177 
datatype_def += JoinList([(", ", ())], values) 

178 
datatype_def += [(")", ())] 

179 
# Data type is an array 

1297  180 
elif basetype_content_type == "array": 
181 
base_type = basetype_content.baseType.getcontent() 

182 
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

183 
# Array derived directly from a user defined type 
1297  184 
if base_type_type == "derived": 
185 
basetype_name = base_type.getname() 

814  186 
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

187 
# Array derived directly from an elementary type 
814  188 
else: 
1297  189 
basetype_name = base_type_type.upper() 
1734
750eeb7230a1
cleanup: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset

190 
dimensions = [[("%s" % dimension.getlower(), (tagname, "range", i, "lower")), 
814  191 
("..", ()), 
1734
750eeb7230a1
cleanup: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset

192 
("%s" % dimension.getupper(), (tagname, "range", i, "upper"))] 
1297  193 
for i, dimension in enumerate(basetype_content.getdimension())] 
814  194 
datatype_def += [("ARRAY [", ())] 
195 
datatype_def += JoinList([(",", ())], dimensions) 

1739
ec153828ded2
cleanup: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset

196 
datatype_def += [("] OF ", ()), 
814  197 
(basetype_name, (tagname, "base"))] 
198 
# Data type is a structure 

1297  199 
elif basetype_content_type == "struct": 
814  200 
elements = [] 
1297  201 
for i, element in enumerate(basetype_content.getvariable()): 
814  202 
element_type = element.type.getcontent() 
1297  203 
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

204 
# Structure element derived directly from a user defined type 
1297  205 
if element_type_type == "derived": 
206 
elementtype_name = element_type.getname() 

814  207 
self.GenerateDataType(elementtype_name) 
1297  208 
elif element_type_type == "array": 
209 
base_type = element_type.baseType.getcontent() 

210 
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

211 
# Array derived directly from a user defined type 
1297  212 
if base_type_type == "derived": 
213 
basetype_name = base_type.getname() 

864
bf4f7f0801b9
Adding support for direct array declaration in structure element declaration
Laurent Bessard
parents:
854
diff
changeset

214 
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

215 
# 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

216 
else: 
1297  217 
basetype_name = base_type_type.upper() 
864
bf4f7f0801b9
Adding support for direct array declaration in structure element declaration
Laurent Bessard
parents:
854
diff
changeset

218 
dimensions = ["%s..%s" % (dimension.getlower(), dimension.getupper()) 
1297  219 
for dimension in element_type.getdimension()] 
864
bf4f7f0801b9
Adding support for direct array declaration in structure element declaration
Laurent Bessard
parents:
854
diff
changeset

220 
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

221 
# Structure element derived directly from an elementary type 
814  222 
else: 
1297  223 
elementtype_name = element_type_type.upper() 
814  224 
element_text = [("\n ", ()), 
225 
(element.getname(), (tagname, "struct", i, "name")), 

226 
(" : ", ()), 

227 
(elementtype_name, (tagname, "struct", i, "type"))] 

228 
if element.initialValue is not None: 

229 
element_text.extend([(" := ", ()), 

230 
(self.ComputeValue(element.initialValue.getvalue(), elementtype_name), (tagname, "struct", i, "initial value"))]) 

231 
element_text.append((";", ())) 

232 
elements.append(element_text) 

233 
datatype_def += [("STRUCT", ())] 

234 
datatype_def += JoinList([("", ())], elements) 

235 
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

236 
# Data type derived directly from a elementary type 
814  237 
else: 
1297  238 
datatype_def += [(basetype_content_type.upper(), (tagname, "base"))] 
814  239 
# Data type has an initial value 
240 
if datatype.initialValue is not None: 

241 
datatype_def += [(" := ", ()), 

242 
(self.ComputeValue(datatype.initialValue.getvalue(), datatype_name), (tagname, "initial value"))] 

243 
datatype_def += [(";\n", ())] 

244 
self.Program += datatype_def 

245 

246 
# Generate a POU from its name 

247 
def GeneratePouProgram(self, pou_name): 

248 
# Verify that POU hasn't been generated yet 

249 
if not self.PouComputed.get(pou_name, True): 

250 
# If not mark POU as computed 

251 
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

252 

814  253 
# Getting POU model from project 
254 
pou = self.Project.getpou(pou_name) 

255 
pou_type = pou.getpouType() 

256 
# Verify that POU type exists 

1763
bcc07ff2362c
cleanup: fix PEP8 W601 .has_key() is deprecated, use 'in'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1753
diff
changeset

257 
if pou_type in pouTypeNames: 
814  258 
# Create a POU program generator 
259 
pou_program = PouProgramGenerator(self, pou.getname(), pouTypeNames[pou_type], self.Errors, self.Warnings) 

260 
program = pou_program.GenerateProgram(pou) 

261 
self.Program += program 

262 
else: 

1765
ccf59c1f0b45
cleanup: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset

263 
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

264 

814  265 
# Generate a POU defined and used in text 
266 
def GeneratePouProgramInText(self, text): 

267 
for pou_name in self.PouComputed.keys(): 

1734
750eeb7230a1
cleanup: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset

268 
model = re.compile("(?:^[^09^AZ])%s(?:$[^09^AZ])" % pou_name.upper()) 
814  269 
if model.search(text) is not None: 
270 
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

271 

814  272 
# Generate a configuration from its model 
273 
def GenerateConfiguration(self, configuration): 

1948
b9a3f771aaab
Moved some definitions away from controller class, and adaped references them through all code.
Edouard Tisserant
parents:
1881
diff
changeset

274 
tagname = ComputeConfigurationName(configuration.getname()) 
814  275 
config = [("\nCONFIGURATION ", ()), 
276 
(configuration.getname(), (tagname, "name")), 

277 
("\n", ())] 

278 
var_number = 0 

1418
c97dc5281419
Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents:
1358
diff
changeset

279 

883
235a9ec83b95
Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents:
864
diff
changeset

280 
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

281 

883
235a9ec83b95
Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents:
864
diff
changeset

282 
extra_variables = self.Controler.GetConfigurationExtraVariables() 
1315
ff14a66bbd12
Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents:
1310
diff
changeset

283 
extra_global_vars = None 
ff14a66bbd12
Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents:
1310
diff
changeset

284 
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

285 
extra_global_vars = PLCOpenParser.CreateElement("globalVars", "interface") 
ff14a66bbd12
Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents:
1310
diff
changeset

286 
configuration.setglobalVars([extra_global_vars]) 
ff14a66bbd12
Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents:
1310
diff
changeset

287 
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

288 

1315
ff14a66bbd12
Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents:
1310
diff
changeset

289 
for variable in extra_variables: 
ff14a66bbd12
Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents:
1310
diff
changeset

290 
varlists[1][0].appendvariable(variable) 
ff14a66bbd12
Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents:
1310
diff
changeset

291 
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

292 

814  293 
# Generate any global variable in configuration 
883
235a9ec83b95
Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents:
864
diff
changeset

294 
for varlist, varlist_variables in varlists: 
814  295 
variable_type = errorVarTypes.get("VAR_GLOBAL", "var_local") 
296 
# Generate variable block with modifier 

297 
config += [(" VAR_GLOBAL", ())] 

298 
if varlist.getconstant(): 

299 
config += [(" CONSTANT", (tagname, variable_type, (var_number, var_number + len(varlist.getvariable())), "constant"))] 

300 
elif varlist.getretain(): 

301 
config += [(" RETAIN", (tagname, variable_type, (var_number, var_number + len(varlist.getvariable())), "retain"))] 

302 
elif varlist.getnonretain(): 

303 
config += [(" NON_RETAIN", (tagname, variable_type, (var_number, var_number + len(varlist.getvariable())), "non_retain"))] 

304 
config += [("\n", ())] 

305 
# Generate any variable of this block 

883
235a9ec83b95
Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents:
864
diff
changeset

306 
for var in varlist_variables: 
814  307 
vartype_content = var.gettype().getcontent() 
1297  308 
if vartype_content.getLocalTag() == "derived": 
309 
var_type = vartype_content.getname() 

814  310 
self.GenerateDataType(var_type) 
311 
else: 

312 
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

313 

814  314 
config += [(" ", ()), 
315 
(var.getname(), (tagname, variable_type, var_number, "name")), 

316 
(" ", ())] 

317 
# Generate variable address if exists 

318 
address = var.getaddress() 

319 
if address: 

320 
config += [("AT ", ()), 

321 
(address, (tagname, variable_type, var_number, "location")), 

322 
(" ", ())] 

323 
config += [(": ", ()), 

324 
(var.gettypeAsText(), (tagname, variable_type, var_number, "type"))] 

325 
# Generate variable initial value if exists 

326 
initial = var.getinitialValue() 

1315
ff14a66bbd12
Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents:
1310
diff
changeset

327 
if initial is not None: 
814  328 
config += [(" := ", ()), 
329 
(self.ComputeValue(initial.getvalue(), var_type), (tagname, variable_type, var_number, "initial value"))] 

330 
config += [(";\n", ())] 

331 
var_number += 1 

332 
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

333 

1315
ff14a66bbd12
Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents:
1310
diff
changeset

334 
if extra_global_vars is not None: 
ff14a66bbd12
Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents:
1310
diff
changeset

335 
configuration.remove(extra_global_vars) 
ff14a66bbd12
Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents:
1310
diff
changeset

336 
else: 
ff14a66bbd12
Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents:
1310
diff
changeset

337 
for variable in extra_variables: 
1358
fe7770a30527
Fixed bug when building project with configuration extra variables
Laurent Bessard
parents:
1322
diff
changeset

338 
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

339 

814  340 
# Generate any resource in the configuration 
341 
for resource in configuration.getresource(): 

342 
config += self.GenerateResource(resource, configuration.getname()) 

343 
config += [("END_CONFIGURATION\n", ())] 

344 
return config 

1418
c97dc5281419
Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents:
1358
diff
changeset

345 

814  346 
# Generate a resource from its model 
347 
def GenerateResource(self, resource, config_name): 

1948
b9a3f771aaab
Moved some definitions away from controller class, and adaped references them through all code.
Edouard Tisserant
parents:
1881
diff
changeset

348 
tagname = ComputeConfigurationResourceName(config_name, resource.getname()) 
814  349 
resrce = [("\n RESOURCE ", ()), 
350 
(resource.getname(), (tagname, "name")), 

351 
(" ON PLC\n", ())] 

352 
var_number = 0 

353 
# Generate any global variable in configuration 

354 
for varlist in resource.getglobalVars(): 

355 
variable_type = errorVarTypes.get("VAR_GLOBAL", "var_local") 

356 
# Generate variable block with modifier 

357 
resrce += [(" VAR_GLOBAL", ())] 

358 
if varlist.getconstant(): 

359 
resrce += [(" CONSTANT", (tagname, variable_type, (var_number, var_number + len(varlist.getvariable())), "constant"))] 

360 
elif varlist.getretain(): 

361 
resrce += [(" RETAIN", (tagname, variable_type, (var_number, var_number + len(varlist.getvariable())), "retain"))] 

362 
elif varlist.getnonretain(): 

363 
resrce += [(" NON_RETAIN", (tagname, variable_type, (var_number, var_number + len(varlist.getvariable())), "non_retain"))] 

364 
resrce += [("\n", ())] 

365 
# Generate any variable of this block 

366 
for var in varlist.getvariable(): 

367 
vartype_content = var.gettype().getcontent() 

1297  368 
if vartype_content.getLocalTag() == "derived": 
369 
var_type = vartype_content.getname() 

814  370 
self.GenerateDataType(var_type) 
371 
else: 

372 
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

373 

814  374 
resrce += [(" ", ()), 
375 
(var.getname(), (tagname, variable_type, var_number, "name")), 

376 
(" ", ())] 

377 
address = var.getaddress() 

378 
# Generate variable address if exists 

379 
if address: 

380 
resrce += [("AT ", ()), 

381 
(address, (tagname, variable_type, var_number, "location")), 

382 
(" ", ())] 

383 
resrce += [(": ", ()), 

384 
(var.gettypeAsText(), (tagname, variable_type, var_number, "type"))] 

385 
# Generate variable initial value if exists 

386 
initial = var.getinitialValue() 

1315
ff14a66bbd12
Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents:
1310
diff
changeset

387 
if initial is not None: 
814  388 
resrce += [(" := ", ()), 
389 
(self.ComputeValue(initial.getvalue(), var_type), (tagname, variable_type, var_number, "initial value"))] 

390 
resrce += [(";\n", ())] 

391 
var_number += 1 

392 
resrce += [(" END_VAR\n", ())] 

393 
# Generate any task in the resource 

394 
tasks = resource.gettask() 

395 
task_number = 0 

396 
for task in tasks: 

397 
# Task declaration 

398 
resrce += [(" TASK ", ()), 

399 
(task.getname(), (tagname, "task", task_number, "name")), 

400 
("(", ())] 

401 
single = task.getsingle() 

402 
# Single argument if exists 

1315
ff14a66bbd12
Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents:
1310
diff
changeset

403 
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

404 
if len(single) == 0: 
1765
ccf59c1f0b45
cleanup: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset

405 
raise PLCGenException( 
ccf59c1f0b45
cleanup: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset

406 
_("Source signal has to be defined for single task '{a1}' in resource '{a2}.{a3}'."). 
ccf59c1f0b45
cleanup: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset

407 
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

408 

1742
92932cd370a4
cleanup: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1740
diff
changeset

409 
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

410 
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

411 
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

412 
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

413 
resrce += [(SNGLKW + " := ", ()), 
814  414 
(single, (tagname, "task", task_number, "single")), 
415 
(",", ())] 

416 
# Interval argument if exists 

417 
interval = task.getinterval() 

1315
ff14a66bbd12
Fixed Beremiz for working with new xmlclass support using lxml
Laurent Bessard
parents:
1310
diff
changeset

418 
if interval is not None: 
814  419 
resrce += [("INTERVAL := ", ()), 
420 
(interval, (tagname, "task", task_number, "interval")), 

421 
(",", ())] 

1753
19f19c66b67e
cleanup: fix most PEP8 E266 too many leading '#' for block comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1751
diff
changeset

422 
# resrce += [("INTERVAL := t#", ())] 
19f19c66b67e
cleanup: fix most PEP8 E266 too many leading '#' for block comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1751
diff
changeset

423 
# if interval.hour != 0: 
19f19c66b67e
cleanup: fix most PEP8 E266 too many leading '#' for block comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1751
diff
changeset

424 
# resrce += [("%dh"%interval.hour, (tagname, "task", task_number, "interval", "hour"))] 
19f19c66b67e
cleanup: fix most PEP8 E266 too many leading '#' for block comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1751
diff
changeset

425 
# if interval.minute != 0: 
19f19c66b67e
cleanup: fix most PEP8 E266 too many leading '#' for block comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1751
diff
changeset

426 
# resrce += [("%dm"%interval.minute, (tagname, "task", task_number, "interval", "minute"))] 
19f19c66b67e
cleanup: fix most PEP8 E266 too many leading '#' for block comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1751
diff
changeset

427 
# if interval.second != 0: 
19f19c66b67e
cleanup: fix most PEP8 E266 too many leading '#' for block comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1751
diff
changeset

428 
# resrce += [("%ds"%interval.second, (tagname, "task", task_number, "interval", "second"))] 
19f19c66b67e
cleanup: fix most PEP8 E266 too many leading '#' for block comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1751
diff
changeset

429 
# if interval.microsecond != 0: 
19f19c66b67e
cleanup: fix most PEP8 E266 too many leading '#' for block comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1751
diff
changeset

430 
# resrce += [("%dms"%(interval.microsecond / 1000), (tagname, "task", task_number, "interval", "millisecond"))] 
19f19c66b67e
cleanup: fix most PEP8 E266 too many leading '#' for block comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1751
diff
changeset

431 
# resrce += [(",", ())] 
814  432 
# Priority argument 
1418
c97dc5281419
Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents:
1358
diff
changeset

433 
resrce += [("PRIORITY := ", ()), 
1734
750eeb7230a1
cleanup: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset

434 
("%d" % task.getpriority(), (tagname, "task", task_number, "priority")), 
814  435 
(");\n", ())] 
436 
task_number += 1 

437 
instance_number = 0 

438 
# Generate any program assign to each task 

439 
for task in tasks: 

440 
for instance in task.getpouInstance(): 

441 
resrce += [(" PROGRAM ", ()), 

442 
(instance.getname(), (tagname, "instance", instance_number, "name")), 

443 
(" WITH ", ()), 

444 
(task.getname(), (tagname, "instance", instance_number, "task")), 

445 
(" : ", ()), 

446 
(instance.gettypeName(), (tagname, "instance", instance_number, "type")), 

447 
(";\n", ())] 

448 
instance_number += 1 

449 
# Generate any program assign to no task 

450 
for instance in resource.getpouInstance(): 

451 
resrce += [(" PROGRAM ", ()), 

1767
c74815729afd
cleanup: fix PEP8 E127 continuation line overindented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1766
diff
changeset

452 
(instance.getname(), (tagname, "instance", instance_number, "name")), 
c74815729afd
cleanup: fix PEP8 E127 continuation line overindented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1766
diff
changeset

453 
(" : ", ()), 
c74815729afd
cleanup: fix PEP8 E127 continuation line overindented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1766
diff
changeset

454 
(instance.gettypeName(), (tagname, "instance", instance_number, "type")), 
c74815729afd
cleanup: fix PEP8 E127 continuation line overindented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1766
diff
changeset

455 
(";\n", ())] 
814  456 
instance_number += 1 
457 
resrce += [(" END_RESOURCE\n", ())] 

458 
return resrce 

1418
c97dc5281419
Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents:
1358
diff
changeset

459 

814  460 
# 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

461 
def GenerateProgram(self): 
814  462 
# Find all data types defined 
463 
for datatype in self.Project.getdataTypes(): 

464 
self.DatatypeComputed[datatype.getname()] = False 

465 
# Find all data types defined 

466 
for pou in self.Project.getpous(): 

467 
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

468 
# Generate data type declaration structure if there is at least one data 
814  469 
# type defined 
470 
if len(self.DatatypeComputed) > 0: 

471 
self.Program += [("TYPE\n", ())] 

472 
# Generate every data types defined 

473 
for datatype_name in self.DatatypeComputed.keys(): 

474 
self.GenerateDataType(datatype_name) 

475 
self.Program += [("END_TYPE\n\n", ())] 

476 
# Generate every POUs defined 

477 
for pou_name in self.PouComputed.keys(): 

478 
self.GeneratePouProgram(pou_name) 

479 
# Generate every configurations defined 

480 
for config in self.Project.getconfigurations(): 

481 
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

482 

814  483 
# Return generated program 
484 
def GetGeneratedProgram(self): 

485 
return self.Program 

486 

487 

1782
5b6ad7a7fd9d
cleanup: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1775
diff
changeset

488 
#  
814  489 
# Generator of POU programs 
1782
5b6ad7a7fd9d
cleanup: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1775
diff
changeset

490 
#  
814  491 

1297  492 
[ConnectorClass, ContinuationClass, ActionBlockClass] = [ 
493 
PLCOpenParser.GetElementClass(instance_name, "commonObjects") 

494 
for instance_name in ["connector", "continuation", "actionBlock"]] 

495 
[InVariableClass, InOutVariableClass, OutVariableClass, BlockClass] = [ 

496 
PLCOpenParser.GetElementClass(instance_name, "fbdObjects") 

497 
for instance_name in ["inVariable", "inOutVariable", "outVariable", "block"]] 

498 
[ContactClass, CoilClass, LeftPowerRailClass, RightPowerRailClass] = [ 

499 
PLCOpenParser.GetElementClass(instance_name, "ldObjects") 

500 
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

501 
[StepClass, TransitionClass, JumpStepClass, 
1297  502 
SelectionConvergenceClass, SelectionDivergenceClass, 
503 
SimultaneousConvergenceClass, SimultaneousDivergenceClass] = [ 

1878
fb73a6b6622d
fix pylint warning '(badcontinuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1864
diff
changeset

504 
PLCOpenParser.GetElementClass(instance_name, "sfcObjects") 
fb73a6b6622d
fix pylint warning '(badcontinuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1864
diff
changeset

505 
for instance_name in ["step", 
fb73a6b6622d
fix pylint warning '(badcontinuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1864
diff
changeset

506 
"transition", 
fb73a6b6622d
fix pylint warning '(badcontinuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1864
diff
changeset

507 
"jumpStep", 
fb73a6b6622d
fix pylint warning '(badcontinuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1864
diff
changeset

508 
"selectionConvergence", 
fb73a6b6622d
fix pylint warning '(badcontinuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1864
diff
changeset

509 
"selectionDivergence", 
fb73a6b6622d
fix pylint warning '(badcontinuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1864
diff
changeset

510 
"simultaneousConvergence", 
fb73a6b6622d
fix pylint warning '(badcontinuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1864
diff
changeset

511 
"simultaneousDivergence"]] 
1297  512 
TransitionObjClass = PLCOpenParser.GetElementClass("transition", "transitions") 
513 
ActionObjClass = PLCOpenParser.GetElementClass("action", "actions") 

814  514 

1736
7e61baa047f0
cleanup: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1734
diff
changeset

515 

1831
56b48961cc68
fix (oldstyleclass) Oldstyle class defined error for most parts of
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1782
diff
changeset

516 
class PouProgramGenerator(object): 
1418
c97dc5281419
Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents:
1358
diff
changeset

517 

814  518 
# Create a new POU program generator 
519 
def __init__(self, parent, name, type, errors, warnings): 

520 
# Keep Reference to the parent generator 

521 
self.ParentGenerator = parent 

522 
self.Name = name 

523 
self.Type = type 

1948
b9a3f771aaab
Moved some definitions away from controller class, and adaped references them through all code.
Edouard Tisserant
parents:
1881
diff
changeset

524 
self.TagName = ComputePouName(name) 
814  525 
self.CurrentIndent = " " 
526 
self.ReturnType = None 

527 
self.Interface = [] 

528 
self.InitialSteps = [] 

529 
self.ComputedBlocks = {} 

530 
self.ComputedConnectors = {} 

531 
self.ConnectionTypes = {} 

532 
self.RelatedConnections = [] 

1740
b789b695b5c6
cleanup: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1739
diff
changeset

533 
self.SFCNetworks = {"Steps": {}, "Transitions": {}, "Actions": {}} 
814  534 
self.SFCComputedBlocks = [] 
535 
self.ActionNumber = 0 

536 
self.Program = [] 

537 
self.Errors = errors 

538 
self.Warnings = warnings 

1418
c97dc5281419
Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents:
1358
diff
changeset

539 

814  540 
def GetBlockType(self, type, inputs=None): 
541 
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

542 

814  543 
def IndentLeft(self): 
544 
if len(self.CurrentIndent) >= 2: 

545 
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

546 

814  547 
def IndentRight(self): 
548 
self.CurrentIndent += " " 

1418
c97dc5281419
Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents:
1358
diff
changeset

549 

814  550 
# Generator of unique ID for inline actions 
551 
def GetActionNumber(self): 

552 
self.ActionNumber += 1 

553 
return self.ActionNumber 

1418
c97dc5281419
Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents:
1358
diff
changeset

554 

814  555 
# Test if a variable has already been defined 
556 
def IsAlreadyDefined(self, name): 

1847
6198190bc121
explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1846
diff
changeset

557 
for _list_type, _option, _located, vars in self.Interface: 
6198190bc121
explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1846
diff
changeset

558 
for _var_type, var_name, _var_address, _var_initial in vars: 
814  559 
if name == var_name: 
560 
return True 

561 
return False 

1418
c97dc5281419
Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents:
1358
diff
changeset

562 

814  563 
# Return the type of a variable defined in interface 
564 
def GetVariableType(self, name): 

565 
parts = name.split('.') 

566 
current_type = None 

567 
if len(parts) > 0: 

568 
name = parts.pop(0) 

1847
6198190bc121
explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1846
diff
changeset

569 
for _list_type, _option, _located, vars in self.Interface: 
6198190bc121
explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1846
diff
changeset

570 
for var_type, var_name, _var_address, _var_initial in vars: 
814  571 
if name == var_name: 
572 
current_type = var_type 

573 
break 

574 
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

575 
blocktype = self.ParentGenerator.Controler.GetBlockType(current_type) 
235a9ec83b95
Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents:
864
diff
changeset

576 
if blocktype is not None: 
235a9ec83b95
Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents:
864
diff
changeset

577 
name = parts.pop(0) 
235a9ec83b95
Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents:
864
diff
changeset

578 
current_type = None 
1847
6198190bc121
explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1846
diff
changeset

579 
for var_name, var_type, _var_modifier in blocktype["inputs"] + blocktype["outputs"]: 
883
235a9ec83b95
Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents:
864
diff
changeset

580 
if var_name == name: 
235a9ec83b95
Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents:
864
diff
changeset

581 
current_type = var_type 
814  582 
break 
883
235a9ec83b95
Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents:
864
diff
changeset

583 
else: 
1948
b9a3f771aaab
Moved some definitions away from controller class, and adaped references them through all code.
Edouard Tisserant
parents:
1881
diff
changeset

584 
tagname = ComputeDataTypeName(current_type) 
883
235a9ec83b95
Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents:
864
diff
changeset

585 
infos = self.ParentGenerator.Controler.GetDataTypeInfos(tagname) 
235a9ec83b95
Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents:
864
diff
changeset

586 
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

587 
name = parts.pop(0) 
235a9ec83b95
Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents:
864
diff
changeset

588 
current_type = None 
235a9ec83b95
Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents:
864
diff
changeset

589 
for element in infos["elements"]: 
235a9ec83b95
Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents:
864
diff
changeset

590 
if element["Name"] == name: 
235a9ec83b95
Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents:
864
diff
changeset

591 
current_type = element["Type"] 
235a9ec83b95
Adding support for defining specific global variables for ConfTreeNodes
Laurent Bessard
parents:
864
diff
changeset

592 
break 
814  593 
return current_type 
1418
c97dc5281419
Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents:
1358
diff
changeset

594 

814  595 
# Return connectors linked by a connection to the given connector 
596 
def GetConnectedConnector(self, connector, body): 

597 
links = connector.getconnections() 

1298
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset

598 
if links is not None and len(links) == 1: 
814  599 
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

600 
return None 
814  601 

602 
def GetLinkedConnector(self, link, body): 

603 
parameter = link.getformalParameter() 

604 
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

605 
if isinstance(instance, (InVariableClass, InOutVariableClass, 
1768
691083b5682a
cleanup: fix PEP8 E128 continuation line underindented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1767
diff
changeset

606 
ContinuationClass, ContactClass, CoilClass)): 
814  607 
return instance.connectionPointOut 
1297  608 
elif isinstance(instance, BlockClass): 
814  609 
outputvariables = instance.outputVariables.getvariable() 
610 
if len(outputvariables) == 1: 

611 
return outputvariables[0].connectionPointOut 

612 
elif parameter: 

613 
for variable in outputvariables: 

614 
if variable.getformalParameter() == parameter: 

615 
return variable.connectionPointOut 

616 
else: 

617 
point = link.getposition()[1] 

618 
for variable in outputvariables: 

619 
relposition = variable.connectionPointOut.getrelPositionXY() 

620 
blockposition = instance.getposition() 

621 
if point.x == blockposition.x + relposition[0] and point.y == blockposition.y + relposition[1]: 

622 
return variable.connectionPointOut 

1297  623 
elif isinstance(instance, LeftPowerRailClass): 
814  624 
outputconnections = instance.getconnectionPointOut() 
625 
if len(outputconnections) == 1: 

626 
return outputconnections[0] 

627 
else: 

628 
point = link.getposition()[1] 

629 
for outputconnection in outputconnections: 

630 
relposition = outputconnection.getrelPositionXY() 

631 
powerrailposition = instance.getposition() 

632 
if point.x == powerrailposition.x + relposition[0] and point.y == powerrailposition.y + relposition[1]: 

633 
return outputconnection 

634 
return None 

1418
c97dc5281419
Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents:
1358
diff
changeset

635 

814  636 
def ExtractRelatedConnections(self, connection): 
637 
for i, related in enumerate(self.RelatedConnections): 

638 
if connection in related: 

639 
return self.RelatedConnections.pop(i) 

640 
return [connection] 

1418
c97dc5281419
Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents:
1358
diff
changeset

641 

814  642 
def ComputeInterface(self, pou): 
643 
interface = pou.getinterface() 

644 
if interface is not None: 

645 
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

646 
returntype_content = interface.getreturnType()[0] 
1297  647 
returntype_content_type = returntype_content.getLocalTag() 
648 
if returntype_content_type == "derived": 

649 
self.ReturnType = returntype_content.getname() 

650 
else: 

651 
self.ReturnType = returntype_content_type.upper() 

814  652 
for varlist in interface.getcontent(): 
653 
variables = [] 

654 
located = [] 

1297  655 
varlist_type = varlist.getLocalTag() 
656 
for var in varlist.getvariable(): 

814  657 
vartype_content = var.gettype().getcontent() 
1297  658 
if vartype_content.getLocalTag() == "derived": 
659 
var_type = vartype_content.getname() 

814  660 
blocktype = self.GetBlockType(var_type) 
661 
if blocktype is not None: 

662 
self.ParentGenerator.GeneratePouProgram(var_type) 

1310
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

663 
variables.append((var_type, var.getname(), None, None)) 
814  664 
else: 
665 
self.ParentGenerator.GenerateDataType(var_type) 

666 
initial = var.getinitialValue() 

1310
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

667 
if initial is not None: 
814  668 
initial_value = initial.getvalue() 
669 
else: 

670 
initial_value = None 

671 
address = var.getaddress() 

672 
if address is not None: 

1297  673 
located.append((vartype_content.getname(), var.getname(), address, initial_value)) 
814  674 
else: 
1297  675 
variables.append((vartype_content.getname(), var.getname(), None, initial_value)) 
814  676 
else: 
677 
var_type = var.gettypeAsText() 

678 
initial = var.getinitialValue() 

1310
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

679 
if initial is not None: 
814  680 
initial_value = initial.getvalue() 
681 
else: 

682 
initial_value = None 

683 
address = var.getaddress() 

684 
if address is not None: 

685 
located.append((var_type, var.getname(), address, initial_value)) 

686 
else: 

687 
variables.append((var_type, var.getname(), None, initial_value)) 

1297  688 
if varlist.getconstant(): 
814  689 
option = "CONSTANT" 
1297  690 
elif varlist.getretain(): 
814  691 
option = "RETAIN" 
1297  692 
elif varlist.getnonretain(): 
814  693 
option = "NON_RETAIN" 
694 
else: 

695 
option = None 

696 
if len(variables) > 0: 

1297  697 
self.Interface.append((varTypeNames[varlist_type], option, False, variables)) 
814  698 
if len(located) > 0: 
1297  699 
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

700 

1181
21e6db77eb29
Fixed bug in PLC code generated with binary, octal and hexadecimal literals
Laurent Bessard
parents:
1134
diff
changeset

701 
LITERAL_TYPES = { 
1751
c28db6f7616b
cleanup: fix PEP8 E301 expected 1 blank line, found 0
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1747
diff
changeset

702 
"T": "TIME", 
c28db6f7616b
cleanup: fix PEP8 E301 expected 1 blank line, found 0
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1747
diff
changeset

703 
"D": "DATE", 
1181
21e6db77eb29
Fixed bug in PLC code generated with binary, octal and hexadecimal literals
Laurent Bessard
parents:
1134
diff
changeset

704 
"TOD": "TIME_OF_DAY", 
1751
c28db6f7616b
cleanup: fix PEP8 E301 expected 1 blank line, found 0
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1747
diff
changeset

705 
"DT": "DATE_AND_TIME", 
c28db6f7616b
cleanup: fix PEP8 E301 expected 1 blank line, found 0
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1747
diff
changeset

706 
"2": None, 
c28db6f7616b
cleanup: fix PEP8 E301 expected 1 blank line, found 0
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1747
diff
changeset

707 
"8": None, 
c28db6f7616b
cleanup: fix PEP8 E301 expected 1 blank line, found 0
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1747
diff
changeset

708 
"16": None, 
1181
21e6db77eb29
Fixed bug in PLC code generated with binary, octal and hexadecimal literals
Laurent Bessard
parents:
1134
diff
changeset

709 
} 
1751
c28db6f7616b
cleanup: fix PEP8 E301 expected 1 blank line, found 0
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1747
diff
changeset

710 

814  711 
def ComputeConnectionTypes(self, pou): 
712 
body = pou.getbody() 

2450
5024c19ca8f0
python3 support: pylint, W1652 # (deprecatedtypesfield) Accessing a deprecated fields on the types module
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2447
diff
changeset

713 
if isinstance(body, list): 
814  714 
body = body[0] 
715 
body_content = body.getcontent() 

1297  716 
body_type = body_content.getLocalTag() 
814  717 
if body_type in ["FBD", "LD", "SFC"]: 
718 
undefined_blocks = [] 

719 
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

720 
if isinstance(instance, (InVariableClass, OutVariableClass, 
1297  721 
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

722 
expression = instance.getexpression() 
814  723 
var_type = self.GetVariableType(expression) 
1766
c1e5b9f19483
cleanup: fix PEP8 E129 visually indented line with same indent as next logical line
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1765
diff
changeset

724 
if isinstance(pou, TransitionObjClass) and expression == pou.getname(): 
822
050045c32d98
Fix bug in PLCGenerator connection types not computed for SFC actions and transitions body
laurent
parents:
814
diff
changeset

725 
var_type = "BOOL" 
1297  726 
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

727 
pou.getpouType() == "function" and expression == pou.getname()): 
814  728 
returntype_content = pou.interface.getreturnType().getcontent() 
1297  729 
returntype_content_type = returntype_content.getLocalTag() 
730 
if returntype_content_type == "derived": 

731 
var_type = returntype_content.getname() 

814  732 
else: 
1297  733 
var_type = returntype_content_type.upper() 
814  734 
elif var_type is None: 
735 
parts = expression.split("#") 

736 
if len(parts) > 1: 

1181
21e6db77eb29
Fixed bug in PLC code generated with binary, octal and hexadecimal literals
Laurent Bessard
parents:
1134
diff
changeset

737 
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

738 
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

739 
literal_prefix) 
814  740 
elif expression.startswith("'"): 
741 
var_type = "STRING" 

742 
elif expression.startswith('"'): 

743 
var_type = "WSTRING" 

744 
if var_type is not None: 

1297  745 
if isinstance(instance, (InVariableClass, InOutVariableClass)): 
814  746 
for connection in self.ExtractRelatedConnections(instance.connectionPointOut): 
747 
self.ConnectionTypes[connection] = var_type 

1297  748 
if isinstance(instance, (OutVariableClass, InOutVariableClass)): 
814  749 
self.ConnectionTypes[instance.connectionPointIn] = var_type 
750 
connected = self.GetConnectedConnector(instance.connectionPointIn, body) 

1775
b45f2768fab1
cleanup: fix PEP8 E713 test for membership should be 'not in'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1769
diff
changeset

751 
if connected is not None and connected not in self.ConnectionTypes: 
1298
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset

752 
for related in self.ExtractRelatedConnections(connected): 
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset

753 
self.ConnectionTypes[related] = var_type 
1297  754 
elif isinstance(instance, (ContactClass, CoilClass)): 
814  755 
for connection in self.ExtractRelatedConnections(instance.connectionPointOut): 
756 
self.ConnectionTypes[connection] = "BOOL" 

757 
self.ConnectionTypes[instance.connectionPointIn] = "BOOL" 

1298
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset

758 
for link in instance.connectionPointIn.getconnections(): 
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset

759 
connected = self.GetLinkedConnector(link, body) 
1775
b45f2768fab1
cleanup: fix PEP8 E713 test for membership should be 'not in'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1769
diff
changeset

760 
if connected is not None and connected not in self.ConnectionTypes: 
1298
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset

761 
for related in self.ExtractRelatedConnections(connected): 
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset

762 
self.ConnectionTypes[related] = "BOOL" 
1297  763 
elif isinstance(instance, LeftPowerRailClass): 
814  764 
for connection in instance.getconnectionPointOut(): 
765 
for related in self.ExtractRelatedConnections(connection): 

766 
self.ConnectionTypes[related] = "BOOL" 

1297  767 
elif isinstance(instance, RightPowerRailClass): 
814  768 
for connection in instance.getconnectionPointIn(): 
769 
self.ConnectionTypes[connection] = "BOOL" 

1298
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset

770 
for link in connection.getconnections(): 
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset

771 
connected = self.GetLinkedConnector(link, body) 
1775
b45f2768fab1
cleanup: fix PEP8 E713 test for membership should be 'not in'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1769
diff
changeset

772 
if connected is not None and connected not in self.ConnectionTypes: 
1298
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset

773 
for related in self.ExtractRelatedConnections(connected): 
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset

774 
self.ConnectionTypes[related] = "BOOL" 
1297  775 
elif isinstance(instance, TransitionClass): 
776 
content = instance.getconditionContent() 

777 
if content["type"] == "connection": 

1298
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset

778 
self.ConnectionTypes[content["value"]] = "BOOL" 
1603
18279f12a6be
fix issue with compiling SFC POU with nonconnected transition (type "connection")
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1581
diff
changeset

779 
connections = content["value"].getconnections() 
18279f12a6be
fix issue with compiling SFC POU with nonconnected transition (type "connection")
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1581
diff
changeset

780 
if not connections: 
1765
ccf59c1f0b45
cleanup: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset

781 
raise PLCGenException( 
ccf59c1f0b45
cleanup: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset

782 
_("SFC transition in POU \"%s\" must be connected.") % self.Name) 
ccf59c1f0b45
cleanup: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset

783 

1730
64d8f52bc8c8
cleanup for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1626
diff
changeset

784 
for link in connections: 
1298
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset

785 
connected = self.GetLinkedConnector(link, body) 
1775
b45f2768fab1
cleanup: fix PEP8 E713 test for membership should be 'not in'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1769
diff
changeset

786 
if connected is not None and connected not in self.ConnectionTypes: 
1298
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset

787 
for related in self.ExtractRelatedConnections(connected): 
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset

788 
self.ConnectionTypes[related] = "BOOL" 
1297  789 
elif isinstance(instance, ContinuationClass): 
814  790 
name = instance.getname() 
791 
connector = None 

792 
var_type = "ANY" 

793 
for element in body.getcontentInstances(): 

1297  794 
if isinstance(element, ConnectorClass) and element.getname() == name: 
814  795 
if connector is not None: 
1765
ccf59c1f0b45
cleanup: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset

796 
raise PLCGenException( 
ccf59c1f0b45
cleanup: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset

797 
_("More than one connector found corresponding to \"{a1}\" continuation in \"{a2}\" POU"). 
ccf59c1f0b45
cleanup: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset

798 
format(a1=name, a2=self.Name)) 
814  799 
connector = element 
800 
if connector is not None: 

801 
undefined = [instance.connectionPointOut, connector.connectionPointIn] 

802 
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

803 
if connected is not None: 
814  804 
undefined.append(connected) 
805 
related = [] 

806 
for connection in undefined: 

1763
bcc07ff2362c
cleanup: fix PEP8 W601 .has_key() is deprecated, use 'in'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1753
diff
changeset

807 
if connection in self.ConnectionTypes: 
814  808 
var_type = self.ConnectionTypes[connection] 
809 
else: 

810 
related.extend(self.ExtractRelatedConnections(connection)) 

811 
if var_type.startswith("ANY") and len(related) > 0: 

812 
self.RelatedConnections.append(related) 

813 
else: 

814 
for connection in related: 

815 
self.ConnectionTypes[connection] = var_type 

816 
else: 

1765
ccf59c1f0b45
cleanup: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset

817 
raise PLCGenException( 
ccf59c1f0b45
cleanup: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset

818 
_("No connector found corresponding to \"{a1}\" continuation in \"{a2}\" POU"). 
ccf59c1f0b45
cleanup: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset

819 
format(a1=name, a2=self.Name)) 
ccf59c1f0b45
cleanup: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset

820 

1297  821 
elif isinstance(instance, BlockClass): 
814  822 
block_infos = self.GetBlockType(instance.gettypeName(), "undefined") 
823 
if block_infos is not None: 

824 
self.ComputeBlockInputTypes(instance, block_infos, body) 

825 
else: 

826 
for variable in instance.inputVariables.getvariable(): 

827 
connected = self.GetConnectedConnector(variable.connectionPointIn, body) 

828 
if connected is not None: 

829 
var_type = self.ConnectionTypes.get(connected, None) 

830 
if var_type is not None: 

831 
self.ConnectionTypes[variable.connectionPointIn] = var_type 

832 
else: 

833 
related = self.ExtractRelatedConnections(connected) 

834 
related.append(variable.connectionPointIn) 

835 
self.RelatedConnections.append(related) 

836 
undefined_blocks.append(instance) 

837 
for instance in undefined_blocks: 

838 
block_infos = self.GetBlockType(instance.gettypeName(), tuple([self.ConnectionTypes.get(variable.connectionPointIn, "ANY") for variable in instance.inputVariables.getvariable() if variable.getformalParameter() != "EN"])) 

839 
if block_infos is not None: 

840 
self.ComputeBlockInputTypes(instance, block_infos, body) 

841 
else: 

1765
ccf59c1f0b45
cleanup: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset

842 
raise PLCGenException( 
ccf59c1f0b45
cleanup: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset

843 
_("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

844 
if body_type == "SFC": 
050045c32d98
Fix bug in PLCGenerator connection types not computed for SFC actions and transitions body
laurent
parents:
814
diff
changeset

845 
previous_tagname = self.TagName 
050045c32d98
Fix bug in PLCGenerator connection types not computed for SFC actions and transitions body
laurent
parents:
814
diff
changeset

846 
for action in pou.getactionList(): 
1948
b9a3f771aaab
Moved some definitions away from controller class, and adaped references them through all code.
Edouard Tisserant
parents:
1881
diff
changeset

847 
self.TagName = ComputePouActionName(self.Name, action.getname()) 
822
050045c32d98
Fix bug in PLCGenerator connection types not computed for SFC actions and transitions body
laurent
parents:
814
diff
changeset

848 
self.ComputeConnectionTypes(action) 
050045c32d98
Fix bug in PLCGenerator connection types not computed for SFC actions and transitions body
laurent
parents:
814
diff
changeset

849 
for transition in pou.gettransitionList(): 
1948
b9a3f771aaab
Moved some definitions away from controller class, and adaped references them through all code.
Edouard Tisserant
parents:
1881
diff
changeset

850 
self.TagName = ComputePouTransitionName(self.Name, transition.getname()) 
822
050045c32d98
Fix bug in PLCGenerator connection types not computed for SFC actions and transitions body
laurent
parents:
814
diff
changeset

851 
self.ComputeConnectionTypes(transition) 
050045c32d98
Fix bug in PLCGenerator connection types not computed for SFC actions and transitions body
laurent
parents:
814
diff
changeset

852 
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

853 

814  854 
def ComputeBlockInputTypes(self, instance, block_infos, body): 
855 
undefined = {} 

856 
for variable in instance.outputVariables.getvariable(): 

857 
output_name = variable.getformalParameter() 

858 
if output_name == "ENO": 

859 
for connection in self.ExtractRelatedConnections(variable.connectionPointOut): 

860 
self.ConnectionTypes[connection] = "BOOL" 

861 
else: 

1847
6198190bc121
explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1846
diff
changeset

862 
for oname, otype, _oqualifier in block_infos["outputs"]: 
814  863 
if output_name == oname: 
864 
if otype.startswith("ANY"): 

1775
b45f2768fab1
cleanup: fix PEP8 E713 test for membership should be 'not in'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1769
diff
changeset

865 
if otype not in undefined: 
814  866 
undefined[otype] = [] 
867 
undefined[otype].append(variable.connectionPointOut) 

1775
b45f2768fab1
cleanup: fix PEP8 E713 test for membership should be 'not in'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1769
diff
changeset

868 
elif variable.connectionPointOut not in self.ConnectionTypes: 
814  869 
for connection in self.ExtractRelatedConnections(variable.connectionPointOut): 
870 
self.ConnectionTypes[connection] = otype 

871 
for variable in instance.inputVariables.getvariable(): 

872 
input_name = variable.getformalParameter() 

873 
if input_name == "EN": 

874 
for connection in self.ExtractRelatedConnections(variable.connectionPointIn): 

875 
self.ConnectionTypes[connection] = "BOOL" 

876 
else: 

1847
6198190bc121
explicitly mark unused variables found by pylint with _ or dummy
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1846
diff
changeset

877 
for iname, itype, _iqualifier in block_infos["inputs"]: 
814  878 
if input_name == iname: 
879 
connected = self.GetConnectedConnector(variable.connectionPointIn, body) 

880 
if itype.startswith("ANY"): 

1775
b45f2768fab1
cleanup: fix PEP8 E713 test for membership should be 'not in'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1769
diff
changeset

881 
if itype not in undefined: 
814  882 
undefined[itype] = [] 
883 
undefined[itype].append(variable.connectionPointIn) 

1298
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset

884 
if connected is not None: 
814  885 
undefined[itype].append(connected) 
886 
else: 

887 
self.ConnectionTypes[variable.connectionPointIn] = itype 

1775
b45f2768fab1
cleanup: fix PEP8 E713 test for membership should be 'not in'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1769
diff
changeset

888 
if connected is not None and connected not in self.ConnectionTypes: 
814  889 
for connection in self.ExtractRelatedConnections(connected): 
890 
self.ConnectionTypes[connection] = itype 

891 
for var_type, connections in undefined.items(): 

892 
related = [] 

893 
for connection in connections: 

854
c10f2092c43a
Fixing bug in PLCGenerator with user defined functions and standard overloaded function
Laurent Bessard
parents:
822
diff
changeset

894 
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

895 
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

896 
var_type = connection_type 
814  897 
else: 
898 
related.extend(self.ExtractRelatedConnections(connection)) 

899 
if var_type.startswith("ANY") and len(related) > 0: 

900 
self.RelatedConnections.append(related) 

901 
else: 

902 
for connection in related: 

903 
self.ConnectionTypes[connection] = var_type 

904 

2258
c9915bc620cd
Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1948
diff
changeset

905 
def GetUsedEno(self, body, connections): 
c9915bc620cd
Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1948
diff
changeset

906 
""" 
c9915bc620cd
Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1948
diff
changeset

907 
Function checks whether value on given connection 
c9915bc620cd
Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1948
diff
changeset

908 
comes from block, that has used EN input and 
c9915bc620cd
Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1948
diff
changeset

909 
returns variable name for ENO output. 
c9915bc620cd
Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1948
diff
changeset

910 

c9915bc620cd
Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1948
diff
changeset

911 
This is needed to avoid value propagation from blocks 
c9915bc620cd
Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1948
diff
changeset

912 
with false signal on EN input. 
c9915bc620cd
Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1948
diff
changeset

913 

c9915bc620cd
Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1948
diff
changeset

914 
:param body: 
c9915bc620cd
Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1948
diff
changeset

915 
body of the block for that program is currently generated 
c9915bc620cd
Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1948
diff
changeset

916 
:param connections: 
c9915bc620cd
Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1948
diff
changeset

917 
connection, that's source is checked for EN/ENO usage 
c9915bc620cd
Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1948
diff
changeset

918 
:return: 
c9915bc620cd
Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1948
diff
changeset

919 
if EN/ENO are not used, then None is returned 
c9915bc620cd
Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1948
diff
changeset

920 
Otherwise BOOL variable corresponding to ENO 
c9915bc620cd
Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1948
diff
changeset

921 
output is returned. 
c9915bc620cd
Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1948
diff
changeset

922 
""" 
c9915bc620cd
Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1948
diff
changeset

923 

c9915bc620cd
Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1948
diff
changeset

924 
if len(connections) != 1: 
c9915bc620cd
Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1948
diff
changeset

925 
return None 
c9915bc620cd
Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1948
diff
changeset

926 
ref_local_id = connections[0].getrefLocalId() 
c9915bc620cd
Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1948
diff
changeset

927 
blk = body.getcontentInstance(ref_local_id) 
c9915bc620cd
Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1948
diff
changeset

928 
if blk is None: 
c9915bc620cd
Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1948
diff
changeset

929 
return None 
c9915bc620cd
Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1948
diff
changeset

930 

2275
1bb8afa02409
Improve fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2258
diff
changeset

931 
if not hasattr(blk, "inputVariables"): 
1bb8afa02409
Improve fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2258
diff
changeset

932 
return None 
1bb8afa02409
Improve fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2258
diff
changeset

933 

2258
c9915bc620cd
Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1948
diff
changeset

934 
for invar in blk.inputVariables.getvariable(): 
c9915bc620cd
Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1948
diff
changeset

935 
if invar.getformalParameter() == "EN": 
c9915bc620cd
Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1948
diff
changeset

936 
if len(invar.getconnectionPointIn().getconnections()) > 0: 
c9915bc620cd
Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1948
diff
changeset

937 
if blk.getinstanceName() is None: 
2633
fecdb71e7514
Fixed sequel of earlier commit in PLCGenerator.py. Some ref to temporary variables are still not using the _TMP_ prefix. They are quite hard to find, so to be continued...
Edouard Tisserant
parents:
2629
diff
changeset

938 
var_name = "_TMP_%s%d_ENO" % (blk.gettypeName(), blk.getlocalId()) 
2258
c9915bc620cd
Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1948
diff
changeset

939 
else: 
c9915bc620cd
Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1948
diff
changeset

940 
var_name = "%s.ENO" % blk.getinstanceName() 
c9915bc620cd
Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1948
diff
changeset

941 
return var_name 
c9915bc620cd
Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1948
diff
changeset

942 
return None 
c9915bc620cd
Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1948
diff
changeset

943 

814  944 
def ComputeProgram(self, pou): 
945 
body = pou.getbody() 

2450
5024c19ca8f0
python3 support: pylint, W1652 # (deprecatedtypesfield) Accessing a deprecated fields on the types module
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2447
diff
changeset

946 
if isinstance(body, list): 
814  947 
body = body[0] 
948 
body_content = body.getcontent() 

1297  949 
body_type = body_content.getLocalTag() 
1740
b789b695b5c6
cleanup: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1739
diff
changeset

950 
if body_type in ["IL", "ST"]: 
1297  951 
text = body_content.getanyText() 
814  952 
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

953 
self.Program = [(ReIndentText(text, len(self.CurrentIndent)), 
1878
fb73a6b6622d
fix pylint warning '(badcontinuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1864
diff
changeset

954 
(self.TagName, "body", len(self.CurrentIndent)))] 
814  955 
elif body_type == "SFC": 
956 
self.IndentRight() 

957 
for instance in body.getcontentInstances(): 

1297  958 
if isinstance(instance, StepClass): 
814  959 
self.GenerateSFCStep(instance, pou) 
1297  960 
elif isinstance(instance, ActionBlockClass): 
814  961 
self.GenerateSFCStepActions(instance, pou) 
1297  962 
elif isinstance(instance, TransitionClass): 
814  963 
self.GenerateSFCTransition(instance, pou) 
1298
f034fb2b1aab
Fixed SFC block edition and SFC to SFC_textual code generating
Laurent Bessard
parents:
1297
diff
changeset

964 
elif isinstance(instance, JumpStepClass): 
814  965 
self.GenerateSFCJump(instance, pou) 
966 
if len(self.InitialSteps) > 0 and len(self.SFCComputedBlocks) > 0: 

967 
action_name = "COMPUTE_FUNCTION_BLOCKS" 

1739
ec153828ded2
cleanup: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset

968 
action_infos = {"qualifier": "S", "content": action_name} 
814  969 
self.SFCNetworks["Steps"][self.InitialSteps[0]]["actions"].append(action_infos) 
970 
self.SFCNetworks["Actions"][action_name] = (self.SFCComputedBlocks, ()) 

971 
self.Program = [] 

972 
self.IndentLeft() 

973 
for initialstep in self.InitialSteps: 

974 
self.ComputeSFCStep(initialstep) 

975 
else: 

1739
ec153828ded2
cleanup: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset

976 
otherInstances = {"outVariables&coils": [], "blocks": [], "connectors": []} 
814  977 
orderedInstances = [] 
978 
for instance in body.getcontentInstances(): 

1297  979 
if isinstance(instance, (OutVariableClass, InOutVariableClass, BlockClass)): 
814  980 
executionOrderId = instance.getexecutionOrderId() 
981 
if executionOrderId > 0: 

982 
orderedInstances.append((executionOrderId, instance)) 

1297  983 
elif isinstance(instance, (OutVariableClass, InOutVariableClass)): 
814  984 
otherInstances["outVariables&coils"].append(instance) 
1297  985 
elif isinstance(instance, BlockClass): 
814  986 
otherInstances["blocks"].append(instance) 
1297  987 
elif isinstance(instance, ConnectorClass): 
814  988 
otherInstances["connectors"].append(instance) 
1297  989 
elif isinstance(instance, CoilClass): 
814  990 
otherInstances["outVariables&coils"].append(instance) 
991 
orderedInstances.sort() 

992 
otherInstances["outVariables&coils"].sort(SortInstances) 

993 
otherInstances["blocks"].sort(SortInstances) 

994 
instances = [instance for (executionOrderId, instance) in orderedInstances] 

1048
b450202605ab
Fixed bug in program elements computation order in PLCGenerator
Laurent Bessard
parents:
1032
diff
changeset

995 
instances.extend(otherInstances["outVariables&coils"] + otherInstances["blocks"] + otherInstances["connectors"]) 
814  996 
for instance in instances: 
1297  997 
if isinstance(instance, (OutVariableClass, InOutVariableClass)): 
814  998 
connections = instance.connectionPointIn.getconnections() 
999 
if connections is not None: 

1000 
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

1001 
if expression is not None: 
2258
c9915bc620cd
Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1948
diff
changeset

1002 
eno_var = self.GetUsedEno(body, connections) 
c9915bc620cd
Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1948
diff
changeset

1003 
if eno_var is not None: 
c9915bc620cd
Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1948
diff
changeset

1004 
self.Program += [(self.CurrentIndent + "IF %s" % eno_var, ())] 
c9915bc620cd
Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1948
diff
changeset

1005 
self.Program += [(" THEN\n ", ())] 
c9915bc620cd
Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1948
diff
changeset

1006 
self.IndentRight() 
c9915bc620cd
Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1948
diff
changeset

1007 

1239
d1f6ea56555d
Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents:
1183
diff
changeset

1008 
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

1009 
(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

1010 
(" := ", ())] 
d1f6ea56555d
Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents:
1183
diff
changeset

1011 
self.Program += expression 
d1f6ea56555d
Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents:
1183
diff
changeset

1012 
self.Program += [(";\n", ())] 
2258
c9915bc620cd
Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1948
diff
changeset

1013 

c9915bc620cd
Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1948
diff
changeset

1014 
if eno_var is not None: 
c9915bc620cd
Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1948
diff
changeset

1015 
self.IndentLeft() 
c9915bc620cd
Fix wrong code generation if EN/ENO are used in FBD/LD/SFC
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1948
diff
changeset

1016 
self.Program += [(self.CurrentIndent + "END_IF;\n", ())] 
1297  1017 
elif isinstance(instance, BlockClass): 
814  1018 
block_type = instance.gettypeName() 
1019 
self.ParentGenerator.GeneratePouProgram(block_type) 

1020 
block_infos = self.GetBlockType(block_type, tuple([self.ConnectionTypes.get(variable.connectionPointIn, "ANY") for variable in instance.inputVariables.getvariable() if variable.getformalParameter() != "EN"])) 

1021 
if block_infos is None: 

1022 
block_infos = self.GetBlockType(block_type) 

1023 
if block_infos is None: 

1765
ccf59c1f0b45
cleanup: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset

1024 
raise PLCGenException( 
ccf59c1f0b45
cleanup: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset

1025 
_("Undefined block type \"{a1}\" in \"{a2}\" POU"). 
ccf59c1f0b45
cleanup: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset

1026 
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

1027 
try: 
1310
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1028 
self.GenerateBlock(instance, block_infos, body, None) 
2418
5587c490a070
Use python 3 compatible exception syntax everywhere
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2277
diff
changeset

1029 
except ValueError as e: 
2447
1c04a50dc7ff
python3 support: pylint, W1645 # (exceptionmessageattribute) Exception.message removed in Python 3
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2432
diff
changeset

1030 
raise PLCGenException(str(e)) 
1297  1031 
elif isinstance(instance, ConnectorClass): 
814  1032 
connector = instance.getname() 
1033 
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

1034 
continue 
d1f6ea56555d
Fixed bug when generating ST code and connection is broken in POU using graphical language
Laurent Bessard
parents:
1183
diff
changeset

1035 
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

1036 
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

1037 
self.ComputedConnectors[connector] = expression 
1297  1038 
elif isinstance(instance, CoilClass): 
814  1039 
connections = instance.connectionPointIn.getconnections() 
1040 
if connections is not None: 

1041 
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

1042 
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

1043 
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

1044 
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

1045 
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

1046 
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

1047 
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

1048 

814  1049 
def FactorizePaths(self, paths): 
1050 
same_paths = {} 

1051 
uncomputed_index = range(len(paths)) 

1052 
factorized_paths = [] 

1053 
for num, path in enumerate(paths): 

2450
5024c19ca8f0
python3 support: pylint, W1652 # (deprecatedtypesfield) Accessing a deprecated fields on the types module
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2447
diff
changeset

1054 
if isinstance(path, list): 
814  1055 
if len(path) > 1: 
1056 
str_path = str(path[1:]) 

1057 
same_paths.setdefault(str_path, []) 

1058 
same_paths[str_path].append((path[:1], num)) 

1059 
else: 

1060 
factorized_paths.append(path) 

1061 
uncomputed_index.remove(num) 

1062 
for same_path, elements in same_paths.items(): 

1063 
if len(elements) > 1: 

1064 
elements_paths = self.FactorizePaths([path for path, num in elements]) 

1065 
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

1066 
factorized_paths.append([tuple(elements_paths)] + eval(same_path)) 
814  1067 
else: 
1068 
factorized_paths.append(elements_paths + eval(same_path)) 

1069 
for path, num in elements: 

1070 
uncomputed_index.remove(num) 

1071 
for num in uncomputed_index: 

1072 
factorized_paths.append(paths[num]) 

1073 
factorized_paths.sort() 

1074 
return factorized_paths 

1075 

1310
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1076 
def GenerateBlock(self, block, block_infos, body, link, order=False, to_inout=False): 
1864
9e64afb38963
fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1863
diff
changeset

1077 

9e64afb38963
fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1863
diff
changeset

1078 
def _GetBlockName(name, type): 
9e64afb38963
fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1863
diff
changeset

1079 
"""function returns name of function or function block instance""" 
9e64afb38963
fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1863
diff
changeset

1080 
if name: 
9e64afb38963
fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1863
diff
changeset

1081 
# function blocks 
9e64afb38963
fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1863
diff
changeset

1082 
blockname = "{a1}({a2})".format(a1=name, a2=type) 
9e64afb38963
fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1863
diff
changeset

1083 
else: 
9e64afb38963
fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1863
diff
changeset

1084 
# functions 
9e64afb38963
fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1863
diff
changeset

1085 
blockname = type 
9e64afb38963
fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1863
diff
changeset

1086 
return blockname 
9e64afb38963
fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1863
diff
changeset

1087 

9e64afb38963
fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1863
diff
changeset

1088 
def _RaiseUnconnectedInOutError(name, type, parameter, place): 
9e64afb38963
fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1863
diff
changeset

1089 
blockname = _GetBlockName(name, type) 
9e64afb38963
fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1863
diff
changeset

1090 
raise ValueError( 
9e64afb38963
fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1863
diff
changeset

1091 
_("InOut variable {a1} in block {a2} in POU {a3} must be connected."). 
9e64afb38963
fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1863
diff
changeset

1092 
format(a1=parameter, a2=blockname, a3=place)) 
9e64afb38963
fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1863
diff
changeset

1093 

1310
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1094 
name = block.getinstanceName() 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1095 
type = block.gettypeName() 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1096 
executionOrderId = block.getexecutionOrderId() 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1097 
input_variables = block.inputVariables.getvariable() 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1098 
output_variables = block.outputVariables.getvariable() 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1099 
inout_variables = {} 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1100 
for input_variable in input_variables: 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1101 
for output_variable in output_variables: 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1102 
if input_variable.getformalParameter() == output_variable.getformalParameter(): 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1103 
inout_variables[input_variable.getformalParameter()] = "" 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1104 
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

1105 
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

1106 
if block_infos["type"] == "function": 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1107 
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

1108 
self.ComputedBlocks[block] = True 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1109 
connected_vars = [] 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1110 
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

1111 
input_connected = dict([("EN", None)] + 
1310
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1112 
[(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

1113 
for variable in input_variables: 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1114 
parameter = variable.getformalParameter() 
1763
bcc07ff2362c
cleanup: fix PEP8 W601 .has_key() is deprecated, use 'in'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1753
diff
changeset

1115 
if parameter in input_connected: 
1310
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1116 
input_connected[parameter] = variable 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1117 
if input_connected["EN"] is None: 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1118 
input_connected.pop("EN") 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1119 
input_parameters = input_names 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1120 
else: 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1121 
input_parameters = ["EN"] + input_names 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1122 
else: 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1123 
input_connected = dict([(variable.getformalParameter(), variable) 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1124 
for variable in input_variables]) 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1125 
input_parameters = [variable.getformalParameter() 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1126 
for variable in input_variables] 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1127 
one_input_connected = False 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1128 
all_input_connected = True 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1129 
for i, parameter in enumerate(input_parameters): 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1130 
variable = input_connected.get(parameter) 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1131 
if variable is not None: 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1132 
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

1133 
connections = variable.connectionPointIn.getconnections() 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1134 
if connections is not None: 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1135 
if parameter != "EN": 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1136 
one_input_connected = True 
1763
bcc07ff2362c
cleanup: fix PEP8 W601 .has_key() is deprecated, use 'in'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1753
diff
changeset

1137 
if parameter in inout_variables: 
1310
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1138 
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

1139 
if expression is not None: 
1863
46d9955e1101
fix error for functions with VAR_IN_OUT
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1862
diff
changeset

1140 
inout_variables[parameter] = expression 
1864
9e64afb38963
fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1863
diff
changeset

1141 
else: 
9e64afb38963
fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1863
diff
changeset

1142 
_RaiseUnconnectedInOutError(name, type, parameter, self.Name) 
1310
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1143 
else: 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1144 
expression = self.ComputeExpression(body, connections, executionOrderId > 0) 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1145 
if expression is not None: 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1146 
connected_vars.append(([(parameter, input_info), (" := ", ())], 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1147 
self.ExtractModifier(variable, expression, input_info))) 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1148 
else: 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1149 
all_input_connected = False 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1150 
else: 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1151 
all_input_connected = False 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1152 
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

1153 
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

1154 
else: 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1155 
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

1156 
if one_input_connected: 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1157 
for i, variable in enumerate(output_variables): 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1158 
parameter = variable.getformalParameter() 
1775
b45f2768fab1
cleanup: fix PEP8 E713 test for membership should be 'not in'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1769
diff
changeset

1159 
if parameter not in inout_variables and parameter in output_names + ["", "ENO"]: 
1310
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1160 
if variable.getformalParameter() == "": 
1734
750eeb7230a1
cleanup: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset

1161 
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

1162 
else: 
2629
caa43c61f90c
Add marker to temporary variables created while generating ST code out of FBD, so that they can be recognized as such.
Edouard Tisserant
parents:
2521
diff
changeset

1163 
variable_name = "_TMP_%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

1164 
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

1165 
self.Interface.append(("VAR", None, False, [])) 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1166 
if variable.connectionPointOut in self.ConnectionTypes: 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1167 
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

1168 
else: 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1169 
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

1170 
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

1171 
vars.append([(parameter, (self.TagName, "block", block.getlocalId(), "output", i)), 
1734
750eeb7230a1
cleanup: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset

1172 
(" => %s" % variable_name, ())]) 
1310
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1173 
else: 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1174 
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

1175 
output_name = variable_name 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1176 
self.Program += [(self.CurrentIndent, ()), 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1177 
(output_name, output_info), 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1178 
(" := ", ()), 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1179 
(type, (self.TagName, "block", block.getlocalId(), "type")), 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1180 
("(", ())] 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1181 
self.Program += JoinList([(", ", ())], vars) 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1182 
self.Program += [(");\n", ())] 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1183 
else: 
1744
69dfdb26f600
cleanup: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset

1184 
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

1185 
self.Warnings.append(msg) 
1310
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1186 
elif block_infos["type"] == "functionBlock": 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1187 
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

1188 
self.ComputedBlocks[block] = True 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1189 
vars = [] 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1190 
offset_idx = 0 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1191 
for variable in input_variables: 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1192 
parameter = variable.getformalParameter() 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1193 
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

1194 
if parameter == "EN": 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1195 
input_idx = 0 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1196 
offset_idx = 1 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1197 
else: 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1198 
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

1199 
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

1200 
connections = variable.connectionPointIn.getconnections() 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1201 
if connections is not None: 
1763
bcc07ff2362c
cleanup: fix PEP8 W601 .has_key() is deprecated, use 'in'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1753
diff
changeset

1202 
expression = self.ComputeExpression(body, connections, executionOrderId > 0, parameter in inout_variables) 
1310
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1203 
if expression is not None: 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1204 
vars.append([(parameter, input_info), 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1205 
(" := ", ())] + self.ExtractModifier(variable, expression, input_info)) 
1864
9e64afb38963
fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1863
diff
changeset

1206 
elif parameter in inout_variables: 
9e64afb38963
fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1863
diff
changeset

1207 
_RaiseUnconnectedInOutError(name, type, parameter, self.Name) 
1418
c97dc5281419
Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents:
1358
diff
changeset

1208 
self.Program += [(self.CurrentIndent, ()), 
1310
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1209 
(name, (self.TagName, "block", block.getlocalId(), "name")), 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1210 
("(", ())] 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1211 
self.Program += JoinList([(", ", ())], vars) 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1212 
self.Program += [(");\n", ())] 
1418
c97dc5281419
Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents:
1358
diff
changeset

1213 

1310
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1214 
if link is not None: 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1215 
connectionPoint = link.getposition()[1] 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1216 
output_parameter = link.getformalParameter() 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1217 
else: 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1218 
connectionPoint = None 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1219 
output_parameter = None 
1418
c97dc5281419
Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents:
1358
diff
changeset

1220 

1310
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1221 
output_variable = None 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1222 
output_idx = 0 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1223 
if output_parameter is not None: 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1224 
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

1225 
for variable in output_variables: 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1226 
if variable.getformalParameter() == output_parameter: 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1227 
output_variable = variable 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1228 
if output_parameter != "ENO": 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1229 
output_idx = output_names.index(output_parameter) 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1230 
else: 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1231 
for i, variable in enumerate(output_variables): 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1232 
blockPointx, blockPointy = variable.connectionPointOut.getrelPositionXY() 
1766
c1e5b9f19483
cleanup: fix PEP8 E129 visually indented line with same indent as next logical line
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1765
diff
changeset

1233 
if connectionPoint is None or \ 
c1e5b9f19483
cleanup: fix PEP8 E129 visually indented line with same indent as next logical line
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1765
diff
changeset

1234 
block.getx() + blockPointx == connectionPoint.getx() and \ 
c1e5b9f19483
cleanup: fix PEP8 E129 visually indented line with same indent as next logical line
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1765
diff
changeset

1235 
block.gety() + blockPointy == connectionPoint.gety(): 
1310
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1236 
output_variable = variable 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1237 
output_parameter = variable.getformalParameter() 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1238 
output_idx = i 
1418
c97dc5281419
Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents:
1358
diff
changeset

1239 

1310
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1240 
if output_variable is not None: 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1241 
if block_infos["type"] == "function": 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1242 
output_info = (self.TagName, "block", block.getlocalId(), "output", output_idx) 
1763
bcc07ff2362c
cleanup: fix PEP8 W601 .has_key() is deprecated, use 'in'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1753
diff
changeset

1243 
if output_parameter in inout_variables: 
1864
9e64afb38963
fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1863
diff
changeset

1244 
for variable in input_variables: 
9e64afb38963
fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1863
diff
changeset

1245 
if variable.getformalParameter() == output_parameter: 
9e64afb38963
fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1863
diff
changeset

1246 
connections = variable.connectionPointIn.getconnections() 
9e64afb38963
fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1863
diff
changeset

1247 
if connections is not None: 
9e64afb38963
fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1863
diff
changeset

1248 
expression = self.ComputeExpression( 
9e64afb38963
fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1863
diff
changeset

1249 
body, connections, executionOrderId > 0, True) 
9e64afb38963
fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1863
diff
changeset

1250 
output_value = expression 
9e64afb38963
fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1863
diff
changeset

1251 
break 
1310
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1252 
else: 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1253 
if output_parameter == "": 
1734
750eeb7230a1
cleanup: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset

1254 
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

1255 
else: 
2629
caa43c61f90c
Add marker to temporary variables created while generating ST code out of FBD, so that they can be recognized as such.
Edouard Tisserant
parents:
2521
diff
changeset

1256 
output_name = "_TMP_%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

1257 
output_value = [(output_name, output_info)] 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1258 
return self.ExtractModifier(output_variable, output_value, output_info) 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1259 
if block_infos["type"] == "functionBlock": 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1260 
output_info = (self.TagName, "block", block.getlocalId(), "output", output_idx) 
1734
750eeb7230a1
cleanup: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset

1261 
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

1262 
if to_inout: 
1734
750eeb7230a1
cleanup: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset

1263 
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

1264 
if not self.IsAlreadyDefined(variable_name): 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1265 
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

1266 
self.Interface.append(("VAR", None, False, [])) 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1267 
if variable.connectionPointOut in self.ConnectionTypes: 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1268 
self.Interface[1][3].append( 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1269 
(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

1270 
else: 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1271 
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

1272 
self.Program += [(self.CurrentIndent, ()), 
1734
750eeb7230a1
cleanup: fix some PEP8 E228 missing whitespace around modulo operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1730
diff
changeset

1273 
("%s := " % variable_name, ())] 
1310
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1274 
self.Program += output_name 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1275 
self.Program += [(";\n", ())] 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1276 
return [(variable_name, ())] 
1418
c97dc5281419
Fixed declaration and ST code gen for IEC function that return derivated types
Edouard Tisserant
parents:
1358
diff
changeset

1277 
return output_name 
1310
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1278 
if link is not None: 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1279 
if output_parameter is None: 
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1280 
output_parameter = "" 
1864
9e64afb38963
fix problems with unconnected input of InOut function variables and
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1863
diff
changeset

1281 
blockname = _GetBlockName(name, type) 
1765
ccf59c1f0b45
cleanup: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset

1282 
raise ValueError( 
ccf59c1f0b45
cleanup: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset

1283 
_("No output {a1} variable found in block {a2} in POU {a3}. Connection must be broken"). 
ccf59c1f0b45
cleanup: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset

1284 
format(a1=output_parameter, a2=blockname, a3=self.Name)) 
1310
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1285 

1744
69dfdb26f600
cleanup: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1743
diff
changeset

1286 
def GeneratePaths(self, connections, body, order=False, to_inout=False): 
814  1287 
paths = [] 
1288 
for connection in connections: 

1289 
localId = connection.getrefLocalId() 

1290 
next = body.getcontentInstance(localId) 

1297  1291 
if isinstance(next, LeftPowerRailClass): 
814  1292 
paths.append(None) 
1297  1293 
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

1294 
paths.append(str([(next.getexpression(), (self.TagName, "io_variable", localId, "expression"))])) 
1297  1295 
elif isinstance(next, BlockClass): 
814  1296 
block_type = next.gettypeName() 
1297 
self.ParentGenerator.GeneratePouProgram(block_type) 

1298 
block_infos = self.GetBlockType(block_type, tuple([self.ConnectionTypes.get(variable.connectionPointIn, "ANY") for variable in next.inputVariables.getvariable() if variable.getformalParameter() != "EN"])) 

1299 
if block_infos is None: 

1300 
block_infos = self.GetBlockType(block_type) 

1301 
if block_infos is None: 

1765
ccf59c1f0b45
cleanup: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset

1302 
raise PLCGenException( 
ccf59c1f0b45
cleanup: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset

1303 
_("Undefined block type \"{a1}\" in \"{a2}\" POU"). 
ccf59c1f0b45
cleanup: fix PEP8 W602 deprecated form of raising exception
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1763
diff
changeset

1304 
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

1305 
try: 
1310
3d7fa2257b24
Removed obsolete process for customizing block code generated in extensions
Laurent Bessard
parents:
1298
diff
changeset

1306 
paths.append(str(self.GenerateBlock(next, block_infos, body, connection, order, to_inout))) 
2418
5587c490a070
Use python 3 compatible exception syntax everywhere
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2277
diff
changeset

1307 
except ValueError as e: 
2447
1c04a50dc7ff
python3 support: pylint, W1645 # (exceptionmessageattribute) Exception.message removed in Python 3
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2432
diff
changeset

1308 
raise PLCGenException(str(e)) 
1297  1309 
elif isinstance(next, ContinuationClass): 
814  1310 
name = next.getname() 
1311 
computed_value = self.ComputedConnectors.get(name, None) 

1743
c3c3d1318130
cleanup: fix PEP8 E711 comparison to None should be 'if cond is not None:'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1742
diff
changeset

1312 
if computed_value is not None: 
814  1313 
paths.append(str(computed_value)) 
1314 
else: 

5743cbdff669
Integration of PLCOpenEditor into Beremiz
Laur&# 