PLCControler.py
changeset 236 9f594b90bb1a
parent 233 de7ddb376150
child 249 d8425712acef
--- a/PLCControler.py	Fri Aug 22 13:49:22 2008 +0200
+++ b/PLCControler.py	Fri Aug 22 13:50:42 2008 +0200
@@ -49,12 +49,28 @@
                   ITEM_FUNCTION,
                   ITEM_FUNCTIONBLOCK,
                   ITEM_PROGRAM,
+                  ITEM_VAR_LOCAL,
+                  ITEM_VAR_GLOBAL,
+                  ITEM_VAR_EXTERNAL,
+                  ITEM_VAR_TEMP,
+                  ITEM_VAR_INPUT,
+                  ITEM_VAR_OUTPUT,
+                  ITEM_VAR_INOUT,
                   ITEM_TRANSITIONS,
                   ITEM_ACTIONS,
                   ITEM_CONFIGURATIONS,
                   ITEM_RESOURCES,
                   ITEM_PROPERTIES,
-                  ]=range(9,18)
+                  ]=range(9,25)
+
+VAR_CLASS_INFOS = {"Local" :    (plcopen.interface_localVars,    ITEM_VAR_LOCAL),
+                   "Global" :   (plcopen.interface_globalVars,   ITEM_VAR_GLOBAL),
+                   "External" : (plcopen.interface_externalVars, ITEM_VAR_EXTERNAL),
+                   "Temp" :     (plcopen.interface_tempVars,     ITEM_VAR_TEMP),
+                   "Input" :    (plcopen.interface_inputVars,    ITEM_VAR_INPUT),
+                   "Output" :   (plcopen.interface_outputVars,   ITEM_VAR_OUTPUT),
+                   "InOut" :    (plcopen.interface_inOutVars,    ITEM_VAR_INOUT)
+                  }
 
 ScriptDirectory = os.path.split(os.path.realpath(__file__))[0]
 
@@ -266,14 +282,14 @@
     # Return project informations
     def GetProjectInfos(self):
         if self.Project:
-            infos = {"name": self.Project.getname(), "type": ITEM_PROJECT, "tagname": ""}
-            datatypes = {"name": "Data Types", "type": ITEM_DATATYPES, "tagname": "", "values":[]}
+            infos = {"name": self.Project.getname(), "type": ITEM_PROJECT}
+            datatypes = {"name": "Data Types", "type": ITEM_DATATYPES, "values":[]}
             for datatype in self.Project.getdataTypes():
                 datatypes["values"].append({"name": datatype.getname(), "type": ITEM_DATATYPE, 
                     "tagname": self.ComputeDataTypeName(datatype.getname()), "values": []})
