PLCControler.py
changeset 295 c6ef6d92ce16
parent 293 dbc366959daf
child 297 e837b67cb184
--- a/PLCControler.py	Mon Dec 15 09:45:16 2008 +0100
+++ b/PLCControler.py	Fri Dec 19 15:07:54 2008 +0100
@@ -247,7 +247,7 @@
         if project is not None:
             for pou in project.getpous():
                 if pou_name is None or pou_name == pou.getname():
-                    variables.extend([var["Name"] for var in self.GetPouInterfaceVars(pou)])
+                    variables.extend([var["Name"] for var in self.GetPouInterfaceVars(pou, debug)])
                     for transition in pou.gettransitionList():
                         variables.append(transition.getname())
                     for action in pou.getactionList():
@@ -964,22 +964,53 @@
             # Found the pou correponding to name and return the interface vars
             pou = project.getpou(name)
             if pou is not None:
-                return self.GetPouInterfaceVars(pou)
+                return self.GetPouInterfaceVars(pou, debug)
         return None
     
+    # Recursively generate element name tree for a structured variable
+    def GenerateVarTree(self, typename, debug = False):
+        project = self.GetProject(debug)
+        if project is not None:
+            blocktype = self.GetBlockType(typename, debug = debug)
+            if blocktype is not None:
+                tree = {}
+                for var_name, var_type, var_modifier in blocktype["inputs"] + blocktype["outputs"]:
+                    tree[var_name] = self.GenerateVarTree(var_type, debug)
+                return tree
+            datatype = project.getdataType(typename)
+            if datatype is not None:
+                tree = {}
+                basetype_content = datatype.baseType.getcontent()
+                if basetype_content["name"] == "derived":
+                    tree = self.GenerateVarTree(basetype_content["value"].getname())
+                elif basetype_content["name"] == "array":
+                    base_type = basetype_content["value"].baseType.getcontent()
+                    if base_type["name"] == "derived":
+                        tree = self.GenerateVarTree(base_type["value"].getname())
+                elif basetype_content["name"] == "struct":
+                    for element in basetype_content["value"].getvariable():
+                        element_type = element.type.getcontent()
+                        if element_type["name"] == "derived":
+                            tree[element.getname()] = self.GenerateVarTree(element_type["value"].getname())
+                        else:
+                            tree[element.getname()] = {}
+                return tree
+        return {}
+    
     # Return the interface for the given pou
-    def GetPouInterfaceVars(self, pou):
+    def GetPouInterfaceVars(self, pou, debug = False):
         vars = []
         # Verify that the pou has an interface
         if pou.interface is not None:
             # Extract variables from every varLists
             for type, varlist in pou.getvars():
                 for var in varlist.getvariable():
-                    tempvar = {"Name" : var.getname(), "Class" : type}
+                    tempvar = {"Name" : var.getname(), "Class" : type, "Tree" : {}}
                     vartype_content = var.gettype().getcontent()
                     if vartype_content["name"] == "derived":
                         tempvar["Type"] = vartype_content["value"].getname()
                         tempvar["Edit"] = not pou.hasblock(tempvar["Name"])
+                        tempvar["Tree"] = self.GenerateVarTree(tempvar["Type"], debug)
                     else:
                         if vartype_content["name"] in ["string", "wstring"]:
                             tempvar["Type"] = vartype_content["name"].upper()
@@ -1303,6 +1334,22 @@
                         infos["base_type"] = base_type["name"]
                     else:
                         infos["base_type"] = base_type["value"].getname()
+                elif basetype_content["name"] == "struct":
+                    infos["type"] = "Structure"
+                    infos["elements"] = []
+                    for element in basetype_content["value"].getvariable():
+                        element_infos = {}
+                        element_infos["Name"] = element.getname()
+                        element_type = element.type.getcontent()
+                        if element_type["value"] is None:
+                            element_infos["Type"] = element_type["name"]
+                        else:
+                            element_infos["Type"] = element_type["value"].getname()
+                        if element.initialValue is not None:
+                            element_infos["Initial Value"] = str(element.initialValue.getvalue())
+                        else:
+                            element_infos["Initial Value"] = ""
+                        infos["elements"].append(element_infos)
                 if datatype.initialValue is not None:
                     infos["initial"] = str(datatype.initialValue.getvalue())
                 else:
@@ -1374,6 +1421,31 @@
                     derived_datatype.setname(infos["base_type"])
                     array.baseType.setcontent({"name" : "derived", "value" : derived_datatype})
                 datatype.baseType.setcontent({"name" : "array", "value" : array})
+            elif infos["type"] == "Structure":
+                struct = plcopen.varListPlain()
+                for i, element_infos in enumerate(infos["elements"]):
+                    element = plcopen.varListPlain_variable()
+                    element.setname(element_infos["Name"])
+                    if element_infos["Type"] in self.GetBaseTypes():
+                        if element_infos["Type"] == "STRING":
+                            element.type.setcontent({"name" : "string", "value" : plcopen.elementaryTypes_string()})
+                        elif element_infos["Type"] == "WSTRING":
+                            element.type.setcontent({"name" : "wstring", "value" : plcopen.wstring()})
+                        else:
+                            element.type.setcontent({"name" : element_infos["Type"], "value" : None})
+                    else:
+                        derived_datatype = plcopen.derivedTypes_derived()
+                        derived_datatype.setname(element_infos["Type"])
+                        element.type.setcontent({"name" : "derived", "value" : derived_datatype})
+                    if element_infos["Initial Value"] != "":
+                        value = plcopen.value()
+                        value.setvalue(element_infos["Initial Value"])
+                        element.setinitialValue(value)
+                    if i == 0:
+                        struct.setvariable([element])
+                    else:
+                        struct.appendvariable(element)
+                datatype.baseType.setcontent({"name" : "struct", "value" : struct})
             if infos["initial"] != "":
                 if datatype.initialValue is None:
                     datatype.initialValue = plcopen.value()
@@ -1381,6 +1453,7 @@
             else:
                 datatype.initialValue = None
             self.Project.RefreshDataTypeHierarchy()
+            self.Project.RefreshElementUsingTree()
             self.BufferProject()
     
 #-------------------------------------------------------------------------------
@@ -1444,7 +1517,7 @@
             if project is not None:
                 pou = project.getpou(words[1])
                 if pou is not None:
-                    return self.GetPouInterfaceVars(pou)
+                    return self.GetPouInterfaceVars(pou, debug)
         return []
 
     # Return the edited element return type