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