-            pou_types = {"function": {"name": "Functions", "type": ITEM_FUNCTION, "tagname": "", "values":[]},
-                         "functionBlock": {"name": "Function Blocks", "type": ITEM_FUNCTIONBLOCK, "tagname": "", "values":[]},
-                         "program": {"name": "Programs", "type": ITEM_PROGRAM, "tagname": "", "values":[]}}
+            pou_types = {"function": {"name": "Functions", "type": ITEM_FUNCTION, "values":[]},
+                         "functionBlock": {"name": "Function Blocks", "type": ITEM_FUNCTIONBLOCK, "values":[]},
+                         "program": {"name": "Programs", "type": ITEM_PROGRAM, "values":[]}}
             for pou in self.Project.getpous():
                 pou_type = pou.getpouType()
                 pou_infos = {"name": pou.getname(), "type": ITEM_POU,
@@ -285,26 +301,23 @@
                         transitions.append({"name": transition.getname(), "type": ITEM_TRANSITION, 
                             "tagname": self.ComputePouTransitionName(pou.getname(), transition.getname()), 
                             "values": []})
-                    pou_values.append({"name": "Transitions", "type": ITEM_TRANSITIONS, 
-                                       "tagname": "", "values": transitions})
+                    pou_values.append({"name": "Transitions", "type": ITEM_TRANSITIONS, "values": transitions})
                     actions = []
                     for action in pou.getactionList():
                         actions.append({"name": action.getname(), "type": ITEM_ACTION, 
                             "tagname": self.ComputePouActionName(pou.getname(), action.getname()), 
                             "values": []})
-                    pou_values.append({"name": "Actions", "type": ITEM_ACTIONS, 
-                                       "tagname": "", "values": actions})
+                    pou_values.append({"name": "Actions", "type": ITEM_ACTIONS, "values": actions})
                 if pou_type in pou_types:
                     pou_infos["values"] = pou_values
                     pou_types[pou_type]["values"].append(pou_infos)
-            configurations = {"name": "Configurations", "type": ITEM_CONFIGURATIONS, 
-                              "tagname": "", "values": []}
+            configurations = {"name": "Configurations", "type": ITEM_CONFIGURATIONS, "values": []}
             for config in self.Project.getconfigurations():
                 config_name = config.getname()
                 config_infos = {"name": config_name, "type": ITEM_CONFIGURATION, 
                     "tagname": self.ComputeConfigurationName(config.getname()), 
                     "values": []}
-                resources = {"name": "Resources", "type": ITEM_RESOURCES, "tagname": "", "values": []}
+                resources = {"name": "Resources", "type": ITEM_RESOURCES, "values": []}
                 for resource in config.getresource():
                     resource_name = resource.getname()
                     resource_infos = {"name": resource_name, "type": ITEM_RESOURCE, 
@@ -313,7 +326,7 @@
                     resources["values"].append(resource_infos)
                 config_infos["values"] = [resources]
                 configurations["values"].append(config_infos)
-            infos["values"] = [{"name": "Properties", "type": ITEM_PROPERTIES, "tagname": "", "values": []},
+            infos["values"] = [{"name": "Properties", "type": ITEM_PROPERTIES, "values": []},
                                datatypes, pou_types["function"], pou_types["functionBlock"], 
                                pou_types["program"], configurations]
             return infos
@@ -340,42 +353,57 @@
                         for variable in varlist.getvariable():
                             vartype_content = variable.gettype().getcontent()
                             if vartype_content["name"] == "derived":
-                                var_infos = self.GetPouTopology(variable.getname(), vartype_content["value"].getname())
+                                var_infos = self.GetPouTopology(variable.getname(), vartype_content["value"].getname(), True)
                                 if var_infos is not None:
                                     resource_infos["values"].append(var_infos)
                             elif vartype_content["name"] in ["string", "wstring"]:
-                                resource_infos["values"].append({"name" : "%s(%s)"%(variable.getname(), vartype_content["name"].upper()), 
-                                                                 "type" : ITEM_VARIABLE, "values" : []})
+                                resource_infos["values"].append({"name" : variable.getname(), 
+                                                                 "elmt_type" : vartype_content["name"].upper(),
+                                                                 "type" : ITEM_VAR_GLOBAL, "values" : []})
                             else:
-                                resource_infos["values"].append({"name" : "%s(%s)"%(variable.getname(), vartype_content["name"]), 
-                                                                 "type" : ITEM_VARIABLE, "values" : []})
+                                resource_infos["values"].append({"name" : variable.getname(), 
+                                                                 "elmt_type" : vartype_content["name"], 
+                                                                 "type" : ITEM_VAR_GLOBAL, "values" : []})
                     config_infos["values"].append(resource_infos)
                 for varlist in config.getglobalVars():
                     for variable in varlist.getvariable():
                         vartype_content = variable.gettype().getcontent()
                         if vartype_content["name"] == "derived":
-                            var_infos = self.GetPouTopology(variable.getname(), vartype_content["value"].getname())
+                            var_infos = self.GetPouTopology(variable.getname(), vartype_content["value"].getname(), True)
                             if var_infos is not None:
                                 config_infos["values"].append(var_infos)
                         elif vartype_content["name"] in ["string", "wstring"]:
-                            config_infos["values"].append({"name" : "%s(%s)"%(variable.getname(), vartype_content["name"].upper()), 
-                                                           "type" : ITEM_VARIABLE, "values" : []})
+                            config_infos["values"].append({"name" : variable.getname(), 
+                                                           "elmt_type" : vartype_content["name"].upper(), 
+                                                           "type" : ITEM_VAR_GLOBAL, "values" : []})
                         else:
-                            config_infos["values"].append({"name" : "%s(%s)"%(variable.getname(), vartype_content["name"]), 
-                                                           "type" : ITEM_VARIABLE, "values" : []})
+                            config_infos["values"].append({"name" : variable.getname(),
+                                                           "elmt_type" : vartype_content["name"], 
+                                                           "type" : ITEM_VAR_GLOBAL, "values" : []})
                 infos["values"].append(config_infos)
             return infos
         return None
     
     # Return pou topology informations
-    def GetPouTopology(self, name, type):
+    def GetPouTopology(self, name, type, global_var=False):
         if self.Project:
             pou = self.Project.getpou(type)
             if pou is not None:
-                pou_infos = {"name" : "%s(%s)"%(name, type), "type" : ITEM_POU, "values" : []}
+                pou_type = pou.getpouType()
+                if pou_type == "function":
+                    return None
+                elif pou_type == "program":
+                    pou_infos = {"name" : name, "elmt_type" : type, "type" : ITEM_PROGRAM, "values" : []}
+                else:
+                    pou_infos = {"name" : name, "elmt_type" : type, "type" : ITEM_FUNCTIONBLOCK, "values" : []}
                 if pou.interface:
                     # Extract variables from every varLists
                     for type, varlist in pou.getvars():
+                        infos = VAR_CLASS_INFOS.get(type, None)
+                        if infos is not None:
+                            current_var_class = infos[1]
+                        else:
+                            current_var_class = ITEM_VAR_LOCAL
                         for variable in varlist.getvariable():
                             vartype_content = variable.gettype().getcontent()
                             if vartype_content["name"] == "derived":
@@ -383,24 +411,33 @@
                                 if var_infos is not None:
                                     pou_infos["values"].append(var_infos)
                             elif vartype_content["name"] in ["string", "wstring"]:
-                                pou_infos["values"].append({"name" : "%s(%s)"%(variable.getname(), vartype_content["name"].upper()), 
-                                                               "type" : ITEM_VARIABLE, "values" : []})
+                                pou_infos["values"].append({"name" : variable.getname(), 
+                                                            "elmt_type" : vartype_content["name"].upper(), 
+                                                            "type" : current_var_class, "values" : []})
                             else:
-                                pou_infos["values"].append({"name" : "%s(%s)"%(variable.getname(), vartype_content["name"]), 
-                                                               "type" : ITEM_VARIABLE, "values" : []})
+                                pou_infos["values"].append({"name" : variable.getname(), 
+                                                            "elmt_type" : vartype_content["name"], 
+                                                            "type" : current_var_class, "values" : []})
                 return pou_infos
             block_infos = self.GetBlockType(type)
-            if block_infos is not None and block_infos["type"] != "function":
-                pou_infos = {"name" : "%s(%s)"%(name, type), "type" : ITEM_POU, "values" : []}
+            if block_infos is not None:
+                if block_infos["type"] == "function":
+                    return None
+                elif block_infos["type"] == "program":
+                    pou_infos = {"name" : name, "elmt_type" : type, "type" : ITEM_PROGRAM, "values" : []}
+                else:
+                    pou_infos = {"name" : name, "elmt_type" : type, "type" : ITEM_FUNCTIONBLOCK, "values" : []}
                 for varname, vartype, varmodifier in block_infos["inputs"]:
-                    pou_infos["values"].append({"name" : "%s(%s)"%(varname, vartype), "type" : ITEM_VARIABLE, "values" : []})
+                    pou_infos["values"].append({"name" : varname, "elmt_type" : vartype, "type" : ITEM_VAR_INPUT, "values" : []})
                 for varname, vartype, varmodifier in block_infos["outputs"]:
-                    pou_infos["values"].append({"name" : "%s(%s)"%(varname, vartype), "type" : ITEM_VARIABLE, "values" : []})
+                    pou_infos["values"].append({"name" : varname, "elmt_type" : vartype, "type" : ITEM_VAR_INPUT, "values" : []})
                 return pou_infos
             
-            if type in TypeHierarchy:
-                return {"name" : "%s(%s)"%(name, type), "type" : ITEM_VARIABLE, "values" : []}
-                
+            if type in self.GetDataTypes():
+                if global_var:
+                    return {"name" : name, "elmt_type" : type, "type" : ITEM_VAR_GLOBAL, "values" : []}
+                else:
+                    return {"name" : name, "elmt_type" : type, "type" : ITEM_VAR_LOCAL, "values" : []}
         return None
 
     # Return if data type given by name is used by another data type or pou
@@ -438,6 +475,7 @@
                 next_row = row + len(lines) - 1
                 next_col = len(lines[-1]) + 1
             else:
+                next_row = row
                 next_col = col + len(chunk)
             if (next_row > from_location[0] or next_row == from_location[0] and next_col >= from_location[1]) and len(chunk_infos) > 0:
                 infos.append((chunk_infos, (row, col)))
@@ -653,20 +691,9 @@
                                     var["Class"] == "Global")
             if current_type != next_type:
                 current_type = next_type
-                if var["Class"] == "Local":
-                    current_varlist = plcopen.interface_localVars()
-                elif var["Class"] == "Global":
-                    current_varlist = plcopen.interface_globalVars()
-                elif var["Class"] == "External":
-                    current_varlist = plcopen.interface_externalVars()
-                elif var["Class"] == "Temp":
-                    current_varlist = plcopen.interface_tempVars()
-                elif var["Class"] == "Input":
-                    current_varlist = plcopen.interface_inputVars()
-                elif var["Class"] == "Output":
-                    current_varlist = plcopen.interface_outputVars()
-                elif var["Class"] == "InOut":
-                    current_varlist = plcopen.interface_inOutVars()
+                infos = VAR_CLASS_INFOS.get(var["Class"], None)
+                if infos is not None:
+                    current_varlist = infos[0]()
                 else:
                     current_varlist = plcopen.varList()
                 varlist_list.append((var["Class"], current_varlist))
@@ -1691,14 +1718,19 @@
             old_type = block.gettypeName()
             new_name = infos.get("name", old_name)
             new_type = infos.get("type", old_type)
-            old_typeinfos = self.GetBlockType(old_type)
-            new_typeinfos = self.GetBlockType(new_type)
-            if new_type != old_type and new_typeinfos["type"] != old_typeinfos["type"]:
-                if new_typeinfos["type"] == "function":
-                    self.RemoveEditedElementPouVar(tagname, old_type, old_name)
-                else:
-                    self.AddEditedElementPouVar(tagname, new_type, new_name)
-            elif old_name != new_name and new_typeinfos["type"] != "function":
+            if new_type != old_type:
+                old_typeinfos = self.GetBlockType(old_type)
+                new_typeinfos = self.GetBlockType(new_type)
+                if old_typeinfos is None or new_typeinfos is None:
+                    self.ChangeEditedElementPouVar(tagname, old_type, old_name, new_type, new_name)
+                elif new_typeinfos["type"] != old_typeinfos["type"]:
+                    if new_typeinfos["type"] == "function":
+                        self.RemoveEditedElementPouVar(tagname, old_type, old_name)
+                    else:
+                        self.AddEditedElementPouVar(tagname, new_type, new_name)
+                elif new_typeinfos["type"] != "function" and old_name != new_name:
+                    self.ChangeEditedElementPouVar(tagname, old_type, old_name, new_type, new_name)
+            elif new_name != old_name:
                 self.ChangeEditedElementPouVar(tagname, old_type, old_name, new_type, new_name)
             for param, value in infos.items():
                 if param == "name":