PLCControler.py
changeset 80 c798a68c5560
parent 71 0578bc212c20
child 81 11ca9ad9e3c3
--- a/PLCControler.py	Thu Aug 23 09:50:35 2007 +0200
+++ b/PLCControler.py	Mon Aug 27 17:37:50 2007 +0200
@@ -196,11 +196,15 @@
     
     # Return project pou names
     def GetProjectPouNames(self):
-        return [pou.getName() for pou in self.Project.getPous()]
+        if self.Project:
+            return [pou.getName() for pou in self.Project.getPous()]
+        return []
     
     # Return project pou names
     def GetProjectConfigNames(self):
-        return [config.getName() for config in self.Project.getConfigurations()]
+        if self.Project:
+            return [config.getName() for config in self.Project.getConfigurations()]
+        return []
     
     # Return project pou variables
     def GetProjectPouVariables(self, pou_name=None):
@@ -383,6 +387,7 @@
     def ProjectAddPou(self, name, pou_type, body_type):
         # Add the pou to project
         self.Project.appendPou(name, pou_type, body_type)
+        self.SetPouInterfaceReturnType(name, "BOOL")
         self.RefreshPouUsingTree()
         self.RefreshBlockTypes()
         self.BufferProject()
@@ -455,9 +460,15 @@
         pou = self.Project.getPou(old_name)
         pou.setName(new_name)
         # If pou is currently opened, change its name in the list of opened pous
-        if old_name in self.ElementsOpened:
-            idx = self.ElementsOpened.index(old_name)
-            self.ElementsOpened[idx] = new_name
+        for idx, element in enumerate(self.ElementsOpened):
+            words = element.split("::")
+            if words[0] in ["P","T","A"] and words[1] == old_name:
+                if words[0] == "P":
+                    self.ElementsOpened[idx] = self.ComputePouName(new_name)
+                elif words[0] == "T":
+                    self.ElementsOpened[idx] = self.ComputePouTransitionName(new_name, words[2])
+                else:
+                    self.ElementsOpened[idx] = self.ComputePouActionName(new_name, words[2])
         self.Project.updateElementName(old_name, new_name)
         self.RefreshPouUsingTree()
         self.RefreshBlockTypes()
@@ -491,7 +502,7 @@
             self.ElementsOpened[idx] = new_computedname
         self.BufferProject()
     
-    # Change the name of a pou action
+    # Change the name of a pou variable
     def ChangePouVariableName(self, pou_name, old_name, new_name):
         # Found the pou action corresponding to old name and change its name to new name
         pou = self.Project.getPou(pou_name)
@@ -509,7 +520,12 @@
         configuration.setName(new_name)
         # If configuration is currently opened, change its name in the list of opened elements
         for idx, element in enumerate(self.ElementsOpened):
-            self.ElementsOpened[idx] = element.replace(old_name, new_name)
+            words = element.split("::")
+            if words[0] in ["C","R"] and words[1] == old_name:
+                if words[0] == "C":
+                    self.ElementsOpened[idx] = self.ComputeConfigurationName(new_name)
+                else:
+                    self.ElementsOpened[idx] = self.ComputeConfigurationResourceName(new_name, words[2])
         self.BufferProject()
     
     # Change the name of a configuration resource
@@ -865,15 +881,14 @@
             if type == "function":
                 blocktypes = []
                 for category in BlockTypes[:-1]:
-                    if category["name"] != "SVGUI function blocks":
-                        cat = {"name" : category["name"], "list" : []}
-                        for block in category["list"]:
-                            if block["type"] == "function":
-                                cat["list"].append(block)
-                        if len(cat["list"]) > 0:
-                            blocktypes.append(cat)
+                    cat = {"name" : category["name"], "list" : []}
+                    for block in category["list"]:
+                        if block["type"] == "function":
+                            cat["list"].append(block)
+                    if len(cat["list"]) > 0:
+                        blocktypes.append(cat)
             else:
