Added 'NamedConstant' drag'n'drop in programs. It does create a simple variable with initial value and is usefull for drag'n'drop constants from extensions while keeping a name associated with constant
authorEdouard Tisserant
Tue, 13 May 2014 00:43:06 +0200
changeset 1406 82db84fe88ea
parent 1405 0b9cd761a356
child 1407 cf3d2b53dd68
child 1469 86797748c2a2
Added 'NamedConstant' drag'n'drop in programs. It does create a simple variable with initial value and is usefull for drag'n'drop constants from extensions while keeping a name associated with constant
PLCControler.py
editors/TextViewer.py
editors/Viewer.py
plcopen/plcopen.py
--- a/PLCControler.py	Wed Apr 02 21:07:35 2014 +0200
+++ b/PLCControler.py	Tue May 13 00:43:06 2014 +0200
@@ -2456,7 +2456,7 @@
             var_type_obj.setcontent(derived_type)
         return var_type_obj
     
-    def AddEditedElementPouVar(self, tagname, var_type, name, location="", description=""):
+    def AddEditedElementPouVar(self, tagname, var_type, name,**args):
         if self.Project is not None:
             words = tagname.split("::")
             if words[0] in ['P', 'T', 'A']:
@@ -2464,7 +2464,7 @@
                 if pou is not None:
                     pou.addpouLocalVar(
                         self.GetVarTypeObject(var_type), 
-                        name, location, description)
+                        name, **args)
     
     def AddEditedElementPouExternalVar(self, tagname, var_type, name):
         if self.Project is not None:
--- a/editors/TextViewer.py	Wed Apr 02 21:07:35 2014 +0200
+++ b/editors/TextViewer.py	Tue May 13 00:43:06 2014 +0200
@@ -313,7 +313,9 @@
                             var_type = values[2]
                         else:
                             var_type = LOCATIONDATATYPES.get(location[2], ["BOOL"])[0]
-                        self.Controler.AddEditedElementPouVar(self.TagName, var_type, var_name, location, values[4])
+                        self.Controler.AddEditedElementPouVar(self.TagName,
+                            var_type, var_name, 
+                            location=location, description=values[4])
                         self.RefreshVariablePanel()
                         self.RefreshVariableTree()
                         event.SetDragText(var_name)
--- a/editors/Viewer.py	Wed Apr 02 21:07:35 2014 +0200
+++ b/editors/Viewer.py	Tue May 13 00:43:06 2014 +0200
@@ -310,7 +310,21 @@
                         else:
                             var_type = LOCATIONDATATYPES.get(location[2], ["BOOL"])[0]
                         if not var_name.upper() in [name.upper() for name in self.ParentWindow.Controler.GetEditedElementVariables(tagname, self.ParentWindow.Debug)]:
-                            self.ParentWindow.Controler.AddEditedElementPouVar(tagname, var_type, var_name, location, values[4])
+                            self.ParentWindow.Controler.AddEditedElementPouVar(tagname, var_type, var_name, location=location, description=values[4])
+                            self.ParentWindow.RefreshVariablePanel()
+                            self.ParentWindow.ParentWindow.RefreshPouInstanceVariablesPanel()
+                        self.ParentWindow.AddVariableBlock(x, y, scaling, var_class, var_name, var_type)
+            elif values[1] == "NamedConstant":
+                if pou_type == "program":
+                    initval = values[0]
+                    var_name = values[3]
+                    if var_name.upper() in [name.upper() for name in self.ParentWindow.Controler.GetProjectPouNames(self.ParentWindow.Debug)]:
+                        message = _("\"%s\" pou already exists!")%var_name
+                    else:
+                        var_class = INPUT
+                        var_type = values[2]
+                        if not var_name.upper() in [name.upper() for name in self.ParentWindow.Controler.GetEditedElementVariables(tagname, self.ParentWindow.Debug)]:
+                            self.ParentWindow.Controler.AddEditedElementPouVar(tagname, var_type, var_name, description=values[4], initval=initval)
                             self.ParentWindow.RefreshVariablePanel()
                             self.ParentWindow.ParentWindow.RefreshPouInstanceVariablesPanel()
                         self.ParentWindow.AddVariableBlock(x, y, scaling, var_class, var_name, var_type)
--- a/plcopen/plcopen.py	Wed Apr 02 21:07:35 2014 +0200
+++ b/plcopen/plcopen.py	Tue May 13 00:43:06 2014 +0200
@@ -1181,16 +1181,12 @@
             self.interface = PLCOpenParser.CreateElement("interface", "pou")
         self.interface.setcontent(vars)
     setattr(cls, "setvars", setvars)
-    
-    def addpouLocalVar(self, var_type, name, location="", description=""):
-        self.addpouVar(var_type, name, location=location, description=description)
-    setattr(cls, "addpouLocalVar", addpouLocalVar)
         
     def addpouExternalVar(self, var_type, name):
-        self.addpouVar(type, name, "externalVars")
+        self.addpouVar(var_type, name, "externalVars")
     setattr(cls, "addpouExternalVar", addpouExternalVar)
     
-    def addpouVar(self, var_type, name, var_class="localVars", location="", description=""):
+    def addpouVar(self, var_type, name, var_class="localVars", location="", description="", initval=""):
         if self.interface is None:
             self.interface = PLCOpenParser.CreateElement("interface", "pou")
         content = self.interface.getcontent()
@@ -1215,9 +1211,14 @@
             ft = PLCOpenParser.CreateElement("documentation", "variable")
             ft.setanyText(description)
             var.setdocumentation(ft)
+        if initval != "":
+            el = PLCOpenParser.CreateElement("initialValue", "variable")
+            el.setvalue(initval)
+            var.setinitialValue(el)
         
         varlist.appendvariable(var)
     setattr(cls, "addpouVar", addpouVar)
+    setattr(cls, "addpouLocalVar", addpouVar)
     
     def changepouVar(self, old_type, old_name, new_type, new_name):
         if self.interface is not None: