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