-                blocktypes = [category for category in BlockTypes[:-1] if category["name"] != "SVGUI function blocks"]
+                blocktypes = [category for category in BlockTypes[:-1]]
             if self.Project:
                 blocktypes.append({"name" : "User-defined POUs", "list": []})
                 for blocktype in BlockTypes[-1]["list"]:
@@ -887,6 +902,7 @@
         if self.CurrentElementEditing != None:
             if self.Project:
                 current_name = self.ElementsOpened[self.CurrentElementEditing]
+                print current_name
                 words = current_name.split("::")
                 if len(words) == 1:
                     name = current_name
@@ -898,10 +914,9 @@
                 type = None
             blocktypes = []
             for category in BlockTypes[:-1]:
-                if category["name"] != "SVGUI function blocks":
-                    for block in category["list"]:
-                        if block["type"] != "function":
-                            blocktypes.append(block["name"])
+                for block in category["list"]:
+                    if block["type"] != "function":
+                        blocktypes.append(block["name"])
             if self.Project:
                 for blocktype in BlockTypes[-1]["list"]:
                     if blocktype["name"] != name and not self.PouIsUsedBy(name, blocktype["name"]) and not (type == "function" and blocktype["type"] != "function"):
@@ -932,15 +947,17 @@
         names = []
         for pou_name in self.ElementsOpened:
             words = pou_name.split("::")
-            if len(words) == 1:
-                names.append(pou_name)
-            elif len(words) == 2:
+            if words[0] in ["P","C"]:
                 names.append(words[1])
             else:
                 names.append("%s-%s"%(words[1],words[2]))
         return names
     
     # Compute a pou transition name
+    def ComputePouName(self, pou):
+        return "P::%s" % pou
+    
+    # Compute a pou transition name
     def ComputePouTransitionName(self, pou, transition):
         return "T::%s::%s" % (pou, transition)
     
@@ -967,6 +984,10 @@
         return None
 
     # Open a pou transition by giving pou and transition names
+    def OpenPouEditing(self, pou):
+        return self.OpenElementEditing(self.ComputePouName(pou))
+
+    # Open a pou transition by giving pou and transition names
     def OpenPouTransitionEditing(self, pou, transition):
         return self.OpenElementEditing(self.ComputePouTransitionName(pou, transition))
 
@@ -983,8 +1004,8 @@
         return self.OpenElementEditing(self.ComputeConfigurationResourceName(config, resource))
 
     # Return if pou given by name is opened
-    def IsElementEditing(self, name):
-        return name in self.ElementsOpened
+    def IsPouEditing(self, pou):
+        return self.ComputePouName(pou) in self.ElementsOpened
 
     # Return if pou transition given by pou and transition names is opened
     def IsPouTransitionEditing(self, pou, transition):
@@ -995,45 +1016,49 @@
         return self.ComputePouActionName(pou, action) in self.ElementsOpened
 
     # Return if pou action given by configuration name is opened
-    def IsConfigurationEditing(self, pou):
+    def IsConfigurationEditing(self, config):
         return self.ComputeConfigurationName(config) in self.ElementsOpened
 
     # Return if pou action given by configuration and resource names is opened
-    def IsConfigurationResourceEditing(self, pou, resource):
+    def IsConfigurationResourceEditing(self, config, resource):
         return self.ComputeConfigurationResourceName(config, resource) in self.ElementsOpened
 
-    # Close current pou editing
+    # Close current element editing
     def CloseElementEditing(self):
         # Remove pou from list of pou opened
         self.ElementsOpened.pop(self.CurrentElementEditing)
-        # Update index of current pou editing
+        # Update index of current element editing
         if len(self.ElementsOpened) > 0:
             self.CurrentElementEditing = min(self.CurrentElementEditing, len(self.ElementsOpened) - 1)
         else:
             self.CurrentElementEditing = None
 
