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