PLCControler.py
changeset 384 ed27a676d5c9
parent 383 25ffba02b6a8
child 385 373635372b93
equal deleted inserted replaced
383:25ffba02b6a8 384:ed27a676d5c9
  1578         words = tagname.split("::")
  1578         words = tagname.split("::")
  1579         if words[0] in ["P","T","A"]:
  1579         if words[0] in ["P","T","A"]:
  1580             return self.GetProjectPouVariables(words[1], debug)
  1580             return self.GetProjectPouVariables(words[1], debug)
  1581         return []
  1581         return []
  1582 
  1582 
       
  1583     def GetEditedElementCopy(self, tagname, debug = False):
       
  1584         element = self.GetEditedElement(tagname, debug)
       
  1585         if element is not None:
       
  1586             name = element.__class__.__name__
       
  1587             return element.generateXMLText(name.split("_")[-1], 0)
       
  1588         return ""
       
  1589         
       
  1590     def GetEditedElementInstancesCopy(self, tagname, blocks_id = None, wires = None, debug = False):
       
  1591         element = self.GetEditedElement(tagname, debug)
       
  1592         text = ""
       
  1593         if element is not None:
       
  1594             wires = dict([(wire, True) for wire in wires if wire[0] in blocks_id and wire[1] in blocks_id])
       
  1595             for id in blocks_id:
       
  1596                 instance = element.getinstance(id)
       
  1597                 if instance is not None:
       
  1598                     instance_copy = self.Copy(instance)
       
  1599                     instance_copy.filterConnections(wires)
       
  1600                     name = instance_copy.__class__.__name__
       
  1601                     text += instance_copy.generateXMLText(name.split("_")[-1], 0)
       
  1602         return text
       
  1603     
       
  1604     def GenerateNewName(self, tagname, name, format, exclude={}, debug=False):
       
  1605         names = exclude.copy()
       
  1606         names.update(dict([(varname.upper(), True) for varname in self.GetEditedElementVariables(tagname, debug)]))
       
  1607         element = self.GetEditedElement(tagname, debug)
       
  1608         if element is not None:
       
  1609             for instance in element.getinstances():
       
  1610                 if isinstance(instance, (plcopen.sfcObjects_step, plcopen.commonObjects_connector, plcopen.commonObjects_continuation)):
       
  1611                     names[instance.getname()] = True
       
  1612         i = 1
       
  1613         while names.get(name.upper(), False):
       
  1614             name = (format%i)
       
  1615             i += 1
       
  1616         return name
       
  1617     
       
  1618     CheckPasteCompatibility = {"SFC": lambda name: True,
       
  1619                                "LD": lambda name: not name.startswith("sfcObjects"),
       
  1620                                "FBD": lambda name: name.startswith("fbdObjects") or name.startswith("commonObjects")}
       
  1621     
       
  1622     def PasteEditedElementInstances(self, tagname, text, new_pos, middle=False, debug=False):
       
  1623         element = self.GetEditedElement(tagname, debug)
       
  1624         element_name, element_type = self.GetEditedElementType(tagname, debug)
       
  1625         if element is not None:
       
  1626             bodytype = element.getbodyType()
       
  1627             
       
  1628             # Get edited element type scaling
       
  1629             scaling = None
       
  1630             project = self.GetProject(debug)
       
  1631             if project is not None:
       
  1632                 properties = project.getcontentHeader()
       
  1633                 scaling = properties["scaling"][bodytype]
       
  1634             
       
  1635             # Get ids already by all the instances in edited element
       
  1636             used_id = dict([(instance.getlocalId(), True) for instance in element.getinstances()])
       
  1637             new_id = {}
       
  1638             
       
  1639             text = "<paste>%s</paste>"%text
       
  1640             
       
  1641             try:
       
  1642                 tree = minidom.parseString(text)
       
  1643             except:
       
  1644                 return _("Invalid plcopen element(s)!!!")
       
  1645             instances = []
       
  1646             exclude = {}
       
  1647             for root in tree.childNodes:
       
  1648                 if root.nodeType == tree.ELEMENT_NODE and root.nodeName == "paste":
       
  1649                     for child in root.childNodes:
       
  1650                         if child.nodeType == tree.ELEMENT_NODE:
       
  1651                             classname = plcopen.ElementNameToClass[child.nodeName]
       
  1652                             if not self.CheckPasteCompatibility[bodytype](classname):
       
  1653                                 return _("\"%s\" element can't be paste here!!!")%child.nodeName
       
  1654                             classobj = getattr(plcopen, classname, None)
       
  1655                             if classobj is not None:
       
  1656                                 instance = classobj()
       
  1657                                 instance.loadXMLTree(child)
       
  1658                                 if child.nodeName == "block":
       
  1659                                     blockname = instance.getinstanceName()
       
  1660                                     if blockname is not None:
       
  1661                                         blocktype = instance.gettypeName()
       
  1662                                         if element_type == "function":
       
  1663                                             return _("FunctionBlock \"%s\" can't be paste in a Function!!!")%blocktype
       
  1664                                         blockname = self.GenerateNewName(tagname, blockname, "Block%d", debug=debug)
       
  1665                                         exclude[blockname] = True
       
  1666                                         instance.setinstanceName(blockname)
       
  1667                                         self.AddEditedElementPouVar(tagname, blocktype, blockname)
       
  1668                                 elif child.nodeName == "step":
       
  1669                                     stepname = self.GenerateNewName(tagname, instance.getname(), "Step%d", exclude, debug)
       
  1670                                     exclude[stepname] = True
       
  1671                                     instance.setname(stepname)
       
  1672                                 localid = instance.getlocalId()
       
  1673                                 if not used_id.has_key(localid):
       
  1674                                     new_id[localid] = True
       
  1675                                 instances.append((child.nodeName, instance))
       
  1676             
       
  1677             if len(instances) == 0:
       
  1678                 return _("Invalid plcopen element(s)!!!")
       
  1679             
       
  1680             idx = 1
       
  1681             translate_id = {}
       
  1682             bbox = plcopen.rect()
       
  1683             for name, instance in instances:
       
  1684                 localId = instance.getlocalId()
       
  1685                 bbox.union(instance.getBoundingBox())
       
  1686                 if used_id.has_key(localId):
       
  1687                     while used_id.has_key(idx) or new_id.has_key(idx):
       
  1688                         idx += 1
       
  1689                     new_id[idx] = True
       
  1690                     instance.setlocalId(idx)
       
  1691                     translate_id[localId] = idx
       
  1692             
       
  1693             x, y, width, height = bbox.bounding_box()
       
  1694             if middle:
       
  1695                 new_pos[0] -= width / 2
       
  1696                 new_pos[1] -= height / 2
       
  1697             else:
       
  1698                 new_pos = map(lambda x: x + 30, new_pos)
       
  1699             if scaling[0] != 0 and scaling[1] != 0:
       
  1700                 min_pos = map(lambda x: 30 / x, scaling)
       
  1701                 minx = round(min_pos[0])
       
  1702                 if int(min_pos[0]) == round(min_pos[0]):
       
  1703                     minx += 1
       
  1704                 miny = round(min_pos[1])
       
  1705                 if int(min_pos[1]) == round(min_pos[1]):
       
  1706                     miny += 1
       
  1707                 minx *= scaling[0]
       
  1708                 miny *= scaling[1]
       
  1709                 new_pos = (max(minx, round(new_pos[0] / scaling[0]) * scaling[0]),
       
  1710                            max(miny, round(new_pos[1] / scaling[1]) * scaling[1]))
       
  1711             else:
       
  1712                 new_pos = (max(30, new_pos[0]), max(30, new_pos[1]))
       
  1713             diff = (new_pos[0] - x, new_pos[1] - y)
       
  1714             
       
  1715             connections = {}
       
  1716             for name, instance in instances:
       
  1717                 connections.update(instance.updateConnectionsId(translate_id))
       
  1718                 if getattr(instance, "setexecutionOrderId", None) is not None:
       
  1719                     instance.setexecutionOrderId(0)
       
  1720                 instance.translate(*diff)
       
  1721                 element.addinstance(name, instance)
       
  1722             
       
  1723             return new_id, connections
       
  1724                 
  1583     # Return the current pou editing informations
  1725     # Return the current pou editing informations
  1584     def GetEditedElementInstanceInfos(self, tagname, id = None, exclude = [], debug = False):
  1726     def GetEditedElementInstanceInfos(self, tagname, id = None, exclude = [], debug = False):
  1585         infos = {}
  1727         infos = {}
  1586         instance = None
  1728         instance = None
  1587         element = self.GetEditedElement(tagname, debug)
  1729         element = self.GetEditedElement(tagname, debug)