Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
authorlaurent
Fri, 07 Aug 2009 18:27:50 +0200
changeset 367 a76ee5307bb7
parent 366 cd90e4c10261
child 368 86ecd8374dae
child 372 35cc4c6a2936
Adding support for python plugin wxglade_hmi allowing creation of PLC HMI using wxglade
Updating linux wxglade example according to this new plugin design
plugins/python/modules/wxglade_hmi/README
plugins/python/modules/wxglade_hmi/__init__.py
plugins/python/modules/wxglade_hmi/wxglade_hmi.py
plugins/python/python.py
tests/linux/wxGlade/plc.xml
tests/linux/wxGlade/python@python/HMIFrame@wxglade_hmi/baseplugin.xml
tests/linux/wxGlade/python@python/HMIFrame@wxglade_hmi/hmi.wxg
tests/linux/wxGlade/python@python/HMIFrame@wxglade_hmi/python.xml
tests/linux/wxGlade/python@python/baseplugin.xml
tests/linux/wxGlade/python@python/python.xml
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/plugins/python/modules/wxglade_hmi/README	Fri Aug 07 18:27:50 2009 +0200
@@ -0,0 +1,1 @@
+WxGlade HMI
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/plugins/python/modules/wxglade_hmi/__init__.py	Fri Aug 07 18:27:50 2009 +0200
@@ -0,0 +1,1 @@
+from wxglade_hmi import *
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/plugins/python/modules/wxglade_hmi/wxglade_hmi.py	Fri Aug 07 18:27:50 2009 +0200
@@ -0,0 +1,113 @@
+import wx
+import os, sys
+from xml.dom import minidom
+
+from plugger import opjimg
+from plugins.python import PythonCodeTemplate
+
+class RootClass(PythonCodeTemplate):
+
+    PluginMethods = [
+        {"bitmap" : opjimg("editWXGLADE"),
+         "name" : _("WXGLADE GUI"),
+         "tooltip" : _("Edit a WxWidgets GUI with WXGlade"),
+         "method" : "_editWXGLADE"},
+    ]
+
+    def _getWXGLADEpath(self):
+        # define name for IEC raw code file
+        return os.path.join(self.PlugPath(), "hmi.wxg")
+
+    def launch_wxglade(self, options, wait=False):
+        from wxglade import __file__ as fileName
+        path = os.path.dirname(fileName)
+        glade = os.path.join(path, 'wxglade.py')
+        if wx.Platform == '__WXMSW__':
+            glade = "\"%s\""%glade
+        mode = {False:os.P_NOWAIT, True:os.P_WAIT}[wait]
+        os.spawnv(mode, sys.executable, ["\"%s\""%sys.executable] + [glade] + options)
+
+
+    def PlugGenerate_C(self, buildpath, locations):
+        """
+        Return C code generated by iec2c compiler 
+        when _generate_softPLC have been called
+        @param locations: ignored
+        @return: [(C_file_name, CFLAGS),...] , LDFLAGS_TO_APPEND
+        """
+        
+        current_location = self.GetCurrentLocation()
+        # define a unique name for the generated C file
+        location_str = "_".join(map(lambda x:str(x), current_location))
+        
+        runtimefile_path = os.path.join(buildpath, "runtime_%s.py"%location_str)
+        runtimefile = open(runtimefile_path, 'w')
+        
+        hmi_frames = {}
+        
+        wxgfile_path=self._getWXGLADEpath()
+        if os.path.exists(wxgfile_path):
+            wxgfile = open(wxgfile_path, 'r')
+            wxgtree = minidom.parse(wxgfile)
+            wxgfile.close()
+            
+            for node in wxgtree.childNodes[1].childNodes:
+                if node.nodeType == wxgtree.ELEMENT_NODE:
+                    hmi_frames[node._attrs["name"].value] =  node._attrs["class"].value
+                    
+            hmipyfile_path=os.path.join(self._getBuildPath(), "hmi.py")
+            if wx.Platform == '__WXMSW__':
+                wxgfile_path = "\"%s\""%wxgfile_path
+                hmipyfile_path = "\"%s\""%hmipyfile_path
+            self.launch_wxglade(['-o', hmipyfile_path, '-g', 'python', wxgfile_path], wait=True)
+            
+            hmipyfile = open(hmipyfile_path, 'r')
+            runtimefile.write(hmipyfile.read())
+            hmipyfile.close()
+        
+        runtimefile.write(self.GetPythonCode())
+        runtimefile.write("""
+%(declare)s
+
+def _runtime_%(location)s_begin():
+    global %(global)s
+    
+    def OnCloseFrame(evt):
+        wx.MessageBox(_("Please stop PLC to close"))
+    
+    %(init)s
+    
+def _runtime_%(location)s_cleanup():
+    global %(global)s
+    
+    %(cleanup)s
+
+""" % {"location": location_str,
+       "declare": "\n".join(map(lambda x:"%s = None" % x, hmi_frames.keys())),
+       "global": ",".join(hmi_frames.keys()),
+       "init": "\n".join(map(lambda x: """
+    %(name)s = %(class)s(None)
+    %(name)s.Bind(wx.EVT_CLOSE, OnCloseFrame)
+    %(name)s.Show()
+""" % {"name": x[0], "class": x[1]},
+                             hmi_frames.items())),
+       "cleanup": "\n    ".join(map(lambda x:"%s.Destroy()" % x, hmi_frames.keys()))})
+        runtimefile.close()
+        
+        return [], "", False, ("runtime_%s.py"%location_str, file(runtimefile_path,"rb"))
+
+    def _editWXGLADE(self):
+        wxg_filename = self._getWXGLADEpath()
+        if not os.path.exists(wxg_filename):
+            hmi_name = self.BaseParams.getName()
+            open(wxg_filename,"w").write("""<?xml version="1.0"?>
+<application path="" name="" class="" option="0" language="python" top_window="%(name)s" encoding="UTF-8" use_gettext="0" overwrite="0" use_new_namespace="1" for_version="2.8" is_template="0">
+    <object class="%(class)s" name="%(name)s" base="EditFrame">
+        <style>wxDEFAULT_FRAME_STYLE</style>
+        <title>frame_1</title>
+    </object>
+</application>
+""" % {"name": hmi_name, "class": "Class_%s" % hmi_name})
+        if wx.Platform == '__WXMSW__':
+            wxg_filename = "\"%s\""%wxg_filename
+        self.launch_wxglade([wxg_filename])
--- a/plugins/python/python.py	Wed Jul 29 15:17:10 2009 +0200
+++ b/plugins/python/python.py	Fri Aug 07 18:27:50 2009 +0200
@@ -244,7 +244,7 @@
         plc_python_code = plc_python_file.read()
         plc_python_file.close()
         python_eval_fb_list = []
