PLCControler.py
branch1.1 Korean release
changeset 1280 72a826dfcfbb
parent 1265 242512c56ea1
child 1283 f3cfe1ff917e
equal deleted inserted replaced
977:c8e008b8cefe 1280:72a826dfcfbb
   264         project = self.GetProject(debug)
   264         project = self.GetProject(debug)
   265         if project is not None:
   265         if project is not None:
   266             return [config.getname() for config in project.getconfigurations()]
   266             return [config.getname() for config in project.getconfigurations()]
   267         return []
   267         return []
   268     
   268     
   269     # Return project pou variables
   269     # Return project pou variable names
   270     def GetProjectPouVariables(self, pou_name = None, debug = False):
   270     def GetProjectPouVariableNames(self, pou_name = None, debug = False):
   271         variables = []
   271         variables = []
   272         project = self.GetProject(debug)
   272         project = self.GetProject(debug)
   273         if project is not None:
   273         if project is not None:
   274             for pou in project.getpous():
   274             for pou in project.getpous():
   275                 if pou_name is None or pou_name == pou.getname():
   275                 if pou_name is None or pou_name == pou.getname():
   559                     var_type = vartype_content["value"].getname()
   559                     var_type = vartype_content["value"].getname()
   560                     if var_type == pou_type:
   560                     if var_type == pou_type:
   561                         instances.append(var_path)
   561                         instances.append(var_path)
   562                     else:
   562                     else:
   563                         pou = project.getpou(var_type)
   563                         pou = project.getpou(var_type)
   564                         if pou is not None:
   564                         if pou is not None and project.ElementIsUsedBy(pou_type, var_type):
   565                             instances.extend(
   565                             instances.extend(
   566                                 self.RecursiveSearchPouInstances(
   566                                 self.RecursiveSearchPouInstances(
   567                                     project, pou_type, var_path, 
   567                                     project, pou_type, var_path, 
   568                                     [varlist for type, varlist in pou.getvars()], 
   568                                     [varlist for type, varlist in pou.getvars()], 
   569                                     debug))
   569                                     debug))
   594                             pou_path = "%s.%s" % (res_path, pou_instance.getname())
   594                             pou_path = "%s.%s" % (res_path, pou_instance.getname())
   595                             pou_type = pou_instance.gettypeName()
   595                             pou_type = pou_instance.gettypeName()
   596                             if pou_type == words[1]:
   596                             if pou_type == words[1]:
   597                                 instances.append(pou_path)
   597                                 instances.append(pou_path)
   598                             pou = project.getpou(pou_type)
   598                             pou = project.getpou(pou_type)
   599                             if pou is not None:
   599                             if pou is not None and project.ElementIsUsedBy(words[1], pou_type):
   600                                 instances.extend(
   600                                 instances.extend(
   601                                     self.RecursiveSearchPouInstances(
   601                                     self.RecursiveSearchPouInstances(
   602                                         project, words[1], pou_path, 
   602                                         project, words[1], pou_path, 
   603                                         [varlist for type, varlist in pou.getvars()], 
   603                                         [varlist for type, varlist in pou.getvars()], 
   604                                         debug))
   604                                         debug))
   647                 for varname, vartype, varmodifier in block_infos["inputs"] + block_infos["outputs"]:
   647                 for varname, vartype, varmodifier in block_infos["inputs"] + block_infos["outputs"]:
   648                     if varname == parts[0]:
   648                     if varname == parts[0]:
   649                         return self.RecursiveGetPouInstanceTagName(project, vartype, parts[1:], debug)
   649                         return self.RecursiveGetPouInstanceTagName(project, vartype, parts[1:], debug)
   650         return None
   650         return None
   651     
   651     
       
   652     def GetGlobalInstanceTagName(self, project, element, parts, debug = False):
       
   653         for varlist in element.getglobalVars():
       
   654             for variable in varlist.getvariable():
       
   655                 if variable.getname() == parts[0]:
       
   656                     vartype_content = variable.gettype().getcontent()
       
   657                     if vartype_content["name"] == "derived":                        
       
   658                         if len(parts) == 1:
       
   659                             return self.ComputePouName(
       
   660                                         vartype_content["value"].getname())
       
   661                         else:
       
   662                             return self.RecursiveGetPouInstanceTagName(
       
   663                                         project, 
       
   664                                         vartype_content["value"].getname(),
       
   665                                         parts[1:], debug)
       
   666         return None
       
   667     
   652     def GetPouInstanceTagName(self, instance_path, debug = False):
   668     def GetPouInstanceTagName(self, instance_path, debug = False):
       
   669         project = self.GetProject(debug)
   653         parts = instance_path.split(".")
   670         parts = instance_path.split(".")
   654         if len(parts) == 1:
   671         if len(parts) == 1:
   655             return self.ComputeConfigurationName(parts[0])
   672             return self.ComputeConfigurationName(parts[0])
   656         elif len(parts) == 2:
   673         elif len(parts) == 2:
       
   674             for config in project.getconfigurations():
       
   675                 if config.getname() == parts[0]:
       
   676                     result = self.GetGlobalInstanceTagName(project, 
       
   677                                                            config, 
       
   678                                                            parts[1:],
       
   679                                                            debug)
       
   680                     if result is not None:
       
   681                         return result
   657             return self.ComputeConfigurationResourceName(parts[0], parts[1])
   682             return self.ComputeConfigurationResourceName(parts[0], parts[1])
   658         else:
   683         else:
   659             project = self.GetProject(debug)
       
   660             for config in project.getconfigurations():
   684             for config in project.getconfigurations():
   661                 if config.getname() == parts[0]:
   685                 if config.getname() == parts[0]:
   662                     for resource in config.getresource():
   686                     for resource in config.getresource():
   663                         if resource.getname() == parts[1]:
   687                         if resource.getname() == parts[1]:
   664                             pou_instances = resource.getpouInstance()[:]
   688                             pou_instances = resource.getpouInstance()[:]
   672                                     else:
   696                                     else:
   673                                         return self.RecursiveGetPouInstanceTagName(
   697                                         return self.RecursiveGetPouInstanceTagName(
   674                                                     project,
   698                                                     project,
   675                                                     pou_instance.gettypeName(),
   699                                                     pou_instance.gettypeName(),
   676                                                     parts[3:], debug)
   700                                                     parts[3:], debug)
       
   701                             return self.GetGlobalInstanceTagName(project, 
       
   702                                                                  resource, 
       
   703                                                                  parts[2:], 
       
   704                                                                  debug)
       
   705                     return self.GetGlobalInstanceTagName(project, 
       
   706                                                          config, 
       
   707                                                          parts[1:],
       
   708                                                          debug)
   677         return None
   709         return None
   678     
   710     
   679     def GetInstanceInfos(self, instance_path, debug = False):
   711     def GetInstanceInfos(self, instance_path, debug = False):
   680         tagname = self.GetPouInstanceTagName(instance_path)
   712         tagname = self.GetPouInstanceTagName(instance_path)
   681         if tagname is not None:
   713         if tagname is not None:
  1259             tempvar["Documentation"] = doc.gettext()
  1291             tempvar["Documentation"] = doc.gettext()
  1260         else:
  1292         else:
  1261             tempvar["Documentation"] = ""
  1293             tempvar["Documentation"] = ""
  1262 
  1294 
  1263         return tempvar
  1295         return tempvar
       
  1296     
       
  1297     # Add a global var to configuration to configuration
       
  1298     def AddConfigurationGlobalVar(self, config_name, type, var_name, 
       
  1299                                            location="", description=""):
       
  1300         if self.Project is not None:
       
  1301             # Found the configuration corresponding to name
       
  1302             configuration = self.Project.getconfiguration(config_name)
       
  1303             if configuration is not None:
       
  1304                 # Set configuration global vars
       
  1305                 configuration.addglobalVar(type, var_name, location, description)
  1264 
  1306 
  1265     # Replace the configuration globalvars by those given
  1307     # Replace the configuration globalvars by those given
  1266     def SetConfigurationGlobalVars(self, name, vars):
  1308     def SetConfigurationGlobalVars(self, name, vars):
  1267         if self.Project is not None:
  1309         if self.Project is not None:
  1268             # Found the configuration corresponding to name
  1310             # Found the configuration corresponding to name
  1287                         tempvar = self.GetVariableDictionary(varlist, var)
  1329                         tempvar = self.GetVariableDictionary(varlist, var)
  1288                         tempvar["Class"] = "Global"
  1330                         tempvar["Class"] = "Global"
  1289                         vars.append(tempvar)
  1331                         vars.append(tempvar)
  1290         return vars
  1332         return vars
  1291 
  1333 
       
  1334     # Return configuration variable names
       
  1335     def GetConfigurationVariableNames(self, config_name = None, debug = False):
       
  1336         variables = []
       
  1337         project = self.GetProject(debug)
       
  1338         if project is not None:
       
  1339             for configuration in self.Project.getconfigurations():
       
  1340                 if config_name is None or config_name == configuration.getname():
       
  1341                     variables.extend(
       
  1342                         [var.getname() for var in reduce(
       
  1343                             lambda x, y: x + y, [varlist.getvariable() 
       
  1344                                 for varlist in configuration.globalVars],
       
  1345                             [])])
       
  1346         return variables
       
  1347 
  1292     # Replace the resource globalvars by those given
  1348     # Replace the resource globalvars by those given
  1293     def SetConfigurationResourceGlobalVars(self, config_name, name, vars):
  1349     def SetConfigurationResourceGlobalVars(self, config_name, name, vars):
  1294         if self.Project is not None:
  1350         if self.Project is not None:
  1295             # Found the resource corresponding to name
  1351             # Found the resource corresponding to name
  1296             resource = self.Project.getconfigurationResource(config_name, name)
  1352             resource = self.Project.getconfigurationResource(config_name, name)
  1313                     for var in varlist.getvariable():
  1369                     for var in varlist.getvariable():
  1314                         tempvar = self.GetVariableDictionary(varlist, var)
  1370                         tempvar = self.GetVariableDictionary(varlist, var)
  1315                         tempvar["Class"] = "Global"
  1371                         tempvar["Class"] = "Global"
  1316                         vars.append(tempvar)
  1372                         vars.append(tempvar)
  1317         return vars
  1373         return vars
       
  1374     
       
  1375     # Return resource variable names
       
  1376     def GetConfigurationResourceVariableNames(self, 
       
  1377                 config_name = None, resource_name = None, debug = False):
       
  1378         variables = []
       
  1379         project = self.GetProject(debug)
       
  1380         if project is not None:
       
  1381             for configuration in self.Project.getconfigurations():
       
  1382                 if config_name is None or config_name == configuration.getname():
       
  1383                     for resource in configuration.getresource():
       
  1384                         if resource_name is None or resource.getname() == resource_name:
       
  1385                             variables.extend(
       
  1386                                 [var.getname() for var in reduce(
       
  1387                                     lambda x, y: x + y, [varlist.getvariable() 
       
  1388                                         for varlist in resource.globalVars],
       
  1389                                     [])])
       
  1390         return variables
  1318     
  1391     
  1319     # Recursively generate element name tree for a structured variable
  1392     # Recursively generate element name tree for a structured variable
  1320     def GenerateVarTree(self, typename, debug = False):
  1393     def GenerateVarTree(self, typename, debug = False):
  1321         project = self.GetProject(debug)
  1394         project = self.GetProject(debug)
  1322         if project is not None:
  1395         if project is not None:
  1490     def GetConfNodeGlobalInstances(self):
  1563     def GetConfNodeGlobalInstances(self):
  1491         return []
  1564         return []
  1492 
  1565 
  1493     def GetConfigurationExtraVariables(self):
  1566     def GetConfigurationExtraVariables(self):
  1494         global_vars = []
  1567         global_vars = []
  1495         for var_name, var_type in self.GetConfNodeGlobalInstances():
  1568         for var_name, var_type, var_initial in self.GetConfNodeGlobalInstances():
  1496             tempvar = plcopen.varListPlain_variable()
  1569             tempvar = plcopen.varListPlain_variable()
  1497             tempvar.setname(var_name)
  1570             tempvar.setname(var_name)
  1498             
  1571             
  1499             tempvartype = plcopen.dataType()
  1572             tempvartype = plcopen.dataType()
  1500             if var_type in self.GetBaseTypes():
  1573             if var_type in self.GetBaseTypes():
  1501                 if var_type == "STRING":
  1574                 if var_type == "STRING":
  1502                     var_type.setcontent({"name" : "string", "value" : plcopen.elementaryTypes_string()})
  1575                     tempvartype.setcontent({"name" : "string", "value" : plcopen.elementaryTypes_string()})
  1503                 elif var_type == "WSTRING":
  1576                 elif var_type == "WSTRING":
  1504                     var_type.setcontent({"name" : "wstring", "value" : plcopen.elementaryTypes_wstring()})
  1577                     tempvartype.setcontent({"name" : "wstring", "value" : plcopen.elementaryTypes_wstring()})
  1505                 else:
  1578                 else:
  1506                     var_type.setcontent({"name" : var_type, "value" : None})
  1579                     tempvartype.setcontent({"name" : var_type, "value" : None})
  1507             else:
  1580             else:
  1508                 tempderivedtype = plcopen.derivedTypes_derived()
  1581                 tempderivedtype = plcopen.derivedTypes_derived()
  1509                 tempderivedtype.setname(var_type)
  1582                 tempderivedtype.setname(var_type)
  1510                 tempvartype.setcontent({"name" : "derived", "value" : tempderivedtype})
  1583                 tempvartype.setcontent({"name" : "derived", "value" : tempderivedtype})
  1511             tempvar.settype(tempvartype)
  1584             tempvar.settype(tempvartype)
       
  1585             
       
  1586             if var_initial != "":
       
  1587                 value = plcopen.value()
       
  1588                 value.setvalue(var_initial)
       
  1589                 tempvar.setinitialValue(value)
  1512             
  1590             
  1513             global_vars.append(tempvar)
  1591             global_vars.append(tempvar)
  1514         return global_vars
  1592         return global_vars
  1515 
  1593 
  1516     # Function that returns the block definition associated to the block type given
  1594     # Function that returns the block definition associated to the block type given
  1529                                 return None
  1607                                 return None
  1530                             else:
  1608                             else:
  1531                                 result_blocktype["inputs"] = [(i[0], "ANY", i[2]) for i in result_blocktype["inputs"]]
  1609                                 result_blocktype["inputs"] = [(i[0], "ANY", i[2]) for i in result_blocktype["inputs"]]
  1532                                 result_blocktype["outputs"] = [(o[0], "ANY", o[2]) for o in result_blocktype["outputs"]]
  1610                                 result_blocktype["outputs"] = [(o[0], "ANY", o[2]) for o in result_blocktype["outputs"]]
  1533                                 return result_blocktype
  1611                                 return result_blocktype
  1534                         result_blocktype = blocktype
  1612                         result_blocktype = blocktype.copy()
  1535         if result_blocktype is not None:
  1613         if result_blocktype is not None:
  1536             return result_blocktype
  1614             return result_blocktype
  1537         project = self.GetProject(debug)
  1615         project = self.GetProject(debug)
  1538         if project is not None:
  1616         if project is not None:
  1539             return project.GetCustomBlockType(type, inputs)
  1617             return project.GetCustomBlockType(type, inputs)
  2069 
  2147 
  2070     # Return the names of the pou elements
  2148     # Return the names of the pou elements
  2071     def GetEditedElementVariables(self, tagname, debug = False):
  2149     def GetEditedElementVariables(self, tagname, debug = False):
  2072         words = tagname.split("::")
  2150         words = tagname.split("::")
  2073         if words[0] in ["P","T","A"]:
  2151         if words[0] in ["P","T","A"]:
  2074             return self.GetProjectPouVariables(words[1], debug)
  2152             return self.GetProjectPouVariableNames(words[1], debug)
       
  2153         elif words[0] in ["C", "R"]:
       
  2154             names = self.GetConfigurationVariableNames(words[1], debug)
       
  2155             if words[0] == "R":
       
  2156                 names.extend(self.GetConfigurationResourceVariableNames(
       
  2157                     words[1], words[2], debug))
       
  2158             return names
  2075         return []
  2159         return []
  2076 
  2160 
  2077     def GetEditedElementCopy(self, tagname, debug = False):
  2161     def GetEditedElementCopy(self, tagname, debug = False):
  2078         element = self.GetEditedElement(tagname, debug)
  2162         element = self.GetEditedElement(tagname, debug)
  2079         if element is not None:
  2163         if element is not None:
  2093                     instance_copy.filterConnections(wires)
  2177                     instance_copy.filterConnections(wires)
  2094                     name = instance_copy.__class__.__name__
  2178                     name = instance_copy.__class__.__name__
  2095                     text += instance_copy.generateXMLText(name.split("_")[-1], 0)
  2179                     text += instance_copy.generateXMLText(name.split("_")[-1], 0)
  2096         return text
  2180         return text
  2097     
  2181     
  2098     def GenerateNewName(self, tagname, name, format, exclude={}, debug=False):
  2182     def GenerateNewName(self, tagname, name, format, start_idx=0, exclude={}, debug=False):
  2099         names = exclude.copy()
  2183         names = exclude.copy()
  2100         if tagname is not None:
  2184         if tagname is not None:
  2101             names.update(dict([(varname.upper(), True) for varname in self.GetEditedElementVariables(tagname, debug)]))
  2185             names.update(dict([(varname.upper(), True) 
  2102             element = self.GetEditedElement(tagname, debug)
  2186                                for varname in self.GetEditedElementVariables(tagname, debug)]))
  2103             if element is not None:
  2187             words = tagname.split("::")
  2104                 for instance in element.getinstances():
  2188             if words[0] in ["P","T","A"]:
  2105                     if isinstance(instance, (plcopen.sfcObjects_step, plcopen.commonObjects_connector, plcopen.commonObjects_continuation)):
  2189                 element = self.GetEditedElement(tagname, debug)
  2106                         names[instance.getname().upper()] = True
  2190                 if element is not None and element.getbodyType() not in ["ST", "IL"]:
       
  2191                     for instance in element.getinstances():
       
  2192                         if isinstance(instance, (plcopen.sfcObjects_step, 
       
  2193                                                  plcopen.commonObjects_connector, 
       
  2194                                                  plcopen.commonObjects_continuation)):
       
  2195                             names[instance.getname().upper()] = True
  2107         else:
  2196         else:
  2108             project = self.GetProject(debug)
  2197             project = self.GetProject(debug)
  2109             if project is not None:
  2198             if project is not None:
  2110                 for datatype in project.getdataTypes():
  2199                 for datatype in project.getdataTypes():
  2111                     names[datatype.getname().upper()] = True
  2200                     names[datatype.getname().upper()] = True
  2120                 for config in project.getconfigurations():
  2209                 for config in project.getconfigurations():
  2121                     names[config.getname().upper()] = True
  2210                     names[config.getname().upper()] = True
  2122                     for resource in config.getresource():
  2211                     for resource in config.getresource():
  2123                         names[resource.getname().upper()] = True
  2212                         names[resource.getname().upper()] = True
  2124             
  2213             
  2125         i = 0
  2214         i = start_idx
  2126         while name is None or names.get(name.upper(), False):
  2215         while name is None or names.get(name.upper(), False):
  2127             name = (format%i)
  2216             name = (format%i)
  2128             i += 1
  2217             i += 1
  2129         return name
  2218         return name
  2130     
  2219     
  2150             new_id = {}
  2239             new_id = {}
  2151             
  2240             
  2152             text = "<paste>%s</paste>"%text
  2241             text = "<paste>%s</paste>"%text
  2153             
  2242             
  2154             try:
  2243             try:
  2155                 tree = minidom.parseString(text)
  2244                 tree = minidom.parseString(text.encode("utf-8"))
  2156             except:
  2245             except:
  2157                 return _("Invalid plcopen element(s)!!!")
  2246                 return _("Invalid plcopen element(s)!!!")
  2158             instances = []
  2247             instances = []
  2159             exclude = {}
  2248             exclude = {}
  2160             for root in tree.childNodes:
  2249             for root in tree.childNodes:
  2176                                     blockname = instance.getinstanceName()
  2265                                     blockname = instance.getinstanceName()
  2177                                     if blockname is not None:
  2266                                     if blockname is not None:
  2178                                         blocktype = instance.gettypeName()
  2267                                         blocktype = instance.gettypeName()
  2179                                         if element_type == "function":
  2268                                         if element_type == "function":
  2180                                             return _("FunctionBlock \"%s\" can't be pasted in a Function!!!")%blocktype
  2269                                             return _("FunctionBlock \"%s\" can't be pasted in a Function!!!")%blocktype
  2181                                         blockname = self.GenerateNewName(tagname, blockname, "%s%%d"%blocktype, debug=debug)
  2270                                         blockname = self.GenerateNewName(tagname, 
       
  2271                                                                          blockname, 
       
  2272                                                                          "%s%%d"%blocktype, 
       
  2273                                                                          debug=debug)
  2182                                         exclude[blockname] = True
  2274                                         exclude[blockname] = True
  2183                                         instance.setinstanceName(blockname)
  2275                                         instance.setinstanceName(blockname)
  2184                                         self.AddEditedElementPouVar(tagname, blocktype, blockname)
  2276                                         self.AddEditedElementPouVar(tagname, blocktype, blockname)
  2185                                 elif child.nodeName == "step":
  2277                                 elif child.nodeName == "step":
  2186                                     stepname = self.GenerateNewName(tagname, instance.getname(), "Step%d", exclude, debug)
  2278                                     stepname = self.GenerateNewName(tagname, 
       
  2279                                                                     instance.getname(), 
       
  2280                                                                     "Step%d", 
       
  2281                                                                     exclude=exclude, 
       
  2282                                                                     debug=debug)
  2187                                     exclude[stepname] = True
  2283                                     exclude[stepname] = True
  2188                                     instance.setname(stepname)
  2284                                     instance.setname(stepname)
  2189                                 localid = instance.getlocalId()
  2285                                 localid = instance.getlocalId()
  2190                                 if not used_id.has_key(localid):
  2286                                 if not used_id.has_key(localid):
  2191                                     new_id[localid] = True
  2287                                     new_id[localid] = True