-    # Change current pou editing for pou given by name
+    # Change current element editing for pou given by name
     def ChangeElementEditing(self, name):
-        # Verify that pou is opened
+        # Verify that element is opened
         if name in self.ElementsOpened:
-            # Change current pou editing
+            # Change current element editing
             self.CurrentElementEditing = self.ElementsOpened.index(name)
             return self.CurrentElementEditing
         return None
-
-    # Change current pou editing for transition given by pou and transition names
+    
+    # Change current element editing for pou given by pou name
+    def ChangePouEditing(self, pou):
+        return self.ChangeElementEditing(self.ComputePouName(pou))
+
+    # Change current element editing for transition given by pou and transition names
     def ChangePouTransitionEditing(self, pou, transition):
         return self.ChangeElementEditing(self.ComputePouTransitionName(pou, transition))
 
-    # Change current pou editing for action given by pou and action names
+    # Change current element editing for action given by pou and action names
     def ChangePouActionEditing(self, pou, action):
         return self.ChangeElementEditing(self.ComputePouActionName(pou, action))
 
-    # Change current pou editing for action given by configuration name
+    # Change current element editing for configuration given by configuration name
     def ChangeConfigurationEditing(self, config):
         return self.ChangeElementEditing(self.ComputeConfigurationName(config))
 
-    # Change current pou editing for action given by configuration and resource names
+    # Change current element editing for resource given by configuration and resource names
     def ChangeConfigurationResourceEditing(self, config, resource):
         return self.ChangeElementEditing(self.ComputeConfigurationResourceName(config, resource))
 
@@ -1047,21 +1072,18 @@
         if self.CurrentElementEditing != None:
             name = self.ElementsOpened[self.CurrentElementEditing]
             words = name.split("::")
-            if len(words) == 1:
-                return self.Project.getPou(name)
-            else:
-                if words[0] in ['T', 'A']:
-                    pou = self.Project.getPou(words[1])
-                    if words[0] == 'T':
-                        return pou.getTransition(words[2])
-                    elif words[0] == 'A':
-                        return pou.getAction(words[2])
-                elif words[0] == 'C':
-                    result = self.Project.getConfiguration(words[1])
-                    return result
-                elif words[0] == 'R':
-                    result = self.Project.getConfigurationResource(words[1], words[2])
-                    return result
+            if words[0] == "P":
+                return self.Project.getPou(words[1])
+            if words[0] in ['T', 'A']:
+                pou = self.Project.getPou(words[1])
+                if words[0] == 'T':
+                    return pou.getTransition(words[2])
+                elif words[0] == 'A':
+                    return pou.getAction(words[2])
+            elif words[0] == 'C':
+                return self.Project.getConfiguration(words[1])
+            elif words[0] == 'R':
+                return self.Project.getConfigurationResource(words[1], words[2])
         return None
     
     # Return current pou editing name
@@ -1070,9 +1092,7 @@
         if self.CurrentElementEditing != None:
             name = self.ElementsOpened[self.CurrentElementEditing]
             words = name.split("::")
-            if len(words) == 1:
-                return name
-            elif len(words) == 2:
+            if words[0] in ["P","C"]:
                 return words[1]
             else:
                 return words[2]
@@ -1087,11 +1107,7 @@
         if self.CurrentElementEditing != None:
             name = self.ElementsOpened[self.CurrentElementEditing]
             words = name.split("::")
-            if len(words) == 1:
-                return self.GetPouType(name)
-            elif len(words) == 2:
-                return None
-            elif words[0] != "R":
+            if words[0] == "P":
                 return self.GetPouType(words[1])
         return None
 
@@ -1100,13 +1116,12 @@
         if self.CurrentElementEditing != None:
             name = self.ElementsOpened[self.CurrentElementEditing]
             words = name.split("::")
