PLCControler.py
changeset 1847 6198190bc121
parent 1846 14b40afccd69
child 1850 614396cbffbf
equal deleted inserted replaced
1846:14b40afccd69 1847:6198190bc121
  1169     def ChangePouVariableName(self, pou_name, old_name, new_name):
  1169     def ChangePouVariableName(self, pou_name, old_name, new_name):
  1170         if self.Project is not None:
  1170         if self.Project is not None:
  1171             # Found the pou action corresponding to old name and change its name to new name
  1171             # Found the pou action corresponding to old name and change its name to new name
  1172             pou = self.Project.getpou(pou_name)
  1172             pou = self.Project.getpou(pou_name)
  1173             if pou is not None:
  1173             if pou is not None:
  1174                 for type, varlist in pou.getvars():
  1174                 for _type, varlist in pou.getvars():
  1175                     for var in varlist.getvariable():
  1175                     for var in varlist.getvariable():
  1176                         if var.getname() == old_name:
  1176                         if var.getname() == old_name:
  1177                             var.setname(new_name)
  1177                             var.setname(new_name)
  1178                 self.BufferProject()
  1178                 self.BufferProject()
  1179 
  1179 
  1326             tempvar.setname(var.Name)
  1326             tempvar.setname(var.Name)
  1327 
  1327 
  1328             var_type = PLCOpenParser.CreateElement("type", "variable")
  1328             var_type = PLCOpenParser.CreateElement("type", "variable")
  1329             if isinstance(var.Type, TupleType):
  1329             if isinstance(var.Type, TupleType):
  1330                 if var.Type[0] == "array":
  1330                 if var.Type[0] == "array":
  1331                     array_type, base_type_name, dimensions = var.Type
  1331                     _array_type, base_type_name, dimensions = var.Type
  1332                     array = PLCOpenParser.CreateElement("array", "dataType")
  1332                     array = PLCOpenParser.CreateElement("array", "dataType")
  1333                     baseType = PLCOpenParser.CreateElement("baseType", "array")
  1333                     baseType = PLCOpenParser.CreateElement("baseType", "array")
  1334                     array.setbaseType(baseType)
  1334                     array.setbaseType(baseType)
  1335                     for i, dimension in enumerate(dimensions):
  1335                     for i, dimension in enumerate(dimensions):
  1336                         dimension_range = PLCOpenParser.CreateElement("dimension", "array")
  1336                         dimension_range = PLCOpenParser.CreateElement("dimension", "array")
  1415             # Found the configuration corresponding to name
  1415             # Found the configuration corresponding to name
  1416             configuration = self.Project.getconfiguration(name)
  1416             configuration = self.Project.getconfiguration(name)
  1417             if configuration is not None:
  1417             if configuration is not None:
  1418                 # Set configuration global vars
  1418                 # Set configuration global vars
  1419                 configuration.setglobalVars([
  1419                 configuration.setglobalVars([
  1420                     varlist for vartype, varlist
  1420                     varlist for _vartype, varlist
  1421                     in self.ExtractVarLists(vars)])
  1421                     in self.ExtractVarLists(vars)])
  1422 
  1422 
  1423     # Return the configuration globalvars
  1423     # Return the configuration globalvars
  1424     def GetConfigurationGlobalVars(self, name, debug=False):
  1424     def GetConfigurationGlobalVars(self, name, debug=False):
  1425         project = self.GetProject(debug)
  1425         project = self.GetProject(debug)
  1453             # Found the resource corresponding to name
  1453             # Found the resource corresponding to name
  1454             resource = self.Project.getconfigurationResource(config_name, name)
  1454             resource = self.Project.getconfigurationResource(config_name, name)
  1455             # Set resource global vars
  1455             # Set resource global vars
  1456             if resource is not None:
  1456             if resource is not None:
  1457                 resource.setglobalVars([
  1457                 resource.setglobalVars([
  1458                     varlist for vartype, varlist
  1458                     varlist for _vartype, varlist
  1459                     in self.ExtractVarLists(vars)])
  1459                     in self.ExtractVarLists(vars)])
  1460 
  1460 
  1461     # Return the resource globalvars
  1461     # Return the resource globalvars
  1462     def GetConfigurationResourceGlobalVars(self, config_name, name, debug=False):
  1462     def GetConfigurationResourceGlobalVars(self, config_name, name, debug=False):
  1463         project = self.GetProject(debug)
  1463         project = self.GetProject(debug)
  1504             pou = self.Project.getpou(name)
  1504             pou = self.Project.getpou(name)
  1505             if pou is not None:
  1505             if pou is not None:
  1506                 if pou.interface is None:
  1506                 if pou.interface is None:
  1507                     pou.interface = PLCOpenParser.CreateElement("interface", "pou")
  1507                     pou.interface = PLCOpenParser.CreateElement("interface", "pou")
  1508                 # Set Pou interface
  1508                 # Set Pou interface
  1509                 pou.setvars([varlist for varlist_type, varlist in self.ExtractVarLists(vars)])
  1509                 pou.setvars([varlist for _varlist_type, varlist in self.ExtractVarLists(vars)])
  1510 
  1510 
  1511     # Replace the return type of the pou given by its name (only for functions)
  1511     # Replace the return type of the pou given by its name (only for functions)
  1512     def SetPouInterfaceReturnType(self, name, return_type):
  1512     def SetPouInterfaceReturnType(self, name, return_type):
  1513         if self.Project is not None:
  1513         if self.Project is not None:
  1514             pou = self.Project.getpou(name)
  1514             pou = self.Project.getpou(name)
  1629         return global_vars
  1629         return global_vars
  1630 
  1630 
  1631     # Function that returns the block definition associated to the block type given
  1631     # Function that returns the block definition associated to the block type given
  1632     def GetBlockType(self, typename, inputs=None, debug=False):
  1632     def GetBlockType(self, typename, inputs=None, debug=False):
  1633         result_blocktype = None
  1633         result_blocktype = None
  1634         for sectioname, blocktype in self.TotalTypesDict.get(typename, []):
  1634         for _sectioname, blocktype in self.TotalTypesDict.get(typename, []):
  1635             if inputs is not None and inputs != "undefined":
  1635             if inputs is not None and inputs != "undefined":
  1636                 block_inputs = tuple([var_type for name, var_type, modifier in blocktype["inputs"]])
  1636                 block_inputs = tuple([var_type for _name, var_type, _modifier in blocktype["inputs"]])
  1637                 if reduce(lambda x, y: x and y, map(lambda x: x[0] == "ANY" or self.IsOfType(*x), zip(inputs, block_inputs)), True):
  1637                 if reduce(lambda x, y: x and y, map(lambda x: x[0] == "ANY" or self.IsOfType(*x), zip(inputs, block_inputs)), True):
  1638                     return blocktype
  1638                     return blocktype
  1639             else:
  1639             else:
  1640                 if result_blocktype is not None:
  1640                 if result_blocktype is not None:
  1641                     if inputs == "undefined":
  1641                     if inputs == "undefined":
  1654                 blocktype_infos = blocktype.getblockInfos()
  1654                 blocktype_infos = blocktype.getblockInfos()
  1655                 if inputs in [None, "undefined"]:
  1655                 if inputs in [None, "undefined"]:
  1656                     return blocktype_infos
  1656                     return blocktype_infos
  1657 
  1657 
  1658                 if inputs == tuple([var_type
  1658                 if inputs == tuple([var_type
  1659                                     for name, var_type, modifier in blocktype_infos["inputs"]]):
  1659                                     for _name, var_type, _modifier in blocktype_infos["inputs"]]):
  1660                     return blocktype_infos
  1660                     return blocktype_infos
  1661 
  1661 
  1662         return None
  1662         return None
  1663 
  1663 
  1664     # Return Block types checking for recursion
  1664     # Return Block types checking for recursion
  1696         name = None
  1696         name = None
  1697         if project is not None and words[0] in ["P", "T", "A"]:
  1697         if project is not None and words[0] in ["P", "T", "A"]:
  1698             name = words[1]
  1698             name = words[1]
  1699         blocktypes = []
  1699         blocktypes = []
  1700         for blocks in self.TotalTypesDict.itervalues():
  1700         for blocks in self.TotalTypesDict.itervalues():
  1701             for sectioname, block in blocks:
  1701             for _sectioname, block in blocks:
  1702                 if block["type"] == "functionBlock":
  1702                 if block["type"] == "functionBlock":
  1703                     blocktypes.append(block["name"])
  1703                     blocktypes.append(block["name"])
  1704         if project is not None:
  1704         if project is not None:
  1705             blocktypes.extend([
  1705             blocktypes.extend([
  1706                 pou.getname()
  1706                 pou.getname()
  1805         '''
  1805         '''
  1806         return the list of datatypes defined in IEC 61131-3.
  1806         return the list of datatypes defined in IEC 61131-3.
  1807         TypeHierarchy_list has a rough order to it (e.g. SINT, INT, DINT, ...),
  1807         TypeHierarchy_list has a rough order to it (e.g. SINT, INT, DINT, ...),
  1808         which makes it easy for a user to find a type in a menu.
  1808         which makes it easy for a user to find a type in a menu.
  1809         '''
  1809         '''
  1810         return [x for x, y in TypeHierarchy_list if not x.startswith("ANY")]
  1810         return [x for x, _y in TypeHierarchy_list if not x.startswith("ANY")]
  1811 
  1811 
  1812     def IsOfType(self, typename, reference, debug=False):
  1812     def IsOfType(self, typename, reference, debug=False):
  1813         if reference is None or typename == reference:
  1813         if reference is None or typename == reference:
  1814             return True
  1814             return True
  1815 
  1815 
  2129                     element = PLCOpenParser.CreateElement("variable", "struct")
  2129                     element = PLCOpenParser.CreateElement("variable", "struct")
  2130                     element.setname(element_infos["Name"])
  2130                     element.setname(element_infos["Name"])
  2131                     element_type = PLCOpenParser.CreateElement("type", "variable")
  2131                     element_type = PLCOpenParser.CreateElement("type", "variable")
  2132                     if isinstance(element_infos["Type"], TupleType):
  2132                     if isinstance(element_infos["Type"], TupleType):
  2133                         if element_infos["Type"][0] == "array":
  2133                         if element_infos["Type"][0] == "array":
  2134                             array_type, base_type_name, dimensions = element_infos["Type"]
  2134                             _array_type, base_type_name, dimensions = element_infos["Type"]
  2135                             array = PLCOpenParser.CreateElement("array", "dataType")
  2135                             array = PLCOpenParser.CreateElement("array", "dataType")
  2136                             baseType = PLCOpenParser.CreateElement("baseType", "array")
  2136                             baseType = PLCOpenParser.CreateElement("baseType", "array")
  2137                             array.setbaseType(baseType)
  2137                             array.setbaseType(baseType)
  2138                             element_type.setcontent(array)
  2138                             element_type.setcontent(array)
  2139                             for j, dimension in enumerate(dimensions):
  2139                             for j, dimension in enumerate(dimensions):
  2371             i += 1
  2371             i += 1
  2372         return name
  2372         return name
  2373 
  2373 
  2374     def PasteEditedElementInstances(self, tagname, text, new_pos, middle=False, debug=False):
  2374     def PasteEditedElementInstances(self, tagname, text, new_pos, middle=False, debug=False):
  2375         element = self.GetEditedElement(tagname, debug)
  2375         element = self.GetEditedElement(tagname, debug)
  2376         element_name, element_type = self.GetEditedElementType(tagname, debug)
  2376         _element_name, element_type = self.GetEditedElementType(tagname, debug)
  2377         if element is not None:
  2377         if element is not None:
  2378             bodytype = element.getbodyType()
  2378             bodytype = element.getbodyType()
  2379 
  2379 
  2380             # Get edited element type scaling
  2380             # Get edited element type scaling
  2381             scaling = None
  2381             scaling = None