1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
# -*- coding: utf-8 -*-
#
# AWL simulator - FUP - Connection classes
#
# Copyright 2016 Michael Buesch <m@bues.ch>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
from __future__ import division, absolute_import, print_function, unicode_literals
#from awlsim.common.cython_support cimport * #@cy
from awlsim.common.compat import *
from awlsim.common.xmlfactory import *
from awlsim.gui.fup.fup_base import *
from awlsim.gui.fup.fup_wire import *
class FupConn_factory(XmlFactory):
def parser_open(self, tag=None):
self.inConn = False
XmlFactory.parser_open(self, tag)
def parser_beginTag(self, tag):
if not self.inConn and self.elem:
if tag.name == "connection":
self.inConn = True
pos = tag.getAttrInt("pos")
dirIn = tag.getAttrInt("dir_in")
dirOut = tag.getAttrInt("dir_out")
wireId = tag.getAttrInt("wire")
text = tag.getAttr("text", "")
inverted = tag.getAttrBool("inverted", False)
uuid = tag.getAttr("uuid", None)
if pos < 0 or pos > 0xFFFF:
raise self.Error("Invalid <connection> pos.")
wire = self.elem.grid.getWireById(wireId)
try:
if dirIn and not dirOut:
self.elem.inputs.extend(
[None] * (pos + 1 - len(self.elem.inputs)))
conn = FupConnIn(elem=self.elem, wire=wire,
inverted=inverted,
uuid=uuid)
if wire:
wire.connect(conn)
self.elem.inputs[pos] = conn
conn.text = text
return
elif dirOut and not dirIn:
self.elem.outputs.extend(
[None] * (pos + 1 - len(self.elem.outputs)))
conn = FupConnOut(elem=self.elem, wire=wire,
inverted=inverted,
uuid=uuid)
if wire:
wire.connect(conn)
self.elem.outputs[pos] = conn
conn.text = text
return
except ValueError:
raise self.Error("Invalid <connection>")
XmlFactory.parser_beginTag(self, tag)
def parser_endTag(self, tag):
if self.inConn:
if tag.name == "connection":
self.inConn = False
return
else:
if tag.name == "connections":
if not all(self.elem.inputs) or\
not all(self.elem.outputs):
raise self.Error("<element> connections "
"are incomplete.")
self.parser_finish()
return
XmlFactory.parser_endTag(self, tag)
def composer_getTags(self):
conn = self.conn
return [
self.Tag(name="connection",
attrs={
"dir_in" : str(int(conn.IN)),
"dir_out" : str(int(conn.OUT)),
"pos" : str(conn.pos),
"wire" : str(-1) if conn.wire is None
else str(conn.wire.idNum),
"text" : str(conn.text),
"inverted" : str(int(conn.inverted))\
if conn.inverted else "",
"uuid" : str(conn.uuid),
}),
]
class FupConn(FupBaseClass):
"""FUP/FBD element connection base class"""
factory = FupConn_factory
IN = False
OUT = False
def __init__(self, elem=None, wire=None, text="", inverted=False, uuid=None):
FupBaseClass.__init__(self, uuid=uuid)
self.elem = elem # The FupElem this connection belongs to
self.wire = wire # The FupWire this connection is connected to (if any).
self.text = text or "" # Description text
self.inverted = inverted # Inverted connection (NOT)
@property
def drawOffset(self):
"""Get the draw offset; in X direction.
"""
if not self.isConnected:
return self.anchorOffset
return 0
@property
def anchorOffset(self):
"""Get the anchor offset; in X direction.
"""
elem = self.elem
if elem:
return elem._connAnchorOffset
return 0
@property
def pos(self):
"""Get the connection position in the owning element.
Top position is 0.
Note that this is _not_ the y grid position. So a top position
might _not_ correspond to relative y=0.
"""
elem = self.elem
if elem:
idx = -1
if self.IN:
idx = elem.inputs.index(self)
if idx < 0 and self.OUT:
idx = elem.outputs.index(self)
if idx >= 0:
return idx
return IndexError
@property
def relCoords(self):
"""Get the (x, y) grid coordinates of this connection
relative to the element's root.
Raises IndexError, if this does not belong to an element.
"""
elem = self.elem
if elem:
return elem.getConnRelCoords(self)
return IndexError
@property
def relPixCoords(self):
"""Get the (x, y) pixel coordinates of this connection
relative to the element's root.
Raises IndexError, if this does not belong to an element.
"""
elem = self.elem
if elem:
return elem.getConnRelPixCoords(self)
raise IndexError
@property
def coords(self):
"""Get the absolute (x, y) grid coordinates of this connection.
Raises IndexError, if this does not belong to an element.
"""
elem = self.elem
if elem:
xAbs, yAbs = elem.x, elem.y
xRel, yRel = self.relCoords
return xAbs + xRel, yAbs + yRel
raise IndexError
@property
def pixCoords(self):
"""Get the absolute (x, y) pixel coordinates of this connection.
Raises IndexError, if this does not belong to an element.
"""
elem = self.elem
if elem:
xAbs, yAbs = elem.pixCoords
xRel, yRel = self.relPixCoords
return xAbs + xRel, yAbs + yRel
raise IndexError
@property
def isConnected(self):
"""Returns True, if this connection is connected to a wire.
"""
return self.wire is not None
def canConnectTo(self, other):
"""Check if this connection can connect to another connection.
"""
if self.wire is not None and other.wire is not None:
return False
return (self is not other) and\
(self.elem is not None and other.elem is not None) and\
(self.elem is not other.elem) and\
((self.IN and other.OUT) or\
(self.OUT and other.IN))
def connectTo(self, other):
"""Connect this connection to another.
Raises ValueError, if the connection cannot be done.
"""
if not self.canConnectTo(other):
raise ValueError
wire = self.wire or other.wire
if not wire:
# Create a new wire
if not self.elem or not self.elem.grid:
raise ValueError
wire = FupWire(self.elem.grid)
wire.connect(self)
wire.connect(other)
def disconnect(self):
"""Break the current connection, if any.
Returns True, if there was a connection.
"""
if self.wire:
self.wire.disconnect(self)
return True
return False
def removeFromElem(self):
"""Remove this connection from its element.
Note that the life time of this conn object may end here.
Returns True, if the connection was removed.
"""
if self.elem:
self.disconnect()
return self.elem.removeConn(self)
return False
def checkWireCollisions(self):
"""Mark the wire as must-check-collisions.
The collision check will be done at the next wire re-draw.
"""
if self.wire:
self.wire.checkCollisions()
class FupConnIn(FupConn):
"""FUP/FBD element input connection"""
IN = True
class FupConnOut(FupConn):
"""FUP/FBD element output connection"""
OUT = True
|