author | Laurent Bessard |
Wed, 05 Jun 2013 23:13:33 +0200 | |
changeset 1223 | d51cea72baa7 |
parent 1222 | 775b48a2be3b |
child 1254 | ebc765355536 |
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 |
||
1171
a506e4de8f84
Add support for Drag'n dropping located variables to function block creating global located variable in configuration and external variable in function block
Laurent Bessard
parents:
1127
diff
changeset
|
269 |
# Return project pou variable names |
a506e4de8f84
Add support for Drag'n dropping located variables to function block creating global located variable in configuration and external variable in function block
Laurent Bessard
parents:
1127
diff
changeset
|
270 |
def GetProjectPouVariableNames(self, pou_name = None, debug = False): |
814 | 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) |
|
1222
775b48a2be3b
Fixed flickering and lag when refreshing PouInstanceVariablesPanel
Laurent Bessard
parents:
1186
diff
changeset
|
564 |
if pou is not None and project.ElementIsUsedBy(pou_type, var_type): |
814 | 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) |
|
1222
775b48a2be3b
Fixed flickering and lag when refreshing PouInstanceVariablesPanel
Laurent Bessard
parents:
1186
diff
changeset
|
599 |
if pou is not None and project.ElementIsUsedBy(words[1], pou_type): |
814 | 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 |
||
886
ace92afe9100
Fix bug debug variables from standard and library function blocks unregistered when transferring program
Laurent Bessard
parents:
884
diff
changeset
|
616 |
def RecursiveGetPouInstanceTagName(self, project, pou_type, parts, debug = False): |
814 | 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(), |
886
ace92afe9100
Fix bug debug variables from standard and library function blocks unregistered when transferring program
Laurent Bessard
parents:
884
diff
changeset
|
630 |
parts[1:], debug) |
827
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]) |
886
ace92afe9100
Fix bug debug variables from standard and library function blocks unregistered when transferring program
Laurent Bessard
parents:
884
diff
changeset
|
639 |
else: |
ace92afe9100
Fix bug debug variables from standard and library function blocks unregistered when transferring program
Laurent Bessard
parents:
884
diff
changeset
|
640 |
block_infos = self.GetBlockType(pou_type, debug=debug) |
ace92afe9100
Fix bug debug variables from standard and library function blocks unregistered when transferring program
Laurent Bessard
parents:
884
diff
changeset
|
641 |
if (block_infos is not None and |
ace92afe9100
Fix bug debug variables from standard and library function blocks unregistered when transferring program
Laurent Bessard
parents:
884
diff
changeset
|
642 |
block_infos["type"] in ["program", "functionBlock"]): |
ace92afe9100
Fix bug debug variables from standard and library function blocks unregistered when transferring program
Laurent Bessard
parents:
884
diff
changeset
|
643 |
|
ace92afe9100
Fix bug debug variables from standard and library function blocks unregistered when transferring program
Laurent Bessard
parents:
884
diff
changeset
|
644 |
if len(parts) == 0: |
ace92afe9100
Fix bug debug variables from standard and library function blocks unregistered when transferring program
Laurent Bessard
parents:
884
diff
changeset
|
645 |
return self.ComputePouName(pou_type) |
ace92afe9100
Fix bug debug variables from standard and library function blocks unregistered when transferring program
Laurent Bessard
parents:
884
diff
changeset
|
646 |
|
ace92afe9100
Fix bug debug variables from standard and library function blocks unregistered when transferring program
Laurent Bessard
parents:
884
diff
changeset
|
647 |
for varname, vartype, varmodifier in block_infos["inputs"] + block_infos["outputs"]: |
ace92afe9100
Fix bug debug variables from standard and library function blocks unregistered when transferring program
Laurent Bessard
parents:
884
diff
changeset
|
648 |
if varname == parts[0]: |
ace92afe9100
Fix bug debug variables from standard and library function blocks unregistered when transferring program
Laurent Bessard
parents:
884
diff
changeset
|
649 |
return self.RecursiveGetPouInstanceTagName(project, vartype, parts[1:], debug) |
814 | 650 |
return None |
651 |
||
652 |
def GetPouInstanceTagName(self, instance_path, debug = False): |
|
653 |
parts = instance_path.split(".") |
|
654 |
if len(parts) == 1: |
|
655 |
return self.ComputeConfigurationName(parts[0]) |
|
656 |
elif len(parts) == 2: |
|
657 |
return self.ComputeConfigurationResourceName(parts[0], parts[1]) |
|
658 |
else: |
|
659 |
project = self.GetProject(debug) |
|
660 |
for config in project.getconfigurations(): |
|
661 |
if config.getname() == parts[0]: |
|
662 |
for resource in config.getresource(): |
|
663 |
if resource.getname() == parts[1]: |
|
664 |
pou_instances = resource.getpouInstance()[:] |
|
665 |
for task in resource.gettask(): |
|
666 |
pou_instances.extend(task.getpouInstance()) |
|
667 |
for pou_instance in pou_instances: |
|
668 |
if pou_instance.getname() == parts[2]: |
|
669 |
if len(parts) == 3: |
|
670 |
return self.ComputePouName( |
|
671 |
pou_instance.gettypeName()) |
|
672 |
else: |
|
673 |
return self.RecursiveGetPouInstanceTagName( |
|
674 |
project, |
|
675 |
pou_instance.gettypeName(), |
|
886
ace92afe9100
Fix bug debug variables from standard and library function blocks unregistered when transferring program
Laurent Bessard
parents:
884
diff
changeset
|
676 |
parts[3:], debug) |
814 | 677 |
return None |
678 |
||
679 |
def GetInstanceInfos(self, instance_path, debug = False): |
|
680 |
tagname = self.GetPouInstanceTagName(instance_path) |
|
681 |
if tagname is not None: |
|
827
a2ce084fb598
Fix restore project tab layout with transition and action debug tabs
laurent
parents:
826
diff
changeset
|
682 |
infos = self.GetPouVariables(tagname, debug) |
a2ce084fb598
Fix restore project tab layout with transition and action debug tabs
laurent
parents:
826
diff
changeset
|
683 |
infos["type"] = tagname |
a2ce084fb598
Fix restore project tab layout with transition and action debug tabs
laurent
parents:
826
diff
changeset
|
684 |
return infos |
814 | 685 |
else: |
686 |
pou_path, var_name = instance_path.rsplit(".", 1) |
|
687 |
tagname = self.GetPouInstanceTagName(pou_path) |
|
688 |
if tagname is not None: |
|
689 |
pou_infos = self.GetPouVariables(tagname, debug) |
|
690 |
for var_infos in pou_infos["variables"]: |
|
691 |
if var_infos["name"] == var_name: |
|
692 |
return var_infos |
|
693 |
return None |
|
694 |
||
695 |
# Return if data type given by name is used by another data type or pou |
|
696 |
def DataTypeIsUsed(self, name, debug = False): |
|
697 |
project = self.GetProject(debug) |
|
698 |
if project is not None: |
|
699 |
return project.ElementIsUsed(name) or project.DataTypeIsDerived(name) |
|
700 |
return False |
|
701 |
||
702 |
# Return if pou given by name is used by another pou |
|
703 |
def PouIsUsed(self, name, debug = False): |
|
704 |
project = self.GetProject(debug) |
|
705 |
if project is not None: |
|
706 |
return project.ElementIsUsed(name) |
|
707 |
return False |
|
708 |
||
709 |
# Return if pou given by name is directly or undirectly used by the reference pou |
|
710 |
def PouIsUsedBy(self, name, reference, debug = False): |
|
711 |
project = self.GetProject(debug) |
|
712 |
if project is not None: |
|
713 |
return project.ElementIsUsedBy(name, reference) |
|
714 |
return False |
|
715 |
||
716 |
def GenerateProgram(self, filepath=None): |
|
717 |
errors = [] |
|
718 |
warnings = [] |
|
719 |
if self.Project is not None: |
|
720 |
try: |
|
721 |
self.ProgramChunks = GenerateCurrentProgram(self, self.Project, errors, warnings) |
|
722 |
self.NextCompiledProject = self.Copy(self.Project) |
|
901
ab43f3e40b9d
Fix bug when compiling project containing non-ascii characters
Laurent Bessard
parents:
887
diff
changeset
|
723 |
program_text = "".join([item[0] for item in self.ProgramChunks]) |
814 | 724 |
if filepath is not None: |
725 |
programfile = open(filepath, "w") |
|
726 |
programfile.write(program_text.encode("utf-8")) |
|
727 |
programfile.close() |
|
728 |
self.ProgramFilePath = filepath |
|
729 |
return program_text, errors, warnings |
|
730 |
except PLCGenException, e: |
|
731 |
errors.append(e.message) |
|
732 |
else: |
|
733 |
errors.append("No project opened") |
|
734 |
return "", errors, warnings |
|
735 |
||
736 |
def DebugAvailable(self): |
|
737 |
return self.CurrentCompiledProject is not None |
|
738 |
||
739 |
def ProgramTransferred(self): |
|
740 |
if self.NextCompiledProject is None: |
|
741 |
self.CurrentCompiledProject = self.NextCompiledProject |
|
742 |
else: |
|
743 |
self.CurrentCompiledProject = self.Copy(self.Project) |
|
744 |
||
745 |
def GetChunkInfos(self, from_location, to_location): |
|
746 |
row = self.ProgramOffset + 1 |
|
747 |
col = 1 |
|
748 |
infos = [] |
|
749 |
for chunk, chunk_infos in self.ProgramChunks: |
|
750 |
lines = chunk.split("\n") |
|
751 |
if len(lines) > 1: |
|
752 |
next_row = row + len(lines) - 1 |
|
753 |
next_col = len(lines[-1]) + 1 |
|
754 |
else: |
|
755 |
next_row = row |
|
756 |
next_col = col + len(chunk) |
|
757 |
if (next_row > from_location[0] or next_row == from_location[0] and next_col >= from_location[1]) and len(chunk_infos) > 0: |
|
758 |
infos.append((chunk_infos, (row, col))) |
|
759 |
if next_row == to_location[0] and next_col > to_location[1] or next_row > to_location[0]: |
|
760 |
return infos |
|
761 |
row, col = next_row, next_col |
|
762 |
return infos |
|
763 |
||
764 |
#------------------------------------------------------------------------------- |
|
765 |
# Project Pous management functions |
|
766 |
#------------------------------------------------------------------------------- |
|
767 |
||
768 |
# Add a Data Type to Project |
|
769 |
def ProjectAddDataType(self, datatype_name=None): |
|
770 |
if self.Project is not None: |
|
771 |
if datatype_name is None: |
|
772 |
datatype_name = self.GenerateNewName(None, None, "datatype%d") |
|
773 |
# Add the datatype to project |
|
774 |
self.Project.appenddataType(datatype_name) |
|
775 |
self.BufferProject() |
|
776 |
return self.ComputeDataTypeName(datatype_name) |
|
777 |
return None |
|
778 |
||
779 |
# Remove a Data Type from project |
|
780 |
def ProjectRemoveDataType(self, datatype_name): |
|
781 |
if self.Project is not None: |
|
782 |
self.Project.removedataType(datatype_name) |
|
783 |
self.BufferProject() |
|
784 |
||
785 |
# Add a Pou to Project |
|
786 |
def ProjectAddPou(self, pou_name, pou_type, body_type): |
|
787 |
if self.Project is not None: |
|
788 |
# Add the pou to project |
|
789 |
self.Project.appendpou(pou_name, pou_type, body_type) |
|
790 |
if pou_type == "function": |
|
791 |
self.SetPouInterfaceReturnType(pou_name, "BOOL") |
|
792 |
self.BufferProject() |
|
793 |
return self.ComputePouName(pou_name) |
|
794 |
return None |
|
795 |
||
796 |
def ProjectChangePouType(self, name, pou_type): |
|
797 |
if self.Project is not None: |
|
798 |
pou = self.Project.getpou(name) |
|
799 |
if pou is not None: |
|
800 |
pou.setpouType(pou_type) |
|
801 |
self.Project.RefreshCustomBlockTypes() |
|
802 |
self.BufferProject() |
|
803 |
||
804 |
def GetPouXml(self, pou_name): |
|
805 |
if self.Project is not None: |
|
806 |
pou = self.Project.getpou(pou_name) |
|
807 |
if pou is not None: |
|
808 |
return pou.generateXMLText('pou', 0) |
|
809 |
return None |
|
810 |
||
811 |
def PastePou(self, pou_type, pou_xml): |
|
812 |
''' |
|
813 |
Adds the POU defined by 'pou_xml' to the current project with type 'pou_type' |
|
814 |
''' |
|
815 |
try: |
|
816 |
tree = minidom.parseString(pou_xml.encode("utf-8")) |
|
817 |
root = tree.childNodes[0] |
|
818 |
except: |
|
819 |
return _("Couldn't paste non-POU object.") |
|
820 |
||
821 |
if root.nodeName == "pou": |
|
822 |
new_pou = plcopen.pous_pou() |
|
823 |
new_pou.loadXMLTree(root) |
|
824 |
||
825 |
name = new_pou.getname() |
|
826 |
||
827 |
idx = 0 |
|
828 |
new_name = name |
|
829 |
while self.Project.getpou(new_name): |
|
830 |
# a POU with that name already exists. |
|
831 |
# make a new name and test if a POU with that name exists. |
|
832 |
# append an incrementing numeric suffix to the POU name. |
|
833 |
idx += 1 |
|
834 |
new_name = "%s%d" % (name, idx) |
|
835 |
||
836 |
# we've found a name that does not already exist, use it |
|
837 |
new_pou.setname(new_name) |
|
838 |
||
839 |
if pou_type is not None: |
|
840 |
orig_type = new_pou.getpouType() |
|
841 |
||
842 |
# prevent violations of POU content restrictions: |
|
843 |
# function blocks cannot be pasted as functions, |
|
844 |
# programs cannot be pasted as functions or function blocks |
|
845 |
if orig_type == 'functionBlock' and pou_type == 'function' or \ |
|
846 |
orig_type == 'program' and pou_type in ['function', 'functionBlock']: |
|
847 |
return _('''%s "%s" can't be pasted as a %s.''') % (orig_type, name, pou_type) |
|
848 |
||
849 |
new_pou.setpouType(pou_type) |
|
850 |
||
851 |
self.Project.insertpou(-1, new_pou) |
|
852 |
self.BufferProject() |
|
853 |
||
854 |
return self.ComputePouName(new_name), |
|
855 |
else: |
|
856 |
return _("Couldn't paste non-POU object.") |
|
857 |
||
858 |
# Remove a Pou from project |
|
859 |
def ProjectRemovePou(self, pou_name): |
|
860 |
if self.Project is not None: |
|
861 |
self.Project.removepou(pou_name) |
|
862 |
self.BufferProject() |
|
863 |
||
864 |
# Return the name of the configuration if only one exist |
|
865 |
def GetProjectMainConfigurationName(self): |
|
866 |
if self.Project is not None: |
|
867 |
# Found the configuration corresponding to old name and change its name to new name |
|
868 |
configurations = self.Project.getconfigurations() |
|
869 |
if len(configurations) == 1: |
|
870 |
return configurations[0].getname() |
|
871 |
return None |
|
872 |
||
873 |
# Add a configuration to Project |
|
874 |
def ProjectAddConfiguration(self, config_name=None): |
|
875 |
if self.Project is not None: |
|
876 |
if config_name is None: |
|
877 |
config_name = self.GenerateNewName(None, None, "configuration%d") |
|
878 |
self.Project.addconfiguration(config_name) |
|
879 |
self.BufferProject() |
|
880 |
return self.ComputeConfigurationName(config_name) |
|
881 |
return None |
|
882 |
||
883 |
# Remove a configuration from project |
|
884 |
def ProjectRemoveConfiguration(self, config_name): |
|
885 |
if self.Project is not None: |
|
886 |
self.Project.removeconfiguration(config_name) |
|
887 |
self.BufferProject() |
|
888 |
||
889 |
# Add a resource to a configuration of the Project |
|
890 |
def ProjectAddConfigurationResource(self, config_name, resource_name=None): |
|
891 |
if self.Project is not None: |
|
892 |
if resource_name is None: |
|
893 |
resource_name = self.GenerateNewName(None, None, "resource%d") |
|
894 |
self.Project.addconfigurationResource(config_name, resource_name) |
|
895 |
self.BufferProject() |
|
896 |
return self.ComputeConfigurationResourceName(config_name, resource_name) |
|
897 |
return None |
|
898 |
||
899 |
# Remove a resource from a configuration of the project |
|
900 |
def ProjectRemoveConfigurationResource(self, config_name, resource_name): |
|
901 |
if self.Project is not None: |
|
902 |
self.Project.removeconfigurationResource(config_name, resource_name) |
|
903 |
self.BufferProject() |
|
904 |
||
905 |
# Add a Transition to a Project Pou |
|
906 |
def ProjectAddPouTransition(self, pou_name, transition_name, transition_type): |
|
907 |
if self.Project is not None: |
|
908 |
pou = self.Project.getpou(pou_name) |
|
909 |
if pou is not None: |
|
910 |
pou.addtransition(transition_name, transition_type) |
|
911 |
self.BufferProject() |
|
912 |
return self.ComputePouTransitionName(pou_name, transition_name) |
|
913 |
return None |
|
914 |
||
915 |
# Remove a Transition from a Project Pou |
|
916 |
def ProjectRemovePouTransition(self, pou_name, transition_name): |
|
917 |
# Search if the pou removed is currently opened |
|
918 |
if self.Project is not None: |
|
919 |
pou = self.Project.getpou(pou_name) |
|
920 |
if pou is not None: |
|
921 |
pou.removetransition(transition_name) |
|
922 |
self.BufferProject() |
|
923 |
||
924 |
# Add an Action to a Project Pou |
|
925 |
def ProjectAddPouAction(self, pou_name, action_name, action_type): |
|
926 |
if self.Project is not None: |
|
927 |
pou = self.Project.getpou(pou_name) |
|
928 |
if pou is not None: |
|
929 |
pou.addaction(action_name, action_type) |
|
930 |
self.BufferProject() |
|
931 |
return self.ComputePouActionName(pou_name, action_name) |
|
932 |
return None |
|
933 |
||
934 |
# Remove an Action from a Project Pou |
|
935 |
def ProjectRemovePouAction(self, pou_name, action_name): |
|
936 |
# Search if the pou removed is currently opened |
|
937 |
if self.Project is not None: |
|
938 |
pou = self.Project.getpou(pou_name) |
|
939 |
if pou is not None: |
|
940 |
pou.removeaction(action_name) |
|
941 |
self.BufferProject() |
|
942 |
||
943 |
# Change the name of a pou |
|
944 |
def ChangeDataTypeName(self, old_name, new_name): |
|
945 |
if self.Project is not None: |
|
946 |
# Found the pou corresponding to old name and change its name to new name |
|
947 |
datatype = self.Project.getdataType(old_name) |
|
948 |
if datatype is not None: |
|
949 |
datatype.setname(new_name) |
|
950 |
self.Project.updateElementName(old_name, new_name) |
|
951 |
self.Project.RefreshElementUsingTree() |
|
952 |
self.Project.RefreshDataTypeHierarchy() |
|
953 |
self.BufferProject() |
|
954 |
||
955 |
# Change the name of a pou |
|
956 |
def ChangePouName(self, old_name, new_name): |
|
957 |
if self.Project is not None: |
|
958 |
# Found the pou corresponding to old name and change its name to new name |
|
959 |
pou = self.Project.getpou(old_name) |
|
960 |
if pou is not None: |
|
961 |
pou.setname(new_name) |
|
962 |
self.Project.updateElementName(old_name, new_name) |
|
963 |
self.Project.RefreshElementUsingTree() |
|
964 |
self.Project.RefreshCustomBlockTypes() |
|
965 |
self.BufferProject() |
|
966 |
||
967 |
# Change the name of a pou transition |
|
968 |
def ChangePouTransitionName(self, pou_name, old_name, new_name): |
|
969 |
if self.Project is not None: |
|
970 |
# Found the pou transition corresponding to old name and change its name to new name |
|
971 |
pou = self.Project.getpou(pou_name) |
|
972 |
if pou is not None: |
|
973 |
transition = pou.gettransition(old_name) |
|
974 |
if transition is not None: |
|
975 |
transition.setname(new_name) |
|
976 |
pou.updateElementName(old_name, new_name) |
|
977 |
self.BufferProject() |
|
978 |
||
979 |
# Change the name of a pou action |
|
980 |
def ChangePouActionName(self, pou_name, old_name, new_name): |
|
981 |
if self.Project is not None: |
|
982 |
# Found the pou action corresponding to old name and change its name to new name |
|
983 |
pou = self.Project.getpou(pou_name) |
|
984 |
if pou is not None: |
|
985 |
action = pou.getaction(old_name) |
|
986 |
if action is not None: |
|
987 |
action.setname(new_name) |
|
988 |
pou.updateElementName(old_name, new_name) |
|
989 |
self.BufferProject() |
|
990 |
||
991 |
# Change the name of a pou variable |
|
992 |
def ChangePouVariableName(self, pou_name, old_name, new_name): |
|
993 |
if self.Project is not None: |
|
994 |
# Found the pou action corresponding to old name and change its name to new name |
|
995 |
pou = self.Project.getpou(pou_name) |
|
996 |
if pou is not None: |
|
997 |
for type, varlist in pou.getvars(): |
|
998 |
for var in varlist.getvariable(): |
|
999 |
if var.getname() == old_name: |
|
1000 |
var.setname(new_name) |
|
1001 |
self.Project.RefreshCustomBlockTypes() |
|
1002 |
self.BufferProject() |
|
1003 |
||
1004 |
# Change the name of a configuration |
|
1005 |
def ChangeConfigurationName(self, old_name, new_name): |
|
1006 |
if self.Project is not None: |
|
1007 |
# Found the configuration corresponding to old name and change its name to new name |
|
1008 |
configuration = self.Project.getconfiguration(old_name) |
|
1009 |
if configuration is not None: |
|
1010 |
configuration.setname(new_name) |
|
1011 |
self.BufferProject() |
|
1012 |
||
1013 |
# Change the name of a configuration resource |
|
1014 |
def ChangeConfigurationResourceName(self, config_name, old_name, new_name): |
|
1015 |
if self.Project is not None: |
|
1016 |
# Found the resource corresponding to old name and change its name to new name |
|
1017 |
resource = self.Project.getconfigurationResource(config_name, old_name) |
|
1018 |
if resource is not None: |
|
1019 |
resource.setname(new_name) |
|
1020 |
self.BufferProject() |
|
1021 |
||
1022 |
# Return the description of the pou given by its name |
|
1023 |
def GetPouDescription(self, name, debug = False): |
|
1024 |
project = self.GetProject(debug) |
|
1025 |
if project is not None: |
|
1026 |
# Found the pou correponding to name and return its type |
|
1027 |
pou = project.getpou(name) |
|
1028 |
if pou is not None: |
|
1029 |
return pou.getdescription() |
|
1030 |
return "" |
|
1031 |
||
1032 |
# Return the description of the pou given by its name |
|
1033 |
def SetPouDescription(self, name, description, 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 |
pou.setdescription(description) |
|
1040 |
project.RefreshCustomBlockTypes() |
|
1041 |
self.BufferProject() |
|
1042 |
||
1043 |
# Return the type of the pou given by its name |
|
1044 |
def GetPouType(self, name, debug = False): |
|
1045 |
project = self.GetProject(debug) |
|
1046 |
if project is not None: |
|
1047 |
# Found the pou correponding to name and return its type |
|
1048 |
pou = project.getpou(name) |
|
1049 |
if pou is not None: |
|
1050 |
return pou.getpouType() |
|
1051 |
return None |
|
1052 |
||
1053 |
# Return pous with SFC language |
|
1054 |
def GetSFCPous(self, debug = False): |
|
1055 |
list = [] |
|
1056 |
project = self.GetProject(debug) |
|
1057 |
if project is not None: |
|
1058 |
for pou in project.getpous(): |
|
1059 |
if pou.getBodyType() == "SFC": |
|
1060 |
list.append(pou.getname()) |
|
1061 |
return list |
|
1062 |
||
1063 |
# Return the body language of the pou given by its name |
|
1064 |
def GetPouBodyType(self, name, debug = False): |
|
1065 |
project = self.GetProject(debug) |
|
1066 |
if project is not None: |
|
1067 |
# Found the pou correponding to name and return its body language |
|
1068 |
pou = project.getpou(name) |
|
1069 |
if pou is not None: |
|
1070 |
return pou.getbodyType() |
|
1071 |
return None |
|
1072 |
||
1073 |
# Return the actions of a pou |
|
1074 |
def GetPouTransitions(self, pou_name, debug = False): |
|
1075 |
transitions = [] |
|
1076 |
project = self.GetProject(debug) |
|
1077 |
if project is not None: |
|
1078 |
# Found the pou correponding to name and return its transitions if SFC |
|
1079 |
pou = project.getpou(pou_name) |
|
1080 |
if pou is not None and pou.getbodyType() == "SFC": |
|
1081 |
for transition in pou.gettransitionList(): |
|
1082 |
transitions.append(transition.getname()) |
|
1083 |
return transitions |
|
1084 |
||
1085 |
# Return the body language of the transition given by its name |
|
1086 |
def GetTransitionBodyType(self, pou_name, pou_transition, debug = False): |
|
1087 |
project = self.GetProject(debug) |
|
1088 |
if project is not None: |
|
1089 |
# Found the pou correponding to name |
|
1090 |
pou = project.getpou(pou_name) |
|
1091 |
if pou is not None: |
|
1092 |
# Found the pou transition correponding to name and return its body language |
|
1093 |
transition = pou.gettransition(pou_transition) |
|
1094 |
if transition is not None: |
|
1095 |
return transition.getbodyType() |
|
1096 |
return None |
|
1097 |
||
1098 |
# Return the actions of a pou |
|
1099 |
def GetPouActions(self, pou_name, debug = False): |
|
1100 |
actions = [] |
|
1101 |
project = self.GetProject(debug) |
|
1102 |
if project is not None: |
|
1103 |
# Found the pou correponding to name and return its actions if SFC |
|
1104 |
pou = project.getpou(pou_name) |
|
1105 |
if pou.getbodyType() == "SFC": |
|
1106 |
for action in pou.getactionList(): |
|
1107 |
actions.append(action.getname()) |
|
1108 |
return actions |
|
1109 |
||
1110 |
# Return the body language of the pou given by its name |
|
1111 |
def GetActionBodyType(self, pou_name, pou_action, debug = False): |
|
1112 |
project = self.GetProject(debug) |
|
1113 |
if project is not None: |
|
1114 |
# Found the pou correponding to name and return its body language |
|
1115 |
pou = project.getpou(pou_name) |
|
1116 |
if pou is not None: |
|
1117 |
action = pou.getaction(pou_action) |
|
1118 |
if action is not None: |
|
1119 |
return action.getbodyType() |
|
1120 |
return None |
|
1121 |
||
1122 |
# Extract varlists from a list of vars |
|
1123 |
def ExtractVarLists(self, vars): |
|
1124 |
varlist_list = [] |
|
1125 |
current_varlist = None |
|
1126 |
current_type = None |
|
1127 |
for var in vars: |
|
1128 |
next_type = (var["Class"], |
|
1129 |
var["Option"], |
|
1130 |
var["Location"] in ["", None] or |
|
1131 |
# When declaring globals, located |
|
1132 |
# and not located variables are |
|
1133 |
# in the same declaration block |
|
1134 |
var["Class"] == "Global") |
|
1135 |
if current_type != next_type: |
|
1136 |
current_type = next_type |
|
1137 |
infos = VAR_CLASS_INFOS.get(var["Class"], None) |
|
1138 |
if infos is not None: |
|
1139 |
current_varlist = infos[0]() |
|
1140 |
else: |
|
1141 |
current_varlist = plcopen.varList() |
|
1142 |
varlist_list.append((var["Class"], current_varlist)) |
|
1143 |
if var["Option"] == "Constant": |
|
1144 |
current_varlist.setconstant(True) |
|
1145 |
elif var["Option"] == "Retain": |
|
1146 |
current_varlist.setretain(True) |
|
1147 |
elif var["Option"] == "Non-Retain": |
|
1148 |
current_varlist.setnonretain(True) |
|
1149 |
# Create variable and change its properties |
|
1150 |
tempvar = plcopen.varListPlain_variable() |
|
1151 |
tempvar.setname(var["Name"]) |
|
1152 |
||
1153 |
var_type = plcopen.dataType() |
|
1154 |
if isinstance(var["Type"], TupleType): |
|
1155 |
if var["Type"][0] == "array": |
|
1156 |
array_type, base_type_name, dimensions = var["Type"] |
|
1157 |
array = plcopen.derivedTypes_array() |
|
1158 |
for i, dimension in enumerate(dimensions): |
|
1159 |
dimension_range = plcopen.rangeSigned() |
|
1160 |
dimension_range.setlower(dimension[0]) |
|
1161 |
dimension_range.setupper(dimension[1]) |
|
1162 |
if i == 0: |
|
1163 |
array.setdimension([dimension_range]) |
|
1164 |
else: |
|
1165 |
array.appenddimension(dimension_range) |
|
1166 |
if base_type_name in self.GetBaseTypes(): |
|
1167 |
if base_type_name == "STRING": |
|
1168 |
array.baseType.setcontent({"name" : "string", "value" : plcopen.elementaryTypes_string()}) |
|
1169 |
elif base_type_name == "WSTRING": |
|
1170 |
array.baseType.setcontent({"name" : "wstring", "value" : plcopen.wstring()}) |
|
1171 |
else: |
|
1172 |
array.baseType.setcontent({"name" : base_type_name, "value" : None}) |
|
1173 |
else: |
|
1174 |
derived_datatype = plcopen.derivedTypes_derived() |
|
1175 |
derived_datatype.setname(base_type_name) |
|
1176 |
array.baseType.setcontent({"name" : "derived", "value" : derived_datatype}) |
|
1177 |
var_type.setcontent({"name" : "array", "value" : array}) |
|
1178 |
elif var["Type"] in self.GetBaseTypes(): |
|
1179 |
if var["Type"] == "STRING": |
|
1180 |
var_type.setcontent({"name" : "string", "value" : plcopen.elementaryTypes_string()}) |
|
1181 |
elif var["Type"] == "WSTRING": |
|
1182 |
var_type.setcontent({"name" : "wstring", "value" : plcopen.elementaryTypes_wstring()}) |
|
1183 |
else: |
|
1184 |
var_type.setcontent({"name" : var["Type"], "value" : None}) |
|
1185 |
else: |
|
1186 |
derived_type = plcopen.derivedTypes_derived() |
|
1187 |
derived_type.setname(var["Type"]) |
|
1188 |
var_type.setcontent({"name" : "derived", "value" : derived_type}) |
|
1189 |
tempvar.settype(var_type) |
|
1190 |
||
1191 |
if var["Initial Value"] != "": |
|
1192 |
value = plcopen.value() |
|
1193 |
value.setvalue(var["Initial Value"]) |
|
1194 |
tempvar.setinitialValue(value) |
|
1195 |
if var["Location"] != "": |
|
1196 |
tempvar.setaddress(var["Location"]) |
|
1197 |
else: |
|
1198 |
tempvar.setaddress(None) |
|
1199 |
if var['Documentation'] != "": |
|
1200 |
ft = plcopen.formattedText() |
|
1201 |
ft.settext(var['Documentation']) |
|
1202 |
tempvar.setdocumentation(ft) |
|
1203 |
||
1204 |
# Add variable to varList |
|
1205 |
current_varlist.appendvariable(tempvar) |
|
1206 |
return varlist_list |
|
1207 |
||
1208 |
def GetVariableDictionary(self, varlist, var): |
|
1209 |
''' |
|
1210 |
convert a PLC variable to the dictionary representation |
|
1211 |
returned by Get*Vars) |
|
1212 |
''' |
|
1213 |
||
1214 |
tempvar = {"Name": var.getname()} |
|
1215 |
||
1216 |
vartype_content = var.gettype().getcontent() |
|
1217 |
if vartype_content["name"] == "derived": |
|
1218 |
tempvar["Type"] = vartype_content["value"].getname() |
|
1219 |
elif vartype_content["name"] == "array": |
|
1220 |
dimensions = [] |
|
1221 |
for dimension in vartype_content["value"].getdimension(): |
|
1222 |
dimensions.append((dimension.getlower(), dimension.getupper())) |
|
1223 |
base_type = vartype_content["value"].baseType.getcontent() |
|
864
bf4f7f0801b9
Adding support for direct array declaration in structure element declaration
Laurent Bessard
parents:
863
diff
changeset
|
1224 |
if base_type["value"] is None or base_type["name"] in ["string", "wstring"]: |
bf4f7f0801b9
Adding support for direct array declaration in structure element declaration
Laurent Bessard
parents:
863
diff
changeset
|
1225 |
base_type_name = base_type["name"].upper() |
814 | 1226 |
else: |
1227 |
base_type_name = base_type["value"].getname() |
|
1228 |
tempvar["Type"] = ("array", base_type_name, dimensions) |
|
1229 |
elif vartype_content["name"] in ["string", "wstring"]: |
|
1230 |
tempvar["Type"] = vartype_content["name"].upper() |
|
1231 |
else: |
|
1232 |
tempvar["Type"] = vartype_content["name"] |
|
1233 |
||
1234 |
tempvar["Edit"] = True |
|
1235 |
||
1236 |
initial = var.getinitialValue() |
|
1237 |
if initial: |
|
1238 |
tempvar["Initial Value"] = initial.getvalue() |
|
1239 |
else: |
|
1240 |
tempvar["Initial Value"] = "" |
|
1241 |
||
1242 |
address = var.getaddress() |
|
1243 |
if address: |
|
1244 |
tempvar["Location"] = address |
|
1245 |
else: |
|
1246 |
tempvar["Location"] = "" |
|
1247 |
||
1248 |
if varlist.getconstant(): |
|
1249 |
tempvar["Option"] = "Constant" |
|
1250 |
elif varlist.getretain(): |
|
1251 |
tempvar["Option"] = "Retain" |
|
1252 |
elif varlist.getnonretain(): |
|
1253 |
tempvar["Option"] = "Non-Retain" |
|
1254 |
else: |
|
1255 |
tempvar["Option"] = "" |
|
1256 |
||
1257 |
doc = var.getdocumentation() |
|
1258 |
if doc: |
|
1259 |
tempvar["Documentation"] = doc.gettext() |
|
1260 |
else: |
|
1261 |
tempvar["Documentation"] = "" |
|
1262 |
||
1263 |
return tempvar |
|
1171
a506e4de8f84
Add support for Drag'n dropping located variables to function block creating global located variable in configuration and external variable in function block
Laurent Bessard
parents:
1127
diff
changeset
|
1264 |
|
a506e4de8f84
Add support for Drag'n dropping located variables to function block creating global located variable in configuration and external variable in function block
Laurent Bessard
parents:
1127
diff
changeset
|
1265 |
# Add a global var to configuration to configuration |
a506e4de8f84
Add support for Drag'n dropping located variables to function block creating global located variable in configuration and external variable in function block
Laurent Bessard
parents:
1127
diff
changeset
|
1266 |
def AddConfigurationGlobalVar(self, config_name, type, var_name, |
a506e4de8f84
Add support for Drag'n dropping located variables to function block creating global located variable in configuration and external variable in function block
Laurent Bessard
parents:
1127
diff
changeset
|
1267 |
location="", description=""): |
a506e4de8f84
Add support for Drag'n dropping located variables to function block creating global located variable in configuration and external variable in function block
Laurent Bessard
parents:
1127
diff
changeset
|
1268 |
if self.Project is not None: |
a506e4de8f84
Add support for Drag'n dropping located variables to function block creating global located variable in configuration and external variable in function block
Laurent Bessard
parents:
1127
diff
changeset
|
1269 |
# Found the configuration corresponding to name |
a506e4de8f84
Add support for Drag'n dropping located variables to function block creating global located variable in configuration and external variable in function block
Laurent Bessard
parents:
1127
diff
changeset
|
1270 |
configuration = self.Project.getconfiguration(config_name) |
a506e4de8f84
Add support for Drag'n dropping located variables to function block creating global located variable in configuration and external variable in function block
Laurent Bessard
parents:
1127
diff
changeset
|
1271 |
if configuration is not None: |
a506e4de8f84
Add support for Drag'n dropping located variables to function block creating global located variable in configuration and external variable in function block
Laurent Bessard
parents:
1127
diff
changeset
|
1272 |
# Set configuration global vars |
a506e4de8f84
Add support for Drag'n dropping located variables to function block creating global located variable in configuration and external variable in function block
Laurent Bessard
parents:
1127
diff
changeset
|
1273 |
configuration.addglobalVar(type, var_name, location, description) |
814 | 1274 |
|
1275 |
# Replace the configuration globalvars by those given |
|
1276 |
def SetConfigurationGlobalVars(self, name, vars): |
|
1277 |
if self.Project is not None: |
|
1278 |
# Found the configuration corresponding to name |
|
1279 |
configuration = self.Project.getconfiguration(name) |
|
1280 |
if configuration is not None: |
|
1281 |
# Set configuration global vars |
|
1282 |
configuration.setglobalVars([]) |
|
1283 |
for vartype, varlist in self.ExtractVarLists(vars): |
|
1284 |
configuration.globalVars.append(varlist) |
|
1285 |
||
1286 |
# Return the configuration globalvars |
|
1287 |
def GetConfigurationGlobalVars(self, name, debug = False): |
|
1288 |
vars = [] |
|
1289 |
project = self.GetProject(debug) |
|
1290 |
if project is not None: |
|
1291 |
# Found the configuration corresponding to name |
|
1292 |
configuration = project.getconfiguration(name) |
|
1293 |
if configuration is not None: |
|
1294 |
# Extract variables from every varLists |
|
1295 |
for varlist in configuration.getglobalVars(): |
|
1296 |
for var in varlist.getvariable(): |
|
1297 |
tempvar = self.GetVariableDictionary(varlist, var) |
|
1298 |
tempvar["Class"] = "Global" |
|
1299 |
vars.append(tempvar) |
|
1300 |
return vars |
|
1301 |
||
1171
a506e4de8f84
Add support for Drag'n dropping located variables to function block creating global located variable in configuration and external variable in function block
Laurent Bessard
parents:
1127
diff
changeset
|
1302 |
# Return configuration variable names |
a506e4de8f84
Add support for Drag'n dropping located variables to function block creating global located variable in configuration and external variable in function block
Laurent Bessard
parents:
1127
diff
changeset
|
1303 |
def GetConfigurationVariableNames(self, config_name = None, debug = False): |
a506e4de8f84
Add support for Drag'n dropping located variables to function block creating global located variable in configuration and external variable in function block
Laurent Bessard
parents:
1127
diff
changeset
|
1304 |
variables = [] |
a506e4de8f84
Add support for Drag'n dropping located variables to function block creating global located variable in configuration and external variable in function block
Laurent Bessard
parents:
1127
diff
changeset
|
1305 |
project = self.GetProject(debug) |
a506e4de8f84
Add support for Drag'n dropping located variables to function block creating global located variable in configuration and external variable in function block
Laurent Bessard
parents:
1127
diff
changeset
|
1306 |
if project is not None: |
a506e4de8f84
Add support for Drag'n dropping located variables to function block creating global located variable in configuration and external variable in function block
Laurent Bessard
parents:
1127
diff
changeset
|
1307 |
for configuration in self.Project.getconfigurations(): |
a506e4de8f84
Add support for Drag'n dropping located variables to function block creating global located variable in configuration and external variable in function block
Laurent Bessard
parents:
1127
diff
changeset
|
1308 |
if config_name is None or config_name == configuration.getname(): |
a506e4de8f84
Add support for Drag'n dropping located variables to function block creating global located variable in configuration and external variable in function block
Laurent Bessard
parents:
1127
diff
changeset
|
1309 |
variables.extend( |
a506e4de8f84
Add support for Drag'n dropping located variables to function block creating global located variable in configuration and external variable in function block
Laurent Bessard
parents:
1127
diff
changeset
|
1310 |
[var.getname() for var in reduce( |
a506e4de8f84
Add support for Drag'n dropping located variables to function block creating global located variable in configuration and external variable in function block
Laurent Bessard
parents:
1127
diff
changeset
|
1311 |
lambda x, y: x + y, [varlist.getvariable() |
a506e4de8f84
Add support for Drag'n dropping located variables to function block creating global located variable in configuration and external variable in function block
Laurent Bessard
parents:
1127
diff
changeset
|
1312 |
for varlist in configuration.globalVars], |
a506e4de8f84
Add support for Drag'n dropping located variables to function block creating global located variable in configuration and external variable in function block
Laurent Bessard
parents:
1127
diff
changeset
|
1313 |
[])]) |
a506e4de8f84
Add support for Drag'n dropping located variables to function block creating global located variable in configuration and external variable in function block
Laurent Bessard
parents:
1127
diff
changeset
|
1314 |
return variables |
a506e4de8f84
Add support for Drag'n dropping located variables to function block creating global located variable in configuration and external variable in function block
Laurent Bessard
parents:
1127
diff
changeset
|
1315 |
|
814 | 1316 |
# Replace the resource globalvars by those given |
1317 |
def SetConfigurationResourceGlobalVars(self, config_name, name, vars): |
|
1318 |
if self.Project is not None: |
|
1319 |
# Found the resource corresponding to name |
|
1320 |
resource = self.Project.getconfigurationResource(config_name, name) |
|
1321 |
# Set resource global vars |
|
1322 |
if resource is not None: |
|
1323 |
resource.setglobalVars([]) |
|
1324 |
for vartype, varlist in self.ExtractVarLists(vars): |
|
1325 |
resource.globalVars.append(varlist) |
|
1326 |
||
1327 |
# Return the resource globalvars |
|
1328 |
def GetConfigurationResourceGlobalVars(self, config_name, name, debug = False): |
|
1329 |
vars = [] |
|
1330 |
project = self.GetProject(debug) |
|
1331 |
if project is not None: |
|
1332 |
# Found the resource corresponding to name |
|
1333 |
resource = project.getconfigurationResource(config_name, name) |
|
1334 |
if resource: |
|
1335 |
# Extract variables from every varLists |
|
1336 |
for varlist in resource.getglobalVars(): |
|
1337 |
for var in varlist.getvariable(): |
|
1338 |
tempvar = self.GetVariableDictionary(varlist, var) |
|
1339 |
tempvar["Class"] = "Global" |
|
1340 |
vars.append(tempvar) |
|
1341 |
return vars |
|
1342 |
||
1171
a506e4de8f84
Add support for Drag'n dropping located variables to function block creating global located variable in configuration and external variable in function block
Laurent Bessard
parents:
1127
diff
changeset
|
1343 |
# Return resource variable names |
a506e4de8f84
Add support for Drag'n dropping located variables to function block creating global located variable in configuration and external variable in function block
Laurent Bessard
parents:
1127
diff
changeset
|
1344 |
def GetConfigurationResourceVariableNames(self, |
a506e4de8f84
Add support for Drag'n dropping located variables to function block creating global located variable in configuration and external variable in function block
Laurent Bessard
parents:
1127
diff
changeset
|
1345 |
config_name = None, resource_name = None, debug = False): |
a506e4de8f84
Add support for Drag'n dropping located variables to function block creating global located variable in configuration and external variable in function block
Laurent Bessard
parents:
1127
diff
changeset
|
1346 |
variables = [] |
a506e4de8f84
Add support for Drag'n dropping located variables to function block creating global located variable in configuration and external variable in function block
Laurent Bessard
parents:
1127
diff
changeset
|
1347 |
project = self.GetProject(debug) |
a506e4de8f84
Add support for Drag'n dropping located variables to function block creating global located variable in configuration and external variable in function block
Laurent Bessard
parents:
1127
diff
changeset
|
1348 |
if project is not None: |
a506e4de8f84
Add support for Drag'n dropping located variables to function block creating global located variable in configuration and external variable in function block
Laurent Bessard
parents:
1127
diff
changeset
|
1349 |
for configuration in self.Project.getconfigurations(): |
a506e4de8f84
Add support for Drag'n dropping located variables to function block creating global located variable in configuration and external variable in function block
Laurent Bessard
parents:
1127
diff
changeset
|
1350 |
if config_name is None or config_name == configuration.getname(): |
a506e4de8f84
Add support for Drag'n dropping located variables to function block creating global located variable in configuration and external variable in function block
Laurent Bessard
parents:
1127
diff
changeset
|
1351 |
for resource in configuration.getresource(): |
a506e4de8f84
Add support for Drag'n dropping located variables to function block creating global located variable in configuration and external variable in function block
Laurent Bessard
parents:
1127
diff
changeset
|
1352 |
if resource_name is None or resource.getname() == resource_name: |
a506e4de8f84
Add support for Drag'n dropping located variables to function block creating global located variable in configuration and external variable in function block
Laurent Bessard
parents:
1127
diff
changeset
|
1353 |
variables.extend( |
a506e4de8f84
Add support for Drag'n dropping located variables to function block creating global located variable in configuration and external variable in function block
Laurent Bessard
parents:
1127
diff
changeset
|
1354 |
[var.getname() for var in reduce( |
a506e4de8f84
Add support for Drag'n dropping located variables to function block creating global located variable in configuration and external variable in function block
Laurent Bessard
parents:
1127
diff
changeset
|
1355 |
lambda x, y: x + y, [varlist.getvariable() |
a506e4de8f84
Add support for Drag'n dropping located variables to function block creating global located variable in configuration and external variable in function block
Laurent Bessard
parents:
1127
diff
changeset
|
1356 |
for varlist in resource.globalVars], |
a506e4de8f84
Add support for Drag'n dropping located variables to function block creating global located variable in configuration and external variable in function block
Laurent Bessard
parents:
1127
diff
changeset
|
1357 |
[])]) |
a506e4de8f84
Add support for Drag'n dropping located variables to function block creating global located variable in configuration and external variable in function block
Laurent Bessard
parents:
1127
diff
changeset
|
1358 |
return variables |
a506e4de8f84
Add support for Drag'n dropping located variables to function block creating global located variable in configuration and external variable in function block
Laurent Bessard
parents:
1127
diff
changeset
|
1359 |
|
814 | 1360 |
# Recursively generate element name tree for a structured variable |
1361 |
def GenerateVarTree(self, typename, debug = False): |
|
1362 |
project = self.GetProject(debug) |
|
1363 |
if project is not None: |
|
1364 |
blocktype = self.GetBlockType(typename, debug = debug) |
|
1365 |
if blocktype is not None: |
|
1366 |
tree = [] |
|
1367 |
en = False |
|
1368 |
eno = False |
|
1369 |
for var_name, var_type, var_modifier in blocktype["inputs"] + blocktype["outputs"]: |
|
1370 |
en |= var_name.upper() == "EN" |
|
1371 |
eno |= var_name.upper() == "ENO" |
|
1372 |
tree.append((var_name, var_type, self.GenerateVarTree(var_type, debug))) |
|
1373 |
if not eno: |
|
1374 |
tree.insert(0, ("ENO", "BOOL", ([], []))) |
|
1375 |
if not en: |
|
1376 |
tree.insert(0, ("EN", "BOOL", ([], []))) |
|
1377 |
return tree, [] |
|
1378 |
datatype = project.getdataType(typename) |
|
1379 |
if datatype is None: |
|
1380 |
datatype = self.GetConfNodeDataType(typename) |
|
1381 |
if datatype is not None: |
|
1382 |
tree = [] |
|
1383 |
basetype_content = datatype.baseType.getcontent() |
|
1384 |
if basetype_content["name"] == "derived": |
|
1385 |
return self.GenerateVarTree(basetype_content["value"].getname()) |
|
1386 |
elif basetype_content["name"] == "array": |
|
1387 |
dimensions = [] |
|
1388 |
base_type = basetype_content["value"].baseType.getcontent() |
|
1389 |
if base_type["name"] == "derived": |
|
1390 |
tree = self.GenerateVarTree(base_type["value"].getname()) |
|
1391 |
if len(tree[1]) == 0: |
|
1392 |
tree = tree[0] |
|
1393 |
for dimension in basetype_content["value"].getdimension(): |
|
1394 |
dimensions.append((dimension.getlower(), dimension.getupper())) |
|
1395 |
return tree, dimensions |
|
1396 |
elif basetype_content["name"] == "struct": |
|
1397 |
for element in basetype_content["value"].getvariable(): |
|
1398 |
element_type = element.type.getcontent() |
|
1399 |
if element_type["name"] == "derived": |
|
1400 |
tree.append((element.getname(), element_type["value"].getname(), self.GenerateVarTree(element_type["value"].getname()))) |
|
1401 |
else: |
|
1402 |
tree.append((element.getname(), element_type["name"], ([], []))) |
|
1403 |
return tree, [] |
|
1404 |
return [], [] |
|
1405 |
||
1406 |
# Return the interface for the given pou |
|
1407 |
def GetPouInterfaceVars(self, pou, debug = False): |
|
1408 |
vars = [] |
|
1409 |
# Verify that the pou has an interface |
|
1410 |
if pou.interface is not None: |
|
1411 |
# Extract variables from every varLists |
|
1412 |
for type, varlist in pou.getvars(): |
|
1413 |
for var in varlist.getvariable(): |
|
1414 |
tempvar = self.GetVariableDictionary(varlist, var) |
|
1415 |
||
1416 |
tempvar["Class"] = type |
|
1417 |
tempvar["Tree"] = ([], []) |
|
1418 |
||
1419 |
vartype_content = var.gettype().getcontent() |
|
1420 |
if vartype_content["name"] == "derived": |
|
1421 |
tempvar["Edit"] = not pou.hasblock(tempvar["Name"]) |
|
1422 |
tempvar["Tree"] = self.GenerateVarTree(tempvar["Type"], debug) |
|
1423 |
||
1424 |
vars.append(tempvar) |
|
1425 |
return vars |
|
1426 |
||
1427 |
# Replace the Pou interface by the one given |
|
1428 |
def SetPouInterfaceVars(self, name, vars): |
|
1429 |
if self.Project is not None: |
|
1430 |
# Found the pou corresponding to name and add interface if there isn't one yet |
|
1431 |
pou = self.Project.getpou(name) |
|
1432 |
if pou is not None: |
|
1433 |
if pou.interface is None: |
|
1434 |
pou.interface = plcopen.pou_interface() |
|
1435 |
# Set Pou interface |
|
1436 |
pou.setvars(self.ExtractVarLists(vars)) |
|
1437 |
self.Project.RefreshElementUsingTree() |
|
1438 |
self.Project.RefreshCustomBlockTypes() |
|
1439 |
||
1440 |
# Replace the return type of the pou given by its name (only for functions) |
|
1441 |
def SetPouInterfaceReturnType(self, name, type): |
|
1442 |
if self.Project is not None: |
|
1443 |
pou = self.Project.getpou(name) |
|
1444 |
if pou is not None: |
|
1445 |
if pou.interface is None: |
|
1446 |
pou.interface = plcopen.pou_interface() |
|
1447 |
# If there isn't any return type yet, add it |
|
1448 |
return_type = pou.interface.getreturnType() |
|
1449 |
if not return_type: |
|
1450 |
return_type = plcopen.dataType() |
|
1451 |
pou.interface.setreturnType(return_type) |
|
1452 |
# Change return type |
|
1453 |
if type in self.GetBaseTypes(): |
|
1454 |
if type == "STRING": |
|
1455 |
return_type.setcontent({"name" : "string", "value" : plcopen.elementaryTypes_string()}) |
|
1456 |
elif type == "WSTRING": |
|
1457 |
return_type.setcontent({"name" : "wstring", "value" : plcopen.elementaryTypes_wstring()}) |
|
1458 |
else: |
|
1459 |
return_type.setcontent({"name" : type, "value" : None}) |
|
1460 |
else: |
|
1461 |
derived_type = plcopen.derivedTypes_derived() |
|
1462 |
derived_type.setname(type) |
|
1463 |
return_type.setcontent({"name" : "derived", "value" : derived_type}) |
|
1464 |
self.Project.RefreshElementUsingTree() |
|
1465 |
self.Project.RefreshCustomBlockTypes() |
|
1466 |
||
1467 |
def UpdateProjectUsedPous(self, old_name, new_name): |
|
1468 |
if self.Project: |
|
1469 |
self.Project.updateElementName(old_name, new_name) |
|
1470 |
||
1471 |
def UpdateEditedElementUsedVariable(self, tagname, old_name, new_name): |
|
1472 |
pou = self.GetEditedElement(tagname) |
|
1473 |
if pou: |
|
1474 |
pou.updateElementName(old_name, new_name) |
|
1475 |
||
1476 |
# Return the return type of the pou given by its name |
|
1477 |
def GetPouInterfaceReturnTypeByName(self, name): |
|
1478 |
project = self.GetProject(debug) |
|
1479 |
if project is not None: |
|
1480 |
# Found the pou correponding to name and return the return type |
|
1481 |
pou = project.getpou(name) |
|
1482 |
if pou is not None: |
|
1483 |
return self.GetPouInterfaceReturnType(pou) |
|
1484 |
return False |
|
1485 |
||
1486 |
# Return the return type of the given pou |
|
1487 |
def GetPouInterfaceReturnType(self, pou): |
|
1488 |
# Verify that the pou has an interface |
|
1489 |
if pou.interface is not None: |
|
1490 |
# Return the return type if there is one |
|
1491 |
return_type = pou.interface.getreturnType() |
|
1492 |
if return_type: |
|
1493 |
returntype_content = return_type.getcontent() |
|
1494 |
if returntype_content["name"] == "derived": |
|
1495 |
return returntype_content["value"].getname() |
|
1496 |
elif returntype_content["name"] in ["string", "wstring"]: |
|
1497 |
return returntype_content["name"].upper() |
|
1498 |
else: |
|
1499 |
return returntype_content["name"] |
|
1500 |
return None |
|
1501 |
||
1502 |
# Function that add a new confnode to the confnode list |
|
1503 |
def AddConfNodeTypesList(self, typeslist): |
|
1504 |
self.ConfNodeTypes.extend(typeslist) |
|
1505 |
||
1506 |
# Function that clear the confnode list |
|
1507 |
def ClearConfNodeTypes(self): |
|
1508 |
for i in xrange(len(self.ConfNodeTypes)): |
|
1509 |
self.ConfNodeTypes.pop(0) |
|
1510 |
||
1511 |
def GetConfNodeBlockTypes(self): |
|
1512 |
return [{"name": _("%s POUs") % confnodetypes["name"], |
|
1513 |
"list": confnodetypes["types"].GetCustomBlockTypes()} |
|
1514 |
for confnodetypes in self.ConfNodeTypes] |
|
1515 |
||
863
b1ead41fbd3b
Fix bug in VariablePanel 'Type' cell editor menu entry 'User Data Types' containing ConfNodes data types
Laurent Bessard
parents:
853
diff
changeset
|
1516 |
def GetConfNodeDataTypes(self, exclude = "", only_locatables = False): |
814 | 1517 |
return [{"name": _("%s Data Types") % confnodetypes["name"], |