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