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