author | lbessard |
Tue, 10 Jul 2007 14:29:31 +0200 | |
changeset 31 | d833bf7567b1 |
parent 28 | fc23e1f415d8 |
child 42 | 4a8400732001 |
permissions | -rw-r--r-- |
0 | 1 |
#!/usr/bin/env python |
2 |
# -*- coding: utf-8 -*- |
|
3 |
||
4 |
#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor |
|
5 |
#based on the plcopen standard. |
|
6 |
# |
|
7 |
#Copyright (C): Edouard TISSERANT and Laurent BESSARD |
|
8 |
# |
|
9 |
#See COPYING file for copyrights details. |
|
10 |
# |
|
11 |
#This library is free software; you can redistribute it and/or |
|
5 | 12 |
#modify it under the terms of the GNU General Public |
0 | 13 |
#License as published by the Free Software Foundation; either |
14 |
#version 2.1 of the License, or (at your option) any later version. |
|
15 |
# |
|
16 |
#This library is distributed in the hope that it will be useful, |
|
17 |
#but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18 |
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
19 |
#Lesser General Public License for more details. |
|
20 |
# |
|
5 | 21 |
#You should have received a copy of the GNU General Public |
0 | 22 |
#License along with this library; if not, write to the Free Software |
23 |
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
24 |
||
25 |
from wxPython.wx import * |
|
26 |
import wx |
|
27 |
from types import * |
|
28 |
||
29 |
from plcopen.structures import * |
|
30 |
from graphics.GraphicCommons import * |
|
31 |
from graphics.FBD_Objects import * |
|
32 |
from Viewer import * |
|
27 | 33 |
from Dialogs import * |
0 | 34 |
|
35 |
def ExtractNextBlocks(block, block_list): |
|
36 |
current_list = [block] |
|
37 |
while len(current_list) > 0: |
|
38 |
next_list = [] |
|
39 |
for current in current_list: |
|
40 |
connectors = current.GetConnectors() |
|
41 |
input_connectors = [] |
|
42 |
if isinstance(current, LD_PowerRail) and current.GetType() == RIGHTRAIL: |
|
43 |
input_connectors = connectors |
|
44 |
else: |
|
45 |
if "inputs" in connectors: |
|
46 |
input_connectors = connectors["inputs"] |
|
47 |
if "input" in connectors: |
|
48 |
input_connectors = [connectors["input"]] |
|
49 |
for connector in input_connectors: |
|
50 |
for wire, handle in connector.GetWires(): |
|
51 |
next = wire.EndConnected.GetParentBlock() |
|
52 |
if not isinstance(next, LD_PowerRail) and next not in block_list: |
|
53 |
block_list.append(next) |
|
54 |
next_list.append(next) |
|
55 |
current_list = next_list |
|
56 |
||
27 | 57 |
def CalcBranchSize(elements, stops): |
0 | 58 |
branch_size = 0 |
27 | 59 |
stop_list = stops |
60 |
for stop in stops: |
|
61 |
ExtractNextBlocks(stop, stop_list) |
|
0 | 62 |
element_tree = {} |
63 |
for element in elements: |
|
64 |
if element not in element_tree: |
|
65 |
element_tree[element] = {"parents":["start"], "children":[], "weight":None} |
|
66 |
GenerateTree(element, element_tree, stop_list) |
|
67 |
elif element_tree[element]: |
|
68 |
element_tree[element]["parents"].append("start") |
|
27 | 69 |
remove_stops = {"start":[], "stop":[]} |
0 | 70 |
for element, values in element_tree.items(): |
27 | 71 |
if "stop" in values["children"]: |
72 |
removed = [] |
|
73 |
for child in values["children"]: |
|
74 |
if child != "stop": |
|
75 |
## if child in elements: |
|
76 |
## RemoveElement(child, element_tree) |
|
77 |
## removed.append(child) |
|
78 |
if "start" in element_tree[child]["parents"]: |
|
79 |
if element not in remove_stops["stop"]: |
|
80 |
remove_stops["stop"].append(element) |
|
81 |
if child not in remove_stops["start"]: |
|
82 |
remove_stops["start"].append(child) |
|
83 |
for child in removed: |
|
84 |
values["children"].remove(child) |
|
85 |
for element in remove_stops["start"]: |
|
86 |
element_tree[element]["parents"].remove("start") |
|
87 |
for element in remove_stops["stop"]: |
|
88 |
element_tree[element]["children"].remove("stop") |
|
89 |
for element, values in element_tree.items(): |
|
90 |
if values and "stop" in values["children"]: |
|
0 | 91 |
CalcWeight(element, element_tree) |
92 |
if values["weight"]: |
|
93 |
branch_size += values["weight"] |
|
94 |
else: |
|
95 |
return 1 |
|
27 | 96 |
#print branch_size |
0 | 97 |
return branch_size |
98 |
||
99 |
def RemoveElement(remove, element_tree): |
|
100 |
if remove in element_tree and element_tree[remove]: |
|
101 |
for child in element_tree[remove]["children"]: |
|
102 |
if child != "stop": |
|
103 |
RemoveElement(child, element_tree) |
|
27 | 104 |
element_tree.pop(remove) |
105 |
## element_tree[remove] = None |
|
0 | 106 |
|
107 |
def GenerateTree(element, element_tree, stop_list): |
|
108 |
if element in element_tree: |
|
109 |
connectors = element.GetConnectors() |
|
110 |
input_connectors = [] |
|
111 |
if isinstance(element, LD_PowerRail) and element.GetType() == RIGHTRAIL: |
|
112 |
input_connectors = connectors |
|
113 |
else: |
|
114 |
if "inputs" in connectors: |
|
115 |
input_connectors = connectors["inputs"] |
|
116 |
if "input" in connectors: |
|
117 |
input_connectors = [connectors["input"]] |
|
118 |
for connector in input_connectors: |
|
119 |
for wire, handle in connector.GetWires(): |
|
120 |
next = wire.EndConnected.GetParentBlock() |
|
121 |
if isinstance(next, LD_PowerRail) and next.GetType() == LEFTRAIL or next in stop_list: |
|
27 | 122 |
## for remove in element_tree[element]["children"]: |
123 |
## RemoveElement(remove, element_tree) |
|
124 |
## element_tree[element]["children"] = ["stop"] |
|
125 |
element_tree[element]["children"].append("stop") |
|
126 |
## elif element_tree[element]["children"] == ["stop"]: |
|
127 |
## element_tree[next] = None |
|
0 | 128 |
elif next not in element_tree or element_tree[next]: |
129 |
element_tree[element]["children"].append(next) |
|
130 |
if next in element_tree: |
|
131 |
element_tree[next]["parents"].append(element) |
|
132 |
else: |
|
133 |
element_tree[next] = {"parents":[element], "children":[], "weight":None} |
|
134 |
GenerateTree(next, element_tree, stop_list) |
|
135 |
||
136 |
def CalcWeight(element, element_tree): |
|
137 |
weight = 0 |
|
138 |
parts = None |
|
139 |
if element in element_tree: |
|
140 |
for parent in element_tree[element]["parents"]: |
|
141 |
if parent == "start": |
|
142 |
weight += 1 |
|
143 |
elif parent in element_tree: |
|
144 |
if not parts: |
|
145 |
parts = len(element_tree[parent]["children"]) |
|
146 |
else: |
|
147 |
parts = min(parts, len(element_tree[parent]["children"])) |
|
148 |
if not element_tree[parent]["weight"]: |
|
149 |
CalcWeight(parent, element_tree) |
|
150 |
if element_tree[parent]["weight"]: |
|
151 |
weight += element_tree[parent]["weight"] |
|
152 |
else: |
|
153 |
element_tree[element]["weight"] = None |
|
154 |
return |
|
155 |
else: |
|
156 |
element_tree[element]["weight"] = None |
|
157 |
return |
|
158 |
if not parts: |
|
159 |
parts = 1 |
|
160 |
element_tree[element]["weight"] = max(1, weight / parts) |
|
161 |
||
162 |
||
163 |
#------------------------------------------------------------------------------- |
|
164 |
# Ladder Diagram Graphic elements Viewer class |
|
165 |
#------------------------------------------------------------------------------- |
|
166 |
||
167 |
""" |
|
168 |
Class derived from Viewer class that implements a Viewer of Ladder Diagram |
|
169 |
""" |
|
170 |
||
171 |
class LD_Viewer(Viewer): |
|
172 |
||
173 |
def __init__(self, parent, window, controler): |
|
174 |
Viewer.__init__(self, parent, window, controler) |
|
175 |
self.Rungs = [] |
|
176 |
self.Comments = [] |
|
177 |
||
178 |
#------------------------------------------------------------------------------- |
|
179 |
# Refresh functions |
|
180 |
#------------------------------------------------------------------------------- |
|
181 |
||
182 |
def RefreshView(self): |
|
183 |
Viewer.RefreshView(self) |
|
184 |
for i, rung in enumerate(self.Rungs): |
|
185 |
bbox = rung.GetBoundingBox() |
|
186 |
if i < len(self.Comments): |
|
187 |
pos = self.Comments[i].GetPosition() |
|
188 |
if pos[1] > bbox.y: |
|
189 |
self.Comment.insert(i, None) |
|
190 |
else: |
|
191 |
self.Comment.insert(i, None) |
|
192 |
||
193 |
def loadInstance(self, instance, ids): |
|
194 |
Viewer.loadInstance(self, instance, ids) |
|
195 |
if instance["type"] == "leftPowerRail": |
|
196 |
element = self.FindElementById(instance["id"]) |
|
197 |
rung = Graphic_Group(self) |
|
198 |
rung.SelectElement(element) |
|
199 |
self.Rungs.append(rung) |
|
200 |
elif instance["type"] == "rightPowerRail": |
|
201 |
rungs = [] |
|
202 |
for connector in instance["connectors"]: |
|
203 |
for link in connector["links"]: |
|
204 |
connected = self.FindElementById(link["refLocalId"]) |
|
205 |
rung = self.FindRung(connected) |
|
206 |
if rung not in rungs: |
|
207 |
rungs.append(rung) |
|
208 |
if len(rungs) > 1: |
|
209 |
raise "ValueError", "Ladder element with id %d is on more than one rung."%instance["id"] |
|
210 |
element = self.FindElementById(instance["id"]) |
|
211 |
self.Rungs[rungs[0]].SelectElement(element) |
|
212 |
for connector in element.GetConnectors(): |
|
213 |
for wire, num in connector.GetWires(): |
|
214 |
self.Rungs[rungs[0]].SelectElement(wire) |
|
215 |
self.RefreshPosition(element) |
|
216 |
elif instance["type"] in ["contact", "coil"]: |
|
217 |
rungs = [] |
|
218 |
for link in instance["connectors"]["input"]["links"]: |
|
219 |
connected = self.FindElementById(link["refLocalId"]) |
|
220 |
rung = self.FindRung(connected) |
|
221 |
if rung not in rungs: |
|
222 |
rungs.append(rung) |
|
223 |
if len(rungs) > 1: |
|
224 |
raise "ValueError", "Ladder element with id %d is on more than one rung."%instance["id"] |
|
225 |
element = self.FindElementById(instance["id"]) |
|
226 |
self.Rungs[rungs[0]].SelectElement(element) |
|
227 |
for wire, num in element.GetConnectors()["input"].GetWires(): |
|
228 |
self.Rungs[rungs[0]].SelectElement(wire) |
|
229 |
self.RefreshPosition(element) |
|
230 |
elif instance["type"] == "comment": |
|
231 |
element = self.FindElementById(instance["id"]) |
|
232 |
pos = element.GetPosition() |
|
233 |
i = 0 |
|
234 |
inserted = False |
|
235 |
while i < len(self.Comments) and not inserted: |
|
236 |
ipos = self.Comments[i].GetPosition() |
|
237 |
if pos[1] < ipos[1]: |
|
238 |
self.Comments.insert(i, element) |
|
239 |
inserted = True |
|
240 |
i += 1 |
|
241 |
if not inserted: |
|
242 |
self.Comments.append(element) |
|
243 |
||
244 |
#------------------------------------------------------------------------------- |
|
245 |
# Search Element functions |
|
246 |
#------------------------------------------------------------------------------- |
|
247 |
||
248 |
def FindRung(self, element): |
|
249 |
for i, rung in enumerate(self.Rungs): |
|
250 |
if rung.IsElementIn(element): |
|
251 |
return i |
|
252 |
return None |
|
253 |
||
254 |
def FindElement(self, pos): |
|
27 | 255 |
if self.GetDrawingMode() == FREEDRAWING_MODE: |
256 |
return Viewer.FindElement(self, pos) |
|
257 |
||
0 | 258 |
elements = [] |
259 |
for element in self.Elements: |
|
260 |
if element.HitTest(pos) or element.TestHandle(pos) != (0, 0): |
|
261 |
elements.append(element) |
|
262 |
if len(elements) == 1: |
|
263 |
return elements[0] |
|
264 |
elif len(elements) > 1: |
|
265 |
group = Graphic_Group(self) |
|
266 |
for element in elements: |
|
267 |
if element in self.Blocks: |
|
268 |
return element |
|
269 |
group.SelectElement(element) |
|
270 |
return group |
|
271 |
return None |
|
272 |
||
273 |
def SearchElements(self, bbox): |
|
27 | 274 |
if self.GetDrawingMode() == FREEDRAWING_MODE: |
275 |
return Viewer.SearchElements(self, bbox) |
|
276 |
||
0 | 277 |
elements = [] |
278 |
for element in self.Blocks: |
|
279 |
element_bbox = element.GetBoundingBox() |
|
280 |
if element_bbox.x >= bbox.x and element_bbox.y >= bbox.y and element_bbox.x + element_bbox.width <= bbox.x + bbox.width and element_bbox.y + element_bbox.height <= bbox.y + bbox.height: |
|
281 |
elements.append(element) |
|
282 |
return elements |
|
283 |
||
284 |
#------------------------------------------------------------------------------- |
|
285 |
# Mouse event functions |
|
286 |
#------------------------------------------------------------------------------- |
|
287 |
||
288 |
def OnViewerLeftDown(self, event): |
|
27 | 289 |
if self.GetDrawingMode() == FREEDRAWING_MODE: |
290 |
Viewer.OnViewerLeftDown(self, event) |
|
291 |
elif self.Mode == MODE_SELECTION: |
|
292 |
dc = self.GetLogicalDC() |
|
293 |
pos = event.GetLogicalPosition(dc) |
|
0 | 294 |
element = self.FindElement(pos) |
295 |
if self.SelectedElement: |
|
296 |
if self.SelectedElement in self.Elements: |
|
297 |
if self.SelectedElement != element: |
|
298 |
if self.SelectedElement in self.Wires: |
|
299 |
self.SelectedElement.SetSelectedSegment(None) |
|
300 |
else: |
|
301 |
self.SelectedElement.SetSelected(False) |
|
302 |
else: |
|
303 |
self.SelectedElement = None |
|
304 |
elif element and element not in self.Elements: |
|
305 |
if self.SelectedElement.GetElements() != element.GetElements(): |
|
306 |
for elt in self.SelectedElement.GetElements(): |
|
307 |
if elt in self.Wires: |
|
308 |
elt.SetSelectedSegment(None) |
|
309 |
self.SelectedElement.SetSelected(False) |
|
310 |
self.SelectedElement = None |
|
311 |
else: |
|
312 |
for elt in self.SelectedElement.GetElements(): |
|
313 |
if elt in self.Wires: |
|
314 |
elt.SetSelectedSegment(None) |
|
315 |
self.SelectedElement.SetSelected(False) |
|
316 |
self.SelectedElement = None |
|
317 |
self.Refresh() |
|
318 |
if element: |
|
319 |
self.SelectedElement = element |
|
27 | 320 |
self.SelectedElement.OnLeftDown(event, dc, self.Scaling) |
0 | 321 |
self.Refresh() |
322 |
else: |
|
323 |
self.rubberBand.Reset() |
|
27 | 324 |
self.rubberBand.OnLeftDown(event, dc, self.Scaling) |
0 | 325 |
event.Skip() |
326 |
||
327 |
def OnViewerLeftUp(self, event): |
|
27 | 328 |
if self.GetDrawingMode() == FREEDRAWING_MODE: |
329 |
Viewer.OnViewerLeftUp(self, event) |
|
330 |
elif self.rubberBand.IsShown(): |
|
0 | 331 |
if self.Mode == MODE_SELECTION: |
332 |
elements = self.SearchElements(self.rubberBand.GetCurrentExtent()) |
|
27 | 333 |
self.rubberBand.OnLeftUp(event, self.GetLogicalDC(), self.Scaling) |
0 | 334 |
if len(elements) > 0: |
335 |
self.SelectedElement = Graphic_Group(self) |
|
336 |
self.SelectedElement.SetElements(elements) |
|
337 |
self.SelectedElement.SetSelected(True) |
|
338 |
self.Refresh() |
|
339 |
elif self.Mode == MODE_SELECTION and self.SelectedElement: |
|
27 | 340 |
dc = self.GetLogicalDC() |
0 | 341 |
if self.SelectedElement in self.Elements: |
342 |
if self.SelectedElement in self.Wires: |
|
27 | 343 |
result = self.SelectedElement.TestSegment(event.GetLogicalPosition(dc), True) |
0 | 344 |
if result and result[1] in [EAST, WEST]: |
345 |
self.SelectedElement.SetSelectedSegment(result[0]) |
|
346 |
else: |
|
27 | 347 |
self.SelectedElement.OnLeftUp(event, dc, self.Scaling) |
0 | 348 |
else: |
349 |
for element in self.SelectedElement.GetElements(): |
|
350 |
if element in self.Wires: |
|
27 | 351 |
result = element.TestSegment(event.GetLogicalPosition(dc), True) |
0 | 352 |
if result and result[1] in [EAST, WEST]: |
353 |
element.SetSelectedSegment(result[0]) |
|
354 |
else: |
|
27 | 355 |
element.OnLeftUp(event, dc, self.Scaling) |
0 | 356 |
wxCallAfter(self.SetCursor, wxNullCursor) |
357 |
self.ReleaseMouse() |
|
358 |
self.Refresh() |
|
359 |
event.Skip() |
|
360 |
||
361 |
def OnViewerRightUp(self, event): |
|
27 | 362 |
if self.GetDrawingMode() == FREEDRAWING_MODE: |
363 |
Viewer.OnViewerRightUp(self, event) |
|
364 |
else: |
|
365 |
dc = self.GetLogicalDC() |
|
366 |
pos = event.GetLogicalPosition(dc) |
|
367 |
element = self.FindElement(pos) |
|
368 |
if element: |
|
369 |
if self.SelectedElement and self.SelectedElement != element: |
|
370 |
self.SelectedElement.SetSelected(False) |
|
371 |
self.SelectedElement = element |
|
372 |
if self.SelectedElement in self.Wires: |
|
373 |
self.SelectedElement.SetSelectedSegment(0) |
|
374 |
else: |
|
375 |
self.SelectedElement.SetSelected(True) |
|
376 |
self.SelectedElement.OnRightUp(event, dc, self.Scaling) |
|
377 |
wxCallAfter(self.SetCursor, wxNullCursor) |
|
378 |
self.ReleaseMouse() |
|
379 |
self.Refresh() |
|
380 |
event.Skip() |
|
381 |
||
382 |
def OnViewerLeftDClick(self, event): |
|
383 |
if self.GetDrawingMode() == FREEDRAWING_MODE: |
|
384 |
Viewer.OnViewerLeftDClick(self, event) |
|
385 |
elif self.Mode == MODE_SELECTION and self.SelectedElement: |
|
386 |
self.SelectedElement.OnLeftDClick(event, self.GetLogicalDC(), self.Scaling) |
|
0 | 387 |
self.Refresh() |
388 |
event.Skip() |
|
389 |
||
27 | 390 |
def OnViewerMotion(self, event): |
391 |
if self.GetDrawingMode() == FREEDRAWING_MODE: |
|
392 |
Viewer.OnViewerMotion(self, event) |
|
393 |
event.Skip() |
|
394 |
||
395 |
#------------------------------------------------------------------------------- |
|
396 |
# Keyboard event functions |
|
397 |
#------------------------------------------------------------------------------- |
|
398 |
||
399 |
def OnChar(self, event): |
|
400 |
if self.GetDrawingMode() == FREEDRAWING_MODE: |
|
401 |
Viewer.OnChar(self, event) |
|
402 |
else: |
|
403 |
keycode = event.GetKeyCode() |
|
404 |
if keycode == WXK_DELETE and self.SelectedElement: |
|
405 |
if self.SelectedElement in self.Blocks: |
|
406 |
self.SelectedElement.Delete() |
|
407 |
elif self.SelectedElement in self.Wires: |
|
408 |
self.DeleteWire(self.SelectedElement) |
|
409 |
elif self.SelectedElement not in self.Elements: |
|
410 |
all_wires = True |
|
411 |
for element in self.SelectedElement.GetElements(): |
|
412 |
all_wires &= element in self.Wires |
|
413 |
if all_wires: |
|
414 |
self.DeleteWire(self.SelectedElement) |
|
415 |
else: |
|
416 |
self.SelectedElement.Delete() |
|
0 | 417 |
self.Refresh() |
418 |
event.Skip() |
|
419 |
||
420 |
#------------------------------------------------------------------------------- |
|
421 |
# Adding element functions |
|
422 |
#------------------------------------------------------------------------------- |
|
423 |
||
424 |
def AddRung(self): |
|
425 |
dialog = LDElementDialog(self.Parent, "coil") |
|
426 |
varlist = [] |
|
427 |
vars = self.Controler.GetCurrentElementEditingInterfaceVars() |
|
428 |
if vars: |
|
429 |
for var in vars: |
|
6 | 430 |
if var["Class"] != "Input" and var["Type"] == "BOOL": |
0 | 431 |
varlist.append(var["Name"]) |
432 |
returntype = self.Controler.GetCurrentElementEditingInterfaceReturnType() |
|
433 |
if returntype == "BOOL": |
|
434 |
varlist.append(self.Controler.GetCurrentElementEditingName()) |
|
435 |
dialog.SetVariables(varlist) |
|
436 |
dialog.SetValues({"name":"","type":COIL_NORMAL}) |
|
437 |
if dialog.ShowModal() == wxID_OK: |
|
438 |
values = dialog.GetValues() |
|
439 |
startx, starty = LD_OFFSET[0], 0 |
|
440 |
if len(self.Rungs) > 0: |
|
441 |
bbox = self.Rungs[-1].GetBoundingBox() |
|
442 |
starty = bbox.y + bbox.height |
|
443 |
starty += LD_OFFSET[1] |
|
444 |
rung = Graphic_Group(self) |
|
445 |
# Create comment |
|
446 |
id = self.GetNewId() |
|
447 |
comment = Comment(self, "Commentaire", id) |
|
448 |
comment.SetPosition(startx, starty) |
|
449 |
comment.SetSize(LD_COMMENT_DEFAULTSIZE[0], LD_COMMENT_DEFAULTSIZE[1]) |
|
450 |
self.Elements.append(comment) |
|
451 |
self.Comments.append(comment) |
|
452 |
self.Controler.AddCurrentElementEditingComment(id) |
|
453 |
self.RefreshCommentModel(comment) |
|
454 |
starty += LD_COMMENT_DEFAULTSIZE[1] + LD_OFFSET[1] |
|
455 |
# Create LeftPowerRail |
|
456 |
id = self.GetNewId() |
|
457 |
leftpowerrail = LD_PowerRail(self, LEFTRAIL, id) |
|
458 |
leftpowerrail.SetPosition(startx, starty) |
|
459 |
self.Elements.append(leftpowerrail) |
|
460 |
self.Blocks.append(leftpowerrail) |
|
461 |
rung.SelectElement(leftpowerrail) |
|
462 |
self.Controler.AddCurrentElementEditingPowerRail(id, LEFTRAIL) |
|
463 |
self.RefreshPowerRailModel(leftpowerrail) |
|
464 |
# Create Coil |
|
465 |
id = self.GetNewId() |
|
466 |
coil = LD_Coil(self, values["type"], values["name"], id) |
|
467 |
coil.SetPosition(startx, starty + (LD_LINE_SIZE - LD_ELEMENT_SIZE[1]) / 2) |
|
468 |
self.Elements.append(coil) |
|
469 |
self.Blocks.append(coil) |
|
470 |
rung.SelectElement(coil) |
|
471 |
self.Controler.AddCurrentElementEditingCoil(id) |
|
472 |
# Create Wire between LeftPowerRail and Coil |
|
473 |
wire = Wire(self) |
|
474 |
start_connector = coil.GetConnectors()["input"] |
|
475 |
end_connector = leftpowerrail.GetConnectors()[0] |
|
476 |
start_connector.Connect((wire, 0), False) |
|
477 |
end_connector.Connect((wire, -1), False) |
|
478 |
wire.ConnectStartPoint(None, start_connector) |
|
479 |
wire.ConnectEndPoint(None, end_connector) |
|
480 |
self.Wires.append(wire) |
|
481 |
self.Elements.append(wire) |
|
482 |
rung.SelectElement(wire) |
|
483 |
# Create RightPowerRail |
|
484 |
id = self.GetNewId() |
|
485 |
rightpowerrail = LD_PowerRail(self, RIGHTRAIL, id) |
|
486 |
rightpowerrail.SetPosition(startx, starty) |
|
487 |
self.Elements.append(rightpowerrail) |
|
488 |
self.Blocks.append(rightpowerrail) |
|
489 |
rung.SelectElement(rightpowerrail) |
|
490 |
self.Controler.AddCurrentElementEditingPowerRail(id, RIGHTRAIL) |
|
491 |
# Create Wire between LeftPowerRail and Coil |
|
492 |
wire = Wire(self) |
|
493 |
start_connector = rightpowerrail.GetConnectors()[0] |
|
494 |
end_connector = coil.GetConnectors()["output"] |
|
495 |
start_connector.Connect((wire, 0), False) |
|
496 |
end_connector.Connect((wire, -1), False) |
|
497 |
wire.ConnectStartPoint(None, start_connector) |
|
498 |
wire.ConnectEndPoint(None, end_connector) |
|
499 |
self.Wires.append(wire) |
|
500 |
self.Elements.append(wire) |
|
501 |
rung.SelectElement(wire) |
|
502 |
self.RefreshPosition(coil) |
|
503 |
self.Rungs.append(rung) |
|
504 |
self.Refresh() |
|
505 |
||
506 |
def AddContact(self): |
|
507 |
wires = [] |
|
508 |
if self.SelectedElement in self.Wires: |
|
509 |
left_element = self.SelectedElement.EndConnected |
|
510 |
if not isinstance(left_element.GetParentBlock(), LD_Coil): |
|
511 |
wires.append(self.SelectedElement) |
|
512 |
elif self.SelectedElement and self.SelectedElement not in self.Elements: |
|
513 |
if False not in [element in self.Wires for element in self.SelectedElement.GetElements()]: |
|
514 |
for element in self.SelectedElement.GetElements(): |
|
515 |
wires.append(element) |
|
516 |
if len(wires) > 0: |
|
517 |
dialog = LDElementDialog(self.Parent, "contact") |
|
518 |
varlist = [] |
|
519 |
vars = self.Controler.GetCurrentElementEditingInterfaceVars() |
|
520 |
if vars: |
|
521 |
for var in vars: |
|
6 | 522 |
if var["Class"] != "Output" and var["Type"] == "BOOL": |
0 | 523 |
varlist.append(var["Name"]) |
524 |
dialog.SetVariables(varlist) |
|
525 |
dialog.SetValues({"name":"","type":CONTACT_NORMAL}) |
|
526 |
if dialog.ShowModal() == wxID_OK: |
|
527 |
values = dialog.GetValues() |
|
528 |
points = wires[0].GetSelectedSegmentPoints() |
|
529 |
id = self.GetNewId() |
|
530 |
contact = LD_Contact(self, values["type"], values["name"], id) |
|
531 |
contact.SetPosition(0, points[0].y - (LD_ELEMENT_SIZE[1] + 1) / 2) |
|
532 |
self.Elements.append(contact) |
|
533 |
self.Blocks.append(contact) |
|
534 |
self.Controler.AddCurrentElementEditingContact(id) |
|
535 |
rungindex = self.FindRung(wires[0]) |
|
536 |
rung = self.Rungs[rungindex] |
|
537 |
old_bbox = rung.GetBoundingBox() |
|
538 |
rung.SelectElement(contact) |
|
539 |
connectors = contact.GetConnectors() |
|
540 |
left_elements = [] |
|
541 |
right_elements = [] |
|
542 |
left_index = [] |
|
543 |
right_index = [] |
|
544 |
for wire in wires: |
|
545 |
if wire.EndConnected not in left_elements: |
|
546 |
left_elements.append(wire.EndConnected) |
|
547 |
left_index.append(wire.EndConnected.GetWireIndex(wire)) |
|
548 |
else: |
|
549 |
idx = left_elements.index(wire.EndConnected) |
|
550 |
left_index[idx] = min(left_index[idx], wire.EndConnected.GetWireIndex(wire)) |
|
551 |
if wire.StartConnected not in right_elements: |
|
552 |
right_elements.append(wire.StartConnected) |
|
553 |
right_index.append(wire.StartConnected.GetWireIndex(wire)) |
|
554 |
else: |
|
555 |
idx = right_elements.index(wire.StartConnected) |
|
556 |
right_index[idx] = min(right_index[idx], wire.StartConnected.GetWireIndex(wire)) |
|
557 |
wire.SetSelectedSegment(None) |
|
558 |
wire.Clean() |
|
559 |
rung.SelectElement(wire) |
|
560 |
self.Wires.remove(wire) |
|
561 |
self.Elements.remove(wire) |
|
562 |
wires = [] |
|
563 |
right_wires = [] |
|
564 |
for i, left_element in enumerate(left_elements): |
|
565 |
wire = Wire(self) |
|
566 |
wires.append(wire) |
|
567 |
connectors["input"].Connect((wire, 0), False) |
|
568 |
left_element.InsertConnect(left_index[i], (wire, -1), False) |
|
569 |
wire.ConnectStartPoint(None, connectors["input"]) |
|
570 |
wire.ConnectEndPoint(None, left_element) |
|
571 |
for i, right_element in enumerate(right_elements): |
|
572 |
wire = Wire(self) |
|
573 |
wires.append(wire) |
|
574 |
right_wires.append(wire) |
|
575 |
right_element.InsertConnect(right_index[i], (wire, 0), False) |
|
576 |
connectors["output"].Connect((wire, -1), False) |
|
577 |
wire.ConnectStartPoint(None, right_element) |
|
578 |
wire.ConnectEndPoint(None, connectors["output"]) |
|
579 |
right_wires.reverse() |
|
580 |
for wire in wires: |
|
581 |
self.Wires.append(wire) |
|
582 |
self.Elements.append(wire) |
|
583 |
rung.SelectElement(wire) |
|
584 |
self.RefreshPosition(contact) |
|
585 |
if len(right_wires) > 1: |
|
586 |
group = Graphic_Group(self) |
|
587 |
group.SetSelected(False) |
|
588 |
for wire in right_wires: |
|
589 |
wire.SetSelectedSegment(-1) |
|
590 |
group.SelectElement(wire) |
|
591 |
self.SelectedElement = group |
|
592 |
else: |
|
593 |
right_wires[0].SetSelectedSegment(-1) |
|
594 |
self.SelectedElement = right_wires[0] |
|
595 |
rung.RefreshBoundingBox() |
|
596 |
new_bbox = rung.GetBoundingBox() |
|
597 |
self.RefreshRungs(new_bbox.height - old_bbox.height, rungindex + 1) |
|
598 |
self.Refresh() |
|
7 | 599 |
else: |
600 |
message = wxMessageDialog(self, "You must select the wire where a contact should be added!", "Error", wxOK|wxICON_ERROR) |
|
601 |
message.ShowModal() |
|
602 |
message.Destroy() |
|
0 | 603 |
|
604 |
def AddBranch(self): |
|
605 |
blocks = [] |
|
606 |
if self.SelectedElement in self.Blocks: |
|
607 |
blocks = [self.SelectedElement] |
|
608 |
elif self.SelectedElement not in self.Elements: |
|
609 |
elements = self.SelectedElement.GetElements() |
|
610 |
for element in elements: |
|
611 |
blocks.append(element) |
|
612 |
if len(blocks) > 0: |
|
613 |
blocks_infos = [] |
|
614 |
left_elements = [] |
|
615 |
left_index = [] |
|
616 |
right_elements = [] |
|
617 |
right_index = [] |
|
618 |
for block in blocks: |
|
619 |
connectors = block.GetConnectors() |
|
620 |
block_infos = {"inputs":[],"outputs":[],"lefts":[],"rights":[]} |
|
621 |
if "inputs" in connectors: |
|
622 |
block_infos["inputs"] = connectors["inputs"] |
|
623 |
if "outputs" in connectors: |
|
624 |
block_infos["outputs"] = connectors["outputs"] |
|
625 |
if "input" in connectors: |
|
626 |
block_infos["inputs"] = [connectors["input"]] |
|
627 |
if "output" in connectors: |
|
628 |
block_infos["outputs"] = [connectors["output"]] |
|
629 |
for connector in block_infos["inputs"]: |
|
630 |
for wire, handle in connector.GetWires(): |
|
631 |
found = False |
|
632 |
for infos in blocks_infos: |
|
633 |
if wire.EndConnected in infos["outputs"]: |
|
634 |
for left_element in infos["lefts"]: |
|
635 |
if left_element not in block_infos["lefts"]: |
|
636 |
block_infos["lefts"].append(left_element) |
|
637 |
found = True |
|
638 |
if not found and wire.EndConnected not in block_infos["lefts"]: |
|
639 |
block_infos["lefts"].append(wire.EndConnected) |
|
640 |
if wire.EndConnected not in left_elements: |
|
641 |
left_elements.append(wire.EndConnected) |
|
642 |
left_index.append(wire.EndConnected.GetWireIndex(wire)) |
|
643 |
else: |
|
644 |
index = left_elements.index(wire.EndConnected) |
|
645 |
left_index[index] = max(left_index[index], wire.EndConnected.GetWireIndex(wire)) |
|
646 |
for connector in block_infos["outputs"]: |
|
647 |
for wire, handle in connector.GetWires(): |
|
648 |
found = False |
|
649 |
for infos in blocks_infos: |
|
650 |
if wire.StartConnected in infos["inputs"]: |
|
651 |
for right_element in infos["rights"]: |
|
652 |
if right_element not in block_infos["rights"]: |
|
653 |
block_infos["rights"].append(right_element) |
|
654 |
found = True |
|
655 |
if not found and wire.StartConnected not in block_infos["rights"]: |
|
656 |
block_infos["rights"].append(wire.StartConnected) |
|
657 |
if wire.StartConnected not in right_elements: |
|
658 |
right_elements.append(wire.StartConnected) |
|
659 |
right_index.append(wire.StartConnected.GetWireIndex(wire)) |
|
660 |
else: |
|
661 |
index = right_elements.index(wire.StartConnected) |
|
662 |
right_index[index] = max(right_index[index], wire.StartConnected.GetWireIndex(wire)) |
|
663 |
for connector in block_infos["inputs"]: |
|
664 |
for infos in blocks_infos: |
|
665 |
if connector in infos["rights"]: |
|
666 |
infos["rights"].remove(connector) |
|
667 |
if connector in right_elements: |
|
668 |
index = right_elements.index(connector) |
|
669 |
right_elements.pop(index) |
|
670 |
right_index.pop(index) |
|
671 |
for right_element in block_infos["rights"]: |
|
672 |
if right_element not in infos["rights"]: |
|
673 |
infos["rights"].append(right_element) |
|
674 |
for connector in block_infos["outputs"]: |
|
675 |
for infos in blocks_infos: |
|
676 |
if connector in infos["lefts"]: |
|
677 |
infos["lefts"].remove(connector) |
|
678 |
if connector in left_elements: |
|
679 |
index = left_elements.index(connector) |
|
680 |
left_elements.pop(index) |
|
681 |
left_index.pop(index) |
|
682 |
for left_element in block_infos["lefts"]: |
|
683 |
if left_element not in infos["lefts"]: |
|
684 |
infos["lefts"].append(left_element) |
|
685 |
blocks_infos.append(block_infos) |
|
686 |
for infos in blocks_infos: |
|
687 |
left_elements = [element for element in infos["lefts"]] |
|
688 |
for left_element in left_elements: |
|
689 |
if isinstance(left_element.GetParentBlock(), LD_PowerRail): |
|
690 |
infos["lefts"].remove(left_element) |
|
691 |
if "LD_PowerRail" not in infos["lefts"]: |
|
692 |
infos["lefts"].append("LD_PowerRail") |
|
693 |
right_elements = [element for element in infos["rights"]] |
|
694 |
for right_element in right_elements: |
|
695 |
if isinstance(right_element.GetParentBlock(), LD_PowerRail): |
|
7 | 696 |
infos["rights"].remove(right_element) |
0 | 697 |
if "LD_PowerRail" not in infos["rights"]: |
698 |
infos["rights"].append("LD_PowerRail") |
|
699 |
infos["lefts"].sort() |
|
700 |
infos["rights"].sort() |
|
701 |
lefts = blocks_infos[0]["lefts"] |
|
702 |
rights = blocks_infos[0]["rights"] |
|
703 |
good = True |
|
704 |
for infos in blocks_infos[1:]: |
|
705 |
good &= infos["lefts"] == lefts |
|
706 |
good &= infos["rights"] == rights |
|
707 |
if good: |
|
708 |
rungindex = self.FindRung(blocks[0]) |
|
709 |
rung = self.Rungs[rungindex] |
|
710 |
old_bbox = rung.GetBoundingBox() |
|
711 |
left_powerrail = True |
|
712 |
right_powerrail = True |
|
713 |
for element in left_elements: |
|
714 |
left_powerrail &= isinstance(element.GetParentBlock(), LD_PowerRail) |
|
715 |
for element in right_elements: |
|
716 |
right_powerrail &= isinstance(element.GetParentBlock(), LD_PowerRail) |
|
717 |
if not left_powerrail or not right_powerrail: |
|
27 | 718 |
wires = [] |
0 | 719 |
if left_powerrail: |
720 |
powerrail = left_elements[0].GetParentBlock() |
|
721 |
index = 0 |
|
722 |
for left_element in left_elements: |
|
723 |
index = max(index, powerrail.GetConnectorIndex(left_element)) |
|
724 |
if powerrail.IsNullConnector(index + 1): |
|
725 |
powerrail.DeleteConnector(index + 1) |
|
726 |
powerrail.InsertConnector(index + 1) |
|
727 |
powerrail.RefreshModel() |
|
728 |
connectors = powerrail.GetConnectors() |
|
27 | 729 |
right_elements.reverse() |
0 | 730 |
for i, right_element in enumerate(right_elements): |
731 |
new_wire = Wire(self) |
|
27 | 732 |
wires.append(new_wire) |
0 | 733 |
right_element.InsertConnect(right_index[i] + 1, (new_wire, 0), False) |
734 |
connectors[index + 1].Connect((new_wire, -1), False) |
|
735 |
new_wire.ConnectStartPoint(None, right_element) |
|
736 |
new_wire.ConnectEndPoint(None, connectors[index + 1]) |
|
737 |
right_elements.reverse() |
|
738 |
elif right_powerrail: |
|
7 | 739 |
dialog = LDElementDialog(self.Parent, "coil") |
740 |
varlist = [] |
|
741 |
vars = self.Controler.GetCurrentElementEditingInterfaceVars() |
|
742 |
if vars: |
|
743 |
for var in vars: |
|
744 |
if var["Class"] != "Input" and var["Type"] == "BOOL": |
|
745 |
varlist.append(var["Name"]) |
|
746 |
returntype = self.Controler.GetCurrentElementEditingInterfaceReturnType() |
|
747 |
if returntype == "BOOL": |
|
748 |
varlist.append(self.Controler.GetCurrentElementEditingName()) |
|
749 |
dialog.SetVariables(varlist) |
|
750 |
dialog.SetValues({"name":"","type":COIL_NORMAL}) |
|
751 |
if dialog.ShowModal() == wxID_OK: |
|
752 |
values = dialog.GetValues() |
|
753 |
powerrail = right_elements[0].GetParentBlock() |
|
754 |
index = 0 |
|
755 |
for right_element in right_elements: |
|
756 |
index = max(index, powerrail.GetConnectorIndex(right_element)) |
|
757 |
if powerrail.IsNullConnector(index + 1): |
|
758 |
powerrail.DeleteConnector(index + 1) |
|
759 |
powerrail.InsertConnector(index + 1) |
|
760 |
powerrail.RefreshModel() |
|
761 |
connectors = powerrail.GetConnectors() |
|
762 |
# Create Coil |
|
763 |
id = self.GetNewId() |
|
764 |
coil = LD_Coil(self, values["type"], values["name"], id) |
|
765 |
pos = blocks[0].GetPosition() |
|
766 |
coil.SetPosition(pos[0], pos[1] + LD_LINE_SIZE) |
|
767 |
self.Elements.append(coil) |
|
768 |
self.Blocks.append(coil) |
|
769 |
rung.SelectElement(coil) |
|
770 |
self.Controler.AddCurrentElementEditingCoil(id) |
|
771 |
coil_connectors = coil.GetConnectors() |
|
772 |
# Create Wire between LeftPowerRail and Coil |
|
773 |
wire = Wire(self) |
|
774 |
connectors[index + 1].Connect((wire, 0), False) |
|
775 |
coil_connectors["output"].Connect((wire, -1), False) |
|
776 |
wire.ConnectStartPoint(None, connectors[index + 1]) |
|
777 |
wire.ConnectEndPoint(None, coil_connectors["output"]) |
|
778 |
self.Wires.append(wire) |
|
779 |
self.Elements.append(wire) |
|
27 | 780 |
rung.SelectElement(wire) |
781 |
left_elements.reverse() |
|
7 | 782 |
for i, left_element in enumerate(left_elements): |
783 |
# Create Wire between LeftPowerRail and Coil |
|
784 |
new_wire = Wire(self) |
|
27 | 785 |
wires.append(new_wire) |
7 | 786 |
coil_connectors["input"].Connect((new_wire, 0), False) |
787 |
left_element.InsertConnect(left_index[i] + 1, (new_wire, -1), False) |
|
788 |
new_wire.ConnectStartPoint(None, coil_connectors["input"]) |
|
789 |
new_wire.ConnectEndPoint(None, left_element) |
|
790 |
self.RefreshPosition(coil) |
|
0 | 791 |
else: |
792 |
left_elements.reverse() |
|
793 |
right_elements.reverse() |
|
794 |
for i, left_element in enumerate(left_elements): |
|
795 |
for j, right_element in enumerate(right_elements): |
|
796 |
exist = False |
|
797 |
for wire, handle in right_element.GetWires(): |
|
798 |
exist |= wire.EndConnected == left_element |
|
799 |
if not exist: |
|
800 |
new_wire = Wire(self) |
|
801 |
wires.append(new_wire) |
|
802 |
right_element.InsertConnect(right_index[j] + 1, (new_wire, 0), False) |
|
803 |
left_element.InsertConnect(left_index[i] + 1, (new_wire, -1), False) |
|
804 |
new_wire.ConnectStartPoint(None, right_element) |
|
805 |
new_wire.ConnectEndPoint(None, left_element) |
|
27 | 806 |
wires.reverse() |
807 |
for wire in wires: |
|
808 |
self.Wires.append(wire) |
|
809 |
self.Elements.append(wire) |
|
810 |
rung.SelectElement(wire) |
|
811 |
right_elements.reverse() |
|
0 | 812 |
for block in blocks: |
813 |
self.RefreshPosition(block) |
|
814 |
for right_element in right_elements: |
|
815 |
self.RefreshPosition(right_element.GetParentBlock()) |
|
816 |
self.SelectedElement.RefreshBoundingBox() |
|
817 |
rung.RefreshBoundingBox() |
|
818 |
new_bbox = rung.GetBoundingBox() |
|
819 |
self.RefreshRungs(new_bbox.height - old_bbox.height, rungindex + 1) |
|
820 |
self.Refresh() |
|
7 | 821 |
else: |
822 |
message = wxMessageDialog(self, "The group of block must be coherent!", "Error", wxOK|wxICON_ERROR) |
|
823 |
message.ShowModal() |
|
824 |
message.Destroy() |
|
825 |
else: |
|
826 |
message = wxMessageDialog(self, "You must select the block or group of blocks around which a branch should be added!", "Error", wxOK|wxICON_ERROR) |
|
827 |
message.ShowModal() |
|
828 |
message.Destroy() |
|
829 |
||
830 |
def AddBlock(self): |
|
831 |
message = wxMessageDialog(self, "This option isn't available yet!", "Warning", wxOK|wxICON_EXCLAMATION) |
|
832 |
message.ShowModal() |
|
833 |
message.Destroy() |
|
0 | 834 |
|
835 |
#------------------------------------------------------------------------------- |
|
836 |
# Delete element functions |
|
837 |
#------------------------------------------------------------------------------- |
|
838 |
||
839 |
def DeleteContact(self, contact): |
|
27 | 840 |
if self.GetDrawingMode() == FREEDRAWING_MODE: |
841 |
Viewer.DeleteContact(self, contact) |
|
842 |
else: |
|
843 |
rungindex = self.FindRung(contact) |
|
844 |
rung = self.Rungs[rungindex] |
|
845 |
old_bbox = rung.GetBoundingBox() |
|
846 |
connectors = contact.GetConnectors() |
|
847 |
input_wires = [wire for wire, handle in connectors["input"].GetWires()] |
|
848 |
output_wires = [wire for wire, handle in connectors["output"].GetWires()] |
|
849 |
left_elements = [(wire.EndConnected, wire.EndConnected.GetWireIndex(wire)) for wire in input_wires] |
|
850 |
right_elements = [(wire.StartConnected, wire.StartConnected.GetWireIndex(wire)) for wire in output_wires] |
|
851 |
for wire in input_wires: |
|
852 |
wire.Clean() |
|
853 |
rung.SelectElement(wire) |
|
854 |
self.Wires.remove(wire) |
|
855 |
self.Elements.remove(wire) |
|
856 |
for wire in output_wires: |
|
857 |
wire.Clean() |
|
858 |
rung.SelectElement(wire) |
|
859 |
self.Wires.remove(wire) |
|
860 |
self.Elements.remove(wire) |
|
861 |
rung.SelectElement(contact) |
|
862 |
contact.Clean() |
|
863 |
left_elements.reverse() |
|
864 |
right_elements.reverse() |
|
865 |
powerrail = len(left_elements) == 1 and isinstance(left_elements[0][0].GetParentBlock(), LD_PowerRail) |
|
866 |
for left_element, left_index in left_elements: |
|
867 |
for right_element, right_index in right_elements: |
|
868 |
wire_removed = [] |
|
869 |
for wire, handle in right_element.GetWires(): |
|
870 |
if wire.EndConnected == left_element: |
|
871 |
wire_removed.append(wire) |
|
872 |
elif isinstance(wire.EndConnected.GetParentBlock(), LD_PowerRail) and powerrail: |
|
873 |
left_powerrail = wire.EndConnected.GetParentBlock() |
|
874 |
index = left_powerrail.GetConnectorIndex(wire.EndConnected) |
|
875 |
left_powerrail.DeleteConnector(index) |
|
876 |
wire_removed.append(wire) |
|
877 |
for wire in wire_removed: |
|
878 |
wire.Clean() |
|
879 |
self.Wires.remove(wire) |
|
880 |
self.Elements.remove(wire) |
|
881 |
rung.SelectElement(wire) |
|
882 |
wires = [] |
|
883 |
for left_element, left_index in left_elements: |
|
884 |
for right_element, right_index in right_elements: |
|
885 |
wire = Wire(self) |
|
886 |
wires.append(wire) |
|
887 |
right_element.InsertConnect(right_index, (wire, 0), False) |
|
888 |
left_element.InsertConnect(left_index, (wire, -1), False) |
|
889 |
wire.ConnectStartPoint(None, right_element) |
|
890 |
wire.ConnectEndPoint(None, left_element) |
|
891 |
wires.reverse() |
|
892 |
for wire in wires: |
|
893 |
self.Wires.append(wire) |
|
894 |
self.Elements.append(wire) |
|
895 |
rung.SelectElement(wire) |
|
896 |
right_elements.reverse() |
|
0 | 897 |
for right_element, right_index in right_elements: |
27 | 898 |
self.RefreshPosition(right_element.GetParentBlock()) |
899 |
self.Blocks.remove(contact) |
|
900 |
self.Elements.remove(contact) |
|
901 |
self.Controler.RemoveCurrentElementEditingInstance(contact.GetId()) |
|
902 |
rung.RefreshBoundingBox() |
|
903 |
new_bbox = rung.GetBoundingBox() |
|
904 |
self.RefreshRungs(new_bbox.height - old_bbox.height, rungindex + 1) |
|
905 |
self.SelectedElement = None |
|
0 | 906 |
|
8 | 907 |
def RecursiveDeletion(self, element, rung): |
908 |
connectors = element.GetConnectors() |
|
909 |
input_wires = [wire for wire, handle in connectors["input"].GetWires()] |
|
910 |
left_elements = [wire.EndConnected for wire in input_wires] |
|
911 |
rung.SelectElement(element) |
|
912 |
element.Clean() |
|
913 |
for wire in input_wires: |
|
914 |
wire.Clean() |
|
915 |
self.Wires.remove(wire) |
|
916 |
self.Elements.remove(wire) |
|
917 |
rung.SelectElement(wire) |
|
918 |
self.Blocks.remove(element) |
|
919 |
self.Elements.remove(element) |
|
920 |
self.Controler.RemoveCurrentElementEditingInstance(element.GetId()) |
|
921 |
for left_element in left_elements: |
|
922 |
block = left_element.GetParentBlock() |
|
923 |
if len(left_element.GetWires()) == 0: |
|
924 |
self.RecursiveDeletion(block, rung) |
|
925 |
else: |
|
926 |
self.RefreshPosition(block) |
|
927 |
||
0 | 928 |
def DeleteCoil(self, coil): |
27 | 929 |
if self.GetDrawingMode() == FREEDRAWING_MODE: |
930 |
Viewer.DeleteContact(self, coil) |
|
8 | 931 |
else: |
27 | 932 |
rungindex = self.FindRung(coil) |
0 | 933 |
rung = self.Rungs[rungindex] |
934 |
old_bbox = rung.GetBoundingBox() |
|
27 | 935 |
nbcoils = 0 |
936 |
for element in rung.GetElements(): |
|
937 |
if isinstance(element, LD_Coil): |
|
938 |
nbcoils += 1 |
|
939 |
if nbcoils > 1: |
|
940 |
connectors = coil.GetConnectors() |
|
941 |
output_wires = [wire for wire, handle in connectors["output"].GetWires()] |
|
942 |
right_elements = [wire.StartConnected for wire in output_wires] |
|
943 |
for wire in output_wires: |
|
0 | 944 |
wire.Clean() |
945 |
self.Wires.remove(wire) |
|
946 |
self.Elements.remove(wire) |
|
947 |
rung.SelectElement(wire) |
|
27 | 948 |
for right_element in right_elements: |
949 |
right_block = right_element.GetParentBlock() |
|
950 |
if isinstance(right_block, LD_PowerRail): |
|
951 |
if len(right_element.GetWires()) == 0: |
|
952 |
index = right_block.GetConnectorIndex(right_element) |
|
953 |
right_block.DeleteConnector(index) |
|
954 |
powerrail_connectors = right_block.GetConnectors() |
|
955 |
for connector in powerrail_connectors: |
|
956 |
for wire, handle in connector.GetWires(): |
|
957 |
block = wire.EndConnected.GetParentBlock() |
|
958 |
endpoint = wire.EndConnected.GetPosition(False) |
|
959 |
startpoint = connector.GetPosition(False) |
|
960 |
block.Move(0, startpoint.y - endpoint.y) |
|
961 |
self.RefreshPosition(block) |
|
962 |
self.RecursiveDeletion(coil, rung) |
|
963 |
else: |
|
964 |
for element in rung.GetElements(): |
|
965 |
if element in self.Wires: |
|
966 |
element.Clean() |
|
967 |
self.Wires.remove(element) |
|
968 |
self.Elements.remove(element) |
|
969 |
for element in rung.GetElements(): |
|
970 |
if element in self.Blocks: |
|
971 |
self.Controler.RemoveCurrentElementEditingInstance(element.GetId()) |
|
972 |
self.Blocks.remove(element) |
|
973 |
self.Elements.remove(element) |
|
974 |
self.Controler.RemoveCurrentElementEditingInstance(self.Comments[rungindex].GetId()) |
|
975 |
self.Elements.remove(self.Comments[rungindex]) |
|
976 |
self.Comments.pop(rungindex) |
|
977 |
self.Rungs.pop(rungindex) |
|
978 |
if rungindex < len(self.Rungs): |
|
979 |
next_bbox = self.Rungs[rungindex].GetBoundingBox() |
|
980 |
self.RefreshRungs(old_bbox.y - next_bbox.y, rungindex) |
|
0 | 981 |
self.SelectedElement = None |
982 |
||
27 | 983 |
def DeleteWire(self, wire): |
984 |
if self.GetDrawingMode() == FREEDRAWING_MODE: |
|
985 |
Viewer.DeleteWire(self, wire) |
|
986 |
else: |
|
987 |
wires = [] |
|
988 |
left_elements = [] |
|
989 |
right_elements = [] |
|
990 |
if wire in self.Wires: |
|
991 |
wires = [wire] |
|
992 |
elif wire not in self.Elements: |
|
993 |
for element in wire.GetElements(): |
|
994 |
if element in self.Wires: |
|
995 |
wires.append(element) |
|
996 |
else: |
|
997 |
wires = [] |
|
998 |
break |
|
999 |
if len(wires) > 0: |
|
1000 |
rungindex = self.FindRung(wires[0]) |
|
1001 |
rung = self.Rungs[rungindex] |
|
1002 |
old_bbox = rung.GetBoundingBox() |
|
1003 |
for wire in wires: |
|
1004 |
connections = wire.GetSelectedSegmentConnections() |
|
1005 |
left_block = wire.EndConnected.GetParentBlock() |
|
1006 |
if wire.EndConnected not in left_elements: |
|
1007 |
left_elements.append(wire.EndConnected) |
|
1008 |
if wire.StartConnected not in right_elements: |
|
1009 |
right_elements.append(wire.StartConnected) |
|
1010 |
if connections == (False, False) or connections == (False, True) and isinstance(left_block, LD_PowerRail): |
|
1011 |
wire.Clean() |
|
1012 |
self.Wires.remove(wire) |
|
1013 |
self.Elements.remove(wire) |
|
1014 |
rung.SelectElement(wire) |
|
1015 |
for left_element in left_elements: |
|
1016 |
left_block = left_element.GetParentBlock() |
|
1017 |
if isinstance(left_block, LD_PowerRail): |
|
1018 |
if len(left_element.GetWires()) == 0: |
|
1019 |
index = left_block.GetConnectorIndex(left_element) |
|
1020 |
left_block.DeleteConnector(index) |
|
1021 |
else: |
|
1022 |
connectors = left_block.GetConnectors() |
|
1023 |
output_connectors = [] |
|
1024 |
if "outputs" in connectors: |
|
1025 |
output_connectors = connectors["outputs"] |
|
1026 |
if "output" in connectors: |
|
1027 |
output_connectors = [connectors["output"]] |
|
1028 |
for connector in output_connectors: |
|
1029 |
for wire, handle in connector.GetWires(): |
|
1030 |
self.RefreshPosition(wire.StartConnected.GetParentBlock()) |
|
1031 |
for right_element in right_elements: |
|
1032 |
self.RefreshPosition(right_element.GetParentBlock()) |
|
1033 |
rung.RefreshBoundingBox() |
|
1034 |
new_bbox = rung.GetBoundingBox() |
|
1035 |
self.RefreshRungs(new_bbox.height - old_bbox.height, rungindex + 1) |
|
1036 |
self.SelectedElement = None |
|
1037 |
||
0 | 1038 |
#------------------------------------------------------------------------------- |
1039 |
# Refresh element position functions |
|
1040 |
#------------------------------------------------------------------------------- |
|
1041 |
||
8 | 1042 |
def RefreshPosition(self, element, recursive=True): |
0 | 1043 |
if isinstance(element, LD_PowerRail) and element.GetType() == LEFTRAIL: |
1044 |
element.RefreshModel() |
|
1045 |
return |
|
1046 |
connectors = element.GetConnectors() |
|
1047 |
input_connectors = [] |
|
1048 |
output_connectors = [] |
|
1049 |
if isinstance(element, LD_PowerRail) and element.GetType() == RIGHTRAIL: |
|
1050 |
input_connectors = connectors |
|
8 | 1051 |
for i, connector in enumerate(input_connectors): |
1052 |
for j, (wire, handle) in enumerate(connector.GetWires()): |
|
1053 |
block = wire.EndConnected.GetParentBlock() |
|
1054 |
self.RefreshPosition(block, False) |
|
0 | 1055 |
else: |
1056 |
if "inputs" in connectors: |
|
1057 |
input_connectors = connectors["inputs"] |
|
1058 |
if "outputs" in connectors: |
|
1059 |
output_connectors = connectors["outputs"] |
|
1060 |
if "input" in connectors: |
|
1061 |
input_connectors = [connectors["input"]] |
|
1062 |
if "output" in connectors: |
|
1063 |
output_connectors = [connectors["output"]] |
|
1064 |
position = element.GetPosition() |
|
1065 |
minx = 0 |
|
1066 |
onlyone = [] |
|
1067 |
for connector in input_connectors: |
|
1068 |
onlyone.append(len(connector.GetWires()) == 1) |
|
1069 |
for wire, handle in connector.GetWires(): |
|
1070 |
onlyone[-1] &= len(wire.EndConnected.GetWires()) == 1 |
|
1071 |
leftblock = wire.EndConnected.GetParentBlock() |
|
1072 |
pos = leftblock.GetPosition() |
|
1073 |
size = leftblock.GetSize() |
|
1074 |
minx = max(minx, pos[0] + size[0]) |
|
1075 |
if isinstance(element, LD_Coil): |
|
1076 |
interval = LD_WIRECOIL_SIZE |
|
1077 |
else: |
|
1078 |
interval = LD_WIRE_SIZE |
|
1079 |
if False in onlyone: |
|
1080 |
interval += LD_WIRE_SIZE |
|
1081 |
movex = minx + interval - position[0] |
|
1082 |
element.Move(movex, 0) |
|
8 | 1083 |
position = element.GetPosition() |
27 | 1084 |
blocks = [] |
1085 |
for i, connector in enumerate(input_connectors): |
|
1086 |
for j, (wire, handle) in enumerate(connector.GetWires()): |
|
1087 |
blocks.append(wire.EndConnected.GetParentBlock()) |
|
0 | 1088 |
for i, connector in enumerate(input_connectors): |
1089 |
startpoint = connector.GetPosition(False) |
|
1090 |
previous_blocks = [] |
|
1091 |
block_list = [] |
|
1092 |
start_offset = 0 |
|
1093 |
if not onlyone[i]: |
|
1094 |
middlepoint = minx + LD_WIRE_SIZE |
|
1095 |
for j, (wire, handle) in enumerate(connector.GetWires()): |
|
1096 |
block = wire.EndConnected.GetParentBlock() |
|
8 | 1097 |
if isinstance(element, LD_PowerRail): |
1098 |
pos = block.GetPosition() |
|
1099 |
size = leftblock.GetSize() |
|
1100 |
movex = position[0] - LD_WIRE_SIZE - size[0] - pos[0] |
|
1101 |
block.Move(movex, 0) |
|
0 | 1102 |
endpoint = wire.EndConnected.GetPosition(False) |
1103 |
if j == 0: |
|
1104 |
if not onlyone[i] and wire.EndConnected.GetWireIndex(wire) > 0: |
|
1105 |
start_offset = endpoint.y - startpoint.y |
|
1106 |
offset = start_offset |
|
1107 |
else: |
|
27 | 1108 |
offset = start_offset + LD_LINE_SIZE * CalcBranchSize(previous_blocks, blocks) |
0 | 1109 |
if block in block_list: |
1110 |
wires = wire.EndConnected.GetWires() |
|
1111 |
endmiddlepoint = wires[0][0].StartConnected.GetPosition(False)[0] - LD_WIRE_SIZE |
|
1112 |
points = [startpoint, wxPoint(middlepoint, startpoint.y), |
|
1113 |
wxPoint(middlepoint, startpoint.y + offset), |
|
1114 |
wxPoint(endmiddlepoint, startpoint.y + offset), |
|
1115 |
wxPoint(endmiddlepoint, endpoint.y), endpoint] |
|
1116 |
else: |
|
1117 |
if startpoint.y + offset != endpoint.y: |
|
8 | 1118 |
if isinstance(element, LD_PowerRail): |
1119 |
diff = (startpoint.y - endpoint.y) / LD_LINE_SIZE |
|
1120 |
for k in xrange(abs(diff)): |
|
1121 |
if diff < 0: |
|
1122 |
element.DeleteConnector(i - 1 - k) |
|
1123 |
else: |
|
1124 |
element.InsertConnector(i + k, False) |
|
1125 |
elif isinstance(block, LD_PowerRail): |
|
0 | 1126 |
index = block.GetConnectorIndex(wire.EndConnected) |
1127 |
if index: |
|
1128 |
diff = (startpoint.y - endpoint.y) / LD_LINE_SIZE |
|
1129 |
for k in xrange(abs(diff)): |
|
1130 |
if diff < 0: |
|
1131 |
block.DeleteConnector(index - 1 - k) |
|
1132 |
else: |
|
1133 |
block.InsertConnector(index + k, False) |
|
1134 |
else: |
|
1135 |
block.Move(0, startpoint.y + offset - endpoint.y) |
|
8 | 1136 |
self.RefreshPosition(block, False) |
0 | 1137 |
endpoint = wire.EndConnected.GetPosition(False) |
1138 |
if not onlyone[i]: |
|
1139 |
points = [startpoint, wxPoint(middlepoint, startpoint.y), |
|
1140 |
wxPoint(middlepoint, endpoint.y), endpoint] |
|
1141 |
else: |
|
1142 |
points = [startpoint, endpoint] |
|
1143 |
wire.SetPoints(points) |
|
1144 |
previous_blocks.append(block) |
|
27 | 1145 |
blocks.remove(block) |
0 | 1146 |
ExtractNextBlocks(block, block_list) |
1147 |
element.RefreshModel(False) |
|
8 | 1148 |
if recursive: |
1149 |
for connector in output_connectors: |
|
1150 |
for wire, handle in connector.GetWires(): |
|
1151 |
self.RefreshPosition(wire.StartConnected.GetParentBlock()) |
|
0 | 1152 |
|
1153 |
def RefreshRungs(self, movey, fromidx): |
|
1154 |
if movey != 0: |
|
1155 |
for i in xrange(fromidx, len(self.Rungs)): |
|
1156 |
self.Comments[i].Move(0, movey) |
|
1157 |
self.Comments[i].RefreshModel() |
|
1158 |
self.Rungs[i].Move(0, movey) |
|
1159 |
for element in self.Rungs[i].GetElements(): |
|
1160 |
if element in self.Blocks: |
|
1161 |
self.RefreshPosition(element) |
|
1162 |
||
1163 |
#------------------------------------------------------------------------------- |
|
1164 |
# Edit element content functions |
|
1165 |
#------------------------------------------------------------------------------- |
|
1166 |
||
1167 |
def EditContactContent(self, contact): |
|
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
1168 |
if self.GetDrawingMode() == FREEDRAWING_MODE: |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
1169 |
Viewer.EditContactContent(self, contact) |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
1170 |
else: |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
1171 |
dialog = LDElementDialog(self.Parent, "contact") |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
1172 |
varlist = [] |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
1173 |
vars = self.Controler.GetCurrentElementEditingInterfaceVars() |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
1174 |
if vars: |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
1175 |
for var in vars: |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
1176 |
if var["Class"] != "Output" and var["Type"] == "BOOL": |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
1177 |
varlist.append(var["Name"]) |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
1178 |
dialog.SetVariables(varlist) |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
1179 |
dialog.SetValues({"name":contact.GetName(),"type":contact.GetType()}) |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
1180 |
if dialog.ShowModal() == wxID_OK: |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
1181 |
values = dialog.GetValues() |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
1182 |
contact.SetName(values["name"]) |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
1183 |
contact.SetType(values["type"]) |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
1184 |
contact.RefreshModel(False) |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
1185 |
self.Refresh() |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
1186 |
dialog.Destroy() |
0 | 1187 |
|
1188 |
def EditCoilContent(self, coil): |
|
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
1189 |
if self.GetDrawingMode() == FREEDRAWING_MODE: |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
1190 |
Viewer.EditCoilContent(self, coil) |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
1191 |
else: |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
1192 |
dialog = LDElementDialog(self.Parent, "coil") |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
1193 |
varlist = [] |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
1194 |
vars = self.Controler.GetCurrentElementEditingInterfaceVars() |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
1195 |
if vars: |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
1196 |
for var in vars: |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
1197 |
if var["Class"] != "Input" and var["Type"] == "BOOL": |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
1198 |
varlist.append(var["Name"]) |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
1199 |
returntype = self.Controler.GetCurrentElementEditingInterfaceReturnType() |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
1200 |
if returntype == "BOOL": |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
1201 |
varlist.append(self.Controler.GetCurrentElementEditingName()) |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
1202 |
dialog.SetVariables(varlist) |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
1203 |
dialog.SetValues({"name":coil.GetName(),"type":coil.GetType()}) |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
1204 |
if dialog.ShowModal() == wxID_OK: |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
1205 |
values = dialog.GetValues() |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
1206 |
coil.SetName(values["name"]) |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
1207 |
coil.SetType(values["type"]) |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
1208 |
coil.RefreshModel(False) |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
1209 |
self.Refresh() |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
1210 |
dialog.Destroy() |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
1211 |
|
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
1212 |
def EditPowerRailContent(self, powerrail): |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
1213 |
if self.GetDrawingMode() == FREEDRAWING_MODE: |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
1214 |
Viewer.EditPowerRailContent(self, powerrail) |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
1215 |