-        for v in plugin_root._VariablesList :
+        for v in plugin_root._VariablesList:
             if v["vartype"] == "FB" and v["type"] in ["PYTHON_EVAL","PYTHON_POLL"]:
                 python_eval_fb_list.append(v)
         python_eval_fb_count = max(1, len(python_eval_fb_list))
@@ -262,14 +262,6 @@
         runtimefile_path = os.path.join(buildpath, "runtime_%s.py"%location_str)
         runtimefile = open(runtimefile_path, 'w')
         runtimefile.write(self.GetPythonCode())
-        runtimefile.write("""
-def _runtime_%(location)s_begin():
-    print "runtime_begin"
-
-def _runtime_%(location)s_cleanup():
-    print "runtime_cleanup"
-
-""" % {"location": location_str})
         runtimefile.close()
         
         if wx.Platform == '__WXMSW__':
--- a/tests/linux/wxGlade/plc.xml	Wed Jul 29 15:17:10 2009 +0200
+++ b/tests/linux/wxGlade/plc.xml	Fri Aug 07 18:27:50 2009 +0200
@@ -6,9 +6,9 @@
   <fileHeader companyName="LOLITECH"
               productName="Beremiz"
               productVersion="0.0"
-              creationDateTime="2008-12-14 16:21:19"/>
+              creationDateTime="2008-12-14T16:21:19"/>
   <contentHeader name="Beremiz Python Support Tests"
-                 modificationDateTime="2009-01-18 18:40:44">
+                 modificationDateTime="2009-08-07T18:17:20">
     <coordinateInfo>
       <pageSize x="1024" y="1024"/>
       <fbd>
