Adding support for using current selected variable for generate newly added variable informations in VariablePanel
authorLaurent Bessard
Sun, 12 May 2013 23:30:00 +0200
changeset 1122 84de51ab40d2
parent 1121 d3838e8f1b90
child 1123 55ed55ef7aea
Adding support for using current selected variable for generate newly added variable informations in VariablePanel
PLCControler.py
controls/VariablePanel.py
editors/Viewer.py
--- a/PLCControler.py	Fri May 10 15:17:33 2013 +0200
+++ b/PLCControler.py	Sun May 12 23:30:00 2013 +0200
@@ -2100,14 +2100,17 @@
                     text += instance_copy.generateXMLText(name.split("_")[-1], 0)
         return text
     
-    def GenerateNewName(self, tagname, name, format, exclude={}, debug=False):
+    def GenerateNewName(self, tagname, name, format, start_idx=0, exclude={}, debug=False):
         names = exclude.copy()
         if tagname is not None:
-            names.update(dict([(varname.upper(), True) for varname in self.GetEditedElementVariables(tagname, debug)]))
+            names.update(dict([(varname.upper(), True) 
+                               for varname in self.GetEditedElementVariables(tagname, debug)]))
             element = self.GetEditedElement(tagname, debug)
-            if element is not None:
+            if element is not None and element.getbodyType() not in ["ST", "IL"]:
                 for instance in element.getinstances():
-                    if isinstance(instance, (plcopen.sfcObjects_step, plcopen.commonObjects_connector, plcopen.commonObjects_continuation)):
+                    if isinstance(instance, (plcopen.sfcObjects_step, 
+                                             plcopen.commonObjects_connector, 
+                                             plcopen.commonObjects_continuation)):
                         names[instance.getname().upper()] = True
         else:
             project = self.GetProject(debug)
@@ -2127,7 +2130,7 @@
                     for resource in config.getresource():
                         names[resource.getname().upper()] = True
             
-        i = 0
+        i = start_idx
         while name is None or names.get(name.upper(), False):
             name = (format%i)
             i += 1
@@ -2183,12 +2186,19 @@
                                         blocktype = instance.gettypeName()
                                         if element_type == "function":
                                             return _("FunctionBlock \"%s\" can't be pasted in a Function!!!")%blocktype
-                                        blockname = self.GenerateNewName(tagname, blockname, "%s%%d"%blocktype, debug=debug)
+                                        blockname = self.GenerateNewName(tagname, 
+                                                                         blockname, 
+                                                                         "%s%%d"%blocktype, 
+                                                                         debug=debug)
                                         exclude[blockname] = True
                                         instance.setinstanceName(blockname)
                                         self.AddEditedElementPouVar(tagname, blocktype, blockname)
                                 elif child.nodeName == "step":
-                                    stepname = self.GenerateNewName(tagname, instance.getname(), "Step%d", exclude, debug)
+                                    stepname = self.GenerateNewName(tagname, 
+                                                                    instance.getname(), 
+                                                                    "Step%d", 
+                                                                    exclude=exclude, 
+                                                                    debug=debug)
                                     exclude[stepname] = True
                                     instance.setname(stepname)
                                 localid = instance.getlocalId()
--- a/controls/VariablePanel.py	Fri May 10 15:17:33 2013 +0200
+++ b/controls/VariablePanel.py	Sun May 12 23:30:00 2013 +0200
@@ -89,6 +89,7 @@
                       }
 
 LOCATION_MODEL = re.compile("((?:%[IQM](?:\*|(?:[XBWLD]?[0-9]+(?:\.[0-9]+)*)))?)$")
+VARIABLE_NAME_SUFFIX_MODEL = re.compile("([0-9]*)$")
 
 #-------------------------------------------------------------------------------
 #                            Variables Panel Table
@@ -490,11 +491,27 @@
         
         def _AddVariable(new_row):
             if not self.PouIsUsed or self.Filter not in ["Interface", "Input", "Output", "InOut"]:
-                row_content = self.DefaultValue.copy()
-                if self.Filter in self.DefaultTypes:
-                    row_content["Class"] = self.DefaultTypes[self.Filter]
+                if new_row > 0:
+                    row_content = self.Values[new_row - 1].copy()
+                    result = VARIABLE_NAME_SUFFIX_MODEL.search(row_content["Name"])
+                    if result is not None:
+                        name = row_content["Name"][:result.start(1)]
+                        suffix = result.group(1)
+                        if suffix != "":
+                            start_idx = int(suffix)
+                        else:
+                            start_idx = 0
+                    else:
+                        name = row_content["Name"]
+                        start_idx = 0
+                    row_content["Name"] = self.Controler.GenerateNewName(
+                        self.TagName, None, name + "%d", start_idx)
                 else:
-                    row_content["Class"] = self.Filter
+                    row_content = self.DefaultValue.copy()
+                    if self.Filter in self.DefaultTypes:
+                        row_content["Class"] = self.DefaultTypes[self.Filter]
+                    else:
+                        row_content["Class"] = self.Filter
                 if self.Filter == "All" and len(self.Values) > 0:
                     self.Values.insert(new_row, row_content)
                 else:
--- a/editors/Viewer.py	Fri May 10 15:17:33 2013 +0200
+++ b/editors/Viewer.py	Sun May 12 23:30:00 2013 +0200
@@ -3085,7 +3085,11 @@
             if blocktype is None:
                 blocktype = "Block"
             format = "%s%%d" % blocktype
-        return self.Controler.GenerateNewName(self.TagName, None, format, exclude, self.Debug)
+        return self.Controler.GenerateNewName(self.TagName, 
+                                              None, 
+                                              format, 
+                                              exclude=exclude, 
+                                              debug=self.Debug)
 
     def IsNamedElement(self, element):
         return isinstance(element, FBD_Block) and element.GetName() != "" or isinstance(element, SFC_Step)