author | lbessard |
Sun, 07 Sep 2008 19:21:39 +0200 | |
changeset 254 | 27abdcf9a460 |
parent 249 | d8425712acef |
child 269 | 34eff05909b0 |
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 * |
|
29 |
||
30 |
#------------------------------------------------------------------------------- |
|
31 |
# Function Block Diagram Block |
|
32 |
#------------------------------------------------------------------------------- |
|
33 |
||
34 |
""" |
|
35 |
Class that implements the graphic representation of a function block |
|
36 |
""" |
|
37 |
||
38 |
class FBD_Block(Graphic_Element): |
|
39 |
||
40 |
# 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
|
41 |
def __init__(self, parent, type, name, id = None, extension = 0, inputs = None, connectors = {}, executionOrder = 0): |
0 | 42 |
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
|
43 |
self.Type = None |
27 | 44 |
self.Extension = None |
0 | 45 |
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
|
46 |
self.SetName(name) |
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
47 |
self.SetExecutionOrder(executionOrder) |
2 | 48 |
self.Inputs = [] |
49 |
self.Outputs = [] |
|
90
2245e8776086
Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents:
64
diff
changeset
|
50 |
self.Colour = wx.BLACK |
2245e8776086
Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents:
64
diff
changeset
|
51 |
self.Pen = wx.BLACK_PEN |
2245e8776086
Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents:
64
diff
changeset
|
52 |
self.SetType(type, extension, inputs, connectors) |
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
53 |
self.Errors = {} |
0 | 54 |
|
112 | 55 |
# Make a clone of this FBD_Block |
162 | 56 |
def Clone(self, parent, id = None, name = "", pos = None): |
144 | 57 |
if self.Name != "" and name == "": |
58 |
name = self.Name |
|
162 | 59 |
block = FBD_Block(parent, self.Type, name, id, self.Extension) |
112 | 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 |
||
249 | 67 |
def Flush(self): |
68 |
for input in self.Inputs: |
|
69 |
input.Flush() |
|
0 | 70 |
self.Inputs = [] |
249 | 71 |
for output in self.Outputs: |
72 |
output.Flush() |
|
0 | 73 |
self.Outputs = [] |
74 |
||
144 | 75 |
# Returns the RedrawRect |
76 |
def GetRedrawRect(self, movex = 0, movey = 0): |
|
77 |
rect = Graphic_Element.GetRedrawRect(self, movex, movey) |
|
78 |
if movex != 0 or movey != 0: |
|
79 |
for input in self.Inputs: |
|
80 |
if input.IsConnected(): |
|
81 |
rect = rect.Union(input.GetConnectedRedrawRect(movex, movey)) |
|
82 |
for output in self.Outputs: |
|
83 |
if output.IsConnected(): |
|
84 |
rect = rect.Union(output.GetConnectedRedrawRect(movex, movey)) |
|
85 |
return rect |
|
86 |
||
0 | 87 |
# Delete this block by calling the appropriate method |
88 |
def Delete(self): |
|
89 |
self.Parent.DeleteBlock(self) |
|
90 |
||
91 |
# Unconnect all inputs and outputs |
|
92 |
def Clean(self): |
|
93 |
for input in self.Inputs: |
|
2 | 94 |
input.UnConnect(delete = True) |
95 |
for output in self.Outputs: |
|
96 |
output.UnConnect(delete = True) |
|
0 | 97 |
|
42 | 98 |
# Refresh the size of text for name |
99 |
def RefreshNameSize(self): |
|
165 | 100 |
self.NameSize = self.Parent.GetTextExtent(self.Name) |
42 | 101 |
|
118
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
102 |
# 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
|
103 |
def RefreshExecutionOrderSize(self): |
165 | 104 |
self.ExecutionOrderSize = self.Parent.GetTextExtent(str(self.ExecutionOrder)) |
118
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
105 |
|
0 | 106 |
# Refresh the block bounding box |
107 |
def RefreshBoundingBox(self): |
|
108 |
# Calculate the size of the name outside the block |
|
42 | 109 |
text_width, text_height = self.NameSize |
0 | 110 |
# Calculate the bounding box size |
111 |
bbx_x = self.Pos.x - max(min(1, len(self.Inputs)) * CONNECTOR_SIZE, (text_width - self.Size[0]) / 2) |
|
112 |
bbx_width = self.Size[0] + 1 + (min(1, len(self.Inputs)) + min(1, len(self.Outputs))) * CONNECTOR_SIZE |
|
113 |
if self.Name != "": |
|
114 |
bbx_y = self.Pos.y - (text_height + 2) |
|
115 |
bbx_height = self.Size[1] + (text_height + 2) |
|
116 |
else: |
|
117 |
bbx_y = self.Pos.y |
|
118 |
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
|
119 |
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
|
120 |
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
|
121 |
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
|
122 |
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
|
123 |
self.BoundingBox = wx.Rect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1) |
0 | 124 |
|
125 |
# Refresh the positions of the block connectors |
|
126 |
def RefreshConnectors(self): |
|
145 | 127 |
scaling = self.Parent.GetScaling() |
0 | 128 |
# Calculate the size for the connector lines |
129 |
lines = max(len(self.Inputs), len(self.Outputs)) |
|
103 | 130 |
if lines > 0: |
131 |
linesize = max((self.Size[1] - BLOCK_LINE_SIZE) / lines, BLOCK_LINE_SIZE) |
|
145 | 132 |
# Update inputs and outputs positions |
103 | 133 |
position = BLOCK_LINE_SIZE + linesize / 2 |
145 | 134 |
for i in xrange(lines): |
135 |
if scaling is not None: |
|
136 |
ypos = round(float(self.Pos.y + position) / float(scaling[1])) * scaling[1] - self.Pos.y |
|
137 |
else: |
|
138 |
ypos = position |
|
139 |
if i < len(self.Inputs): |
|
140 |
self.Inputs[i].SetPosition(wx.Point(0, ypos)) |
|
141 |
if i < len(self.Outputs): |
|
142 |
self.Outputs[i].SetPosition(wx.Point(self.Size[0], ypos)) |
|
103 | 143 |
position += linesize |
0 | 144 |
self.RefreshConnected() |
145 |
||
146 |
# Refresh the positions of wires connected to inputs and outputs |
|
147 |
def RefreshConnected(self, exclude = []): |
|
148 |
for input in self.Inputs: |
|
149 |
input.MoveConnected(exclude) |
|
150 |
for output in self.Outputs: |
|
151 |
output.MoveConnected(exclude) |
|
152 |
||
153 |
# Returns the block connector that starts with the point given if it exists |
|
27 | 154 |
def GetConnector(self, position, name = None): |
155 |
# if a name is given |
|
156 |
if name: |
|
157 |
# Test each input and output connector |
|
158 |
for input in self.Inputs: |
|
159 |
if name == input.GetName(): |
|
160 |
return input |
|
161 |
for output in self.Outputs: |
|
162 |
if name == output.GetName(): |
|
163 |
return output |
|
0 | 164 |
# Test each input connector |
165 |
for input in self.Inputs: |
|
166 |
input_pos = input.GetRelPosition() |
|
167 |
if position.x == self.Pos.x + input_pos.x and position.y == self.Pos.y + input_pos.y: |
|
168 |
return input |
|
169 |
# Test each output connector |
|
170 |
for output in self.Outputs: |
|
171 |
output_pos = output.GetRelPosition() |
|
172 |
if position.x == self.Pos.x + output_pos.x and position.y == self.Pos.y + output_pos.y: |
|
173 |
return output |
|
174 |
return None |
|
175 |
||
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
176 |
def GetInputTypes(self): |
112 | 177 |
return tuple([input.GetType(True) for input in self.Inputs]) |
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
178 |
|
102
85875dcb7754
Adding edit user's POU by double click on block instance
lbessard
parents:
99
diff
changeset
|
179 |
def SetOutputValues(self, values): |
85875dcb7754
Adding edit user's POU by double click on block instance
lbessard
parents:
99
diff
changeset
|
180 |
for output in self.Outputs: |
85875dcb7754
Adding edit user's POU by double click on block instance
lbessard
parents:
99
diff
changeset
|
181 |
output.SetValue(values.get(ouput.getName(), None)) |
85875dcb7754
Adding edit user's POU by double click on block instance
lbessard
parents:
99
diff
changeset
|
182 |
|
85875dcb7754
Adding edit user's POU by double click on block instance
lbessard
parents:
99
diff
changeset
|
183 |
def GetConnectionResultType(self, connector, connectortype): |
85875dcb7754
Adding edit user's POU by double click on block instance
lbessard
parents:
99
diff
changeset
|
184 |
resulttype = connectortype |
99
2b18a72dcaf0
Added support for standard functions type compatibility check
lbessard
parents:
90
diff
changeset
|
185 |
for input in self.Inputs: |
2b18a72dcaf0
Added support for standard functions type compatibility check
lbessard
parents:
90
diff
changeset
|
186 |
name = input.GetName() |
2b18a72dcaf0
Added support for standard functions type compatibility check
lbessard
parents:
90
diff
changeset
|
187 |
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
|
188 |
inputtype = input.GetConnectedType() |
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
189 |
if resulttype is None or inputtype is not None and self.IsOfType(inputtype, resulttype): |
99
2b18a72dcaf0
Added support for standard functions type compatibility check
lbessard
parents:
90
diff
changeset
|
190 |
resulttype = inputtype |
2b18a72dcaf0
Added support for standard functions type compatibility check
lbessard
parents:
90
diff
changeset
|
191 |
for output in self.Outputs: |
2b18a72dcaf0
Added support for standard functions type compatibility check
lbessard
parents:
90
diff
changeset
|
192 |
name = output.GetName() |
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
193 |
if output != connector and name == "OUT" and not self.IsEndType(output.GetType()): |
99
2b18a72dcaf0
Added support for standard functions type compatibility check
lbessard
parents:
90
diff
changeset
|
194 |
outputtype = output.GetConnectedType() |
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
195 |
if resulttype is None or outputtype is not None and self.IsOfType(outputtype, resulttype): |
99
2b18a72dcaf0
Added support for standard functions type compatibility check
lbessard
parents:
90
diff
changeset
|
196 |
resulttype = outputtype |
2b18a72dcaf0
Added support for standard functions type compatibility check
lbessard
parents:
90
diff
changeset
|
197 |
return resulttype |
2b18a72dcaf0
Added support for standard functions type compatibility check
lbessard
parents:
90
diff
changeset
|
198 |
|
145 | 199 |
# Returns all the block connectors |
0 | 200 |
def GetConnectors(self): |
201 |
return {"inputs" : self.Inputs, "outputs" : self.Outputs} |
|
202 |
||
203 |
# Test if point given is on one of the block connectors |
|
243
c5da8b706cde
Adding support for allowing connections only between an input and an output connector
lbessard
parents:
231
diff
changeset
|
204 |
def TestConnector(self, pt, direction = None, exclude = True): |
0 | 205 |
# Test each input connector |
206 |
for input in self.Inputs: |
|
243
c5da8b706cde
Adding support for allowing connections only between an input and an output connector
lbessard
parents:
231
diff
changeset
|
207 |
if input.TestPoint(pt, direction, exclude): |
0 | 208 |
return input |
209 |
# Test each output connector |
|
210 |
for output in self.Outputs: |
|
243
c5da8b706cde
Adding support for allowing connections only between an input and an output connector
lbessard
parents:
231
diff
changeset
|
211 |
if output.TestPoint(pt, direction, exclude): |
0 | 212 |
return output |
213 |
return None |
|
214 |
||
2 | 215 |
# Changes the block type |
90
2245e8776086
Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents:
64
diff
changeset
|
216 |
def SetType(self, type, extension, inputs = None, connectors = {}): |
27 | 217 |
if type != self.Type or self.Extension != extension: |
42 | 218 |
if type != self.Type: |
219 |
self.Type = type |
|
165 | 220 |
self.TypeSize = self.Parent.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 |
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
224 |
blocktype = self.Parent.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 |
243
c5da8b706cde
Adding support for allowing connections only between an input and an output connector
lbessard
parents:
231
diff
changeset
|
341 |
connector = self.TestConnector(pos, exclude=False) |
0 | 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 |
||
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
356 |
def AddError(self, infos, start, end): |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
357 |
if infos[0] in ["type", "name"] and start[0] == 0 and end[0] == 0: |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
358 |
self.Errors[infos[0]] = (start, end) |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
359 |
elif infos[0] == "input" and infos[1] < len(self.Inputs): |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
360 |
self.Inputs[infos[1]].AddError(infos[2:], start, end) |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
361 |
elif infos[0] == "output" and infos[1] < len(self.Outputs): |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
362 |
self.Outputs[infos[1]].AddError(infos[2:], start, end) |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
363 |
|
0 | 364 |
# Draws block |
365 |
def Draw(self, dc): |
|
144 | 366 |
Graphic_Element.Draw(self, dc) |
90
2245e8776086
Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents:
64
diff
changeset
|
367 |
dc.SetPen(self.Pen) |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
368 |
dc.SetBrush(wx.WHITE_BRUSH) |
90
2245e8776086
Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents:
64
diff
changeset
|
369 |
dc.SetTextForeground(self.Colour) |
213 | 370 |
|
371 |
if getattr(dc, "printing", False): |
|
372 |
name_size = dc.GetTextExtent(self.Name) |
|
373 |
type_size = dc.GetTextExtent(self.Type) |
|
374 |
executionorder_size = dc.GetTextExtent(str(self.ExecutionOrder)) |
|
375 |
else: |
|
376 |
name_size = self.NameSize |
|
377 |
type_size = self.TypeSize |
|
378 |
executionorder_size = self.ExecutionOrderSize |
|
379 |
||
0 | 380 |
# Draw a rectangle with the block size |
381 |
dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1) |
|
382 |
# Draw block name and block type |
|
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
383 |
name_pos = (self.Pos.x + (self.Size[0] - name_size[0]) / 2, |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
384 |
self.Pos.y - (name_size[1] + 2)) |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
385 |
type_pos = (self.Pos.x + (self.Size[0] - type_size[0]) / 2, |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
386 |
self.Pos.y + 5) |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
387 |
dc.DrawText(self.Name, name_pos[0], name_pos[1]) |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
388 |
dc.DrawText(self.Type, type_pos[0], type_pos[1]) |
0 | 389 |
# Draw inputs and outputs connectors |
390 |
for input in self.Inputs: |
|
391 |
input.Draw(dc) |
|
392 |
for output in self.Outputs: |
|
393 |
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
|
394 |
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
|
395 |
# Draw block execution order |
213 | 396 |
dc.DrawText(str(self.ExecutionOrder), self.Pos.x + self.Size[0] - executionorder_size[0], |
118
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
397 |
self.Pos.y + self.Size[1] + 2) |
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
398 |
if "name" in self.Errors: |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
399 |
HighlightErrorZone(dc, name_pos[0], name_pos[1], name_size[0], name_size[1]) |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
400 |
if "type" in self.Errors: |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
401 |
HighlightErrorZone(dc, type_pos[0], type_pos[1], type_size[0], type_size[1]) |
140
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
118
diff
changeset
|
402 |
dc.SetTextForeground(wx.BLACK) |
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
118
diff
changeset
|
403 |
|
0 | 404 |
|
405 |
#------------------------------------------------------------------------------- |
|
406 |
# Function Block Diagram Variable |
|
407 |
#------------------------------------------------------------------------------- |
|
408 |
||
409 |
""" |
|
410 |
Class that implements the graphic representation of a variable |
|
411 |
""" |
|
412 |
||
413 |
class FBD_Variable(Graphic_Element): |
|
414 |
||
415 |
# 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
|
416 |
def __init__(self, parent, type, name, value_type, id = None, executionOrder = 0): |
0 | 417 |
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
|
418 |
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
|
419 |
self.ValueType = None |
0 | 420 |
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
|
421 |
self.SetName(name) |
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
422 |
self.SetExecutionOrder(executionOrder) |
0 | 423 |
self.Input = None |
424 |
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
|
425 |
self.SetType(type, value_type) |
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
426 |
self.Errors = [] |
0 | 427 |
|
112 | 428 |
# Make a clone of this FBD_Variable |
162 | 429 |
def Clone(self, parent, id = None, pos = None): |
430 |
variable = FBD_Variable(parent, self.Type, self.Name, self.ValueType, id) |
|
112 | 431 |
variable.SetSize(self.Size[0], self.Size[1]) |
432 |
if pos is not None: |
|
433 |
variable.SetPosition(pos.x, pos.y) |
|
144 | 434 |
if self.Input: |
435 |
variable.Input = self.Input.Clone(variable) |
|
436 |
if self.Output: |
|
437 |
variable.Output = self.Output.Clone(variable) |
|
112 | 438 |
return variable |
439 |
||
249 | 440 |
def Flush(self): |
441 |
if self.Input is not None: |
|
442 |
self.Input.Flush() |
|
443 |
self.Input = None |
|
444 |
if self.Output is not None: |
|
445 |
self.Output.Flush() |
|
446 |
self.Output = None |
|
0 | 447 |
|
144 | 448 |
# Returns the RedrawRect |
449 |
def GetRedrawRect(self, movex = 0, movey = 0): |
|
450 |
rect = Graphic_Element.GetRedrawRect(self, movex, movey) |
|
451 |
if movex != 0 or movey != 0: |
|
452 |
if self.Input and self.Input.IsConnected(): |
|
453 |
rect = rect.Union(self.Input.GetConnectedRedrawRect(movex, movey)) |
|
454 |
if self.Output and self.Output.IsConnected(): |
|
455 |
rect = rect.Union(self.Output.GetConnectedRedrawRect(movex, movey)) |
|
456 |
return rect |
|
457 |
||
0 | 458 |
# Unconnect connector |
459 |
def Clean(self): |
|
460 |
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
|
461 |
self.Input.UnConnect(delete = True) |
0 | 462 |
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
|
463 |
self.Output.UnConnect(delete = True) |
0 | 464 |
|
465 |
# Delete this variable by calling the appropriate method |
|
466 |
def Delete(self): |
|
467 |
self.Parent.DeleteVariable(self) |
|
468 |
||
42 | 469 |
# Refresh the size of text for name |
470 |
def RefreshNameSize(self): |
|
165 | 471 |
self.NameSize = self.Parent.GetTextExtent(self.Name) |
42 | 472 |
|
118
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
473 |
# 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
|
474 |
def RefreshExecutionOrderSize(self): |
165 | 475 |
self.ExecutionOrderSize = self.Parent.GetTextExtent(str(self.ExecutionOrder)) |
118
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
476 |
|
0 | 477 |
# Refresh the variable bounding box |
478 |
def RefreshBoundingBox(self): |
|
479 |
if self.Type in (OUTPUT, INOUT): |
|
480 |
bbx_x = self.Pos.x - CONNECTOR_SIZE |
|
481 |
else: |
|
482 |
bbx_x = self.Pos.x |
|
483 |
if self.Type == INOUT: |
|
484 |
bbx_width = self.Size[0] + 2 * CONNECTOR_SIZE |
|
485 |
else: |
|
486 |
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
|
487 |
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
|
488 |
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
|
489 |
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
|
490 |
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
|
491 |
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
|
492 |
self.BoundingBox = wx.Rect(bbx_x, self.Pos.y, bbx_width + 1, bbx_height + 1) |
0 | 493 |
|
494 |
# Refresh the position of the variable connector |
|
495 |
def RefreshConnectors(self): |
|
145 | 496 |
scaling = self.Parent.GetScaling() |
497 |
if scaling is not None: |
|
498 |
position = round(float(self.Pos.y + self.Size[1] / 2) / float(scaling[1])) * scaling[1] - self.Pos.y |
|
499 |
else: |
|
500 |
position = self.Size[1] / 2 |
|
0 | 501 |
if self.Input: |
145 | 502 |
self.Input.SetPosition(wx.Point(0, position)) |
0 | 503 |
if self.Output: |
145 | 504 |
self.Output.SetPosition(wx.Point(self.Size[0], position)) |
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
505 |
self.RefreshConnected() |
0 | 506 |
|
507 |
# Refresh the position of wires connected to connector |
|
508 |
def RefreshConnected(self, exclude = []): |
|
509 |
if self.Input: |
|
510 |
self.Input.MoveConnected(exclude) |
|
511 |
if self.Output: |
|
512 |
self.Output.MoveConnected(exclude) |
|
513 |
||
514 |
# Test if point given is on the variable connector |
|
243
c5da8b706cde
Adding support for allowing connections only between an input and an output connector
lbessard
parents:
231
diff
changeset
|
515 |
def TestConnector(self, pt, direction = None, exclude=True): |
c5da8b706cde
Adding support for allowing connections only between an input and an output connector
lbessard
parents:
231
diff
changeset
|
516 |
if self.Input and self.Input.TestPoint(pt, direction, exclude): |
0 | 517 |
return self.Input |
243
c5da8b706cde
Adding support for allowing connections only between an input and an output connector
lbessard
parents:
231
diff
changeset
|
518 |
if self.Output and self.Output.TestPoint(pt, direction, exclude): |
0 | 519 |
return self.Output |
520 |
return None |
|
521 |
||
522 |
# Returns the block connector that starts with the point given if it exists |
|
27 | 523 |
def GetConnector(self, position, name = None): |
524 |
# if a name is given |
|
525 |
if name: |
|
526 |
# Test input and output connector if they exists |
|
527 |
if self.Input and name == self.Input.GetName(): |
|
528 |
return self.Input |
|
529 |
if self.Output and name == self.Output.GetName(): |
|
530 |
return self.Output |
|
0 | 531 |
# Test input connector if it exists |
532 |
if self.Input: |
|
533 |
input_pos = self.Input.GetRelPosition() |
|
534 |
if position.x == self.Pos.x + input_pos.x and position.y == self.Pos.y + input_pos.y: |
|
535 |
return self.Input |
|
536 |
# Test output connector if it exists |
|
537 |
if self.Output: |
|
538 |
output_pos = self.Output.GetRelPosition() |
|
539 |
if position.x == self.Pos.x + output_pos.x and position.y == self.Pos.y + output_pos.y: |
|
540 |
return self.Output |
|
541 |
return None |
|
542 |
||
543 |
# Returns all the block connectors |
|
544 |
def GetConnectors(self): |
|
545 |
return {"input" : self.Input, "output" : self.Output} |
|
546 |
||
547 |
# Changes the negated property of the variable connector if handled |
|
548 |
def SetConnectorNegated(self, negated): |
|
549 |
handle_type, handle = self.Handle |
|
550 |
if handle_type == HANDLE_CONNECTOR: |
|
551 |
handle.SetNegated(negated) |
|
552 |
self.RefreshModel(False) |
|
553 |
||
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
554 |
# 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
|
555 |
def SetType(self, type, value_type): |
32
cf9efccff009
FBD_Variable don't remove wire when just expression changed
lbessard
parents:
28
diff
changeset
|
556 |
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
|
557 |
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
|
558 |
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
|
559 |
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
|
560 |
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
|
561 |
# 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
|
562 |
if self.Type != INPUT: |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
563 |
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
|
564 |
if self.Type != OUTPUT: |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
565 |
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
|
566 |
self.RefreshConnectors() |
32
cf9efccff009
FBD_Variable don't remove wire when just expression changed
lbessard
parents:
28
diff
changeset
|
567 |
elif value_type != self.ValueType: |
cf9efccff009
FBD_Variable don't remove wire when just expression changed
lbessard
parents:
28
diff
changeset
|
568 |
if self.Input: |
cf9efccff009
FBD_Variable don't remove wire when just expression changed
lbessard
parents:
28
diff
changeset
|
569 |
self.Input.SetType(value_type) |
cf9efccff009
FBD_Variable don't remove wire when just expression changed
lbessard
parents:
28
diff
changeset
|
570 |
if self.Output: |
cf9efccff009
FBD_Variable don't remove wire when just expression changed
lbessard
parents:
28
diff
changeset
|
571 |
self.Output.SetType(value_type) |
cf9efccff009
FBD_Variable don't remove wire when just expression changed
lbessard
parents:
28
diff
changeset
|
572 |
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
|
573 |
|
0 | 574 |
# Returns the variable type |
575 |
def GetType(self): |
|
576 |
return self.Type |
|
577 |
||
578 |
# Changes the variable name |
|
579 |
def SetName(self, name): |
|
580 |
self.Name = name |
|
42 | 581 |
self.RefreshNameSize() |
0 | 582 |
|
583 |
# Returns the variable name |
|
584 |
def GetName(self): |
|
585 |
return self.Name |
|
586 |
||
118
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
587 |
# 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
|
588 |
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
|
589 |
self.ExecutionOrder = executionOrder |
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
590 |
self.RefreshExecutionOrderSize() |
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
591 |
|
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
592 |
# 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
|
593 |
def GetExecutionOrder(self): |
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
594 |
return self.ExecutionOrder |
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
595 |
|
0 | 596 |
# Returns the variable minimum size |
597 |
def GetMinSize(self): |
|
42 | 598 |
return self.NameSize[0] + 10, self.NameSize[1] + 10 |
0 | 599 |
|
3
86ccc89d7b0b
FBD Blocks and Variables can now be modified and wires can't be unconnected on both sides
lbessard
parents:
2
diff
changeset
|
600 |
# Method called when a LeftDClick event have been generated |
27 | 601 |
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
|
602 |
# 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
|
603 |
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
|
604 |
|
0 | 605 |
# Method called when a RightUp event have been generated |
27 | 606 |
def OnRightUp(self, event, dc, scaling): |
102
85875dcb7754
Adding edit user's POU by double click on block instance
lbessard
parents:
99
diff
changeset
|
607 |
self.Parent.PopupDefaultMenu() |
0 | 608 |
|
609 |
# Refreshes the variable model |
|
610 |
def RefreshModel(self, move=True): |
|
611 |
self.Parent.RefreshVariableModel(self) |
|
612 |
# If variable has moved and variable is not of type OUTPUT, refresh the model |
|
613 |
# of wires connected to output connector |
|
614 |
if move and self.Type != OUTPUT: |
|
615 |
if self.Output: |
|
616 |
self.Output.RefreshWires() |
|
617 |
||
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
618 |
def AddError(self, infos, start, end): |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
619 |
if infos[0] == "expression" and start[0] == 0 and end[0] == 0: |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
620 |
self.Errors.append((start[1], end[1])) |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
621 |
|
0 | 622 |
# Draws variable |
623 |
def Draw(self, dc): |
|
144 | 624 |
Graphic_Element.Draw(self, dc) |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
625 |
dc.SetPen(wx.BLACK_PEN) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
626 |
dc.SetBrush(wx.WHITE_BRUSH) |
213 | 627 |
|
628 |
if getattr(dc, "printing", False): |
|
629 |
name_size = dc.GetTextExtent(self.Name) |
|
630 |
executionorder_size = dc.GetTextExtent(str(self.ExecutionOrder)) |
|
631 |
else: |
|
632 |
name_size = self.NameSize |
|
633 |
executionorder_size = self.ExecutionOrderSize |
|
634 |
||
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
635 |
text_pos = (self.Pos.x + (self.Size[0] - name_size[0]) / 2, |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
636 |
self.Pos.y + (self.Size[1] - name_size[1]) / 2) |
0 | 637 |
# Draw a rectangle with the variable size |
638 |
dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1) |
|
639 |
# Draw variable name |
|
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
640 |
dc.DrawText(self.Name, text_pos[0], text_pos[1]) |
0 | 641 |
# Draw connectors |
642 |
if self.Input: |
|
643 |
self.Input.Draw(dc) |
|
644 |
if self.Output: |
|
645 |
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
|
646 |
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
|
647 |
# Draw variable execution order |
213 | 648 |
dc.DrawText(str(self.ExecutionOrder), self.Pos.x + self.Size[0] - executionorder_size[0], |
118
0c53d6a36013
Add support for defining execution order in FBD networks (related ST code not generated yet)
lbessard
parents:
112
diff
changeset
|
649 |
self.Pos.y + self.Size[1] + 2) |
231
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
650 |
for start, end in self.Errors: |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
651 |
offset = dc.GetTextExtent(self.Name[:start]) |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
652 |
size = dc.GetTextExtent(self.Name[start:end + 1]) |
fc2d6cbb8b39
Adding support for highlighing compiling errors from matiec
lbessard
parents:
213
diff
changeset
|
653 |
HighlightErrorZone(dc, text_pos[0] + offset[0], text_pos[1], size[0], size[1]) |
0 | 654 |
|
655 |
#------------------------------------------------------------------------------- |
|
656 |
# Function Block Diagram Connector |
|
657 |
#------------------------------------------------------------------------------- |
|
658 |
||
659 |
""" |
|
660 |
Class that implements the graphic representation of a connection |
|
661 |
""" |
|
662 |
||
663 |
class FBD_Connector(Graphic_Element): |
|
664 |
||
665 |
# Create a new connection |
|
666 |
def __init__(self, parent, type, name, id = None): |
|
667 |
Graphic_Element.__init__(self, parent) |
|
668 |
self.Type = type |
|
669 |
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
|
670 |
self.SetName(name) |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
671 |
self.Pos = wx.Point(0, 0) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
672 |
self.Size = wx.Size(0, 0) |
0 | 673 |
# Create an input or output connector according to connection type |
674 |
if self.Type == CONNECTOR: |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
675 |
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
|
676 |
else: |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
677 |
self.Connector = Connector(self, "", "ANY", wx.Point(0, 0), EAST) |
0 | 678 |
self.RefreshConnectors() |
42 | 679 |
self.RefreshNameSize() |
0 | 680 |
|
249 | 681 |
def Flush(self): |
682 |
if self.Connector: |
|
683 |
self.Connector.Flush() |
|
684 |
self.Connector = None |
|
0 | 685 |
|
144 | 686 |
# Returns the RedrawRect |
687 |
def GetRedrawRect(self, movex = 0, movey = 0): |
|
688 |
rect = Graphic_Element.GetRedrawRect(self, movex, movey) |
|
689 |
if movex != 0 or movey != 0: |
|
690 |
if self.Connector and self.Connector.IsConnected(): |
|
691 |
rect = rect.Union(self.Connector.GetConnectedRedrawRect(movex, movey)) |
|
692 |
return rect |
|
693 |
||
112 | 694 |
# Make a clone of this FBD_Connector |
162 | 695 |
def Clone(self, parent, id = None, pos = None): |
696 |
connection = FBD_Connector(parent, self.Type, self.Name, id) |
|
112 | 697 |
connection.SetSize(self.Size[0], self.Size[1]) |
698 |
if pos is not None: |
|
699 |
connection.SetPosition(pos.x, pos.y) |
|
700 |
connection.Connector = self.Connector.Clone(connection) |
|
701 |
return connection |
|
702 |
||
0 | 703 |
# Unconnect connector |
704 |
def Clean(self): |
|
705 |
if self.Connector: |
|
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
706 |
self.Connector.UnConnect(delete = True) |
0 | 707 |
|
708 |
# Delete this connection by calling the appropriate method |
|
709 |
def Delete(self): |
|
710 |
self.Parent.DeleteConnection(self) |
|
711 |
||
42 | 712 |
# Refresh the size of text for name |
713 |
def RefreshNameSize(self): |
|
165 | 714 |
self.NameSize = self.Parent.GetTextExtent(self.Name) |
42 | 715 |
|
0 | 716 |
# Refresh the connection bounding box |
717 |
def RefreshBoundingBox(self): |
|
718 |
if self.Type == CONNECTOR: |
|
719 |
bbx_x = self.Pos.x - CONNECTOR_SIZE |
|
720 |
else: |
|
721 |
bbx_x = self.Pos.x |
|
722 |
bbx_width = self.Size[0] + CONNECTOR_SIZE |
|
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
723 |
self.BoundingBox = wx.Rect(bbx_x, self.Pos.y, bbx_width, self.Size[1]) |
0 | 724 |
|
725 |
# Refresh the position of the connection connector |
|
726 |
def RefreshConnectors(self): |
|
145 | 727 |
scaling = self.Parent.GetScaling() |
728 |
if scaling is not None: |
|
729 |
position = round(float(self.Pos.y + self.Size[1] / 2) / float(scaling[1])) * scaling[1] - self.Pos.y |
|
730 |
else: |
|
731 |
position = self.Size[1] / 2 |
|
0 | 732 |
if self.Type == CONNECTOR: |
145 | 733 |
self.Connector.SetPosition(wx.Point(0, position)) |
734 |
else: |
|
735 |
self.Connector.SetPosition(wx.Point(self.Size[0], position)) |
|
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
736 |
self.RefreshConnected() |
0 | 737 |
|
738 |
# Refresh the position of wires connected to connector |
|
739 |
def RefreshConnected(self, exclude = []): |
|
740 |
if self.Connector: |
|
741 |
self.Connector.MoveConnected(exclude) |
|
742 |
||
743 |
# Test if point given is on the connection connector |
|
243
c5da8b706cde
Adding support for allowing connections only between an input and an output connector
lbessard
parents:
231
diff
changeset
|
744 |
def TestConnector(self, pt, direction = None, exclude=True): |
c5da8b706cde
Adding support for allowing connections only between an input and an output connector
lbessard
parents:
231
diff
changeset
|
745 |
if self.Connector and self.Connector.TestPoint(pt, direction, exclude): |
0 | 746 |
return self.Connector |
747 |
return None |
|
748 |
||
749 |
# Returns the connection connector |
|
27 | 750 |
def GetConnector(self, position = None, name = None): |
0 | 751 |
return self.Connector |
752 |
||
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
753 |
# Changes the variable type |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
754 |
def SetType(self, type): |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
755 |
if type != self.Type: |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
756 |
self.Type = type |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
757 |
self.Clean() |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
758 |
# Create an input or output connector according to connection type |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
759 |
if self.Type == CONNECTOR: |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
760 |
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
|
761 |
else: |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
762 |
self.Connector = Connector(self, "", "ANY", wx.Point(0, 0), EAST) |
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
763 |
self.RefreshConnectors() |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
764 |
|
0 | 765 |
# Returns the connection type |
766 |
def GetType(self): |
|
767 |
return self.Type |
|
768 |
||
145 | 769 |
def GetConnectionResultType(self, connector, connectortype): |
770 |
return connectortype |
|
771 |
||
0 | 772 |
# Changes the connection name |
773 |
def SetName(self, name): |
|
774 |
self.Name = name |
|
42 | 775 |
self.RefreshNameSize() |
0 | 776 |
|
777 |
# Returns the connection name |
|
778 |
def GetName(self): |
|
779 |
return self.Name |
|
780 |
||
781 |
# Returns the connection minimum size |
|
782 |
def GetMinSize(self): |
|
42 | 783 |
text_width, text_height = self.NameSize |
0 | 784 |
if text_height % 2 == 1: |
785 |
text_height += 1 |
|
786 |
return text_width + text_height + 20, text_height + 10 |
|
787 |
||
28
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
788 |
# Method called when a LeftDClick event have been generated |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
789 |
def OnLeftDClick(self, event, dc, scaling): |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
790 |
# Edit the connection properties |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
791 |
self.Parent.EditConnectionContent(self) |
fc23e1f415d8
Adding support for concurrent overriden standard function
lbessard
parents:
27
diff
changeset
|
792 |
|
0 | 793 |
# Method called when a RightUp event have been generated |
27 | 794 |
def OnRightUp(self, event, dc, scaling): |
0 | 795 |
# Popup the default menu |
796 |
self.Parent.PopupDefaultMenu() |
|
797 |
||
798 |
# Refreshes the connection model |
|
799 |
def RefreshModel(self, move=True): |
|
800 |
self.Parent.RefreshConnectionModel(self) |
|
801 |
# If connection has moved and connection is of type CONTINUATION, refresh |
|
802 |
# the model of wires connected to connector |
|
803 |
if move and self.Type == CONTINUATION: |
|
804 |
if self.Connector: |
|
805 |
self.Connector.RefreshWires() |
|
806 |
||
807 |
# Draws connection |
|
808 |
def Draw(self, dc): |
|
144 | 809 |
Graphic_Element.Draw(self, dc) |
64
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
810 |
dc.SetPen(wx.BLACK_PEN) |
dd6f693e46a1
Cleaning code for using only wxPython 2.6 class naming
lbessard
parents:
58
diff
changeset
|
811 |
dc.SetBrush(wx.WHITE_BRUSH) |
213 | 812 |
|
813 |
if getattr(dc, "printing", False): |
|
814 |
name_size = dc.GetTextExtent(self.Name) |
|
815 |
else: |
|
816 |
name_size = self.NameSize |
|
817 |
||
0 | 818 |
# Draw a rectangle with the connection size with arrows in |
819 |
dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1) |
|
213 | 820 |
arrowsize = min(self.Size[1] / 2, (self.Size[0] - name_size[0] - 10) / 2) |
0 | 821 |
dc.DrawLine(self.Pos.x, self.Pos.y, self.Pos.x + arrowsize, |
822 |
self.Pos.y + self.Size[1] / 2) |
|
823 |
dc.DrawLine(self.Pos.x + arrowsize, self.Pos.y + self.Size[1] / 2, |
|
824 |
self.Pos.x, self.Pos.y + self.Size[1]) |
|
825 |
dc.DrawLine(self.Pos.x + self.Size[0] - arrowsize, self.Pos.y, |
|
826 |
self.Pos.x + self.Size[0], self.Pos.y + self.Size[1] / 2) |
|
827 |
dc.DrawLine(self.Pos.x + self.Size[0], self.Pos.y + self.Size[1] / 2, |
|
828 |
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
|
829 |
# Draw connection name |
213 | 830 |
dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - name_size[0]) / 2, |
831 |
self.Pos.y + (self.Size[1] - name_size[1]) / 2) |
|
0 | 832 |
# Draw connector |
833 |
if self.Connector: |
|
834 |
self.Connector.Draw(dc) |
|
140
06d28f03f6f4
Adding highlighting on group or element when mouse is over
lbessard
parents:
118
diff
changeset
|
835 |