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
|
|
46 |
# Create a connector or a blank according to 'connectors' and add it in
|
|
47 |
# the connectors list
|
|
48 |
self.Connectors = []
|
|
49 |
for connector in connectors:
|
|
50 |
self.AddConnector(connector)
|
|
51 |
self.RefreshSize()
|
|
52 |
|
|
53 |
# Destructor
|
|
54 |
def __del__(self):
|
|
55 |
self.Connectors = []
|
|
56 |
|
|
57 |
# Forbids to change the power rail size
|
|
58 |
def SetSize(self, width, height):
|
|
59 |
pass
|
|
60 |
|
|
61 |
# Forbids to select a power rail
|
|
62 |
def HitTest(self, pt):
|
|
63 |
return False
|
|
64 |
|
|
65 |
# Deletes this power rail by calling the appropriate method
|
|
66 |
def Delete(self):
|
|
67 |
self.Parent.DeletePowerRail(self)
|
|
68 |
|
|
69 |
# Unconnect all connectors
|
|
70 |
def Clean(self):
|
|
71 |
for connector in self.Connectors:
|
|
72 |
if connector:
|
|
73 |
connector.UnConnect()
|
|
74 |
|
|
75 |
# Refresh the power rail bounding box
|
|
76 |
def RefreshBoundingBox(self):
|
|
77 |
dc = wxClientDC(self.Parent)
|
|
78 |
if self.Type == LEFTRAIL:
|
|
79 |
bbx_x = self.Pos.x
|
|
80 |
elif self.Type == RIGHTRAIL:
|
|
81 |
bbx_x = self.Pos.x - CONNECTOR_SIZE
|
|
82 |
self.BoundingBox = wxRect(bbx_x, self.Pos.y, self.Size[0] + CONNECTOR_SIZE + 1, self.Size[1] + 1)
|
|
83 |
|
|
84 |
# Refresh the power rail size
|
|
85 |
def RefreshSize(self):
|
|
86 |
self.Size = wxSize(2, LD_LINE_SIZE * len(self.Connectors))
|
|
87 |
self.RefreshBoundingBox()
|
|
88 |
|
|
89 |
# Add a connector or a blank to this power rail at the last place
|
|
90 |
def AddConnector(self, connector = True):
|
|
91 |
self.InsertConnector(len(self.Connectors), connector)
|
|
92 |
|
|
93 |
# Add a connector or a blank to this power rail at the place given
|
|
94 |
def InsertConnector(self, idx, connector = True):
|
|
95 |
if connector:
|
|
96 |
if self.Type == LEFTRAIL:
|
|
97 |
connector = Connector(self, "", "BOOL", wxPoint(2, 0), EAST)
|
|
98 |
elif self.Type == RIGHTRAIL:
|
|
99 |
connector = Connector(self, "", "BOOL", wxPoint(0, 0), WEST)
|
|
100 |
self.Connectors.insert(idx, connector)
|
|
101 |
else:
|
|
102 |
self.Connectors.insert(idx, None)
|
|
103 |
self.RefreshSize()
|
|
104 |
self.RefreshConnectors()
|
|
105 |
|
|
106 |
# Returns the index in connectors list for the connector given
|
|
107 |
def GetConnectorIndex(self, connector):
|
|
108 |
if connector in self.Connectors:
|
|
109 |
return self.Connectors.index(connector)
|
|
110 |
return None
|
|
111 |
|
|
112 |
# Returns if there is a connector in connectors list at the index given
|
|
113 |
def IsNullConnector(self, idx):
|
|
114 |
if idx < len(self.Connectors):
|
|
115 |
return self.Connectors[idx] == None
|
|
116 |
return False
|
|
117 |
|
|
118 |
# Delete the connector or blank from connectors list at the index given
|
|
119 |
def DeleteConnector(self, idx):
|
|
120 |
self.Connectors.pop(idx)
|
|
121 |
self.RefreshConnectors()
|
|
122 |
self.RefreshSize()
|
|
123 |
|
|
124 |
# Refresh the positions of the power rail connectors
|
|
125 |
def RefreshConnectors(self):
|
|
126 |
position = LD_LINE_SIZE / 2
|
|
127 |
for connector in self.Connectors:
|
|
128 |
if connector:
|
|
129 |
if self.Type == LEFTRAIL:
|
|
130 |
connector.SetPosition(wxPoint(self.Size[0], position))
|
|
131 |
elif self.Type == RIGHTRAIL:
|
|
132 |
connector.SetPosition(wxPoint(0, position))
|
|
133 |
position += LD_LINE_SIZE
|
|
134 |
self.RefreshConnected()
|
|
135 |
|
7
|
136 |
# Refresh the position of wires connected to power rail
|
0
|
137 |
def RefreshConnected(self, exclude = []):
|
|
138 |
for connector in self.Connectors:
|
8
|
139 |
if connector:
|
|
140 |
connector.MoveConnected(exclude)
|
0
|
141 |
|
|
142 |
# Returns the power rail connector that starts with the point given if it exists
|
|
143 |
def GetConnector(self, position):
|
|
144 |
for connector in self.Connectors:
|
|
145 |
if connector:
|
|
146 |
connector_pos = connector.GetRelPosition()
|
|
147 |
if position.x == self.Pos.x + connector_pos.x and position.y == self.Pos.y + connector_pos.y:
|
|
148 |
return connector
|
|
149 |
return None
|
|
150 |
|
|
151 |
# Returns all the power rail connectors
|
|
152 |
def GetConnectors(self):
|
|
153 |
return [connector for connector in self.Connectors if connector]
|
|
154 |
|
|
155 |
# Test if point given is on one of the power rail connectors
|
|
156 |
def TestConnector(self, pt, exclude=True):
|
|
157 |
for connector in self.Connectors:
|
|
158 |
if connector and connector.TestPoint(pt, exclude):
|
|
159 |
return connector
|
|
160 |
return None
|
|
161 |
|
|
162 |
# Returns the power rail type
|
|
163 |
def GetType(self):
|
|
164 |
return self.Type
|
|
165 |
|
|
166 |
# Refreshes the power rail model
|
|
167 |
def RefreshModel(self, move=True):
|
|
168 |
self.Parent.RefreshPowerRailModel(self)
|
|
169 |
# If power rail has moved and power rail is of type LEFT, refresh the model
|
|
170 |
# of wires connected to connectors
|
|
171 |
if move and self.Type == LEFTRAIL:
|
|
172 |
for connector in self.Connectors:
|
|
173 |
if connector:
|
|
174 |
connector.RefreshWires()
|
|
175 |
|
|
176 |
# Draws power rail
|
|
177 |
def Draw(self, dc):
|
|
178 |
dc.SetPen(wxBLACK_PEN)
|
|
179 |
dc.SetBrush(wxBLACK_BRUSH)
|
|
180 |
# Draw a rectangle with the power rail size
|
|
181 |
dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
|
|
182 |
# Draw connectors
|
|
183 |
for connector in self.Connectors:
|
|
184 |
if connector:
|
|
185 |
connector.Draw(dc)
|
|
186 |
Graphic_Element.Draw(self, dc)
|
|
187 |
|
|
188 |
|
|
189 |
#-------------------------------------------------------------------------------
|
|
190 |
# Ladder Diagram Contact
|
|
191 |
#-------------------------------------------------------------------------------
|
|
192 |
|
|
193 |
"""
|
|
194 |
Class that implements the graphic representation of a contact
|
|
195 |
"""
|
|
196 |
|
|
197 |
class LD_Contact(Graphic_Element):
|
|
198 |
|
|
199 |
# Create a new contact
|
|
200 |
def __init__(self, parent, type, name, id = None):
|
|
201 |
Graphic_Element.__init__(self, parent)
|
|
202 |
self.Type = type
|
|
203 |
self.Name = name
|
|
204 |
self.Id = id
|
|
205 |
self.Size = wxSize(LD_ELEMENT_SIZE[0], LD_ELEMENT_SIZE[1])
|
|
206 |
# Create an input and output connector
|
|
207 |
self.Input = Connector(self, "", "BOOL", wxPoint(0, self.Size[1] / 2 + 1), WEST)
|
|
208 |
self.Output = Connector(self, "", "BOOL", wxPoint(self.Size[0], self.Size[1] / 2 + 1), EAST)
|
|
209 |
|
|
210 |
# Destructor
|
|
211 |
def __del__(self):
|
|
212 |
self.Input = None
|
|
213 |
self.Output = None
|
|
214 |
|
|
215 |
# Forbids to change the contact size
|
|
216 |
def SetSize(self, width, height):
|
|
217 |
pass
|
|
218 |
|
|
219 |
# Delete this contact by calling the appropriate method
|
|
220 |
def Delete(self):
|
|
221 |
self.Parent.DeleteContact(self)
|
|
222 |
|
|
223 |
# Unconnect input and output
|
|
224 |
def Clean(self):
|
|
225 |
self.Input.UnConnect()
|
|
226 |
self.Output.UnConnect()
|
|
227 |
|
|
228 |
# Refresh the contact bounding box
|
|
229 |
def RefreshBoundingBox(self):
|
|
230 |
dc = wxClientDC(self.Parent)
|
|
231 |
# Calculate the size of the name outside the contact
|
|
232 |
text_width, text_height = dc.GetTextExtent(self.Name)
|
|
233 |
# Calculate the bounding box size
|
|
234 |
if self.Name != "":
|
|
235 |
bbx_x = self.Pos.x - max(0, (text_width - self.Size[0]) / 2)
|
|
236 |
bbx_width = max(self.Size[0], text_width)
|
|
237 |
bbx_y = self.Pos.y - (text_height + 2)
|
|
238 |
bbx_height = self.Size[1] + (text_height + 2)
|
|
239 |
else:
|
|
240 |
bbx_x = self.Pos.x
|
|
241 |
bbx_width = self.Size[0]
|
|
242 |
bbx_y = self.Pos.y
|
|
243 |
bbx_height = self.Size[1]
|
|
244 |
self.BoundingBox = wxRect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1)
|
|
245 |
|
|
246 |
# Refresh the position of wire connected to contact
|
|
247 |
def RefreshConnected(self, exclude = []):
|
|
248 |
self.Input.MoveConnected(exclude)
|
|
249 |
self.Output.MoveConnected(exclude)
|
|
250 |
|
|
251 |
# Returns the contact connector that starts with the point given if it exists
|
|
252 |
def GetConnector(self, position):
|
|
253 |
# Test input connector
|
|
254 |
input_pos = self.Input.GetRelPosition()
|
|
255 |
if position.x == self.Pos.x + input_pos.x and position.y == self.Pos.y + input_pos.y:
|
|
256 |
return self.Input
|
|
257 |
# Test output connector
|
|
258 |
output_pos = self.Output.GetRelPosition()
|
|
259 |
if position.x == self.Pos.x + output_pos.x and position.y == self.Pos.y + output_pos.y:
|
|
260 |
return self.Output
|
|
261 |
return None
|
|
262 |
|
|
263 |
# Returns input and output contact connectors
|
|
264 |
def GetConnectors(self):
|
|
265 |
return {"input":self.Input,"output":self.Output}
|
|
266 |
|
|
267 |
# Test if point given is on contact input or output connector
|
|
268 |
def TestConnector(self, pt, exclude=True):
|
|
269 |
# Test input connector
|
|
270 |
if self.Input.TestPoint(pt, exclude):
|
|
271 |
return self.Input
|
|
272 |
# Test output connector
|
|
273 |
if self.Output.TestPoint(pt, exclude):
|
|
274 |
return self.Output
|
|
275 |
return None
|
|
276 |
|
|
277 |
# Changes the contact name
|
|
278 |
def SetName(self, name):
|
|
279 |
self.Name = name
|
|
280 |
|
|
281 |
# Returns the contact name
|
|
282 |
def GetName(self):
|
|
283 |
return self.Name
|
|
284 |
|
|
285 |
# Changes the contact type
|
|
286 |
def SetType(self, type):
|
|
287 |
self.Type = type
|
|
288 |
|
|
289 |
# Returns the contact type
|
|
290 |
def GetType(self):
|
|
291 |
return self.Type
|
|
292 |
|
|
293 |
# Method called when a LeftDClick event have been generated
|
|
294 |
def OnLeftDClick(self, event, scaling):
|
|
295 |
# Edit the contact properties
|
|
296 |
self.Parent.EditContactContent(self)
|
|
297 |
|
|
298 |
# Refreshes the contact model
|
|
299 |
def RefreshModel(self, move=True):
|
|
300 |
self.Parent.RefreshContactModel(self)
|
|
301 |
# If contact has moved, refresh the model of wires connected to output
|
|
302 |
if move:
|
|
303 |
self.Output.RefreshWires()
|
|
304 |
|
|
305 |
# Draws contact
|
|
306 |
def Draw(self, dc):
|
|
307 |
dc.SetPen(wxBLACK_PEN)
|
|
308 |
dc.SetBrush(wxBLACK_BRUSH)
|
|
309 |
# Draw two rectangles for representing the contact
|
|
310 |
dc.DrawRectangle(self.Pos.x, self.Pos.y, 2, self.Size[1] + 1)
|
|
311 |
dc.DrawRectangle(self.Pos.x + self.Size[0] - 1, self.Pos.y, 2, self.Size[1] + 1)
|
|
312 |
# Draw contact name
|
|
313 |
namewidth, nameheight = dc.GetTextExtent(self.Name)
|
|
314 |
dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - namewidth) / 2,
|
|
315 |
self.Pos.y - (nameheight + 2))
|
|
316 |
# Draw the modifier symbol in the middle of contact
|
|
317 |
typetext = ""
|
|
318 |
if self.Type == CONTACT_REVERSE:
|
|
319 |
typetext = "/"
|
|
320 |
elif self.Type == CONTACT_RISING:
|
|
321 |
typetext = "P"
|
|
322 |
elif self.Type == CONTACT_FALLING:
|
|
323 |
typetext = "N"
|
|
324 |
if typetext != "":
|
|
325 |
typewidth, typeheight = dc.GetTextExtent(typetext)
|
|
326 |
dc.DrawText(typetext, self.Pos.x + (self.Size[0] - typewidth) / 2 + 1,
|
|
327 |
self.Pos.y + (self.Size[1] - typeheight) / 2)
|
|
328 |
# Draw input and output connectors
|
|
329 |
self.Input.Draw(dc)
|
|
330 |
self.Output.Draw(dc)
|
|
331 |
Graphic_Element.Draw(self, dc)
|
|
332 |
|
|
333 |
|
|
334 |
#-------------------------------------------------------------------------------
|
|
335 |
# Ladder Diagram Coil
|
|
336 |
#-------------------------------------------------------------------------------
|
|
337 |
|
|
338 |
"""
|
|
339 |
Class that implements the graphic representation of a coil
|
|
340 |
"""
|
|
341 |
|
|
342 |
class LD_Coil(Graphic_Element):
|
|
343 |
|
|
344 |
# Create a new coil
|
|
345 |
def __init__(self, parent, type, name, id = None):
|
|
346 |
Graphic_Element.__init__(self, parent)
|
|
347 |
self.Type = type
|
|
348 |
self.Name = name
|
|
349 |
self.Id = id
|
|
350 |
self.Size = wxSize(LD_ELEMENT_SIZE[0], LD_ELEMENT_SIZE[1])
|
|
351 |
# Create an input and output connector
|
|
352 |
self.Input = Connector(self, "", "BOOL", wxPoint(0, self.Size[1] / 2 + 1), WEST)
|
|
353 |
self.Output = Connector(self, "", "BOOL", wxPoint(self.Size[0], self.Size[1] / 2 + 1), EAST)
|
|
354 |
|
|
355 |
# Destructor
|
|
356 |
def __del__(self):
|
|
357 |
self.Input = None
|
|
358 |
self.Output = None
|
|
359 |
|
|
360 |
# Forbids to change the contact size
|
|
361 |
def SetSize(self, width, height):
|
|
362 |
pass
|
|
363 |
|
|
364 |
# Delete this coil by calling the appropriate method
|
|
365 |
def Delete(self):
|
|
366 |
self.Parent.DeleteCoil(self)
|
|
367 |
|
|
368 |
# Unconnect input and output
|
|
369 |
def Clean(self):
|
|
370 |
self.Input.UnConnect()
|
|
371 |
self.Output.UnConnect()
|
|
372 |
|
|
373 |
# Refresh the coil bounding box
|
|
374 |
def RefreshBoundingBox(self):
|
|
375 |
dc = wxClientDC(self.Parent)
|
|
376 |
# Calculate the size of the name outside the coil
|
|
377 |
text_width, text_height = dc.GetTextExtent(self.Name)
|
|
378 |
# Calculate the bounding box size
|
|
379 |
if self.Name != "":
|
|
380 |
bbx_x = self.Pos.x - max(0, (text_width - self.Size[0]) / 2)
|
|
381 |
bbx_width = max(self.Size[0], text_width)
|
|
382 |
bbx_y = self.Pos.y - (text_height + 2)
|
|
383 |
bbx_height = self.Size[1] + (text_height + 2)
|
|
384 |
else:
|
|
385 |
bbx_x = self.Pos.x
|
|
386 |
bbx_width = self.Size[0]
|
|
387 |
bbx_y = self.Pos.y
|
|
388 |
bbx_height = self.Size[1]
|
|
389 |
self.BoundingBox = wxRect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1)
|
|
390 |
|
|
391 |
# Refresh the position of wire connected to coil
|
|
392 |
def RefreshConnected(self, exclude = []):
|
|
393 |
self.Input.MoveConnected(exclude)
|
|
394 |
self.Output.MoveConnected(exclude)
|
|
395 |
|
|
396 |
# Returns the coil connector that starts with the point given if it exists
|
|
397 |
def GetConnector(self, position):
|
|
398 |
# Test input connector
|
|
399 |
input_pos = self.Input.GetRelPosition()
|
|
400 |
if position.x == self.Pos.x + input_pos.x and position.y == self.Pos.y + input_pos.y:
|
|
401 |
return self.Input
|
|
402 |
# Test output connector
|
|
403 |
output_pos = self.Output.GetRelPosition()
|
|
404 |
if position.x == self.Pos.x + output_pos.x and position.y == self.Pos.y + output_pos.y:
|
|
405 |
return self.Output
|
|
406 |
return None
|
|
407 |
|
|
408 |
# Returns input and output coil connectors
|
|
409 |
def GetConnectors(self):
|
|
410 |
return {"input":self.Input,"output":self.Output}
|
|
411 |
|
|
412 |
# Test if point given is on coil input or output connector
|
|
413 |
def TestConnector(self, pt, exclude=True):
|
|
414 |
# Test input connector
|
|
415 |
if self.Input.TestPoint(pt, exclude):
|
|
416 |
return self.Input
|
|
417 |
# Test output connector
|
|
418 |
if self.Output.TestPoint(pt, exclude):
|
|
419 |
return self.Output
|
|
420 |
return None
|
|
421 |
|
|
422 |
# Changes the coil name
|
|
423 |
def SetName(self, name):
|
|
424 |
self.Name = name
|
|
425 |
|
|
426 |
# Returns the coil name
|
|
427 |
def GetName(self):
|
|
428 |
return self.Name
|
|
429 |
|
|
430 |
# Changes the coil type
|
|
431 |
def SetType(self, type):
|
|
432 |
self.Type = type
|
|
433 |
|
|
434 |
# Returns the coil type
|
|
435 |
def GetType(self):
|
|
436 |
return self.Type
|
|
437 |
|
|
438 |
# Method called when a LeftDClick event have been generated
|
|
439 |
def OnLeftDClick(self, event, scaling):
|
|
440 |
# Edit the coil properties
|
|
441 |
self.Parent.EditCoilContent(self)
|
|
442 |
|
|
443 |
# Refreshes the coil model
|
|
444 |
def RefreshModel(self, move=True):
|
|
445 |
self.Parent.RefreshCoilModel(self)
|
|
446 |
# If coil has moved, refresh the model of wires connected to output
|
|
447 |
if move:
|
|
448 |
self.Output.RefreshWires()
|
|
449 |
|
|
450 |
# Draws coil
|
|
451 |
def Draw(self, dc):
|
|
452 |
dc.SetPen(wxPen(wxBLACK, 2, wxSOLID))
|
|
453 |
dc.SetBrush(wxTRANSPARENT_BRUSH)
|
|
454 |
# Draw a two circle arcs for representing the coil
|
|
455 |
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)
|
|
456 |
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)
|
|
457 |
dc.SetPen(wxBLACK_PEN)
|
|
458 |
dc.DrawPoint(self.Pos.x + 1, self.Pos.y + self.Size[1] / 2 + 1)
|
|
459 |
# Draw coil name
|
|
460 |
namewidth, nameheight = dc.GetTextExtent(self.Name)
|
|
461 |
dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - namewidth) / 2,
|
|
462 |
self.Pos.y - (nameheight + 2))
|
|
463 |
# Draw the modifier symbol in the middle of coil
|
|
464 |
typetext = ""
|
|
465 |
if self.Type == COIL_REVERSE:
|
|
466 |
typetext = "/"
|
|
467 |
elif self.Type == COIL_SET:
|
|
468 |
typetext = "S"
|
|
469 |
elif self.Type == COIL_RESET:
|
|
470 |
typetext = "R"
|
|
471 |
if typetext != "":
|
|
472 |
typewidth, typeheight = dc.GetTextExtent(typetext)
|
|
473 |
dc.DrawText(typetext, self.Pos.x + (self.Size[0] - typewidth) / 2 + 1,
|
|
474 |
self.Pos.y + (self.Size[1] - typeheight) / 2)
|
|
475 |
# Draw input and output connectors
|
|
476 |
self.Input.Draw(dc)
|
|
477 |
self.Output.Draw(dc)
|
|
478 |
Graphic_Element.Draw(self, dc)
|