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