Added support for speed-up loading of graphic POU tabs
authorLaurent Bessard
Fri, 27 Sep 2013 16:27:54 +0200
changeset 1331 38c5de794e62
parent 1330 96b242e4c59d
child 1332 ac7d39f4e376
Added support for speed-up loading of graphic POU tabs
PLCControler.py
editors/Viewer.py
plcopen/plcopen.py
--- a/PLCControler.py	Fri Sep 27 16:22:40 2013 +0200
+++ b/PLCControler.py	Fri Sep 27 16:27:54 2013 +0200
@@ -2287,24 +2287,25 @@
                 instance.translate(*diff)
             
             return new_id, connections
-                
+    
+    # Return the current pou editing instances idx
+    def GetEditedElementInstancesIds(self, tagname, debug = False):
+        element = self.GetEditedElement(tagname, debug)
+        if element is not None:
+            return element.getinstancesIds()
+        return []
+    
     # Return the current pou editing informations
-    def GetEditedElementInstanceInfos(self, tagname, id = None, exclude = [], debug = False):
-        infos = {}
-        instance = None
+    def GetEditedElementInstanceInfos(self, tagname, id, debug = False):
         element = self.GetEditedElement(tagname, debug)
         if element is not None:
-            # if id is defined
-            if id is not None:
-                instance = element.getinstance(id)
-            else:
-                instance = element.getrandomInstance(exclude)
-        if instance is not None:
-            infos = instance.getinfos()
-            if infos["type"] in ["input", "output", "inout"]:
-                var_type = self.GetEditedElementVarValueType(tagname, infos["specific_values"]["name"], debug)
-                infos["specific_values"]["value_type"] = var_type
-            return infos
+            instance = element.getinstance(id)
+            if instance is not None:
+                infos = instance.getinfos()
+                if infos["type"] in ["input", "output", "inout"]:
+                    var_type = self.GetEditedElementVarValueType(tagname, infos["specific_values"]["name"], debug)
+                    infos["specific_values"]["value_type"] = var_type
+                return infos
         return None
     
     def ClearEditedElementExecutionOrder(self, tagname):
--- a/editors/Viewer.py	Fri Sep 27 16:22:40 2013 +0200
+++ b/editors/Viewer.py	Fri Sep 27 16:27:54 2013 +0200
@@ -1089,10 +1089,11 @@
         self.ResetBuffer()
         instance = {}
         # List of ids of already loaded blocks
-        ids = []
+        ids = self.Controler.GetEditedElementInstancesIds(self.TagName, debug = self.Debug)
         # Load Blocks until they are all loaded
-        while instance is not None:
-            instance = self.Controler.GetEditedElementInstanceInfos(self.TagName, exclude = ids, debug = self.Debug)
+        while len(ids) > 0:
+            instance = self.Controler.GetEditedElementInstanceInfos(
+                self.TagName, ids.popitem(0)[0], debug = self.Debug)
             if instance is not None:
                 self.loadInstance(instance, ids, selection)
         
@@ -1221,7 +1222,6 @@
         
     # Load instance from given informations
     def loadInstance(self, instance, ids, selection):
-        ids.append(instance["id"])
         self.current_id = max(self.current_id, instance["id"])
         creation_function = ElementCreationFunctions.get(instance["type"], None)
         connectors = {"inputs" : [], "outputs" : []}
@@ -1319,7 +1319,7 @@
                 links_connected = False
                 continue
             
-            if refLocalId not in ids:
+            if ids.pop(refLocalId, False):
                 new_instance = self.Controler.GetEditedElementInstanceInfos(self.TagName, refLocalId, debug = self.Debug)
                 if new_instance is not None:
                     self.loadInstance(new_instance, ids, selection)
--- a/plcopen/plcopen.py	Fri Sep 27 16:22:40 2013 +0200
+++ b/plcopen/plcopen.py	Fri Sep 27 16:27:54 2013 +0200
@@ -26,6 +26,8 @@
 from types import *
 import os, re
 from lxml import etree
+from collections import OrderedDict
+
 """
 Dictionary that makes the relation between var names in plcopen and displayed values
 """
@@ -1089,11 +1091,11 @@
         return None
     setattr(cls, "getinstance", getinstance)
     
-    def getrandomInstance(self, exclude):
+    def getinstancesIds(self):
         if len(self.body) > 0:
-            return self.body[0].getcontentRandomInstance(exclude)
-        return None
-    setattr(cls, "getrandomInstance", getrandomInstance)
+            return self.body[0].getcontentInstancesIds()
+        return []
+    setattr(cls, "getinstancesIds", getinstancesIds)
     
     def getinstanceByName(self, name):
         if len(self.body) > 0:
@@ -1609,18 +1611,13 @@
             raise TypeError, _("%s body don't have instances!")%self.content.getLocalTag()
     setattr(cls, "getcontentInstance", getcontentInstance)
     
-    def getcontentRandomInstance(self, exclude):
+    def getcontentInstancesIds(self):
         if self.content.getLocalTag() in ["LD","FBD","SFC"]:
-            instance = self.content.xpath("*%s[position()=1]" %  
-                ("[not(%s)]" % " or ".join(
-                    map(lambda x: "@localId=%d" % x, exclude))
-                if len(exclude) > 0 else ""))
-            if len(instance) > 0:
-                return instance[0]
-            return None
+            return OrderedDict([(instance.getlocalId(), True)
+                                for instance in self.content])
         else:
             raise TypeError, _("%s body don't have instances!")%self.content.getLocalTag()
-    setattr(cls, "getcontentRandomInstance", getcontentRandomInstance)
+    setattr(cls, "getcontentInstancesIds", getcontentInstancesIds)
     
     def getcontentInstanceByName(self, name):
         if self.content.getLocalTag() in ["LD","FBD","SFC"]: