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:
|
|
211 |
self.RealConnectors = {"Inputs":[],"Outputs":[]}
|
|
212 |
for input in self.Inputs:
|
|
213 |
position = input.GetRelPosition()
|
|
214 |
self.RealConnectors["Inputs"].append(float(position.x)/float(self.Size[0]))
|
|
215 |
for output in self.Outputs:
|
|
216 |
position = output.GetRelPosition()
|
|
217 |
self.RealConnectors["Outputs"].append(float(position.x)/float(self.Size[0]))
|
|
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 |
|
|
235 |
# Method called when a RightUp event have been generated
|
|
236 |
def OnRightUp(self, event, dc, scaling):
|
|
237 |
pos = GetScaledEventPosition(event, dc, scaling)
|
|
238 |
# Popup the menu with special items for a block and a connector if one is handled
|
|
239 |
connector = self.TestConnector(pos, False)
|
|
240 |
if connector:
|
|
241 |
self.Handle = (HANDLE_CONNECTOR, connector)
|
|
242 |
# self.Parent.PopupDivergenceMenu(True)
|
|
243 |
#else:
|
|
244 |
# Popup the divergence menu without delete branch
|
|
245 |
# self.Parent.PopupDivergenceMenu(False)
|
|
246 |
|
|
247 |
# Refreshes the divergence state according to move defined and handle selected
|
|
248 |
def ProcessDragging(self, movex, movey):
|
|
249 |
handle_type, handle = self.Handle
|
|
250 |
# A connector has been handled
|
|
251 |
if handle_type == HANDLE_CONNECTOR:
|
|
252 |
self.MoveConnector(handle, movey)
|
|
253 |
else:
|
|
254 |
Graphic_Element.ProcessDragging(self, movex, movey)
|
|
255 |
|
0
|
256 |
# Refreshes the power rail model
|
|
257 |
def RefreshModel(self, move=True):
|
|
258 |
self.Parent.RefreshPowerRailModel(self)
|
|
259 |
# If power rail has moved and power rail is of type LEFT, refresh the model
|
|
260 |
# of wires connected to connectors
|
|
261 |
if move and self.Type == LEFTRAIL:
|
|
262 |
for connector in self.Connectors:
|
|
263 |
if connector:
|
|
264 |
connector.RefreshWires()
|
|
265 |
|
|
266 |
# Draws power rail
|
|
267 |
def Draw(self, dc):
|
|
268 |
dc.SetPen(wxBLACK_PEN)
|
|
269 |
dc.SetBrush(wxBLACK_BRUSH)
|
|
270 |
# Draw a rectangle with the power rail size
|
|
271 |
dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
|
|
272 |
# Draw connectors
|
|
273 |
for connector in self.Connectors:
|
|
274 |
if connector:
|
|
275 |
connector.Draw(dc)
|
|
276 |
Graphic_Element.Draw(self, dc)
|
|
277 |
|
|
278 |
|
|
279 |
#-------------------------------------------------------------------------------
|
|
280 |
# Ladder Diagram Contact
|
|
281 |
#-------------------------------------------------------------------------------
|
|
282 |
|
|
283 |
"""
|
|
284 |
Class that implements the graphic representation of a contact
|
|
285 |
"""
|
|
286 |
|
|
287 |
class LD_Contact(Graphic_Element):
|
|
288 |
|
|
289 |
# Create a new contact
|
|
290 |
def __init__(self, parent, type, name, id = None):
|
|
291 |
Graphic_Element.__init__(self, parent)
|
|
292 |
self.Type = type
|
|
293 |
self.Name = name
|
|
294 |
self.Id = id
|
|
295 |
self.Size = wxSize(LD_ELEMENT_SIZE[0], LD_ELEMENT_SIZE[1])
|
|
296 |
# Create an input and output connector
|
|
297 |
self.Input = Connector(self, "", "BOOL", wxPoint(0, self.Size[1] / 2 + 1), WEST)
|
|
298 |
self.Output = Connector(self, "", "BOOL", wxPoint(self.Size[0], self.Size[1] / 2 + 1), EAST)
|
|
299 |
|
|
300 |
# Destructor
|
|
301 |
def __del__(self):
|
|
302 |
self.Input = None
|
|
303 |
self.Output = None
|
|
304 |
|
|
305 |
# Forbids to change the contact size
|
|
306 |
def SetSize(self, width, height):
|
27
|
307 |
if isinstance(self.Parent, wxPanel) or self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
|
|
308 |
Graphic_Element.SetSize(self, width, height)
|
|
309 |
self.RefreshConnectors()
|
0
|
310 |
|
|
311 |
# Delete this contact by calling the appropriate method
|
|
312 |
def Delete(self):
|
|
313 |
self.Parent.DeleteContact(self)
|
|
314 |
|
|
315 |
# Unconnect input and output
|
|
316 |
def Clean(self):
|
|
317 |
self.Input.UnConnect()
|
|
318 |
self.Output.UnConnect()
|
|
319 |
|
|
320 |
# Refresh the contact bounding box
|
|
321 |
def RefreshBoundingBox(self):
|
|
322 |
dc = wxClientDC(self.Parent)
|
|
323 |
# Calculate the size of the name outside the contact
|
|
324 |
text_width, text_height = dc.GetTextExtent(self.Name)
|
|
325 |
# Calculate the bounding box size
|
|
326 |
if self.Name != "":
|
|
327 |
bbx_x = self.Pos.x - max(0, (text_width - self.Size[0]) / 2)
|
|
328 |
bbx_width = max(self.Size[0], text_width)
|
|
329 |
bbx_y = self.Pos.y - (text_height + 2)
|
|
330 |
bbx_height = self.Size[1] + (text_height + 2)
|
|
331 |
else:
|
|
332 |
bbx_x = self.Pos.x
|
|
333 |
bbx_width = self.Size[0]
|
|
334 |
bbx_y = self.Pos.y
|
|
335 |
bbx_height = self.Size[1]
|
|
336 |
self.BoundingBox = wxRect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1)
|
|
337 |
|
27
|
338 |
# Returns the block minimum size
|
|
339 |
def GetMinSize(self):
|
|
340 |
return LD_ELEMENT_SIZE
|
|
341 |
|
0
|
342 |
# Refresh the position of wire connected to contact
|
|
343 |
def RefreshConnected(self, exclude = []):
|
|
344 |
self.Input.MoveConnected(exclude)
|
|
345 |
self.Output.MoveConnected(exclude)
|
|
346 |
|
|
347 |
# Returns the contact connector that starts with the point given if it exists
|
27
|
348 |
def GetConnector(self, position, name = None):
|
|
349 |
# if a name is given
|
|
350 |
if name:
|
|
351 |
# Test input and output connector
|
|
352 |
if name == self.Input.GetName():
|
|
353 |
return self.Input
|
|
354 |
if name == self.Output.GetName():
|
|
355 |
return self.Output
|
0
|
356 |
# Test input connector
|
|
357 |
input_pos = self.Input.GetRelPosition()
|
|
358 |
if position.x == self.Pos.x + input_pos.x and position.y == self.Pos.y + input_pos.y:
|
|
359 |
return self.Input
|
|
360 |
# Test output connector
|
|
361 |
output_pos = self.Output.GetRelPosition()
|
|
362 |
if position.x == self.Pos.x + output_pos.x and position.y == self.Pos.y + output_pos.y:
|
|
363 |
return self.Output
|
|
364 |
return None
|
|
365 |
|
|
366 |
# Returns input and output contact connectors
|
|
367 |
def GetConnectors(self):
|
|
368 |
return {"input":self.Input,"output":self.Output}
|
|
369 |
|
|
370 |
# Test if point given is on contact input or output connector
|
|
371 |
def TestConnector(self, pt, exclude=True):
|
|
372 |
# Test input connector
|
|
373 |
if self.Input.TestPoint(pt, exclude):
|
|
374 |
return self.Input
|
|
375 |
# Test output connector
|
|
376 |
if self.Output.TestPoint(pt, exclude):
|
|
377 |
return self.Output
|
|
378 |
return None
|
|
379 |
|
27
|
380 |
# Refresh the positions of the block connectors
|
|
381 |
def RefreshConnectors(self):
|
|
382 |
self.Input.SetPosition(wxPoint(0, self.Size[1] / 2 + 1))
|
|
383 |
self.Output.SetPosition(wxPoint(self.Size[0], self.Size[1] / 2 + 1))
|
|
384 |
self.RefreshConnected()
|
|
385 |
|
0
|
386 |
# Changes the contact name
|
|
387 |
def SetName(self, name):
|
|
388 |
self.Name = name
|
|
389 |
|
|
390 |
# Returns the contact name
|
|
391 |
def GetName(self):
|
|
392 |
return self.Name
|
|
393 |
|
|
394 |
# Changes the contact type
|
|
395 |
def SetType(self, type):
|
|
396 |
self.Type = type
|
|
397 |
|
|
398 |
# Returns the contact type
|
|
399 |
def GetType(self):
|
|
400 |
return self.Type
|
|
401 |
|
|
402 |
# Method called when a LeftDClick event have been generated
|
27
|
403 |
def OnLeftDClick(self, event, dc, scaling):
|
0
|
404 |
# Edit the contact properties
|
|
405 |
self.Parent.EditContactContent(self)
|
|
406 |
|
|
407 |
# Refreshes the contact model
|
|
408 |
def RefreshModel(self, move=True):
|
|
409 |
self.Parent.RefreshContactModel(self)
|
|
410 |
# If contact has moved, refresh the model of wires connected to output
|
|
411 |
if move:
|
|
412 |
self.Output.RefreshWires()
|
|
413 |
|
|
414 |
# Draws contact
|
|
415 |
def Draw(self, dc):
|
|
416 |
dc.SetPen(wxBLACK_PEN)
|
|
417 |
dc.SetBrush(wxBLACK_BRUSH)
|
|
418 |
# Draw two rectangles for representing the contact
|
|
419 |
dc.DrawRectangle(self.Pos.x, self.Pos.y, 2, self.Size[1] + 1)
|
|
420 |
dc.DrawRectangle(self.Pos.x + self.Size[0] - 1, self.Pos.y, 2, self.Size[1] + 1)
|
|
421 |
# Draw contact name
|
|
422 |
namewidth, nameheight = dc.GetTextExtent(self.Name)
|
|
423 |
dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - namewidth) / 2,
|
|
424 |
self.Pos.y - (nameheight + 2))
|
|
425 |
# Draw the modifier symbol in the middle of contact
|
|
426 |
typetext = ""
|
|
427 |
if self.Type == CONTACT_REVERSE:
|
|
428 |
typetext = "/"
|
|
429 |
elif self.Type == CONTACT_RISING:
|
|
430 |
typetext = "P"
|
|
431 |
elif self.Type == CONTACT_FALLING:
|
|
432 |
typetext = "N"
|
|
433 |
if typetext != "":
|
|
434 |
typewidth, typeheight = dc.GetTextExtent(typetext)
|
|
435 |
dc.DrawText(typetext, self.Pos.x + (self.Size[0] - typewidth) / 2 + 1,
|
|
436 |
self.Pos.y + (self.Size[1] - typeheight) / 2)
|
|
437 |
# Draw input and output connectors
|
|
438 |
self.Input.Draw(dc)
|
|
439 |
self.Output.Draw(dc)
|
|
440 |
Graphic_Element.Draw(self, dc)
|
|
441 |
|
|
442 |
|
|
443 |
#-------------------------------------------------------------------------------
|
|
444 |
# Ladder Diagram Coil
|
|
445 |
#-------------------------------------------------------------------------------
|
|
446 |
|
|
447 |
"""
|
|
448 |
Class that implements the graphic representation of a coil
|
|
449 |
"""
|
|
450 |
|
|
451 |
class LD_Coil(Graphic_Element):
|
|
452 |
|
|
453 |
# Create a new coil
|
|
454 |
def __init__(self, parent, type, name, id = None):
|
|
455 |
Graphic_Element.__init__(self, parent)
|
|
456 |
self.Type = type
|
|
457 |
self.Name = name
|
|
458 |
self.Id = id
|
|
459 |
self.Size = wxSize(LD_ELEMENT_SIZE[0], LD_ELEMENT_SIZE[1])
|
|
460 |
# Create an input and output connector
|
|
461 |
self.Input = Connector(self, "", "BOOL", wxPoint(0, self.Size[1] / 2 + 1), WEST)
|
|
462 |
self.Output = Connector(self, "", "BOOL", wxPoint(self.Size[0], self.Size[1] / 2 + 1), EAST)
|
|
463 |
|
|
464 |
# Destructor
|
|
465 |
def __del__(self):
|
|
466 |
self.Input = None
|
|
467 |
self.Output = None
|
|
468 |
|
|
469 |
# Forbids to change the contact size
|
|
470 |
def SetSize(self, width, height):
|
27
|
471 |
if isinstance(self.Parent, wxPanel) or self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
|
|
472 |
Graphic_Element.SetSize(self, width, height)
|
|
473 |
self.RefreshConnectors()
|
0
|
474 |
|
|
475 |
# Delete this coil by calling the appropriate method
|
|
476 |
def Delete(self):
|
|
477 |
self.Parent.DeleteCoil(self)
|
|
478 |
|
|
479 |
# Unconnect input and output
|
|
480 |
def Clean(self):
|
|
481 |
self.Input.UnConnect()
|
|
482 |
self.Output.UnConnect()
|
|
483 |
|
|
484 |
# Refresh the coil bounding box
|
|
485 |
def RefreshBoundingBox(self):
|
|
486 |
dc = wxClientDC(self.Parent)
|
|
487 |
# Calculate the size of the name outside the coil
|
|
488 |
text_width, text_height = dc.GetTextExtent(self.Name)
|
|
489 |
# Calculate the bounding box size
|
|
490 |
if self.Name != "":
|
|
491 |
bbx_x = self.Pos.x - max(0, (text_width - self.Size[0]) / 2)
|
|
492 |
bbx_width = max(self.Size[0], text_width)
|
|
493 |
bbx_y = self.Pos.y - (text_height + 2)
|
|
494 |
bbx_height = self.Size[1] + (text_height + 2)
|
|
495 |
else:
|
|
496 |
bbx_x = self.Pos.x
|
|
497 |
bbx_width = self.Size[0]
|
|
498 |
bbx_y = self.Pos.y
|
|
499 |
bbx_height = self.Size[1]
|
|
500 |
self.BoundingBox = wxRect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1)
|
27
|
501 |
|
|
502 |
# Returns the block minimum size
|
|
503 |
def GetMinSize(self):
|
|
504 |
return LD_ELEMENT_SIZE
|
0
|
505 |
|
|
506 |
# Refresh the position of wire connected to coil
|
|
507 |
def RefreshConnected(self, exclude = []):
|
|
508 |
self.Input.MoveConnected(exclude)
|
|
509 |
self.Output.MoveConnected(exclude)
|
|
510 |
|
|
511 |
# Returns the coil connector that starts with the point given if it exists
|
27
|
512 |
def GetConnector(self, position, name = None):
|
|
513 |
# if a name is given
|
|
514 |
if name:
|
|
515 |
# Test input and output connector
|
|
516 |
if self.Input and name == self.Input.GetName():
|
|
517 |
return self.Input
|
|
518 |
if self.Output and name == self.Output.GetName():
|
|
519 |
return self.Output
|
0
|
520 |
# Test input connector
|
|
521 |
input_pos = self.Input.GetRelPosition()
|
|
522 |
if position.x == self.Pos.x + input_pos.x and position.y == self.Pos.y + input_pos.y:
|
|
523 |
return self.Input
|
|
524 |
# Test output connector
|
|
525 |
output_pos = self.Output.GetRelPosition()
|
|
526 |
if position.x == self.Pos.x + output_pos.x and position.y == self.Pos.y + output_pos.y:
|
|
527 |
return self.Output
|
|
528 |
return None
|
|
529 |
|
|
530 |
# Returns input and output coil connectors
|
|
531 |
def GetConnectors(self):
|
|
532 |
return {"input":self.Input,"output":self.Output}
|
|
533 |
|
|
534 |
# Test if point given is on coil input or output connector
|
|
535 |
def TestConnector(self, pt, exclude=True):
|
|
536 |
# Test input connector
|
|
537 |
if self.Input.TestPoint(pt, exclude):
|
|
538 |
return self.Input
|
|
539 |
# Test output connector
|
|
540 |
if self.Output.TestPoint(pt, exclude):
|
|
541 |
return self.Output
|
|
542 |
return None
|
|
543 |
|
27
|
544 |
# Refresh the positions of the block connectors
|
|
545 |
def RefreshConnectors(self):
|
|
546 |
self.Input.SetPosition(wxPoint(0, self.Size[1] / 2 + 1))
|
|
547 |
self.Output.SetPosition(wxPoint(self.Size[0], self.Size[1] / 2 + 1))
|
|
548 |
self.RefreshConnected()
|
|
549 |
|
0
|
550 |
# Changes the coil name
|
|
551 |
def SetName(self, name):
|
|
552 |
self.Name = name
|
|
553 |
|
|
554 |
# Returns the coil name
|
|
555 |
def GetName(self):
|
|
556 |
return self.Name
|
|
557 |
|
|
558 |
# Changes the coil type
|
|
559 |
def SetType(self, type):
|
|
560 |
self.Type = type
|
|
561 |
|
|
562 |
# Returns the coil type
|
|
563 |
def GetType(self):
|
|
564 |
return self.Type
|
|
565 |
|
|
566 |
# Method called when a LeftDClick event have been generated
|
27
|
567 |
def OnLeftDClick(self, event, dc, scaling):
|
0
|
568 |
# Edit the coil properties
|
|
569 |
self.Parent.EditCoilContent(self)
|
|
570 |
|
|
571 |
# Refreshes the coil model
|
|
572 |
def RefreshModel(self, move=True):
|
|
573 |
self.Parent.RefreshCoilModel(self)
|
|
574 |
# If coil has moved, refresh the model of wires connected to output
|
|
575 |
if move:
|
|
576 |
self.Output.RefreshWires()
|
|
577 |
|
|
578 |
# Draws coil
|
|
579 |
def Draw(self, dc):
|
|
580 |
dc.SetPen(wxPen(wxBLACK, 2, wxSOLID))
|
|
581 |
dc.SetBrush(wxTRANSPARENT_BRUSH)
|
|
582 |
# Draw a two circle arcs for representing the coil
|
|
583 |
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)
|
|
584 |
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)
|
|
585 |
dc.SetPen(wxBLACK_PEN)
|
|
586 |
dc.DrawPoint(self.Pos.x + 1, self.Pos.y + self.Size[1] / 2 + 1)
|
|
587 |
# Draw coil name
|
|
588 |
namewidth, nameheight = dc.GetTextExtent(self.Name)
|
|
589 |
dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - namewidth) / 2,
|
|
590 |
self.Pos.y - (nameheight + 2))
|
|
591 |
# Draw the modifier symbol in the middle of coil
|
|
592 |
typetext = ""
|
|
593 |
if self.Type == COIL_REVERSE:
|
|
594 |
typetext = "/"
|
|
595 |
elif self.Type == COIL_SET:
|
|
596 |
typetext = "S"
|
|
597 |
elif self.Type == COIL_RESET:
|
|
598 |
typetext = "R"
|
|
599 |
if typetext != "":
|
|
600 |
typewidth, typeheight = dc.GetTextExtent(typetext)
|
|
601 |
dc.DrawText(typetext, self.Pos.x + (self.Size[0] - typewidth) / 2 + 1,
|
|
602 |
self.Pos.y + (self.Size[1] - typeheight) / 2)
|
|
603 |
# Draw input and output connectors
|
|
604 |
self.Input.Draw(dc)
|
|
605 |
self.Output.Draw(dc)
|
|
606 |
Graphic_Element.Draw(self, dc)
|