author | greg |
Mon, 31 Dec 2007 13:36:11 +0100 | |
changeset 139 | c2d093402005 |
parent 138 | 9c74d00ce93e |
child 140 | 06d28f03f6f4 |
permissions | -rw-r--r-- |
0 | 1 |
#!/usr/bin/env python |
2 |
# -*- coding: utf-8 -*- |
|
3 |
||
4 |
#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor |
|
5 |
#based on the plcopen standard. |
|
6 |
# |
|
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 |
||
27 |
from GraphicCommons import * |
|
28 |
from plcopen.structures import * |
|
29 |
||
30 |
#------------------------------------------------------------------------------- |
|
31 |
# Ladder Diagram PowerRail |
|
32 |
#------------------------------------------------------------------------------- |
|
33 |
||
34 |
""" |
|
35 |
Class that implements the graphic representation of a power rail |
|
36 |
""" |
|
37 |
||
38 |
class LD_PowerRail(Graphic_Element): |
|
39 |
||
40 |
# Create a new power rail |
|
41 |
def __init__(self, parent, type, id = None, connectors = [True]): |
|
42 |
Graphic_Element.__init__(self, parent) |
|
61
dc7142ae9438
Adding possibility to change Type and Pin number of power rails
lbessard
parents:
58
diff
changeset
|
43 |
self.Type = None |
dc7142ae9438
Adding possibility to change Type and Pin number of power rails
lbessard
parents:
58
diff
changeset
|
44 |
self.Connectors = [] |
71 | 45 |
self.RealConnectors = None |
0 | 46 |
self.Id = id |
27 | 47 |
self.Extensions = [LD_LINE_SIZE / 2, LD_LINE_SIZE / 2] |
48 |
if len(connectors) < 1: |
|
49 |
connectors = [True] |
|
61
dc7142ae9438
Adding possibility to change Type and Pin number of power rails
lbessard
parents:
58
diff
changeset
|
50 |
self.SetType(type, connectors) |
0 | 51 |
|
52 |
# Destructor |
|
53 |
def __del__(self): |
|
54 |
self.Connectors = [] |
|
55 |
||
112 | 56 |
# Make a clone of this LD_PowerRail |
57 |
def Clone(self, id = None, pos = None): |
|
58 |
powerrail = LD_PowerRail(self.Parent, self.Type, id) |
|
59 |
powerrail.SetSize(self.Size[0], self.Size[1]) |
|
60 |
if pos is not None: |
|
61 |
powerrail.SetPosition(pos.x, pos.y) |
|
62 |
powerrail.Connectors = [] |
|
63 |
for connector in self.Connectors: |
|
64 |
if connector is not None: |
|
65 |
powerrail.Connectors.append(connector.Clone(powerrail)) |
|
66 |
else: |
|
67 |
powerrail.Connectors.append(None) |
|
68 |
return powerrail |
|
69 |
||
0 | 70 |
# Forbids to change the power rail size |
71 |
def SetSize(self, width, height): |
|
42 | 72 |
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: |
27 | 73 |
Graphic_Element.SetSize(self, width, height) |
74 |
self.RefreshConnectors() |
|
0 | 75 |
|
76 |
# Forbids to select a power rail |
|
77 |
def HitTest(self, pt): |
|
42 | 78 |
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: |
27 | 79 |
return Graphic_Element.HitTest(self, pt) or self.TestConnector(pt, False) != None |
0 | 80 |
return False |
81 |
||
42 | 82 |
# Forbids to select a power rail |
83 |
def IsInSelection(self, rect): |
|
84 |
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: |
|
61
dc7142ae9438
Adding possibility to change Type and Pin number of power rails
lbessard
parents:
58
diff
changeset
|
85 |
return Graphic_Element.IsInSelection(self, rect) |
42 | 86 |
return False |
87 |
||
0 | 88 |
# Deletes this power rail by calling the appropriate method |
89 |
def Delete(self): |
|
90 |
self.Parent.DeletePowerRail(self) |
|
91 |
||
92 |
# Unconnect all connectors |
|
93 |
def Clean(self): |
|
94 |
for connector in self.Connectors: |
|
95 |
if connector: |
|
61
dc7142ae9438
Adding possibility to change Type and Pin number of power rails
lbessard
parents:
58
diff
changeset
|
96 |
connector.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE) |
0 | 97 |
|
98 |
# Refresh the power rail bounding box |
|
99 |
def RefreshBoundingBox(self): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
100 |
dc = wx.ClientDC(self.Parent) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
101 |
self.BoundingBox = wx.Rect(self.Pos.x, self.Pos.y, self.Size[0], self.Size[1] + 1) |
0 | 102 |
|
103 |
# Refresh the power rail size |
|
104 |
def RefreshSize(self): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
105 |
self.Size = wx.Size(2, LD_LINE_SIZE * len(self.Connectors)) |
0 | 106 |
self.RefreshBoundingBox() |
107 |
||
27 | 108 |
# Returns the block minimum size |
109 |
def GetMinSize(self): |
|
96 | 110 |
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: |
111 |
return 2, self.Extensions[0] + self.Extensions[1] |
|
112 |
else: |
|
113 |
return 2, LD_LINE_SIZE * len(self.Connectors) |
|
27 | 114 |
|
0 | 115 |
# Add a connector or a blank to this power rail at the last place |
116 |
def AddConnector(self, connector = True): |
|
117 |
self.InsertConnector(len(self.Connectors), connector) |
|
118 |
||
119 |
# Add a connector or a blank to this power rail at the place given |
|
120 |
def InsertConnector(self, idx, connector = True): |
|
121 |
if connector: |
|
122 |
if self.Type == LEFTRAIL: |
|
80 | 123 |
connector = Connector(self, "", "BOOL", wx.Point(self.Size[0], 0), EAST) |
0 | 124 |
elif self.Type == RIGHTRAIL: |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
125 |
connector = Connector(self, "", "BOOL", wx.Point(0, 0), WEST) |
0 | 126 |
self.Connectors.insert(idx, connector) |
127 |
else: |
|
128 |
self.Connectors.insert(idx, None) |
|
129 |
self.RefreshSize() |
|
130 |
self.RefreshConnectors() |
|
131 |
||
27 | 132 |
# Moves the divergence connector given |
133 |
def MoveConnector(self, connector, movey): |
|
134 |
position = connector.GetRelPosition() |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
135 |
connector.SetPosition(wx.Point(position.x, position.y + movey)) |
27 | 136 |
miny = self.Size[1] |
137 |
maxy = 0 |
|
138 |
for connect in self.Connectors: |
|
139 |
connect_pos = connect.GetRelPosition() |
|
80 | 140 |
miny = min(miny, connect_pos.y - self.Extensions[0]) |
141 |
maxy = max(maxy, connect_pos.y - self.Extensions[0]) |
|
142 |
min_pos = self.Pos.y + miny |
|
27 | 143 |
self.Pos.y = min(min_pos, self.Pos.y) |
144 |
if min_pos == self.Pos.y: |
|
145 |
for connect in self.Connectors: |
|
146 |
connect_pos = connect.GetRelPosition() |
|
80 | 147 |
connect.SetPosition(wx.Point(connect_pos.x, connect_pos.y - miny)) |
110
29b6b70e1721
Bug that makes element resizing acting strongly fixed
lbessard
parents:
96
diff
changeset
|
148 |
self.Connectors.sort(lambda x, y: x.Pos.y.__cmp__(y.Pos.y)) |
80 | 149 |
maxy = 0 |
150 |
for connect in self.Connectors: |
|
151 |
connect_pos = connect.GetRelPosition() |
|
152 |
maxy = max(maxy, connect_pos.y) |
|
153 |
self.Size[1] = max(maxy + self.Extensions[1], self.Size[1]) |
|
27 | 154 |
connector.MoveConnected() |
155 |
self.RefreshBoundingBox() |
|
156 |
||
0 | 157 |
# Returns the index in connectors list for the connector given |
158 |
def GetConnectorIndex(self, connector): |
|
159 |
if connector in self.Connectors: |
|
160 |
return self.Connectors.index(connector) |
|
161 |
return None |
|
162 |
||
163 |
# Returns if there is a connector in connectors list at the index given |
|
164 |
def IsNullConnector(self, idx): |
|
165 |
if idx < len(self.Connectors): |
|
166 |
return self.Connectors[idx] == None |
|
167 |
return False |
|
168 |
||
169 |
# Delete the connector or blank from connectors list at the index given |
|
170 |
def DeleteConnector(self, idx): |
|
171 |
self.Connectors.pop(idx) |
|
172 |
self.RefreshConnectors() |
|
173 |
self.RefreshSize() |
|
174 |
||
175 |
# Refresh the positions of the power rail connectors |
|
176 |
def RefreshConnectors(self): |
|
71 | 177 |
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: |
178 |
height = self.Size[1] - self.Extensions[0] - self.Extensions[1] |
|
80 | 179 |
interval = float(height) / float(max(len(self.Connectors) - 1, 1)) |
71 | 180 |
for i, connector in enumerate(self.Connectors): |
181 |
position = connector.GetRelPosition() |
|
80 | 182 |
if self.Type == LEFTRAIL: |
183 |
if self.RealConnectors: |
|
71 | 184 |
connector.SetPosition(wx.Point(self.Size[0], self.Extensions[0] + int(round(self.RealConnectors[i] * height)))) |
80 | 185 |
else: |
96 | 186 |
connector.SetPosition(wx.Point(self.Size[0], self.Extensions[0] + int(round(i * interval)))) |
80 | 187 |
elif self.Type == RIGHTRAIL: |
188 |
if self.RealConnectors: |
|
71 | 189 |
connector.SetPosition(wx.Point(0, self.Extensions[0] + int(round(self.RealConnectors[i] * height)))) |
80 | 190 |
else: |
96 | 191 |
connector.SetPosition(wx.Point(0, self.Extensions[0] + int(round(i * interval)))) |
71 | 192 |
else: |
193 |
position = self.Extensions[0] |
|
194 |
for connector in self.Connectors: |
|
195 |
if connector: |
|
196 |
if self.Type == LEFTRAIL: |
|
197 |
connector.SetPosition(wx.Point(self.Size[0], position)) |
|
198 |
elif self.Type == RIGHTRAIL: |
|
199 |
connector.SetPosition(wx.Point(0, position)) |
|
200 |
position += LD_LINE_SIZE |
|
0 | 201 |
self.RefreshConnected() |
202 |
||
7 | 203 |
# Refresh the position of wires connected to power rail |
0 | 204 |
def RefreshConnected(self, exclude = []): |
205 |
for connector in self.Connectors: |
|
8 | 206 |
if connector: |
207 |
connector.MoveConnected(exclude) |
|
0 | 208 |
|
209 |
# Returns the power rail connector that starts with the point given if it exists |
|
27 | 210 |
def GetConnector(self, position, name = None): |
211 |
# if a name is given |
|
212 |
if name: |
|
213 |
# Test each connector if it exists |
|
214 |
for connector in self.Connectors: |
|
215 |
if connector and name == connector.GetName(): |
|
216 |
return connector |
|
0 | 217 |
for connector in self.Connectors: |
218 |
if connector: |
|
219 |
connector_pos = connector.GetRelPosition() |
|
220 |
if position.x == self.Pos.x + connector_pos.x and position.y == self.Pos.y + connector_pos.y: |
|
221 |
return connector |
|
222 |
return None |
|
223 |
||
224 |
# Returns all the power rail connectors |
|
225 |
def GetConnectors(self): |
|
226 |
return [connector for connector in self.Connectors if connector] |
|
227 |
||
228 |
# Test if point given is on one of the power rail connectors |
|
229 |
def TestConnector(self, pt, exclude=True): |
|
230 |
for connector in self.Connectors: |
|
231 |
if connector and connector.TestPoint(pt, exclude): |
|
232 |
return connector |
|
233 |
return None |
|
234 |
||
235 |
# Returns the power rail type |
|
61
dc7142ae9438
Adding possibility to change Type and Pin number of power rails
lbessard
parents:
58
diff
changeset
|
236 |
def SetType(self, type, connectors): |
dc7142ae9438
Adding possibility to change Type and Pin number of power rails
lbessard
parents:
58
diff
changeset
|
237 |
if type != self.Type or len(self.Connectors) != len(connectors): |
dc7142ae9438
Adding possibility to change Type and Pin number of power rails
lbessard
parents:
58
diff
changeset
|
238 |
# Create a connector or a blank according to 'connectors' and add it in |
dc7142ae9438
Adding possibility to change Type and Pin number of power rails
lbessard
parents:
58
diff
changeset
|
239 |
# the connectors list |
dc7142ae9438
Adding possibility to change Type and Pin number of power rails
lbessard
parents:
58
diff
changeset
|
240 |
self.Type = type |
dc7142ae9438
Adding possibility to change Type and Pin number of power rails
lbessard
parents:
58
diff
changeset
|
241 |
self.Clean() |
dc7142ae9438
Adding possibility to change Type and Pin number of power rails
lbessard
parents:
58
diff
changeset
|
242 |
self.Connectors = [] |
dc7142ae9438
Adding possibility to change Type and Pin number of power rails
lbessard
parents:
58
diff
changeset
|
243 |
for connector in connectors: |
dc7142ae9438
Adding possibility to change Type and Pin number of power rails
lbessard
parents:
58
diff
changeset
|
244 |
self.AddConnector(connector) |
dc7142ae9438
Adding possibility to change Type and Pin number of power rails
lbessard
parents:
58
diff
changeset
|
245 |
self.RefreshSize() |
dc7142ae9438
Adding possibility to change Type and Pin number of power rails
lbessard
parents:
58
diff
changeset
|
246 |
|
dc7142ae9438
Adding possibility to change Type and Pin number of power rails
lbessard
parents:
58
diff
changeset
|
247 |
# Returns the power rail type |
0 | 248 |
def GetType(self): |
249 |
return self.Type |
|
250 |
||
27 | 251 |
# Method called when a LeftDown event have been generated |
252 |
def OnLeftDown(self, event, dc, scaling): |
|
253 |
pos = GetScaledEventPosition(event, dc, scaling) |
|
254 |
# Test if a connector have been handled |
|
255 |
connector = self.TestConnector(pos, False) |
|
256 |
if connector: |
|
257 |
self.Handle = (HANDLE_CONNECTOR, connector) |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
258 |
self.Parent.SetCursor(wx.StockCursor(wx.CURSOR_HAND)) |
27 | 259 |
self.Selected = False |
260 |
# Initializes the last position |
|
261 |
self.oldPos = GetScaledEventPosition(event, dc, scaling) |
|
262 |
else: |
|
71 | 263 |
self.RealConnectors = [] |
264 |
height = self.Size[1] - self.Extensions[0] - self.Extensions[1] |
|
265 |
for connector in self.Connectors: |
|
266 |
position = connector.GetRelPosition() |
|
267 |
self.RealConnectors.append(float(position.y - self.Extensions[0])/float(max(1, height))) |
|
27 | 268 |
Graphic_Element.OnLeftDown(self, event, dc, scaling) |
269 |
||
270 |
# Method called when a LeftUp event have been generated |
|
271 |
def OnLeftUp(self, event, dc, scaling): |
|
272 |
handle_type, handle = self.Handle |
|
273 |
if handle_type == HANDLE_CONNECTOR: |
|
274 |
wires = handle.GetWires() |
|
80 | 275 |
if len(wires) == 1: |
276 |
if handle == wires[0][0].StartConnected: |
|
277 |
block = wires[0][0].EndConnected.GetParentBlock() |
|
278 |
else: |
|
279 |
block = wires[0][0].StartConnected.GetParentBlock() |
|
280 |
block.RefreshModel(False) |
|
27 | 281 |
Graphic_Element.OnLeftUp(self, event, dc, scaling) |
96 | 282 |
self.RealConnectors = None |
27 | 283 |
|
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
284 |
# Method called when a LeftDClick event have been generated |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
285 |
def OnLeftDClick(self, event, dc, scaling): |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
286 |
# Edit the powerrail properties |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
287 |
self.Parent.EditPowerRailContent(self) |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
288 |
|
27 | 289 |
# Method called when a RightUp event have been generated |
290 |
def OnRightUp(self, event, dc, scaling): |
|
127
436268f31dae
Adding contextual menu on LD_Contact, LD_Coil, LD_PowerRail and SFC_ActionBlock
lbessard
parents:
112
diff
changeset
|
291 |
self.Parent.PopupDefaultMenu() |
27 | 292 |
|
110
29b6b70e1721
Bug that makes element resizing acting strongly fixed
lbessard
parents:
96
diff
changeset
|
293 |
# Refreshes the powerrail state according to move defined and handle selected |
27 | 294 |
def ProcessDragging(self, movex, movey): |
295 |
handle_type, handle = self.Handle |
|
296 |
# A connector has been handled |
|
297 |
if handle_type == HANDLE_CONNECTOR: |
|
138
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
127
diff
changeset
|
298 |
movey = max(-self.BoundingBox.y, movey) |
27 | 299 |
self.MoveConnector(handle, movey) |
138
9c74d00ce93e
Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents:
127
diff
changeset
|
300 |
return 0, movey |
110
29b6b70e1721
Bug that makes element resizing acting strongly fixed
lbessard
parents:
96
diff
changeset
|
301 |
else: |
29b6b70e1721
Bug that makes element resizing acting strongly fixed
lbessard
parents:
96
diff
changeset
|
302 |
return Graphic_Element.ProcessDragging(self, movex, movey) |
27 | 303 |
|
0 | 304 |
# Refreshes the power rail model |
305 |
def RefreshModel(self, move=True): |
|
306 |
self.Parent.RefreshPowerRailModel(self) |
|
307 |
# If power rail has moved and power rail is of type LEFT, refresh the model |
|
308 |
# of wires connected to connectors |
|
309 |
if move and self.Type == LEFTRAIL: |
|
310 |
for connector in self.Connectors: |
|
311 |
if connector: |
|
312 |
connector.RefreshWires() |
|
313 |
||
314 |
# Draws power rail |
|
315 |
def Draw(self, dc): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
316 |
dc.SetPen(wx.BLACK_PEN) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
317 |
dc.SetBrush(wx.BLACK_BRUSH) |
0 | 318 |
# Draw a rectangle with the power rail size |
319 |
dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1) |
|
320 |
# Draw connectors |
|
321 |
for connector in self.Connectors: |
|
322 |
if connector: |
|
323 |
connector.Draw(dc) |
|
324 |
Graphic_Element.Draw(self, dc) |
|
325 |
||
326 |
||
327 |
#------------------------------------------------------------------------------- |
|
328 |
# Ladder Diagram Contact |
|
329 |
#------------------------------------------------------------------------------- |
|
330 |
||
331 |
""" |
|
332 |
Class that implements the graphic representation of a contact |
|
333 |
""" |
|
334 |
||
335 |
class LD_Contact(Graphic_Element): |
|
336 |
||
337 |
# Create a new contact |
|
338 |
def __init__(self, parent, type, name, id = None): |
|
339 |
Graphic_Element.__init__(self, parent) |
|
340 |
self.Type = type |
|
341 |
self.Name = name |
|
342 |
self.Id = id |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
343 |
self.Size = wx.Size(LD_ELEMENT_SIZE[0], LD_ELEMENT_SIZE[1]) |
0 | 344 |
# Create an input and output connector |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
345 |
self.Input = Connector(self, "", "BOOL", wx.Point(0, self.Size[1] / 2 + 1), WEST) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
346 |
self.Output = Connector(self, "", "BOOL", wx.Point(self.Size[0], self.Size[1] / 2 + 1), EAST) |
42 | 347 |
self.RefreshNameSize() |
348 |
self.RefreshTypeSize() |
|
0 | 349 |
|
350 |
# Destructor |
|
351 |
def __del__(self): |
|
352 |
self.Input = None |
|
353 |
self.Output = None |
|
354 |
||
112 | 355 |
# Make a clone of this LD_Contact |
356 |
def Clone(self, id = None, pos = None): |
|
357 |
contact = LD_Contact(self.Parent, self.Type, self.Name, id) |
|
358 |
contact.SetSize(self.Size[0], self.Size[1]) |
|
359 |
if pos is not None: |
|
360 |
contact.SetPosition(pos.x, pos.y) |
|
361 |
contact.Input = self.Input.Clone(contact) |
|
362 |
contact.Output = self.Output.Clone(contact) |
|
363 |
return contact |
|
364 |
||
0 | 365 |
# Forbids to change the contact size |
366 |
def SetSize(self, width, height): |
|
42 | 367 |
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: |
27 | 368 |
Graphic_Element.SetSize(self, width, height) |
369 |
self.RefreshConnectors() |
|
0 | 370 |
|
371 |
# Delete this contact by calling the appropriate method |
|
372 |
def Delete(self): |
|
373 |
self.Parent.DeleteContact(self) |
|
374 |
||
375 |
# Unconnect input and output |
|
376 |
def Clean(self): |
|
61
dc7142ae9438
Adding possibility to change Type and Pin number of power rails
lbessard
parents:
58
diff
changeset
|
377 |
self.Input.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE) |
dc7142ae9438
Adding possibility to change Type and Pin number of power rails
lbessard
parents:
58
diff
changeset
|
378 |
self.Output.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE) |
0 | 379 |
|
42 | 380 |
# Refresh the size of text for name |
381 |
def RefreshNameSize(self): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
382 |
dc = wx.ClientDC(self.Parent) |
42 | 383 |
if self.Name != "": |
384 |
self.NameSize = dc.GetTextExtent(self.Name) |
|
385 |
else: |
|
386 |
self.NameSize = 0, 0 |
|
387 |
||
388 |
# Refresh the size of text for type |
|
389 |
def RefreshTypeSize(self): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
390 |
dc = wx.ClientDC(self.Parent) |
42 | 391 |
typetext = "" |
392 |
if self.Type == CONTACT_REVERSE: |
|
393 |
typetext = "/" |
|
394 |
elif self.Type == CONTACT_RISING: |
|
395 |
typetext = "P" |
|
396 |
elif self.Type == CONTACT_FALLING: |
|
397 |
typetext = "N" |
|
398 |
if typetext != "": |
|
399 |
self.TypeSize = dc.GetTextExtent(typetext) |
|
400 |
else: |
|
401 |
self.TypeSize = 0, 0 |
|
402 |
||
0 | 403 |
# Refresh the contact bounding box |
404 |
def RefreshBoundingBox(self): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
405 |
dc = wx.ClientDC(self.Parent) |
0 | 406 |
# Calculate the size of the name outside the contact |
407 |
text_width, text_height = dc.GetTextExtent(self.Name) |
|
408 |
# Calculate the bounding box size |
|
409 |
if self.Name != "": |
|
410 |
bbx_x = self.Pos.x - max(0, (text_width - self.Size[0]) / 2) |
|
411 |
bbx_width = max(self.Size[0], text_width) |
|
412 |
bbx_y = self.Pos.y - (text_height + 2) |
|
413 |
bbx_height = self.Size[1] + (text_height + 2) |
|
414 |
else: |
|
415 |
bbx_x = self.Pos.x |
|
416 |
bbx_width = self.Size[0] |
|
417 |
bbx_y = self.Pos.y |
|
418 |
bbx_height = self.Size[1] |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
419 |
self.BoundingBox = wx.Rect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1) |
0 | 420 |
|
27 | 421 |
# Returns the block minimum size |
422 |
def GetMinSize(self): |
|
423 |
return LD_ELEMENT_SIZE |
|
424 |
||
0 | 425 |
# Refresh the position of wire connected to contact |
426 |
def RefreshConnected(self, exclude = []): |
|
427 |
self.Input.MoveConnected(exclude) |
|
428 |
self.Output.MoveConnected(exclude) |
|
429 |
||
430 |
# Returns the contact connector that starts with the point given if it exists |
|
27 | 431 |
def GetConnector(self, position, name = None): |
432 |
# if a name is given |
|
433 |
if name: |
|
434 |
# Test input and output connector |
|
435 |
if name == self.Input.GetName(): |
|
436 |
return self.Input |
|
437 |
if name == self.Output.GetName(): |
|
438 |
return self.Output |
|
0 | 439 |
# Test input connector |
440 |
input_pos = self.Input.GetRelPosition() |
|
441 |
if position.x == self.Pos.x + input_pos.x and position.y == self.Pos.y + input_pos.y: |
|
442 |
return self.Input |
|
443 |
# Test output connector |
|
444 |
output_pos = self.Output.GetRelPosition() |
|
445 |
if position.x == self.Pos.x + output_pos.x and position.y == self.Pos.y + output_pos.y: |
|
446 |
return self.Output |
|
447 |
return None |
|
448 |
||
449 |
# Returns input and output contact connectors |
|
450 |
def GetConnectors(self): |
|
451 |
return {"input":self.Input,"output":self.Output} |
|
452 |
||
453 |
# Test if point given is on contact input or output connector |
|
454 |
def TestConnector(self, pt, exclude=True): |
|
455 |
# Test input connector |
|
456 |
if self.Input.TestPoint(pt, exclude): |
|
457 |
return self.Input |
|
458 |
# Test output connector |
|
459 |
if self.Output.TestPoint(pt, exclude): |
|
460 |
return self.Output |
|
461 |
return None |
|
462 |
||
27 | 463 |
# Refresh the positions of the block connectors |
464 |
def RefreshConnectors(self): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
465 |
self.Input.SetPosition(wx.Point(0, self.Size[1] / 2 + 1)) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
466 |
self.Output.SetPosition(wx.Point(self.Size[0], self.Size[1] / 2 + 1)) |
27 | 467 |
self.RefreshConnected() |
468 |
||
0 | 469 |
# Changes the contact name |
470 |
def SetName(self, name): |
|
471 |
self.Name = name |
|
42 | 472 |
self.RefreshNameSize() |
0 | 473 |
|
474 |
# Returns the contact name |
|
475 |
def GetName(self): |
|
476 |
return self.Name |
|
477 |
||
478 |
# Changes the contact type |
|
479 |
def SetType(self, type): |
|
480 |
self.Type = type |
|
50 | 481 |
self.RefreshTypeSize() |
0 | 482 |
|
483 |
# Returns the contact type |
|
484 |
def GetType(self): |
|
485 |
return self.Type |
|
486 |
||
487 |
# Method called when a LeftDClick event have been generated |
|
27 | 488 |
def OnLeftDClick(self, event, dc, scaling): |
0 | 489 |
# Edit the contact properties |
490 |
self.Parent.EditContactContent(self) |
|
491 |
||
127
436268f31dae
Adding contextual menu on LD_Contact, LD_Coil, LD_PowerRail and SFC_ActionBlock
lbessard
parents:
112
diff
changeset
|
492 |
# Method called when a RightUp event have been generated |
436268f31dae
Adding contextual menu on LD_Contact, LD_Coil, LD_PowerRail and SFC_ActionBlock
lbessard
parents:
112
diff
changeset
|
493 |
def OnRightUp(self, event, dc, scaling): |
436268f31dae
Adding contextual menu on LD_Contact, LD_Coil, LD_PowerRail and SFC_ActionBlock
lbessard
parents:
112
diff
changeset
|
494 |
# Popup the default menu |
436268f31dae
Adding contextual menu on LD_Contact, LD_Coil, LD_PowerRail and SFC_ActionBlock
lbessard
parents:
112
diff
changeset
|
495 |
self.Parent.PopupDefaultMenu() |
436268f31dae
Adding contextual menu on LD_Contact, LD_Coil, LD_PowerRail and SFC_ActionBlock
lbessard
parents:
112
diff
changeset
|
496 |
|
0 | 497 |
# Refreshes the contact model |
498 |
def RefreshModel(self, move=True): |
|
499 |
self.Parent.RefreshContactModel(self) |
|
500 |
# If contact has moved, refresh the model of wires connected to output |
|
501 |
if move: |
|
502 |
self.Output.RefreshWires() |
|
503 |
||
504 |
# Draws contact |
|
505 |
def Draw(self, dc): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
506 |
dc.SetPen(wx.BLACK_PEN) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
507 |
dc.SetBrush(wx.BLACK_BRUSH) |
0 | 508 |
# Draw two rectangles for representing the contact |
509 |
dc.DrawRectangle(self.Pos.x, self.Pos.y, 2, self.Size[1] + 1) |
|
510 |
dc.DrawRectangle(self.Pos.x + self.Size[0] - 1, self.Pos.y, 2, self.Size[1] + 1) |
|
511 |
# Draw contact name |
|
42 | 512 |
dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - self.NameSize[0]) / 2, |
513 |
self.Pos.y - (self.NameSize[1] + 2)) |
|
0 | 514 |
# Draw the modifier symbol in the middle of contact |
515 |
typetext = "" |
|
516 |
if self.Type == CONTACT_REVERSE: |
|
517 |
typetext = "/" |
|
518 |
elif self.Type == CONTACT_RISING: |
|
519 |
typetext = "P" |
|
520 |
elif self.Type == CONTACT_FALLING: |
|
521 |
typetext = "N" |
|
522 |
if typetext != "": |
|
42 | 523 |
dc.DrawText(typetext, self.Pos.x + (self.Size[0] - self.TypeSize[0]) / 2 + 1, |
524 |
self.Pos.y + (self.Size[1] - self.TypeSize[1]) / 2) |
|
0 | 525 |
# Draw input and output connectors |
526 |
self.Input.Draw(dc) |
|
527 |
self.Output.Draw(dc) |
|
528 |
Graphic_Element.Draw(self, dc) |
|
529 |
||
530 |
||
531 |
#------------------------------------------------------------------------------- |
|
532 |
# Ladder Diagram Coil |
|
533 |
#------------------------------------------------------------------------------- |
|
534 |
||
535 |
""" |
|
536 |
Class that implements the graphic representation of a coil |
|
537 |
""" |
|
538 |
||
539 |
class LD_Coil(Graphic_Element): |
|
540 |
||
541 |
# Create a new coil |
|
542 |
def __init__(self, parent, type, name, id = None): |
|
543 |
Graphic_Element.__init__(self, parent) |
|
544 |
self.Type = type |
|
545 |
self.Name = name |
|
546 |
self.Id = id |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
547 |
self.Size = wx.Size(LD_ELEMENT_SIZE[0], LD_ELEMENT_SIZE[1]) |
0 | 548 |
# Create an input and output connector |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
549 |
self.Input = Connector(self, "", "BOOL", wx.Point(0, self.Size[1] / 2 + 1), WEST) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
550 |
self.Output = Connector(self, "", "BOOL", wx.Point(self.Size[0], self.Size[1] / 2 + 1), EAST) |
42 | 551 |
self.RefreshNameSize() |
552 |
self.RefreshTypeSize() |
|
0 | 553 |
|
554 |
# Destructor |
|
555 |
def __del__(self): |
|
556 |
self.Input = None |
|
557 |
self.Output = None |
|
558 |
||
112 | 559 |
# Make a clone of this LD_Coil |
560 |
def Clone(self, id = None, pos = None): |
|
561 |
coil = LD_Coil(self.Parent, self.Type, self.Name, id) |
|
562 |
coil.SetSize(self.Size[0], self.Size[1]) |
|
563 |
if pos is not None: |
|
564 |
coil.SetPosition(pos.x, pos.y) |
|
565 |
coil.Input = self.Input.Clone(coil) |
|
566 |
coil.Output = self.Output.Clone(coil) |
|
567 |
return coil |
|
568 |
||
0 | 569 |
# Forbids to change the contact size |
570 |
def SetSize(self, width, height): |
|
42 | 571 |
if self.Parent.GetDrawingMode() == FREEDRAWING_MODE: |
27 | 572 |
Graphic_Element.SetSize(self, width, height) |
573 |
self.RefreshConnectors() |
|
0 | 574 |
|
575 |
# Delete this coil by calling the appropriate method |
|
576 |
def Delete(self): |
|
577 |
self.Parent.DeleteCoil(self) |
|
578 |
||
579 |
# Unconnect input and output |
|
580 |
def Clean(self): |
|
581 |
self.Input.UnConnect() |
|
582 |
self.Output.UnConnect() |
|
583 |
||
42 | 584 |
# Refresh the size of text for name |
585 |
def RefreshNameSize(self): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
586 |
dc = wx.ClientDC(self.Parent) |
42 | 587 |
if self.Name != "": |
588 |
self.NameSize = dc.GetTextExtent(self.Name) |
|
589 |
else: |
|
590 |
self.NameSize = 0, 0 |
|
591 |
||
592 |
# Refresh the size of text for type |
|
593 |
def RefreshTypeSize(self): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
594 |
dc = wx.ClientDC(self.Parent) |
42 | 595 |
typetext = "" |
596 |
if self.Type == COIL_REVERSE: |
|
597 |
typetext = "/" |
|
598 |
elif self.Type == COIL_SET: |
|
599 |
typetext = "S" |
|
600 |
elif self.Type == COIL_RESET: |
|
601 |
typetext = "R" |
|
602 |
if typetext != "": |
|
603 |
self.TypeSize = dc.GetTextExtent(typetext) |
|
604 |
else: |
|
605 |
self.TypeSize = 0, 0 |
|
606 |
||
0 | 607 |
# Refresh the coil bounding box |
608 |
def RefreshBoundingBox(self): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
609 |
dc = wx.ClientDC(self.Parent) |
0 | 610 |
# Calculate the size of the name outside the coil |
611 |
text_width, text_height = dc.GetTextExtent(self.Name) |
|
612 |
# Calculate the bounding box size |
|
613 |
if self.Name != "": |
|
614 |
bbx_x = self.Pos.x - max(0, (text_width - self.Size[0]) / 2) |
|
615 |
bbx_width = max(self.Size[0], text_width) |
|
616 |
bbx_y = self.Pos.y - (text_height + 2) |
|
617 |
bbx_height = self.Size[1] + (text_height + 2) |
|
618 |
else: |
|
619 |
bbx_x = self.Pos.x |
|
620 |
bbx_width = self.Size[0] |
|
621 |
bbx_y = self.Pos.y |
|
622 |
bbx_height = self.Size[1] |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
623 |
self.BoundingBox = wx.Rect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1) |
27 | 624 |
|
625 |
# Returns the block minimum size |
|
626 |
def GetMinSize(self): |
|
627 |
return LD_ELEMENT_SIZE |
|
0 | 628 |
|
629 |
# Refresh the position of wire connected to coil |
|
630 |
def RefreshConnected(self, exclude = []): |
|
631 |
self.Input.MoveConnected(exclude) |
|
632 |
self.Output.MoveConnected(exclude) |
|
633 |
||
634 |
# Returns the coil connector that starts with the point given if it exists |
|
27 | 635 |
def GetConnector(self, position, name = None): |
636 |
# if a name is given |
|
637 |
if name: |
|
638 |
# Test input and output connector |
|
639 |
if self.Input and name == self.Input.GetName(): |
|
640 |
return self.Input |
|
641 |
if self.Output and name == self.Output.GetName(): |
|
642 |
return self.Output |
|
0 | 643 |
# Test input connector |
644 |
input_pos = self.Input.GetRelPosition() |
|
645 |
if position.x == self.Pos.x + input_pos.x and position.y == self.Pos.y + input_pos.y: |
|
646 |
return self.Input |
|
647 |
# Test output connector |
|
648 |
output_pos = self.Output.GetRelPosition() |
|
649 |
if position.x == self.Pos.x + output_pos.x and position.y == self.Pos.y + output_pos.y: |
|
650 |
return self.Output |
|
651 |
return None |
|
652 |
||
653 |
# Returns input and output coil connectors |
|
654 |
def GetConnectors(self): |
|
655 |
return {"input":self.Input,"output":self.Output} |
|
656 |
||
657 |
# Test if point given is on coil input or output connector |
|
658 |
def TestConnector(self, pt, exclude=True): |
|
659 |
# Test input connector |
|
660 |
if self.Input.TestPoint(pt, exclude): |
|
661 |
return self.Input |
|
662 |
# Test output connector |
|
663 |
if self.Output.TestPoint(pt, exclude): |
|
664 |
return self.Output |
|
665 |
return None |
|
666 |
||
27 | 667 |
# Refresh the positions of the block connectors |
668 |
def RefreshConnectors(self): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
669 |
self.Input.SetPosition(wx.Point(0, self.Size[1] / 2 + 1)) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
670 |
self.Output.SetPosition(wx.Point(self.Size[0], self.Size[1] / 2 + 1)) |
27 | 671 |
self.RefreshConnected() |
672 |
||
0 | 673 |
# Changes the coil name |
674 |
def SetName(self, name): |
|
675 |
self.Name = name |
|
42 | 676 |
self.RefreshNameSize() |
0 | 677 |
|
678 |
# Returns the coil name |
|
679 |
def GetName(self): |
|
680 |
return self.Name |
|
681 |
||
682 |
# Changes the coil type |
|
683 |
def SetType(self, type): |
|
684 |
self.Type = type |
|
42 | 685 |
self.RefreshTypeSize() |
0 | 686 |
|
687 |
# Returns the coil type |
|
688 |
def GetType(self): |
|
689 |
return self.Type |
|
690 |
||
691 |
# Method called when a LeftDClick event have been generated |
|
27 | 692 |
def OnLeftDClick(self, event, dc, scaling): |
0 | 693 |
# Edit the coil properties |
694 |
self.Parent.EditCoilContent(self) |
|
695 |
||
127
436268f31dae
Adding contextual menu on LD_Contact, LD_Coil, LD_PowerRail and SFC_ActionBlock
lbessard
parents:
112
diff
changeset
|
696 |
# Method called when a RightUp event have been generated |
436268f31dae
Adding contextual menu on LD_Contact, LD_Coil, LD_PowerRail and SFC_ActionBlock
lbessard
parents:
112
diff
changeset
|
697 |
def OnRightUp(self, event, dc, scaling): |
436268f31dae
Adding contextual menu on LD_Contact, LD_Coil, LD_PowerRail and SFC_ActionBlock
lbessard
parents:
112
diff
changeset
|
698 |
# Popup the default menu |
436268f31dae
Adding contextual menu on LD_Contact, LD_Coil, LD_PowerRail and SFC_ActionBlock
lbessard
parents:
112
diff
changeset
|
699 |
self.Parent.PopupDefaultMenu() |
436268f31dae
Adding contextual menu on LD_Contact, LD_Coil, LD_PowerRail and SFC_ActionBlock
lbessard
parents:
112
diff
changeset
|
700 |
|
0 | 701 |
# Refreshes the coil model |
702 |
def RefreshModel(self, move=True): |
|
703 |
self.Parent.RefreshCoilModel(self) |
|
704 |
# If coil has moved, refresh the model of wires connected to output |
|
705 |
if move: |
|
706 |
self.Output.RefreshWires() |
|
707 |
||
708 |
# Draws coil |
|
709 |
def Draw(self, dc): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
710 |
dc.SetPen(wx.Pen(wx.BLACK, 2, wx.SOLID)) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
711 |
dc.SetBrush(wx.TRANSPARENT_BRUSH) |
0 | 712 |
# Draw a two circle arcs for representing the coil |
713 |
dc.DrawEllipticArc(self.Pos.x, self.Pos.y - int(self.Size[1] * (sqrt(2) - 1.) / 2.) + 1, self.Size[0], int(self.Size[1] * sqrt(2)) - 1, 135, 225) |
|
714 |
dc.DrawEllipticArc(self.Pos.x, self.Pos.y - int(self.Size[1] * (sqrt(2) - 1.) / 2.) + 1, self.Size[0], int(self.Size[1] * sqrt(2)) - 1, -45, 45) |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
61
diff
changeset
|
715 |
dc.SetPen(wx.BLACK_PEN) |
0 | 716 |
dc.DrawPoint(self.Pos.x + 1, self.Pos.y + self.Size[1] / 2 + 1) |
717 |
# Draw coil name |
|
42 | 718 |
dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - self.NameSize[0]) / 2, |
719 |
self.Pos.y - (self.NameSize[1] + 2)) |
|
0 | 720 |
# Draw the modifier symbol in the middle of coil |
721 |
typetext = "" |
|
722 |
if self.Type == COIL_REVERSE: |
|
723 |
typetext = "/" |
|
724 |
elif self.Type == COIL_SET: |
|
725 |
typetext = "S" |
|
726 |
elif self.Type == COIL_RESET: |
|
727 |
typetext = "R" |
|
728 |
if typetext != "": |
|
42 | 729 |
dc.DrawText(typetext, self.Pos.x + (self.Size[0] - self.TypeSize[0]) / 2 + 1, |
730 |
self.Pos.y + (self.Size[1] - self.TypeSize[1]) / 2) |
|
0 | 731 |
# Draw input and output connectors |
732 |
self.Input.Draw(dc) |
|
733 |
self.Output.Draw(dc) |
|
734 |
Graphic_Element.Draw(self, dc) |