Adding support for defining plugin library as a plcopen xml file in plugin folder
authorgreg
Wed, 10 Dec 2008 17:37:21 +0100
changeset 274 8628f3dd0979
parent 273 d15a20eb2b8b
child 275 ff7c8eb3f362
Adding support for defining plugin library as a plcopen xml file in plugin folder
plugger.py
--- a/plugger.py	Wed Nov 26 09:05:04 2008 +0100
+++ b/plugger.py	Wed Dec 10 17:37:21 2008 +0100
@@ -18,6 +18,8 @@
 from xmlclass import GenerateClassesFromXSDstring
 from wxPopen import ProcessLogger
 
+from PLCControler import PLCControler
+
 _BaseParamsClass = GenerateClassesFromXSDstring("""<?xml version="1.0" encoding="ISO-8859-1" ?>
         <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
           <xsd:element name="BaseParams">
@@ -83,6 +85,7 @@
     PlugChildsTypes = []
     PlugMaxCount = None
     PluginMethods = []
+    LibraryControler = None
 
     def _AddParamsMembers(self):
         self.PlugParams = None
@@ -110,6 +113,9 @@
     def PluginXmlFilePath(self, PlugName=None):
         return os.path.join(self.PlugPath(PlugName), "plugin.xml")
 
+    def PluginLibraryFilePath(self, PlugName=None):
+        return os.path.join(os.path.join(os.path.split(__file__)[0], "plugins", self.PlugType, "pous.xml"))
+
     def PlugPath(self,PlugName=None):
         if not PlugName:
             PlugName = self.BaseParams.getName()
@@ -264,9 +270,13 @@
         return LocationCFilesAndCFLAGS, LDFLAGS, extra_files
 
     def BlockTypesFactory(self):
+        if self.LibraryControler is not None:
+            return [{"name" : "%s POUs" % self.PlugType, "list": self.LibraryControler.Project.GetCustomBlockTypes()}]
         return []
 
     def STLibraryFactory(self):
+        if self.LibraryControler is not None:
+            return self.LibraryControler.GenerateProgram()
         return ""
 
     def IterChilds(self):
@@ -528,7 +538,13 @@
         methode_name = os.path.join(self.PlugPath(PlugName), "methods.py")
         if os.path.isfile(methode_name):
             execfile(methode_name)
-
+        
+        # Get library blocks if plcopen library exist
+        library_path = self.PluginLibraryFilePath(PlugName)
+        if os.path.isfile(library_path):
+            self.LibraryControler = PLCControler()
+            self.LibraryControler.OpenXMLFile(library_path)
+        
         # Get the base xml tree
         if self.MandatoryParams:
             try:
@@ -604,7 +620,6 @@
 from time import localtime
 from datetime import datetime
 # import necessary stuff from PLCOpenEditor
-from PLCControler import PLCControler
 from PLCOpenEditor import PLCOpenEditor, ProjectDialog
 from TextViewer import TextViewer
 from plcopen.structures import IEC_KEYWORDS, TypeHierarchy_list
@@ -688,6 +703,9 @@
         # copy PluginMethods so that it can be later customized
         self.PluginMethods = [dic.copy() for dic in self.PluginMethods]
 
+    def PluginLibraryFilePath(self, PlugName=None):
+        return os.path.join(os.path.join(os.path.split(__file__)[0], "pous.xml"))
+
     def PlugTestModified(self):
          return self.ChangesToSave or not self.ProjectIsSaved()
 
@@ -875,6 +893,13 @@
             self.logger.write_error("Error in ST/IL/SFC code generator :\n%s\n"%result)
             return False
         plc_file = open(self._getIECcodepath(), "w")
+        if getattr(self, "PluggedChilds", None) is not None:
+            # Add ST Library from plugins
+            plc_file.write(self.STLibraryFactory())
+            plc_file.write("\n")
+            for child in self.IterChilds():
+                plc_file.write(child.STLibraryFactory())
+                plc_file.write("\n")
         if os.path.isfile(self._getIECrawcodepath()):
             plc_file.write(open(self._getIECrawcodepath(), "r").read())
             plc_file.write("\n")