PLCControler.py
changeset 705 1a343b239e9c
parent 704 aca0c83ed82e
child 707 e850bc674852
equal deleted inserted replaced
704:aca0c83ed82e 705:1a343b239e9c
   480                                 "variables": vars,
   480                                 "variables": vars,
   481                                 "edit": False,
   481                                 "edit": False,
   482                                 "debug": False}
   482                                 "debug": False}
   483             elif words[0] in ['C', 'R']:
   483             elif words[0] in ['C', 'R']:
   484                 if words[0] == 'C':
   484                 if words[0] == 'C':
   485                     pou_type = ITEM_CONFIGURATION
   485                     element_type = ITEM_CONFIGURATION
   486                     element = project.getconfiguration(words[1])
   486                     element = project.getconfiguration(words[1])
   487                     for resource in element.getresource():
   487                     if element is not None:
   488                         vars.append({"name": resource.getname(),
   488                         for resource in element.getresource():
   489                                      "type": None,
   489                             vars.append({"name": resource.getname(),
   490                                      "class": ITEM_RESOURCE,
   490                                          "type": None,
   491                                      "edit": True,
   491                                          "class": ITEM_RESOURCE,
   492                                      "debug": False})
   492                                          "edit": True,
       
   493                                          "debug": False})
   493                 elif words[0] == 'R':
   494                 elif words[0] == 'R':
   494                     pou_type = ITEM_RESOURCE
   495                     element_type = ITEM_RESOURCE
   495                     element = project.getconfigurationResource(words[1], words[2])
   496                     element = project.getconfigurationResource(words[1], words[2])
   496                     for task in element.gettask():
   497                     if element is not None:
   497                         for pou in task.getpouInstance():
   498                         for task in element.gettask():
       
   499                             for pou in task.getpouInstance():
       
   500                                 vars.append({"name": pou.getname(),
       
   501                                              "type": pou.gettypeName(),
       
   502                                              "class": ITEM_PROGRAM,
       
   503                                              "edit": True,
       
   504                                              "debug": True})
       
   505                         for pou in element.getpouInstance():
   498                             vars.append({"name": pou.getname(),
   506                             vars.append({"name": pou.getname(),
   499                                          "type": pou.gettypeName(),
   507                                          "type": pou.gettypeName(),
   500                                          "class": ITEM_PROGRAM,
   508                                          "class": ITEM_PROGRAM,
   501                                          "edit": True,
   509                                          "edit": True,
   502                                          "debug": True})
   510                                          "debug": True})
   503                     for pou in element.getpouInstance():
   511                 if element is not None:
   504                         vars.append({"name": pou.getname(),
   512                     for varlist in element.getglobalVars():
   505                                      "type": pou.gettypeName(),
   513                         for variable in varlist.getvariable():
   506                                      "class": ITEM_PROGRAM,
   514                             var_infos = self.GetPouVariableInfos(project, variable, ITEM_VAR_GLOBAL, debug)
   507                                      "edit": True,
   515                             if var_infos is not None:
   508                                      "debug": True})
   516                                 vars.append(var_infos)
   509                 for varlist in element.getglobalVars():
   517                     return {"class": element_type,
   510                     for variable in varlist.getvariable():
   518                             "type": None,
   511                         var_infos = self.GetPouVariableInfos(project, variable, ITEM_VAR_GLOBAL, debug)
   519                             "variables": vars,
   512                         if var_infos is not None:
   520                             "edit": True,
   513                             vars.append(var_infos)
   521                             "debug": False}
   514                 return {"class": pou_type,
       
   515                         "type": None,
       
   516                         "variables": vars,
       
   517                         "edit": True,
       
   518                         "debug": False}
       
   519         return None
   522         return None
   520 
   523 
   521     def RecursiveSearchPouInstances(self, project, pou_type, parent_path, varlists, debug = False):
   524     def RecursiveSearchPouInstances(self, project, pou_type, parent_path, varlists, debug = False):
   522         instances = []
   525         instances = []
   523         for varlist in varlists:
   526         for varlist in varlists:
   707 #-------------------------------------------------------------------------------
   710 #-------------------------------------------------------------------------------
   708 #                        Project Pous management functions
   711 #                        Project Pous management functions
   709 #-------------------------------------------------------------------------------
   712 #-------------------------------------------------------------------------------
   710     
   713     
   711     # Add a Data Type to Project
   714     # Add a Data Type to Project
   712     def ProjectAddDataType(self, datatype_name):
   715     def ProjectAddDataType(self, datatype_name=None):
   713         if self.Project is not None:
   716         if self.Project is not None:
       
   717             if datatype_name is None:
       
   718                 datatype_name = self.GenerateNewName(None, None, "datatype%d")
   714             # Add the datatype to project
   719             # Add the datatype to project
   715             self.Project.appenddataType(datatype_name)
   720             self.Project.appenddataType(datatype_name)
   716             self.BufferProject()
   721             self.BufferProject()
   717             return self.ComputeDataTypeName(datatype_name)
   722             return self.ComputeDataTypeName(datatype_name)
   718         return None
   723         return None
   805             if len(configurations) == 1:
   810             if len(configurations) == 1:
   806                 return configurations[0].getname()
   811                 return configurations[0].getname()
   807         return None
   812         return None
   808                 
   813                 
   809     # Add a configuration to Project
   814     # Add a configuration to Project
   810     def ProjectAddConfiguration(self, config_name):
   815     def ProjectAddConfiguration(self, config_name=None):
   811         if self.Project is not None:
   816         if self.Project is not None:
       
   817             if config_name is None:
       
   818                 config_name = self.GenerateNewName(None, None, "configuration%d")
   812             self.Project.addconfiguration(config_name)
   819             self.Project.addconfiguration(config_name)
   813             self.BufferProject()
   820             self.BufferProject()
   814             return self.ComputeConfigurationName(config_name)
   821             return self.ComputeConfigurationName(config_name)
   815         return None
   822         return None
   816     
   823     
   819         if self.Project is not None:
   826         if self.Project is not None:
   820             self.Project.removeconfiguration(config_name)
   827             self.Project.removeconfiguration(config_name)
   821             self.BufferProject()
   828             self.BufferProject()
   822     
   829     
   823     # Add a resource to a configuration of the Project
   830     # Add a resource to a configuration of the Project
   824     def ProjectAddConfigurationResource(self, config_name, resource_name):
   831     def ProjectAddConfigurationResource(self, config_name, resource_name=None):
   825         if self.Project is not None:
   832         if self.Project is not None:
       
   833             if resource_name is None:
       
   834                 resource_name = self.GenerateNewName(None, None, "resource%d")
   826             self.Project.addconfigurationResource(config_name, resource_name)
   835             self.Project.addconfigurationResource(config_name, resource_name)
   827             self.BufferProject()
   836             self.BufferProject()
   828             return self.ComputeConfigurationResourceName(config_name, resource_name)
   837             return self.ComputeConfigurationResourceName(config_name, resource_name)
   829         return None
   838         return None
   830     
   839     
  1956                     text += instance_copy.generateXMLText(name.split("_")[-1], 0)
  1965                     text += instance_copy.generateXMLText(name.split("_")[-1], 0)
  1957         return text
  1966         return text
  1958     
  1967     
  1959     def GenerateNewName(self, tagname, name, format, exclude={}, debug=False):
  1968     def GenerateNewName(self, tagname, name, format, exclude={}, debug=False):
  1960         names = exclude.copy()
  1969         names = exclude.copy()
  1961         names.update(dict([(varname.upper(), True) for varname in self.GetEditedElementVariables(tagname, debug)]))
  1970         if tagname is not None:
  1962         element = self.GetEditedElement(tagname, debug)
  1971             names.update(dict([(varname.upper(), True) for varname in self.GetEditedElementVariables(tagname, debug)]))
  1963         if element is not None:
  1972             element = self.GetEditedElement(tagname, debug)
  1964             for instance in element.getinstances():
  1973             if element is not None:
  1965                 if isinstance(instance, (plcopen.sfcObjects_step, plcopen.commonObjects_connector, plcopen.commonObjects_continuation)):
  1974                 for instance in element.getinstances():
  1966                     names[instance.getname()] = True
  1975                     if isinstance(instance, (plcopen.sfcObjects_step, plcopen.commonObjects_connector, plcopen.commonObjects_continuation)):
       
  1976                         names[instance.getname().upper()] = True
       
  1977         else:
       
  1978             project = self.GetProject(debug)
       
  1979             if project is not None:
       
  1980                 for datatype in project.getdataTypes():
       
  1981                     names[datatype.getname().upper()] = True
       
  1982                 for pou in project.getpous():
       
  1983                     names[pou.getname().upper()] = True
       
  1984                     for var in self.GetPouInterfaceVars(pou, debug):
       
  1985                         names[var["Name"].upper()] = True
       
  1986                     for transition in pou.gettransitionList():
       
  1987                         names[transition.getname().upper()] = True
       
  1988                     for action in pou.getactionList():
       
  1989                         names[action.getname().upper()] = True
       
  1990                 for config in project.getconfigurations():
       
  1991                     names[config.getname().upper()] = True
       
  1992                     for resource in config.getresource():
       
  1993                         names[resource.getname().upper()] = True
       
  1994             
  1967         i = 0
  1995         i = 0
  1968         while name is None or names.get(name.upper(), False):
  1996         while name is None or names.get(name.upper(), False):
  1969             name = (format%i)
  1997             name = (format%i)
  1970             i += 1
  1998             i += 1
  1971         return name
  1999         return name