@@ -104,10 +104,10 @@
               </connectionPointOut>
               <expression>')'</expression>
             </inVariable>
-            <inVariable localId="28" height="30" width="350">
+            <inVariable localId="28" height="30" width="330">
               <position x="125" y="265"/>
               <connectionPointOut>
-                <relPosition x="350" y="15"/>
+                <relPosition x="330" y="15"/>
               </connectionPointOut>
               <expression>'HMIFrame.spin_ctrl_1.SetValue('</expression>
             </inVariable>
@@ -221,9 +221,9 @@
                     <relPosition x="0" y="50"/>
                     <connection refLocalId="28">
                       <position x="580" y="330"/>
-                      <position x="560" y="330"/>
-                      <position x="560" y="280"/>
-                      <position x="475" y="280"/>
+                      <position x="537" y="330"/>
+                      <position x="537" y="280"/>
+                      <position x="455" y="280"/>
                     </connection>
                   </connectionPointIn>
                 </variable>
@@ -421,8 +421,8 @@
     <configurations>
       <configuration name="conf_pytest">
         <resource name="res_pytest">
-          <task name="pytest_task" interval="00:00:00.100000" priority="0"/>
-          <pouInstance name="pytest_instance" type="main_pytest"/>
+          <task name="pytest_task" interval="t#100ms" priority="0"/>
+          <pouInstance name="pytest_instance" typeName="main_pytest"/>
         </resource>
       </configuration>
     </configurations>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/linux/wxGlade/python@python/HMIFrame@wxglade_hmi/baseplugin.xml	Fri Aug 07 18:27:50 2009 +0200
@@ -0,0 +1,2 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<BaseParams Name="HMIFrame" IEC_Channel="0"/>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/linux/wxGlade/python@python/HMIFrame@wxglade_hmi/hmi.wxg	Fri Aug 07 18:27:50 2009 +0200
@@ -0,0 +1,34 @@
+<?xml version="1.0"?>
+<!-- generated by wxGlade 0.6.3 on Fri Aug  7 18:16:44 2009 -->
+
+<application path="" name="" class="" option="0" language="python" top_window="HMIFrame" encoding="UTF-8" use_gettext="0" overwrite="0" use_new_namespace="1" for_version="2.8" is_template="0">
+    <object class="Class_HMIFrame" name="HMIFrame" base="EditFrame">
+        <style>wxDEFAULT_FRAME_STYLE</style>
+        <title>HMIFrame</title>
+        <object class="wxBoxSizer" name="sizer_1" base="EditBoxSizer">
+            <orient>wxVERTICAL</orient>
+            <object class="sizeritem">
+                <border>0</border>
+                <option>0</option>
+                <object class="wxSpinCtrl" name="spin_ctrl_1" base="EditSpinCtrl">
+                    <range>0, 10000</range>
+                </object>
+            </object>
+            <object class="sizeritem">
+                <border>0</border>
+                <option>0</option>
+                <object class="wxCheckBox" name="checkbox_1" base="EditCheckBox">
+                    <label>checkbox_1</label>
+                </object>
+            </object>
+            <object class="sizeritem">
+                <border>0</border>
+                <option>0</option>
+                <object class="wxStaticText" name="label_1" base="EditStaticText">
+                    <attribute>1</attribute>
+                    <label>GUI modifiée !</label>
+                </object>
+            </object>
+        </object>
+    </object>
+</application>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/linux/wxGlade/python@python/HMIFrame@wxglade_hmi/python.xml	Fri Aug 07 18:27:50 2009 +0200
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<Python xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.w3.org/2001/XMLSchema" xsi:schemaLocation="python_xsd.xsd">
+<![CDATA[]]>
+</Python>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/linux/wxGlade/python@python/baseplugin.xml	Fri Aug 07 18:27:50 2009 +0200
@@ -0,0 +1,2 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<BaseParams Name="python" IEC_Channel="0"/>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/linux/wxGlade/python@python/python.xml	Fri Aug 07 18:27:50 2009 +0200
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<Python xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.w3.org/2001/XMLSchema" xsi:schemaLocation="python_xsd.xsd">
+<![CDATA[import time,sys
+def myprintfunc(arg):
+    print arg
+    sys.stdout.flush()
+    return arg]]>
+</Python>