author | greg |
Tue, 15 Jan 2008 10:51:18 +0100 | |
changeset 148 | a083d4a94bd3 |
parent 145 | 4fb225afddf4 |
child 162 | e746ff4aa8be |
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 |
# |
|
58 | 7 |
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD |
0 | 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 |
|
58 | 19 |
#General Public License for more details. |
0 | 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 |
import wx |
|
26 |
||
27 |
from GraphicCommons import * |
|
28 |
from plcopen.structures import * |
|
99
2b18a72dcaf0
Added support for standard functions type compatibility check
lbessard
parents:
90
diff
changeset
|
29 |
from plcopen.structures import IsOfType |
0 | 30 |
|
31 |
#------------------------------------------------------------------------------- |
|
32 |
# Function Block Diagram Block |
|
33 |
#------------------------------------------------------------------------------- |
|
34 |
||
35 |
""" |
|
36 |
Class that implements the graphic representation of a function block |
|
37 |
""" |
|
38 |
||
39 |
class FBD_Block(Graphic_Element): |
|
40 |
||
41 |
# Create a new block |
|
118
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
42 |
def __init__(self, parent, type, name, id = None, extension = 0, inputs = None, connectors = {}, executionOrder = 0): |
0 | 43 |
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
|
44 |
self.Type = None |
27 | 45 |
self.Extension = None |
0 | 46 |
self.Id = id |
118
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
47 |
self.SetName(name) |
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
48 |
self.SetExecutionOrder(executionOrder) |
2 | 49 |
self.Inputs = [] |
50 |
self.Outputs = [] |
|
90
2245e8776086
Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents:
64
diff
changeset
|
51 |
self.Colour = wx.BLACK |
2245e8776086
Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents:
64
diff
changeset
|
52 |
self.Pen = wx.BLACK_PEN |
2245e8776086
Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents:
64
diff
changeset
|
53 |
self.SetType(type, extension, inputs, connectors) |
0 | 54 |
|
112 | 55 |
# Make a clone of this FBD_Block |
56 |
def Clone(self, id = None, name = "", pos = None): |
|
144 | 57 |
if self.Name != "" and name == "": |
58 |
name = self.Name |
|
112 | 59 |
block = FBD_Block(self.Parent, self.Type, name, id, self.Extension) |
60 |
block.SetSize(self.Size[0], self.Size[1]) |
|
61 |
if pos is not None: |
|
62 |
block.SetPosition(pos.x, pos.y) |
|
63 |
block.Inputs = [input.Clone(block) for input in self.Inputs] |
|
64 |
block.Outputs = [output.Clone(block) for output in self.Outputs] |
|
65 |
return block |
|
66 |
||
0 | 67 |
# Destructor |
68 |
def __del__(self): |
|
69 |
self.Inputs = [] |
|
70 |
self.Outputs = [] |
|
71 |
||
144 | 72 |
# Returns the RedrawRect |
73 |
def GetRedrawRect(self, movex = 0, movey = 0): |
|
74 |
rect = Graphic_Element.GetRedrawRect(self, movex, movey) |
|
75 |
if movex != 0 or movey != 0: |
|
76 |
for input in self.Inputs: |
|
77 |
if input.IsConnected(): |
|
78 |
rect = rect.Union(input.GetConnectedRedrawRect(movex, movey)) |
|
79 |
for output in self.Outputs: |
|
80 |
if output.IsConnected(): |
|
81 |
rect = rect.Union(output.GetConnectedRedrawRect(movex, movey)) |
|
82 |
return rect |
|
83 |
||
0 | 84 |
# Delete this block by calling the appropriate method |
85 |
def Delete(self): |
|
86 |
self.Parent.DeleteBlock(self) |
|
87 |
||
88 |
# Unconnect all inputs and outputs |
|
89 |
def Clean(self): |
|
90 |
for input in self.Inputs: |
|
2 | 91 |
input.UnConnect(delete = True) |
92 |
for output in self.Outputs: |
|
93 |
output.UnConnect(delete = True) |
|
0 | 94 |
|
42 | 95 |
# Refresh the size of text for name |
96 |
def RefreshNameSize(self): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
97 |
dc = wx.ClientDC(self.Parent) |
42 | 98 |
self.NameSize = dc.GetTextExtent(self.Name) |
99 |
||
118
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
100 |
# Refresh the size of text for execution order |
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
101 |
def RefreshExecutionOrderSize(self): |
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
102 |
dc = wx.ClientDC(self.Parent) |
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
103 |
self.ExecutionOrderSize = dc.GetTextExtent(str(self.ExecutionOrder)) |
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
104 |
|
0 | 105 |
# Refresh the block bounding box |
106 |
def RefreshBoundingBox(self): |
|
107 |
# Calculate the size of the name outside the block |
|
42 | 108 |
text_width, text_height = self.NameSize |
0 | 109 |
# Calculate the bounding box size |
110 |
bbx_x = self.Pos.x - max(min(1, len(self.Inputs)) * CONNECTOR_SIZE, (text_width - self.Size[0]) / 2) |
|
111 |
bbx_width = self.Size[0] + 1 + (min(1, len(self.Inputs)) + min(1, len(self.Outputs))) * CONNECTOR_SIZE |
|
112 |
if self.Name != "": |
|
113 |
bbx_y = self.Pos.y - (text_height + 2) |
|
114 |
bbx_height = self.Size[1] + (text_height + 2) |
|
115 |
else: |
|
116 |
bbx_y = self.Pos.y |
|
117 |
bbx_height = self.Size[1] |
|
118
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
118 |
if self.ExecutionOrder != 0: |
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
119 |
bbx_x = min(bbx_x, self.Pos.x + self.Size[0] - self.ExecutionOrderSize[0]) |
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
120 |
bbx_width = max(bbx_width, bbx_width + self.Pos.x + self.ExecutionOrderSize[0] - bbx_x - self.Size[0]) |
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
121 |
bbx_height = bbx_height + (self.ExecutionOrderSize[1] + 2) |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
122 |
self.BoundingBox = wx.Rect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1) |
0 | 123 |
|
124 |
# Refresh the positions of the block connectors |
|
125 |
def RefreshConnectors(self): |
|
145 | 126 |
scaling = self.Parent.GetScaling() |
0 | 127 |
# Calculate the size for the connector lines |
128 |
lines = max(len(self.Inputs), len(self.Outputs)) |
|
103 | 129 |
if lines > 0: |
130 |
linesize = max((self.Size[1] - BLOCK_LINE_SIZE) / lines, BLOCK_LINE_SIZE) |
|
145 | 131 |
# Update inputs and outputs positions |
103 | 132 |
position = BLOCK_LINE_SIZE + linesize / 2 |
145 | 133 |
for i in xrange(lines): |
134 |
if scaling is not None: |
|
135 |
ypos = round(float(self.Pos.y + position) / float(scaling[1])) * scaling[1] - self.Pos.y |
|
136 |
else: |
|
137 |
ypos = position |
|
138 |
if i < len(self.Inputs): |
|
139 |
self.Inputs[i].SetPosition(wx.Point(0, ypos)) |
|
140 |
if i < len(self.Outputs): |
|
141 |
self.Outputs[i].SetPosition(wx.Point(self.Size[0], ypos)) |
|
103 | 142 |
position += linesize |
0 | 143 |
self.RefreshConnected() |
144 |
||
145 |
# Refresh the positions of wires connected to inputs and outputs |
|
146 |
def RefreshConnected(self, exclude = []): |
|
147 |
for input in self.Inputs: |
|
148 |
input.MoveConnected(exclude) |
|
149 |
for output in self.Outputs: |
|
150 |
output.MoveConnected(exclude) |
|
151 |
||
152 |
# Returns the block connector that starts with the point given if it exists |
|
27 | 153 |
def GetConnector(self, position, name = None): |
154 |
# if a name is given |
|
155 |
if name: |
|
156 |
# Test each input and output connector |
|
157 |
for input in self.Inputs: |
|
158 |
if name == input.GetName(): |
|
159 |
return input |
|
160 |
for output in self.Outputs: |
|
161 |
if name == output.GetName(): |
|
162 |
return output |
|
0 | 163 |
# Test each input connector |
164 |
for input in self.Inputs: |
|
165 |
input_pos = input.GetRelPosition() |
|
166 |
if position.x == self.Pos.x + input_pos.x and position.y == self.Pos.y + input_pos.y: |
|
167 |
return input |
|
168 |
# Test each output connector |
|
169 |
for output in self.Outputs: |
|
170 |
output_pos = output.GetRelPosition() |
|
171 |
if position.x == self.Pos.x + output_pos.x and position.y == self.Pos.y + output_pos.y: |
|
172 |
return output |
|
173 |
return None |
|
174 |
||
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
175 |
def GetInputTypes(self): |
112 | 176 |
return tuple([input.GetType(True) for input in self.Inputs]) |
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
177 |
|
102
85875dcb7754
Adding edit user's POU by double click on block instance
lbessard
parents:
99
diff
changeset
|
178 |
def SetOutputValues(self, values): |
85875dcb7754
Adding edit user's POU by double click on block instance
lbessard
parents:
99
diff
changeset
|
179 |
for output in self.Outputs: |
85875dcb7754
Adding edit user's POU by double click on block instance
lbessard
parents:
99
diff
changeset
|
180 |
output.SetValue(values.get(ouput.getName(), None)) |
85875dcb7754
Adding edit user's POU by double click on block instance
lbessard
parents:
99
diff
changeset
|
181 |
|
85875dcb7754
Adding edit user's POU by double click on block instance
lbessard
parents:
99
diff
changeset
|
182 |
def GetConnectionResultType(self, connector, connectortype): |
85875dcb7754
Adding edit user's POU by double click on block instance
lbessard
parents:
99
diff
changeset
|
183 |
resulttype = connectortype |
99
2b18a72dcaf0
Added support for standard functions type compatibility check
lbessard
parents:
90
diff
changeset
|
184 |
for input in self.Inputs: |
2b18a72dcaf0
Added support for standard functions type compatibility check
lbessard
parents:
90
diff
changeset
|
185 |
name = input.GetName() |
2b18a72dcaf0
Added support for standard functions type compatibility check
lbessard
parents:
90
diff
changeset
|
186 |
if input != connector and (name.startswith("IN") or name in ["MN", "MX"]): |
2b18a72dcaf0
Added support for standard functions type compatibility check
lbessard
parents:
90
diff
changeset
|
187 |
inputtype = input.GetConnectedType() |
2b18a72dcaf0
Added support for standard functions type compatibility check
lbessard
parents:
90
diff
changeset
|
188 |
if resulttype is None or inputtype is not None and IsOfType(inputtype, resulttype): |
2b18a72dcaf0
Added support for standard functions type compatibility check
lbessard
parents:
90
diff
changeset
|
189 |
resulttype = inputtype |
2b18a72dcaf0
Added support for standard functions type compatibility check
lbessard
parents:
90
diff
changeset
|
190 |
for output in self.Outputs: |
2b18a72dcaf0
Added support for standard functions type compatibility check
lbessard
parents:
90
diff
changeset
|
191 |
name = output.GetName() |
144 | 192 |
if output != connector and name == "OUT" and not IsEndType(output.GetType()): |
99
2b18a72dcaf0
Added support for standard functions type compatibility check
lbessard
parents:
90
diff
changeset
|
193 |
outputtype = output.GetConnectedType() |
2b18a72dcaf0
Added support for standard functions type compatibility check
lbessard
parents:
90
diff
changeset
|
194 |
if resulttype is None or outputtype is not None and IsOfType(outputtype, resulttype): |
2b18a72dcaf0
Added support for standard functions type compatibility check
lbessard
parents:
90
diff
changeset
|
195 |
resulttype = outputtype |
2b18a72dcaf0
Added support for standard functions type compatibility check
lbessard
parents:
90
diff
changeset
|
196 |
return resulttype |
2b18a72dcaf0
Added support for standard functions type compatibility check
lbessard
parents:
90
diff
changeset
|
197 |
|
145 | 198 |
# Returns all the block connectors |
0 | 199 |
def GetConnectors(self): |
200 |
return {"inputs" : self.Inputs, "outputs" : self.Outputs} |
|
201 |
||
202 |
# 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
|
203 |
def TestConnector(self, pt, exclude = True): |
0 | 204 |
# Test each input connector |
205 |
for input in self.Inputs: |
|
206 |
if input.TestPoint(pt, exclude): |
|
207 |
return input |
|
208 |
# Test each output connector |
|
209 |
for output in self.Outputs: |
|
210 |
if output.TestPoint(pt, exclude): |
|
211 |
return output |
|
212 |
return None |
|
213 |
||
2 | 214 |
# Changes the block type |
90
2245e8776086
Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents:
64
diff
changeset
|
215 |
def SetType(self, type, extension, inputs = None, connectors = {}): |
27 | 216 |
if type != self.Type or self.Extension != extension: |
42 | 217 |
if type != self.Type: |
218 |
self.Type = type |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
219 |
dc = wx.ClientDC(self.Parent) |
42 | 220 |
self.TypeSize = dc.GetTextExtent(self.Type) |
27 | 221 |
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
|
222 |
# 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
|
223 |
# inputs and outputs |
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
224 |
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
|
225 |
if blocktype: |
90
2245e8776086
Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents:
64
diff
changeset
|
226 |
self.Colour = wx.BLACK |
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
227 |
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
|
228 |
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
|
229 |
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
|
230 |
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
|
231 |
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
|
232 |
start += 1 |
27 | 233 |
inputs.append(("IN%d"%start, inputs[-1][1], inputs[-1][2])) |
16 | 234 |
else: |
90
2245e8776086
Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents:
64
diff
changeset
|
235 |
self.Colour = wx.RED |
2245e8776086
Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents:
64
diff
changeset
|
236 |
if "inputs" in connectors: |
2245e8776086
Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents:
64
diff
changeset
|
237 |
inputs = connectors["inputs"] |
2245e8776086
Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents:
64
diff
changeset
|
238 |
else: |
2245e8776086
Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents:
64
diff
changeset
|
239 |
inputs = [] |
2245e8776086
Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents:
64
diff
changeset
|
240 |
if "outputs" in connectors: |
2245e8776086
Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents:
64
diff
changeset
|
241 |
outputs = connectors["outputs"] |
2245e8776086
Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents:
64
diff
changeset
|
242 |
else: |
2245e8776086
Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents:
64
diff
changeset
|
243 |
outputs = [] |
2245e8776086
Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents:
64
diff
changeset
|
244 |
self.Pen = wx.Pen(self.Colour) |
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
245 |
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
|
246 |
# 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
|
247 |
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
|
248 |
for input_name, input_type, input_modifier in inputs: |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
249 |
connector = Connector(self, input_name, input_type, wx.Point(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
|
250 |
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
|
251 |
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
|
252 |
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
|
253 |
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
|
254 |
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
|
255 |
# 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
|
256 |
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
|
257 |
for output_name, output_type, output_modifier in outputs: |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
258 |
connector = Connector(self, output_name, output_type, wx.Point(0, 0), EAST) |
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
259 |
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
|
260 |
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
|
261 |
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
|
262 |
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
|
263 |
self.Outputs.append(connector) |
42 | 264 |
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
|
265 |
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
|
266 |
self.RefreshBoundingBox() |
2 | 267 |
|
0 | 268 |
# Returns the block type |
269 |
def GetType(self): |
|
270 |
return self.Type |
|
271 |
||
272 |
# Changes the block name |
|
273 |
def SetName(self, name): |
|
274 |
self.Name = name |
|
42 | 275 |
self.RefreshNameSize() |
0 | 276 |
|
277 |
# Returs the block name |
|
278 |
def GetName(self): |
|
279 |
return self.Name |
|
280 |
||
2 | 281 |
# Changes the extension name |
282 |
def SetExtension(self, extension): |
|
283 |
self.Extension = extension |
|
284 |
||
285 |
# Returs the extension name |
|
286 |
def GetExtension(self): |
|
287 |
return self.Extension |
|
288 |
||
118
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
289 |
# Changes the execution order |
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
290 |
def SetExecutionOrder(self, executionOrder): |
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
291 |
self.ExecutionOrder = executionOrder |
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
292 |
self.RefreshExecutionOrderSize() |
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
293 |
|
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
294 |
# Returs the execution order |
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
295 |
def GetExecutionOrder(self): |
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
296 |
return self.ExecutionOrder |
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
297 |
|
42 | 298 |
# Refresh the block minimum size |
299 |
def RefreshMinSize(self): |
|
0 | 300 |
# Calculate the inputs maximum width |
301 |
max_input = 0 |
|
302 |
for input in self.Inputs: |
|
42 | 303 |
w, h = input.GetNameSize() |
0 | 304 |
max_input = max(max_input, w) |
305 |
# Calculate the outputs maximum width |
|
306 |
max_output = 0 |
|
307 |
for output in self.Outputs: |
|
42 | 308 |
w, h = output.GetNameSize() |
0 | 309 |
max_output = max(max_output, w) |
42 | 310 |
width = max(self.TypeSize[0] + 10, max_input + max_output + 15) |
0 | 311 |
height = (max(len(self.Inputs), len(self.Outputs)) + 1) * BLOCK_LINE_SIZE |
42 | 312 |
self.MinSize = width, height |
313 |
||
314 |
# Returns the block minimum size |
|
315 |
def GetMinSize(self): |
|
316 |
return self.MinSize |
|
0 | 317 |
|
318 |
# Changes the negated property of the connector handled |
|
319 |
def SetConnectorNegated(self, negated): |
|
320 |
handle_type, handle = self.Handle |
|
321 |
if handle_type == HANDLE_CONNECTOR: |
|
322 |
handle.SetNegated(negated) |
|
323 |
self.RefreshModel(False) |
|
324 |
||
325 |
# Changes the edge property of the connector handled |
|
326 |
def SetConnectorEdge(self, edge): |
|
327 |
handle_type, handle = self.Handle |
|
328 |
if handle_type == HANDLE_CONNECTOR: |
|
329 |
handle.SetEdge(edge) |
|
330 |
self.RefreshModel(False) |
|
331 |
||
2 | 332 |
# Method called when a LeftDClick event have been generated |
27 | 333 |
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
|
334 |
# Edit the block properties |
2 | 335 |
self.Parent.EditBlockContent(self) |
336 |
||
0 | 337 |
# Method called when a RightUp event have been generated |
27 | 338 |
def OnRightUp(self, event, dc, scaling): |
339 |
pos = GetScaledEventPosition(event, dc, scaling) |
|
0 | 340 |
# Popup the menu with special items for a block and a connector if one is handled |
341 |
connector = self.TestConnector(pos, False) |
|
342 |
if connector: |
|
343 |
self.Handle = (HANDLE_CONNECTOR, connector) |
|
344 |
self.Parent.PopupBlockMenu(connector) |
|
345 |
else: |
|
346 |
self.Parent.PopupBlockMenu() |
|
347 |
||
348 |
# Refreshes the block model |
|
349 |
def RefreshModel(self, move=True): |
|
350 |
self.Parent.RefreshBlockModel(self) |
|
351 |
# If block has moved, refresh the model of wires connected to outputs |
|
352 |
if move: |
|
353 |
for output in self.Outputs: |
|
354 |
output.RefreshWires() |
|
355 |
||
356 |
# Draws block |
|
357 |
def Draw(self, dc): |
|
144 | 358 |
Graphic_Element.Draw(self, dc) |
90
2245e8776086
Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents:
64
diff
changeset
|
359 |
dc.SetPen(self.Pen) |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
360 |
dc.SetBrush(wx.WHITE_BRUSH) |
90
2245e8776086
Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents:
64
diff
changeset
|
361 |
dc.SetTextForeground(self.Colour) |
0 | 362 |
# Draw a rectangle with the block size |
363 |
dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1) |
|
364 |
# Draw block name and block type |
|
42 | 365 |
dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - self.NameSize[0]) / 2, |
366 |
self.Pos.y - (self.NameSize[1] + 2)) |
|
367 |
dc.DrawText(self.Type, self.Pos.x + (self.Size[0] - self.TypeSize[0]) / 2, |
|
0 | 368 |
self.Pos.y + 5) |
369 |
# Draw inputs and outputs connectors |
|
370 |
for input in self.Inputs: |
|
371 |
input.Draw(dc) |
|
372 |
for output in self.Outputs: |
|
373 |
output.Draw(dc) |
|
118
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
374 |
if self.ExecutionOrder != 0: |
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
375 |
# Draw block execution order |
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
376 |
dc.DrawText(str(self.ExecutionOrder), self.Pos.x + self.Size[0] - self.ExecutionOrderSize[0], |
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
377 |
self.Pos.y + self.Size[1] + 2) |
140
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
118
diff
changeset
|
378 |
dc.SetTextForeground(wx.BLACK) |
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
118
diff
changeset
|
379 |
|
0 | 380 |
|
381 |
#------------------------------------------------------------------------------- |
|
382 |
# Function Block Diagram Variable |
|
383 |
#------------------------------------------------------------------------------- |
|
384 |
||
385 |
""" |
|
386 |
Class that implements the graphic representation of a variable |
|
387 |
""" |
|
388 |
||
389 |
class FBD_Variable(Graphic_Element): |
|
390 |
||
391 |
# Create a new variable |
|
118
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
392 |
def __init__(self, parent, type, name, value_type, id = None, executionOrder = 0): |
0 | 393 |
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
|
394 |
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
|
395 |
self.ValueType = None |
0 | 396 |
self.Id = id |
118
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
397 |
self.SetName(name) |
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
398 |
self.SetExecutionOrder(executionOrder) |
0 | 399 |
self.Input = None |
400 |
self.Output = None |
|
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
401 |
self.SetType(type, value_type) |
0 | 402 |
|
112 | 403 |
# Make a clone of this FBD_Variable |
404 |
def Clone(self, id = None, pos = None): |
|
405 |
variable = FBD_Variable(self.Parent, self.Type, self.Name, self.ValueType, id) |
|
406 |
variable.SetSize(self.Size[0], self.Size[1]) |
|
407 |
if pos is not None: |
|
408 |
variable.SetPosition(pos.x, pos.y) |
|
144 | 409 |
if self.Input: |
410 |
variable.Input = self.Input.Clone(variable) |
|
411 |
if self.Output: |
|
412 |
variable.Output = self.Output.Clone(variable) |
|
112 | 413 |
return variable |
414 |
||
0 | 415 |
# Destructor |
416 |
def __del__(self): |
|
417 |
self.Input = None |
|
418 |
self.Output = None |
|
419 |
||
144 | 420 |
# Returns the RedrawRect |
421 |
def GetRedrawRect(self, movex = 0, movey = 0): |
|
422 |
rect = Graphic_Element.GetRedrawRect(self, movex, movey) |
|
423 |
if movex != 0 or movey != 0: |
|
424 |
if self.Input and self.Input.IsConnected(): |
|
425 |
rect = rect.Union(self.Input.GetConnectedRedrawRect(movex, movey)) |
|
426 |
if self.Output and self.Output.IsConnected(): |
|
427 |
rect = rect.Union(self.Output.GetConnectedRedrawRect(movex, movey)) |
|
428 |
return rect |
|
429 |
||
0 | 430 |
# Unconnect connector |
431 |
def Clean(self): |
|
432 |
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
|
433 |
self.Input.UnConnect(delete = True) |
0 | 434 |
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
|
435 |
self.Output.UnConnect(delete = True) |
0 | 436 |
|
437 |
# Delete this variable by calling the appropriate method |
|
438 |
def Delete(self): |
|
439 |
self.Parent.DeleteVariable(self) |
|
440 |
||
42 | 441 |
# Refresh the size of text for name |
442 |
def RefreshNameSize(self): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
443 |
dc = wx.ClientDC(self.Parent) |
42 | 444 |
self.NameSize = dc.GetTextExtent(self.Name) |
445 |
||
118
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
446 |
# Refresh the size of text for execution order |
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
447 |
def RefreshExecutionOrderSize(self): |
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
448 |
dc = wx.ClientDC(self.Parent) |
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
449 |
self.ExecutionOrderSize = dc.GetTextExtent(str(self.ExecutionOrder)) |
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
450 |
|
0 | 451 |
# Refresh the variable bounding box |
452 |
def RefreshBoundingBox(self): |
|
453 |
if self.Type in (OUTPUT, INOUT): |
|
454 |
bbx_x = self.Pos.x - CONNECTOR_SIZE |
|
455 |
else: |
|
456 |
bbx_x = self.Pos.x |
|
457 |
if self.Type == INOUT: |
|
458 |
bbx_width = self.Size[0] + 2 * CONNECTOR_SIZE |
|
459 |
else: |
|
460 |
bbx_width = self.Size[0] + CONNECTOR_SIZE |
|
118
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
461 |
bbx_height = self.Size[1] |
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
462 |
if self.ExecutionOrder != 0: |
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
463 |
bbx_x = min(bbx_x, self.Pos.x + self.Size[0] - self.ExecutionOrderSize[0]) |
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
464 |
bbx_width = max(bbx_width, bbx_width + self.Pos.x + self.ExecutionOrderSize[0] - bbx_x - self.Size[0]) |
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
465 |
bbx_height = bbx_height + (self.ExecutionOrderSize[1] + 2) |
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
466 |
self.BoundingBox = wx.Rect(bbx_x, self.Pos.y, bbx_width + 1, bbx_height + 1) |
0 | 467 |
|
468 |
# Refresh the position of the variable connector |
|
469 |
def RefreshConnectors(self): |
|
145 | 470 |
scaling = self.Parent.GetScaling() |
471 |
if scaling is not None: |
|
472 |
position = round(float(self.Pos.y + self.Size[1] / 2) / float(scaling[1])) * scaling[1] - self.Pos.y |
|
473 |
else: |
|
474 |
position = self.Size[1] / 2 |
|
0 | 475 |
if self.Input: |
145 | 476 |
self.Input.SetPosition(wx.Point(0, position)) |
0 | 477 |
if self.Output: |
145 | 478 |
self.Output.SetPosition(wx.Point(self.Size[0], position)) |
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
479 |
self.RefreshConnected() |
0 | 480 |
|
481 |
# Refresh the position of wires connected to connector |
|
482 |
def RefreshConnected(self, exclude = []): |
|
483 |
if self.Input: |
|
484 |
self.Input.MoveConnected(exclude) |
|
485 |
if self.Output: |
|
486 |
self.Output.MoveConnected(exclude) |
|
487 |
||
488 |
# Test if point given is on the variable connector |
|
489 |
def TestConnector(self, pt, exclude=True): |
|
490 |
if self.Input and self.Input.TestPoint(pt, exclude): |
|
491 |
return self.Input |
|
492 |
if self.Output and self.Output.TestPoint(pt, exclude): |
|
493 |
return self.Output |
|
494 |
return None |
|
495 |
||
496 |
# Returns the block connector that starts with the point given if it exists |
|
27 | 497 |
def GetConnector(self, position, name = None): |
498 |
# if a name is given |
|
499 |
if name: |
|
500 |
# Test input and output connector if they exists |
|
501 |
if self.Input and name == self.Input.GetName(): |
|
502 |
return self.Input |
|
503 |
if self.Output and name == self.Output.GetName(): |
|
504 |
return self.Output |
|
0 | 505 |
# Test input connector if it exists |
506 |
if self.Input: |
|
507 |
input_pos = self.Input.GetRelPosition() |
|
508 |
if position.x == self.Pos.x + input_pos.x and position.y == self.Pos.y + input_pos.y: |
|
509 |
return self.Input |
|
510 |
# Test output connector if it exists |
|
511 |
if self.Output: |
|
512 |
output_pos = self.Output.GetRelPosition() |
|
513 |
if position.x == self.Pos.x + output_pos.x and position.y == self.Pos.y + output_pos.y: |
|
514 |
return self.Output |
|
515 |
return None |
|
516 |
||
517 |
# Returns all the block connectors |
|
518 |
def GetConnectors(self): |
|
519 |
return {"input" : self.Input, "output" : self.Output} |
|
520 |
||
521 |
# Changes the negated property of the variable connector if handled |
|
522 |
def SetConnectorNegated(self, negated): |
|
523 |
handle_type, handle = self.Handle |
|
524 |
if handle_type == HANDLE_CONNECTOR: |
|
525 |
handle.SetNegated(negated) |
|
526 |
self.RefreshModel(False) |
|
527 |
||
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
528 |
# 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
|
529 |
def SetType(self, type, value_type): |
32
cf9efccff009
FBD_Variable don't remove wire when just expression changed
lbessard
parents:
28
diff
changeset
|
530 |
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
|
531 |
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
|
532 |
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
|
533 |
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
|
534 |
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
|
535 |
# 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
|
536 |
if self.Type != INPUT: |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
537 |
self.Input = Connector(self, "", value_type, wx.Point(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
|
538 |
if self.Type != OUTPUT: |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
539 |
self.Output = Connector(self, "", value_type, wx.Point(0, 0), EAST) |
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
540 |
self.RefreshConnectors() |
32
cf9efccff009
FBD_Variable don't remove wire when just expression changed
lbessard
parents:
28
diff
changeset
|
541 |
elif value_type != self.ValueType: |
cf9efccff009
FBD_Variable don't remove wire when just expression changed
lbessard
parents:
28
diff
changeset
|
542 |
if self.Input: |
cf9efccff009
FBD_Variable don't remove wire when just expression changed
lbessard
parents:
28
diff
changeset
|
543 |
self.Input.SetType(value_type) |
cf9efccff009
FBD_Variable don't remove wire when just expression changed
lbessard
parents:
28
diff
changeset
|
544 |
if self.Output: |
cf9efccff009
FBD_Variable don't remove wire when just expression changed
lbessard
parents:
28
diff
changeset
|
545 |
self.Output.SetType(value_type) |
cf9efccff009
FBD_Variable don't remove wire when just expression changed
lbessard
parents:
28
diff
changeset
|
546 |
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
|
547 |
|
0 | 548 |
# Returns the variable type |
549 |
def GetType(self): |
|
550 |
return self.Type |
|
551 |
||
552 |
# Changes the variable name |
|
553 |
def SetName(self, name): |
|
554 |
self.Name = name |
|
42 | 555 |
self.RefreshNameSize() |
0 | 556 |
|
557 |
# Returns the variable name |
|
558 |
def GetName(self): |
|
559 |
return self.Name |
|
560 |
||
118
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
561 |
# Changes the execution order |
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
562 |
def SetExecutionOrder(self, executionOrder): |
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
563 |
self.ExecutionOrder = executionOrder |
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
564 |
self.RefreshExecutionOrderSize() |
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
565 |
|
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
566 |
# Returs the execution order |
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
567 |
def GetExecutionOrder(self): |
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
568 |
return self.ExecutionOrder |
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
569 |
|
0 | 570 |
# Returns the variable minimum size |
571 |
def GetMinSize(self): |
|
42 | 572 |
return self.NameSize[0] + 10, self.NameSize[1] + 10 |
0 | 573 |
|
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
574 |
# Method called when a LeftDClick event have been generated |
27 | 575 |
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
|
576 |
# 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
|
577 |
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
|
578 |
|
0 | 579 |
# Method called when a RightUp event have been generated |
27 | 580 |
def OnRightUp(self, event, dc, scaling): |
102
85875dcb7754
Adding edit user's POU by double click on block instance
lbessard
parents:
99
diff
changeset
|
581 |
self.Parent.PopupDefaultMenu() |
0 | 582 |
|
583 |
# Refreshes the variable model |
|
584 |
def RefreshModel(self, move=True): |
|
585 |
self.Parent.RefreshVariableModel(self) |
|
586 |
# If variable has moved and variable is not of type OUTPUT, refresh the model |
|
587 |
# of wires connected to output connector |
|
588 |
if move and self.Type != OUTPUT: |
|
589 |
if self.Output: |
|
590 |
self.Output.RefreshWires() |
|
591 |
||
592 |
# Draws variable |
|
593 |
def Draw(self, dc): |
|
144 | 594 |
Graphic_Element.Draw(self, dc) |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
595 |
dc.SetPen(wx.BLACK_PEN) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
596 |
dc.SetBrush(wx.WHITE_BRUSH) |
0 | 597 |
# Draw a rectangle with the variable size |
598 |
dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1) |
|
599 |
# Draw variable name |
|
42 | 600 |
dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - self.NameSize[0]) / 2, |
601 |
self.Pos.y + (self.Size[1] - self.NameSize[1]) / 2) |
|
0 | 602 |
# Draw connectors |
603 |
if self.Input: |
|
604 |
self.Input.Draw(dc) |
|
605 |
if self.Output: |
|
606 |
self.Output.Draw(dc) |
|
118
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
607 |
if self.ExecutionOrder != 0: |
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
608 |
# Draw variable execution order |
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
609 |
dc.DrawText(str(self.ExecutionOrder), self.Pos.x + self.Size[0] - self.ExecutionOrderSize[0], |
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
610 |
self.Pos.y + self.Size[1] + 2) |
0 | 611 |
|
612 |
||
613 |
#------------------------------------------------------------------------------- |
|
614 |
# Function Block Diagram Connector |
|
615 |
#------------------------------------------------------------------------------- |
|
616 |
||
617 |
""" |
|
618 |
Class that implements the graphic representation of a connection |
|
619 |
""" |
|
620 |
||
621 |
class FBD_Connector(Graphic_Element): |
|
622 |
||
623 |
# Create a new connection |
|
624 |
def __init__(self, parent, type, name, id = None): |
|
625 |
Graphic_Element.__init__(self, parent) |
|
626 |
self.Type = type |
|
627 |
self.Id = id |
|
118
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
628 |
self.SetName(name) |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
629 |
self.Pos = wx.Point(0, 0) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
630 |
self.Size = wx.Size(0, 0) |
0 | 631 |
# Create an input or output connector according to connection type |
632 |
if self.Type == CONNECTOR: |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
633 |
self.Connector = Connector(self, "", "ANY", wx.Point(0, 0), WEST, onlyone = True) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
634 |
else: |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
635 |
self.Connector = Connector(self, "", "ANY", wx.Point(0, 0), EAST) |
0 | 636 |
self.RefreshConnectors() |
42 | 637 |
self.RefreshNameSize() |
0 | 638 |
|
639 |
# Destructor |
|
640 |
def __del__(self): |
|
641 |
self.Connector = None |
|
642 |
||
144 | 643 |
# Returns the RedrawRect |
644 |
def GetRedrawRect(self, movex = 0, movey = 0): |
|
645 |
rect = Graphic_Element.GetRedrawRect(self, movex, movey) |
|
646 |
if movex != 0 or movey != 0: |
|
647 |
if self.Connector and self.Connector.IsConnected(): |
|
648 |
rect = rect.Union(self.Connector.GetConnectedRedrawRect(movex, movey)) |
|
649 |
return rect |
|
650 |
||
112 | 651 |
# Make a clone of this FBD_Connector |
652 |
def Clone(self, id = None, pos = None): |
|
653 |
connection = FBD_Connector(self.Parent, self.Type, self.Name, id) |
|
654 |
connection.SetSize(self.Size[0], self.Size[1]) |
|
655 |
if pos is not None: |
|
656 |
connection.SetPosition(pos.x, pos.y) |
|
657 |
connection.Connector = self.Connector.Clone(connection) |
|
658 |
return connection |
|
659 |
||
0 | 660 |
# Unconnect connector |
661 |
def Clean(self): |
|
662 |
if self.Connector: |
|
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
663 |
self.Connector.UnConnect(delete = True) |
0 | 664 |
|
665 |
# Delete this connection by calling the appropriate method |
|
666 |
def Delete(self): |
|
667 |
self.Parent.DeleteConnection(self) |
|
668 |
||
42 | 669 |
# Refresh the size of text for name |
670 |
def RefreshNameSize(self): |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
671 |
dc = wx.ClientDC(self.Parent) |
42 | 672 |
self.NameSize = dc.GetTextExtent(self.Name) |
673 |
||
0 | 674 |
# Refresh the connection bounding box |
675 |
def RefreshBoundingBox(self): |
|
676 |
if self.Type == CONNECTOR: |
|
677 |
bbx_x = self.Pos.x - CONNECTOR_SIZE |
|
678 |
else: |
|
679 |
bbx_x = self.Pos.x |
|
680 |
bbx_width = self.Size[0] + CONNECTOR_SIZE |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
681 |
self.BoundingBox = wx.Rect(bbx_x, self.Pos.y, bbx_width, self.Size[1]) |
0 | 682 |
|
683 |
# Refresh the position of the connection connector |
|
684 |
def RefreshConnectors(self): |
|
145 | 685 |
scaling = self.Parent.GetScaling() |
686 |
if scaling is not None: |
|
687 |
position = round(float(self.Pos.y + self.Size[1] / 2) / float(scaling[1])) * scaling[1] - self.Pos.y |
|
688 |
else: |
|
689 |
position = self.Size[1] / 2 |
|
0 | 690 |
if self.Type == CONNECTOR: |
145 | 691 |
self.Connector.SetPosition(wx.Point(0, position)) |
692 |
else: |
|
693 |
self.Connector.SetPosition(wx.Point(self.Size[0], position)) |
|
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
694 |
self.RefreshConnected() |
0 | 695 |
|
696 |
# Refresh the position of wires connected to connector |
|
697 |
def RefreshConnected(self, exclude = []): |
|
698 |
if self.Connector: |
|
699 |
self.Connector.MoveConnected(exclude) |
|
700 |
||
701 |
# Test if point given is on the connection connector |
|
702 |
def TestConnector(self, pt, exclude=True): |
|
703 |
if self.Connector and self.Connector.TestPoint(pt, exclude): |
|
704 |
return self.Connector |
|
705 |
return None |
|
706 |
||
707 |
# Returns the connection connector |
|
27 | 708 |
def GetConnector(self, position = None, name = None): |
0 | 709 |
return self.Connector |
710 |
||
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
711 |
# Changes the variable type |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
712 |
def SetType(self, type): |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
713 |
if type != self.Type: |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
714 |
self.Type = type |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
715 |
self.Clean() |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
716 |
# Create an input or output connector according to connection type |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
717 |
if self.Type == CONNECTOR: |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
718 |
self.Connector = Connector(self, "", "ANY", wx.Point(0, 0), WEST, onlyone = True) |
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
719 |
else: |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
720 |
self.Connector = Connector(self, "", "ANY", wx.Point(0, 0), EAST) |
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
721 |
self.RefreshConnectors() |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
722 |
|
0 | 723 |
# Returns the connection type |
724 |
def GetType(self): |
|
725 |
return self.Type |
|
726 |
||
145 | 727 |
def GetConnectionResultType(self, connector, connectortype): |
728 |
return connectortype |
|
729 |
||
0 | 730 |
# Changes the connection name |
731 |
def SetName(self, name): |
|
732 |
self.Name = name |
|
42 | 733 |
self.RefreshNameSize() |
0 | 734 |
|
735 |
# Returns the connection name |
|
736 |
def GetName(self): |
|
737 |
return self.Name |
|
738 |
||
739 |
# Returns the connection minimum size |
|
740 |
def GetMinSize(self): |
|
42 | 741 |
text_width, text_height = self.NameSize |
0 | 742 |
if text_height % 2 == 1: |
743 |
text_height += 1 |
|
744 |
return text_width + text_height + 20, text_height + 10 |
|
745 |
||
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
746 |
# Method called when a LeftDClick event have been generated |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
747 |
def OnLeftDClick(self, event, dc, scaling): |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
748 |
# Edit the connection properties |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
749 |
self.Parent.EditConnectionContent(self) |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
750 |
|
0 | 751 |
# Method called when a RightUp event have been generated |
27 | 752 |
def OnRightUp(self, event, dc, scaling): |
0 | 753 |
# Popup the default menu |
754 |
self.Parent.PopupDefaultMenu() |
|
755 |
||
756 |
# Refreshes the connection model |
|
757 |
def RefreshModel(self, move=True): |
|
758 |
self.Parent.RefreshConnectionModel(self) |
|
759 |
# If connection has moved and connection is of type CONTINUATION, refresh |
|
760 |
# the model of wires connected to connector |
|
761 |
if move and self.Type == CONTINUATION: |
|
762 |
if self.Connector: |
|
763 |
self.Connector.RefreshWires() |
|
764 |
||
765 |
# Draws connection |
|
766 |
def Draw(self, dc): |
|
144 | 767 |
Graphic_Element.Draw(self, dc) |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
768 |
dc.SetPen(wx.BLACK_PEN) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
769 |
dc.SetBrush(wx.WHITE_BRUSH) |
0 | 770 |
# Draw a rectangle with the connection size with arrows in |
771 |
dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1) |
|
42 | 772 |
arrowsize = min(self.Size[1] / 2, (self.Size[0] - self.NameSize[0] - 10) / 2) |
0 | 773 |
dc.DrawLine(self.Pos.x, self.Pos.y, self.Pos.x + arrowsize, |
774 |
self.Pos.y + self.Size[1] / 2) |
|
775 |
dc.DrawLine(self.Pos.x + arrowsize, self.Pos.y + self.Size[1] / 2, |
|
776 |
self.Pos.x, self.Pos.y + self.Size[1]) |
|
777 |
dc.DrawLine(self.Pos.x + self.Size[0] - arrowsize, self.Pos.y, |
|
778 |
self.Pos.x + self.Size[0], self.Pos.y + self.Size[1] / 2) |
|
779 |
dc.DrawLine(self.Pos.x + self.Size[0], self.Pos.y + self.Size[1] / 2, |
|
780 |
self.Pos.x + self.Size[0] - arrowsize, self.Pos.y + self.Size[1]) |
|
118
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
781 |
# Draw connection name |
42 | 782 |
dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - self.NameSize[0]) / 2, |
783 |
self.Pos.y + (self.Size[1] - self.NameSize[1]) / 2) |
|
0 | 784 |
# Draw connector |
785 |
if self.Connector: |
|
786 |
self.Connector.Draw(dc) |
|
140
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
118
diff
changeset
|
787 |