author | Laurent Bessard |
Thu, 18 Oct 2012 12:05:45 +0200 | |
changeset 856 | b64e436f000e |
parent 853 | 0f97bddb5a30 |
child 863 | b1ead41fbd3b |
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 |
||
25 |
from xml.dom import minidom |
|
26 |
from types import StringType, UnicodeType, TupleType |
|
27 |
import cPickle |
|
28 |
import os,sys,re |
|
29 |
import datetime |
|
30 |
from time import localtime |
|
31 |
||
32 |
from plcopen import plcopen |
|
33 |
from plcopen.structures import * |
|
34 |
from graphics.GraphicCommons import * |
|
35 |
from PLCGenerator import * |
|
36 |
||
37 |
duration_model = re.compile("(?:([0-9]{1,2})h)?(?:([0-9]{1,2})m(?!s))?(?:([0-9]{1,2})s)?(?:([0-9]{1,3}(?:\.[0-9]*)?)ms)?") |
|
38 |
||
39 |
ITEMS_EDITABLE = [ITEM_PROJECT, |
|
40 |
ITEM_POU, |
|
41 |
ITEM_VARIABLE, |
|
42 |
ITEM_TRANSITION, |
|
43 |
ITEM_ACTION, |
|
44 |
ITEM_CONFIGURATION, |
|
45 |
ITEM_RESOURCE, |
|
46 |
ITEM_DATATYPE |
|
47 |
] = range(8) |
|
48 |
||
49 |
ITEMS_UNEDITABLE = [ITEM_DATATYPES, |
|
50 |
ITEM_FUNCTION, |
|
51 |
ITEM_FUNCTIONBLOCK, |
|
52 |
ITEM_PROGRAM, |
|
53 |
ITEM_TRANSITIONS, |
|
54 |
ITEM_ACTIONS, |
|
55 |
ITEM_CONFIGURATIONS, |
|
56 |
ITEM_RESOURCES, |
|
57 |
ITEM_PROPERTIES |
|
58 |
] = range(8, 17) |
|
59 |
||
60 |
ITEMS_VARIABLE = [ITEM_VAR_LOCAL, |
|
61 |
ITEM_VAR_GLOBAL, |
|
62 |
ITEM_VAR_EXTERNAL, |
|
63 |
ITEM_VAR_TEMP, |
|
64 |
ITEM_VAR_INPUT, |
|
65 |
ITEM_VAR_OUTPUT, |
|
66 |
ITEM_VAR_INOUT |
|
67 |
] = range(17, 24) |
|
68 |
||
69 |
VAR_CLASS_INFOS = {"Local" : (plcopen.interface_localVars, ITEM_VAR_LOCAL), |
|
70 |
"Global" : (plcopen.interface_globalVars, ITEM_VAR_GLOBAL), |
|
71 |
"External" : (plcopen.interface_externalVars, ITEM_VAR_EXTERNAL), |
|
72 |
"Temp" : (plcopen.interface_tempVars, ITEM_VAR_TEMP), |
|
73 |
"Input" : (plcopen.interface_inputVars, ITEM_VAR_INPUT), |
|
74 |
"Output" : (plcopen.interface_outputVars, ITEM_VAR_OUTPUT), |
|
75 |
"InOut" : (plcopen.interface_inOutVars, ITEM_VAR_INOUT) |
|
76 |
} |
|
77 |
||
78 |
POU_TYPES = {"program": ITEM_PROGRAM, |
|
79 |
"functionBlock": ITEM_FUNCTIONBLOCK, |
|
80 |
"function": ITEM_FUNCTION, |
|
81 |
} |
|
82 |
||
83 |
LOCATIONS_ITEMS = [LOCATION_CONFNODE, |
|
84 |
LOCATION_MODULE, |
|
85 |
LOCATION_GROUP, |
|
86 |
LOCATION_VAR_INPUT, |
|
87 |
LOCATION_VAR_OUTPUT, |
|
88 |
LOCATION_VAR_MEMORY] = range(6) |
|
89 |
||
90 |
ScriptDirectory = os.path.split(os.path.realpath(__file__))[0] |
|
91 |
||
92 |
def GetUneditableNames(): |
|
93 |
_ = lambda x:x |
|
94 |
return [_("User-defined POUs"), _("Functions"), _("Function Blocks"), |
|
95 |
_("Programs"), _("Data Types"), _("Transitions"), _("Actions"), |
|
96 |
_("Configurations"), _("Resources"), _("Properties")] |
|
97 |
UNEDITABLE_NAMES = GetUneditableNames() |
|
98 |
[USER_DEFINED_POUS, FUNCTIONS, FUNCTION_BLOCKS, PROGRAMS, |
|
99 |
DATA_TYPES, TRANSITIONS, ACTIONS, CONFIGURATIONS, |
|
100 |
RESOURCES, PROPERTIES] = UNEDITABLE_NAMES |
|
101 |
||
102 |
#------------------------------------------------------------------------------- |
|
103 |
# Undo Buffer for PLCOpenEditor |
|
104 |
#------------------------------------------------------------------------------- |
|
105 |
||
106 |
# Length of the buffer |
|
107 |
UNDO_BUFFER_LENGTH = 20 |
|
108 |
||
109 |
""" |
|
110 |
Class implementing a buffer of changes made on the current editing model |
|
111 |
""" |
|
112 |
class UndoBuffer: |
|
113 |
||
114 |
# Constructor initialising buffer |
|
115 |
def __init__(self, currentstate, issaved = False): |
|
116 |
self.Buffer = [] |
|
117 |
self.CurrentIndex = -1 |
|
118 |
self.MinIndex = -1 |
|
119 |
self.MaxIndex = -1 |
|
120 |
# if current state is defined |
|
121 |
if currentstate: |
|
122 |
self.CurrentIndex = 0 |
|
123 |
self.MinIndex = 0 |
|
124 |
self.MaxIndex = 0 |
|
125 |
# Initialising buffer with currentstate at the first place |
|
126 |
for i in xrange(UNDO_BUFFER_LENGTH): |
|
127 |
if i == 0: |
|
128 |
self.Buffer.append(currentstate) |
|
129 |
else: |
|
130 |
self.Buffer.append(None) |
|
131 |
# Initialising index of state saved |
|
132 |
if issaved: |
|
133 |
self.LastSave = 0 |
|
134 |
else: |
|
135 |
self.LastSave = -1 |
|
136 |
||
137 |
# Add a new state in buffer |
|
138 |
def Buffering(self, currentstate): |
|
139 |
self.CurrentIndex = (self.CurrentIndex + 1) % UNDO_BUFFER_LENGTH |
|
140 |
self.Buffer[self.CurrentIndex] = currentstate |
|
141 |
# Actualising buffer limits |
|
142 |
self.MaxIndex = self.CurrentIndex |
|
143 |
if self.MinIndex == self.CurrentIndex: |
|
144 |
# If the removed state was the state saved, there is no state saved in the buffer |
|
145 |
if self.LastSave == self.MinIndex: |
|
146 |
self.LastSave = -1 |
|
147 |
self.MinIndex = (self.MinIndex + 1) % UNDO_BUFFER_LENGTH |
|
148 |
self.MinIndex = max(self.MinIndex, 0) |
|
149 |
||
150 |
# Return current state of buffer |
|
151 |
def Current(self): |
|
152 |
return self.Buffer[self.CurrentIndex] |
|
153 |
||
154 |
# Change current state to previous in buffer and return new current state |
|
155 |
def Previous(self): |
|
156 |
if self.CurrentIndex != self.MinIndex: |
|
157 |
self.CurrentIndex = (self.CurrentIndex - 1) % UNDO_BUFFER_LENGTH |
|
158 |
return self.Buffer[self.CurrentIndex] |
|
159 |
return None |
|
160 |
||
161 |
# Change current state to next in buffer and return new current state |
|
162 |
def Next(self): |
|
163 |
if self.CurrentIndex != self.MaxIndex: |
|
164 |
self.CurrentIndex = (self.CurrentIndex + 1) % UNDO_BUFFER_LENGTH |
|
165 |
return self.Buffer[self.CurrentIndex] |
|
166 |
return None |
|
167 |
||
168 |
# Return True if current state is the first in buffer |
|
169 |
def IsFirst(self): |
|
170 |
return self.CurrentIndex == self.MinIndex |
|
171 |
||
172 |
# Return True if current state is the last in buffer |
|
173 |
def IsLast(self): |
|
174 |
return self.CurrentIndex == self.MaxIndex |
|
175 |
||
176 |
# Note that current state is saved |
|
177 |
def CurrentSaved(self): |
|
178 |
self.LastSave = self.CurrentIndex |
|
179 |
||
180 |
# Return True if current state is saved |
|
181 |
def IsCurrentSaved(self): |
|
182 |
return self.LastSave == self.CurrentIndex |
|
183 |
||
184 |
||
185 |
#------------------------------------------------------------------------------- |
|
186 |
# Controler for PLCOpenEditor |
|
187 |
#------------------------------------------------------------------------------- |
|
188 |
||
189 |
""" |
|
190 |
Class which controls the operations made on the plcopen model and answers to view requests |
|
191 |
""" |
|
192 |
class PLCControler: |
|
193 |
||
194 |
# Create a new PLCControler |
|
195 |
def __init__(self): |
|
196 |
self.LastNewIndex = 0 |
|
197 |
self.Reset() |
|
198 |
||
199 |
# Reset PLCControler internal variables |
|
200 |
def Reset(self): |
|
201 |
self.Project = None |
|
202 |
self.ProjectBufferEnabled = True |
|
203 |
self.ProjectBuffer = None |
|
204 |
self.ProjectSaved = True |
|
205 |
self.Buffering = False |
|
206 |
self.FilePath = "" |
|
207 |
self.FileName = "" |
|
208 |
self.ProgramChunks = [] |
|
209 |
self.ProgramOffset = 0 |
|
210 |
self.NextCompiledProject = None |
|
211 |
self.CurrentCompiledProject = None |
|
212 |
self.ConfNodeTypes = [] |
|
213 |
self.ProgramFilePath = "" |
|
214 |
||
215 |
def GetQualifierTypes(self): |
|
216 |
return plcopen.QualifierList |
|
217 |
||
218 |
def GetProject(self, debug = False): |
|
219 |
if debug and self.CurrentCompiledProject is not None: |
|
220 |
return self.CurrentCompiledProject |
|
221 |
else: |
|
222 |
return self.Project |
|
223 |
||
224 |
#------------------------------------------------------------------------------- |
|
225 |
# Project management functions |
|
226 |
#------------------------------------------------------------------------------- |
|
227 |
||
228 |
# Return if a project is opened |
|
229 |
def HasOpenedProject(self): |
|
230 |
return self.Project is not None |
|
231 |
||
232 |
# Create a new project by replacing the current one |
|
233 |
def CreateNewProject(self, properties): |
|
234 |
# Create the project |
|
235 |
self.Project = plcopen.project() |
|
236 |
properties["creationDateTime"] = datetime.datetime(*localtime()[:6]) |
|
237 |
self.Project.setfileHeader(properties) |
|
238 |
self.Project.setcontentHeader(properties) |
|
239 |
self.SetFilePath("") |
|
240 |
# Initialize the project buffer |
|
241 |
self.CreateProjectBuffer(False) |
|
242 |
self.ProgramChunks = [] |
|
243 |
self.ProgramOffset = 0 |
|
244 |
self.NextCompiledProject = self.Copy(self.Project) |
|
245 |
self.CurrentCompiledProject = None |
|
246 |
self.Buffering = False |
|
247 |
||
248 |
# Return project data type names |
|
249 |
def GetProjectDataTypeNames(self, debug = False): |
|
250 |
project = self.GetProject(debug) |
|
251 |
if project is not None: |
|
252 |
return [datatype.getname() for datatype in project.getdataTypes()] |
|
253 |
return [] |
|
254 |
||
255 |
# Return project pou names |
|
256 |
def GetProjectPouNames(self, debug = False): |
|
257 |
project = self.GetProject(debug) |
|
258 |
if project is not None: |
|
259 |
return [pou.getname() for pou in project.getpous()] |
|
260 |
return [] |
|
261 |
||
262 |
# Return project pou names |
|
263 |
def GetProjectConfigNames(self, debug = False): |
|
264 |
project = self.GetProject(debug) |
|
265 |
if project is not None: |
|
266 |
return [config.getname() for config in project.getconfigurations()] |
|
267 |
return [] |
|
268 |
||
269 |
# Return project pou variables |
|
270 |
def GetProjectPouVariables(self, pou_name = None, debug = False): |
|
271 |
variables = [] |
|
272 |
project = self.GetProject(debug) |
|
273 |
if project is not None: |
|
274 |
for pou in project.getpous(): |
|
275 |
if pou_name is None or pou_name == pou.getname(): |
|
276 |
variables.extend([var["Name"] for var in self.GetPouInterfaceVars(pou, debug)]) |
|
277 |
for transition in pou.gettransitionList(): |
|
278 |
variables.append(transition.getname()) |
|
279 |
for action in pou.getactionList(): |
|
280 |
variables.append(action.getname()) |
|
281 |
return variables |
|
282 |
||
283 |
# Return file path if project is an open file |
|
284 |
def GetFilePath(self): |
|
285 |
return self.FilePath |
|
286 |
||
287 |
# Return file path if project is an open file |
|
288 |
def GetProgramFilePath(self): |
|
289 |
return self.ProgramFilePath |
|
290 |
||
291 |
# Return file name and point out if file is up to date |
|
292 |
def GetFilename(self): |
|
293 |
if self.Project is not None: |
|
294 |
if self.ProjectIsSaved(): |
|
295 |
return self.FileName |
|
296 |
else: |
|
297 |
return "~%s~"%self.FileName |
|
298 |
return "" |
|
299 |
||
300 |
# Change file path and save file name or create a default one if file path not defined |
|
301 |
def SetFilePath(self, filepath): |
|
302 |
self.FilePath = filepath |
|
303 |
if filepath == "": |
|
304 |
self.LastNewIndex += 1 |
|
305 |
self.FileName = _("Unnamed%d")%self.LastNewIndex |
|
306 |
else: |
|
307 |
self.FileName = os.path.splitext(os.path.basename(filepath))[0] |
|
308 |
||
309 |
# Change project properties |
|
310 |
def SetProjectProperties(self, name = None, properties = None, buffer = True): |
|
311 |
if self.Project is not None: |
|
312 |
if name is not None: |
|
313 |
self.Project.setname(name) |
|
314 |
if properties is not None: |
|
315 |
self.Project.setfileHeader(properties) |
|
316 |
self.Project.setcontentHeader(properties) |
|
317 |
if buffer and (name is not None or properties is not None): |
|
318 |
self.BufferProject() |
|
319 |
||
320 |
# Return project name |
|
321 |
def GetProjectName(self, debug=False): |
|
322 |
project = self.GetProject(debug) |
|
323 |
if project is not None: |
|
324 |
return project.getname() |
|
325 |
return None |
|
326 |
||
327 |
# Return project properties |
|
328 |
def GetProjectProperties(self, debug = False): |
|
329 |
project = self.GetProject(debug) |
|
330 |
if project is not None: |
|
331 |
properties = project.getfileHeader() |
|
332 |
properties.update(project.getcontentHeader()) |
|
333 |
return properties |
|
334 |
return None |
|
335 |
||
336 |
# Return project informations |
|
337 |
def GetProjectInfos(self, debug = False): |
|
338 |
project = self.GetProject(debug) |
|
339 |
if project is not None: |
|
340 |
infos = {"name": project.getname(), "type": ITEM_PROJECT} |
|
341 |
datatypes = {"name": DATA_TYPES, "type": ITEM_DATATYPES, "values":[]} |
|
342 |
for datatype in project.getdataTypes(): |
|
343 |
datatypes["values"].append({"name": datatype.getname(), "type": ITEM_DATATYPE, |
|
344 |
"tagname": self.ComputeDataTypeName(datatype.getname()), "values": []}) |
|
345 |
pou_types = {"function": {"name": FUNCTIONS, "type": ITEM_FUNCTION, "values":[]}, |
|
346 |
"functionBlock": {"name": FUNCTION_BLOCKS, "type": ITEM_FUNCTIONBLOCK, "values":[]}, |
|
347 |
"program": {"name": PROGRAMS, "type": ITEM_PROGRAM, "values":[]}} |
|
348 |
for pou in project.getpous(): |
|
349 |
pou_type = pou.getpouType() |
|
350 |
pou_infos = {"name": pou.getname(), "type": ITEM_POU, |
|
351 |
"tagname": self.ComputePouName(pou.getname())} |
|
352 |
pou_values = [] |
|
353 |
if pou.getbodyType() == "SFC": |
|
354 |
transitions = [] |
|
355 |
for transition in pou.gettransitionList(): |
|
356 |
transitions.append({"name": transition.getname(), "type": ITEM_TRANSITION, |
|
357 |
"tagname": self.ComputePouTransitionName(pou.getname(), transition.getname()), |
|
358 |
"values": []}) |
|
359 |
pou_values.append({"name": TRANSITIONS, "type": ITEM_TRANSITIONS, "values": transitions}) |
|
360 |
actions = [] |
|
361 |
for action in pou.getactionList(): |
|
362 |
actions.append({"name": action.getname(), "type": ITEM_ACTION, |
|
363 |
"tagname": self.ComputePouActionName(pou.getname(), action.getname()), |
|
364 |
"values": []}) |
|
365 |
pou_values.append({"name": ACTIONS, "type": ITEM_ACTIONS, "values": actions}) |
|
366 |
if pou_type in pou_types: |
|
367 |
pou_infos["values"] = pou_values |
|
368 |
pou_types[pou_type]["values"].append(pou_infos) |
|
369 |
configurations = {"name": CONFIGURATIONS, "type": ITEM_CONFIGURATIONS, "values": []} |
|
370 |
for config in project.getconfigurations(): |
|
371 |
config_name = config.getname() |
|
372 |
config_infos = {"name": config_name, "type": ITEM_CONFIGURATION, |
|
373 |
"tagname": self.ComputeConfigurationName(config.getname()), |
|
374 |
"values": []} |
|
375 |
resources = {"name": RESOURCES, "type": ITEM_RESOURCES, "values": []} |
|
376 |
for resource in config.getresource(): |
|
377 |
resource_name = resource.getname() |
|
378 |
resource_infos = {"name": resource_name, "type": ITEM_RESOURCE, |
|
379 |
"tagname": self.ComputeConfigurationResourceName(config.getname(), resource.getname()), |
|
380 |
"values": []} |
|
381 |
resources["values"].append(resource_infos) |
|
382 |
config_infos["values"] = [resources] |
|
383 |
configurations["values"].append(config_infos) |
|
384 |
infos["values"] = [datatypes, pou_types["function"], pou_types["functionBlock"], |
|
385 |
pou_types["program"], configurations] |
|
386 |
return infos |
|
387 |
return None |
|
388 |
||
389 |
def GetPouVariableInfos(self, project, variable, var_class, debug=False): |
|
390 |
vartype_content = variable.gettype().getcontent() |
|
391 |
if vartype_content["name"] == "derived": |
|
392 |
var_type = vartype_content["value"].getname() |
|
393 |
pou_type = None |
|
394 |
pou = project.getpou(var_type) |
|
395 |
if pou is not None: |
|
396 |
pou_type = pou.getpouType() |
|
397 |
edit = debug = pou_type is not None |
|
398 |
if pou_type is None: |
|
399 |
block_infos = self.GetBlockType(var_type, debug = debug) |
|
400 |
if block_infos is not None: |
|
401 |
pou_type = block_infos["type"] |
|
402 |
if pou_type is not None: |
|
403 |
var_class = None |
|
404 |
if pou_type == "program": |
|
405 |
var_class = ITEM_PROGRAM |
|
406 |
elif pou_type != "function": |
|
407 |
var_class = ITEM_FUNCTIONBLOCK |
|
408 |
if var_class is not None: |
|
409 |
return {"name": variable.getname(), |
|
410 |
"type": var_type, |
|
411 |
"class": var_class, |
|
412 |
"edit": edit, |
|
413 |
"debug": debug} |
|
414 |
elif var_type in self.GetDataTypes(debug = debug): |
|
415 |
return {"name": variable.getname(), |
|
416 |
"type": var_type, |
|
417 |
"class": var_class, |
|
418 |
"edit": False, |
|
419 |
"debug": False} |
|
420 |
elif vartype_content["name"] in ["string", "wstring"]: |
|
421 |
return {"name": variable.getname(), |
|
422 |
"type": vartype_content["name"].upper(), |
|
423 |
"class": var_class, |
|
424 |
"edit": False, |
|
425 |
"debug": True} |
|
426 |
else: |
|
427 |
return {"name": variable.getname(), |
|
428 |
"type": vartype_content["name"], |
|
429 |
"class": var_class, |
|
430 |
"edit": False, |
|
431 |
"debug": True} |
|
432 |
return None |
|
433 |
||
434 |
def GetPouVariables(self, tagname, debug = False): |
|
435 |
vars = [] |
|
436 |
pou_type = None |
|
437 |
project = self.GetProject(debug) |
|
438 |
if project is not None: |
|
439 |
words = tagname.split("::") |
|
440 |
if words[0] == "P": |
|
441 |
pou = project.getpou(words[1]) |
|
442 |
if pou is not None: |
|
443 |
pou_type = pou.getpouType() |
|
444 |
if (pou_type in ["program", "functionBlock"] and |
|
445 |
pou.interface is not None): |
|
446 |
# Extract variables from every varLists |
|
447 |
for varlist_type, varlist in pou.getvars(): |
|
448 |
var_infos = VAR_CLASS_INFOS.get(varlist_type, None) |
|
449 |
if var_infos is not None: |
|
450 |
var_class = var_infos[1] |
|
451 |
else: |
|
452 |
var_class = ITEM_VAR_LOCAL |
|
453 |
for variable in varlist.getvariable(): |
|
454 |
var_infos = self.GetPouVariableInfos(project, variable, var_class, debug) |
|
455 |
if var_infos is not None: |
|
456 |
vars.append(var_infos) |
|
826
098f822ef308
Adding transition and action in list of instances of SFC POU in PouInstanceVariablesPanel
laurent
parents:
823
diff
changeset
|
457 |
if pou.getbodyType() == "SFC": |
098f822ef308
Adding transition and action in list of instances of SFC POU in PouInstanceVariablesPanel
laurent
parents:
823
diff
changeset
|
458 |
for transition in pou.gettransitionList(): |
098f822ef308
Adding transition and action in list of instances of SFC POU in PouInstanceVariablesPanel
laurent
parents:
823
diff
changeset
|
459 |
vars.append({ |
098f822ef308
Adding transition and action in list of instances of SFC POU in PouInstanceVariablesPanel
laurent
parents:
823
diff
changeset
|
460 |
"name": transition.getname(), |
098f822ef308
Adding transition and action in list of instances of SFC POU in PouInstanceVariablesPanel
laurent
parents:
823
diff
changeset
|
461 |
"type": None, |
098f822ef308
Adding transition and action in list of instances of SFC POU in PouInstanceVariablesPanel
laurent
parents:
823
diff
changeset
|
462 |
"class": ITEM_TRANSITION, |
098f822ef308
Adding transition and action in list of instances of SFC POU in PouInstanceVariablesPanel
laurent
parents:
823
diff
changeset
|
463 |
"edit": True, |
098f822ef308
Adding transition and action in list of instances of SFC POU in PouInstanceVariablesPanel
laurent
parents:
823
diff
changeset
|
464 |
"debug": True}) |
098f822ef308
Adding transition and action in list of instances of SFC POU in PouInstanceVariablesPanel
laurent
parents:
823
diff
changeset
|
465 |
for action in pou.getactionList(): |
098f822ef308
Adding transition and action in list of instances of SFC POU in PouInstanceVariablesPanel
laurent
parents:
823
diff
changeset
|
466 |
vars.append({ |
098f822ef308
Adding transition and action in list of instances of SFC POU in PouInstanceVariablesPanel
laurent
parents:
823
diff
changeset
|
467 |
"name": action.getname(), |
098f822ef308
Adding transition and action in list of instances of SFC POU in PouInstanceVariablesPanel
laurent
parents:
823
diff
changeset
|
468 |
"type": None, |
098f822ef308
Adding transition and action in list of instances of SFC POU in PouInstanceVariablesPanel
laurent
parents:
823
diff
changeset
|
469 |
"class": ITEM_ACTION, |
098f822ef308
Adding transition and action in list of instances of SFC POU in PouInstanceVariablesPanel
laurent
parents:
823
diff
changeset
|
470 |
"edit": True, |
098f822ef308
Adding transition and action in list of instances of SFC POU in PouInstanceVariablesPanel
laurent
parents:
823
diff
changeset
|
471 |
"debug": True}) |
814 | 472 |
return {"class": POU_TYPES[pou_type], |
473 |
"type": words[1], |
|
474 |
"variables": vars, |
|
475 |
"edit": True, |
|
476 |
"debug": True} |
|
477 |
else: |
|
478 |
block_infos = self.GetBlockType(words[1], debug = debug) |
|
479 |
if (block_infos is not None and |
|
480 |
block_infos["type"] in ["program", "functionBlock"]): |
|
481 |
for varname, vartype, varmodifier in block_infos["inputs"]: |
|
482 |
vars.append({"name" : varname, |
|
483 |
"type" : vartype, |
|
484 |
"class" : ITEM_VAR_INPUT, |
|
485 |
"edit": False, |
|
486 |
"debug": True}) |
|
487 |
for varname, vartype, varmodifier in block_infos["outputs"]: |
|
488 |
vars.append({"name" : varname, |
|
489 |
"type" : vartype, |
|
490 |
"class" : ITEM_VAR_OUTPUT, |
|
491 |
"edit": False, |
|
492 |
"debug": True}) |
|
493 |
return {"class": POU_TYPES[block_infos["type"]], |
|
494 |
"type": None, |
|
495 |
"variables": vars, |
|
496 |
"edit": False, |
|
497 |
"debug": False} |
|
826
098f822ef308
Adding transition and action in list of instances of SFC POU in PouInstanceVariablesPanel
laurent
parents:
823
diff
changeset
|
498 |
elif words[0] in ['A', 'T']: |
098f822ef308
Adding transition and action in list of instances of SFC POU in PouInstanceVariablesPanel
laurent
parents:
823
diff
changeset
|
499 |
pou_vars = self.GetPouVariables(self.ComputePouName(words[1]), debug) |
098f822ef308
Adding transition and action in list of instances of SFC POU in PouInstanceVariablesPanel
laurent
parents:
823
diff
changeset
|
500 |
if pou_vars is not None: |
098f822ef308
Adding transition and action in list of instances of SFC POU in PouInstanceVariablesPanel
laurent
parents:
823
diff
changeset
|
501 |
if words[0] == 'A': |
098f822ef308
Adding transition and action in list of instances of SFC POU in PouInstanceVariablesPanel
laurent
parents:
823
diff
changeset
|
502 |
element_type = ITEM_ACTION |
098f822ef308
Adding transition and action in list of instances of SFC POU in PouInstanceVariablesPanel
laurent
parents:
823
diff
changeset
|
503 |
elif words[0] == 'T': |
098f822ef308
Adding transition and action in list of instances of SFC POU in PouInstanceVariablesPanel
laurent
parents:
823
diff
changeset
|
504 |
element_type = ITEM_TRANSITION |
098f822ef308
Adding transition and action in list of instances of SFC POU in PouInstanceVariablesPanel
laurent
parents:
823
diff
changeset
|
505 |
return {"class": element_type, |
098f822ef308
Adding transition and action in list of instances of SFC POU in PouInstanceVariablesPanel
laurent
parents:
823
diff
changeset
|
506 |
"type": None, |
098f822ef308
Adding transition and action in list of instances of SFC POU in PouInstanceVariablesPanel
laurent
parents:
823
diff
changeset
|
507 |
"variables": [var for var in pou_vars["variables"] |
098f822ef308
Adding transition and action in list of instances of SFC POU in PouInstanceVariablesPanel
laurent
parents:
823
diff
changeset
|
508 |
if var["class"] not in [ITEM_ACTION, ITEM_TRANSITION]], |
098f822ef308
Adding transition and action in list of instances of SFC POU in PouInstanceVariablesPanel
laurent
parents:
823
diff
changeset
|
509 |
"edit": True, |
098f822ef308
Adding transition and action in list of instances of SFC POU in PouInstanceVariablesPanel
laurent
parents:
823
diff
changeset
|
510 |
"debug": True} |
814 | 511 |
elif words[0] in ['C', 'R']: |
512 |
if words[0] == 'C': |
|
513 |
element_type = ITEM_CONFIGURATION |
|
514 |
element = project.getconfiguration(words[1]) |
|
515 |
if element is not None: |
|
516 |
for resource in element.getresource(): |
|
517 |
vars.append({"name": resource.getname(), |
|
518 |
"type": None, |
|
519 |
"class": ITEM_RESOURCE, |
|
520 |
"edit": True, |
|
521 |
"debug": False}) |
|
522 |
elif words[0] == 'R': |
|
523 |
element_type = ITEM_RESOURCE |
|
524 |
element = project.getconfigurationResource(words[1], words[2]) |
|
525 |
if element is not None: |
|
526 |
for task in element.gettask(): |
|
527 |
for pou in task.getpouInstance(): |
|
528 |
vars.append({"name": pou.getname(), |
|
529 |
"type": pou.gettypeName(), |
|
530 |
"class": ITEM_PROGRAM, |
|
531 |
"edit": True, |
|
532 |
"debug": True}) |
|
533 |
for pou in element.getpouInstance(): |
|
534 |
vars.append({"name": pou.getname(), |
|
535 |
"type": pou.gettypeName(), |
|
536 |
"class": ITEM_PROGRAM, |
|
537 |
"edit": True, |
|
538 |
"debug": True}) |
|
539 |
if element is not None: |
|
540 |
for varlist in element.getglobalVars(): |
|
541 |
for variable in varlist.getvariable(): |
|
542 |
var_infos = self.GetPouVariableInfos(project, variable, ITEM_VAR_GLOBAL, debug) |
|
543 |
if var_infos is not None: |
|
544 |
vars.append(var_infos) |
|
545 |
return {"class": element_type, |
|
546 |
"type": None, |
|
547 |
"variables": vars, |
|
548 |
"edit": True, |
|
549 |
"debug": False} |
|
550 |
return None |
|
551 |
||
552 |
def RecursiveSearchPouInstances(self, project, pou_type, parent_path, varlists, debug = False): |
|
553 |
instances = [] |
|
554 |
for varlist in varlists: |
|
555 |
for variable in varlist.getvariable(): |
|
556 |
vartype_content = variable.gettype().getcontent() |
|
557 |
if vartype_content["name"] == "derived": |
|
558 |
var_path = "%s.%s" % (parent_path, variable.getname()) |
|
559 |
var_type = vartype_content["value"].getname() |
|
560 |
if var_type == pou_type: |
|
561 |
instances.append(var_path) |
|
562 |
else: |
|
563 |
pou = project.getpou(var_type) |
|
564 |
if pou is not None: |
|
565 |
instances.extend( |
|
566 |
self.RecursiveSearchPouInstances( |
|
567 |
project, pou_type, var_path, |
|
568 |
[varlist for type, varlist in pou.getvars()], |
|
569 |
debug)) |
|
570 |
return instances |
|
571 |
||
572 |
def SearchPouInstances(self, tagname, debug = False): |
|
573 |
project = self.GetProject(debug) |
|
574 |
if project is not None: |
|
575 |
words = tagname.split("::") |
|
576 |
if words[0] == "P": |
|
577 |
instances = [] |
|
578 |
for config in project.getconfigurations(): |
|
579 |
config_name = config.getname() |
|
580 |
instances.extend( |
|
581 |
self.RecursiveSearchPouInstances( |
|
582 |
project, words[1], config_name, |
|
583 |
config.getglobalVars(), debug)) |
|
584 |
for resource in config.getresource(): |
|
585 |
res_path = "%s.%s" % (config_name, resource.getname()) |
|
586 |
instances.extend( |
|
587 |
self.RecursiveSearchPouInstances( |
|
588 |
project, words[1], res_path, |
|
589 |
resource.getglobalVars(), debug)) |
|
590 |
pou_instances = resource.getpouInstance()[:] |
|
591 |
for task in resource.gettask(): |
|
592 |
pou_instances.extend(task.getpouInstance()) |
|
593 |
for pou_instance in pou_instances: |
|
594 |
pou_path = "%s.%s" % (res_path, pou_instance.getname()) |
|
595 |
pou_type = pou_instance.gettypeName() |
|
596 |
if pou_type == words[1]: |
|
597 |
instances.append(pou_path) |
|
598 |
pou = project.getpou(pou_type) |
|
599 |
if pou is not None: |
|
600 |
instances.extend( |
|
601 |
self.RecursiveSearchPouInstances( |
|
602 |
project, words[1], pou_path, |
|
603 |
[varlist for type, varlist in pou.getvars()], |
|
604 |
debug)) |
|
605 |
return instances |
|
606 |
elif words[0] == 'C': |
|
607 |
return [words[1]] |
|
608 |
elif words[0] == 'R': |
|
609 |
return ["%s.%s" % (words[1], words[2])] |
|
826
098f822ef308
Adding transition and action in list of instances of SFC POU in PouInstanceVariablesPanel
laurent
parents:
823
diff
changeset
|
610 |
elif words[0] in ['T', 'A']: |
098f822ef308
Adding transition and action in list of instances of SFC POU in PouInstanceVariablesPanel
laurent
parents:
823
diff
changeset
|
611 |
return ["%s.%s" % (instance, words[2]) |
098f822ef308
Adding transition and action in list of instances of SFC POU in PouInstanceVariablesPanel
laurent
parents:
823
diff
changeset
|
612 |
for instance in self.SearchPouInstances( |
098f822ef308
Adding transition and action in list of instances of SFC POU in PouInstanceVariablesPanel
laurent
parents:
823
diff
changeset
|
613 |
self.ComputePouName(words[1]), debug)] |
814 | 614 |
return [] |
615 |
||
616 |
def RecursiveGetPouInstanceTagName(self, project, pou_type, parts): |
|
617 |
pou = project.getpou(pou_type) |
|
618 |
if pou is not None: |
|
619 |
if len(parts) == 0: |
|
620 |
return self.ComputePouName(pou_type) |
|
621 |
||
622 |
for varlist_type, varlist in pou.getvars(): |
|
623 |
for variable in varlist.getvariable(): |
|
827
a2ce084fb598
Fix restore project tab layout with transition and action debug tabs
laurent
parents:
826
diff
changeset
|
624 |
if variable.getname() == parts[0]: |
a2ce084fb598
Fix restore project tab layout with transition and action debug tabs
laurent
parents:
826
diff
changeset
|
625 |
vartype_content = variable.gettype().getcontent() |
a2ce084fb598
Fix restore project tab layout with transition and action debug tabs
laurent
parents:
826
diff
changeset
|
626 |
if vartype_content["name"] == "derived": |
a2ce084fb598
Fix restore project tab layout with transition and action debug tabs
laurent
parents:
826
diff
changeset
|
627 |
return self.RecursiveGetPouInstanceTagName( |
a2ce084fb598
Fix restore project tab layout with transition and action debug tabs
laurent
parents:
826
diff
changeset
|
628 |
project, |
a2ce084fb598
Fix restore project tab layout with transition and action debug tabs
laurent
parents:
826
diff
changeset
|
629 |
vartype_content["value"].getname(), |
a2ce084fb598
Fix restore project tab layout with transition and action debug tabs
laurent
parents:
826
diff
changeset
|
630 |
parts[1:]) |
a2ce084fb598
Fix restore project tab layout with transition and action debug tabs
laurent
parents:
826
diff
changeset
|
631 |
|
a2ce084fb598
Fix restore project tab layout with transition and action debug tabs
laurent
parents:
826
diff
changeset
|
632 |
if pou.getbodyType() == "SFC" and len(parts) == 1: |
a2ce084fb598
Fix restore project tab layout with transition and action debug tabs
laurent
parents:
826
diff
changeset
|
633 |
for action in pou.getactionList(): |
a2ce084fb598
Fix restore project tab layout with transition and action debug tabs
laurent
parents:
826
diff
changeset
|
634 |
if action.getname() == parts[0]: |
a2ce084fb598
Fix restore project tab layout with transition and action debug tabs
laurent
parents:
826
diff
changeset
|
635 |
return self.ComputePouActionName(pou_type, parts[0]) |
a2ce084fb598
Fix restore project tab layout with transition and action debug tabs
laurent
parents:
826
diff
changeset
|
636 |
for transition in pou.gettransitionList(): |
a2ce084fb598
Fix restore project tab layout with transition and action debug tabs
laurent
parents:
826
diff
changeset
|
637 |
if transition.getname() == parts[0]: |
a2ce084fb598
Fix restore project tab layout with transition and action debug tabs
laurent
parents:
826
diff
changeset
|
638 |
return self.ComputePouTransitionName(pou_type, parts[0]) |
814 | 639 |
return None |
640 |
||
641 |
def GetPouInstanceTagName(self, instance_path, debug = False): |
|
642 |
parts = instance_path.split(".") |
|
643 |
if len(parts) == 1: |
|
644 |
return self.ComputeConfigurationName(parts[0]) |
|
645 |
elif len(parts) == 2: |
|
646 |
return self.ComputeConfigurationResourceName(parts[0], parts[1]) |
|
647 |
else: |
|
648 |
project = self.GetProject(debug) |
|
649 |
for config in project.getconfigurations(): |
|
650 |
if config.getname() == parts[0]: |
|
651 |
for resource in config.getresource(): |
|
652 |
if resource.getname() == parts[1]: |
|
653 |
pou_instances = resource.getpouInstance()[:] |
|
654 |
for task in resource.gettask(): |
|
655 |
pou_instances.extend(task.getpouInstance()) |
|
656 |
for pou_instance in pou_instances: |
|
657 |
if pou_instance.getname() == parts[2]: |
|
658 |
if len(parts) == 3: |
|
659 |
return self.ComputePouName( |
|
660 |
pou_instance.gettypeName()) |
|
661 |
else: |
|
662 |
return self.RecursiveGetPouInstanceTagName( |
|
663 |
project, |
|
664 |
pou_instance.gettypeName(), |
|
665 |
parts[3:]) |
|
666 |
return None |
|
667 |
||
668 |
def GetInstanceInfos(self, instance_path, debug = False): |
|
669 |
tagname = self.GetPouInstanceTagName(instance_path) |
|
670 |
if tagname is not None: |
|
827
a2ce084fb598
Fix restore project tab layout with transition and action debug tabs
laurent
parents:
826
diff
changeset
|
671 |
infos = self.GetPouVariables(tagname, debug) |
a2ce084fb598
Fix restore project tab layout with transition and action debug tabs
laurent
parents:
826
diff
changeset
|
672 |
infos["type"] = tagname |
a2ce084fb598
Fix restore project tab layout with transition and action debug tabs
laurent
parents:
826
diff
changeset
|
673 |
return infos |
814 | 674 |
else: |
675 |
pou_path, var_name = instance_path.rsplit(".", 1) |
|
676 |
tagname = self.GetPouInstanceTagName(pou_path) |
|
677 |
if tagname is not None: |
|
678 |
pou_infos = self.GetPouVariables(tagname, debug) |
|
679 |
for var_infos in pou_infos["variables"]: |
|
680 |
if var_infos["name"] == var_name: |
|
681 |
return var_infos |
|
682 |
return None |
|
683 |
||
684 |
# Return if data type given by name is used by another data type or pou |
|
685 |
def DataTypeIsUsed(self, name, debug = False): |
|
686 |
project = self.GetProject(debug) |
|
687 |
if project is not None: |
|
688 |
return project.ElementIsUsed(name) or project.DataTypeIsDerived(name) |
|
689 |
return False |
|
690 |
||
691 |
# Return if pou given by name is used by another pou |
|
692 |
def PouIsUsed(self, name, debug = False): |
|
693 |
project = self.GetProject(debug) |
|
694 |
if project is not None: |
|
695 |
return project.ElementIsUsed(name) |
|
696 |
return False |
|
697 |
||
698 |
# Return if pou given by name is directly or undirectly used by the reference pou |
|
699 |
def PouIsUsedBy(self, name, reference, debug = False): |
|
700 |
project = self.GetProject(debug) |
|
701 |
if project is not None: |
|
702 |
return project.ElementIsUsedBy(name, reference) |
|
703 |
return False |
|
704 |
||
705 |
def GenerateProgram(self, filepath=None): |
|
706 |
errors = [] |
|
707 |
warnings = [] |
|
708 |
if self.Project is not None: |
|
709 |
try: |
|
710 |
self.ProgramChunks = GenerateCurrentProgram(self, self.Project, errors, warnings) |
|
711 |
self.NextCompiledProject = self.Copy(self.Project) |
|
712 |
program_text = "".join([item[0].decode("utf-8") for item in self.ProgramChunks]) |
|
713 |
if filepath is not None: |
|
714 |
programfile = open(filepath, "w") |
|
715 |
programfile.write(program_text.encode("utf-8")) |
|
716 |
programfile.close() |
|
717 |
self.ProgramFilePath = filepath |
|
718 |
return program_text, errors, warnings |
|
719 |
except PLCGenException, e: |
|
720 |
errors.append(e.message) |
|
721 |
else: |
|
722 |
errors.append("No project opened") |
|
723 |
return "", errors, warnings |
|
724 |
||
725 |
def DebugAvailable(self): |
|
726 |
return self.CurrentCompiledProject is not None |
|
727 |
||
728 |
def ProgramTransferred(self): |
|
729 |
if self.NextCompiledProject is None: |
|
730 |
self.CurrentCompiledProject = self.NextCompiledProject |
|
731 |
else: |
|
732 |
self.CurrentCompiledProject = self.Copy(self.Project) |
|
733 |
||
734 |
def GetChunkInfos(self, from_location, to_location): |
|
735 |
row = self.ProgramOffset + 1 |
|
736 |
col = 1 |
|
737 |
infos = [] |
|
738 |
for chunk, chunk_infos in self.ProgramChunks: |
|
739 |
lines = chunk.split("\n") |
|
740 |
if len(lines) > 1: |
|
741 |
next_row = row + len(lines) - 1 |
|
742 |
next_col = len(lines[-1]) + 1 |
|
743 |
else: |
|
744 |
next_row = row |
|
745 |
next_col = col + len(chunk) |
|
746 |
if (next_row > from_location[0] or next_row == from_location[0] and next_col >= from_location[1]) and len(chunk_infos) > 0: |
|
747 |
infos.append((chunk_infos, (row, col))) |
|
748 |
if next_row == to_location[0] and next_col > to_location[1] or next_row > to_location[0]: |
|
749 |
return infos |
|
750 |
row, col = next_row, next_col |
|
751 |
return infos |
|
752 |
||
753 |
#------------------------------------------------------------------------------- |
|
754 |
# Project Pous management functions |
|
755 |
#------------------------------------------------------------------------------- |
|
756 |
||
757 |
# Add a Data Type to Project |
|
758 |
def ProjectAddDataType(self, datatype_name=None): |
|
759 |
if self.Project is not None: |
|
760 |
if datatype_name is None: |
|
761 |
datatype_name = self.GenerateNewName(None, None, "datatype%d") |
|
762 |
# Add the datatype to project |
|
763 |
self.Project.appenddataType(datatype_name) |
|
764 |
self.BufferProject() |
|
765 |
return self.ComputeDataTypeName(datatype_name) |
|
766 |
return None |
|
767 |
||
768 |
# Remove a Data Type from project |
|
769 |
def ProjectRemoveDataType(self, datatype_name): |
|
770 |
if self.Project is not None: |
|
771 |
self.Project.removedataType(datatype_name) |
|
772 |
self.BufferProject() |
|
773 |
||
774 |
# Add a Pou to Project |
|
775 |
def ProjectAddPou(self, pou_name, pou_type, body_type): |
|
776 |
if self.Project is not None: |
|
777 |
# Add the pou to project |
|
778 |
self.Project.appendpou(pou_name, pou_type, body_type) |
|
779 |
if pou_type == "function": |
|
780 |
self.SetPouInterfaceReturnType(pou_name, "BOOL") |
|
781 |
self.BufferProject() |
|
782 |
return self.ComputePouName(pou_name) |
|
783 |
return None |
|
784 |
||
785 |
def ProjectChangePouType(self, name, pou_type): |
|
786 |
if self.Project is not None: |
|
787 |
pou = self.Project.getpou(name) |
|
788 |
if pou is not None: |
|
789 |
pou.setpouType(pou_type) |
|
790 |
self.Project.RefreshCustomBlockTypes() |
|
791 |
self.BufferProject() |
|
792 |
||
793 |
def GetPouXml(self, pou_name): |
|
794 |
if self.Project is not None: |
|
795 |
pou = self.Project.getpou(pou_name) |
|
796 |
if pou is not None: |
|
797 |
return pou.generateXMLText('pou', 0) |
|
798 |
return None |
|
799 |
||
800 |
def PastePou(self, pou_type, pou_xml): |
|
801 |
''' |
|
802 |
Adds the POU defined by 'pou_xml' to the current project with type 'pou_type' |
|
803 |
''' |
|
804 |
try: |
|
805 |
tree = minidom.parseString(pou_xml.encode("utf-8")) |
|
806 |
root = tree.childNodes[0] |
|
807 |
except: |
|
808 |
return _("Couldn't paste non-POU object.") |
|
809 |
||
810 |
if root.nodeName == "pou": |
|
811 |
new_pou = plcopen.pous_pou() |
|
812 |
new_pou.loadXMLTree(root) |
|
813 |
||
814 |
name = new_pou.getname() |
|
815 |
||
816 |
idx = 0 |
|
817 |
new_name = name |
|
818 |
while self.Project.getpou(new_name): |
|
819 |
# a POU with that name already exists. |
|
820 |
# make a new name and test if a POU with that name exists. |
|
821 |
# append an incrementing numeric suffix to the POU name. |
|
822 |
idx += 1 |
|
823 |
new_name = "%s%d" % (name, idx) |
|
824 |
||
825 |
# we've found a name that does not already exist, use it |
|
826 |
new_pou.setname(new_name) |
|
827 |
||
828 |
if pou_type is not None: |
|
829 |
orig_type = new_pou.getpouType() |
|
830 |
||
831 |
# prevent violations of POU content restrictions: |
|
832 |
# function blocks cannot be pasted as functions, |
|
833 |
# programs cannot be pasted as functions or function blocks |
|
834 |
if orig_type == 'functionBlock' and pou_type == 'function' or \ |
|
835 |
orig_type == 'program' and pou_type in ['function', 'functionBlock']: |
|
836 |
return _('''%s "%s" can't be pasted as a %s.''') % (orig_type, name, pou_type) |
|
837 |
||
838 |
new_pou.setpouType(pou_type) |
|
839 |
||
840 |
self.Project.insertpou(-1, new_pou) |
|
841 |
self.BufferProject() |
|
842 |
||
843 |
return self.ComputePouName(new_name), |
|
844 |
else: |
|
845 |
return _("Couldn't paste non-POU object.") |
|
846 |
||
847 |
# Remove a Pou from project |
|
848 |
def ProjectRemovePou(self, pou_name): |
|
849 |
if self.Project is not None: |
|
850 |
self.Project.removepou(pou_name) |
|
851 |
self.BufferProject() |
|
852 |
||
853 |
# Return the name of the configuration if only one exist |
|
854 |
def GetProjectMainConfigurationName(self): |
|
855 |
if self.Project is not None: |
|
856 |
# Found the configuration corresponding to old name and change its name to new name |
|
857 |
configurations = self.Project.getconfigurations() |
|
858 |
if len(configurations) == 1: |
|
859 |
return configurations[0].getname() |
|
860 |
return None |
|
861 |
||
862 |
# Add a configuration to Project |
|
863 |
def ProjectAddConfiguration(self, config_name=None): |
|
864 |
if self.Project is not None: |
|
865 |
if config_name is None: |
|
866 |
config_name = self.GenerateNewName(None, None, "configuration%d") |
|
867 |
self.Project.addconfiguration(config_name) |
|
868 |
self.BufferProject() |
|
869 |
return self.ComputeConfigurationName(config_name) |
|
870 |
return None |
|
871 |
||
872 |
# Remove a configuration from project |
|
873 |
def ProjectRemoveConfiguration(self, config_name): |
|
874 |
if self.Project is not None: |
|
875 |
self.Project.removeconfiguration(config_name) |
|
876 |
self.BufferProject() |
|
877 |
||
878 |
# Add a resource to a configuration of the Project |
|
879 |
def ProjectAddConfigurationResource(self, config_name, resource_name=None): |
|
880 |
if self.Project is not None: |
|
881 |
if resource_name is None: |
|
882 |
resource_name = self.GenerateNewName(None, None, "resource%d") |
|
883 |
self.Project.addconfigurationResource(config_name, resource_name) |
|
884 |
self.BufferProject() |
|
885 |
return self.ComputeConfigurationResourceName(config_name, resource_name) |
|
886 |
return None |
|
887 |
||
888 |
# Remove a resource from a configuration of the project |
|
889 |
def ProjectRemoveConfigurationResource(self, config_name, resource_name): |
|
890 |
if self.Project is not None: |
|
891 |
self.Project.removeconfigurationResource(config_name, resource_name) |
|
892 |
self.BufferProject() |
|
893 |
||
894 |
# Add a Transition to a Project Pou |
|
895 |
def ProjectAddPouTransition(self, pou_name, transition_name, transition_type): |
|
896 |
if self.Project is not None: |
|
897 |
pou = self.Project.getpou(pou_name) |
|
898 |
if pou is not None: |
|
899 |
pou.addtransition(transition_name, transition_type) |
|
900 |
self.BufferProject() |
|
901 |
return self.ComputePouTransitionName(pou_name, transition_name) |
|
902 |
return None |
|
903 |
||
904 |
# Remove a Transition from a Project Pou |
|
905 |
def ProjectRemovePouTransition(self, pou_name, transition_name): |
|
906 |
# Search if the pou removed is currently opened |
|
907 |
if self.Project is not None: |
|
908 |
pou = self.Project.getpou(pou_name) |
|
909 |
if pou is not None: |
|
910 |
pou.removetransition(transition_name) |
|
911 |
self.BufferProject() |
|
912 |
||
913 |
# Add an Action to a Project Pou |
|
914 |
def ProjectAddPouAction(self, pou_name, action_name, action_type): |
|
915 |
if self.Project is not None: |
|
916 |
pou = self.Project.getpou(pou_name) |
|
917 |
if pou is not None: |
|
918 |
pou.addaction(action_name, action_type) |
|
919 |
self.BufferProject() |
|
920 |
return self.ComputePouActionName(pou_name, action_name) |
|
921 |
return None |
|
922 |
||
923 |
# Remove an Action from a Project Pou |
|
924 |
def ProjectRemovePouAction(self, pou_name, action_name): |
|
925 |
# Search if the pou removed is currently opened |
|
926 |
if self.Project is not None: |
|
927 |
pou = self.Project.getpou(pou_name) |
|
928 |
if pou is not None: |
|
929 |
pou.removeaction(action_name) |
|
930 |
self.BufferProject() |
|
931 |
||
932 |
# Change the name of a pou |
|
933 |
def ChangeDataTypeName(self, old_name, new_name): |
|
934 |
if self.Project is not None: |
|
935 |
# Found the pou corresponding to old name and change its name to new name |
|
936 |
datatype = self.Project.getdataType(old_name) |
|
937 |
if datatype is not None: |
|
938 |
datatype.setname(new_name) |
|
939 |
self.Project.updateElementName(old_name, new_name) |
|
940 |
self.Project.RefreshElementUsingTree() |
|
941 |
self.Project.RefreshDataTypeHierarchy() |
|
942 |
self.BufferProject() |
|
943 |
||
944 |
# Change the name of a pou |
|
945 |
def ChangePouName(self, old_name, new_name): |
|
946 |
if self.Project is not None: |
|
947 |
# Found the pou corresponding to old name and change its name to new name |
|
948 |
pou = self.Project.getpou(old_name) |
|
949 |
if pou is not None: |
|
950 |
pou.setname(new_name) |
|
951 |
self.Project.updateElementName(old_name, new_name) |
|
952 |
self.Project.RefreshElementUsingTree() |
|
953 |
self.Project.RefreshCustomBlockTypes() |
|
954 |
self.BufferProject() |
|
955 |
||
956 |
# Change the name of a pou transition |
|
957 |
def ChangePouTransitionName(self, pou_name, old_name, new_name): |
|
958 |
if self.Project is not None: |
|
959 |
# Found the pou transition corresponding to old name and change its name to new name |
|
960 |
pou = self.Project.getpou(pou_name) |
|
961 |
if pou is not None: |
|
962 |
transition = pou.gettransition(old_name) |
|
963 |
if transition is not None: |
|
964 |
transition.setname(new_name) |
|
965 |
pou.updateElementName(old_name, new_name) |
|
966 |
self.BufferProject() |
|
967 |
||
968 |
# Change the name of a pou action |
|
969 |
def ChangePouActionName(self, pou_name, old_name, new_name): |
|
970 |
if self.Project is not None: |
|
971 |
# Found the pou action corresponding to old name and change its name to new name |
|
972 |
pou = self.Project.getpou(pou_name) |
|
973 |
if pou is not None: |
|
974 |
action = pou.getaction(old_name) |
|
975 |
if action is not None: |
|
976 |
action.setname(new_name) |
|
977 |
pou.updateElementName(old_name, new_name) |
|
978 |
self.BufferProject() |
|
979 |
||
980 |
# Change the name of a pou variable |
|
981 |
def ChangePouVariableName(self, pou_name, old_name, new_name): |
|
982 |
if self.Project is not None: |
|
983 |
# Found the pou action corresponding to old name and change its name to new name |
|
984 |
pou = self.Project.getpou(pou_name) |
|
985 |
if pou is not None: |
|
986 |
for type, varlist in pou.getvars(): |
|
987 |
for var in varlist.getvariable(): |
|
988 |
if var.getname() == old_name: |
|
989 |
var.setname(new_name) |
|
990 |
self.Project.RefreshCustomBlockTypes() |
|
991 |
self.BufferProject() |
|
992 |
||
993 |
# Change the name of a configuration |
|
994 |
def ChangeConfigurationName(self, old_name, new_name): |
|
995 |
if self.Project is not None: |
|
996 |
# Found the configuration corresponding to old name and change its name to new name |
|
997 |
configuration = self.Project.getconfiguration(old_name) |
|
998 |
if configuration is not None: |
|
999 |
configuration.setname(new_name) |
|
1000 |
self.BufferProject() |
|
1001 |
||
1002 |
# Change the name of a configuration resource |
|
1003 |
def ChangeConfigurationResourceName(self, config_name, old_name, new_name): |
|
1004 |
if self.Project is not None: |
|
1005 |
# Found the resource corresponding to old name and change its name to new name |
|
1006 |
resource = self.Project.getconfigurationResource(config_name, old_name) |
|
1007 |
if resource is not None: |
|
1008 |
resource.setname(new_name) |
|
1009 |
self.BufferProject() |
|
1010 |
||
1011 |
# Return the description of the pou given by its name |
|
1012 |
def GetPouDescription(self, name, debug = False): |
|
1013 |
project = self.GetProject(debug) |
|
1014 |
if project is not None: |
|
1015 |
# Found the pou correponding to name and return its type |
|
1016 |
pou = project.getpou(name) |
|
1017 |
if pou is not None: |
|
1018 |
return pou.getdescription() |
|
1019 |
return "" |
|
1020 |
||
1021 |
# Return the description of the pou given by its name |
|
1022 |
def SetPouDescription(self, name, description, debug = False): |
|
1023 |
project = self.GetProject(debug) |
|
1024 |
if project is not None: |
|
1025 |
# Found the pou correponding to name and return its type |
|
1026 |
pou = project.getpou(name) |
|
1027 |
if pou is not None: |
|
1028 |
pou.setdescription(description) |
|
1029 |
project.RefreshCustomBlockTypes() |
|
1030 |
self.BufferProject() |
|
1031 |
||
1032 |
# Return the type of the pou given by its name |
|
1033 |
def GetPouType(self, name, debug = False): |
|
1034 |
project = self.GetProject(debug) |
|
1035 |
if project is not None: |
|
1036 |
# Found the pou correponding to name and return its type |
|
1037 |
pou = project.getpou(name) |
|
1038 |
if pou is not None: |
|
1039 |
return pou.getpouType() |
|
1040 |
return None |
|
1041 |
||
1042 |
# Return pous with SFC language |
|
1043 |
def GetSFCPous(self, debug = False): |
|
1044 |
list = [] |
|
1045 |
project = self.GetProject(debug) |
|
1046 |
if project is not None: |
|
1047 |
for pou in project.getpous(): |
|
1048 |
if pou.getBodyType() == "SFC": |
|
1049 |
list.append(pou.getname()) |
|
1050 |
return list |
|
1051 |
||
1052 |
# Return the body language of the pou given by its name |
|
1053 |
def GetPouBodyType(self, name, debug = False): |
|
1054 |
project = self.GetProject(debug) |
|
1055 |
if project is not None: |
|
1056 |
# Found the pou correponding to name and return its body language |
|
1057 |
pou = project.getpou(name) |
|
1058 |
if pou is not None: |
|
1059 |
return pou.getbodyType() |
|
1060 |
return None |
|
1061 |
||
1062 |
# Return the actions of a pou |
|
1063 |
def GetPouTransitions(self, pou_name, debug = False): |
|
1064 |
transitions = [] |
|
1065 |
project = self.GetProject(debug) |
|
1066 |
if project is not None: |
|
1067 |
# Found the pou correponding to name and return its transitions if SFC |
|
1068 |
pou = project.getpou(pou_name) |
|
1069 |
if pou is not None and pou.getbodyType() == "SFC": |
|
1070 |
for transition in pou.gettransitionList(): |
|
1071 |
transitions.append(transition.getname()) |
|
1072 |
return transitions |
|
1073 |
||
1074 |
# Return the body language of the transition given by its name |
|
1075 |
def GetTransitionBodyType(self, pou_name, pou_transition, debug = False): |
|
1076 |
project = self.GetProject(debug) |
|
1077 |
if project is not None: |
|
1078 |
# Found the pou correponding to name |
|
1079 |
pou = project.getpou(pou_name) |
|
1080 |
if pou is not None: |
|
1081 |
# Found the pou transition correponding to name and return its body language |
|
1082 |
transition = pou.gettransition(pou_transition) |
|
1083 |
if transition is not None: |
|
1084 |
return transition.getbodyType() |
|
1085 |
return None |
|
1086 |
||
1087 |
# Return the actions of a pou |
|
1088 |
def GetPouActions(self, pou_name, debug = False): |
|
1089 |
actions = [] |
|
1090 |
project = self.GetProject(debug) |
|
1091 |
if project is not None: |
|
1092 |
# Found the pou correponding to name and return its actions if SFC |
|
1093 |
pou = project.getpou(pou_name) |
|
1094 |
if pou.getbodyType() == "SFC": |
|
1095 |
for action in pou.getactionList(): |
|
1096 |
actions.append(action.getname()) |
|
1097 |
return actions |
|
1098 |
||
1099 |
# Return the body language of the pou given by its name |
|
1100 |
def GetActionBodyType(self, pou_name, pou_action, debug = False): |
|
1101 |
project = self.GetProject(debug) |
|
1102 |
if project is not None: |
|
1103 |
# Found the pou correponding to name and return its body language |
|
1104 |
pou = project.getpou(pou_name) |
|
1105 |
if pou is not None: |
|
1106 |
action = pou.getaction(pou_action) |
|
1107 |
if action is not None: |
|
1108 |
return action.getbodyType() |
|
1109 |
return None |
|
1110 |
||
1111 |
# Extract varlists from a list of vars |
|
1112 |
def ExtractVarLists(self, vars): |
|
1113 |
varlist_list = [] |
|
1114 |
current_varlist = None |
|
1115 |
current_type = None |
|
1116 |
for var in vars: |
|
1117 |
next_type = (var["Class"], |
|
1118 |
var["Option"], |
|
1119 |
var["Location"] in ["", None] or |
|
1120 |
# When declaring globals, located |
|
1121 |
# and not located variables are |
|
1122 |
# in the same declaration block |
|
1123 |
var["Class"] == "Global") |
|
1124 |
if current_type != next_type: |
|
1125 |
current_type = next_type |
|
1126 |
infos = VAR_CLASS_INFOS.get(var["Class"], None) |
|
1127 |
if infos is not None: |
|
1128 |
current_varlist = infos[0]() |
|
1129 |
else: |
|
1130 |
current_varlist = plcopen.varList() |
|
1131 |
varlist_list.append((var["Class"], current_varlist)) |
|
1132 |
if var["Option"] == "Constant": |
|
1133 |
current_varlist.setconstant(True) |
|
1134 |
elif var["Option"] == "Retain": |
|
1135 |
current_varlist.setretain(True) |
|
1136 |
elif var["Option"] == "Non-Retain": |
|
1137 |
current_varlist.setnonretain(True) |
|
1138 |
# Create variable and change its properties |
|
1139 |
tempvar = plcopen.varListPlain_variable() |
|
1140 |
tempvar.setname(var["Name"]) |
|
1141 |
||
1142 |
var_type = plcopen.dataType() |
|
1143 |
if isinstance(var["Type"], TupleType): |
|
1144 |
if var["Type"][0] == "array": |
|
1145 |
array_type, base_type_name, dimensions = var["Type"] |
|
1146 |
array = plcopen.derivedTypes_array() |
|
1147 |
for i, dimension in enumerate(dimensions): |
|
1148 |
dimension_range = plcopen.rangeSigned() |
|
1149 |
dimension_range.setlower(dimension[0]) |
|
1150 |
dimension_range.setupper(dimension[1]) |
|
1151 |
if i == 0: |
|
1152 |
array.setdimension([dimension_range]) |
|
1153 |
else: |
|
1154 |
array.appenddimension(dimension_range) |
|
1155 |
if base_type_name in self.GetBaseTypes(): |
|
1156 |
if base_type_name == "STRING": |
|
1157 |
array.baseType.setcontent({"name" : "string", "value" : plcopen.elementaryTypes_string()}) |
|
1158 |
elif base_type_name == "WSTRING": |
|
1159 |
array.baseType.setcontent({"name" : "wstring", "value" : plcopen.wstring()}) |
|
1160 |
else: |
|
1161 |
array.baseType.setcontent({"name" : base_type_name, "value" : None}) |
|
1162 |
else: |
|
1163 |
derived_datatype = plcopen.derivedTypes_derived() |
|
1164 |
derived_datatype.setname(base_type_name) |
|
1165 |
array.baseType.setcontent({"name" : "derived", "value" : derived_datatype}) |
|
1166 |
var_type.setcontent({"name" : "array", "value" : array}) |
|
1167 |
elif var["Type"] in self.GetBaseTypes(): |
|
1168 |
if var["Type"] == "STRING": |
|
1169 |
var_type.setcontent({"name" : "string", "value" : plcopen.elementaryTypes_string()}) |
|
1170 |
elif var["Type"] == "WSTRING": |
|
1171 |
var_type.setcontent({"name" : "wstring", "value" : plcopen.elementaryTypes_wstring()}) |
|
1172 |
else: |
|
1173 |
var_type.setcontent({"name" : var["Type"], "value" : None}) |
|
1174 |
else: |
|
1175 |
derived_type = plcopen.derivedTypes_derived() |
|
1176 |
derived_type.setname(var["Type"]) |
|
1177 |
var_type.setcontent({"name" : "derived", "value" : derived_type}) |
|
1178 |
tempvar.settype(var_type) |
|
1179 |
||
1180 |
if var["Initial Value"] != "": |
|
1181 |
value = plcopen.value() |
|
1182 |
value.setvalue(var["Initial Value"]) |
|
1183 |
tempvar.setinitialValue(value) |
|
1184 |
if var["Location"] != "": |
|
1185 |
tempvar.setaddress(var["Location"]) |
|
1186 |
else: |
|
1187 |
tempvar.setaddress(None) |
|
1188 |
if var['Documentation'] != "": |
|
1189 |
ft = plcopen.formattedText() |
|
1190 |
ft.settext(var['Documentation']) |
|
1191 |
tempvar.setdocumentation(ft) |
|
1192 |
||
1193 |
# Add variable to varList |
|
1194 |
current_varlist.appendvariable(tempvar) |
|
1195 |
return varlist_list |
|
1196 |
||
1197 |
def GetVariableDictionary(self, varlist, var): |
|
1198 |
''' |
|
1199 |
convert a PLC variable to the dictionary representation |
|
1200 |
returned by Get*Vars) |
|
1201 |
''' |
|
1202 |
||
1203 |
tempvar = {"Name": var.getname()} |
|
1204 |
||
1205 |
vartype_content = var.gettype().getcontent() |
|
1206 |
if vartype_content["name"] == "derived": |
|
1207 |
tempvar["Type"] = vartype_content["value"].getname() |
|
1208 |
elif vartype_content["name"] == "array": |
|
1209 |
dimensions = [] |
|
1210 |
for dimension in vartype_content["value"].getdimension(): |
|
1211 |
dimensions.append((dimension.getlower(), dimension.getupper())) |
|
1212 |
base_type = vartype_content["value"].baseType.getcontent() |
|
1213 |
if base_type["value"] is None: |
|
1214 |
base_type_name = base_type["name"] |
|
1215 |
else: |
|
1216 |
base_type_name = base_type["value"].getname() |
|
1217 |
tempvar["Type"] = ("array", base_type_name, dimensions) |
|
1218 |
elif vartype_content["name"] in ["string", "wstring"]: |
|
1219 |
tempvar["Type"] = vartype_content["name"].upper() |
|
1220 |
else: |
|
1221 |
tempvar["Type"] = vartype_content["name"] |
|
1222 |
||
1223 |
tempvar["Edit"] = True |
|
1224 |
||
1225 |
initial = var.getinitialValue() |
|
1226 |
if initial: |
|
1227 |
tempvar["Initial Value"] = initial.getvalue() |
|
1228 |
else: |
|
1229 |
tempvar["Initial Value"] = "" |
|
1230 |
||
1231 |
address = var.getaddress() |
|
1232 |
if address: |
|
1233 |
tempvar["Location"] = address |
|
1234 |
else: |
|
1235 |
tempvar["Location"] = "" |
|
1236 |
||
1237 |
if varlist.getconstant(): |
|
1238 |
tempvar["Option"] = "Constant" |
|
1239 |
elif varlist.getretain(): |
|
1240 |
tempvar["Option"] = "Retain" |
|
1241 |
elif varlist.getnonretain(): |
|
1242 |
tempvar["Option"] = "Non-Retain" |
|
1243 |
else: |
|
1244 |
tempvar["Option"] = "" |
|
1245 |
||
1246 |
doc = var.getdocumentation() |
|
1247 |
if doc: |
|
1248 |
tempvar["Documentation"] = doc.gettext() |
|
1249 |
else: |
|
1250 |
tempvar["Documentation"] = "" |
|
1251 |
||
1252 |
return tempvar |
|
1253 |
||
1254 |
# Replace the configuration globalvars by those given |
|
1255 |
def SetConfigurationGlobalVars(self, name, vars): |
|
1256 |
if self.Project is not None: |
|
1257 |
# Found the configuration corresponding to name |
|
1258 |
configuration = self.Project.getconfiguration(name) |
|
1259 |
if configuration is not None: |
|
1260 |
# Set configuration global vars |
|
1261 |
configuration.setglobalVars([]) |
|
1262 |
for vartype, varlist in self.ExtractVarLists(vars): |
|
1263 |
configuration.globalVars.append(varlist) |
|
1264 |
||
1265 |
# Return the configuration globalvars |
|
1266 |
def GetConfigurationGlobalVars(self, name, debug = False): |
|
1267 |
vars = [] |
|
1268 |
project = self.GetProject(debug) |
|
1269 |
if project is not None: |
|
1270 |
# Found the configuration corresponding to name |
|
1271 |
configuration = project.getconfiguration(name) |
|
1272 |
if configuration is not None: |
|
1273 |
# Extract variables from every varLists |
|
1274 |
for varlist in configuration.getglobalVars(): |
|
1275 |
for var in varlist.getvariable(): |
|
1276 |
tempvar = self.GetVariableDictionary(varlist, var) |
|
1277 |
tempvar["Class"] = "Global" |
|
1278 |
vars.append(tempvar) |
|
1279 |
return vars |
|
1280 |
||
1281 |
# Replace the resource globalvars by those given |
|
1282 |
def SetConfigurationResourceGlobalVars(self, config_name, name, vars): |
|
1283 |
if self.Project is not None: |
|
1284 |
# Found the resource corresponding to name |
|
1285 |
resource = self.Project.getconfigurationResource(config_name, name) |
|
1286 |
# Set resource global vars |
|
1287 |
if resource is not None: |
|
1288 |
resource.setglobalVars([]) |
|
1289 |
for vartype, varlist in self.ExtractVarLists(vars): |
|
1290 |
resource.globalVars.append(varlist) |
|
1291 |
||
1292 |
# Return the resource globalvars |
|
1293 |
def GetConfigurationResourceGlobalVars(self, config_name, name, debug = False): |
|
1294 |
vars = [] |
|
1295 |
project = self.GetProject(debug) |
|
1296 |
if project is not None: |
|
1297 |
# Found the resource corresponding to name |
|
1298 |
resource = project.getconfigurationResource(config_name, name) |
|
1299 |
if resource: |
|
1300 |
# Extract variables from every varLists |
|
1301 |
for varlist in resource.getglobalVars(): |
|
1302 |
for var in varlist.getvariable(): |
|
1303 |
tempvar = self.GetVariableDictionary(varlist, var) |
|
1304 |
tempvar["Class"] = "Global" |
|
1305 |
vars.append(tempvar) |
|
1306 |
return vars |
|
1307 |
||
1308 |
# Recursively generate element name tree for a structured variable |
|
1309 |
def GenerateVarTree(self, typename, debug = False): |
|
1310 |
project = self.GetProject(debug) |
|
1311 |
if project is not None: |
|
1312 |
blocktype = self.GetBlockType(typename, debug = debug) |
|
1313 |
if blocktype is not None: |
|
1314 |
tree = [] |
|
1315 |
en = False |
|
1316 |
eno = False |
|
1317 |
for var_name, var_type, var_modifier in blocktype["inputs"] + blocktype["outputs"]: |
|
1318 |
en |= var_name.upper() == "EN" |
|
1319 |
eno |= var_name.upper() == "ENO" |
|
1320 |
tree.append((var_name, var_type, self.GenerateVarTree(var_type, debug))) |
|
1321 |
if not eno: |
|
1322 |
tree.insert(0, ("ENO", "BOOL", ([], []))) |
|
1323 |
if not en: |
|
1324 |
tree.insert(0, ("EN", "BOOL", ([], []))) |
|
1325 |
return tree, [] |
|
1326 |
datatype = project.getdataType(typename) |
|
1327 |
if datatype is None: |
|
1328 |
datatype = self.GetConfNodeDataType(typename) |
|
1329 |
if datatype is not None: |
|
1330 |
tree = [] |
|
1331 |
basetype_content = datatype.baseType.getcontent() |
|
1332 |
if basetype_content["name"] == "derived": |
|
1333 |
return self.GenerateVarTree(basetype_content["value"].getname()) |
|
1334 |
elif basetype_content["name"] == "array": |
|
1335 |
dimensions = [] |
|
1336 |
base_type = basetype_content["value"].baseType.getcontent() |
|
1337 |
if base_type["name"] == "derived": |
|
1338 |
tree = self.GenerateVarTree(base_type["value"].getname()) |
|
1339 |
if len(tree[1]) == 0: |
|
1340 |
tree = tree[0] |
|
1341 |
for dimension in basetype_content["value"].getdimension(): |
|
1342 |
dimensions.append((dimension.getlower(), dimension.getupper())) |
|
1343 |
return tree, dimensions |
|
1344 |
elif basetype_content["name"] == "struct": |
|
1345 |
for element in basetype_content["value"].getvariable(): |
|
1346 |
element_type = element.type.getcontent() |
|
1347 |
if element_type["name"] == "derived": |
|
1348 |
tree.append((element.getname(), element_type["value"].getname(), self.GenerateVarTree(element_type["value"].getname()))) |
|
1349 |
else: |
|
1350 |
tree.append((element.getname(), element_type["name"], ([], []))) |
|
1351 |
return tree, [] |
|
1352 |
return [], [] |
|
1353 |
||
1354 |
# Return the interface for the given pou |
|
1355 |
def GetPouInterfaceVars(self, pou, debug = False): |
|
1356 |
vars = [] |
|
1357 |
# Verify that the pou has an interface |
|
1358 |
if pou.interface is not None: |
|
1359 |
# Extract variables from every varLists |
|
1360 |
for type, varlist in pou.getvars(): |
|
1361 |
for var in varlist.getvariable(): |
|
1362 |
tempvar = self.GetVariableDictionary(varlist, var) |
|
1363 |
||
1364 |
tempvar["Class"] = type |
|
1365 |
tempvar["Tree"] = ([], []) |
|
1366 |
||
1367 |
vartype_content = var.gettype().getcontent() |
|
1368 |
if vartype_content["name"] == "derived": |
|
1369 |
tempvar["Edit"] = not pou.hasblock(tempvar["Name"]) |
|
1370 |
tempvar["Tree"] = self.GenerateVarTree(tempvar["Type"], debug) |
|
1371 |
||
1372 |
vars.append(tempvar) |
|
1373 |
return vars |
|
1374 |
||
1375 |
# Replace the Pou interface by the one given |
|
1376 |
def SetPouInterfaceVars(self, name, vars): |
|
1377 |
if self.Project is not None: |
|
1378 |
# Found the pou corresponding to name and add interface if there isn't one yet |
|
1379 |
pou = self.Project.getpou(name) |
|
1380 |
if pou is not None: |
|
1381 |
if pou.interface is None: |
|
1382 |
pou.interface = plcopen.pou_interface() |
|
1383 |
# Set Pou interface |
|
1384 |
pou.setvars(self.ExtractVarLists(vars)) |
|
1385 |
self.Project.RefreshElementUsingTree() |
|
1386 |
self.Project.RefreshCustomBlockTypes() |
|
1387 |
||
1388 |
# Replace the return type of the pou given by its name (only for functions) |
|
1389 |
def SetPouInterfaceReturnType(self, name, type): |
|
1390 |
if self.Project is not None: |
|
1391 |
pou = self.Project.getpou(name) |
|
1392 |
if pou is not None: |
|
1393 |
if pou.interface is None: |
|
1394 |
pou.interface = plcopen.pou_interface() |
|
1395 |
# If there isn't any return type yet, add it |
|
1396 |
return_type = pou.interface.getreturnType() |
|
1397 |
if not return_type: |
|
1398 |
return_type = plcopen.dataType() |
|
1399 |
pou.interface.setreturnType(return_type) |
|
1400 |
# Change return type |
|
1401 |
if type in self.GetBaseTypes(): |
|
1402 |
if type == "STRING": |
|
1403 |
return_type.setcontent({"name" : "string", "value" : plcopen.elementaryTypes_string()}) |
|
1404 |
elif type == "WSTRING": |
|
1405 |
return_type.setcontent({"name" : "wstring", "value" : plcopen.elementaryTypes_wstring()}) |
|
1406 |
else: |
|
1407 |
return_type.setcontent({"name" : type, "value" : None}) |
|
1408 |
else: |
|
1409 |
derived_type = plcopen.derivedTypes_derived() |
|
1410 |
derived_type.setname(type) |
|
1411 |
return_type.setcontent({"name" : "derived", "value" : derived_type}) |
|
1412 |
self.Project.RefreshElementUsingTree() |
|
1413 |
self.Project.RefreshCustomBlockTypes() |
|
1414 |
||
1415 |
def UpdateProjectUsedPous(self, old_name, new_name): |
|
1416 |
if self.Project: |
|
1417 |
self.Project.updateElementName(old_name, new_name) |
|
1418 |
||
1419 |
def UpdateEditedElementUsedVariable(self, tagname, old_name, new_name): |
|
1420 |
pou = self.GetEditedElement(tagname) |
|
1421 |
if pou: |
|
1422 |
pou.updateElementName(old_name, new_name) |
|
1423 |
||
1424 |
# Return the return type of the pou given by its name |
|
1425 |
def GetPouInterfaceReturnTypeByName(self, name): |
|
1426 |
project = self.GetProject(debug) |
|
1427 |
if project is not None: |
|
1428 |
# Found the pou correponding to name and return the return type |
|
1429 |
pou = project.getpou(name) |
|
1430 |
if pou is not None: |
|
1431 |
return self.GetPouInterfaceReturnType(pou) |
|
1432 |
return False |
|
1433 |
||
1434 |
# Return the return type of the given pou |
|
1435 |
def GetPouInterfaceReturnType(self, pou): |
|
1436 |
# Verify that the pou has an interface |
|
1437 |
if pou.interface is not None: |
|
1438 |
# Return the return type if there is one |
|
1439 |
return_type = pou.interface.getreturnType() |
|
1440 |
if return_type: |
|
1441 |
returntype_content = return_type.getcontent() |
|
1442 |
if returntype_content["name"] == "derived": |
|
1443 |
return returntype_content["value"].getname() |
|
1444 |
elif returntype_content["name"] in ["string", "wstring"]: |
|
1445 |
return returntype_content["name"].upper() |
|
1446 |
else: |
|
1447 |
return returntype_content["name"] |
|
1448 |
return None |
|
1449 |
||
1450 |
# Function that add a new confnode to the confnode list |
|
1451 |
def AddConfNodeTypesList(self, typeslist): |
|
1452 |
self.ConfNodeTypes.extend(typeslist) |
|
1453 |
||
1454 |
# Function that clear the confnode list |
|
1455 |
def ClearConfNodeTypes(self): |
|
1456 |
for i in xrange(len(self.ConfNodeTypes)): |
|
1457 |
self.ConfNodeTypes.pop(0) |
|
1458 |
||
1459 |
def GetConfNodeBlockTypes(self): |
|
1460 |
return [{"name": _("%s POUs") % confnodetypes["name"], |
|
1461 |
"list": confnodetypes["types"].GetCustomBlockTypes()} |
|
1462 |
for confnodetypes in self.ConfNodeTypes] |
|
1463 |
||
1464 |
def GetConfNodeDataTypes(self, exclude = ""): |
|
1465 |
return [{"name": _("%s Data Types") % confnodetypes["name"], |
|
1466 |
"list": [datatype["name"] for datatype in confnodetypes["types"].GetCustomDataTypes(exclude)]} |
|
1467 |
for confnodetypes in self.ConfNodeTypes] |
|
1468 |
||
1469 |
def GetConfNodeDataType(self, type): |
|
1470 |
for confnodetype in self.ConfNodeTypes: |
|
1471 |
datatype = confnodetype["types"].getdataType(type) |
|
1472 |
if datatype is not None: |
|
1473 |
return datatype |
|
1474 |
return None |
|
1475 |
||
1476 |
def GetVariableLocationTree(self): |
|
1477 |
return [] |
|
1478 |
||
1479 |
# Function that returns the block definition associated to the block type given |
|
1480 |
def GetBlockType(self, type, inputs = None, debug = False): |
|
1481 |
result_blocktype = None |
|
1482 |
for category in BlockTypes + self.GetConfNodeBlockTypes(): |
|
1483 |
for blocktype in category["list"]: |
|
1484 |
if blocktype["name"] == type: |
|
1485 |
if inputs is not None and inputs != "undefined": |
|
1486 |
block_inputs = tuple([var_type for name, var_type, modifier in blocktype["inputs"]]) |
|
1487 |
if reduce(lambda x, y: x and y, map(lambda x: x[0] == "ANY" or self.IsOfType(*x), zip(inputs, block_inputs)), True): |
|
1488 |
return blocktype |
|
1489 |
else: |
|
1490 |
if result_blocktype is not None: |
|
1491 |
if inputs == "undefined": |
|
1492 |
return None |
|
1493 |
else: |
|
1494 |
result_blocktype["inputs"] = [(i[0], "ANY", i[2]) for i in result_blocktype["inputs"]] |
|
1495 |
result_blocktype["outputs"] = [(o[0], "ANY", o[2]) for o in result_blocktype["outputs"]] |
|
1496 |
return result_blocktype |
|
1497 |
result_blocktype = blocktype |
|
1498 |
if result_blocktype is not None: |
|
1499 |
return result_blocktype |
|
1500 |
project = self.GetProject(debug) |
|
1501 |
if project is not None: |
|
1502 |
return project.GetCustomBlockType(type, inputs) |
|
1503 |
return None |
|
1504 |
||
1505 |
# Return Block types checking for recursion |
|
1506 |
def GetBlockTypes(self, tagname = "", debug = False): |
|
1507 |
type = None |
|
1508 |
words = tagname.split("::") |
|
1509 |
if self.Project: |
|
1510 |
name = "" |
|
1511 |
if words[0] in ["P","T","A"]: |
|
1512 |
name = words[1] |
|
1513 |
type = self.GetPouType(name, debug) |
|
823
c6ed7b933617
Remove restriction of function block usage in SFC transitions body
laurent
parents:
814
diff
changeset
|
1514 |
if type == "function": |
814 | 1515 |
blocktypes = [] |
1516 |
for category in BlockTypes + self.GetConfNodeBlockTypes(): |
|
1517 |
cat = {"name" : category["name"], "list" : []} |
|
1518 |
for block in category["list"]: |
|
1519 |
if block["type"] == "function": |
|
1520 |
cat["list"].append(block) |
|
1521 |
if len(cat["list"]) > 0: |
|
1522 |
blocktypes.append(cat) |
|
1523 |
else: |
|
1524 |
blocktypes = [category for category in BlockTypes + self.GetConfNodeBlockTypes()] |
|
1525 |
project = self.GetProject(debug) |
|
1526 |
if project is not None: |
|
1527 |
blocktypes.append({"name" : USER_DEFINED_POUS, "list": project.GetCustomBlockTypes(name, type == "function" or words[0] == "T")}) |
|
1528 |
return blocktypes |
|
1529 |
||
1530 |
# Return Function Block types checking for recursion |
|
1531 |
def GetFunctionBlockTypes(self, tagname = "", debug = False): |
|
1532 |
blocktypes = [] |
|
1533 |
for category in BlockTypes + self.GetConfNodeBlockTypes(): |
|
1534 |
for block in category["list"]: |
|
1535 |
if block["type"] == "functionBlock": |
|
1536 |
blocktypes.append(block["name"]) |
|
1537 |
project = self.GetProject(debug) |
|
1538 |
if project is not None: |
|
1539 |
name = "" |
|
1540 |
words = tagname.split("::") |
|
1541 |
if words[0] in ["P","T","A"]: |
|
1542 |
name = words[1] |
|
1543 |
blocktypes.extend(project.GetCustomFunctionBlockTypes(name)) |
|
1544 |
return blocktypes |
|
1545 |
||
1546 |
# Return Block types checking for recursion |
|
1547 |
def GetBlockResource(self, debug = False): |
|
1548 |
blocktypes = [] |
|
1549 |
for category in BlockTypes[:-1]: |
|
1550 |
for blocktype in category["list"]: |
|
1551 |
if blocktype["type"] == "program": |
|
1552 |
blocktypes.append(blocktype["name"]) |
|
1553 |
project = self.GetProject(debug) |
|
1554 |
if project is not None: |
|
1555 |
blocktypes.extend(project.GetCustomBlockResource()) |
|
1556 |
return blocktypes |
|
1557 |
||
1558 |
# Return Data Types checking for recursion |
|
1559 |
def GetDataTypes(self, tagname = "", basetypes = True, only_locatables = False, debug = False): |
|
1560 |
if basetypes: |
|
1561 |
datatypes = self.GetBaseTypes() |
|
1562 |
else: |
|
1563 |
datatypes = [] |
|
1564 |
project = self.GetProject(debug) |
|
1565 |
if project is not None: |
|
1566 |
name = "" |
|
1567 |
words = tagname.split("::") |
|
1568 |
if words[0] in ["D"]: |
|
1569 |
name = words[1] |
|
1570 |
datatypes.extend([datatype["name"] for datatype in project.GetCustomDataTypes(name, only_locatables)]) |
|
853
0f97bddb5a30
Adding datatypes defined in ConfNode as possible function return type
Laurent Bessard
parents:
827
diff
changeset
|
1571 |
for category in self.GetConfNodeDataTypes(): |
0f97bddb5a30
Adding datatypes defined in ConfNode as possible function return type
Laurent Bessard
parents:
827
diff
changeset
|
1572 |
datatypes.extend(category["list"]) |
814 | 1573 |
return datatypes |
1574 |
||
1575 |
# Return Base Type of given possible derived type |
|
1576 |
def GetBaseType(self, type, debug = False): |
|
1577 |
project = self.GetProject(debug) |
|
1578 |
if project is not None: |
|
1579 |
result = project.GetBaseType(type) |
|
1580 |
if result is not None: |
|
1581 |
return result |
|
1582 |
for confnodetype in self.ConfNodeTypes: |
|
1583 |
result = confnodetype["types"].GetBaseType(type) |
|
1584 |
if result is not None: |
|
1585 |
return result |
|
1586 |
return None |
|
1587 |
||
1588 |
def GetBaseTypes(self): |
|
1589 |
''' |
|
1590 |
return the list of datatypes defined in IEC 61131-3. |
|
1591 |
TypeHierarchy_list has a rough order to it (e.g. SINT, INT, DINT, ...), |
|
1592 |
which makes it easy for a user to find a type in a menu. |
|
1593 |
''' |
|
1594 |
return [x for x,y in TypeHierarchy_list if not x.startswith("ANY")] |
|
1595 |
||
1596 |
def IsOfType(self, type, reference, debug = False): |
|
1597 |
if reference is None: |
|
1598 |
return True |
|
1599 |
elif type == reference: |
|
1600 |
return True |
|
1601 |
elif type in TypeHierarchy: |
|
1602 |
return self.IsOfType(TypeHierarchy[type], reference) |
|
1603 |
else: |
|
1604 |
project = self.GetProject(debug) |
|
1605 |
if project is not None and project.IsOfType(type, reference): |
|
1606 |
return True |
|
1607 |
for confnodetype in self.ConfNodeTypes: |
|
1608 |
if confnodetype["types"].IsOfType(type, reference): |
|
1609 |
return True |
|
1610 |
return False |
|
1611 |
||
1612 |
def IsEndType(self, type): |
|
1613 |
if type is not None: |
|
1614 |
return not type.startswith("ANY") |
|
1615 |
return True |
|
1616 |
||
1617 |
def IsLocatableType(self, type, debug = False): |
|
1618 |
if isinstance(type, TupleType): |
|
1619 |
return False |
|
1620 |
project = self.GetProject(debug) |
|
1621 |
if project is not None: |
|
1622 |
datatype = project.getdataType(type) |
|
1623 |
if datatype is None: |
|
1624 |
datatype = self.GetConfNodeDataType(type) |
|
1625 |
if datatype is not None: |
|
1626 |
return project.IsLocatableType(datatype) |
|
1627 |
return True |
|
1628 |
||
1629 |
def IsEnumeratedType(self, type, debug = False): |
|
1630 |
project = self.GetProject(debug) |
|
1631 |
if project is not None: |
|
1632 |
datatype = project.getdataType(type) |
|
1633 |
if datatype is None: |
|
1634 |
datatype = self.GetConfNodeDataType(type) |
|
1635 |
if datatype is not None: |
|
1636 |
basetype_content = datatype.baseType.getcontent() |
|
1637 |
return basetype_content["name"] == "enum" |
|
1638 |
return False |
|
1639 |
||
1640 |
def GetDataTypeRange(self, type, debug = False): |
|
1641 |
if type in DataTypeRange: |
|
1642 |
return DataTypeRange[type] |
|
1643 |
else: |
|
1644 |
project = self.GetProject(debug) |
|
1645 |
if project is not None: |
|
1646 |
result = project.GetDataTypeRange(type) |
|
1647 |
if result is not None: |
|
1648 |
return result |
|
1649 |
for confnodetype in self.ConfNodeTypes: |
|
1650 |
result = confnodetype["types"].GetDataTypeRange(type) |
|
1651 |
if result is not None: |
|
1652 |
return result |
|
1653 |
return None |
|
1654 |
||
1655 |
# Return Subrange types |
|
1656 |
def GetSubrangeBaseTypes(self, exclude, debug = False): |
|
1657 |
subrange_basetypes = [] |
|
1658 |
project = self.GetProject(debug) |
|
1659 |
if project is not None: |
|
1660 |
subrange_basetypes.extend(project.GetSubrangeBaseTypes(exclude)) |
|
1661 |
for confnodetype in self.ConfNodeTypes: |
|
1662 |
subrange_basetypes.extend(confnodetype["types"].GetSubrangeBaseTypes(exclude)) |
|
1663 |
return DataTypeRange.keys() + subrange_basetypes |
|
1664 |
||
1665 |
# Return Enumerated Values |
|
1666 |
def GetEnumeratedDataValues(self, type = None, debug = False): |
|
1667 |
values = [] |
|
1668 |
project = self.GetProject(debug) |
|
1669 |
if project is not None: |
|
1670 |
values.extend(project.GetEnumeratedDataTypeValues(type)) |
|
1671 |
if type is None and len(values) > 0: |
|
1672 |
return values |
|
1673 |
for confnodetype in self.ConfNodeTypes: |
|
1674 |
values.extend(confnodetype["types"].GetEnumeratedDataTypeValues(type)) |
|
1675 |
if type is None and len(values) > 0: |
|
1676 |
return values |
|
1677 |
return values |
|
1678 |
||
1679 |
#------------------------------------------------------------------------------- |
|
1680 |
# Project Element tag name computation functions |
|
1681 |
#------------------------------------------------------------------------------- |
|
1682 |
||
1683 |
# Compute a data type name |
|
1684 |
def ComputeDataTypeName(self, datatype): |
|
1685 |
return "D::%s" % datatype |
|
1686 |
||
1687 |
# Compute a pou name |
|
1688 |
def ComputePouName(self, pou): |
|
1689 |
return "P::%s" % pou |
|
1690 |
||
1691 |
# Compute a pou transition name |
|
1692 |
def ComputePouTransitionName(self, pou, transition): |
|
1693 |
return "T::%s::%s" % (pou, transition) |
|
1694 |
||
1695 |
# Compute a pou action name |
|
1696 |
def ComputePouActionName(self, pou, action): |
|
1697 |
return "A::%s::%s" % (pou, action) |
|
1698 |
||
1699 |
# Compute a pou name |
|
1700 |
def ComputeConfigurationName(self, config): |
|
1701 |
return "C::%s" % config |
|
1702 |
||
1703 |
# Compute a pou name |
|
1704 |
def ComputeConfigurationResourceName(self, config, resource): |
|
1705 |
return "R::%s::%s" % (config, resource) |
|
1706 |
||
1707 |
def GetElementType(self, tagname): |
|
1708 |
words = tagname.split("::") |
|
1709 |
return {"D" : ITEM_DATATYPE, "P" : ITEM_POU, |
|
1710 |
"T" : ITEM_TRANSITION, "A" : ITEM_ACTION, |
|
1711 |
"C" : ITEM_CONFIGURATION, "R" : ITEM_RESOURCE}[words[0]] |
|
1712 |
||
1713 |
#------------------------------------------------------------------------------- |
|
1714 |
# Project opened Data types management functions |
|
1715 |
#------------------------------------------------------------------------------- |
|
1716 |
||
1717 |
# Return the data type informations |
|
1718 |
def GetDataTypeInfos(self, tagname, debug = False): |
|
1719 |
project = self.GetProject(debug) |
|
1720 |
if project is not None: |
|
1721 |
words = tagname.split("::") |
|
1722 |
if words[0] == "D": |
|
1723 |
infos = {} |
|
1724 |
datatype = project.getdataType(words[1]) |
|
1725 |
basetype_content = datatype.baseType.getcontent() |
|
1726 |
if basetype_content["value"] is None or basetype_content["name"] in ["string", "wstring"]: |
|
1727 |
infos["type"] = "Directly" |
|
1728 |
infos["base_type"] = basetype_content["name"].upper() |
|
1729 |
elif basetype_content["name"] == "derived": |
|
1730 |
infos["type"] = "Directly" |
|
1731 |
infos["base_type"] = basetype_content["value"].getname() |
|
1732 |
elif basetype_content["name"] in ["subrangeSigned", "subrangeUnsigned"]: |
|
1733 |
infos["type"] = "Subrange" |
|
1734 |
infos["min"] = basetype_content["value"].range.getlower() |
|
1735 |
infos["max"] = basetype_content["value"].range.getupper() |
|
1736 |
base_type = basetype_content["value"].baseType.getcontent() |
|
1737 |
if base_type["value"] is None: |
|
1738 |
infos["base_type"] = base_type["name"] |
|
1739 |
else: |
|
1740 |
infos["base_type"] = base_type["value"].getname() |
|
1741 |
elif basetype_content["name"] == "enum": |
|
1742 |
infos["type"] = "Enumerated" |
|
1743 |
infos["values"] = [] |
|
1744 |
for value in basetype_content["value"].values.getvalue(): |
|
1745 |
infos["values"].append(value.getname()) |
|
1746 |
elif basetype_content["name"] == "array": |
|
1747 |
infos["type"] = "Array" |
|
1748 |
infos["dimensions"] = [] |
|
1749 |
for dimension in basetype_content["value"].getdimension(): |
|
1750 |
infos["dimensions"].append((dimension.getlower(), dimension.getupper())) |
|
1751 |
base_type = basetype_content["value"].baseType.getcontent() |
|
1752 |
if base_type["value"] is None or base_type["name"] in ["string", "wstring"]: |
|
1753 |
infos["base_type"] = base_type["name"].upper() |
|
1754 |
else: |
|
1755 |
infos["base_type"] = base_type["value"].getname() |
|
1756 |
elif basetype_content["name"] == "struct": |
|
1757 |
infos["type"] = "Structure" |
|
1758 |
infos["elements"] = [] |
|
1759 |
for element in basetype_content["value"].getvariable(): |
|
1760 |
element_infos = {} |
|
1761 |
element_infos["Name"] = element.getname() |
|
1762 |
element_type = element.type.getcontent() |
|
1763 |
if element_type["value"] is None or element_type["name"] in ["string", "wstring"]: |
|
1764 |
element_infos["Type"] = element_type["name"].upper() |
|
1765 |
else: |
|
1766 |
element_infos["Type"] = element_type["value"].getname() |
|
1767 |
if element.initialValue is not None: |
|
1768 |
element_infos["Initial Value"] = str(element.initialValue.getvalue()) |
|
1769 |
else: |
|
1770 |
element_infos["Initial Value"] = "" |
|
1771 |
infos["elements"].append(element_infos) |
|
1772 |
if datatype.initialValue is not None: |
|
1773 |
infos["initial"] = str(datatype.initialValue.getvalue()) |
|
1774 |
else: |
|
1775 |
infos["initial"] = "" |
|
1776 |
return infos |
|
1777 |
return None |
|
1778 |
||
1779 |
# Change the data type informations |
|
1780 |
def SetDataTypeInfos(self, tagname, infos): |
|
1781 |
words = tagname.split("::") |
|
1782 |
if self.Project is not None and words[0] == "D": |
|
1783 |
datatype = self.Project.getdataType(words[1]) |
|
1784 |
if infos["type"] == "Directly": |
|
1785 |
if infos["base_type"] in self.GetBaseTypes(): |
|
1786 |
if infos["base_type"] == "STRING": |
|
1787 |
datatype.baseType.setcontent({"name" : "string", "value" : plcopen.elementaryTypes_string()}) |
|
1788 |
elif infos["base_type"] == "WSTRING": |
|
1789 |
datatype.baseType.setcontent({"name" : "wstring", "value" : plcopen.elementaryTypes_wstring()}) |
|
1790 |
else: |
|
1791 |
datatype.baseType.setcontent({"name" : infos["base_type"], "value" : None}) |
|
1792 |
else: |
|
1793 |
derived_datatype = plcopen.derivedTypes_derived() |
|
1794 |
derived_datatype.setname(infos["base_type"]) |
|
1795 |
datatype.baseType.setcontent({"name" : "derived", "value" : derived_datatype}) |
|
1796 |
elif infos["type"] == "Subrange": |
|
1797 |
if infos["base_type"] in GetSubTypes("ANY_UINT"): |
|
1798 |
subrange = plcopen.derivedTypes_subrangeUnsigned() |
|
1799 |
datatype.baseType.setcontent({"name" : "subrangeUnsigned", "value" : subrange}) |
|
1800 |
else: |
|
1801 |
subrange = plcopen.derivedTypes_subrangeSigned() |
|
1802 |
datatype.baseType.setcontent({"name" : "subrangeSigned", "value" : subrange}) |
|
1803 |
subrange.range.setlower(infos["min"]) |
|
1804 |
subrange.range.setupper(infos["max"]) |
|
1805 |
if infos["base_type"] in self.GetBaseTypes(): |
|
1806 |
subrange.baseType.setcontent({"name" : infos["base_type"], "value" : None}) |
|
1807 |
else: |
|
1808 |
derived_datatype = plcopen.derivedTypes_derived() |
|
1809 |
derived_datatype.setname(infos["base_type"]) |
|
1810 |
subrange.baseType.setcontent({"name" : "derived", "value" : derived_datatype}) |
|
1811 |
elif infos["type"] == "Enumerated": |
|
1812 |
enumerated = plcopen.derivedTypes_enum() |
|
1813 |
for i, enum_value in enumerate(infos["values"]): |
|
1814 |
value = plcopen.values_value() |
|
1815 |
value.setname(enum_value) |
|
1816 |
if i == 0: |
|
1817 |
enumerated.values.setvalue([value]) |
|
1818 |
else: |
|
1819 |
enumerated.values.appendvalue(value) |
|
1820 |
datatype.baseType.setcontent({"name" : "enum", "value" : enumerated}) |
|
1821 |
elif infos["type"] == "Array": |
|
1822 |
array = plcopen.derivedTypes_array() |
|
1823 |
for i, dimension in enumerate(infos["dimensions"]): |
|
1824 |
dimension_range = plcopen.rangeSigned() |
|
1825 |
dimension_range.setlower(dimension[0]) |
|
1826 |
dimension_range.setupper(dimension[1]) |
|
1827 |
if i == 0: |
|
1828 |
array.setdimension([dimension_range]) |
|
1829 |
else: |
|
1830 |
array.appenddimension(dimension_range) |
|
1831 |
if infos["base_type"] in self.GetBaseTypes(): |
|
1832 |
if infos["base_type"] == "STRING": |
|
1833 |
array.baseType.setcontent({"name" : "string", "value" : plcopen.elementaryTypes_string()}) |
|
1834 |
elif infos["base_type"] == "WSTRING": |
|
1835 |
array.baseType.setcontent({"name" : "wstring", "value" : plcopen.wstring()}) |
|
1836 |
else: |
|
1837 |
array.baseType.setcontent({"name" : infos["base_type"], "value" : None}) |
|
1838 |
else: |
|
1839 |
derived_datatype = plcopen.derivedTypes_derived() |
|
1840 |
derived_datatype.setname(infos["base_type"]) |
|
1841 |
array.baseType.setcontent({"name" : "derived", "value" : derived_datatype}) |
|
1842 |
datatype.baseType.setcontent({"name" : "array", "value" : array}) |
|
1843 |
elif infos["type"] == "Structure": |
|
1844 |
struct = plcopen.varListPlain() |
|
1845 |
for i, element_infos in enumerate(infos["elements"]): |
|
1846 |
element = plcopen.varListPlain_variable() |
|
1847 |
element.setname(element_infos["Name"]) |
|
1848 |
if element_infos["Type"] in self.GetBaseTypes(): |
|
1849 |
if element_infos["Type"] == "STRING": |
|
1850 |
element.type.setcontent({"name" : "string", "value" : plcopen.elementaryTypes_string()}) |
|
1851 |
elif element_infos["Type"] == "WSTRING": |
|
1852 |
element.type.setcontent({"name" : "wstring", "value" : plcopen.wstring()}) |
|
1853 |
else: |
|
1854 |
element.type.setcontent({"name" : element_infos["Type"], "value" : None}) |
|
1855 |
else: |
|
1856 |
derived_datatype = plcopen.derivedTypes_derived() |
|
1857 |
derived_datatype.setname(element_infos["Type"]) |
|
1858 |
element.type.setcontent({"name" : "derived", "value" : derived_datatype}) |
|
1859 |
if element_infos["Initial Value"] != "": |
|
1860 |
value = plcopen.value() |
|
1861 |
value.setvalue(element_infos["Initial Value"]) |
|
1862 |
element.setinitialValue(value) |
|
1863 |
if i == 0: |
|
1864 |
struct.setvariable([element]) |
|
1865 |
else: |
|
1866 |
struct.appendvariable(element) |
|
1867 |
datatype.baseType.setcontent({"name" : "struct", "value" : struct}) |
|
1868 |
if infos["initial"] != "": |
|
1869 |
if datatype.initialValue is None: |
|
1870 |
datatype.initialValue = plcopen.value() |
|
1871 |
datatype.initialValue.setvalue(infos["initial"]) |
|
1872 |
else: |
|
1873 |
datatype.initialValue = None |
|
1874 |
self.Project.RefreshDataTypeHierarchy() |
|
1875 |
self.Project.RefreshElementUsingTree() |
|
1876 |
self.BufferProject() |
|
1877 |
||
1878 |
#------------------------------------------------------------------------------- |
|
1879 |
# Project opened Pous management functions |
|
1880 |
#------------------------------------------------------------------------------- |
|
1881 |
||
1882 |
# Return edited element |
|
1883 |
def GetEditedElement(self, tagname, debug = False): |
|
1884 |
project = self.GetProject(debug) |
|
1885 |
if project is not None: |
|
1886 |
words = tagname.split("::") |
|
1887 |
if words[0] == "D": |
|
1888 |
return project.getdataType(words[1]) |
|
1889 |
elif words[0] == "P": |
|
1890 |
return project.getpou(words[1]) |
|
1891 |
elif words[0] in ['T', 'A']: |
|
1892 |
pou = project.getpou(words[1]) |
|
1893 |
if pou is not None: |
|
1894 |
if words[0] == 'T': |
|
1895 |
return pou.gettransition(words[2]) |
|
1896 |
elif words[0] == 'A': |
|
1897 |
return pou.getaction(words[2]) |
|
1898 |
elif words[0] == 'C': |
|
1899 |
return project.getconfiguration(words[1]) |
|
1900 |
elif words[0] == 'R': |
|
1901 |
return project.getconfigurationResource(words[1], words[2]) |
|
1902 |
return None |
|
1903 |
||
1904 |
# Return edited element name |
|
1905 |
def GetEditedElementName(self, tagname): |
|
1906 |
words = tagname.split("::") |
|
1907 |
if words[0] in ["P","C","D"]: |
|
1908 |
return words[1] |
|
1909 |
else: |
|
1910 |
return words[2] |
|
1911 |
return None |
|
1912 |
||
1913 |
# Return edited element name and type |
|
1914 |
def GetEditedElementType(self, tagname, debug = False): |
|
1915 |
words = tagname.split("::") |
|
1916 |
if words[0] in ["P","T","A"]: |
|
1917 |
return words[1], self.GetPouType(words[1], debug) |
|
1918 |
return None, None |
|
1919 |
||
1920 |
# Return language in which edited element is written |
|
1921 |
def GetEditedElementBodyType(self, tagname, debug = False): |
|
1922 |
words = tagname.split("::") |
|
1923 |
if words[0] == "P": |
|
1924 |
return self.GetPouBodyType(words[1], debug) |
|
1925 |
elif words[0] == 'T': |
|
1926 |
return self.GetTransitionBodyType(words[1], words[2], debug) |
|
1927 |
elif words[0] == 'A': |
|
1928 |
return self.GetActionBodyType(words[1], words[2], debug) |
|
1929 |
return None |
|
1930 |
||
1931 |
# Return the edited element variables |
|
1932 |
def GetEditedElementInterfaceVars(self, tagname, debug = False): |
|
1933 |
words = tagname.split("::") |
|
1934 |
if words[0] in ["P","T","A"]: |
|
1935 |
project = self.GetProject(debug) |
|
1936 |
if project is not None: |
|
1937 |
pou = project.getpou(words[1]) |
|
1938 |
if pou is not None: |
|
1939 |
return self.GetPouInterfaceVars(pou, debug) |
|
1940 |
return [] |
|
1941 |
||
1942 |
# Return the edited element return type |
|
1943 |
def GetEditedElementInterfaceReturnType(self, tagname, debug = False): |
|
1944 |
words = tagname.split("::") |
|
1945 |
if words[0] == "P": |
|
1946 |
project = self.GetProject(debug) |
|
1947 |
if project is not None: |
|
1948 |
pou = self.Project.getpou(words[1]) |
|
1949 |
if pou is not None: |
|
1950 |
return self.GetPouInterfaceReturnType(pou) |
|
1951 |
elif words[0] == 'T': |
|
1952 |
return "BOOL" |
|
1953 |
return None |
|
1954 |
||
1955 |
# Change the edited element text |
|
1956 |
def SetEditedElementText(self, tagname, text): |
|
1957 |
if self.Project is not None: |
|
1958 |
element = self.GetEditedElement(tagname) |
|
1959 |
if element is not None: |
|
1960 |
element.settext(text) |
|
1961 |
self.Project.RefreshElementUsingTree() |
|
1962 |
||
1963 |
# Return the edited element text |
|
1964 |
def GetEditedElementText(self, tagname, debug = False): |
|
1965 |
element = self.GetEditedElement(tagname, debug) |
|
1966 |
if element is not None: |
|
1967 |
return element.gettext() |
|
1968 |
return "" |
|
1969 |
||
1970 |
# Return the edited element transitions |
|
1971 |
def GetEditedElementTransitions(self, tagname, debug = False): |
|
1972 |
pou = self.GetEditedElement(tagname, debug) |
|
1973 |
if pou is not None and pou.getbodyType() == "SFC": |
|
1974 |
transitions = [] |
|
1975 |
for transition in pou.gettransitionList(): |
|
1976 |
transitions.append(transition.getname()) |
|
1977 |
return transitions |
|
1978 |
return [] |
|
1979 |
||
1980 |
# Return edited element transitions |
|
1981 |
def GetEditedElementActions(self, tagname, debug = False): |
|
1982 |
pou = self.GetEditedElement(tagname, debug) |
|
1983 |
if pou is not None and pou.getbodyType() == "SFC": |
|
1984 |
actions = [] |
|
1985 |
for action in pou.getactionList(): |
|
1986 |
actions.append(action.getname()) |
|
1987 |
return actions |
|
1988 |
return [] |
|
1989 |
||
1990 |
# Return the names of the pou elements |
|
1991 |
def GetEditedElementVariables(self, tagname, debug = False): |
|
1992 |
words = tagname.split("::") |
|
1993 |
if words[0] in ["P","T","A"]: |
|
1994 |
return self.GetProjectPouVariables(words[1], debug) |
|
1995 |
return [] |
|
1996 |
||
1997 |
def GetEditedElementCopy(self, tagname, debug = False): |
|
1998 |
element = self.GetEditedElement(tagname, debug) |
|
1999 |
if element is not None: |
|
2000 |
name = element.__class__.__name__ |
|
2001 |
return element.generateXMLText(name.split("_")[-1], 0) |
|
2002 |
return "" |
|
2003 |
||
2004 |
def GetEditedElementInstancesCopy(self, tagname, blocks_id = None, wires = None, debug = False): |
|
2005 |
element = self.GetEditedElement(tagname, debug) |
|
2006 |
text = "" |
|
2007 |
if element is not None: |
|
2008 |
wires = dict([(wire, True) for wire in wires if wire[0] in blocks_id and wire[1] in blocks_id]) |
|
2009 |
for id in blocks_id: |
|
2010 |
instance = element.getinstance(id) |
|
2011 |
if instance is not None: |
|
2012 |
instance_copy = self.Copy(instance) |
|
2013 |
instance_copy.filterConnections(wires) |
|
2014 |
name = instance_copy.__class__.__name__ |
|
2015 |
text += instance_copy.generateXMLText(name.split("_")[-1], 0) |
|
2016 |
return text |
|
2017 |
||
2018 |
def GenerateNewName(self, tagname, name, format, exclude={}, debug=False): |
|
2019 |
names = exclude.copy() |
|
2020 |
if tagname is not None: |
|
2021 |
names.update(dict([(varname.upper(), True) for varname in self.GetEditedElementVariables(tagname, debug)])) |
|
2022 |
element = self.GetEditedElement(tagname, debug) |
|
2023 |
if element is not None: |
|
2024 |
for instance in element.getinstances(): |
|
2025 |
if isinstance(instance, (plcopen.sfcObjects_step, plcopen.commonObjects_connector, plcopen.commonObjects_continuation)): |
|
2026 |
names[instance.getname().upper()] = True |
|
2027 |
else: |
|
2028 |
project = self.GetProject(debug) |
|
2029 |
if project is not None: |
|
2030 |
for datatype in project.getdataTypes(): |
|
2031 |
names[datatype.getname().upper()] = True |
|
2032 |
for pou in project.getpous(): |
|
2033 |
names[pou.getname().upper()] = True |
|
2034 |
for var in self.GetPouInterfaceVars(pou, debug): |
|
2035 |
names[var["Name"].upper()] = True |
|
2036 |
for transition in pou.gettransitionList(): |
|
2037 |
names[transition.getname().upper()] = True |
|
2038 |
for action in pou.getactionList(): |
|
2039 |
names[action.getname().upper()] = True |
|
2040 |
for config in project.getconfigurations(): |
|
2041 |
names[config.getname().upper()] = True |
|
2042 |
for resource in config.getresource(): |
|
2043 |
names[resource.getname().upper()] = True |
|
2044 |
||
2045 |
i = 0 |
|
2046 |
while name is None or names.get(name.upper(), False): |
|
2047 |
name = (format%i) |
|
2048 |
i += 1 |
|
2049 |
return name |
|
2050 |
||
2051 |
CheckPasteCompatibility = {"SFC": lambda name: True, |
|
2052 |
"LD": lambda name: not name.startswith("sfcObjects"), |
|
2053 |
"FBD": lambda name: name.startswith("fbdObjects") or name.startswith("commonObjects")} |
|
2054 |
||
2055 |
def PasteEditedElementInstances(self, tagname, text, new_pos, middle=False, debug=False): |
|
2056 |
element = self.GetEditedElement(tagname, debug) |
|
2057 |
element_name, element_type = self.GetEditedElementType(tagname, debug) |
|
2058 |
if element is not None: |
|
2059 |
bodytype = element.getbodyType() |
|
2060 |
||
2061 |
# Get edited element type scaling |
|
2062 |
scaling = None |
|
2063 |
project = self.GetProject(debug) |
|
2064 |
if project is not None: |
|
2065 |
properties = project.getcontentHeader() |
|
2066 |
scaling = properties["scaling"][bodytype] |
|
2067 |
||
2068 |
# Get ids already by all the instances in edited element |
|
2069 |
used_id = dict([(instance.getlocalId(), True) for instance in element.getinstances()]) |
|
2070 |
new_id = {} |
|
2071 |
||
2072 |
text = "<paste>%s</paste>"%text |
|
2073 |
||
2074 |
try: |
|
2075 |
tree = minidom.parseString(text) |
|
2076 |
except: |
|
2077 |
return _("Invalid plcopen element(s)!!!") |
|
2078 |
instances = [] |
|
2079 |
exclude = {} |
|
2080 |
for root in tree.childNodes: |
|
2081 |
if root.nodeType == tree.ELEMENT_NODE and root.nodeName == "paste": |
|
2082 |
for child in root.childNodes: |
|
2083 |
if child.nodeType == tree.ELEMENT_NODE: |
|
2084 |
if not child.nodeName in plcopen.ElementNameToClass: |
|
2085 |
return _("\"%s\" element can't be pasted here!!!")%child.nodeName |
|
2086 |
||
2087 |
classname = plcopen.ElementNameToClass[child.nodeName] |
|
2088 |
if not self.CheckPasteCompatibility[bodytype](classname): |
|
2089 |
return _("\"%s\" element can't be pasted here!!!")%child.nodeName |
|
2090 |
||
2091 |
classobj = getattr(plcopen, classname, None) |
|
2092 |
if classobj is not None: |
|
2093 |
instance = classobj() |
|
2094 |
instance.loadXMLTree(child) |
|
2095 |
if child.nodeName == "block": |
|
2096 |
blockname = instance.getinstanceName() |
|
2097 |
if blockname is not None: |
|
2098 |
blocktype = instance.gettypeName() |
|
2099 |
if element_type == "function": |
|
2100 |
return _("FunctionBlock \"%s\" can't be pasted in a Function!!!")%blocktype |
|
2101 |
blockname = self.GenerateNewName(tagname, blockname, "%s%%d"%blocktype, debug=debug) |
|
2102 |
exclude[blockname] = True |
|
2103 |
instance.setinstanceName(blockname) |
|
2104 |
self.AddEditedElementPouVar(tagname, blocktype, blockname) |
|
2105 |
elif child.nodeName == "step": |
|
2106 |
stepname = self.GenerateNewName(tagname, instance.getname(), "Step%d", exclude, debug) |
|
2107 |
exclude[stepname] = True |
|
2108 |
instance.setname(stepname) |
|
2109 |
localid = instance.getlocalId() |
|
2110 |
if not used_id.has_key(localid): |
|
2111 |
new_id[localid] = True |
|
2112 |
instances.append((child.nodeName, instance)) |
|
2113 |
||
2114 |
if len(instances) == 0: |
|
2115 |
return _("Invalid plcopen element(s)!!!") |
|
2116 |
||
2117 |
idx = 1 |
|
2118 |
translate_id = {} |
|
2119 |
bbox = plcopen.rect() |
|
2120 |
for name, instance in instances: |
|
2121 |
localId = instance.getlocalId() |
|
2122 |
bbox.union(instance.getBoundingBox()) |
|
2123 |
if used_id.has_key(localId): |
|
2124 |
while used_id.has_key(idx) or new_id.has_key(idx): |
|
2125 |
idx += 1 |
|
2126 |
new_id[idx] = True |
|
2127 |
instance.setlocalId(idx) |
|
2128 |
translate_id[localId] = idx |
|
2129 |
||
2130 |
x, y, width, height = bbox.bounding_box() |
|
2131 |
if middle: |
|
2132 |
new_pos[0] -= width / 2 |
|
2133 |
new_pos[1] -= height / 2 |
|
2134 |
else: |
|
2135 |
new_pos = map(lambda x: x + 30, new_pos) |
|
2136 |
if scaling[0] != 0 and scaling[1] != 0: |
|
2137 |
min_pos = map(lambda x: 30 / x, scaling) |
|
2138 |
minx = round(min_pos[0]) |
|
2139 |
if int(min_pos[0]) == round(min_pos[0]): |
|
2140 |
minx += 1 |
|
2141 |
miny = round(min_pos[1]) |
|
2142 |
if int(min_pos[1]) == round(min_pos[1]): |
|
2143 |
miny += 1 |
|
2144 |
minx *= scaling[0] |
|
2145 |
miny *= scaling[1] |
|
2146 |
new_pos = (max(minx, round(new_pos[0] / scaling[0]) * scaling[0]), |
|
2147 |
max(miny, round(new_pos[1] / scaling[1]) * scaling[1])) |
|
2148 |
else: |
|
2149 |
new_pos = (max(30, new_pos[0]), max(30, new_pos[1])) |
|
2150 |
diff = (new_pos[0] - x, new_pos[1] - y) |
|
2151 |
||
2152 |
connections = {} |
|
2153 |
for name, instance in instances: |
|
2154 |
connections.update(instance.updateConnectionsId(translate_id)) |
|
2155 |
if getattr(instance, "setexecutionOrderId", None) is not None: |
|
2156 |
instance.setexecutionOrderId(0) |
|
2157 |
instance.translate(*diff) |
|
2158 |
element.addinstance(name, instance) |
|
2159 |
||
2160 |
return new_id, connections |
|
2161 |
||
2162 |
# Return the current pou editing informations |
|
2163 |
def GetEditedElementInstanceInfos(self, tagname, id = None, exclude = [], debug = False): |
|
2164 |
infos = {} |
|
2165 |
instance = None |
|
2166 |
element = self.GetEditedElement(tagname, debug) |
|
2167 |
if element is not None: |
|
2168 |
# if id is defined |
|
2169 |
if id is not None: |
|
2170 |
instance = element.getinstance(id) |
|
2171 |
else: |
|
2172 |
instance = element.getrandomInstance(exclude) |
|
2173 |
if instance is not None: |
|
2174 |
infos = instance.getinfos() |
|
2175 |
if infos["type"] in ["input", "output", "inout"]: |
|
2176 |
var_type = self.GetEditedElementVarValueType(tagname, infos["specific_values"]["name"], debug) |
|
2177 |
infos["specific_values"]["value_type"] = var_type |
|
2178 |
return infos |
|
2179 |
return None |
|
2180 |
||
2181 |
def ClearEditedElementExecutionOrder(self, tagname): |
|
2182 |
element = self.GetEditedElement(tagname) |
|
2183 |
if element is not None: |
|
2184 |
element.resetexecutionOrder() |
|
2185 |
||
2186 |
def ResetEditedElementExecutionOrder(self, tagname): |
|
2187 |
element = self.GetEditedElement(tagname) |
|
2188 |
if element is not None: |
|
2189 |
element.compileexecutionOrder() |
|
2190 |
||
2191 |
# Return the variable type of the given pou |
|
2192 |
def GetEditedElementVarValueType(self, tagname, varname, debug = False): |
|
2193 |
project = self.GetProject(debug) |
|
2194 |
if project is not None: |
|
2195 |
words = tagname.split("::") |
|
2196 |
if words[0] in ["P","T","A"]: |
|
2197 |
pou = self.Project.getpou(words[1]) |
|
2198 |
if pou is not None: |
|
2199 |
if words[0] == "T" and varname == words[2]: |
|
2200 |
return "BOOL" |
|
2201 |
if words[1] == varname: |
|
2202 |
return self.GetPouInterfaceReturnType(pou) |
|
2203 |
for type, varlist in pou.getvars(): |
|
2204 |
for var in varlist.getvariable(): |
|
2205 |
if var.getname() == varname: |
|
2206 |
vartype_content = var.gettype().getcontent() |
|
2207 |
if vartype_content["name"] == "derived": |
|
2208 |
return vartype_content["value"].getname() |
|
2209 |
elif vartype_content["name"] in ["string", "wstring"]: |
|
2210 |
return vartype_content["name"].upper() |
|
2211 |
else: |
|
2212 |
return vartype_content["name"] |
|
2213 |
return None |
|
2214 |
||
2215 |
def SetConnectionWires(self, connection, connector): |
|
2216 |
wires = connector.GetWires() |
|
2217 |
idx = 0 |
|
2218 |
for wire, handle in wires: |
|
2219 |
points = wire.GetPoints(handle != 0) |
|
2220 |
if handle == 0: |
|
2221 |
result = wire.GetConnectedInfos(-1) |
|
2222 |
else: |
|
2223 |
result = wire.GetConnectedInfos(0) |
|
2224 |
if result != None: |
|
2225 |
refLocalId, formalParameter = result |
|
2226 |
connections = connection.getconnections() |
|
2227 |
if connections is None or len(connection.getconnections()) <= idx: |
|
2228 |
connection.addconnection() |
|
2229 |
connection.setconnectionId(idx, refLocalId) |
|
2230 |
connection.setconnectionPoints(idx, points) |
|
2231 |
if formalParameter != "": |
|
2232 |
connection.setconnectionParameter(idx, formalParameter) |
|
2233 |
else: |
|
2234 |
connection.setconnectionParameter(idx, None) |
|
2235 |
idx += 1 |
|
2236 |
||
2237 |
def AddEditedElementPouVar(self, tagname, type, name, location="", description=""): |
|
2238 |
if self.Project is not None: |
|
2239 |
words = tagname.split("::") |
|
2240 |
if words[0] in ['P', 'T', 'A']: |
|
2241 |
pou = self.Project.getpou(words[1]) |
|
2242 |
if pou is not None: |
|
2243 |
pou.addpouLocalVar(type, name, location, description) |
|
2244 |
||
2245 |
def AddEditedElementPouExternalVar(self, tagname, type, name): |
|
2246 |
if self.Project is not None: |
|
2247 |
words = tagname.split("::") |
|
2248 |
if words[0] in ['P', 'T', 'A']: |
|
2249 |
pou = self.Project.getpou(words[1]) |
|
2250 |
if pou is not None: |
|
2251 |
pou.addpouExternalVar(type, name) |
|
2252 |
||
2253 |
def ChangeEditedElementPouVar(self, tagname, old_type, old_name, new_type, new_name): |
|
2254 |
if self.Project is not None: |
|
2255 |
words = tagname.split("::") |
|
2256 |
if words[0] in ['P', 'T', 'A']: |
|
2257 |
pou = self.Project.getpou(words[1]) |
|
2258 |
if pou is not None: |
|
2259 |
pou.changepouVar(old_type, old_name, new_type, new_name) |
|
2260 |
||
2261 |
def RemoveEditedElementPouVar(self, tagname, type, name): |
|
2262 |
if self.Project is not None: |
|
2263 |
words = tagname.split("::") |
|
2264 |
if words[0] in ['P', 'T', 'A']: |
|
2265 |
pou = self.Project.getpou(words[1]) |
|
2266 |
if pou is not None: |
|
2267 |
pou.removepouVar(type, name) |
|
2268 |
||
2269 |
def AddEditedElementBlock(self, tagname, id, blocktype, blockname = None): |
|
2270 |
element = self.GetEditedElement(tagname) |
|
2271 |
if element is not None: |
|
2272 |
block = plcopen.fbdObjects_block() |
|
2273 |
block.setlocalId(id) |
|
2274 |
block.settypeName(blocktype) |
|
2275 |
blocktype_infos = self.GetBlockType(blocktype) |
|
2276 |
if blocktype_infos["type"] != "function" and blockname is not None: |
|
2277 |
block.setinstanceName(blockname) |
|
2278 |
self.AddEditedElementPouVar(tagname, blocktype, blockname) |
|
2279 |
element.addinstance("block", block) |
|
2280 |
self.Project.RefreshElementUsingTree() |
|
2281 |
||
2282 |
def SetEditedElementBlockInfos(self, tagname, id, infos): |
|
2283 |
element = self.GetEditedElement(tagname) |
|
2284 |
if element is not None: |
|
2285 |
block = element.getinstance(id) |
|
2286 |
if block is None: |
|
2287 |
return |
|
2288 |
old_name = block.getinstanceName() |
|
2289 |
old_type = block.gettypeName() |
|
2290 |
new_name = infos.get("name", old_name) |
|
2291 |
new_type = infos.get("type", old_type) |
|
2292 |
if new_type != old_type: |
|
2293 |
old_typeinfos = self.GetBlockType(old_type) |
|
2294 |
new_typeinfos = self.GetBlockType(new_type) |
|
2295 |
if old_typeinfos is None or new_typeinfos is None: |
|
2296 |
self.ChangeEditedElementPouVar(tagname, old_type, old_name, new_type, new_name) |
|
2297 |
elif new_typeinfos["type"] != old_typeinfos["type"]: |
|
2298 |
if new_typeinfos["type"] == "function": |
|
2299 |
self.RemoveEditedElementPouVar(tagname, old_type, old_name) |
|
2300 |
else: |
|
2301 |
self.AddEditedElementPouVar(tagname, new_type, new_name) |
|
2302 |
elif new_typeinfos["type"] != "function": |
|
2303 |
self.ChangeEditedElementPouVar(tagname, old_type, old_name, new_type, new_name) |
|
2304 |
elif new_name != old_name: |
|
2305 |
self.ChangeEditedElementPouVar(tagname, old_type, old_name, new_type, new_name) |
|
2306 |
for param, value in infos.items(): |
|
2307 |
if param == "name": |
|
2308 |
block.setinstanceName(value) |
|
2309 |
elif param == "type": |
|
2310 |
block.settypeName(value) |
|
2311 |
elif param == "executionOrder" and block.getexecutionOrderId() != value: |
|
2312 |
element.setelementExecutionOrder(block, value) |
|
2313 |
elif param == "height": |
|
2314 |
block.setheight(value) |
|
2315 |
elif param == "width": |
|
2316 |
block.setwidth(value) |
|
2317 |
elif param == "x": |
|
2318 |
block.setx(value) |
|
2319 |
elif param == "y": |
|
2320 |
block.sety(value) |
|
2321 |
elif param == "connectors": |
|
2322 |
block.inputVariables.setvariable([]) |
|
2323 |
block.outputVariables.setvariable([]) |
|
2324 |
for connector in value["inputs"]: |
|
2325 |
variable = plcopen.inputVariables_variable() |
|
2326 |
variable.setformalParameter(connector.GetName()) |
|
2327 |
if connector.IsNegated(): |
|
2328 |
variable.setnegated(True) |
|
2329 |
if connector.GetEdge() != "none": |
|
2330 |
variable.setedge(connector.GetEdge()) |
|
2331 |
position = connector.GetRelPosition() |
|
2332 |
variable.connectionPointIn.setrelPositionXY(position.x, position.y) |
|
2333 |
self.SetConnectionWires(variable.connectionPointIn, connector) |
|
2334 |
block.inputVariables.appendvariable(variable) |
|
2335 |
for connector in value["outputs"]: |
|
2336 |
variable = plcopen.outputVariables_variable() |
|
2337 |
variable.setformalParameter(connector.GetName()) |
|
2338 |
if connector.IsNegated(): |
|
2339 |
variable.setnegated(True) |
|
2340 |
if connector.GetEdge() != "none": |
|
2341 |
variable.setedge(connector.GetEdge()) |
|
2342 |
position = connector.GetRelPosition() |
|
2343 |
variable.addconnectionPointOut() |
|
2344 |
variable.connectionPointOut.setrelPositionXY(position.x, position.y) |
|
2345 |
block.outputVariables.appendvariable(variable) |
|
2346 |
self.Project.RefreshElementUsingTree() |
|
2347 |
||
2348 |
def AddEditedElementVariable(self, tagname, id, type): |
|
2349 |
element = self.GetEditedElement(tagname) |
|
2350 |
if element is not None: |
|
2351 |
if type == INPUT: |
|
2352 |
name = "inVariable" |
|
2353 |
variable = plcopen.fbdObjects_inVariable() |
|
2354 |
elif type == OUTPUT: |
|
2355 |
name = "outVariable" |
|
2356 |
variable = plcopen.fbdObjects_outVariable() |
|
2357 |
elif type == INOUT: |
|
2358 |
name = "inOutVariable" |
|
2359 |
variable = plcopen.fbdObjects_inOutVariable() |
|
2360 |
variable.setlocalId(id) |
|
2361 |
element.addinstance(name, variable) |
|
2362 |
||
2363 |
def SetEditedElementVariableInfos(self, tagname, id, infos): |
|
2364 |
element = self.GetEditedElement(tagname) |
|
2365 |
if element is not None: |
|
2366 |
variable = element.getinstance(id) |
|
2367 |
if variable is None: |
|
2368 |
return |
|
2369 |
for param, value in infos.items(): |
|
2370 |
if param == "name": |
|
2371 |
variable.setexpression(value) |
|
2372 |
elif param == "executionOrder" and variable.getexecutionOrderId() != value: |
|
2373 |
element.setelementExecutionOrder(variable, value) |
|
2374 |
elif param == "height": |
|
2375 |
variable.setheight(value) |
|
2376 |
elif param == "width": |
|
2377 |
variable.setwidth(value) |
|
2378 |
elif param == "x": |
|
2379 |
variable.setx(value) |
|
2380 |
elif param == "y": |
|
2381 |
variable.sety(value) |
|
2382 |
elif param == "connectors": |
|
2383 |
if len(value["outputs"]) > 0: |
|
2384 |
output = value["outputs"][0] |
|
2385 |
if len(value["inputs"]) > 0: |
|
2386 |
variable.setnegatedOut(output.IsNegated()) |
|
2387 |
variable.setedgeOut(output.GetEdge()) |
|
2388 |
else: |
|
2389 |
variable.setnegated(output.IsNegated()) |
|
2390 |
variable.setedge(output.GetEdge()) |
|
2391 |
position = output.GetRelPosition() |
|
2392 |
variable.addconnectionPointOut() |
|
2393 |
variable.connectionPointOut.setrelPositionXY(position.x, position.y) |
|
2394 |
if len(value["inputs"]) > 0: |
|
2395 |
input = value["inputs"][0] |
|
2396 |
if len(value["outputs"]) > 0: |
|
2397 |
variable.setnegatedIn(input.IsNegated()) |
|
2398 |
variable.setedgeIn(input.GetEdge()) |
|
2399 |
else: |
|
2400 |
variable.setnegated(input.IsNegated()) |
|
2401 |
variable.setedge(input.GetEdge()) |
|
2402 |
position = input.GetRelPosition() |
|
2403 |
variable.addconnectionPointIn() |
|
2404 |
variable.connectionPointIn.setrelPositionXY(position.x, position.y) |
|
2405 |
self.SetConnectionWires(variable.connectionPointIn, input) |
|
2406 |
||
2407 |
def AddEditedElementConnection(self, tagname, id, type): |
|
2408 |
element = self.GetEditedElement(tagname) |
|
2409 |
if element is not None: |
|
2410 |
if type == CONNECTOR: |
|
2411 |
name = "connector" |
|
2412 |
connection = plcopen.commonObjects_connector() |
|
2413 |
elif type == CONTINUATION: |
|
2414 |
name = "continuation" |
|
2415 |
connection = plcopen.commonObjects_continuation() |
|
2416 |
connection.setlocalId(id) |
|
2417 |
element.addinstance(name, connection) |
|
2418 |
||
2419 |
def SetEditedElementConnectionInfos(self, tagname, id, infos): |
|
2420 |
element = self.GetEditedElement(tagname) |
|
2421 |
if element is not None: |
|
2422 |
connection = element.getinstance(id) |
|
2423 |
if connection is None: |
|
2424 |
return |
|
2425 |
for param, value in infos.items(): |
|
2426 |
if param == "name": |
|
2427 |
connection.setname(value) |
|
2428 |
elif param == "height": |
|
2429 |
connection.setheight(value) |
|
2430 |
elif param == "width": |
|
2431 |
connection.setwidth(value) |
|
2432 |
elif param == "x": |
|
2433 |
connection.setx(value) |
|
2434 |
elif param == "y": |
|
2435 |
connection.sety(value) |
|
2436 |
elif param == "connector": |
|
2437 |
position = value.GetRelPosition() |
|
2438 |
if isinstance(connection, plcopen.commonObjects_continuation): |
|
2439 |
connection.addconnectionPointOut() |
|
2440 |
connection.connectionPointOut.setrelPositionXY(position.x, position.y) |
|
2441 |
elif isinstance(connection, plcopen.commonObjects_connector): |
|
2442 |
connection.addconnectionPointIn() |
|
2443 |
connection.connectionPointIn.setrelPositionXY(position.x, position.y) |
|
2444 |
self.SetConnectionWires(connection.connectionPointIn, value) |
|
2445 |
||
2446 |
def AddEditedElementComment(self, tagname, id): |
|
2447 |
element = self.GetEditedElement(tagname) |
|
2448 |
if element is not None: |
|
2449 |
comment = plcopen.commonObjects_comment() |
|
2450 |
comment.setlocalId(id) |
|
2451 |
element.addinstance("comment", comment) |
|
2452 |
||
2453 |
def SetEditedElementCommentInfos(self, tagname, id, infos): |
|
2454 |
element = self.GetEditedElement(tagname) |
|
2455 |
if element is not None: |
|
2456 |
comment = element.getinstance(id) |
|
2457 |
for param, value in infos.items(): |
|
2458 |
if param == "content": |
|
2459 |
comment.setcontentText(value) |
|
2460 |
elif param == "height": |
|
2461 |
comment.setheight(value) |
|
2462 |
elif param == "width": |
|
2463 |
comment.setwidth(value) |
|
2464 |
elif param == "x": |
|
2465 |
comment.setx(value) |
|
2466 |
elif param == "y": |
|
2467 |
comment.sety(value) |
|
2468 |
||
2469 |
def AddEditedElementPowerRail(self, tagname, id, type): |
|
2470 |
element = self.GetEditedElement(tagname) |
|
2471 |
if element is not None: |
|
2472 |
if type == LEFTRAIL: |
|
2473 |
name = "leftPowerRail" |
|
2474 |
powerrail = plcopen.ldObjects_leftPowerRail() |
|
2475 |
elif type == RIGHTRAIL: |
|
2476 |
name = "rightPowerRail" |
|
2477 |
powerrail = plcopen.ldObjects_rightPowerRail() |
|
2478 |
powerrail.setlocalId(id) |
|
2479 |
element.addinstance(name, powerrail) |
|
2480 |
||
2481 |
def SetEditedElementPowerRailInfos(self, tagname, id, infos): |
|
2482 |
element = self.GetEditedElement(tagname) |
|
2483 |
if element is not None: |
|
2484 |
powerrail = element.getinstance(id) |
|
2485 |
if powerrail is None: |
|
2486 |
return |
|
2487 |
for param, value in infos.items(): |
|
2488 |
if param == "height": |
|
2489 |
powerrail.setheight(value) |
|
2490 |
elif param == "width": |
|
2491 |
powerrail.setwidth(value) |
|
2492 |
elif param == "x": |
|
2493 |
powerrail.setx(value) |
|
2494 |
elif param == "y": |
|
2495 |
powerrail.sety(value) |
|
2496 |
elif param == "connectors": |
|
2497 |
if isinstance(powerrail, plcopen.ldObjects_leftPowerRail): |
|
2498 |
powerrail.setconnectionPointOut([]) |
|
2499 |
for connector in value["outputs"]: |
|
2500 |
position = connector.GetRelPosition() |
|
2501 |
connection = plcopen.leftPowerRail_connectionPointOut() |
|
2502 |
connection.setrelPositionXY(position.x, position.y) |
|
2503 |
powerrail.connectionPointOut.append(connection) |
|
2504 |
elif isinstance(powerrail, plcopen.ldObjects_rightPowerRail): |
|
2505 |
powerrail.setconnectionPointIn([]) |
|
2506 |
for connector in value["inputs"]: |
|
2507 |
position = connector.GetRelPosition() |
|
2508 |
connection = plcopen.connectionPointIn() |
|
2509 |
connection.setrelPositionXY(position.x, position.y) |
|
2510 |
self.SetConnectionWires(connection, connector) |
|
2511 |
powerrail.connectionPointIn.append(connection) |
|
2512 |
||
2513 |
def AddEditedElementContact(self, tagname, id): |
|
2514 |
element = self.GetEditedElement(tagname) |
|
2515 |
if element is not None: |
|
2516 |
contact = plcopen.ldObjects_contact() |
|
2517 |
contact.setlocalId(id) |
|
2518 |
element.addinstance("contact", contact) |
|
2519 |
||
2520 |
def SetEditedElementContactInfos(self, tagname, id, infos): |
|
2521 |
element = self.GetEditedElement(tagname) |
|
2522 |
if element is not None: |
|
2523 |
contact = element.getinstance(id) |
|
2524 |
if contact is None: |
|
2525 |
return |
|
2526 |
for param, value in infos.items(): |
|
2527 |
if param == "name": |
|
2528 |
contact.setvariable(value) |
|
2529 |
elif param == "type": |
|
2530 |
if value == CONTACT_NORMAL: |
|
2531 |
contact.setnegated(False) |
|
2532 |
contact.setedge("none") |
|
2533 |
elif value == CONTACT_REVERSE: |
|
2534 |
contact.setnegated(True) |
|
2535 |
contact.setedge("none") |
|
2536 |
elif value == CONTACT_RISING: |
|
2537 |
contact.setnegated(False) |
|
2538 |
contact.setedge("rising") |
|
2539 |
elif value == CONTACT_FALLING: |
|
2540 |
contact.setnegated(False) |
|
2541 |
contact.setedge("falling") |
|
2542 |
elif param == "height": |
|
2543 |
contact.setheight(value) |
|
2544 |
elif param == "width": |
|
2545 |
contact.setwidth(value) |
|
2546 |
elif param == "x": |
|
2547 |
contact.setx(value) |
|
2548 |
elif param == "y": |
|
2549 |
contact.sety(value) |
|
2550 |
elif param == "connectors": |
|
2551 |
input_connector = value["inputs"][0] |
|
2552 |
position = input_connector.GetRelPosition() |
|
2553 |
contact.addconnectionPointIn() |
|
2554 |
contact.connectionPointIn.setrelPositionXY(position.x, position.y) |
|
2555 |
self.SetConnectionWires(contact.connectionPointIn, input_connector) |
|
2556 |
output_connector = value["outputs"][0] |
|
2557 |
position = output_connector.GetRelPosition() |
|
2558 |
contact.addconnectionPointOut() |
|
2559 |
contact.connectionPointOut.setrelPositionXY(position.x, position.y) |
|
2560 |
||
2561 |
def AddEditedElementCoil(self, tagname, id): |
|
2562 |
element = self.GetEditedElement(tagname) |
|
2563 |
if element is not None: |
|
2564 |
coil = plcopen.ldObjects_coil() |
|
2565 |
coil.setlocalId(id) |
|
2566 |
element.addinstance("coil", coil) |
|
2567 |
||
2568 |
def SetEditedElementCoilInfos(self, tagname, id, infos): |
|
2569 |
element = self.GetEditedElement(tagname) |
|
2570 |
if element is not None: |
|
2571 |
coil = element.getinstance(id) |
|
2572 |
if coil is None: |
|
2573 |
return |
|
2574 |
for param, value in infos.items(): |
|
2575 |
if param == "name": |
|
2576 |
coil.setvariable(value) |
|
2577 |
elif param == "type": |
|
2578 |
if value == COIL_NORMAL: |
|
2579 |
coil.setnegated(False) |
|
2580 |
coil.setstorage("none") |
|
2581 |
coil.setedge("none") |
|
2582 |
elif value == COIL_REVERSE: |
|
2583 |
coil.setnegated(True) |
|
2584 |
coil.setstorage("none") |
|
2585 |
coil.setedge("none") |
|
2586 |
elif value == COIL_SET: |
|
2587 |
coil.setnegated(False) |
|
2588 |
coil.setstorage("set") |
|
2589 |
coil.setedge("none") |
|
2590 |
elif value == COIL_RESET: |
|
2591 |
coil.setnegated(False) |
|
2592 |
coil.setstorage("reset") |
|
2593 |
coil.setedge("none") |
|
2594 |
elif value == COIL_RISING: |
|
2595 |
coil.setnegated(False) |
|
2596 |
coil.setstorage("none") |
|
2597 |
coil.setedge("rising") |
|
2598 |
elif value == COIL_FALLING: |
|
2599 |
coil.setnegated(False) |
|
2600 |
coil.setstorage("none") |
|
2601 |
coil.setedge("falling") |
|
2602 |
elif param == "height": |
|
2603 |
coil.setheight(value) |
|
2604 |
elif param == "width": |
|
2605 |
coil.setwidth(value) |
|
2606 |
elif param == "x": |
|
2607 |
coil.setx(value) |
|
2608 |
elif param == "y": |
|
2609 |
coil.sety(value) |
|
2610 |
elif param == "connectors": |
|
2611 |
input_connector = value["inputs"][0] |
|
2612 |
position = input_connector.GetRelPosition() |
|
2613 |
coil.addconnectionPointIn() |
|
2614 |
coil.connectionPointIn.setrelPositionXY(position.x, position.y) |
|
2615 |
self.SetConnectionWires(coil.connectionPointIn, input_connector) |
|
2616 |
output_connector = value["outputs"][0] |
|
2617 |
position = output_connector.GetRelPosition() |
|
2618 |
coil.addconnectionPointOut() |
|
2619 |
coil.connectionPointOut.setrelPositionXY(position.x, position.y) |
|
2620 |
||
2621 |
def AddEditedElementStep(self, tagname, id): |
|
2622 |
element = self.GetEditedElement(tagname) |
|
2623 |
if element is not None: |
|
2624 |
step = plcopen.sfcObjects_step() |
|
2625 |
step.setlocalId(id) |
|
2626 |
element.addinstance("step", step) |
|
2627 |
||
2628 |
def SetEditedElementStepInfos(self, tagname, id, infos): |
|
2629 |
element = self.GetEditedElement(tagname) |
|
2630 |
if element is not None: |
|
2631 |
step = element.getinstance(id) |
|
2632 |
if step is None: |
|
2633 |
return |
|
2634 |
for param, value in infos.items(): |
|
2635 |
if param == "name": |
|
2636 |
step.setname(value) |
|
2637 |
elif param == "initial": |
|
2638 |
step.setinitialStep(value) |
|
2639 |
elif param == "height": |
|
2640 |
step.setheight(value) |
|
2641 |
elif param == "width": |
|
2642 |
step.setwidth(value) |
|
2643 |
elif param == "x": |
|
2644 |
step.setx(value) |
|
2645 |
elif param == "y": |
|
2646 |
step.sety(value) |
|
2647 |
elif param == "connectors": |
|
2648 |
if len(value["inputs"]) > 0: |
|
2649 |
input_connector = value["inputs"][0] |
|
2650 |
position = input_connector.GetRelPosition() |
|
2651 |
step.addconnectionPointIn() |
|
2652 |
step.connectionPointIn.setrelPositionXY(position.x, position.y) |
|
2653 |
self.SetConnectionWires(step.connectionPointIn, input_connector) |
|
2654 |
else: |
|
2655 |
step.deleteconnectionPointIn() |
|
2656 |
if len(value["outputs"]) > 0: |
|
2657 |
output_connector = value["outputs"][0] |
|
2658 |
position = output_connector.GetRelPosition() |
|
2659 |
step.addconnectionPointOut() |
|
2660 |
step.connectionPointOut.setrelPositionXY(position.x, position.y) |
|
2661 |
else: |
|
2662 |
step.deleteconnectionPointOut() |
|
2663 |
elif param == "action": |
|
2664 |
if value: |
|
2665 |
position = value.GetRelPosition() |
|
2666 |
step.addconnectionPointOutAction() |
|
2667 |
step.connectionPointOutAction.setrelPositionXY(position.x, position.y) |
|
2668 |
else: |
|
2669 |
step.deleteconnectionPointOutAction() |
|
2670 |
||
2671 |
def AddEditedElementTransition(self, tagname, id): |
|
2672 |
element = self.GetEditedElement(tagname) |
|
2673 |
if element is not None: |
|
2674 |
transition = plcopen.sfcObjects_transition() |
|
2675 |
transition.setlocalId(id) |
|
2676 |
element.addinstance("transition", transition) |
|
2677 |
||
2678 |
def SetEditedElementTransitionInfos(self, tagname, id, infos): |
|
2679 |
element = self.GetEditedElement(tagname) |
|
2680 |
if element is not None: |
|
2681 |
transition = element.getinstance(id) |
|
2682 |
if transition is None: |
|
2683 |
return |
|
2684 |
for param, value in infos.items(): |
|
2685 |
if param == "type" and value != "connection": |
|
2686 |
transition.setconditionContent(value, infos["condition"]) |
|
2687 |
elif param == "height": |
|
2688 |
transition.setheight(value) |
|
2689 |
elif param == "width": |
|
2690 |
transition.setwidth(value) |
|
2691 |
elif param == "x": |
|
2692 |
transition.setx(value) |
|
2693 |
elif param == "y": |
|
2694 |
transition.sety(value) |
|
2695 |
elif param == "priority": |
|
2696 |
if value != 0: |
|
2697 |
transition.setpriority(value) |
|
2698 |
else: |
|
2699 |
transition.setpriority(None) |
|
2700 |
elif param == "connectors": |
|
2701 |
input_connector = value["inputs"][0] |
|
2702 |
position = input_connector.GetRelPosition() |
|
2703 |
transition.addconnectionPointIn() |
|
2704 |
transition.connectionPointIn.setrelPositionXY(position.x, position.y) |
|
2705 |
self.SetConnectionWires(transition.connectionPointIn, input_connector) |
|
2706 |
output_connector = value["outputs"][0] |
|
2707 |
position = output_connector.GetRelPosition() |
|
2708 |
transition.addconnectionPointOut() |
|
2709 |
transition.connectionPointOut.setrelPositionXY(position.x, position.y) |
|
2710 |
elif infos.get("type", None) == "connection" and param == "connection" and value: |
|
2711 |
transition.setconditionContent("connection", None) |
|
2712 |
self.SetConnectionWires(transition.condition.content["value"], value) |
|
2713 |
||
2714 |
def AddEditedElementDivergence(self, tagname, id, type): |
|
2715 |
element = self.GetEditedElement(tagname) |
|
2716 |
if element is not None: |
|
2717 |
if type == SELECTION_DIVERGENCE: |
|
2718 |
name = "selectionDivergence" |
|
2719 |
divergence = plcopen.sfcObjects_selectionDivergence() |
|
2720 |
elif type == SELECTION_CONVERGENCE: |
|
2721 |
name = "selectionConvergence" |
|
2722 |
divergence = plcopen.sfcObjects_selectionConvergence() |
|
2723 |
elif type == SIMULTANEOUS_DIVERGENCE: |
|
2724 |
name = "simultaneousDivergence" |
|
2725 |
divergence = plcopen.sfcObjects_simultaneousDivergence() |
|
2726 |
elif type == SIMULTANEOUS_CONVERGENCE: |
|
2727 |
name = "simultaneousConvergence" |
|
2728 |
divergence = plcopen.sfcObjects_simultaneousConvergence() |
|
2729 |
divergence.setlocalId(id) |
|
2730 |
element.addinstance(name, divergence) |
|
2731 |
||
2732 |
def SetEditedElementDivergenceInfos(self, tagname, id, infos): |
|
2733 |
element = self.GetEditedElement(tagname) |
|
2734 |
if element is not None: |
|
2735 |
divergence = element.getinstance(id) |
|
2736 |
if divergence is None: |
|
2737 |
return |
|
2738 |
for param, value in infos.items(): |
|
2739 |
if param == "height": |
|
2740 |
divergence.setheight(value) |
|
2741 |
elif param == "width": |
|
2742 |
divergence.setwidth(value) |
|
2743 |
elif param == "x": |
|
2744 |
divergence.setx(value) |
|
2745 |
elif param == "y": |
|
2746 |
divergence.sety(value) |
|
2747 |
elif param == "connectors": |
|
2748 |
input_connectors = value["inputs"] |
|
2749 |
if isinstance(divergence, (plcopen.sfcObjects_selectionDivergence, plcopen.sfcObjects_simultaneousDivergence)): |
|
2750 |
position = input_connectors[0].GetRelPosition() |
|
2751 |
divergence.addconnectionPointIn() |
|
2752 |
divergence.connectionPointIn.setrelPositionXY(position.x, position.y) |
|
2753 |
self.SetConnectionWires(divergence.connectionPointIn, input_connectors[0]) |
|
2754 |
else: |
|
2755 |
divergence.setconnectionPointIn([]) |
|
2756 |
for input_connector in input_connectors: |
|
2757 |
position = input_connector.GetRelPosition() |
|
2758 |
if isinstance(divergence, plcopen.sfcObjects_selectionConvergence): |
|
2759 |
connection = plcopen.selectionConvergence_connectionPointIn() |
|
2760 |
else: |
|
2761 |
connection = plcopen.connectionPointIn() |
|
2762 |
connection.setrelPositionXY(position.x, position.y) |
|
2763 |
self.SetConnectionWires(connection, input_connector) |
|
2764 |
divergence.appendconnectionPointIn(connection) |
|
2765 |
output_connectors = value["outputs"] |
|
2766 |
if isinstance(divergence, (plcopen.sfcObjects_selectionConvergence, plcopen.sfcObjects_simultaneousConvergence)): |
|
2767 |
position = output_connectors[0].GetRelPosition() |
|
2768 |
divergence.addconnectionPointOut() |
|
2769 |
divergence.connectionPointOut.setrelPositionXY(position.x, position.y) |
|
2770 |
else: |
|
2771 |
divergence.setconnectionPointOut([]) |
|
2772 |
for output_connector in output_connectors: |
|
2773 |
position = output_connector.GetRelPosition() |
|
2774 |
if isinstance(divergence, plcopen.sfcObjects_selectionDivergence): |
|
2775 |
connection = plcopen.selectionDivergence_connectionPointOut() |
|
2776 |
else: |
|
2777 |
connection = plcopen.simultaneousDivergence_connectionPointOut() |
|
2778 |
connection.setrelPositionXY(position.x, position.y) |
|
2779 |
divergence.appendconnectionPointOut(connection) |
|
2780 |
||
2781 |
def AddEditedElementJump(self, tagname, id): |
|
2782 |
element = self.GetEditedElement(tagname) |
|
2783 |
if element is not None: |
|
2784 |
jump = plcopen.sfcObjects_jumpStep() |
|
2785 |
jump.setlocalId(id) |
|
2786 |
element.addinstance("jumpStep", jump) |
|
2787 |
||
2788 |
def SetEditedElementJumpInfos(self, tagname, id, infos): |
|
2789 |
element = self.GetEditedElement(tagname) |
|
2790 |
if element is not None: |
|
2791 |
jump = element.getinstance(id) |
|
2792 |
if jump is None: |
|
2793 |
return |
|
2794 |
for param, value in infos.items(): |
|
2795 |
if param == "target": |
|
2796 |
jump.settargetName(value) |
|
2797 |
elif param == "height": |
|
2798 |
jump.setheight(value) |
|
2799 |
elif param == "width": |
|
2800 |
jump.setwidth(value) |
|
2801 |
elif param == "x": |
|
2802 |
jump.setx(value) |
|
2803 |
elif param == "y": |
|
2804 |
jump.sety(value) |
|
2805 |
elif param == "connector": |
|
2806 |
position = value.GetRelPosition() |
|
2807 |
jump.addconnectionPointIn() |
|
2808 |
jump.connectionPointIn.setrelPositionXY(position.x, position.y) |
|
2809 |
self.SetConnectionWires(jump.connectionPointIn, value) |
|
2810 |
||
2811 |
def AddEditedElementActionBlock(self, tagname, id): |
|
2812 |
element = self.GetEditedElement(tagname) |
|
2813 |
if element is not None: |
|
2814 |
actionBlock = plcopen.commonObjects_actionBlock() |
|
2815 |
actionBlock.setlocalId(id) |
|
2816 |
element.addinstance("actionBlock", actionBlock) |
|
2817 |
||
2818 |
def SetEditedElementActionBlockInfos(self, tagname, id, infos): |
|
2819 |
element = self.GetEditedElement(tagname) |
|
2820 |
if element is not None: |
|
2821 |
actionBlock = element.getinstance(id) |
|
2822 |
if actionBlock is None: |
|
2823 |
return |
|
2824 |
for param, value in infos.items(): |
|
2825 |
if param == "actions": |
|
2826 |
actionBlock.setactions(value) |
|
2827 |
elif param == "height": |
|
2828 |
actionBlock.setheight(value) |
|
2829 |
elif param == "width": |
|
2830 |
actionBlock.setwidth(value) |
|
2831 |
elif param == "x": |
|
2832 |
actionBlock.setx(value) |
|
2833 |
elif param == "y": |
|
2834 |
actionBlock.sety(value) |
|
2835 |
elif param == "connector": |
|
2836 |
position = value.GetRelPosition() |
|
2837 |
actionBlock.addconnectionPointIn() |
|
2838 |
actionBlock.connectionPointIn.setrelPositionXY(position.x, position.y) |
|
2839 |
self.SetConnectionWires(actionBlock.connectionPointIn, value) |
|
2840 |
||
2841 |
def RemoveEditedElementInstance(self, tagname, id): |
|
2842 |
element = self.GetEditedElement(tagname) |
|
2843 |
if element is not None: |
|
2844 |
instance = element.getinstance(id) |
|
2845 |
if isinstance(instance, plcopen.fbdObjects_block): |
|
2846 |
self.RemoveEditedElementPouVar(tagname, instance.gettypeName(), instance.getinstanceName()) |
|
2847 |
element.removeinstance(id) |
|
2848 |
self.Project.RefreshElementUsingTree() |
|
2849 |
||
2850 |
def GetEditedResourceVariables(self, tagname, debug = False): |
|
2851 |
varlist = [] |
|
2852 |
words = tagname.split("::") |
|
2853 |
for var in self.GetConfigurationGlobalVars(words[1], debug): |
|
2854 |
if var["Type"] == "BOOL": |
|
2855 |
varlist.append(var["Name"]) |
|
2856 |
for var in self.GetConfigurationResourceGlobalVars(words[1], words[2], debug): |
|
2857 |
if var["Type"] == "BOOL": |
|
2858 |
varlist.append(var["Name"]) |
|
2859 |
return varlist |
|
2860 |
||
2861 |
def SetEditedResourceInfos(self, tagname, tasks, instances): |
|
2862 |
resource = self.GetEditedElement(tagname) |
|
2863 |
if resource is not None: |
|
2864 |
resource.settask([]) |
|
2865 |
resource.setpouInstance([]) |
|
2866 |
task_list = {} |
|
2867 |
for task in tasks: |
|
2868 |
new_task = plcopen.resource_task() |
|
2869 |
new_task.setname(task["Name"]) |
|
2870 |
if task["Triggering"] == "Interrupt": |
|
2871 |
new_task.setsingle(task["Single"]) |
|
2872 |
## result = duration_model.match(task["Interval"]).groups() |
|
2873 |
## if reduce(lambda x, y: x or y != None, result): |
|
2874 |
## values = [] |
|
2875 |
## for value in result[:-1]: |
|
2876 |
## if value != None: |
|
2877 |
## values.append(int(value)) |
|
2878 |
## else: |
|
2879 |
## values.append(0) |
|
2880 |
## if result[-1] is not None: |
|
2881 |
## values.append(int(float(result[-1]) * 1000)) |
|
2882 |
## new_task.setinterval(datetime.time(*values)) |
|
2883 |
if task["Triggering"] == "Cyclic": |
|
2884 |
new_task.setinterval(task["Interval"]) |
|
2885 |
new_task.setpriority(int(task["Priority"])) |
|
2886 |
if task["Name"] != "": |
|
2887 |
task_list[task["Name"]] = new_task |
|
2888 |
resource.appendtask(new_task) |
|
2889 |
for instance in instances: |
|
2890 |
new_instance = plcopen.pouInstance() |
|
2891 |
new_instance.setname(instance["Name"]) |
|
2892 |
new_instance.settypeName(instance["Type"]) |
|
2893 |
task_list.get(instance["Task"], resource).appendpouInstance(new_instance) |
|
2894 |
||
2895 |
def GetEditedResourceInfos(self, tagname, debug = False): |
|
2896 |
resource = self.GetEditedElement(tagname, debug) |
|
2897 |
if resource is not None: |
|
2898 |
tasks = resource.gettask() |
|
2899 |
instances = resource.getpouInstance() |
|
2900 |
tasks_data = [] |
|
2901 |
instances_data = [] |
|
2902 |
for task in tasks: |
|
2903 |
new_task = {} |
|
2904 |
new_task["Name"] = task.getname() |
|
2905 |
single = task.getsingle() |
|
2906 |
if single is not None: |
|
2907 |
new_task["Single"] = single |
|
2908 |
else: |
|
2909 |
new_task["Single"] = "" |
|
2910 |
interval = task.getinterval() |
|
2911 |
if interval is not None: |
|
2912 |
## text = "" |
|
2913 |
## if interval.hour != 0: |
|
2914 |
## text += "%dh"%interval.hour |
|
2915 |
## if interval.minute != 0: |
|
2916 |
## text += "%dm"%interval.minute |
|
2917 |
## if interval.second != 0: |
|
2918 |
## text += "%ds"%interval.second |
|
2919 |
## if interval.microsecond != 0: |
|
2920 |
## if interval.microsecond % 1000 != 0: |
|
2921 |
## text += "%.3fms"%(float(interval.microsecond) / 1000) |
|
2922 |
## else: |
|
2923 |
## text += "%dms"%(interval.microsecond / 1000) |
|
2924 |
## new_task["Interval"] = text |
|
2925 |
new_task["Interval"] = interval |
|
2926 |
else: |
|
2927 |
new_task["Interval"] = "" |
|
2928 |
if single is not None and interval is None: |
|
2929 |
new_task["Triggering"] = "Interrupt" |
|
2930 |
elif interval is not None and single is None: |
|
2931 |
new_task["Triggering"] = "Cyclic" |
|
2932 |
else: |
|
2933 |
new_task["Triggering"] = "" |
|
2934 |
new_task["Priority"] = str(task.getpriority()) |
|
2935 |
tasks_data.append(new_task) |
|
2936 |
for instance in task.getpouInstance(): |
|
2937 |
new_instance = {} |
|
2938 |
new_instance["Name"] = instance.getname() |
|
2939 |
new_instance["Type"] = instance.gettypeName() |
|
2940 |
new_instance["Task"] = task.getname() |
|
2941 |
instances_data.append(new_instance) |
|
2942 |
for instance in instances: |
|
2943 |
new_instance = {} |
|
2944 |
new_instance["Name"] = instance.getname() |
|
2945 |
new_instance["Type"] = instance.gettypeName() |
|
2946 |
new_instance["Task"] = "" |
|
2947 |
instances_data.append(new_instance) |
|
2948 |
return tasks_data, instances_data |
|
2949 |
||
2950 |
def OpenXMLFile(self, filepath): |
|
2951 |
xmlfile = open(filepath, 'r') |
|
2952 |
tree = minidom.parse(xmlfile) |
|
2953 |
xmlfile.close() |
|
2954 |
||
2955 |
self.Project = plcopen.project() |
|
2956 |
for child in tree.childNodes: |
|
2957 |
if child.nodeType == tree.ELEMENT_NODE and child.nodeName == "project": |
|
2958 |
try: |
|
2959 |
result = self.Project.loadXMLTree(child) |
|
2960 |
except ValueError, e: |
|
2961 |
return _("Project file syntax error:\n\n") + str(e) |
|
2962 |
self.SetFilePath(filepath) |
|
2963 |
self.Project.RefreshElementUsingTree() |
|
2964 |
self.Project.RefreshDataTypeHierarchy() |
|
2965 |
self.Project.RefreshCustomBlockTypes() |
|
2966 |
self.CreateProjectBuffer(True) |
|
2967 |
self.ProgramChunks = [] |
|
2968 |
self.ProgramOffset = 0 |
|
2969 |
self.NextCompiledProject = self.Copy(self.Project) |
|
2970 |
self.CurrentCompiledProject = None |
|
2971 |
self.Buffering = False |
|
2972 |
self.CurrentElementEditing = None |
|
2973 |
return None |
|
2974 |
return _("No PLC project found") |
|
2975 |
||
2976 |
def SaveXMLFile(self, filepath = None): |
|
2977 |
if not filepath and self.FilePath == "": |
|
2978 |
return False |
|
2979 |
else: |
|
2980 |
contentheader = {"modificationDateTime": datetime.datetime(*localtime()[:6])} |
|
2981 |
self.Project.setcontentHeader(contentheader) |
|
2982 |
||
2983 |
text = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
|
2984 |
extras = {"xmlns" : "http://www.plcopen.org/xml/tc6.xsd", |
|
2985 |
"xmlns:xhtml" : "http://www.w3.org/1999/xhtml", |
|
2986 |
"xmlns:xsi" : "http://www.w3.org/2001/XMLSchema-instance", |
|
2987 |
"xsi:schemaLocation" : "http://www.plcopen.org/xml/tc6.xsd"} |
|
2988 |
text += self.Project.generateXMLText("project", 0, extras) |
|
2989 |
||
2990 |
if filepath: |
|
2991 |
xmlfile = open(filepath,"w") |
|
2992 |
else: |
|
2993 |
xmlfile = open(self.FilePath,"w") |
|
2994 |
xmlfile.write(text.encode("utf-8")) |
|
2995 |
xmlfile.close() |
|
2996 |
self.MarkProjectAsSaved() |
|
2997 |
if filepath: |
|
2998 |
self.SetFilePath(filepath) |
|
2999 |
return True |
|
3000 |
||
3001 |
#------------------------------------------------------------------------------- |
|
3002 |
# Search in Current Project Functions |
|
3003 |
#------------------------------------------------------------------------------- |
|
3004 |
||
3005 |
def SearchInProject(self, criteria): |
|
3006 |
return self.Project.Search(criteria) |
|
3007 |
||
3008 |
def SearchInPou(self, tagname, criteria, debug=False): |
|
3009 |
pou = self.GetEditedElement(tagname, debug) |
|
3010 |
if pou is not None: |
|
3011 |
return pou.Search(criteria) |
|
3012 |
return [] |
|
3013 |
||
3014 |
#------------------------------------------------------------------------------- |
|
3015 |
# Current Buffering Management Functions |
|
3016 |
#------------------------------------------------------------------------------- |
|
3017 |
||
3018 |
""" |
|
3019 |
Return a copy of the project |
|
3020 |
""" |
|
3021 |
def Copy(self, model): |
|
3022 |
return cPickle.loads(cPickle.dumps(model)) |
|
3023 |
||
3024 |
def CreateProjectBuffer(self, saved): |
|
3025 |
if self.ProjectBufferEnabled: |
|
3026 |
self.ProjectBuffer = UndoBuffer(cPickle.dumps(self.Project), saved) |
|
3027 |
else: |
|
3028 |
self.ProjectBuffer = None |
|
3029 |
self.ProjectSaved = saved |
|
3030 |
||
3031 |
def IsProjectBufferEnabled(self): |
|
3032 |
return self.ProjectBufferEnabled |
|
3033 |
||
3034 |
def EnableProjectBuffer(self, enable): |
|
3035 |
self.ProjectBufferEnabled = enable |
|
3036 |
if self.Project is not None: |
|
3037 |
if enable: |
|
3038 |
current_saved = self.ProjectSaved |
|
3039 |
else: |
|
3040 |
current_saved = self.ProjectBuffer.IsCurrentSaved() |
|
3041 |
self.CreateProjectBuffer(current_saved) |
|
3042 |
||
3043 |
def BufferProject(self): |
|
3044 |
if self.ProjectBuffer is not None: |
|
3045 |
self.ProjectBuffer.Buffering(cPickle.dumps(self.Project)) |
|
3046 |
else: |
|
3047 |
self.ProjectSaved = False |
|
3048 |
||
3049 |
def StartBuffering(self): |
|
3050 |
if self.ProjectBuffer is not None: |
|
3051 |
self.Buffering = True |
|
3052 |
else: |
|
3053 |
self.ProjectSaved = False |
|
3054 |
||
3055 |
def EndBuffering(self): |
|
3056 |
if self.ProjectBuffer is not None and self.Buffering: |
|
3057 |
self.ProjectBuffer.Buffering(cPickle.dumps(self.Project)) |
|
3058 |
self.Buffering = False |
|
3059 |
||
3060 |
def MarkProjectAsSaved(self): |
|
3061 |
self.EndBuffering() |
|
3062 |
if self.ProjectBuffer is not None: |
|
3063 |
self.ProjectBuffer.CurrentSaved() |
|
3064 |
else: |
|
3065 |
self.ProjectSaved = True |
|
3066 |
||
3067 |
# Return if project is saved |
|
3068 |
def ProjectIsSaved(self): |
|
3069 |
if self.ProjectBuffer is not None: |
|
3070 |
return self.ProjectBuffer.IsCurrentSaved() and not self.Buffering |
|
3071 |
else: |
|
3072 |
return self.ProjectSaved |
|
3073 |
||
3074 |
def LoadPrevious(self): |
|
3075 |
self.EndBuffering() |
|
3076 |
if self.ProjectBuffer is not None: |
|
3077 |
self.Project = cPickle.loads(self.ProjectBuffer.Previous()) |
|
3078 |
||
3079 |
def LoadNext(self): |
|
3080 |
if self.ProjectBuffer is not None: |
|
3081 |
self.Project = cPickle.loads(self.ProjectBuffer.Next()) |
|
3082 |
||
3083 |
def GetBufferState(self): |
|
3084 |
if self.ProjectBuffer is not None: |
|
3085 |
first = self.ProjectBuffer.IsFirst() and not self.Buffering |
|
3086 |
last = self.ProjectBuffer.IsLast() |
|
3087 |
return not first, not last |
|
3088 |
return False, False |