-            if len(words) == 1:
-                return self.GetPouBodyType(name)
-            else:
-                if words[0] == 'T':
-                    return self.GetTransitionBodyType(words[1], words[2])
-                elif words[0] == 'A':
-                    return self.GetActionBodyType(words[1], words[2])
+            if words[0] == "P":
+                return self.GetPouBodyType(words[1])
+            elif words[0] == 'T':
+                return self.GetTransitionBodyType(words[1], words[2])
+            elif words[0] == 'A':
+                return self.GetActionBodyType(words[1], words[2])
         return None
 
     # Return the variables of the current pou editing
@@ -1114,10 +1129,7 @@
         if self.CurrentElementEditing != None:
             current_name = self.ElementsOpened[self.CurrentElementEditing]
             words = current_name.split("::")
-            if len(words) == 1:
-                pou = self.Project.getPou(current_name)
-                return self.GetPouInterfaceVars(pou)
-            else:
+            if words[0] in ["P","T","A"]:
                 pou = self.Project.getPou(words[1])
                 return self.GetPouInterfaceVars(pou)
         return []
@@ -1127,8 +1139,8 @@
         if self.CurrentElementEditing != None:
             current_name = self.ElementsOpened[self.CurrentElementEditing]
             words = current_name.split("::")
-            if len(words) == 1:
-                pou = self.Project.getPou(current_name)
+            if words[0] == "P":
+                pou = self.Project.getPou(words[1])
                 return self.GetPouInterfaceReturnType(pou)
             elif words[0] == 'T':
                 return "BOOL"
@@ -1173,9 +1185,7 @@
         if self.CurrentElementEditing != None:
             current_name = self.ElementsOpened[self.CurrentElementEditing]
             words = current_name.split("::")
-            if len(words) == 1:
-                return self.GetProjectPouVariables(current_name)
-            else:
+            if words[0] in ["P","T","A"]:
                 return self.GetProjectPouVariables(words[1])
         return []
 
@@ -1345,6 +1355,11 @@
             elif isinstance(instance, plcopen.transition):
                 infos["type"] = "transition"
                 condition = instance.getConditionContent()
+                priority = instance.getPriority()
+                if priority == None:
+                    infos["priority"] = 0
+                else:
+                    infos["priority"] = priority
                 infos["condition_type"] = condition["type"]
                 infos["connectors"] = {"input":{},"output":{}}
                 infos["connectors"]["input"]["position"] = instance.connectionPointIn.getRelPosition()
@@ -1433,14 +1448,12 @@
     def GetCurrentPouVarValueType(self, varname):
         current_name = self.ElementsOpened[self.CurrentElementEditing]
         words = current_name.split("::")
-        if len(words) == 1:
-            pou = self.Project.getPou(current_name)
-        else:
+        if words[0] in ["P","T","A"]:
             pou = self.Project.getPou(words[1])
-        for type, varlist in pou.getVars():
-            for var in varlist.getVariable():
-                if var.getName() == varname:
-                    return var.getType()
+            for type, varlist in pou.getVars():
+                for var in varlist.getVariable():
+                    if var.getName() == varname:
+                        return var.getType()
         return ""
     
     def SetConnectionWires(self, connection, connector):
@@ -1475,9 +1488,7 @@
             if self.CurrentElementEditing != None:
                 name = self.ElementsOpened[self.CurrentElementEditing]
                 words = name.split("::")
-                if len(words) == 1:
-                    element.addPouVar(blocktype, blockname)
-                elif words[0] in ['T', 'A']:
+                if words[0] in ["P","T","A"]:
                     pou = self.Project.getPou(words[1])
                     pou.addPouVar(blocktype, blockname)    
         element.addInstance("block", block)
@@ -1823,6 +1834,11 @@
                 transition.setX(value)
             elif param == "y":
                 transition.setY(value)
+            elif param == "priority":
+                if value != 0:
+                    transition.setPriority(value)
+                else:
+                    transition.setPriority(None)
             elif param == "connectors":
                 input_connector = value["input"]
                 position = input_connector.GetRelPosition()