PLCControler.py
changeset 705 1a343b239e9c
parent 704 aca0c83ed82e
child 707 e850bc674852
--- a/PLCControler.py	Wed Jun 06 16:57:58 2012 +0200
+++ b/PLCControler.py	Wed Jun 06 19:02:30 2012 +0200
@@ -482,40 +482,43 @@
                                 "debug": False}
             elif words[0] in ['C', 'R']:
                 if words[0] == 'C':
-                    pou_type = ITEM_CONFIGURATION
+                    element_type = ITEM_CONFIGURATION
                     element = project.getconfiguration(words[1])
-                    for resource in element.getresource():
-                        vars.append({"name": resource.getname(),
-                                     "type": None,
-                                     "class": ITEM_RESOURCE,
-                                     "edit": True,
-                                     "debug": False})
+                    if element is not None:
+                        for resource in element.getresource():
+                            vars.append({"name": resource.getname(),
+                                         "type": None,
+                                         "class": ITEM_RESOURCE,
+                                         "edit": True,
+                                         "debug": False})
                 elif words[0] == 'R':
-                    pou_type = ITEM_RESOURCE
+                    element_type = ITEM_RESOURCE
                     element = project.getconfigurationResource(words[1], words[2])
-                    for task in element.gettask():
-                        for pou in task.getpouInstance():
+                    if element is not None:
+                        for task in element.gettask():
+                            for pou in task.getpouInstance():
+                                vars.append({"name": pou.getname(),
+                                             "type": pou.gettypeName(),
+                                             "class": ITEM_PROGRAM,
+                                             "edit": True,
+                                             "debug": True})
+                        for pou in element.getpouInstance():
                             vars.append({"name": pou.getname(),
                                          "type": pou.gettypeName(),
                                          "class": ITEM_PROGRAM,
                                          "edit": True,
                                          "debug": True})
-                    for pou in element.getpouInstance():
-                        vars.append({"name": pou.getname(),
-                                     "type": pou.gettypeName(),
-                                     "class": ITEM_PROGRAM,
-                                     "edit": True,
-                                     "debug": True})
-                for varlist in element.getglobalVars():
-                    for variable in varlist.getvariable():
-                        var_infos = self.GetPouVariableInfos(project, variable, ITEM_VAR_GLOBAL, debug)
-                        if var_infos is not None:
-                            vars.append(var_infos)
-                return {"class": pou_type,
-                        "type": None,
-                        "variables": vars,
-                        "edit": True,
-                        "debug": False}
+                if element is not None:
+                    for varlist in element.getglobalVars():
+                        for variable in varlist.getvariable():
+                            var_infos = self.GetPouVariableInfos(project, variable, ITEM_VAR_GLOBAL, debug)
+                            if var_infos is not None:
+                                vars.append(var_infos)
+                    return {"class": element_type,
+                            "type": None,
+                            "variables": vars,
+                            "edit": True,
+                            "debug": False}
         return None
 
     def RecursiveSearchPouInstances(self, project, pou_type, parent_path, varlists, debug = False):
@@ -709,8 +712,10 @@
 #-------------------------------------------------------------------------------
     
     # Add a Data Type to Project
-    def ProjectAddDataType(self, datatype_name):
-        if self.Project is not None:
+    def ProjectAddDataType(self, datatype_name=None):
+        if self.Project is not None:
+            if datatype_name is None:
+                datatype_name = self.GenerateNewName(None, None, "datatype%d")
             # Add the datatype to project
             self.Project.appenddataType(datatype_name)
             self.BufferProject()
@@ -807,8 +812,10 @@
         return None
                 
     # Add a configuration to Project
-    def ProjectAddConfiguration(self, config_name):
-        if self.Project is not None:
+    def ProjectAddConfiguration(self, config_name=None):
+        if self.Project is not None:
+            if config_name is None:
+                config_name = self.GenerateNewName(None, None, "configuration%d")
             self.Project.addconfiguration(config_name)
             self.BufferProject()
             return self.ComputeConfigurationName(config_name)
@@ -821,8 +828,10 @@
             self.BufferProject()
     
     # Add a resource to a configuration of the Project
-    def ProjectAddConfigurationResource(self, config_name, resource_name):
-        if self.Project is not None:
+    def ProjectAddConfigurationResource(self, config_name, resource_name=None):
+        if self.Project is not None:
+            if resource_name is None:
+                resource_name = self.GenerateNewName(None, None, "resource%d")
             self.Project.addconfigurationResource(config_name, resource_name)
             self.BufferProject()
             return self.ComputeConfigurationResourceName(config_name, resource_name)
@@ -1958,12 +1967,31 @@
     
     def GenerateNewName(self, tagname, name, format, exclude={}, debug=False):
         names = exclude.copy()
-        names.update(dict([(varname.upper(), True) for varname in self.GetEditedElementVariables(tagname, debug)]))
-        element = self.GetEditedElement(tagname, debug)
-        if element is not None:
-            for instance in element.getinstances():
-                if isinstance(instance, (plcopen.sfcObjects_step, plcopen.commonObjects_connector, plcopen.commonObjects_continuation)):
-                    names[instance.getname()] = True
+        if tagname is not None:
+            names.update(dict([(varname.upper(), True) for varname in self.GetEditedElementVariables(tagname, debug)]))
+            element = self.GetEditedElement(tagname, debug)
+            if element is not None:
+                for instance in element.getinstances():
+                    if isinstance(instance, (plcopen.sfcObjects_step, plcopen.commonObjects_connector, plcopen.commonObjects_continuation)):
+                        names[instance.getname().upper()] = True
+        else:
+            project = self.GetProject(debug)
+            if project is not None:
+                for datatype in project.getdataTypes():
+                    names[datatype.getname().upper()] = True
+                for pou in project.getpous():
+                    names[pou.getname().upper()] = True
+                    for var in self.GetPouInterfaceVars(pou, debug):
+                        names[var["Name"].upper()] = True
+                    for transition in pou.gettransitionList():
+                        names[transition.getname().upper()] = True
+                    for action in pou.getactionList():
+                        names[action.getname().upper()] = True
+                for config in project.getconfigurations():
+                    names[config.getname().upper()] = True
+                    for resource in config.getresource():
+                        names[resource.getname().upper()] = True
+            
         i = 0
         while name is None or names.get(name.upper(), False):
             name = (format%i)