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