author | lbessard |
Fri, 20 Jul 2007 23:04:50 +0200 | |
changeset 51 | 47c72c77590a |
parent 42 | 4a8400732001 |
child 58 | 39cd981ff242 |
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 |
#------------------------------------------------------------------------------- |
|
33 |
# Function Block Diagram Block |
|
34 |
#------------------------------------------------------------------------------- |
|
35 |
||
36 |
""" |
|
37 |
Class that implements the graphic representation of a function block |
|
38 |
""" |
|
39 |
||
40 |
class FBD_Block(Graphic_Element): |
|
41 |
||
42 |
# Create a new block |
|
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
43 |
def __init__(self, parent, type, name, id = None, extension = 0, inputs = None): |
0 | 44 |
Graphic_Element.__init__(self, parent) |
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
45 |
self.Type = None |
27 | 46 |
self.Extension = None |
0 | 47 |
self.Name = name |
48 |
self.Id = id |
|
2 | 49 |
self.Inputs = [] |
50 |
self.Outputs = [] |
|
42 | 51 |
self.RefreshNameSize() |
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
52 |
self.SetType(type, extension, inputs) |
0 | 53 |
|
54 |
# Destructor |
|
55 |
def __del__(self): |
|
56 |
self.Inputs = [] |
|
57 |
self.Outputs = [] |
|
58 |
||
59 |
# Delete this block by calling the appropriate method |
|
60 |
def Delete(self): |
|
61 |
self.Parent.DeleteBlock(self) |
|
62 |
||
63 |
# Unconnect all inputs and outputs |
|
64 |
def Clean(self): |
|
65 |
for input in self.Inputs: |
|
2 | 66 |
input.UnConnect(delete = True) |
67 |
for output in self.Outputs: |
|
68 |
output.UnConnect(delete = True) |
|
0 | 69 |
|
42 | 70 |
# Refresh the size of text for name |
71 |
def RefreshNameSize(self): |
|
72 |
dc = wxClientDC(self.Parent) |
|
73 |
self.NameSize = dc.GetTextExtent(self.Name) |
|
74 |
||
0 | 75 |
# Refresh the block bounding box |
76 |
def RefreshBoundingBox(self): |
|
77 |
# Calculate the size of the name outside the block |
|
42 | 78 |
text_width, text_height = self.NameSize |
0 | 79 |
# Calculate the bounding box size |
80 |
bbx_x = self.Pos.x - max(min(1, len(self.Inputs)) * CONNECTOR_SIZE, (text_width - self.Size[0]) / 2) |
|
81 |
bbx_width = self.Size[0] + 1 + (min(1, len(self.Inputs)) + min(1, len(self.Outputs))) * CONNECTOR_SIZE |
|
82 |
if self.Name != "": |
|
83 |
bbx_y = self.Pos.y - (text_height + 2) |
|
84 |
bbx_height = self.Size[1] + (text_height + 2) |
|
85 |
else: |
|
86 |
bbx_y = self.Pos.y |
|
87 |
bbx_height = self.Size[1] |
|
88 |
self.BoundingBox = wxRect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1) |
|
89 |
||
90 |
# Refresh the positions of the block connectors |
|
91 |
def RefreshConnectors(self): |
|
92 |
# Calculate the size for the connector lines |
|
93 |
lines = max(len(self.Inputs), len(self.Outputs)) |
|
94 |
linesize = max((self.Size[1] - BLOCK_LINE_SIZE) / lines, BLOCK_LINE_SIZE) |
|
95 |
# Update inputs positions |
|
96 |
position = BLOCK_LINE_SIZE + linesize / 2 |
|
97 |
for input in self.Inputs: |
|
98 |
input.SetPosition(wxPoint(0, position)) |
|
99 |
position += linesize |
|
100 |
# Update outputs positions |
|
101 |
position = BLOCK_LINE_SIZE + linesize / 2 |
|
102 |
for output in self.Outputs: |
|
103 |
output.SetPosition(wxPoint(self.Size[0], position)) |
|
104 |
position += linesize |
|
105 |
self.RefreshConnected() |
|
106 |
||
107 |
# Refresh the positions of wires connected to inputs and outputs |
|
108 |
def RefreshConnected(self, exclude = []): |
|
109 |
for input in self.Inputs: |
|
110 |
input.MoveConnected(exclude) |
|
111 |
for output in self.Outputs: |
|
112 |
output.MoveConnected(exclude) |
|
113 |
||
114 |
# Returns the block connector that starts with the point given if it exists |
|
27 | 115 |
def GetConnector(self, position, name = None): |
116 |
# if a name is given |
|
117 |
if name: |
|
118 |
# Test each input and output connector |
|
119 |
for input in self.Inputs: |
|
120 |
if name == input.GetName(): |
|
121 |
return input |
|
122 |
for output in self.Outputs: |
|
123 |
if name == output.GetName(): |
|
124 |
return output |
|
0 | 125 |
# Test each input connector |
126 |
for input in self.Inputs: |
|
127 |
input_pos = input.GetRelPosition() |
|
128 |
if position.x == self.Pos.x + input_pos.x and position.y == self.Pos.y + input_pos.y: |
|
129 |
return input |
|
130 |
# Test each output connector |
|
131 |
for output in self.Outputs: |
|
132 |
output_pos = output.GetRelPosition() |
|
133 |
if position.x == self.Pos.x + output_pos.x and position.y == self.Pos.y + output_pos.y: |
|
134 |
return output |
|
135 |
return None |
|
136 |
||
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
137 |
def GetInputTypes(self): |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
138 |
return tuple([input.GetType() for input in self.Inputs]) |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
139 |
|
0 | 140 |
# Returns all the block connectors |
141 |
def GetConnectors(self): |
|
142 |
return {"inputs" : self.Inputs, "outputs" : self.Outputs} |
|
143 |
||
144 |
# Test if point given is on one of the block connectors |
|
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
145 |
def TestConnector(self, pt, exclude = True): |
0 | 146 |
# Test each input connector |
147 |
for input in self.Inputs: |
|
148 |
if input.TestPoint(pt, exclude): |
|
149 |
return input |
|
150 |
# Test each output connector |
|
151 |
for output in self.Outputs: |
|
152 |
if output.TestPoint(pt, exclude): |
|
153 |
return output |
|
154 |
return None |
|
155 |
||
2 | 156 |
# Changes the block type |
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
157 |
def SetType(self, type, extension, inputs = None): |
27 | 158 |
if type != self.Type or self.Extension != extension: |
42 | 159 |
if type != self.Type: |
160 |
self.Type = type |
|
161 |
dc = wxClientDC(self.Parent) |
|
162 |
self.TypeSize = dc.GetTextExtent(self.Type) |
|
27 | 163 |
self.Extension = extension |
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
164 |
# Find the block definition from type given and create the corresponding |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
165 |
# inputs and outputs |
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
166 |
blocktype = GetBlockType(type, inputs) |
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
167 |
if blocktype: |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
168 |
inputs = [input for input in blocktype["inputs"]] |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
169 |
outputs = [output for output in blocktype["outputs"]] |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
170 |
if blocktype["extensible"]: |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
171 |
start = int(inputs[-1][0].replace("IN", "")) |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
172 |
for i in xrange(self.Extension - len(blocktype["inputs"])): |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
173 |
start += 1 |
27 | 174 |
inputs.append(("IN%d"%start, inputs[-1][1], inputs[-1][2])) |
16 | 175 |
else: |
176 |
raise ValueError, "This block type isn't defined" |
|
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
177 |
self.Clean() |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
178 |
# Extract the inputs properties and create the corresponding connector |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
179 |
self.Inputs = [] |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
180 |
for input_name, input_type, input_modifier in inputs: |
27 | 181 |
connector = Connector(self, input_name, input_type, wxPoint(0, 0), WEST, onlyone = True) |
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
182 |
if input_modifier == "negated": |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
183 |
connector.SetNegated(True) |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
184 |
elif input_modifier != "none": |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
185 |
connector.SetEdge(input_modifier) |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
186 |
self.Inputs.append(connector) |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
187 |
# Extract the outputs properties and create the corresponding connector |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
188 |
self.Outputs = [] |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
189 |
for output_name, output_type, output_modifier in outputs: |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
190 |
connector = Connector(self, output_name, output_type, wxPoint(0, 0), EAST) |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
191 |
if output_modifier == "negated": |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
192 |
connector.SetNegated(True) |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
193 |
elif output_modifier != "none": |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
194 |
connector.SetEdge(output_modifier) |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
195 |
self.Outputs.append(connector) |
42 | 196 |
self.RefreshMinSize() |
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
197 |
self.RefreshConnectors() |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
198 |
self.RefreshBoundingBox() |
2 | 199 |
|
0 | 200 |
# Returns the block type |
201 |
def GetType(self): |
|
202 |
return self.Type |
|
203 |
||
204 |
# Changes the block name |
|
205 |
def SetName(self, name): |
|
206 |
self.Name = name |
|
42 | 207 |
self.RefreshNameSize() |
0 | 208 |
|
209 |
# Returs the block name |
|
210 |
def GetName(self): |
|
211 |
return self.Name |
|
212 |
||
2 | 213 |
# Changes the extension name |
214 |
def SetExtension(self, extension): |
|
215 |
self.Extension = extension |
|
216 |
||
217 |
# Returs the extension name |
|
218 |
def GetExtension(self): |
|
219 |
return self.Extension |
|
220 |
||
42 | 221 |
# Refresh the block minimum size |
222 |
def RefreshMinSize(self): |
|
0 | 223 |
# Calculate the inputs maximum width |
224 |
max_input = 0 |
|
225 |
for input in self.Inputs: |
|
42 | 226 |
w, h = input.GetNameSize() |
0 | 227 |
max_input = max(max_input, w) |
228 |
# Calculate the outputs maximum width |
|
229 |
max_output = 0 |
|
230 |
for output in self.Outputs: |
|
42 | 231 |
w, h = output.GetNameSize() |
0 | 232 |
max_output = max(max_output, w) |
42 | 233 |
width = max(self.TypeSize[0] + 10, max_input + max_output + 15) |
0 | 234 |
height = (max(len(self.Inputs), len(self.Outputs)) + 1) * BLOCK_LINE_SIZE |
42 | 235 |
self.MinSize = width, height |
236 |
||
237 |
# Returns the block minimum size |
|
238 |
def GetMinSize(self): |
|
239 |
return self.MinSize |
|
0 | 240 |
|
241 |
# Changes the negated property of the connector handled |
|
242 |
def SetConnectorNegated(self, negated): |
|
243 |
handle_type, handle = self.Handle |
|
244 |
if handle_type == HANDLE_CONNECTOR: |
|
245 |
handle.SetNegated(negated) |
|
246 |
self.RefreshModel(False) |
|
247 |
||
248 |
# Changes the edge property of the connector handled |
|
249 |
def SetConnectorEdge(self, edge): |
|
250 |
handle_type, handle = self.Handle |
|
251 |
if handle_type == HANDLE_CONNECTOR: |
|
252 |
handle.SetEdge(edge) |
|
253 |
self.RefreshModel(False) |
|
254 |
||
2 | 255 |
# Method called when a LeftDClick event have been generated |
27 | 256 |
def OnLeftDClick(self, event, dc, scaling): |
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
257 |
# Edit the block properties |
2 | 258 |
self.Parent.EditBlockContent(self) |
259 |
||
0 | 260 |
# Method called when a RightUp event have been generated |
27 | 261 |
def OnRightUp(self, event, dc, scaling): |
262 |
pos = GetScaledEventPosition(event, dc, scaling) |
|
0 | 263 |
# Popup the menu with special items for a block and a connector if one is handled |
264 |
connector = self.TestConnector(pos, False) |
|
265 |
if connector: |
|
266 |
self.Handle = (HANDLE_CONNECTOR, connector) |
|
267 |
self.Parent.PopupBlockMenu(connector) |
|
268 |
else: |
|
269 |
self.Parent.PopupBlockMenu() |
|
270 |
||
271 |
# Refreshes the block model |
|
272 |
def RefreshModel(self, move=True): |
|
273 |
self.Parent.RefreshBlockModel(self) |
|
274 |
# If block has moved, refresh the model of wires connected to outputs |
|
275 |
if move: |
|
276 |
for output in self.Outputs: |
|
277 |
output.RefreshWires() |
|
278 |
||
279 |
# Draws block |
|
280 |
def Draw(self, dc): |
|
281 |
dc.SetPen(wxBLACK_PEN) |
|
282 |
dc.SetBrush(wxWHITE_BRUSH) |
|
283 |
# Draw a rectangle with the block size |
|
284 |
dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1) |
|
285 |
# Draw block name and block type |
|
42 | 286 |
dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - self.NameSize[0]) / 2, |
287 |
self.Pos.y - (self.NameSize[1] + 2)) |
|
288 |
dc.DrawText(self.Type, self.Pos.x + (self.Size[0] - self.TypeSize[0]) / 2, |
|
0 | 289 |
self.Pos.y + 5) |
290 |
# Draw inputs and outputs connectors |
|
291 |
for input in self.Inputs: |
|
292 |
input.Draw(dc) |
|
293 |
for output in self.Outputs: |
|
294 |
output.Draw(dc) |
|
295 |
Graphic_Element.Draw(self, dc) |
|
296 |
||
297 |
||
298 |
#------------------------------------------------------------------------------- |
|
299 |
# Function Block Diagram Variable |
|
300 |
#------------------------------------------------------------------------------- |
|
301 |
||
302 |
""" |
|
303 |
Class that implements the graphic representation of a variable |
|
304 |
""" |
|
305 |
||
306 |
class FBD_Variable(Graphic_Element): |
|
307 |
||
308 |
# Create a new variable |
|
309 |
def __init__(self, parent, type, name, value_type, id = None): |
|
310 |
Graphic_Element.__init__(self, parent) |
|
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
311 |
self.Type = None |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
312 |
self.ValueType = None |
0 | 313 |
self.Name = name |
314 |
self.Id = id |
|
315 |
self.Input = None |
|
316 |
self.Output = None |
|
42 | 317 |
self.RefreshNameSize() |
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
318 |
self.SetType(type, value_type) |
0 | 319 |
|
320 |
# Destructor |
|
321 |
def __del__(self): |
|
322 |
self.Input = None |
|
323 |
self.Output = None |
|
324 |
||
325 |
# Unconnect connector |
|
326 |
def Clean(self): |
|
327 |
if self.Input: |
|
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
328 |
self.Input.UnConnect(delete = True) |
0 | 329 |
if self.Output: |
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
330 |
self.Output.UnConnect(delete = True) |
0 | 331 |
|
332 |
# Delete this variable by calling the appropriate method |
|
333 |
def Delete(self): |
|
334 |
self.Parent.DeleteVariable(self) |
|
335 |
||
42 | 336 |
# Refresh the size of text for name |
337 |
def RefreshNameSize(self): |
|
338 |
dc = wxClientDC(self.Parent) |
|
339 |
self.NameSize = dc.GetTextExtent(self.Name) |
|
340 |
||
0 | 341 |
# Refresh the variable bounding box |
342 |
def RefreshBoundingBox(self): |
|
343 |
if self.Type in (OUTPUT, INOUT): |
|
344 |
bbx_x = self.Pos.x - CONNECTOR_SIZE |
|
345 |
else: |
|
346 |
bbx_x = self.Pos.x |
|
347 |
if self.Type == INOUT: |
|
348 |
bbx_width = self.Size[0] + 2 * CONNECTOR_SIZE |
|
349 |
else: |
|
350 |
bbx_width = self.Size[0] + CONNECTOR_SIZE |
|
351 |
self.BoundingBox = wxRect(bbx_x, self.Pos.y, bbx_width + 1, self.Size[1] + 1) |
|
352 |
||
353 |
# Refresh the position of the variable connector |
|
354 |
def RefreshConnectors(self): |
|
355 |
if self.Input: |
|
356 |
self.Input.SetPosition(wxPoint(0, self.Size[1] / 2)) |
|
357 |
if self.Output: |
|
358 |
self.Output.SetPosition(wxPoint(self.Size[0], self.Size[1] / 2)) |
|
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
359 |
self.RefreshConnected() |
0 | 360 |
|
361 |
# Refresh the position of wires connected to connector |
|
362 |
def RefreshConnected(self, exclude = []): |
|
363 |
if self.Input: |
|
364 |
self.Input.MoveConnected(exclude) |
|
365 |
if self.Output: |
|
366 |
self.Output.MoveConnected(exclude) |
|
367 |
||
368 |
# Test if point given is on the variable connector |
|
369 |
def TestConnector(self, pt, exclude=True): |
|
370 |
if self.Input and self.Input.TestPoint(pt, exclude): |
|
371 |
return self.Input |
|
372 |
if self.Output and self.Output.TestPoint(pt, exclude): |
|
373 |
return self.Output |
|
374 |
return None |
|
375 |
||
376 |
# Returns the block connector that starts with the point given if it exists |
|
27 | 377 |
def GetConnector(self, position, name = None): |
378 |
# if a name is given |
|
379 |
if name: |
|
380 |
# Test input and output connector if they exists |
|
381 |
if self.Input and name == self.Input.GetName(): |
|
382 |
return self.Input |
|
383 |
if self.Output and name == self.Output.GetName(): |
|
384 |
return self.Output |
|
0 | 385 |
# Test input connector if it exists |
386 |
if self.Input: |
|
387 |
input_pos = self.Input.GetRelPosition() |
|
388 |
if position.x == self.Pos.x + input_pos.x and position.y == self.Pos.y + input_pos.y: |
|
389 |
return self.Input |
|
390 |
# Test output connector if it exists |
|
391 |
if self.Output: |
|
392 |
output_pos = self.Output.GetRelPosition() |
|
393 |
if position.x == self.Pos.x + output_pos.x and position.y == self.Pos.y + output_pos.y: |
|
394 |
return self.Output |
|
395 |
return None |
|
396 |
||
397 |
# Returns all the block connectors |
|
398 |
def GetConnectors(self): |
|
399 |
return {"input" : self.Input, "output" : self.Output} |
|
400 |
||
401 |
# Changes the negated property of the variable connector if handled |
|
402 |
def SetConnectorNegated(self, negated): |
|
403 |
handle_type, handle = self.Handle |
|
404 |
if handle_type == HANDLE_CONNECTOR: |
|
405 |
handle.SetNegated(negated) |
|
406 |
self.RefreshModel(False) |
|
407 |
||
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
408 |
# Changes the variable type |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
409 |
def SetType(self, type, value_type): |
32
cf9efccff009
FBD_Variable don't remove wire when just expression changed
lbessard
parents:
28
diff
changeset
|
410 |
if type != self.Type: |
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
411 |
self.Type = type |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
412 |
self.Clean() |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
413 |
self.Input = None |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
414 |
self.Output = None |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
415 |
# Create an input or output connector according to variable type |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
416 |
if self.Type != INPUT: |
27 | 417 |
self.Input = Connector(self, "", value_type, wxPoint(0, 0), WEST, onlyone = True) |
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
418 |
if self.Type != OUTPUT: |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
419 |
self.Output = Connector(self, "", value_type, wxPoint(0, 0), EAST) |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
420 |
self.RefreshConnectors() |
32
cf9efccff009
FBD_Variable don't remove wire when just expression changed
lbessard
parents:
28
diff
changeset
|
421 |
elif value_type != self.ValueType: |
cf9efccff009
FBD_Variable don't remove wire when just expression changed
lbessard
parents:
28
diff
changeset
|
422 |
if self.Input: |
cf9efccff009
FBD_Variable don't remove wire when just expression changed
lbessard
parents:
28
diff
changeset
|
423 |
self.Input.SetType(value_type) |
cf9efccff009
FBD_Variable don't remove wire when just expression changed
lbessard
parents:
28
diff
changeset
|
424 |
if self.Output: |
cf9efccff009
FBD_Variable don't remove wire when just expression changed
lbessard
parents:
28
diff
changeset
|
425 |
self.Output.SetType(value_type) |
cf9efccff009
FBD_Variable don't remove wire when just expression changed
lbessard
parents:
28
diff
changeset
|
426 |
self.RefreshConnectors() |
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
427 |
|
0 | 428 |
# Returns the variable type |
429 |
def GetType(self): |
|
430 |
return self.Type |
|
431 |
||
432 |
# Changes the variable name |
|
433 |
def SetName(self, name): |
|
434 |
self.Name = name |
|
42 | 435 |
self.RefreshNameSize() |
0 | 436 |
|
437 |
# Returns the variable name |
|
438 |
def GetName(self): |
|
439 |
return self.Name |
|
440 |
||
441 |
# Returns the variable minimum size |
|
442 |
def GetMinSize(self): |
|
42 | 443 |
return self.NameSize[0] + 10, self.NameSize[1] + 10 |
0 | 444 |
|
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
445 |
# Method called when a LeftDClick event have been generated |
27 | 446 |
def OnLeftDClick(self, event, dc, scaling): |
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
447 |
# Edit the variable properties |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
448 |
self.Parent.EditVariableContent(self) |
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
449 |
|
0 | 450 |
# Method called when a RightUp event have been generated |
27 | 451 |
def OnRightUp(self, event, dc, scaling): |
452 |
pos = GetScaledEventPosition(event, dc, scaling) |
|
0 | 453 |
# Popup the menu with special items for a variable and a connector if it's handled |
27 | 454 |
connector = self.TestConnector(pos, False) |
0 | 455 |
if connector: |
456 |
self.Handle = (HANDLE_CONNECTOR, connector) |
|
457 |
self.Parent.PopupVariableMenu(connector) |
|
458 |
else: |
|
459 |
self.Parent.PopupVariableMenu() |
|
460 |
||
461 |
# Refreshes the variable model |
|
462 |
def RefreshModel(self, move=True): |
|
463 |
self.Parent.RefreshVariableModel(self) |
|
464 |
# If variable has moved and variable is not of type OUTPUT, refresh the model |
|
465 |
# of wires connected to output connector |
|
466 |
if move and self.Type != OUTPUT: |
|
467 |
if self.Output: |
|
468 |
self.Output.RefreshWires() |
|
469 |
||
470 |
# Draws variable |
|
471 |
def Draw(self, dc): |
|
472 |
dc.SetPen(wxBLACK_PEN) |
|
473 |
dc.SetBrush(wxWHITE_BRUSH) |
|
474 |
# Draw a rectangle with the variable size |
|
475 |
dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1) |
|
476 |
# Draw variable name |
|
42 | 477 |
dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - self.NameSize[0]) / 2, |
478 |
self.Pos.y + (self.Size[1] - self.NameSize[1]) / 2) |
|
0 | 479 |
# Draw connectors |
480 |
if self.Input: |
|
481 |
self.Input.Draw(dc) |
|
482 |
if self.Output: |
|
483 |
self.Output.Draw(dc) |
|
484 |
Graphic_Element.Draw(self, dc) |
|
485 |
||
486 |
||
487 |
#------------------------------------------------------------------------------- |
|
488 |
# Function Block Diagram Connector |
|
489 |
#------------------------------------------------------------------------------- |
|
490 |
||
491 |
""" |
|
492 |
Class that implements the graphic representation of a connection |
|
493 |
""" |
|
494 |
||
495 |
class FBD_Connector(Graphic_Element): |
|
496 |
||
497 |
# Create a new connection |
|
498 |
def __init__(self, parent, type, name, id = None): |
|
499 |
Graphic_Element.__init__(self, parent) |
|
500 |
self.Type = type |
|
501 |
self.Name = name |
|
502 |
self.Id = id |
|
503 |
self.Pos = wxPoint(0, 0) |
|
504 |
self.Size = wxSize(0, 0) |
|
505 |
# Create an input or output connector according to connection type |
|
506 |
if self.Type == CONNECTOR: |
|
27 | 507 |
self.Connector = Connector(self, "", "ANY", wxPoint(0, 0), WEST, onlyone = True) |
0 | 508 |
else: |
509 |
self.Connector = Connector(self, "", "ANY", wxPoint(0, 0), EAST) |
|
510 |
self.RefreshConnectors() |
|
42 | 511 |
self.RefreshNameSize() |
0 | 512 |
|
513 |
# Destructor |
|
514 |
def __del__(self): |
|
515 |
self.Connector = None |
|
516 |
||
517 |
# Unconnect connector |
|
518 |
def Clean(self): |
|
519 |
if self.Connector: |
|
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
520 |
self.Connector.UnConnect(delete = True) |
0 | 521 |
|
522 |
# Delete this connection by calling the appropriate method |
|
523 |
def Delete(self): |
|
524 |
self.Parent.DeleteConnection(self) |
|
525 |
||
42 | 526 |
# Refresh the size of text for name |
527 |
def RefreshNameSize(self): |
|
528 |
dc = wxClientDC(self.Parent) |
|
529 |
self.NameSize = dc.GetTextExtent(self.Name) |
|
530 |
||
0 | 531 |
# Refresh the connection bounding box |
532 |
def RefreshBoundingBox(self): |
|
533 |
if self.Type == CONNECTOR: |
|
534 |
bbx_x = self.Pos.x - CONNECTOR_SIZE |
|
535 |
else: |
|
536 |
bbx_x = self.Pos.x |
|
537 |
bbx_width = self.Size[0] + CONNECTOR_SIZE |
|
538 |
self.BoundingBox = wxRect(bbx_x, self.Pos.y, bbx_width, self.Size[1]) |
|
539 |
||
540 |
# Refresh the position of the connection connector |
|
541 |
def RefreshConnectors(self): |
|
542 |
if self.Type == CONNECTOR: |
|
543 |
self.Connector.SetPosition(wxPoint(0, self.Size[1] / 2)) |
|
544 |
else: |
|
545 |
self.Connector.SetPosition(wxPoint(self.Size[0], self.Size[1] / 2)) |
|
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
546 |
self.RefreshConnected() |
0 | 547 |
|
548 |
# Refresh the position of wires connected to connector |
|
549 |
def RefreshConnected(self, exclude = []): |
|
550 |
if self.Connector: |
|
551 |
self.Connector.MoveConnected(exclude) |
|
552 |
||
553 |
# Test if point given is on the connection connector |
|
554 |
def TestConnector(self, pt, exclude=True): |
|
555 |
if self.Connector and self.Connector.TestPoint(pt, exclude): |
|
556 |
return self.Connector |
|
557 |
return None |
|
558 |
||
559 |
# Returns the connection connector |
|
27 | 560 |
def GetConnector(self, position = None, name = None): |
0 | 561 |
return self.Connector |
562 |
||
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
563 |
# Changes the variable type |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
564 |
def SetType(self, type): |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
565 |
if type != self.Type: |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
566 |
self.Type = type |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
567 |
self.Clean() |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
568 |
# Create an input or output connector according to connection type |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
569 |
if self.Type == CONNECTOR: |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
570 |
self.Connector = Connector(self, "", "ANY", wxPoint(0, 0), WEST, onlyone = True) |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
571 |
else: |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
572 |
self.Connector = Connector(self, "", "ANY", wxPoint(0, 0), EAST) |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
573 |
self.RefreshConnectors() |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
574 |
|
0 | 575 |
# Returns the connection type |
576 |
def GetType(self): |
|
577 |
return self.Type |
|
578 |
||
579 |
# Changes the connection name |
|
580 |
def SetName(self, name): |
|
581 |
self.Name = name |
|
42 | 582 |
self.RefreshNameSize() |
0 | 583 |
|
584 |
# Returns the connection name |
|
585 |
def GetName(self): |
|
586 |
return self.Name |
|
587 |
||
588 |
# Returns the connection minimum size |
|
589 |
def GetMinSize(self): |
|
42 | 590 |
text_width, text_height = self.NameSize |
0 | 591 |
if text_height % 2 == 1: |
592 |
text_height += 1 |
|
593 |
return text_width + text_height + 20, text_height + 10 |
|
594 |
||
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
595 |
# Method called when a LeftDClick event have been generated |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
596 |
def OnLeftDClick(self, event, dc, scaling): |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
597 |
# Edit the connection properties |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
598 |
self.Parent.EditConnectionContent(self) |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
599 |
|
0 | 600 |
# Method called when a RightUp event have been generated |
27 | 601 |
def OnRightUp(self, event, dc, scaling): |
0 | 602 |
# Popup the default menu |
603 |
self.Parent.PopupDefaultMenu() |
|
604 |
||
605 |
# Refreshes the connection model |
|
606 |
def RefreshModel(self, move=True): |
|
607 |
self.Parent.RefreshConnectionModel(self) |
|
608 |
# If connection has moved and connection is of type CONTINUATION, refresh |
|
609 |
# the model of wires connected to connector |
|
610 |
if move and self.Type == CONTINUATION: |
|
611 |
if self.Connector: |
|
612 |
self.Connector.RefreshWires() |
|
613 |
||
614 |
# Draws connection |
|
615 |
def Draw(self, dc): |
|
616 |
dc.SetPen(wxBLACK_PEN) |
|
617 |
dc.SetBrush(wxWHITE_BRUSH) |
|
618 |
# Draw a rectangle with the connection size with arrows in |
|
619 |
dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1) |
|
42 | 620 |
arrowsize = min(self.Size[1] / 2, (self.Size[0] - self.NameSize[0] - 10) / 2) |
0 | 621 |
dc.DrawLine(self.Pos.x, self.Pos.y, self.Pos.x + arrowsize, |
622 |
self.Pos.y + self.Size[1] / 2) |
|
623 |
dc.DrawLine(self.Pos.x + arrowsize, self.Pos.y + self.Size[1] / 2, |
|
624 |
self.Pos.x, self.Pos.y + self.Size[1]) |
|
625 |
dc.DrawLine(self.Pos.x + self.Size[0] - arrowsize, self.Pos.y, |
|
626 |
self.Pos.x + self.Size[0], self.Pos.y + self.Size[1] / 2) |
|
627 |
dc.DrawLine(self.Pos.x + self.Size[0], self.Pos.y + self.Size[1] / 2, |
|
628 |
self.Pos.x + self.Size[0] - arrowsize, self.Pos.y + self.Size[1]) |
|
629 |
# Draw variable name |
|
42 | 630 |
dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - self.NameSize[0]) / 2, |
631 |
self.Pos.y + (self.Size[1] - self.NameSize[1]) / 2) |
|
0 | 632 |
# Draw connector |
633 |
if self.Connector: |
|
634 |
self.Connector.Draw(dc) |
|
635 |
Graphic_Element.Draw(self, dc) |