plcopen/plcopen.py
changeset 384 ed27a676d5c9
parent 383 25ffba02b6a8
child 387 fcae4e40f296
equal deleted inserted replaced
383:25ffba02b6a8 384:ed27a676d5c9
    43 Define which action qualifier must be associated with a duration 
    43 Define which action qualifier must be associated with a duration 
    44 """
    44 """
    45 QualifierList = {"N" : False, "R" : False, "S" : False, "L" : True, "D" : True, 
    45 QualifierList = {"N" : False, "R" : False, "S" : False, "L" : True, "D" : True, 
    46     "P" : False, "P0" : False, "P1" : False, "SD" : True, "DS" : True, "SL" : True}
    46     "P" : False, "P0" : False, "P1" : False, "SD" : True, "DS" : True, "SL" : True}
    47 
    47 
       
    48 
       
    49 def _init_and_compare(function, v1, v2):
       
    50     if v1 is None:
       
    51         return v2
       
    52     if v2 is not None:
       
    53         return function(v1, v2)
       
    54     return v1
       
    55 
       
    56 """
       
    57 Helper class for bounding_box calculation 
       
    58 """
       
    59 class rect:
       
    60     
       
    61     def __init__(self, x=None, y=None, width=None, height=None):
       
    62         self.x_min = x
       
    63         self.x_max = None
       
    64         self.y_min = y
       
    65         self.y_max = None
       
    66         if width is not None and x is not None:
       
    67             self.x_max = x + width
       
    68         if height is not None and y is not None:
       
    69             self.y_max = y + height
       
    70     
       
    71     def update(self, x, y):
       
    72         self.x_min = _init_and_compare(min, self.x_min, x)
       
    73         self.x_max = _init_and_compare(max, self.x_max, x)
       
    74         self.y_min = _init_and_compare(min, self.y_min, y)
       
    75         self.y_max = _init_and_compare(max, self.y_max, y)
       
    76         
       
    77     def union(self, rect):
       
    78         self.x_min = _init_and_compare(min, self.x_min, rect.x_min)
       
    79         self.x_max = _init_and_compare(max, self.x_max, rect.x_max)
       
    80         self.y_min = _init_and_compare(min, self.y_min, rect.y_min)
       
    81         self.y_max = _init_and_compare(max, self.y_max, rect.y_max)
       
    82     
       
    83     def bounding_box(self):
       
    84         width = height = None
       
    85         if self.x_min is not None and self.x_max is not None:
       
    86             width = self.x_max - self.x_min
       
    87         if self.y_min is not None and self.y_max is not None:
       
    88             height = self.y_max - self.y_min
       
    89         return self.x_min, self.y_min, width, height
       
    90             
       
    91 
    48 PLCOpenClasses = GenerateClassesFromXSD(os.path.join(os.path.split(__file__)[0], "TC6_XML_V10_B.xsd"))
    92 PLCOpenClasses = GenerateClassesFromXSD(os.path.join(os.path.split(__file__)[0], "TC6_XML_V10_B.xsd"))
       
    93 
       
    94 ElementNameToClass = {}
    49 
    95 
    50 cls = PLCOpenClasses.get("formattedText", None)
    96 cls = PLCOpenClasses.get("formattedText", None)
    51 if cls:
    97 if cls:
    52     def updateElementName(self, old_name, new_name):
    98     def updateElementName(self, old_name, new_name):
    53         index = self.text.find(old_name)
    99         index = self.text.find(old_name)
  1221     self.position.setx(x)
  1267     self.position.setx(x)
  1222     
  1268     
  1223 def sety(self, y):
  1269 def sety(self, y):
  1224     self.position.sety(y)
  1270     self.position.sety(y)
  1225 
  1271 
       
  1272 def _getBoundingBox(self):
       
  1273     return rect(self.getx(), self.gety(), self.getwidth(), self.getheight())
       
  1274 
       
  1275 def _getConnectionsBoundingBox(connectionPointIn):
       
  1276     bbox = rect()
       
  1277     connections = connectionPointIn.getconnections()
       
  1278     if connections is not None:
       
  1279         for connection in connections:
       
  1280             for x, y in connection.getpoints():
       
  1281                 bbox.update(x, y)
       
  1282     return bbox
       
  1283 
       
  1284 def _getBoundingBoxSingle(self):
       
  1285     bbox = _getBoundingBox(self)
       
  1286     if self.connectionPointIn is not None:
       
  1287         bbox.union(_getConnectionsBoundingBox(self.connectionPointIn))
       
  1288     return bbox
       
  1289 
       
  1290 def _getBoundingBoxMultiple(self):
       
  1291     bbox = _getBoundingBox(self)
       
  1292     for connectionPointIn in self.getconnectionPointIn():
       
  1293         bbox.union(_getConnectionsBoundingBox(connectionPointIn))
       
  1294     return bbox
       
  1295 
       
  1296 def _filterConnections(connectionPointIn, localId, connections):
       
  1297     in_connections = connectionPointIn.getconnections()
       
  1298     if in_connections is not None:
       
  1299         to_delete = []
       
  1300         for i, connection in enumerate(in_connections):
       
  1301             connected = connection.getrefLocalId()
       
  1302             if not connections.has_key((localId, connected)) and \
       
  1303                not connections.has_key((connected, localId)):
       
  1304                 to_delete.append(i)
       
  1305         to_delete.reverse()
       
  1306         for i in to_delete:
       
  1307             connectionPointIn.removeconnection(i)
       
  1308 
       
  1309 def _filterConnectionsSingle(self, connections):
       
  1310     if self.connectionPointIn is not None:
       
  1311         _filterConnections(self.connectionPointIn, self.localId, connections)
       
  1312 
       
  1313 def _filterConnectionsMultiple(self, connections):
       
  1314     for connectionPointIn in self.getconnectionPointIn():
       
  1315         _filterConnections(connectionPointIn, self.localId, connections)
       
  1316 
       
  1317 def _getconnectionsdefinition(instance, connections_end):
       
  1318     id = instance.getlocalId()
       
  1319     return dict([((id, end), True) for end in connections_end])
       
  1320 
       
  1321 def _updateConnectionsId(connectionPointIn, translation):
       
  1322     connections_end = []
       
  1323     connections = connectionPointIn.getconnections()
       
  1324     if connections is not None:
       
  1325         for connection in connections:
       
  1326             refLocalId = connection.getrefLocalId()
       
  1327             new_reflocalId = translation.get(refLocalId, refLocalId)
       
  1328             connection.setrefLocalId(new_reflocalId)
       
  1329             connections_end.append(new_reflocalId)
       
  1330     return connections_end
       
  1331 
       
  1332 def _updateConnectionsIdSingle(self, translation):
       
  1333     connections_end = []
       
  1334     if self.connectionPointIn is not None:
       
  1335         connections_end = _updateConnectionsId(self.connectionPointIn, translation)
       
  1336     return _getconnectionsdefinition(self, connections_end)
       
  1337 
       
  1338 def _updateConnectionsIdMultiple(self, translation):
       
  1339     connections_end = []
       
  1340     for connectionPointIn in self.getconnectionPointIn():
       
  1341         connections_end.extend(_updateConnectionsId(connectionPointIn, translation))
       
  1342     return _getconnectionsdefinition(self, connections_end)
       
  1343 
       
  1344 def _translate(self, dx, dy):
       
  1345     self.setx(self.getx() + dx)
       
  1346     self.sety(self.gety() + dy)
       
  1347     
       
  1348 def _translateConnections(connectionPointIn, dx, dy):
       
  1349     connections = connectionPointIn.getconnections()
       
  1350     if connections is not None:
       
  1351         for connection in connections:
       
  1352             for position in connection.getposition():
       
  1353                 position.setx(position.getx() + dx)
       
  1354                 position.sety(position.gety() + dy)
       
  1355 
       
  1356 def _translateSingle(self, dx, dy):
       
  1357     _translate(self, dx, dy)
       
  1358     if self.connectionPointIn is not None:
       
  1359         _translateConnections(self.connectionPointIn, dx, dy)
       
  1360 
       
  1361 def _translateMultiple(self, dx, dy):
       
  1362     _translate(self, dx, dy)
       
  1363     for connectionPointIn in self.getconnectionPointIn():
       
  1364         _translateConnections(connectionPointIn, dx, dy)
       
  1365 
  1226 def _updateElementName(self, old_name, new_name):
  1366 def _updateElementName(self, old_name, new_name):
  1227     pass
  1367     pass
  1228 
  1368 
       
  1369 _connectionsFunctions = {
       
  1370     "bbox": {"none": _getBoundingBox,
       
  1371              "single": _getBoundingBoxSingle,
       
  1372              "multiple": _getBoundingBoxMultiple},
       
  1373     "translate": {"none": _translate,
       
  1374                "single": _translateSingle,
       
  1375                "multiple": _translateMultiple},
       
  1376     "filter": {"none": lambda self, connections: None,
       
  1377                "single": _filterConnectionsSingle,
       
  1378                "multiple": _filterConnectionsMultiple},
       
  1379     "update": {"none": lambda self, translation: None,
       
  1380                "single": _updateConnectionsIdSingle,
       
  1381                "multiple": _updateConnectionsIdMultiple}
       
  1382 }
       
  1383 
  1229 def _initElementClass(name, classname, connectionPointInType="none"):
  1384 def _initElementClass(name, classname, connectionPointInType="none"):
       
  1385     ElementNameToClass[name] = classname
  1230     cls = PLCOpenClasses.get(classname, None)
  1386     cls = PLCOpenClasses.get(classname, None)
  1231     if cls:
  1387     if cls:
  1232         setattr(cls, "getx", getx)
  1388         setattr(cls, "getx", getx)
  1233         setattr(cls, "gety", gety)
  1389         setattr(cls, "gety", gety)
  1234         setattr(cls, "setx", setx)
  1390         setattr(cls, "setx", setx)
  1235         setattr(cls, "sety", sety)
  1391         setattr(cls, "sety", sety)
  1236         setattr(cls, "updateElementName", _updateElementName)
  1392         setattr(cls, "updateElementName", _updateElementName)
       
  1393         setattr(cls, "getBoundingBox", _connectionsFunctions["bbox"][connectionPointInType])
       
  1394         setattr(cls, "translate", _connectionsFunctions["translate"][connectionPointInType])
       
  1395         setattr(cls, "filterConnections", _connectionsFunctions["filter"][connectionPointInType])
       
  1396         setattr(cls, "updateConnectionsId", _connectionsFunctions["update"][connectionPointInType])
  1237     return cls
  1397     return cls
  1238 
  1398 
  1239 def _getexecutionOrder(instance, specific_values):
  1399 def _getexecutionOrder(instance, specific_values):
  1240     executionOrder = instance.getexecutionOrderId()
  1400     executionOrder = instance.getexecutionOrderId()
  1241     if executionOrder is None:
  1401     if executionOrder is None:
  1411 
  1571 
  1412     def updateElementName(self, old_name, new_name):
  1572     def updateElementName(self, old_name, new_name):
  1413         if self.typeName == old_name:
  1573         if self.typeName == old_name:
  1414             self.typeName = new_name
  1574             self.typeName = new_name
  1415     setattr(cls, "updateElementName", updateElementName)
  1575     setattr(cls, "updateElementName", updateElementName)
       
  1576 
       
  1577     def filterConnections(self, connections):
       
  1578         for input in self.inputVariables.getvariable():
       
  1579             _filterConnections(input.connectionPointIn, self.localId, connections)
       
  1580     setattr(cls, "filterConnections", filterConnections)
       
  1581 
       
  1582     def updateConnectionsId(self, translation):
       
  1583         connections_end = []
       
  1584         for input in self.inputVariables.getvariable():
       
  1585             connections_end.extend(_updateConnectionsId(input.connectionPointIn, translation))
       
  1586         return _getconnectionsdefinition(self, connections_end)
       
  1587     setattr(cls, "updateConnectionsId", updateConnectionsId)
       
  1588 
       
  1589     def translate(self, dx, dy):
       
  1590         _translate(self, dx, dy)
       
  1591         for input in self.inputVariables.getvariable():
       
  1592             _translateConnections(input.connectionPointIn, dx, dy)
       
  1593     setattr(cls, "translate", translate)
  1416 
  1594 
  1417 cls = _initElementClass("leftPowerRail", "ldObjects_leftPowerRail")
  1595 cls = _initElementClass("leftPowerRail", "ldObjects_leftPowerRail")
  1418 if cls:
  1596 if cls:
  1419     setattr(cls, "getinfos", _getpowerrailinfosFunction("leftPowerRail"))
  1597     setattr(cls, "getinfos", _getpowerrailinfosFunction("leftPowerRail"))
  1420 
  1598