PLCControler.py
changeset 1740 b789b695b5c6
parent 1739 ec153828ded2
child 1741 dd94b9a68c61
equal deleted inserted replaced
1739:ec153828ded2 1740:b789b695b5c6
    94 
    94 
    95 ScriptDirectory = paths.AbsDir(__file__)
    95 ScriptDirectory = paths.AbsDir(__file__)
    96 
    96 
    97 
    97 
    98 def GetUneditableNames():
    98 def GetUneditableNames():
    99     _ = lambda x:x
    99     _ = lambda x: x
   100     return [_("User-defined POUs"), _("Functions"), _("Function Blocks"),
   100     return [_("User-defined POUs"), _("Functions"), _("Function Blocks"),
   101             _("Programs"), _("Data Types"), _("Transitions"), _("Actions"),
   101             _("Programs"), _("Data Types"), _("Transitions"), _("Actions"),
   102             _("Configurations"), _("Resources"), _("Properties")]
   102             _("Configurations"), _("Resources"), _("Properties")]
   103 UNEDITABLE_NAMES = GetUneditableNames()
   103 UNEDITABLE_NAMES = GetUneditableNames()
   104 [USER_DEFINED_POUS, FUNCTIONS, FUNCTION_BLOCKS, PROGRAMS,
   104 [USER_DEFINED_POUS, FUNCTIONS, FUNCTION_BLOCKS, PROGRAMS,
   693     # Return project informations
   693     # Return project informations
   694     def GetProjectInfos(self, debug = False):
   694     def GetProjectInfos(self, debug = False):
   695         project = self.GetProject(debug)
   695         project = self.GetProject(debug)
   696         if project is not None:
   696         if project is not None:
   697             infos = {"name": project.getname(), "type": ITEM_PROJECT}
   697             infos = {"name": project.getname(), "type": ITEM_PROJECT}
   698             datatypes = {"name": DATA_TYPES, "type": ITEM_DATATYPES, "values":[]}
   698             datatypes = {"name": DATA_TYPES, "type": ITEM_DATATYPES, "values": []}
   699             for datatype in project.getdataTypes():
   699             for datatype in project.getdataTypes():
   700                 datatypes["values"].append({"name": datatype.getname(), "type": ITEM_DATATYPE,
   700                 datatypes["values"].append({"name": datatype.getname(), "type": ITEM_DATATYPE,
   701                     "tagname": self.ComputeDataTypeName(datatype.getname()), "values": []})
   701                     "tagname": self.ComputeDataTypeName(datatype.getname()), "values": []})
   702             pou_types = {"function": {"name": FUNCTIONS, "type": ITEM_FUNCTION, "values":[]},
   702             pou_types = {
   703                          "functionBlock": {"name": FUNCTION_BLOCKS, "type": ITEM_FUNCTIONBLOCK, "values":[]},
   703                 "function": {
   704                          "program": {"name": PROGRAMS, "type": ITEM_PROGRAM, "values":[]}}
   704                     "name":   FUNCTIONS,
       
   705                     "type":   ITEM_FUNCTION,
       
   706                     "values": []
       
   707                 },
       
   708                 "functionBlock": {
       
   709                     "name":   FUNCTION_BLOCKS,
       
   710                     "type":   ITEM_FUNCTIONBLOCK,
       
   711                     "values": []
       
   712                 },
       
   713                 "program": {
       
   714                     "name":   PROGRAMS,
       
   715                     "type":   ITEM_PROGRAM,
       
   716                     "values": []
       
   717                 }
       
   718             }
   705             for pou in project.getpous():
   719             for pou in project.getpous():
   706                 pou_type = pou.getpouType()
   720                 pou_type = pou.getpouType()
   707                 pou_infos = {"name": pou.getname(), "type": ITEM_POU,
   721                 pou_infos = {"name": pou.getname(), "type": ITEM_POU,
   708                              "tagname": self.ComputePouName(pou.getname())}
   722                              "tagname": self.ComputePouName(pou.getname())}
   709                 pou_values = []
   723                 pou_values = []
  1542                               for pou in confnodetypes["types"].getpous()]}
  1556                               for pou in confnodetypes["types"].getpous()]}
  1543                      for confnodetypes in typeslist]
  1557                      for confnodetypes in typeslist]
  1544         self.TotalTypes.extend(addedcat)
  1558         self.TotalTypes.extend(addedcat)
  1545         for cat in addedcat:
  1559         for cat in addedcat:
  1546             for desc in cat["list"]:
  1560             for desc in cat["list"]:
  1547                 BlkLst = self.TotalTypesDict.setdefault(desc["name"],[])
  1561                 BlkLst = self.TotalTypesDict.setdefault(desc["name"], [])
  1548                 BlkLst.append((section["name"], desc))
  1562                 BlkLst.append((section["name"], desc))
  1549 
  1563 
  1550     # Function that clear the confnode list
  1564     # Function that clear the confnode list
  1551     def ClearConfNodeTypes(self):
  1565     def ClearConfNodeTypes(self):
  1552         self.ConfNodeTypes = []
  1566         self.ConfNodeTypes = []
  1594         return global_vars
  1608         return global_vars
  1595 
  1609 
  1596     # Function that returns the block definition associated to the block type given
  1610     # Function that returns the block definition associated to the block type given
  1597     def GetBlockType(self, typename, inputs = None, debug = False):
  1611     def GetBlockType(self, typename, inputs = None, debug = False):
  1598         result_blocktype = None
  1612         result_blocktype = None
  1599         for sectioname, blocktype in self.TotalTypesDict.get(typename,[]):
  1613         for sectioname, blocktype in self.TotalTypesDict.get(typename, []):
  1600             if inputs is not None and inputs != "undefined":
  1614             if inputs is not None and inputs != "undefined":
  1601                 block_inputs = tuple([var_type for name, var_type, modifier in blocktype["inputs"]])
  1615                 block_inputs = tuple([var_type for name, var_type, modifier in blocktype["inputs"]])
  1602                 if reduce(lambda x, y: x and y, map(lambda x: x[0] == "ANY" or self.IsOfType(*x), zip(inputs, block_inputs)), True):
  1616                 if reduce(lambda x, y: x and y, map(lambda x: x[0] == "ANY" or self.IsOfType(*x), zip(inputs, block_inputs)), True):
  1603                     return blocktype
  1617                     return blocktype
  1604             else:
  1618             else:
  1632         words = tagname.split("::")
  1646         words = tagname.split("::")
  1633         name = None
  1647         name = None
  1634         project = self.GetProject(debug)
  1648         project = self.GetProject(debug)
  1635         if project is not None:
  1649         if project is not None:
  1636             pou_type = None
  1650             pou_type = None
  1637             if words[0] in ["P","T","A"]:
  1651             if words[0] in ["P", "T", "A"]:
  1638                 name = words[1]
  1652                 name = words[1]
  1639                 pou_type = self.GetPouType(name, debug)
  1653                 pou_type = self.GetPouType(name, debug)
  1640             filter = (["function"]
  1654             filter = (["function"]
  1641                       if pou_type == "function" or words[0] == "T"
  1655                       if pou_type == "function" or words[0] == "T"
  1642                       else ["functionBlock", "function"])
  1656                       else ["functionBlock", "function"])
  1656     # Return Function Block types checking for recursion
  1670     # Return Function Block types checking for recursion
  1657     def GetFunctionBlockTypes(self, tagname = "", debug = False):
  1671     def GetFunctionBlockTypes(self, tagname = "", debug = False):
  1658         project = self.GetProject(debug)
  1672         project = self.GetProject(debug)
  1659         words = tagname.split("::")
  1673         words = tagname.split("::")
  1660         name = None
  1674         name = None
  1661         if project is not None and words[0] in ["P","T","A"]:
  1675         if project is not None and words[0] in ["P", "T", "A"]:
  1662             name = words[1]
  1676             name = words[1]
  1663         blocktypes = []
  1677         blocktypes = []
  1664         for blocks in self.TotalTypesDict.itervalues():
  1678         for blocks in self.TotalTypesDict.itervalues():
  1665             for sectioname,block in blocks:
  1679             for sectioname, block in blocks:
  1666                 if block["type"] == "functionBlock":
  1680                 if block["type"] == "functionBlock":
  1667                     blocktypes.append(block["name"])
  1681                     blocktypes.append(block["name"])
  1668         if project is not None:
  1682         if project is not None:
  1669             blocktypes.extend([pou.getname()
  1683             blocktypes.extend([pou.getname()
  1670                 for pou in project.getpous(name, ["functionBlock"])
  1684                 for pou in project.getpous(name, ["functionBlock"])
  1771         '''
  1785         '''
  1772         return the list of datatypes defined in IEC 61131-3.
  1786         return the list of datatypes defined in IEC 61131-3.
  1773         TypeHierarchy_list has a rough order to it (e.g. SINT, INT, DINT, ...),
  1787         TypeHierarchy_list has a rough order to it (e.g. SINT, INT, DINT, ...),
  1774         which makes it easy for a user to find a type in a menu.
  1788         which makes it easy for a user to find a type in a menu.
  1775         '''
  1789         '''
  1776         return [x for x,y in TypeHierarchy_list if not x.startswith("ANY")]
  1790         return [x for x, y in TypeHierarchy_list if not x.startswith("ANY")]
  1777 
  1791 
  1778     def IsOfType(self, typename, reference, debug = False):
  1792     def IsOfType(self, typename, reference, debug = False):
  1779         if reference is None or typename == reference:
  1793         if reference is None or typename == reference:
  1780             return True
  1794             return True
  1781 
  1795 
  2172         return None
  2186         return None
  2173 
  2187 
  2174     # Return edited element name
  2188     # Return edited element name
  2175     def GetEditedElementName(self, tagname):
  2189     def GetEditedElementName(self, tagname):
  2176         words = tagname.split("::")
  2190         words = tagname.split("::")
  2177         if words[0] in ["P","C","D"]:
  2191         if words[0] in ["P", "C", "D"]:
  2178             return words[1]
  2192             return words[1]
  2179         else:
  2193         else:
  2180             return words[2]
  2194             return words[2]
  2181         return None
  2195         return None
  2182 
  2196 
  2183     # Return edited element name and type
  2197     # Return edited element name and type
  2184     def GetEditedElementType(self, tagname, debug = False):
  2198     def GetEditedElementType(self, tagname, debug = False):
  2185         words = tagname.split("::")
  2199         words = tagname.split("::")
  2186         if words[0] in ["P","T","A"]:
  2200         if words[0] in ["P", "T", "A"]:
  2187             return words[1], self.GetPouType(words[1], debug)
  2201             return words[1], self.GetPouType(words[1], debug)
  2188         return None, None
  2202         return None, None
  2189 
  2203 
  2190     # Return language in which edited element is written
  2204     # Return language in which edited element is written
  2191     def GetEditedElementBodyType(self, tagname, debug = False):
  2205     def GetEditedElementBodyType(self, tagname, debug = False):
  2199         return None
  2213         return None
  2200 
  2214 
  2201     # Return the edited element variables
  2215     # Return the edited element variables
  2202     def GetEditedElementInterfaceVars(self, tagname, tree=False, debug = False):
  2216     def GetEditedElementInterfaceVars(self, tagname, tree=False, debug = False):
  2203         words = tagname.split("::")
  2217         words = tagname.split("::")
  2204         if words[0] in ["P","T","A"]:
  2218         if words[0] in ["P", "T", "A"]:
  2205             project = self.GetProject(debug)
  2219             project = self.GetProject(debug)
  2206             if project is not None:
  2220             if project is not None:
  2207                 pou = project.getpou(words[1])
  2221                 pou = project.getpou(words[1])
  2208                 if pou is not None:
  2222                 if pou is not None:
  2209                     return self.GetPouInterfaceVars(pou, tree, debug)
  2223                     return self.GetPouInterfaceVars(pou, tree, debug)
  2257         return []
  2271         return []
  2258 
  2272 
  2259     # Return the names of the pou elements
  2273     # Return the names of the pou elements
  2260     def GetEditedElementVariables(self, tagname, debug = False):
  2274     def GetEditedElementVariables(self, tagname, debug = False):
  2261         words = tagname.split("::")
  2275         words = tagname.split("::")
  2262         if words[0] in ["P","T","A"]:
  2276         if words[0] in ["P", "T", "A"]:
  2263             return self.GetProjectPouVariableNames(words[1], debug)
  2277             return self.GetProjectPouVariableNames(words[1], debug)
  2264         elif words[0] in ["C", "R"]:
  2278         elif words[0] in ["C", "R"]:
  2265             names = self.GetConfigurationVariableNames(words[1], debug)
  2279             names = self.GetConfigurationVariableNames(words[1], debug)
  2266             if words[0] == "R":
  2280             if words[0] == "R":
  2267                 names.extend(self.GetConfigurationResourceVariableNames(
  2281                 names.extend(self.GetConfigurationResourceVariableNames(
  2300         names = exclude.copy()
  2314         names = exclude.copy()
  2301         if tagname is not None:
  2315         if tagname is not None:
  2302             names.update(dict([(varname.upper(), True)
  2316             names.update(dict([(varname.upper(), True)
  2303                                for varname in self.GetEditedElementVariables(tagname, debug)]))
  2317                                for varname in self.GetEditedElementVariables(tagname, debug)]))
  2304             words = tagname.split("::")
  2318             words = tagname.split("::")
  2305             if words[0] in ["P","T","A"]:
  2319             if words[0] in ["P", "T", "A"]:
  2306                 element = self.GetEditedElement(tagname, debug)
  2320                 element = self.GetEditedElement(tagname, debug)
  2307                 if element is not None and element.getbodyType() not in ["ST", "IL"]:
  2321                 if element is not None and element.getbodyType() not in ["ST", "IL"]:
  2308                     for instance in element.getinstances():
  2322                     for instance in element.getinstances():
  2309                         if isinstance(instance,
  2323                         if isinstance(instance,
  2310                             (PLCOpenParser.GetElementClass("step", "sfcObjects"),
  2324                             (PLCOpenParser.GetElementClass("step", "sfcObjects"),
  2492             derived_type = PLCOpenParser.CreateElement("derived", "dataType")
  2506             derived_type = PLCOpenParser.CreateElement("derived", "dataType")
  2493             derived_type.setname(var_type)
  2507             derived_type.setname(var_type)
  2494             var_type_obj.setcontent(derived_type)
  2508             var_type_obj.setcontent(derived_type)
  2495         return var_type_obj
  2509         return var_type_obj
  2496 
  2510 
  2497     def AddEditedElementPouVar(self, tagname, var_type, name,**args):
  2511     def AddEditedElementPouVar(self, tagname, var_type, name, **args):
  2498         if self.Project is not None:
  2512         if self.Project is not None:
  2499             words = tagname.split("::")
  2513             words = tagname.split("::")
  2500             if words[0] in ['P', 'T', 'A']:
  2514             if words[0] in ['P', 'T', 'A']:
  2501                 pou = self.Project.getpou(words[1])
  2515                 pou = self.Project.getpou(words[1])
  2502                 if pou is not None:
  2516                 if pou is not None: