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