23 # along with this program; if not, write to the Free Software |
23 # along with this program; if not, write to the Free Software |
24 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
24 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
25 |
25 |
26 from xmlclass import * |
26 from xmlclass import * |
27 from types import * |
27 from types import * |
28 import os, re |
28 import os |
|
29 import re |
29 from lxml import etree |
30 from lxml import etree |
30 from collections import OrderedDict |
31 from collections import OrderedDict |
31 import util.paths as paths |
32 import util.paths as paths |
32 |
33 |
33 """ |
34 """ |
34 Dictionary that makes the relation between var names in plcopen and displayed values |
35 Dictionary that makes the relation between var names in plcopen and displayed values |
35 """ |
36 """ |
36 VarTypes = {"Local" : "localVars", "Temp" : "tempVars", "Input" : "inputVars", |
37 VarTypes = { |
37 "Output" : "outputVars", "InOut" : "inOutVars", "External" : "externalVars", |
38 "Local": "localVars", |
38 "Global" : "globalVars", "Access" : "accessVars"} |
39 "Temp": "tempVars", |
|
40 "Input": "inputVars", |
|
41 "Output": "outputVars", |
|
42 "InOut": "inOutVars", |
|
43 "External": "externalVars", |
|
44 "Global": "globalVars", |
|
45 "Access": "accessVars" |
|
46 } |
39 |
47 |
40 searchResultVarTypes = { |
48 searchResultVarTypes = { |
41 "inputVars": "var_input", |
49 "inputVars": "var_input", |
42 "outputVars": "var_output", |
50 "outputVars": "var_output", |
43 "inOutVars": "var_inout" |
51 "inOutVars": "var_inout" |
44 } |
52 } |
45 |
53 |
46 """ |
54 """ |
47 Define in which order var types must be displayed |
55 Define in which order var types must be displayed |
48 """ |
56 """ |
49 VarOrder = ["Local","Temp","Input","Output","InOut","External","Global","Access"] |
57 VarOrder = ["Local", "Temp", "Input", "Output", "InOut", "External", "Global", "Access"] |
50 |
58 |
51 """ |
59 """ |
52 Define which action qualifier must be associated with a duration |
60 Define which action qualifier must be associated with a duration |
53 """ |
61 """ |
54 QualifierList = OrderedDict([("N", False), ("R", False), ("S", False), |
62 QualifierList = OrderedDict([ |
55 ("L", True), ("D", True), ("P", False), ("P0", False), |
63 ("N", False), ("R", False), ("S", False), |
|
64 ("L", True), ("D", True), ("P", False), ("P0", False), |
56 ("P1", False), ("SD", True), ("DS", True), ("SL", True)]) |
65 ("P1", False), ("SD", True), ("DS", True), ("SL", True)]) |
57 |
66 |
58 |
67 |
59 FILTER_ADDRESS_MODEL = "(%%[IQM](?:[XBWDL])?)(%s)((?:\.[0-9]+)*)" |
68 FILTER_ADDRESS_MODEL = "(%%[IQM](?:[XBWDL])?)(%s)((?:\.[0-9]+)*)" |
|
69 |
60 |
70 |
61 def update_address(address, address_model, new_leading): |
71 def update_address(address, address_model, new_leading): |
62 result = address_model.match(address) |
72 result = address_model.match(address) |
63 if result is None: |
73 if result is None: |
64 return address |
74 return address |
65 groups = result.groups() |
75 groups = result.groups() |
66 return groups[0] + new_leading + groups[2] |
76 return groups[0] + new_leading + groups[2] |
67 |
77 |
|
78 |
68 def _init_and_compare(function, v1, v2): |
79 def _init_and_compare(function, v1, v2): |
69 if v1 is None: |
80 if v1 is None: |
70 return v2 |
81 return v2 |
71 if v2 is not None: |
82 if v2 is not None: |
72 return function(v1, v2) |
83 return function(v1, v2) |
73 return v1 |
84 return v1 |
74 |
85 |
75 """ |
86 |
76 Helper class for bounding_box calculation |
|
77 """ |
|
78 class rect: |
87 class rect: |
79 |
88 """ |
|
89 Helper class for bounding_box calculation |
|
90 """ |
|
91 |
80 def __init__(self, x=None, y=None, width=None, height=None): |
92 def __init__(self, x=None, y=None, width=None, height=None): |
81 self.x_min = x |
93 self.x_min = x |
82 self.x_max = None |
94 self.x_max = None |
83 self.y_min = y |
95 self.y_min = y |
84 self.y_max = None |
96 self.y_max = None |
85 if width is not None and x is not None: |
97 if width is not None and x is not None: |
86 self.x_max = x + width |
98 self.x_max = x + width |
87 if height is not None and y is not None: |
99 if height is not None and y is not None: |
88 self.y_max = y + height |
100 self.y_max = y + height |
89 |
101 |
90 def update(self, x, y): |
102 def update(self, x, y): |
91 self.x_min = _init_and_compare(min, self.x_min, x) |
103 self.x_min = _init_and_compare(min, self.x_min, x) |
92 self.x_max = _init_and_compare(max, self.x_max, x) |
104 self.x_max = _init_and_compare(max, self.x_max, x) |
93 self.y_min = _init_and_compare(min, self.y_min, y) |
105 self.y_min = _init_and_compare(min, self.y_min, y) |
94 self.y_max = _init_and_compare(max, self.y_max, y) |
106 self.y_max = _init_and_compare(max, self.y_max, y) |
95 |
107 |
96 def union(self, rect): |
108 def union(self, rect): |
97 self.x_min = _init_and_compare(min, self.x_min, rect.x_min) |
109 self.x_min = _init_and_compare(min, self.x_min, rect.x_min) |
98 self.x_max = _init_and_compare(max, self.x_max, rect.x_max) |
110 self.x_max = _init_and_compare(max, self.x_max, rect.x_max) |
99 self.y_min = _init_and_compare(min, self.y_min, rect.y_min) |
111 self.y_min = _init_and_compare(min, self.y_min, rect.y_min) |
100 self.y_max = _init_and_compare(max, self.y_max, rect.y_max) |
112 self.y_max = _init_and_compare(max, self.y_max, rect.y_max) |
101 |
113 |
102 def bounding_box(self): |
114 def bounding_box(self): |
103 width = height = None |
115 width = height = None |
104 if self.x_min is not None and self.x_max is not None: |
116 if self.x_min is not None and self.x_max is not None: |
105 width = self.x_max - self.x_min |
117 width = self.x_max - self.x_min |
106 if self.y_min is not None and self.y_max is not None: |
118 if self.y_min is not None and self.y_max is not None: |
107 height = self.y_max - self.y_min |
119 height = self.y_max - self.y_min |
108 return self.x_min, self.y_min, width, height |
120 return self.x_min, self.y_min, width, height |
109 |
121 |
|
122 |
110 def TextLenInRowColumn(text): |
123 def TextLenInRowColumn(text): |
111 if text == "": |
124 if text == "": |
112 return (0, 0) |
125 return (0, 0) |
113 lines = text.split("\n") |
126 lines = text.split("\n") |
114 return len(lines) - 1, len(lines[-1]) |
127 return len(lines) - 1, len(lines[-1]) |
|
128 |
115 |
129 |
116 def CompilePattern(criteria): |
130 def CompilePattern(criteria): |
117 flag = 0 if criteria["case_sensitive"] else re.IGNORECASE |
131 flag = 0 if criteria["case_sensitive"] else re.IGNORECASE |
118 find_pattern = criteria["find_pattern"] |
132 find_pattern = criteria["find_pattern"] |
119 if not criteria["regular_expression"]: |
133 if not criteria["regular_expression"]: |
120 find_pattern = re.escape(find_pattern) |
134 find_pattern = re.escape(find_pattern) |
121 criteria["pattern"] = re.compile(find_pattern, flag) |
135 criteria["pattern"] = re.compile(find_pattern, flag) |
122 |
136 |
|
137 |
123 def TestTextElement(text, criteria): |
138 def TestTextElement(text, criteria): |
124 lines = text.splitlines() |
139 lines = text.splitlines() |
125 test_result = [] |
140 test_result = [] |
126 result = criteria["pattern"].search(text) |
141 result = criteria["pattern"].search(text) |
127 while result is not None: |
142 while result is not None: |
128 prev_pos=result.endpos |
143 prev_pos = result.endpos |
129 start = TextLenInRowColumn(text[:result.start()]) |
144 start = TextLenInRowColumn(text[:result.start()]) |
130 end = TextLenInRowColumn(text[:result.end() - 1]) |
145 end = TextLenInRowColumn(text[:result.end() - 1]) |
131 test_result.append((start, end, "\n".join(lines[start[0]:end[0] + 1]))) |
146 test_result.append((start, end, "\n".join(lines[start[0]:end[0] + 1]))) |
132 result = criteria["pattern"].search(text, result.end()) |
147 result = criteria["pattern"].search(text, result.end()) |
133 if result is not None and prev_pos==result.endpos: |
148 if result is not None and prev_pos == result.endpos: |
134 break |
149 break |
135 return test_result |
150 return test_result |
136 |
151 |
|
152 |
137 def TextMatched(str1, str2): |
153 def TextMatched(str1, str2): |
138 return str1 and str2 and (str1.upper() == str2.upper()) |
154 return str1 and str2 and (str1.upper() == str2.upper()) |
139 |
155 |
|
156 |
140 PLCOpenParser = GenerateParserFromXSD(paths.AbsNeighbourFile(__file__, "tc6_xml_v201.xsd")) |
157 PLCOpenParser = GenerateParserFromXSD(paths.AbsNeighbourFile(__file__, "tc6_xml_v201.xsd")) |
141 PLCOpen_XPath = lambda xpath: etree.XPath(xpath, namespaces=PLCOpenParser.NSMAP) |
158 |
|
159 |
|
160 def PLCOpen_XPath(xpath): |
|
161 return etree.XPath(xpath, namespaces=PLCOpenParser.NSMAP) |
|
162 |
142 |
163 |
143 LOAD_POU_PROJECT_TEMPLATE = """ |
164 LOAD_POU_PROJECT_TEMPLATE = """ |
144 <project xmlns:ns1="http://www.plcopen.org/xml/tc6_0201" |
165 <project xmlns:ns1="http://www.plcopen.org/xml/tc6_0201" |
145 xmlns:xhtml="http://www.w3.org/1999/xhtml" |
166 xmlns:xhtml="http://www.w3.org/1999/xhtml" |
146 xmlns:xsd="http://www.w3.org/2001/XMLSchema" |
167 xmlns:xsd="http://www.w3.org/2001/XMLSchema" |
147 xmlns="http://www.plcopen.org/xml/tc6_0201"> |
168 xmlns="http://www.plcopen.org/xml/tc6_0201"> |
148 <fileHeader companyName="" productName="" productVersion="" |
169 <fileHeader companyName="" productName="" productVersion="" |
149 creationDateTime="1970-01-01T00:00:00"/> |
170 creationDateTime="1970-01-01T00:00:00"/> |
150 <contentHeader name="paste_project"> |
171 <contentHeader name="paste_project"> |
151 <coordinateInfo> |
172 <coordinateInfo> |
152 <fbd><scaling x="0" y="0"/></fbd> |
173 <fbd><scaling x="0" y="0"/></fbd> |
153 <ld><scaling x="0" y="0"/></ld> |
174 <ld><scaling x="0" y="0"/></ld> |
162 <configurations/> |
183 <configurations/> |
163 </instances> |
184 </instances> |
164 </project> |
185 </project> |
165 """ |
186 """ |
166 |
187 |
|
188 |
167 def LOAD_POU_INSTANCES_PROJECT_TEMPLATE(body_type): |
189 def LOAD_POU_INSTANCES_PROJECT_TEMPLATE(body_type): |
168 return LOAD_POU_PROJECT_TEMPLATE % """ |
190 return LOAD_POU_PROJECT_TEMPLATE % """ |
169 <pou name="paste_pou" pouType="program"> |
191 <pou name="paste_pou" pouType="program"> |
170 <body> |
192 <body> |
171 <%(body_type)s>%%s</%(body_type)s> |
193 <%(body_type)s>%%s</%(body_type)s> |
172 </body> |
194 </body> |
173 </pou>""" % locals() |
195 </pou>""" % locals() |
174 |
196 |
|
197 |
175 PLCOpen_v1_file = open(paths.AbsNeighbourFile(__file__, "TC6_XML_V10_B.xsd")) |
198 PLCOpen_v1_file = open(paths.AbsNeighbourFile(__file__, "TC6_XML_V10_B.xsd")) |
176 PLCOpen_v1_xml = PLCOpen_v1_file.read() |
199 PLCOpen_v1_xml = PLCOpen_v1_file.read() |
177 PLCOpen_v1_file.close() |
200 PLCOpen_v1_file.close() |
178 PLCOpen_v1_xml = PLCOpen_v1_xml.replace( |
201 PLCOpen_v1_xml = PLCOpen_v1_xml.replace( |
179 "http://www.plcopen.org/xml/tc6.xsd", |
202 "http://www.plcopen.org/xml/tc6.xsd", |
180 "http://www.plcopen.org/xml/tc6_0201") |
203 "http://www.plcopen.org/xml/tc6_0201") |
181 PLCOpen_v1_xsd = etree.XMLSchema(etree.fromstring(PLCOpen_v1_xml)) |
204 PLCOpen_v1_xsd = etree.XMLSchema(etree.fromstring(PLCOpen_v1_xml)) |
182 |
205 |
183 # XPath for file compatibility process |
206 # XPath for file compatibility process |
184 ProjectResourcesXPath = PLCOpen_XPath("ppx:instances/ppx:configurations/ppx:configuration/ppx:resource") |
207 ProjectResourcesXPath = PLCOpen_XPath("ppx:instances/ppx:configurations/ppx:configuration/ppx:resource") |
185 ResourceInstancesXpath = PLCOpen_XPath("ppx:pouInstance | ppx:task/ppx:pouInstance") |
208 ResourceInstancesXpath = PLCOpen_XPath("ppx:pouInstance | ppx:task/ppx:pouInstance") |
186 TransitionsConditionXPath = PLCOpen_XPath("ppx:types/ppx:pous/ppx:pou/ppx:body/*/ppx:transition/ppx:condition") |
209 TransitionsConditionXPath = PLCOpen_XPath("ppx:types/ppx:pous/ppx:pou/ppx:body/*/ppx:transition/ppx:condition") |
187 ConditionConnectionsXPath = PLCOpen_XPath("ppx:connection") |
210 ConditionConnectionsXPath = PLCOpen_XPath("ppx:connection") |
188 ActionBlocksXPath = PLCOpen_XPath("ppx:types/ppx:pous/ppx:pou/ppx:body/*/ppx:actionBlock") |
211 ActionBlocksXPath = PLCOpen_XPath("ppx:types/ppx:pous/ppx:pou/ppx:body/*/ppx:actionBlock") |
189 ActionBlocksConnectionPointOutXPath = PLCOpen_XPath("ppx:connectionPointOut") |
212 ActionBlocksConnectionPointOutXPath = PLCOpen_XPath("ppx:connectionPointOut") |
190 |
213 |
|
214 |
191 def LoadProjectXML(project_xml): |
215 def LoadProjectXML(project_xml): |
192 project_xml = project_xml.replace( |
216 project_xml = project_xml.replace( |
193 "http://www.plcopen.org/xml/tc6.xsd", |
217 "http://www.plcopen.org/xml/tc6.xsd", |
194 "http://www.plcopen.org/xml/tc6_0201") |
218 "http://www.plcopen.org/xml/tc6_0201") |
195 for cre, repl in [ |
219 for cre, repl in [ |
196 (re.compile("(?<!<xhtml:p>)(?:<!\[CDATA\[)"), "<xhtml:p><![CDATA["), |
220 (re.compile("(?<!<xhtml:p>)(?:<!\[CDATA\[)"), "<xhtml:p><![CDATA["), |
197 (re.compile("(?:]]>)(?!</xhtml:p>)"), "]]></xhtml:p>")]: |
221 (re.compile("(?:]]>)(?!</xhtml:p>)"), "]]></xhtml:p>")]: |
198 project_xml = cre.sub(repl, project_xml) |
222 project_xml = cre.sub(repl, project_xml) |
199 |
223 |
200 try: |
224 try: |
201 tree, error = PLCOpenParser.LoadXMLString(project_xml) |
225 tree, error = PLCOpenParser.LoadXMLString(project_xml) |
202 if error is None: |
226 if error is None: |
203 return tree, None |
227 return tree, None |
204 |
228 |
205 if PLCOpen_v1_xsd.validate(tree): |
229 if PLCOpen_v1_xsd.validate(tree): |
206 # Make file compatible with PLCOpen v2 |
230 # Make file compatible with PLCOpen v2 |
207 |
231 |
208 # Update resource interval value |
232 # Update resource interval value |
209 for resource in ProjectResourcesXPath(tree): |
233 for resource in ProjectResourcesXPath(tree): |
210 for task in resource.gettask(): |
234 for task in resource.gettask(): |
211 interval = task.get("interval") |
235 interval = task.get("interval") |
212 if interval is not None: |
236 if interval is not None: |
216 time_values = [int(v) for v in values[:2]] |
240 time_values = [int(v) for v in values[:2]] |
217 seconds = float(values[2]) |
241 seconds = float(values[2]) |
218 time_values.extend([int(seconds), int((seconds % 1) * 1000000)]) |
242 time_values.extend([int(seconds), int((seconds % 1) * 1000000)]) |
219 text = "T#" |
243 text = "T#" |
220 if time_values[0] != 0: |
244 if time_values[0] != 0: |
221 text += "%dh"%time_values[0] |
245 text += "%dh" % time_values[0] |
222 if time_values[1] != 0: |
246 if time_values[1] != 0: |
223 text += "%dm"%time_values[1] |
247 text += "%dm" % time_values[1] |
224 if time_values[2] != 0: |
248 if time_values[2] != 0: |
225 text += "%ds"%time_values[2] |
249 text += "%ds" % time_values[2] |
226 if time_values[3] != 0: |
250 if time_values[3] != 0: |
227 if time_values[3] % 1000 != 0: |
251 if time_values[3] % 1000 != 0: |
228 text += "%.3fms"%(float(time_values[3]) / 1000) |
252 text += "%.3fms" % (float(time_values[3]) / 1000) |
229 else: |
253 else: |
230 text += "%dms"%(time_values[3] / 1000) |
254 text += "%dms" % (time_values[3] / 1000) |
231 task.set("interval", text) |
255 task.set("interval", text) |
232 |
256 |
233 # Update resources pou instance attributes |
257 # Update resources pou instance attributes |
234 for pouInstance in ResourceInstancesXpath(resource): |
258 for pouInstance in ResourceInstancesXpath(resource): |
235 type_name = pouInstance.attrib.pop("type") |
259 type_name = pouInstance.attrib.pop("type") |
236 if type_name is not None: |
260 if type_name is not None: |
237 pouInstance.set("typeName", type_name) |
261 pouInstance.set("typeName", type_name) |
238 |
262 |
239 # Update transitions condition |
263 # Update transitions condition |
240 for transition_condition in TransitionsConditionXPath(tree): |
264 for transition_condition in TransitionsConditionXPath(tree): |
241 connections = ConditionConnectionsXPath(transition_condition) |
265 connections = ConditionConnectionsXPath(transition_condition) |
242 if len(connections) > 0: |
266 if len(connections) > 0: |
243 connectionPointIn = PLCOpenParser.CreateElement("connectionPointIn", "condition") |
267 connectionPointIn = PLCOpenParser.CreateElement("connectionPointIn", "condition") |
244 transition_condition.setcontent(connectionPointIn) |
268 transition_condition.setcontent(connectionPointIn) |
245 connectionPointIn.setrelPositionXY(0, 0) |
269 connectionPointIn.setrelPositionXY(0, 0) |
246 for connection in connections: |
270 for connection in connections: |
247 connectionPointIn.append(connection) |
271 connectionPointIn.append(connection) |
248 |
272 |
249 # Update actionBlocks |
273 # Update actionBlocks |
250 for actionBlock in ActionBlocksXPath(tree): |
274 for actionBlock in ActionBlocksXPath(tree): |
251 for connectionPointOut in ActionBlocksConnectionPointOutXPath(actionBlock): |
275 for connectionPointOut in ActionBlocksConnectionPointOutXPath(actionBlock): |
252 actionBlock.remove(connectionPointOut) |
276 actionBlock.remove(connectionPointOut) |
253 |
277 |
254 for action in actionBlock.getaction(): |
278 for action in actionBlock.getaction(): |
255 action.set("localId", "0") |
279 action.set("localId", "0") |
256 relPosition = PLCOpenParser.CreateElement("relPosition", "action") |
280 relPosition = PLCOpenParser.CreateElement("relPosition", "action") |
257 relPosition.set("x", "0") |
281 relPosition.set("x", "0") |
258 relPosition.set("y", "0") |
282 relPosition.set("y", "0") |
259 action.setrelPosition(relPosition) |
283 action.setrelPosition(relPosition) |
260 |
284 |
261 return tree, None |
285 return tree, None |
262 |
286 |
263 return tree, error |
287 return tree, error |
264 |
288 |
265 except Exception, e: |
289 except Exception, e: |
266 return None, e.message |
290 return None, e.message |
|
291 |
267 |
292 |
268 def LoadProject(filepath): |
293 def LoadProject(filepath): |
269 project_file = open(filepath) |
294 project_file = open(filepath) |
270 project_xml = project_file.read() |
295 project_xml = project_file.read() |
271 project_file.close() |
296 project_file.close() |
272 return LoadProjectXML(project_xml) |
297 return LoadProjectXML(project_xml) |
273 |
298 |
|
299 |
274 project_pou_xpath = PLCOpen_XPath("/ppx:project/ppx:types/ppx:pous/ppx:pou") |
300 project_pou_xpath = PLCOpen_XPath("/ppx:project/ppx:types/ppx:pous/ppx:pou") |
|
301 |
|
302 |
275 def LoadPou(xml_string): |
303 def LoadPou(xml_string): |
276 root, error = LoadProjectXML(LOAD_POU_PROJECT_TEMPLATE % xml_string) |
304 root, error = LoadProjectXML(LOAD_POU_PROJECT_TEMPLATE % xml_string) |
277 return project_pou_xpath(root)[0], error |
305 return project_pou_xpath(root)[0], error |
|
306 |
278 |
307 |
279 project_pou_instances_xpath = { |
308 project_pou_instances_xpath = { |
280 body_type: PLCOpen_XPath( |
309 body_type: PLCOpen_XPath( |
281 "/ppx:project/ppx:types/ppx:pous/ppx:pou[@name='paste_pou']/ppx:body/ppx:%s/*" % body_type) |
310 "/ppx:project/ppx:types/ppx:pous/ppx:pou[@name='paste_pou']/ppx:body/ppx:%s/*" % body_type) |
282 for body_type in ["FBD", "LD", "SFC"]} |
311 for body_type in ["FBD", "LD", "SFC"]} |
|
312 |
|
313 |
283 def LoadPouInstances(xml_string, body_type): |
314 def LoadPouInstances(xml_string, body_type): |
284 root, error = LoadProjectXML( |
315 root, error = LoadProjectXML( |
285 LOAD_POU_INSTANCES_PROJECT_TEMPLATE(body_type) % xml_string) |
316 LOAD_POU_INSTANCES_PROJECT_TEMPLATE(body_type) % xml_string) |
286 return project_pou_instances_xpath[body_type](root), error |
317 return project_pou_instances_xpath[body_type](root), error |
287 |
318 |
|
319 |
288 def SaveProject(project, filepath): |
320 def SaveProject(project, filepath): |
289 project_file = open(filepath, 'w') |
321 project_file = open(filepath, 'w') |
290 project_file.write(etree.tostring( |
322 project_file.write(etree.tostring( |
291 project, |
323 project, |
292 pretty_print=True, |
324 pretty_print=True, |
293 xml_declaration=True, |
325 xml_declaration=True, |
294 encoding='utf-8')) |
326 encoding='utf-8')) |
295 project_file.close() |
327 project_file.close() |
|
328 |
296 |
329 |
297 cls = PLCOpenParser.GetElementClass("formattedText") |
330 cls = PLCOpenParser.GetElementClass("formattedText") |
298 if cls: |
331 if cls: |
299 def updateElementName(self, old_name, new_name): |
332 def updateElementName(self, old_name, new_name): |
300 text = self.getanyText() |
333 text = self.getanyText() |
301 pattern = re.compile('\\b' + old_name + '\\b', re.IGNORECASE) |
334 pattern = re.compile('\\b' + old_name + '\\b', re.IGNORECASE) |
302 text = pattern.sub(new_name, text) |
335 text = pattern.sub(new_name, text) |
303 self.setanyText(text) |
336 self.setanyText(text) |
304 setattr(cls, "updateElementName", updateElementName) |
337 setattr(cls, "updateElementName", updateElementName) |
305 |
338 |
306 def updateElementAddress(self, address_model, new_leading): |
339 def updateElementAddress(self, address_model, new_leading): |
307 text = self.getanyText() |
340 text = self.getanyText() |
308 startpos = 0 |
341 startpos = 0 |
309 result = address_model.search(text, startpos) |
342 result = address_model.search(text, startpos) |
310 while result is not None: |
343 while result is not None: |
313 text = text[:result.start()] + new_address + text[result.end():] |
346 text = text[:result.start()] + new_address + text[result.end():] |
314 startpos = result.start() + len(new_address) |
347 startpos = result.start() + len(new_address) |
315 result = address_model.search(text, startpos) |
348 result = address_model.search(text, startpos) |
316 self.setanyText(text) |
349 self.setanyText(text) |
317 setattr(cls, "updateElementAddress", updateElementAddress) |
350 setattr(cls, "updateElementAddress", updateElementAddress) |
318 |
351 |
319 def hasblock(self, block_type): |
352 def hasblock(self, block_type): |
320 text = self.getanyText() |
353 text = self.getanyText() |
321 pattern = re.compile('\\b' + block_type + '\\b', re.IGNORECASE) |
354 pattern = re.compile('\\b' + block_type + '\\b', re.IGNORECASE) |
322 return pattern.search(text) is not None |
355 return pattern.search(text) is not None |
323 setattr(cls, "hasblock", hasblock) |
356 setattr(cls, "hasblock", hasblock) |
324 |
357 |
325 def Search(self, criteria, parent_infos): |
358 def Search(self, criteria, parent_infos): |
326 return [(tuple(parent_infos),) + result for result in TestTextElement(self.getanyText(), criteria)] |
359 return [(tuple(parent_infos),) + result for result in TestTextElement(self.getanyText(), criteria)] |
327 setattr(cls, "Search", Search) |
360 setattr(cls, "Search", Search) |
328 |
361 |
329 cls = PLCOpenParser.GetElementClass("project") |
362 cls = PLCOpenParser.GetElementClass("project") |
330 if cls: |
363 if cls: |
331 |
364 |
332 def setname(self, name): |
365 def setname(self, name): |
333 self.contentHeader.setname(name) |
366 self.contentHeader.setname(name) |
334 setattr(cls, "setname", setname) |
367 setattr(cls, "setname", setname) |
335 |
368 |
336 def getname(self): |
369 def getname(self): |
337 return self.contentHeader.getname() |
370 return self.contentHeader.getname() |
338 setattr(cls, "getname", getname) |
371 setattr(cls, "getname", getname) |
339 |
372 |
340 def getfileHeader(self): |
373 def getfileHeader(self): |
341 fileheader_obj = self.fileHeader |
374 fileheader_obj = self.fileHeader |
342 return { |
375 return { |
343 attr: value if value is not None else "" |
376 attr: value if value is not None else "" |
344 for attr, value in [ |
377 for attr, value in [ |
390 if func is not None: |
423 if func is not None: |
391 func(value) |
424 func(value) |
392 elif attr in ["modificationDateTime", "organization", "language"]: |
425 elif attr in ["modificationDateTime", "organization", "language"]: |
393 setattr(contentheader_obj, attr, value) |
426 setattr(contentheader_obj, attr, value) |
394 setattr(cls, "setcontentHeader", setcontentHeader) |
427 setattr(cls, "setcontentHeader", setcontentHeader) |
395 |
428 |
396 def gettypeElementFunc(element_type): |
429 def gettypeElementFunc(element_type): |
397 elements_xpath = PLCOpen_XPath( |
430 elements_xpath = PLCOpen_XPath( |
398 "ppx:types/ppx:%(element_type)ss/ppx:%(element_type)s[@name=$name]" % locals()) |
431 "ppx:types/ppx:%(element_type)ss/ppx:%(element_type)s[@name=$name]" % locals()) |
|
432 |
399 def gettypeElement(self, name): |
433 def gettypeElement(self, name): |
400 elements = elements_xpath(self, name=name) |
434 elements = elements_xpath(self, name=name) |
401 if len(elements) == 1: |
435 if len(elements) == 1: |
402 return elements[0] |
436 return elements[0] |
403 return None |
437 return None |
404 return gettypeElement |
438 return gettypeElement |
405 |
439 |
406 datatypes_xpath = PLCOpen_XPath("ppx:types/ppx:dataTypes/ppx:dataType") |
440 datatypes_xpath = PLCOpen_XPath("ppx:types/ppx:dataTypes/ppx:dataType") |
407 filtered_datatypes_xpath = PLCOpen_XPath( |
441 filtered_datatypes_xpath = PLCOpen_XPath( |
408 "ppx:types/ppx:dataTypes/ppx:dataType[@name!=$exclude]") |
442 "ppx:types/ppx:dataTypes/ppx:dataType[@name!=$exclude]") |
|
443 |
409 def getdataTypes(self, exclude=None): |
444 def getdataTypes(self, exclude=None): |
410 if exclude is not None: |
445 if exclude is not None: |
411 return filtered_datatypes_xpath(self, exclude=exclude) |
446 return filtered_datatypes_xpath(self, exclude=exclude) |
412 return datatypes_xpath(self) |
447 return datatypes_xpath(self) |
413 setattr(cls, "getdataTypes", getdataTypes) |
448 setattr(cls, "getdataTypes", getdataTypes) |
414 |
449 |
415 setattr(cls, "getdataType", gettypeElementFunc("dataType")) |
450 setattr(cls, "getdataType", gettypeElementFunc("dataType")) |
416 |
451 |
417 def appenddataType(self, name): |
452 def appenddataType(self, name): |
418 if self.getdataType(name) is not None: |
453 if self.getdataType(name) is not None: |
419 raise ValueError, "\"%s\" Data Type already exists !!!"%name |
454 raise ValueError("\"%s\" Data Type already exists !!!" % name) |
420 self.types.appenddataTypeElement(name) |
455 self.types.appenddataTypeElement(name) |
421 setattr(cls, "appenddataType", appenddataType) |
456 setattr(cls, "appenddataType", appenddataType) |
422 |
457 |
423 def insertdataType(self, index, datatype): |
458 def insertdataType(self, index, datatype): |
424 self.types.insertdataTypeElement(index, datatype) |
459 self.types.insertdataTypeElement(index, datatype) |
425 setattr(cls, "insertdataType", insertdataType) |
460 setattr(cls, "insertdataType", insertdataType) |
426 |
461 |
427 def removedataType(self, name): |
462 def removedataType(self, name): |
428 self.types.removedataTypeElement(name) |
463 self.types.removedataTypeElement(name) |
429 setattr(cls, "removedataType", removedataType) |
464 setattr(cls, "removedataType", removedataType) |
430 |
465 |
431 def getpous(self, exclude=None, filter=[]): |
466 def getpous(self, exclude=None, filter=[]): |
432 return self.xpath( |
467 return self.xpath( |
433 "ppx:types/ppx:pous/ppx:pou%s%s" % |
468 "ppx:types/ppx:pous/ppx:pou%s%s" % |
434 (("[@name!='%s']" % exclude) if exclude is not None else '', |
469 (("[@name!='%s']" % exclude) if exclude is not None else '', |
435 ("[%s]" % " or ".join( |
470 ("[%s]" % " or ".join( |
436 map(lambda x: "@pouType='%s'" % x, filter))) |
471 map(lambda x: "@pouType='%s'" % x, filter))) |
437 if len(filter) > 0 else ""), |
472 if len(filter) > 0 else ""), |
438 namespaces=PLCOpenParser.NSMAP) |
473 namespaces=PLCOpenParser.NSMAP) |
439 setattr(cls, "getpous", getpous) |
474 setattr(cls, "getpous", getpous) |
440 |
475 |
441 setattr(cls, "getpou", gettypeElementFunc("pou")) |
476 setattr(cls, "getpou", gettypeElementFunc("pou")) |
442 |
477 |
443 def appendpou(self, name, pou_type, body_type): |
478 def appendpou(self, name, pou_type, body_type): |
444 self.types.appendpouElement(name, pou_type, body_type) |
479 self.types.appendpouElement(name, pou_type, body_type) |
445 setattr(cls, "appendpou", appendpou) |
480 setattr(cls, "appendpou", appendpou) |
446 |
481 |
447 def insertpou(self, index, pou): |
482 def insertpou(self, index, pou): |
448 self.types.insertpouElement(index, pou) |
483 self.types.insertpouElement(index, pou) |
449 setattr(cls, "insertpou", insertpou) |
484 setattr(cls, "insertpou", insertpou) |
450 |
485 |
451 def removepou(self, name): |
486 def removepou(self, name): |
452 self.types.removepouElement(name) |
487 self.types.removepouElement(name) |
453 setattr(cls, "removepou", removepou) |
488 setattr(cls, "removepou", removepou) |
454 |
489 |
455 configurations_xpath = PLCOpen_XPath( |
490 configurations_xpath = PLCOpen_XPath( |
456 "ppx:instances/ppx:configurations/ppx:configuration") |
491 "ppx:instances/ppx:configurations/ppx:configuration") |
|
492 |
457 def getconfigurations(self): |
493 def getconfigurations(self): |
458 return configurations_xpath(self) |
494 return configurations_xpath(self) |
459 setattr(cls, "getconfigurations", getconfigurations) |
495 setattr(cls, "getconfigurations", getconfigurations) |
460 |
496 |
461 configuration_xpath = PLCOpen_XPath( |
497 configuration_xpath = PLCOpen_XPath( |
462 "ppx:instances/ppx:configurations/ppx:configuration[@name=$name]") |
498 "ppx:instances/ppx:configurations/ppx:configuration[@name=$name]") |
|
499 |
463 def getconfiguration(self, name): |
500 def getconfiguration(self, name): |
464 configurations = configuration_xpath(self, name=name) |
501 configurations = configuration_xpath(self, name=name) |
465 if len(configurations) == 1: |
502 if len(configurations) == 1: |
466 return configurations[0] |
503 return configurations[0] |
467 return None |
504 return None |
468 setattr(cls, "getconfiguration", getconfiguration) |
505 setattr(cls, "getconfiguration", getconfiguration) |
469 |
506 |
470 def addconfiguration(self, name): |
507 def addconfiguration(self, name): |
471 if self.getconfiguration(name) is not None: |
508 if self.getconfiguration(name) is not None: |
472 raise ValueError, _("\"%s\" configuration already exists !!!") % name |
509 raise ValueError(_("\"%s\" configuration already exists !!!") % name) |
473 new_configuration = PLCOpenParser.CreateElement("configuration", "configurations") |
510 new_configuration = PLCOpenParser.CreateElement("configuration", "configurations") |
474 new_configuration.setname(name) |
511 new_configuration.setname(name) |
475 self.instances.configurations.appendconfiguration(new_configuration) |
512 self.instances.configurations.appendconfiguration(new_configuration) |
476 setattr(cls, "addconfiguration", addconfiguration) |
513 setattr(cls, "addconfiguration", addconfiguration) |
477 |
514 |
478 def removeconfiguration(self, name): |
515 def removeconfiguration(self, name): |
479 configuration = self.getconfiguration(name) |
516 configuration = self.getconfiguration(name) |
480 if configuration is None: |
517 if configuration is None: |
481 raise ValueError, ("\"%s\" configuration doesn't exist !!!") % name |
518 raise ValueError(_("\"%s\" configuration doesn't exist !!!") % name) |
482 self.instances.configurations.remove(configuration) |
519 self.instances.configurations.remove(configuration) |
483 setattr(cls, "removeconfiguration", removeconfiguration) |
520 setattr(cls, "removeconfiguration", removeconfiguration) |
484 |
521 |
485 resources_xpath = PLCOpen_XPath( |
522 resources_xpath = PLCOpen_XPath( |
486 "ppx:instances/ppx:configurations/ppx:configuration[@name=$configname]/ppx:resource[@name=$name]") |
523 "ppx:instances/ppx:configurations/ppx:configuration[@name=$configname]/ppx:resource[@name=$name]") |
|
524 |
487 def getconfigurationResource(self, config_name, name): |
525 def getconfigurationResource(self, config_name, name): |
488 resources = resources_xpath(self, configname=config_name, name=name) |
526 resources = resources_xpath(self, configname=config_name, name=name) |
489 if len(resources) == 1: |
527 if len(resources) == 1: |
490 return resources[0] |
528 return resources[0] |
491 return None |
529 return None |
492 setattr(cls, "getconfigurationResource", getconfigurationResource) |
530 setattr(cls, "getconfigurationResource", getconfigurationResource) |
493 |
531 |
494 def addconfigurationResource(self, config_name, name): |
532 def addconfigurationResource(self, config_name, name): |
495 if self.getconfigurationResource(config_name, name) is not None: |
533 if self.getconfigurationResource(config_name, name) is not None: |
496 msg = _("\"{a1}\" resource already exists in \"{a2}\" configuration !!!").format(a1 = name, a2 = config_name) |
534 raise ValueError( |
497 raise ValueError, msg |
535 _("\"{a1}\" resource already exists in \"{a2}\" configuration !!!"). |
|
536 format(a1=name, a2=config_name)) |
|
537 |
498 configuration = self.getconfiguration(config_name) |
538 configuration = self.getconfiguration(config_name) |
499 if configuration is not None: |
539 if configuration is not None: |
500 new_resource = PLCOpenParser.CreateElement("resource", "configuration") |
540 new_resource = PLCOpenParser.CreateElement("resource", "configuration") |
501 new_resource.setname(name) |
541 new_resource.setname(name) |
502 configuration.appendresource(new_resource) |
542 configuration.appendresource(new_resource) |
611 self.ld.scaling.sety(y) |
654 self.ld.scaling.sety(y) |
612 elif language == "SFC": |
655 elif language == "SFC": |
613 self.sfc.scaling.setx(x) |
656 self.sfc.scaling.setx(x) |
614 self.sfc.scaling.sety(y) |
657 self.sfc.scaling.sety(y) |
615 setattr(cls, "setscaling", setscaling) |
658 setattr(cls, "setscaling", setscaling) |
616 |
659 |
617 def getscaling(self, language): |
660 def getscaling(self, language): |
618 if language == "FBD": |
661 if language == "FBD": |
619 return self.fbd.scaling.getx(), self.fbd.scaling.gety() |
662 return self.fbd.scaling.getx(), self.fbd.scaling.gety() |
620 elif language == "LD": |
663 elif language == "LD": |
621 return self.ld.scaling.getx(), self.ld.scaling.gety() |
664 return self.ld.scaling.getx(), self.ld.scaling.gety() |
622 elif language == "SFC": |
665 elif language == "SFC": |
623 return self.sfc.scaling.getx(), self.sfc.scaling.gety() |
666 return self.sfc.scaling.getx(), self.sfc.scaling.gety() |
624 return 0, 0 |
667 return 0, 0 |
625 setattr(cls, "getscaling", getscaling) |
668 setattr(cls, "getscaling", getscaling) |
626 |
669 |
|
670 |
627 def _Search(attributes, criteria, parent_infos): |
671 def _Search(attributes, criteria, parent_infos): |
628 search_result = [] |
672 search_result = [] |
629 for attr, value in attributes: |
673 for attr, value in attributes: |
630 if value is not None: |
674 if value is not None: |
631 search_result.extend([(tuple(parent_infos + [attr]),) + result for result in TestTextElement(value, criteria)]) |
675 search_result.extend([(tuple(parent_infos + [attr]),) + result for result in TestTextElement(value, criteria)]) |
632 return search_result |
676 return search_result |
|
677 |
633 |
678 |
634 def _updateConfigurationResourceElementName(self, old_name, new_name): |
679 def _updateConfigurationResourceElementName(self, old_name, new_name): |
635 for varlist in self.getglobalVars(): |
680 for varlist in self.getglobalVars(): |
636 for var in varlist.getvariable(): |
681 for var in varlist.getvariable(): |
637 var_address = var.getaddress() |
682 var_address = var.getaddress() |
639 if TextMatched(var_address, old_name): |
684 if TextMatched(var_address, old_name): |
640 var.setaddress(new_name) |
685 var.setaddress(new_name) |
641 if TextMatched(var.getname(), old_name): |
686 if TextMatched(var.getname(), old_name): |
642 var.setname(new_name) |
687 var.setname(new_name) |
643 |
688 |
|
689 |
644 def _updateConfigurationResourceElementAddress(self, address_model, new_leading): |
690 def _updateConfigurationResourceElementAddress(self, address_model, new_leading): |
645 for varlist in self.getglobalVars(): |
691 for varlist in self.getglobalVars(): |
646 for var in varlist.getvariable(): |
692 for var in varlist.getvariable(): |
647 var_address = var.getaddress() |
693 var_address = var.getaddress() |
648 if var_address is not None: |
694 if var_address is not None: |
649 var.setaddress(update_address(var_address, address_model, new_leading)) |
695 var.setaddress(update_address(var_address, address_model, new_leading)) |
650 |
696 |
|
697 |
651 def _removeConfigurationResourceVariableByAddress(self, address): |
698 def _removeConfigurationResourceVariableByAddress(self, address): |
652 for varlist in self.getglobalVars(): |
699 for varlist in self.getglobalVars(): |
653 variables = varlist.getvariable() |
700 variables = varlist.getvariable() |
654 for i in xrange(len(variables)-1, -1, -1): |
701 for i in xrange(len(variables)-1, -1, -1): |
655 if variables[i].getaddress() == address: |
702 if variables[i].getaddress() == address: |
656 variables.remove(variables[i]) |
703 variables.remove(variables[i]) |
|
704 |
657 |
705 |
658 def _removeConfigurationResourceVariableByFilter(self, address_model): |
706 def _removeConfigurationResourceVariableByFilter(self, address_model): |
659 for varlist in self.getglobalVars(): |
707 for varlist in self.getglobalVars(): |
660 variables = varlist.getvariable() |
708 variables = varlist.getvariable() |
661 for i in xrange(len(variables)-1, -1, -1): |
709 for i in xrange(len(variables)-1, -1, -1): |
662 var_address = variables[i].getaddress() |
710 var_address = variables[i].getaddress() |
663 if var_address is not None: |
711 if var_address is not None: |
664 result = address_model.match(var_address) |
712 result = address_model.match(var_address) |
665 if result is not None: |
713 if result is not None: |
666 variables.remove(variables[i]) |
714 variables.remove(variables[i]) |
|
715 |
667 |
716 |
668 def _SearchInConfigurationResource(self, criteria, parent_infos=[]): |
717 def _SearchInConfigurationResource(self, criteria, parent_infos=[]): |
669 search_result = _Search([("name", self.getname())], criteria, parent_infos) |
718 search_result = _Search([("name", self.getname())], criteria, parent_infos) |
670 var_number = 0 |
719 var_number = 0 |
671 for varlist in self.getglobalVars(): |
720 for varlist in self.getglobalVars(): |
820 return vartype_content_name.upper() |
870 return vartype_content_name.upper() |
821 # Variable type is an array |
871 # Variable type is an array |
822 elif vartype_content_name == "array": |
872 elif vartype_content_name == "array": |
823 base_type = vartype_content.baseType.getcontent() |
873 base_type = vartype_content.baseType.getcontent() |
824 base_type_name = base_type.getLocalTag() |
874 base_type_name = base_type.getLocalTag() |
825 # Array derived directly from a user defined type |
875 # Array derived directly from a user defined type |
826 if base_type_name == "derived": |
876 if base_type_name == "derived": |
827 basetype_name = base_type.getname() |
877 basetype_name = base_type.getname() |
828 # Array derived directly from a string type |
878 # Array derived directly from a string type |
829 elif base_type_name in ["string", "wstring"]: |
879 elif base_type_name in ["string", "wstring"]: |
830 basetype_name = base_type_name.upper() |
880 basetype_name = base_type_name.upper() |
831 # Array derived directly from an elementary type |
881 # Array derived directly from an elementary type |
832 else: |
882 else: |
833 basetype_name = base_type_name |
883 basetype_name = base_type_name |
834 return "ARRAY [%s] OF %s" % (",".join(map(lambda x : "%s..%s" % (x.getlower(), x.getupper()), vartype_content.getdimension())), basetype_name) |
884 return "ARRAY [%s] OF %s" % (",".join(map(lambda x: "%s..%s" % (x.getlower(), x.getupper()), vartype_content.getdimension())), basetype_name) |
835 # Variable type is an elementary type |
885 # Variable type is an elementary type |
836 return vartype_content_name |
886 return vartype_content_name |
837 setattr(cls, "gettypeAsText", gettypeAsText) |
887 setattr(cls, "gettypeAsText", gettypeAsText) |
838 |
888 |
839 def Search(self, criteria, parent_infos=[]): |
889 def Search(self, criteria, parent_infos=[]): |
840 search_result = _Search([("name", self.getname()), |
890 search_result = _Search([("name", self.getname()), |
841 ("type", self.gettypeAsText()), |
891 ("type", self.gettypeAsText()), |
842 ("location", self.getaddress())], |
892 ("location", self.getaddress())], |
843 criteria, parent_infos) |
893 criteria, parent_infos) |
844 initial = self.getinitialValue() |
894 initial = self.getinitialValue() |
845 if initial is not None: |
895 if initial is not None: |
868 new_datatype = PLCOpenParser.CreateElement("dataType", "dataTypes") |
918 new_datatype = PLCOpenParser.CreateElement("dataType", "dataTypes") |
869 self.dataTypes.appenddataType(new_datatype) |
919 self.dataTypes.appenddataType(new_datatype) |
870 new_datatype.setname(name) |
920 new_datatype.setname(name) |
871 new_datatype.baseType.setcontent(PLCOpenParser.CreateElement("BOOL", "dataType")) |
921 new_datatype.baseType.setcontent(PLCOpenParser.CreateElement("BOOL", "dataType")) |
872 setattr(cls, "appenddataTypeElement", appenddataTypeElement) |
922 setattr(cls, "appenddataTypeElement", appenddataTypeElement) |
873 |
923 |
874 def insertdataTypeElement(self, index, dataType): |
924 def insertdataTypeElement(self, index, dataType): |
875 self.dataTypes.insertdataType(index, dataType) |
925 self.dataTypes.insertdataType(index, dataType) |
876 setattr(cls, "insertdataTypeElement", insertdataTypeElement) |
926 setattr(cls, "insertdataTypeElement", insertdataTypeElement) |
877 |
927 |
878 def removedataTypeElement(self, name): |
928 def removedataTypeElement(self, name): |
879 found = False |
929 found = False |
880 for element in self.dataTypes.getdataType(): |
930 for element in self.dataTypes.getdataType(): |
881 if TextMatched(element.getname(), name): |
931 if TextMatched(element.getname(), name): |
882 self.dataTypes.remove(element) |
932 self.dataTypes.remove(element) |
883 found = True |
933 found = True |
884 break |
934 break |
885 if not found: |
935 if not found: |
886 raise ValueError, _("\"%s\" Data Type doesn't exist !!!")%name |
936 raise ValueError(_("\"%s\" Data Type doesn't exist !!!") % name) |
887 setattr(cls, "removedataTypeElement", removedataTypeElement) |
937 setattr(cls, "removedataTypeElement", removedataTypeElement) |
888 |
938 |
889 def getpouElements(self): |
939 def getpouElements(self): |
890 return self.pous.getpou() |
940 return self.pous.getpou() |
891 setattr(cls, "getpouElements", getpouElements) |
941 setattr(cls, "getpouElements", getpouElements) |
892 |
942 |
893 def getpouElement(self, name): |
943 def getpouElement(self, name): |
894 elements = self.pous.getpou() |
944 elements = self.pous.getpou() |
895 for element in elements: |
945 for element in elements: |
896 if TextMatched(element.getname(), name): |
946 if TextMatched(element.getname(), name): |
897 return element |
947 return element |
899 setattr(cls, "getpouElement", getpouElement) |
949 setattr(cls, "getpouElement", getpouElement) |
900 |
950 |
901 def appendpouElement(self, name, pou_type, body_type): |
951 def appendpouElement(self, name, pou_type, body_type): |
902 for element in self.pous.getpou(): |
952 for element in self.pous.getpou(): |
903 if TextMatched(element.getname(), name): |
953 if TextMatched(element.getname(), name): |
904 raise ValueError, _("\"%s\" POU already exists !!!")%name |
954 raise ValueError(_("\"%s\" POU already exists !!!") % name) |
905 new_pou = PLCOpenParser.CreateElement("pou", "pous") |
955 new_pou = PLCOpenParser.CreateElement("pou", "pous") |
906 self.pous.appendpou(new_pou) |
956 self.pous.appendpou(new_pou) |
907 new_pou.setname(name) |
957 new_pou.setname(name) |
908 new_pou.setpouType(pou_type) |
958 new_pou.setpouType(pou_type) |
909 new_pou.appendbody(PLCOpenParser.CreateElement("body", "pou")) |
959 new_pou.appendbody(PLCOpenParser.CreateElement("body", "pou")) |
910 new_pou.setbodyType(body_type) |
960 new_pou.setbodyType(body_type) |
911 setattr(cls, "appendpouElement", appendpouElement) |
961 setattr(cls, "appendpouElement", appendpouElement) |
912 |
962 |
913 def insertpouElement(self, index, pou): |
963 def insertpouElement(self, index, pou): |
914 self.pous.insertpou(index, pou) |
964 self.pous.insertpou(index, pou) |
915 setattr(cls, "insertpouElement", insertpouElement) |
965 setattr(cls, "insertpouElement", insertpouElement) |
916 |
966 |
917 def removepouElement(self, name): |
967 def removepouElement(self, name): |
918 found = False |
968 found = False |
919 for element in self.pous.getpou(): |
969 for element in self.pous.getpou(): |
920 if TextMatched(element.getname(), name): |
970 if TextMatched(element.getname(), name): |
921 self.pous.remove(element) |
971 self.pous.remove(element) |
922 found = True |
972 found = True |
923 break |
973 break |
924 if not found: |
974 if not found: |
925 raise ValueError, _("\"%s\" POU doesn't exist !!!")%name |
975 raise ValueError(_("\"%s\" POU doesn't exist !!!") % name) |
926 setattr(cls, "removepouElement", removepouElement) |
976 setattr(cls, "removepouElement", removepouElement) |
927 |
977 |
928 def Search(self, criteria, parent_infos=[]): |
978 def Search(self, criteria, parent_infos=[]): |
929 search_result = [] |
979 search_result = [] |
930 filter = criteria["filter"] |
980 filter = criteria["filter"] |
985 if cls: |
1037 if cls: |
986 def updateElementName(self, old_name, new_name): |
1038 def updateElementName(self, old_name, new_name): |
987 if TextMatched(self.name, old_name): |
1039 if TextMatched(self.name, old_name): |
988 self.name = new_name |
1040 self.name = new_name |
989 setattr(cls, "updateElementName", updateElementName) |
1041 setattr(cls, "updateElementName", updateElementName) |
990 |
1042 |
991 def Search(self, criteria, parent_infos=[]): |
1043 def Search(self, criteria, parent_infos=[]): |
992 return [(tuple(parent_infos),) + result for result in TestTextElement(self.name, criteria)] |
1044 return [(tuple(parent_infos),) + result for result in TestTextElement(self.name, criteria)] |
993 setattr(cls, "Search", Search) |
1045 setattr(cls, "Search", Search) |
994 |
1046 |
995 cls = PLCOpenParser.GetElementClass("array", "dataType") |
1047 cls = PLCOpenParser.GetElementClass("array", "dataType") |
996 if cls: |
1048 if cls: |
997 setattr(cls, "updateElementName", _updateBaseTypeElementName) |
1049 setattr(cls, "updateElementName", _updateBaseTypeElementName) |
998 |
1050 |
999 def Search(self, criteria, parent_infos=[]): |
1051 def Search(self, criteria, parent_infos=[]): |
1000 search_result = self.baseType.Search(criteria, parent_infos) |
1052 search_result = self.baseType.Search(criteria, parent_infos) |
1001 for i, dimension in enumerate(self.getdimension()): |
1053 for i, dimension in enumerate(self.getdimension()): |
1002 search_result.extend(_Search([("lower", dimension.getlower()), |
1054 search_result.extend(_Search([("lower", dimension.getlower()), |
1003 ("upper", dimension.getupper())], |
1055 ("upper", dimension.getupper())], |
1004 criteria, parent_infos + ["range", i])) |
1056 criteria, parent_infos + ["range", i])) |
1005 return search_result |
1057 return search_result |
1006 setattr(cls, "Search", Search) |
1058 setattr(cls, "Search", Search) |
1007 |
1059 |
|
1060 |
1008 def _SearchInSubrange(self, criteria, parent_infos=[]): |
1061 def _SearchInSubrange(self, criteria, parent_infos=[]): |
1009 search_result = self.baseType.Search(criteria, parent_infos) |
1062 search_result = self.baseType.Search(criteria, parent_infos) |
1010 search_result.extend(_Search([("lower", self.range.getlower()), |
1063 search_result.extend(_Search([("lower", self.range.getlower()), |
1011 ("upper", self.range.getupper())], |
1064 ("upper", self.range.getupper())], |
1012 criteria, parent_infos)) |
1065 criteria, parent_infos)) |
1013 return search_result |
1066 return search_result |
1014 |
1067 |
|
1068 |
1015 cls = PLCOpenParser.GetElementClass("subrangeSigned", "dataType") |
1069 cls = PLCOpenParser.GetElementClass("subrangeSigned", "dataType") |
1016 if cls: |
1070 if cls: |
1017 setattr(cls, "updateElementName", _updateBaseTypeElementName) |
1071 setattr(cls, "updateElementName", _updateBaseTypeElementName) |
1018 setattr(cls, "Search", _SearchInSubrange) |
1072 setattr(cls, "Search", _SearchInSubrange) |
1019 |
1073 |
1022 setattr(cls, "updateElementName", _updateBaseTypeElementName) |
1076 setattr(cls, "updateElementName", _updateBaseTypeElementName) |
1023 setattr(cls, "Search", _SearchInSubrange) |
1077 setattr(cls, "Search", _SearchInSubrange) |
1024 |
1078 |
1025 cls = PLCOpenParser.GetElementClass("enum", "dataType") |
1079 cls = PLCOpenParser.GetElementClass("enum", "dataType") |
1026 if cls: |
1080 if cls: |
1027 |
1081 |
1028 def updateElementName(self, old_name, new_name): |
1082 def updateElementName(self, old_name, new_name): |
1029 pass |
1083 pass |
1030 setattr(cls, "updateElementName", updateElementName) |
1084 setattr(cls, "updateElementName", updateElementName) |
1031 |
1085 |
1032 enumerated_datatype_values_xpath = PLCOpen_XPath("ppx:values/ppx:value") |
1086 enumerated_datatype_values_xpath = PLCOpen_XPath("ppx:values/ppx:value") |
|
1087 |
1033 def Search(self, criteria, parent_infos=[]): |
1088 def Search(self, criteria, parent_infos=[]): |
1034 search_result = [] |
1089 search_result = [] |
1035 for i, value in enumerate(enumerated_datatype_values_xpath(self)): |
1090 for i, value in enumerate(enumerated_datatype_values_xpath(self)): |
1036 for result in TestTextElement(value.getname(), criteria): |
1091 for result in TestTextElement(value.getname(), criteria): |
1037 search_result.append((tuple(parent_infos + ["value", i]),) + result) |
1092 search_result.append((tuple(parent_infos + ["value", i]),) + result) |
1038 return search_result |
1093 return search_result |
1039 setattr(cls, "Search", Search) |
1094 setattr(cls, "Search", Search) |
1040 |
1095 |
|
1096 |
1041 def _getvariableTypeinfos(variable_type): |
1097 def _getvariableTypeinfos(variable_type): |
1042 type_content = variable_type.getcontent() |
1098 type_content = variable_type.getcontent() |
1043 type_content_type = type_content.getLocalTag() |
1099 type_content_type = type_content.getLocalTag() |
1044 if type_content_type == "derived": |
1100 if type_content_type == "derived": |
1045 return type_content.getname() |
1101 return type_content.getname() |
1046 return type_content_type.upper() |
1102 return type_content_type.upper() |
1047 |
1103 |
|
1104 |
1048 cls = PLCOpenParser.GetElementClass("pou", "pous") |
1105 cls = PLCOpenParser.GetElementClass("pou", "pous") |
1049 if cls: |
1106 if cls: |
1050 |
1107 |
1051 block_inputs_xpath = PLCOpen_XPath( |
1108 block_inputs_xpath = PLCOpen_XPath( |
1052 "ppx:interface/*[self::ppx:inputVars or self::ppx:inOutVars]/ppx:variable") |
1109 "ppx:interface/*[self::ppx:inputVars or self::ppx:inOutVars]/ppx:variable") |
1053 block_outputs_xpath = PLCOpen_XPath( |
1110 block_outputs_xpath = PLCOpen_XPath( |
1054 "ppx:interface/*[self::ppx:outputVars or self::ppx:inOutVars]/ppx:variable") |
1111 "ppx:interface/*[self::ppx:outputVars or self::ppx:inOutVars]/ppx:variable") |
1055 def getblockInfos(self): |
1112 |
|
1113 def getblockInfos(self): |
1056 block_infos = { |
1114 block_infos = { |
1057 "name" : self.getname(), |
1115 "name": self.getname(), |
1058 "type" : self.getpouType(), |
1116 "type": self.getpouType(), |
1059 "extensible" : False, |
1117 "extensible": False, |
1060 "inputs" : [], |
1118 "inputs": [], |
1061 "outputs" : [], |
1119 "outputs": [], |
1062 "comment" : self.getdescription()} |
1120 "comment": self.getdescription()} |
1063 if self.interface is not None: |
1121 if self.interface is not None: |
1064 return_type = self.interface.getreturnType() |
1122 return_type = self.interface.getreturnType() |
1065 if return_type is not None: |
1123 if return_type is not None: |
1066 block_infos["outputs"].append( |
1124 block_infos["outputs"].append( |
1067 ("OUT", _getvariableTypeinfos(return_type), "none")) |
1125 ("OUT", _getvariableTypeinfos(return_type), "none")) |
1069 [(var.getname(), _getvariableTypeinfos(var.type), "none") |
1127 [(var.getname(), _getvariableTypeinfos(var.type), "none") |
1070 for var in block_inputs_xpath(self)]) |
1128 for var in block_inputs_xpath(self)]) |
1071 block_infos["outputs"].extend( |
1129 block_infos["outputs"].extend( |
1072 [(var.getname(), _getvariableTypeinfos(var.type), "none") |
1130 [(var.getname(), _getvariableTypeinfos(var.type), "none") |
1073 for var in block_outputs_xpath(self)]) |
1131 for var in block_outputs_xpath(self)]) |
1074 |
1132 |
1075 block_infos["usage"] = ("\n (%s) => (%s)" % |
1133 block_infos["usage"] = ("\n (%s) => (%s)" % |
1076 (", ".join(["%s:%s" % (input[1], input[0]) |
1134 (", ".join(["%s:%s" % (input[1], input[0]) |
1077 for input in block_infos["inputs"]]), |
1135 for input in block_infos["inputs"]]), |
1078 ", ".join(["%s:%s" % (output[1], output[0]) |
1136 ", ".join(["%s:%s" % (output[1], output[0]) |
1079 for output in block_infos["outputs"]]))) |
1137 for output in block_infos["outputs"]]))) |
1080 return block_infos |
1138 return block_infos |
1081 setattr(cls, "getblockInfos", getblockInfos) |
1139 setattr(cls, "getblockInfos", getblockInfos) |
1082 |
1140 |
1083 def setdescription(self, description): |
1141 def setdescription(self, description): |
1084 doc = self.getdocumentation() |
1142 doc = self.getdocumentation() |
1085 if doc is None: |
1143 if doc is None: |
1086 doc = PLCOpenParser.CreateElement("documentation", "pou") |
1144 doc = PLCOpenParser.CreateElement("documentation", "pou") |
1087 self.setdocumentation(doc) |
1145 self.setdocumentation(doc) |
1088 doc.setanyText(description) |
1146 doc.setanyText(description) |
1089 setattr(cls, "setdescription", setdescription) |
1147 setattr(cls, "setdescription", setdescription) |
1090 |
1148 |
1091 def getdescription(self): |
1149 def getdescription(self): |
1092 doc = self.getdocumentation() |
1150 doc = self.getdocumentation() |
1093 if doc is not None: |
1151 if doc is not None: |
1094 return doc.getanyText() |
1152 return doc.getanyText() |
1095 return "" |
1153 return "" |
1096 setattr(cls, "getdescription", getdescription) |
1154 setattr(cls, "getdescription", getdescription) |
1097 |
1155 |
1098 def setbodyType(self, body_type): |
1156 def setbodyType(self, body_type): |
1099 if len(self.body) > 0: |
1157 if len(self.body) > 0: |
1100 if body_type in ["IL", "ST", "LD", "FBD", "SFC"]: |
1158 if body_type in ["IL", "ST", "LD", "FBD", "SFC"]: |
1101 self.body[0].setcontent(PLCOpenParser.CreateElement(body_type, "body")) |
1159 self.body[0].setcontent(PLCOpenParser.CreateElement(body_type, "body")) |
1102 else: |
1160 else: |
1103 raise ValueError, "%s isn't a valid body type!"%type |
1161 raise ValueError("%s isn't a valid body type!" % type) |
1104 setattr(cls, "setbodyType", setbodyType) |
1162 setattr(cls, "setbodyType", setbodyType) |
1105 |
1163 |
1106 def getbodyType(self): |
1164 def getbodyType(self): |
1107 if len(self.body) > 0: |
1165 if len(self.body) > 0: |
1108 return self.body[0].getcontent().getLocalTag() |
1166 return self.body[0].getcontent().getLocalTag() |
1109 setattr(cls, "getbodyType", getbodyType) |
1167 setattr(cls, "getbodyType", getbodyType) |
1110 |
1168 |
1111 def resetexecutionOrder(self): |
1169 def resetexecutionOrder(self): |
1112 if len(self.body) > 0: |
1170 if len(self.body) > 0: |
1113 self.body[0].resetexecutionOrder() |
1171 self.body[0].resetexecutionOrder() |
1114 setattr(cls, "resetexecutionOrder", resetexecutionOrder) |
1172 setattr(cls, "resetexecutionOrder", resetexecutionOrder) |
1115 |
1173 |
1116 def compileexecutionOrder(self): |
1174 def compileexecutionOrder(self): |
1117 if len(self.body) > 0: |
1175 if len(self.body) > 0: |
1118 self.body[0].compileexecutionOrder() |
1176 self.body[0].compileexecutionOrder() |
1119 setattr(cls, "compileexecutionOrder", compileexecutionOrder) |
1177 setattr(cls, "compileexecutionOrder", compileexecutionOrder) |
1120 |
1178 |
1121 def setelementExecutionOrder(self, instance, new_executionOrder): |
1179 def setelementExecutionOrder(self, instance, new_executionOrder): |
1122 if len(self.body) > 0: |
1180 if len(self.body) > 0: |
1123 self.body[0].setelementExecutionOrder(instance, new_executionOrder) |
1181 self.body[0].setelementExecutionOrder(instance, new_executionOrder) |
1124 setattr(cls, "setelementExecutionOrder", setelementExecutionOrder) |
1182 setattr(cls, "setelementExecutionOrder", setelementExecutionOrder) |
1125 |
1183 |
1126 def addinstance(self, instance): |
1184 def addinstance(self, instance): |
1127 if len(self.body) > 0: |
1185 if len(self.body) > 0: |
1128 self.body[0].appendcontentInstance(instance) |
1186 self.body[0].appendcontentInstance(instance) |
1129 setattr(cls, "addinstance", addinstance) |
1187 setattr(cls, "addinstance", addinstance) |
1130 |
1188 |
1131 def getinstances(self): |
1189 def getinstances(self): |
1132 if len(self.body) > 0: |
1190 if len(self.body) > 0: |
1133 return self.body[0].getcontentInstances() |
1191 return self.body[0].getcontentInstances() |
1134 return [] |
1192 return [] |
1135 setattr(cls, "getinstances", getinstances) |
1193 setattr(cls, "getinstances", getinstances) |
1136 |
1194 |
1137 def getinstance(self, id): |
1195 def getinstance(self, id): |
1138 if len(self.body) > 0: |
1196 if len(self.body) > 0: |
1139 return self.body[0].getcontentInstance(id) |
1197 return self.body[0].getcontentInstance(id) |
1140 return None |
1198 return None |
1141 setattr(cls, "getinstance", getinstance) |
1199 setattr(cls, "getinstance", getinstance) |
1142 |
1200 |
1143 def getinstancesIds(self): |
1201 def getinstancesIds(self): |
1144 if len(self.body) > 0: |
1202 if len(self.body) > 0: |
1145 return self.body[0].getcontentInstancesIds() |
1203 return self.body[0].getcontentInstancesIds() |
1146 return [] |
1204 return [] |
1147 setattr(cls, "getinstancesIds", getinstancesIds) |
1205 setattr(cls, "getinstancesIds", getinstancesIds) |
1148 |
1206 |
1149 def getinstanceByName(self, name): |
1207 def getinstanceByName(self, name): |
1150 if len(self.body) > 0: |
1208 if len(self.body) > 0: |
1151 return self.body[0].getcontentInstanceByName(name) |
1209 return self.body[0].getcontentInstanceByName(name) |
1152 return None |
1210 return None |
1153 setattr(cls, "getinstanceByName", getinstanceByName) |
1211 setattr(cls, "getinstanceByName", getinstanceByName) |
1154 |
1212 |
1155 def removeinstance(self, id): |
1213 def removeinstance(self, id): |
1156 if len(self.body) > 0: |
1214 if len(self.body) > 0: |
1157 self.body[0].removecontentInstance(id) |
1215 self.body[0].removecontentInstance(id) |
1158 setattr(cls, "removeinstance", removeinstance) |
1216 setattr(cls, "removeinstance", removeinstance) |
1159 |
1217 |
1160 def settext(self, text): |
1218 def settext(self, text): |
1161 if len(self.body) > 0: |
1219 if len(self.body) > 0: |
1162 self.body[0].settext(text) |
1220 self.body[0].settext(text) |
1163 setattr(cls, "settext", settext) |
1221 setattr(cls, "settext", settext) |
1164 |
1222 |
1165 def gettext(self): |
1223 def gettext(self): |
1166 if len(self.body) > 0: |
1224 if len(self.body) > 0: |
1167 return self.body[0].gettext() |
1225 return self.body[0].gettext() |
1168 return "" |
1226 return "" |
1169 setattr(cls, "gettext", gettext) |
1227 setattr(cls, "gettext", gettext) |
1176 reverse_types[value] = name |
1234 reverse_types[value] = name |
1177 for varlist in self.interface.getcontent(): |
1235 for varlist in self.interface.getcontent(): |
1178 vars.append((reverse_types[varlist.getLocalTag()], varlist)) |
1236 vars.append((reverse_types[varlist.getLocalTag()], varlist)) |
1179 return vars |
1237 return vars |
1180 setattr(cls, "getvars", getvars) |
1238 setattr(cls, "getvars", getvars) |
1181 |
1239 |
1182 def setvars(self, vars): |
1240 def setvars(self, vars): |
1183 if self.interface is None: |
1241 if self.interface is None: |
1184 self.interface = PLCOpenParser.CreateElement("interface", "pou") |
1242 self.interface = PLCOpenParser.CreateElement("interface", "pou") |
1185 self.interface.setcontent(vars) |
1243 self.interface.setcontent(vars) |
1186 setattr(cls, "setvars", setvars) |
1244 setattr(cls, "setvars", setvars) |
1187 |
1245 |
1188 def addpouExternalVar(self, var_type, name): |
1246 def addpouExternalVar(self, var_type, name): |
1189 self.addpouVar(var_type, name, "externalVars") |
1247 self.addpouVar(var_type, name, "externalVars") |
1190 setattr(cls, "addpouExternalVar", addpouExternalVar) |
1248 setattr(cls, "addpouExternalVar", addpouExternalVar) |
1191 |
1249 |
1192 def addpouVar(self, var_type, name, var_class="localVars", location="", description="", initval=""): |
1250 def addpouVar(self, var_type, name, var_class="localVars", location="", description="", initval=""): |
1193 if self.interface is None: |
1251 if self.interface is None: |
1194 self.interface = PLCOpenParser.CreateElement("interface", "pou") |
1252 self.interface = PLCOpenParser.CreateElement("interface", "pou") |
1195 content = self.interface.getcontent() |
1253 content = self.interface.getcontent() |
1196 if len(content) == 0: |
1254 if len(content) == 0: |
1289 transition.setname(name) |
1348 transition.setname(name) |
1290 transition.setbodyType(body_type) |
1349 transition.setbodyType(body_type) |
1291 if body_type == "ST": |
1350 if body_type == "ST": |
1292 transition.settext(":= ;") |
1351 transition.settext(":= ;") |
1293 setattr(cls, "addtransition", addtransition) |
1352 setattr(cls, "addtransition", addtransition) |
1294 |
1353 |
1295 def gettransition(self, name): |
1354 def gettransition(self, name): |
1296 if self.transitions is not None: |
1355 if self.transitions is not None: |
1297 for transition in self.transitions.gettransition(): |
1356 for transition in self.transitions.gettransition(): |
1298 if TextMatched(transition.getname(), name): |
1357 if TextMatched(transition.getname(), name): |
1299 return transition |
1358 return transition |
1300 return None |
1359 return None |
1301 setattr(cls, "gettransition", gettransition) |
1360 setattr(cls, "gettransition", gettransition) |
1302 |
1361 |
1303 def gettransitionList(self): |
1362 def gettransitionList(self): |
1304 if self.transitions is not None: |
1363 if self.transitions is not None: |
1305 return self.transitions.gettransition() |
1364 return self.transitions.gettransition() |
1306 return [] |
1365 return [] |
1307 setattr(cls, "gettransitionList", gettransitionList) |
1366 setattr(cls, "gettransitionList", gettransitionList) |
1308 |
1367 |
1309 def removetransition(self, name): |
1368 def removetransition(self, name): |
1310 if self.transitions is not None: |
1369 if self.transitions is not None: |
1311 removed = False |
1370 removed = False |
1312 for transition in self.transitions.gettransition(): |
1371 for transition in self.transitions.gettransition(): |
1313 if TextMatched(transition.getname(), name): |
1372 if TextMatched(transition.getname(), name): |
1314 if transition.getbodyType() in ["FBD", "LD", "SFC"]: |
1373 if transition.getbodyType() in ["FBD", "LD", "SFC"]: |
1315 for instance in transition.getinstances(): |
1374 for instance in transition.getinstances(): |
1316 if isinstance(instance, PLCOpenParser.GetElementClass("block", "fbdObjects")): |
1375 if isinstance(instance, PLCOpenParser.GetElementClass("block", "fbdObjects")): |
1317 self.removepouVar(instance.gettypeName(), |
1376 self.removepouVar(instance.gettypeName(), |
1318 instance.getinstanceName()) |
1377 instance.getinstanceName()) |
1319 self.transitions.remove(transition) |
1378 self.transitions.remove(transition) |
1320 removed = True |
1379 removed = True |
1321 break |
1380 break |
1322 if not removed: |
1381 if not removed: |
1323 raise ValueError, _("Transition with name %s doesn't exist!")%name |
1382 raise ValueError(_("Transition with name %s doesn't exist!") % name) |
1324 setattr(cls, "removetransition", removetransition) |
1383 setattr(cls, "removetransition", removetransition) |
1325 |
1384 |
1326 def addaction(self, name, body_type): |
1385 def addaction(self, name, body_type): |
1327 if self.actions is None: |
1386 if self.actions is None: |
1328 self.addactions() |
1387 self.addactions() |
1330 action = PLCOpenParser.CreateElement("action", "actions") |
1389 action = PLCOpenParser.CreateElement("action", "actions") |
1331 self.actions.appendaction(action) |
1390 self.actions.appendaction(action) |
1332 action.setname(name) |
1391 action.setname(name) |
1333 action.setbodyType(body_type) |
1392 action.setbodyType(body_type) |
1334 setattr(cls, "addaction", addaction) |
1393 setattr(cls, "addaction", addaction) |
1335 |
1394 |
1336 def getaction(self, name): |
1395 def getaction(self, name): |
1337 if self.actions is not None: |
1396 if self.actions is not None: |
1338 for action in self.actions.getaction(): |
1397 for action in self.actions.getaction(): |
1339 if TextMatched(action.getname(), name): |
1398 if TextMatched(action.getname(), name): |
1340 return action |
1399 return action |
1341 return None |
1400 return None |
1342 setattr(cls, "getaction", getaction) |
1401 setattr(cls, "getaction", getaction) |
1343 |
1402 |
1344 def getactionList(self): |
1403 def getactionList(self): |
1345 if self.actions is not None: |
1404 if self.actions is not None: |
1346 return self.actions.getaction() |
1405 return self.actions.getaction() |
1347 return [] |
1406 return [] |
1348 setattr(cls, "getactionList", getactionList) |
1407 setattr(cls, "getactionList", getactionList) |
1349 |
1408 |
1350 def removeaction(self, name): |
1409 def removeaction(self, name): |
1351 if self.actions is not None: |
1410 if self.actions is not None: |
1352 removed = False |
1411 removed = False |
1353 for action in self.actions.getaction(): |
1412 for action in self.actions.getaction(): |
1354 if TextMatched(action.getname(), name): |
1413 if TextMatched(action.getname(), name): |
1355 if action.getbodyType() in ["FBD", "LD", "SFC"]: |
1414 if action.getbodyType() in ["FBD", "LD", "SFC"]: |
1356 for instance in action.getinstances(): |
1415 for instance in action.getinstances(): |
1357 if isinstance(instance, PLCOpenParser.GetElementClass("block", "fbdObjects")): |
1416 if isinstance(instance, PLCOpenParser.GetElementClass("block", "fbdObjects")): |
1358 self.removepouVar(instance.gettypeName(), |
1417 self.removepouVar(instance.gettypeName(), |
1359 instance.getinstanceName()) |
1418 instance.getinstanceName()) |
1360 self.actions.remove(action) |
1419 self.actions.remove(action) |
1361 removed = True |
1420 removed = True |
1362 break |
1421 break |
1363 if not removed: |
1422 if not removed: |
1364 raise ValueError, _("Action with name %s doesn't exist!")%name |
1423 raise ValueError(_("Action with name %s doesn't exist!") % name) |
1365 setattr(cls, "removeaction", removeaction) |
1424 setattr(cls, "removeaction", removeaction) |
1366 |
1425 |
1367 def updateElementName(self, old_name, new_name): |
1426 def updateElementName(self, old_name, new_name): |
1368 if self.interface is not None: |
1427 if self.interface is not None: |
1369 for content in self.interface.getcontent(): |
1428 for content in self.interface.getcontent(): |
1447 for transition in self.gettransitionList(): |
1506 for transition in self.gettransitionList(): |
1448 search_result.extend(transition.Search(criteria, parent_infos)) |
1507 search_result.extend(transition.Search(criteria, parent_infos)) |
1449 return search_result |
1508 return search_result |
1450 setattr(cls, "Search", Search) |
1509 setattr(cls, "Search", Search) |
1451 |
1510 |
|
1511 |
1452 def setbodyType(self, body_type): |
1512 def setbodyType(self, body_type): |
1453 if body_type in ["IL", "ST", "LD", "FBD", "SFC"]: |
1513 if body_type in ["IL", "ST", "LD", "FBD", "SFC"]: |
1454 self.body.setcontent(PLCOpenParser.CreateElement(body_type, "body")) |
1514 self.body.setcontent(PLCOpenParser.CreateElement(body_type, "body")) |
1455 else: |
1515 else: |
1456 raise ValueError, "%s isn't a valid body type!"%type |
1516 raise ValueError("%s isn't a valid body type!" % type) |
|
1517 |
1457 |
1518 |
1458 def getbodyType(self): |
1519 def getbodyType(self): |
1459 return self.body.getcontent().getLocalTag() |
1520 return self.body.getcontent().getLocalTag() |
1460 |
1521 |
|
1522 |
1461 def resetexecutionOrder(self): |
1523 def resetexecutionOrder(self): |
1462 self.body.resetexecutionOrder() |
1524 self.body.resetexecutionOrder() |
1463 |
1525 |
|
1526 |
1464 def compileexecutionOrder(self): |
1527 def compileexecutionOrder(self): |
1465 self.body.compileexecutionOrder() |
1528 self.body.compileexecutionOrder() |
1466 |
1529 |
|
1530 |
1467 def setelementExecutionOrder(self, instance, new_executionOrder): |
1531 def setelementExecutionOrder(self, instance, new_executionOrder): |
1468 self.body.setelementExecutionOrder(instance, new_executionOrder) |
1532 self.body.setelementExecutionOrder(instance, new_executionOrder) |
1469 |
1533 |
|
1534 |
1470 def addinstance(self, instance): |
1535 def addinstance(self, instance): |
1471 self.body.appendcontentInstance(instance) |
1536 self.body.appendcontentInstance(instance) |
1472 |
1537 |
|
1538 |
1473 def getinstances(self): |
1539 def getinstances(self): |
1474 return self.body.getcontentInstances() |
1540 return self.body.getcontentInstances() |
1475 |
1541 |
|
1542 |
1476 def getinstance(self, id): |
1543 def getinstance(self, id): |
1477 return self.body.getcontentInstance(id) |
1544 return self.body.getcontentInstance(id) |
1478 |
1545 |
|
1546 |
1479 def getrandomInstance(self, exclude): |
1547 def getrandomInstance(self, exclude): |
1480 return self.body.getcontentRandomInstance(exclude) |
1548 return self.body.getcontentRandomInstance(exclude) |
1481 |
1549 |
|
1550 |
1482 def getinstanceByName(self, name): |
1551 def getinstanceByName(self, name): |
1483 return self.body.getcontentInstanceByName(name) |
1552 return self.body.getcontentInstanceByName(name) |
1484 |
1553 |
|
1554 |
1485 def removeinstance(self, id): |
1555 def removeinstance(self, id): |
1486 self.body.removecontentInstance(id) |
1556 self.body.removecontentInstance(id) |
1487 |
1557 |
|
1558 |
1488 def settext(self, text): |
1559 def settext(self, text): |
1489 self.body.settext(text) |
1560 self.body.settext(text) |
1490 |
1561 |
|
1562 |
1491 def gettext(self): |
1563 def gettext(self): |
1492 return self.body.gettext() |
1564 return self.body.gettext() |
|
1565 |
1493 |
1566 |
1494 def hasblock(self, name=None, block_type=None): |
1567 def hasblock(self, name=None, block_type=None): |
1495 if self.getbodyType() in ["FBD", "LD", "SFC"]: |
1568 if self.getbodyType() in ["FBD", "LD", "SFC"]: |
1496 for instance in self.getinstances(): |
1569 for instance in self.getinstances(): |
1497 if (isinstance(instance, PLCOpenParser.GetElementClass("block", "fbdObjects")) and |
1570 if isinstance(instance, PLCOpenParser.GetElementClass("block", "fbdObjects")) and \ |
1498 (TextMatched(instance.getinstanceName(), name) or TextMatched(instance.gettypeName(), block_type))): |
1571 (TextMatched(instance.getinstanceName(), name) or TextMatched(instance.gettypeName(), block_type)): |
1499 return True |
1572 return True |
1500 elif block_type is not None: |
1573 elif block_type is not None: |
1501 return self.body.hasblock(block_type) |
1574 return self.body.hasblock(block_type) |
1502 return False |
1575 return False |
1503 |
1576 |
|
1577 |
1504 def updateElementName(self, old_name, new_name): |
1578 def updateElementName(self, old_name, new_name): |
1505 self.body.updateElementName(old_name, new_name) |
1579 self.body.updateElementName(old_name, new_name) |
1506 |
1580 |
|
1581 |
1507 def updateElementAddress(self, address_model, new_leading): |
1582 def updateElementAddress(self, address_model, new_leading): |
1508 self.body.updateElementAddress(address_model, new_leading) |
1583 self.body.updateElementAddress(address_model, new_leading) |
1509 |
1584 |
1510 |
1585 |
1511 cls = PLCOpenParser.GetElementClass("transition", "transitions") |
1586 cls = PLCOpenParser.GetElementClass("transition", "transitions") |
1512 if cls: |
1587 if cls: |
1513 setattr(cls, "setbodyType", setbodyType) |
1588 setattr(cls, "setbodyType", setbodyType) |
1514 setattr(cls, "getbodyType", getbodyType) |
1589 setattr(cls, "getbodyType", getbodyType) |
1566 |
1641 |
1567 cls = PLCOpenParser.GetElementClass("body") |
1642 cls = PLCOpenParser.GetElementClass("body") |
1568 if cls: |
1643 if cls: |
1569 cls.currentExecutionOrderId = 0 |
1644 cls.currentExecutionOrderId = 0 |
1570 cls.checkedBlocksDict = {} |
1645 cls.checkedBlocksDict = {} |
|
1646 |
1571 def resetcurrentExecutionOrderId(self): |
1647 def resetcurrentExecutionOrderId(self): |
1572 object.__setattr__(self, "currentExecutionOrderId", 0) |
1648 object.__setattr__(self, "currentExecutionOrderId", 0) |
1573 setattr(cls, "resetcurrentExecutionOrderId", resetcurrentExecutionOrderId) |
1649 setattr(cls, "resetcurrentExecutionOrderId", resetcurrentExecutionOrderId) |
1574 |
1650 |
1575 def getnewExecutionOrderId(self): |
1651 def getnewExecutionOrderId(self): |
1576 object.__setattr__(self, "currentExecutionOrderId", self.currentExecutionOrderId + 1) |
1652 object.__setattr__(self, "currentExecutionOrderId", self.currentExecutionOrderId + 1) |
1577 return self.currentExecutionOrderId |
1653 return self.currentExecutionOrderId |
1578 setattr(cls, "getnewExecutionOrderId", getnewExecutionOrderId) |
1654 setattr(cls, "getnewExecutionOrderId", getnewExecutionOrderId) |
1579 |
1655 |
1580 def resetexecutionOrder(self): |
1656 def resetexecutionOrder(self): |
1581 if self.content.getLocalTag() == "FBD": |
1657 if self.content.getLocalTag() == "FBD": |
1582 for element in self.content.getcontent(): |
1658 for element in self.content.getcontent(): |
1583 if not isinstance(element, (PLCOpenParser.GetElementClass("comment", "commonObjects"), |
1659 if not isinstance(element, (PLCOpenParser.GetElementClass("comment", "commonObjects"), |
1584 PLCOpenParser.GetElementClass("connector", "commonObjects"), |
1660 PLCOpenParser.GetElementClass("connector", "commonObjects"), |
1585 PLCOpenParser.GetElementClass("continuation", "commonObjects"))): |
1661 PLCOpenParser.GetElementClass("continuation", "commonObjects"))): |
1586 element.setexecutionOrderId(0) |
1662 element.setexecutionOrderId(0) |
1587 self.checkedBlocksDict.clear() |
1663 self.checkedBlocksDict.clear() |
1588 else: |
1664 else: |
1589 raise TypeError, _("Can only generate execution order on FBD networks!") |
1665 raise TypeError(_("Can only generate execution order on FBD networks!")) |
1590 setattr(cls, "resetexecutionOrder", resetexecutionOrder) |
1666 setattr(cls, "resetexecutionOrder", resetexecutionOrder) |
1591 |
1667 |
1592 def compileexecutionOrder(self): |
1668 def compileexecutionOrder(self): |
1593 if self.content.getLocalTag() == "FBD": |
1669 if self.content.getLocalTag() == "FBD": |
1594 self.resetexecutionOrder() |
1670 self.resetexecutionOrder() |
1595 self.resetcurrentExecutionOrderId() |
1671 self.resetcurrentExecutionOrderId() |
1596 for element in self.content.getcontent(): |
1672 for element in self.content.getcontent(): |
1598 connections = element.connectionPointIn.getconnections() |
1674 connections = element.connectionPointIn.getconnections() |
1599 if connections and len(connections) == 1: |
1675 if connections and len(connections) == 1: |
1600 self.compileelementExecutionOrder(connections[0]) |
1676 self.compileelementExecutionOrder(connections[0]) |
1601 element.setexecutionOrderId(self.getnewExecutionOrderId()) |
1677 element.setexecutionOrderId(self.getnewExecutionOrderId()) |
1602 else: |
1678 else: |
1603 raise TypeError, _("Can only generate execution order on FBD networks!") |
1679 raise TypeError(_("Can only generate execution order on FBD networks!")) |
1604 setattr(cls, "compileexecutionOrder", compileexecutionOrder) |
1680 setattr(cls, "compileexecutionOrder", compileexecutionOrder) |
1605 |
1681 |
1606 def compileelementExecutionOrder(self, link): |
1682 def compileelementExecutionOrder(self, link): |
1607 if self.content.getLocalTag() == "FBD": |
1683 if self.content.getLocalTag() == "FBD": |
1608 localid = link.getrefLocalId() |
1684 localid = link.getrefLocalId() |
1609 instance = self.getcontentInstance(localid) |
1685 instance = self.getcontentInstance(localid) |
1610 self.checkedBlocksDict[localid] = True |
1686 self.checkedBlocksDict[localid] = True |
1611 if isinstance(instance, PLCOpenParser.GetElementClass("block", "fbdObjects")) and instance.getexecutionOrderId() == 0: |
1687 if isinstance(instance, PLCOpenParser.GetElementClass("block", "fbdObjects")) and instance.getexecutionOrderId() == 0: |
1612 for variable in instance.inputVariables.getvariable(): |
1688 for variable in instance.inputVariables.getvariable(): |
1613 connections = variable.connectionPointIn.getconnections() |
1689 connections = variable.connectionPointIn.getconnections() |
1614 if connections and len(connections) == 1: |
1690 if connections and len(connections) == 1: |
1615 if (self.checkedBlocksDict.has_key(connections[0].getrefLocalId()) == False): |
1691 if not connections[0].getrefLocalId() in self.checkedBlocksDict: |
1616 self.compileelementExecutionOrder(connections[0]) |
1692 self.compileelementExecutionOrder(connections[0]) |
1617 if instance.getexecutionOrderId() == 0: |
1693 if instance.getexecutionOrderId() == 0: |
1618 instance.setexecutionOrderId(self.getnewExecutionOrderId()) |
1694 instance.setexecutionOrderId(self.getnewExecutionOrderId()) |
1619 elif isinstance(instance, PLCOpenParser.GetElementClass("continuation", "commonObjects")) and instance.getexecutionOrderId() == 0: |
1695 elif isinstance(instance, PLCOpenParser.GetElementClass("continuation", "commonObjects")) and instance.getexecutionOrderId() == 0: |
1620 for tmp_instance in self.getcontentInstances(): |
1696 for tmp_instance in self.getcontentInstances(): |
1621 if (isinstance(tmp_instance, PLCOpenParser.GetElementClass("connector", "commonObjects")) and |
1697 if isinstance(tmp_instance, PLCOpenParser.GetElementClass("connector", "commonObjects")) and \ |
1622 TextMatched(tmp_instance.getname(), instance.getname()) and tmp_instance.getexecutionOrderId() == 0): |
1698 TextMatched(tmp_instance.getname(), instance.getname()) and \ |
|
1699 tmp_instance.getexecutionOrderId() == 0: |
1623 connections = tmp_instance.connectionPointIn.getconnections() |
1700 connections = tmp_instance.connectionPointIn.getconnections() |
1624 if connections and len(connections) == 1: |
1701 if connections and len(connections) == 1: |
1625 self.compileelementExecutionOrder(connections[0]) |
1702 self.compileelementExecutionOrder(connections[0]) |
1626 else: |
1703 else: |
1627 raise TypeError, _("Can only generate execution order on FBD networks!") |
1704 raise TypeError(_("Can only generate execution order on FBD networks!")) |
1628 setattr(cls, "compileelementExecutionOrder", compileelementExecutionOrder) |
1705 setattr(cls, "compileelementExecutionOrder", compileelementExecutionOrder) |
1629 |
1706 |
1630 def setelementExecutionOrder(self, instance, new_executionOrder): |
1707 def setelementExecutionOrder(self, instance, new_executionOrder): |
1631 if self.content.getLocalTag() == "FBD": |
1708 if self.content.getLocalTag() == "FBD": |
1632 old_executionOrder = instance.getexecutionOrderId() |
1709 old_executionOrder = instance.getexecutionOrderId() |
1633 if old_executionOrder is not None and old_executionOrder != 0 and new_executionOrder != 0: |
1710 if old_executionOrder is not None and old_executionOrder != 0 and new_executionOrder != 0: |
1634 for element in self.content.getcontent(): |
1711 for element in self.content.getcontent(): |
1638 element.setexecutionOrderId(element_executionOrder - 1) |
1715 element.setexecutionOrderId(element_executionOrder - 1) |
1639 if new_executionOrder <= element_executionOrder <= old_executionOrder: |
1716 if new_executionOrder <= element_executionOrder <= old_executionOrder: |
1640 element.setexecutionOrderId(element_executionOrder + 1) |
1717 element.setexecutionOrderId(element_executionOrder + 1) |
1641 instance.setexecutionOrderId(new_executionOrder) |
1718 instance.setexecutionOrderId(new_executionOrder) |
1642 else: |
1719 else: |
1643 raise TypeError, _("Can only generate execution order on FBD networks!") |
1720 raise TypeError(_("Can only generate execution order on FBD networks!")) |
1644 setattr(cls, "setelementExecutionOrder", setelementExecutionOrder) |
1721 setattr(cls, "setelementExecutionOrder", setelementExecutionOrder) |
1645 |
1722 |
1646 def appendcontentInstance(self, instance): |
1723 def appendcontentInstance(self, instance): |
1647 if self.content.getLocalTag() in ["LD","FBD","SFC"]: |
1724 if self.content.getLocalTag() in ["LD", "FBD", "SFC"]: |
1648 self.content.appendcontent(instance) |
1725 self.content.appendcontent(instance) |
1649 else: |
1726 else: |
1650 raise TypeError, _("%s body don't have instances!")%self.content.getLocalTag() |
1727 raise TypeError(_("%s body don't have instances!") % self.content.getLocalTag()) |
1651 setattr(cls, "appendcontentInstance", appendcontentInstance) |
1728 setattr(cls, "appendcontentInstance", appendcontentInstance) |
1652 |
1729 |
1653 def getcontentInstances(self): |
1730 def getcontentInstances(self): |
1654 if self.content.getLocalTag() in ["LD","FBD","SFC"]: |
1731 if self.content.getLocalTag() in ["LD", "FBD", "SFC"]: |
1655 return self.content.getcontent() |
1732 return self.content.getcontent() |
1656 else: |
1733 else: |
1657 raise TypeError, _("%s body don't have instances!")%self.content.getLocalTag() |
1734 raise TypeError(_("%s body don't have instances!") % self.content.getLocalTag()) |
1658 setattr(cls, "getcontentInstances", getcontentInstances) |
1735 setattr(cls, "getcontentInstances", getcontentInstances) |
1659 |
1736 |
1660 instance_by_id_xpath = PLCOpen_XPath("*[@localId=$localId]") |
1737 instance_by_id_xpath = PLCOpen_XPath("*[@localId=$localId]") |
1661 instance_by_name_xpath = PLCOpen_XPath("ppx:block[@instanceName=$name]") |
1738 instance_by_name_xpath = PLCOpen_XPath("ppx:block[@instanceName=$name]") |
|
1739 |
1662 def getcontentInstance(self, local_id): |
1740 def getcontentInstance(self, local_id): |
1663 if self.content.getLocalTag() in ["LD","FBD","SFC"]: |
1741 if self.content.getLocalTag() in ["LD", "FBD", "SFC"]: |
1664 instance = instance_by_id_xpath(self.content, localId=local_id) |
1742 instance = instance_by_id_xpath(self.content, localId=local_id) |
1665 if len(instance) > 0: |
1743 if len(instance) > 0: |
1666 return instance[0] |
1744 return instance[0] |
1667 return None |
1745 return None |
1668 else: |
1746 else: |
1669 raise TypeError, _("%s body don't have instances!")%self.content.getLocalTag() |
1747 raise TypeError(_("%s body don't have instances!") % self.content.getLocalTag()) |
1670 setattr(cls, "getcontentInstance", getcontentInstance) |
1748 setattr(cls, "getcontentInstance", getcontentInstance) |
1671 |
1749 |
1672 def getcontentInstancesIds(self): |
1750 def getcontentInstancesIds(self): |
1673 if self.content.getLocalTag() in ["LD","FBD","SFC"]: |
1751 if self.content.getLocalTag() in ["LD", "FBD", "SFC"]: |
1674 return OrderedDict([(instance.getlocalId(), True) |
1752 return OrderedDict([(instance.getlocalId(), True) |
1675 for instance in self.content]) |
1753 for instance in self.content]) |
1676 else: |
1754 else: |
1677 raise TypeError, _("%s body don't have instances!")%self.content.getLocalTag() |
1755 raise TypeError(_("%s body don't have instances!") % self.content.getLocalTag()) |
1678 setattr(cls, "getcontentInstancesIds", getcontentInstancesIds) |
1756 setattr(cls, "getcontentInstancesIds", getcontentInstancesIds) |
1679 |
1757 |
1680 def getcontentInstanceByName(self, name): |
1758 def getcontentInstanceByName(self, name): |
1681 if self.content.getLocalTag() in ["LD","FBD","SFC"]: |
1759 if self.content.getLocalTag() in ["LD", "FBD", "SFC"]: |
1682 instance = instance_by_name_xpath(self.content) |
1760 instance = instance_by_name_xpath(self.content) |
1683 if len(instance) > 0: |
1761 if len(instance) > 0: |
1684 return instance[0] |
1762 return instance[0] |
1685 return None |
1763 return None |
1686 else: |
1764 else: |
1687 raise TypeError, _("%s body don't have instances!")%self.content.getLocalTag() |
1765 raise TypeError(_("%s body don't have instances!") % self.content.getLocalTag()) |
1688 setattr(cls, "getcontentInstanceByName", getcontentInstanceByName) |
1766 setattr(cls, "getcontentInstanceByName", getcontentInstanceByName) |
1689 |
1767 |
1690 def removecontentInstance(self, local_id): |
1768 def removecontentInstance(self, local_id): |
1691 if self.content.getLocalTag() in ["LD","FBD","SFC"]: |
1769 if self.content.getLocalTag() in ["LD", "FBD", "SFC"]: |
1692 instance = instance_by_id_xpath(self.content, localId=local_id) |
1770 instance = instance_by_id_xpath(self.content, localId=local_id) |
1693 if len(instance) > 0: |
1771 if len(instance) > 0: |
1694 self.content.remove(instance[0]) |
1772 self.content.remove(instance[0]) |
1695 else: |
1773 else: |
1696 raise ValueError, _("Instance with id %d doesn't exist!")%id |
1774 raise ValueError(_("Instance with id %d doesn't exist!") % id) |
1697 else: |
1775 else: |
1698 raise TypeError, "%s body don't have instances!"%self.content.getLocalTag() |
1776 raise TypeError(_("%s body don't have instances!") % self.content.getLocalTag()) |
1699 setattr(cls, "removecontentInstance", removecontentInstance) |
1777 setattr(cls, "removecontentInstance", removecontentInstance) |
1700 |
1778 |
1701 def settext(self, text): |
1779 def settext(self, text): |
1702 if self.content.getLocalTag() in ["IL","ST"]: |
1780 if self.content.getLocalTag() in ["IL", "ST"]: |
1703 self.content.setanyText(text) |
1781 self.content.setanyText(text) |
1704 else: |
1782 else: |
1705 raise TypeError, _("%s body don't have text!")%self.content.getLocalTag() |
1783 raise TypeError(_("%s body don't have text!") % self.content.getLocalTag()) |
1706 setattr(cls, "settext", settext) |
1784 setattr(cls, "settext", settext) |
1707 |
1785 |
1708 def gettext(self): |
1786 def gettext(self): |
1709 if self.content.getLocalTag() in ["IL","ST"]: |
1787 if self.content.getLocalTag() in ["IL", "ST"]: |
1710 return self.content.getanyText() |
1788 return self.content.getanyText() |
1711 else: |
1789 else: |
1712 raise TypeError, _("%s body don't have text!")%self.content.getLocalTag() |
1790 raise TypeError(_("%s body don't have text!") % self.content.getLocalTag()) |
1713 setattr(cls, "gettext", gettext) |
1791 setattr(cls, "gettext", gettext) |
1714 |
1792 |
1715 def hasblock(self, block_type): |
1793 def hasblock(self, block_type): |
1716 if self.content.getLocalTag() in ["IL","ST"]: |
1794 if self.content.getLocalTag() in ["IL", "ST"]: |
1717 return self.content.hasblock(block_type) |
1795 return self.content.hasblock(block_type) |
1718 else: |
1796 else: |
1719 raise TypeError, _("%s body don't have text!")%self.content.getLocalTag() |
1797 raise TypeError(_("%s body don't have text!") % self.content.getLocalTag()) |
1720 setattr(cls, "hasblock", hasblock) |
1798 setattr(cls, "hasblock", hasblock) |
1721 |
1799 |
1722 def updateElementName(self, old_name, new_name): |
1800 def updateElementName(self, old_name, new_name): |
1723 if self.content.getLocalTag() in ["IL", "ST"]: |
1801 if self.content.getLocalTag() in ["IL", "ST"]: |
1724 self.content.updateElementName(old_name, new_name) |
1802 self.content.updateElementName(old_name, new_name) |
1725 else: |
1803 else: |
1726 for element in self.content.getcontent(): |
1804 for element in self.content.getcontent(): |
1743 for element in self.content.getcontent(): |
1821 for element in self.content.getcontent(): |
1744 search_result.extend(element.Search(criteria, parent_infos)) |
1822 search_result.extend(element.Search(criteria, parent_infos)) |
1745 return search_result |
1823 return search_result |
1746 setattr(cls, "Search", Search) |
1824 setattr(cls, "Search", Search) |
1747 |
1825 |
|
1826 |
1748 def getx(self): |
1827 def getx(self): |
1749 return self.position.getx() |
1828 return self.position.getx() |
1750 |
1829 |
|
1830 |
1751 def gety(self): |
1831 def gety(self): |
1752 return self.position.gety() |
1832 return self.position.gety() |
1753 |
1833 |
|
1834 |
1754 def setx(self, x): |
1835 def setx(self, x): |
1755 self.position.setx(x) |
1836 self.position.setx(x) |
1756 |
1837 |
|
1838 |
1757 def sety(self, y): |
1839 def sety(self, y): |
1758 self.position.sety(y) |
1840 self.position.sety(y) |
1759 |
1841 |
|
1842 |
1760 def _getBoundingBox(self): |
1843 def _getBoundingBox(self): |
1761 return rect(self.getx(), self.gety(), self.getwidth(), self.getheight()) |
1844 return rect(self.getx(), self.gety(), self.getwidth(), self.getheight()) |
|
1845 |
1762 |
1846 |
1763 def _getConnectionsBoundingBox(connectionPointIn): |
1847 def _getConnectionsBoundingBox(connectionPointIn): |
1764 bbox = rect() |
1848 bbox = rect() |
1765 connections = connectionPointIn.getconnections() |
1849 connections = connectionPointIn.getconnections() |
1766 if connections is not None: |
1850 if connections is not None: |
1767 for connection in connections: |
1851 for connection in connections: |
1768 for x, y in connection.getpoints(): |
1852 for x, y in connection.getpoints(): |
1769 bbox.update(x, y) |
1853 bbox.update(x, y) |
1770 return bbox |
1854 return bbox |
1771 |
1855 |
|
1856 |
1772 def _getBoundingBoxSingle(self): |
1857 def _getBoundingBoxSingle(self): |
1773 bbox = _getBoundingBox(self) |
1858 bbox = _getBoundingBox(self) |
1774 if self.connectionPointIn is not None: |
1859 if self.connectionPointIn is not None: |
1775 bbox.union(_getConnectionsBoundingBox(self.connectionPointIn)) |
1860 bbox.union(_getConnectionsBoundingBox(self.connectionPointIn)) |
1776 return bbox |
1861 return bbox |
1777 |
1862 |
|
1863 |
1778 def _getBoundingBoxMultiple(self): |
1864 def _getBoundingBoxMultiple(self): |
1779 bbox = _getBoundingBox(self) |
1865 bbox = _getBoundingBox(self) |
1780 for connectionPointIn in self.getconnectionPointIn(): |
1866 for connectionPointIn in self.getconnectionPointIn(): |
1781 bbox.union(_getConnectionsBoundingBox(connectionPointIn)) |
1867 bbox.union(_getConnectionsBoundingBox(connectionPointIn)) |
1782 return bbox |
1868 return bbox |
1783 |
1869 |
|
1870 |
1784 def _filterConnections(connectionPointIn, localId, connections): |
1871 def _filterConnections(connectionPointIn, localId, connections): |
1785 in_connections = connectionPointIn.getconnections() |
1872 in_connections = connectionPointIn.getconnections() |
1786 if in_connections is not None: |
1873 if in_connections is not None: |
1787 for connection in in_connections: |
1874 for connection in in_connections: |
1788 connected = connection.getrefLocalId() |
1875 connected = connection.getrefLocalId() |
1789 if not connections.has_key((localId, connected)) and \ |
1876 if not (localId, connected) in connections and \ |
1790 not connections.has_key((connected, localId)): |
1877 not (connected, localId) in connections: |
1791 connectionPointIn.remove(connection) |
1878 connectionPointIn.remove(connection) |
|
1879 |
1792 |
1880 |
1793 def _filterConnectionsSingle(self, connections): |
1881 def _filterConnectionsSingle(self, connections): |
1794 if self.connectionPointIn is not None: |
1882 if self.connectionPointIn is not None: |
1795 _filterConnections(self.connectionPointIn, self.localId, connections) |
1883 _filterConnections(self.connectionPointIn, self.localId, connections) |
1796 |
1884 |
|
1885 |
1797 def _filterConnectionsMultiple(self, connections): |
1886 def _filterConnectionsMultiple(self, connections): |
1798 for connectionPointIn in self.getconnectionPointIn(): |
1887 for connectionPointIn in self.getconnectionPointIn(): |
1799 _filterConnections(connectionPointIn, self.localId, connections) |
1888 _filterConnections(connectionPointIn, self.localId, connections) |
1800 |
1889 |
|
1890 |
1801 def _getconnectionsdefinition(instance, connections_end): |
1891 def _getconnectionsdefinition(instance, connections_end): |
1802 local_id = instance.getlocalId() |
1892 local_id = instance.getlocalId() |
1803 return dict([((local_id, end), True) for end in connections_end]) |
1893 return dict([((local_id, end), True) for end in connections_end]) |
|
1894 |
1804 |
1895 |
1805 def _updateConnectionsId(connectionPointIn, translation): |
1896 def _updateConnectionsId(connectionPointIn, translation): |
1806 connections_end = [] |
1897 connections_end = [] |
1807 connections = connectionPointIn.getconnections() |
1898 connections = connectionPointIn.getconnections() |
1808 if connections is not None: |
1899 if connections is not None: |
1811 new_reflocalId = translation.get(refLocalId, refLocalId) |
1902 new_reflocalId = translation.get(refLocalId, refLocalId) |
1812 connection.setrefLocalId(new_reflocalId) |
1903 connection.setrefLocalId(new_reflocalId) |
1813 connections_end.append(new_reflocalId) |
1904 connections_end.append(new_reflocalId) |
1814 return connections_end |
1905 return connections_end |
1815 |
1906 |
|
1907 |
1816 def _updateConnectionsIdSingle(self, translation): |
1908 def _updateConnectionsIdSingle(self, translation): |
1817 connections_end = [] |
1909 connections_end = [] |
1818 if self.connectionPointIn is not None: |
1910 if self.connectionPointIn is not None: |
1819 connections_end = _updateConnectionsId(self.connectionPointIn, translation) |
1911 connections_end = _updateConnectionsId(self.connectionPointIn, translation) |
1820 return _getconnectionsdefinition(self, connections_end) |
1912 return _getconnectionsdefinition(self, connections_end) |
1821 |
1913 |
|
1914 |
1822 def _updateConnectionsIdMultiple(self, translation): |
1915 def _updateConnectionsIdMultiple(self, translation): |
1823 connections_end = [] |
1916 connections_end = [] |
1824 for connectionPointIn in self.getconnectionPointIn(): |
1917 for connectionPointIn in self.getconnectionPointIn(): |
1825 connections_end.extend(_updateConnectionsId(connectionPointIn, translation)) |
1918 connections_end.extend(_updateConnectionsId(connectionPointIn, translation)) |
1826 return _getconnectionsdefinition(self, connections_end) |
1919 return _getconnectionsdefinition(self, connections_end) |
1827 |
1920 |
|
1921 |
1828 def _translate(self, dx, dy): |
1922 def _translate(self, dx, dy): |
1829 self.setx(self.getx() + dx) |
1923 self.setx(self.getx() + dx) |
1830 self.sety(self.gety() + dy) |
1924 self.sety(self.gety() + dy) |
1831 |
1925 |
|
1926 |
1832 def _translateConnections(connectionPointIn, dx, dy): |
1927 def _translateConnections(connectionPointIn, dx, dy): |
1833 connections = connectionPointIn.getconnections() |
1928 connections = connectionPointIn.getconnections() |
1834 if connections is not None: |
1929 if connections is not None: |
1835 for connection in connections: |
1930 for connection in connections: |
1836 for position in connection.getposition(): |
1931 for position in connection.getposition(): |
1837 position.setx(position.getx() + dx) |
1932 position.setx(position.getx() + dx) |
1838 position.sety(position.gety() + dy) |
1933 position.sety(position.gety() + dy) |
1839 |
1934 |
|
1935 |
1840 def _translateSingle(self, dx, dy): |
1936 def _translateSingle(self, dx, dy): |
1841 _translate(self, dx, dy) |
1937 _translate(self, dx, dy) |
1842 if self.connectionPointIn is not None: |
1938 if self.connectionPointIn is not None: |
1843 _translateConnections(self.connectionPointIn, dx, dy) |
1939 _translateConnections(self.connectionPointIn, dx, dy) |
1844 |
1940 |
|
1941 |
1845 def _translateMultiple(self, dx, dy): |
1942 def _translateMultiple(self, dx, dy): |
1846 _translate(self, dx, dy) |
1943 _translate(self, dx, dy) |
1847 for connectionPointIn in self.getconnectionPointIn(): |
1944 for connectionPointIn in self.getconnectionPointIn(): |
1848 _translateConnections(connectionPointIn, dx, dy) |
1945 _translateConnections(connectionPointIn, dx, dy) |
1849 |
1946 |
|
1947 |
1850 def _updateElementName(self, old_name, new_name): |
1948 def _updateElementName(self, old_name, new_name): |
1851 pass |
1949 pass |
1852 |
1950 |
|
1951 |
1853 def _updateElementAddress(self, address_model, new_leading): |
1952 def _updateElementAddress(self, address_model, new_leading): |
1854 pass |
1953 pass |
1855 |
1954 |
|
1955 |
1856 def _SearchInElement(self, criteria, parent_infos=[]): |
1956 def _SearchInElement(self, criteria, parent_infos=[]): |
1857 return [] |
1957 return [] |
|
1958 |
1858 |
1959 |
1859 _connectionsFunctions = { |
1960 _connectionsFunctions = { |
1860 "bbox": {"none": _getBoundingBox, |
1961 "bbox": {"none": _getBoundingBox, |
1861 "single": _getBoundingBoxSingle, |
1962 "single": _getBoundingBoxSingle, |
1862 "multiple": _getBoundingBoxMultiple}, |
1963 "multiple": _getBoundingBoxMultiple}, |
1863 "translate": {"none": _translate, |
1964 "translate": {"none": _translate, |
1864 "single": _translateSingle, |
1965 "single": _translateSingle, |
1865 "multiple": _translateMultiple}, |
1966 "multiple": _translateMultiple}, |
1866 "filter": {"none": lambda self, connections: None, |
1967 "filter": {"none": lambda self, connections: None, |
1867 "single": _filterConnectionsSingle, |
1968 "single": _filterConnectionsSingle, |
1868 "multiple": _filterConnectionsMultiple}, |
1969 "multiple": _filterConnectionsMultiple}, |
1869 "update": {"none": lambda self, translation: {}, |
1970 "update": {"none": lambda self, translation: {}, |
1870 "single": _updateConnectionsIdSingle, |
1971 "single": _updateConnectionsIdSingle, |
1871 "multiple": _updateConnectionsIdMultiple}, |
1972 "multiple": _updateConnectionsIdMultiple}, |
1872 } |
1973 } |
|
1974 |
1873 |
1975 |
1874 def _initElementClass(name, parent, connectionPointInType="none"): |
1976 def _initElementClass(name, parent, connectionPointInType="none"): |
1875 cls = PLCOpenParser.GetElementClass(name, parent) |
1977 cls = PLCOpenParser.GetElementClass(name, parent) |
1876 if cls: |
1978 if cls: |
1877 setattr(cls, "getx", getx) |
1979 setattr(cls, "getx", getx) |
1956 setattr(cls, "Search", Search) |
2059 setattr(cls, "Search", Search) |
1957 |
2060 |
1958 _initElementClass("leftPowerRail", "ldObjects") |
2061 _initElementClass("leftPowerRail", "ldObjects") |
1959 _initElementClass("rightPowerRail", "ldObjects", "multiple") |
2062 _initElementClass("rightPowerRail", "ldObjects", "multiple") |
1960 |
2063 |
|
2064 |
1961 def _UpdateLDElementName(self, old_name, new_name): |
2065 def _UpdateLDElementName(self, old_name, new_name): |
1962 if TextMatched(self.variable, old_name): |
2066 if TextMatched(self.variable, old_name): |
1963 self.variable = new_name |
2067 self.variable = new_name |
1964 |
2068 |
|
2069 |
1965 def _UpdateLDElementAddress(self, address_model, new_leading): |
2070 def _UpdateLDElementAddress(self, address_model, new_leading): |
1966 self.variable = update_address(self.variable, address_model, new_leading) |
2071 self.variable = update_address(self.variable, address_model, new_leading) |
|
2072 |
1967 |
2073 |
1968 def _getSearchInLDElement(ld_element_type): |
2074 def _getSearchInLDElement(ld_element_type): |
1969 def SearchInLDElement(self, criteria, parent_infos=[]): |
2075 def SearchInLDElement(self, criteria, parent_infos=[]): |
1970 return _Search([("reference", self.variable)], criteria, parent_infos + [ld_element_type, self.getlocalId()]) |
2076 return _Search([("reference", self.variable)], criteria, parent_infos + [ld_element_type, self.getlocalId()]) |
1971 return SearchInLDElement |
2077 return SearchInLDElement |
|
2078 |
1972 |
2079 |
1973 cls = _initElementClass("contact", "ldObjects", "single") |
2080 cls = _initElementClass("contact", "ldObjects", "single") |
1974 if cls: |
2081 if cls: |
1975 setattr(cls, "updateElementName", _UpdateLDElementName) |
2082 setattr(cls, "updateElementName", _UpdateLDElementName) |
1976 setattr(cls, "updateElementAddress", _UpdateLDElementAddress) |
2083 setattr(cls, "updateElementAddress", _UpdateLDElementAddress) |
2033 condition_connection = self.getconditionConnection() |
2140 condition_connection = self.getconditionConnection() |
2034 if condition_connection is not None: |
2141 if condition_connection is not None: |
2035 bbox.union(_getConnectionsBoundingBox(condition_connection)) |
2142 bbox.union(_getConnectionsBoundingBox(condition_connection)) |
2036 return bbox |
2143 return bbox |
2037 setattr(cls, "getBoundingBox", getBoundingBox) |
2144 setattr(cls, "getBoundingBox", getBoundingBox) |
2038 |
2145 |
2039 def translate(self, dx, dy): |
2146 def translate(self, dx, dy): |
2040 _translateSingle(self, dx, dy) |
2147 _translateSingle(self, dx, dy) |
2041 condition_connection = self.getconditionConnection() |
2148 condition_connection = self.getconditionConnection() |
2042 if condition_connection is not None: |
2149 if condition_connection is not None: |
2043 _translateConnections(condition_connection, dx, dy) |
2150 _translateConnections(condition_connection, dx, dy) |
2044 setattr(cls, "translate", translate) |
2151 setattr(cls, "translate", translate) |
2045 |
2152 |
2046 def filterConnections(self, connections): |
2153 def filterConnections(self, connections): |
2047 _filterConnectionsSingle(self, connections) |
2154 _filterConnectionsSingle(self, connections) |
2048 condition_connection = self.getconditionConnection() |
2155 condition_connection = self.getconditionConnection() |
2049 if condition_connection is not None: |
2156 if condition_connection is not None: |
2050 _filterConnections(condition_connection, self.localId, connections) |
2157 _filterConnections(condition_connection, self.localId, connections) |
2051 setattr(cls, "filterConnections", filterConnections) |
2158 setattr(cls, "filterConnections", filterConnections) |
2052 |
2159 |
2053 def updateConnectionsId(self, translation): |
2160 def updateConnectionsId(self, translation): |
2054 connections_end = [] |
2161 connections_end = [] |
2055 if self.connectionPointIn is not None: |
2162 if self.connectionPointIn is not None: |
2056 connections_end = _updateConnectionsId(self.connectionPointIn, translation) |
2163 connections_end = _updateConnectionsId(self.connectionPointIn, translation) |
2057 condition_connection = self.getconditionConnection() |
2164 condition_connection = self.getconditionConnection() |
2316 setattr(cls, "removeconnection", removeconnection) |
2428 setattr(cls, "removeconnection", removeconnection) |
2317 |
2429 |
2318 def removeconnections(self): |
2430 def removeconnections(self): |
2319 self.content = None |
2431 self.content = None |
2320 setattr(cls, "removeconnections", removeconnections) |
2432 setattr(cls, "removeconnections", removeconnections) |
2321 |
2433 |
2322 connection_xpath = PLCOpen_XPath("ppx:connection") |
2434 connection_xpath = PLCOpen_XPath("ppx:connection") |
2323 connection_by_position_xpath = PLCOpen_XPath("ppx:connection[position()=$pos]") |
2435 connection_by_position_xpath = PLCOpen_XPath("ppx:connection[position()=$pos]") |
|
2436 |
2324 def getconnections(self): |
2437 def getconnections(self): |
2325 return connection_xpath(self) |
2438 return connection_xpath(self) |
2326 setattr(cls, "getconnections", getconnections) |
2439 setattr(cls, "getconnections", getconnections) |
2327 |
2440 |
2328 def getconnection(self, idx): |
2441 def getconnection(self, idx): |
2329 connection = connection_by_position_xpath(self, pos=idx+1) |
2442 connection = connection_by_position_xpath(self, pos=idx+1) |
2330 if len(connection) > 0: |
2443 if len(connection) > 0: |
2331 return connection[0] |
2444 return connection[0] |
2332 return None |
2445 return None |
2333 setattr(cls, "getconnection", getconnection) |
2446 setattr(cls, "getconnection", getconnection) |
2334 |
2447 |
2335 def setconnectionId(self, idx, local_id): |
2448 def setconnectionId(self, idx, local_id): |
2336 connection = self.getconnection(idx) |
2449 connection = self.getconnection(idx) |
2337 if connection is not None: |
2450 if connection is not None: |
2338 connection.setrefLocalId(local_id) |
2451 connection.setrefLocalId(local_id) |
2339 setattr(cls, "setconnectionId", setconnectionId) |
2452 setattr(cls, "setconnectionId", setconnectionId) |
2340 |
2453 |
2341 def getconnectionId(self, idx): |
2454 def getconnectionId(self, idx): |
2342 connection = self.getconnection(idx) |
2455 connection = self.getconnection(idx) |
2343 if connection is not None: |
2456 if connection is not None: |
2344 return connection.getrefLocalId() |
2457 return connection.getrefLocalId() |
2345 return None |
2458 return None |
2346 setattr(cls, "getconnectionId", getconnectionId) |
2459 setattr(cls, "getconnectionId", getconnectionId) |
2347 |
2460 |
2348 def setconnectionPoints(self, idx, points): |
2461 def setconnectionPoints(self, idx, points): |
2349 connection = self.getconnection(idx) |
2462 connection = self.getconnection(idx) |
2350 if connection is not None: |
2463 if connection is not None: |
2351 connection.setpoints(points) |
2464 connection.setpoints(points) |
2352 setattr(cls, "setconnectionPoints", setconnectionPoints) |
2465 setattr(cls, "setconnectionPoints", setconnectionPoints) |