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