wxglade_hmi/wxglade_hmi.py
changeset 1132 28f96aa9c070
parent 1131 f794fbff8f02
child 1151 38d6aaad8ffd
equal deleted inserted replaced
1131:f794fbff8f02 1132:28f96aa9c070
    36             shutil.copyfile(self._getWXGLADEpath(from_project_path),
    36             shutil.copyfile(self._getWXGLADEpath(from_project_path),
    37                             self._getWXGLADEpath())
    37                             self._getWXGLADEpath())
    38         return PythonFileCTNMixin.OnCTNSave(self, from_project_path)
    38         return PythonFileCTNMixin.OnCTNSave(self, from_project_path)
    39 
    39 
    40     def CTNGenerate_C(self, buildpath, locations):
    40     def CTNGenerate_C(self, buildpath, locations):
    41         """
       
    42         Return C code generated by iec2c compiler 
       
    43         when _generate_softPLC have been called
       
    44         @param locations: ignored
       
    45         @return: [(C_file_name, CFLAGS),...] , LDFLAGS_TO_APPEND
       
    46         """
       
    47         
       
    48         current_location = self.GetCurrentLocation()
       
    49         # define a unique name for the generated C file
       
    50         location_str = "_".join(map(lambda x:str(x), current_location))
       
    51         
       
    52         runtimefile_path = os.path.join(buildpath, "runtime_%s.py"%location_str)
       
    53         runtimefile = open(runtimefile_path, 'w')
       
    54         
    41         
    55         hmi_frames = {}
    42         hmi_frames = {}
    56         
    43         
    57         wxgfile_path=self._getWXGLADEpath()
    44         wxgfile_path=self._getWXGLADEpath()
    58         if os.path.exists(wxgfile_path):
    45         if os.path.exists(wxgfile_path):
    71             else:
    58             else:
    72                 wxghmipyfile_path = hmipyfile_path
    59                 wxghmipyfile_path = hmipyfile_path
    73             self.launch_wxglade(['-o', wxghmipyfile_path, '-g', 'python', wxgfile_path], wait=True)
    60             self.launch_wxglade(['-o', wxghmipyfile_path, '-g', 'python', wxgfile_path], wait=True)
    74             
    61             
    75             hmipyfile = open(hmipyfile_path, 'r')
    62             hmipyfile = open(hmipyfile_path, 'r')
    76             runtimefile.write(hmipyfile.read())
    63             define_hmi = hmipyfile.read().decode('utf-8')
    77             hmipyfile.close()
    64             hmipyfile.close()
    78         
    65         
    79         sections_code = self.GetSectionsCode()
    66         declare_hmi = "\n".join(map(lambda x:"%s = None" % x,
       
    67                                 hmi_frames.keys()))
       
    68         global_hmi = "global "+",".join(hmi_frames.keys())
       
    69         init_hmi = "\n".join(map(lambda x: """\
       
    70 def OnCloseFrame(evt):
       
    71     wx.MessageBox(_("Please stop PLC to close"))
       
    72 
       
    73 %(name)s = %(class)s(None)
       
    74 %(name)s.Bind(wx.EVT_CLOSE, OnCloseFrame)
       
    75 %(name)s.Show()
       
    76 """ % {"name": x[0], "class": x[1]}, hmi_frames.items()))
       
    77         cleanup_hmi = "\n".join(map(lambda x:"if %s is not None: %s.Destroy()" % (x,x), hmi_frames.keys()))
    80         
    78         
    81         # Adding variables
    79         self.PreSectionsTexts = {
    82         runtimefile.write("## User variables reference\n" +
    80             "globals":define_hmi + declare_hmi,
    83                           sections_code["variables"] + "\n")
    81             "start":global_hmi,
    84         
    82             "stop":global_hmi
    85         # Adding user global variables and routines
    83         }
    86         runtimefile.write("## User internal user variables and routines\n" +
    84         self.PostSectionsTexts = {
    87                           sections_code["globals"] + "\n")
    85             "start":init_hmi,
    88         
    86         }
    89         for section in ["init", "cleanup"]:
       
    90             if not sections_code[section]:
       
    91                 sections_code[section] = "    pass"
       
    92         
       
    93         sections_code.update({
       
    94             "location": location_str,
       
    95             "declare_hmi": "\n".join(map(lambda x:"%s = None" % x, hmi_frames.keys())),
       
    96             "global_hmi": ",".join(hmi_frames.keys()),
       
    97             "init_hmi": "\n".join(map(lambda x: """
       
    98     %(name)s = %(class)s(None)
       
    99     %(name)s.Bind(wx.EVT_CLOSE, OnCloseFrame)
       
   100     %(name)s.Show()
       
   101 """ % {"name": x[0], "class": x[1]},
       
   102                              hmi_frames.items())),
       
   103             "cleanup_hmi": "\n    ".join(map(lambda x:"if %s is not None: %s.Destroy()" % (x,x), hmi_frames.keys()))})
       
   104         
       
   105         runtimefile.write("""
       
   106 %(declare_hmi)s
       
   107 
    87 
   108 def _runtime_%(location)s_init():
    88         return PythonFileCTNMixin.CTNGenerate_C(self, buildpath, locations)
   109 %(init)s
       
   110 
       
   111 def _runtime_%(location)s_cleanup():
       
   112 %(cleanup)s
       
   113 
       
   114 def _runtime_%(location)s_start():
       
   115     global %(global_hmi)s
       
   116     
       
   117 %(start)s
       
   118 
       
   119     def OnCloseFrame(evt):
       
   120         wx.MessageBox(_("Please stop PLC to close"))
       
   121     
       
   122     %(init_hmi)s
       
   123     
       
   124 def _runtime_%(location)s_stop():
       
   125     global %(global_hmi)s
       
   126     
       
   127     %(cleanup_hmi)s
       
   128 
       
   129 %(stop)s
       
   130 
       
   131 """ % sections_code)
       
   132         runtimefile.close()
       
   133         
       
   134         return [], "", False, ("runtime_%s.py"%location_str, file(runtimefile_path,"rb"))
       
   135 
    89 
   136     def _editWXGLADE(self):
    90     def _editWXGLADE(self):
   137         wxg_filename = self._getWXGLADEpath()
    91         wxg_filename = self._getWXGLADEpath()
   138         open_wxglade = True
    92         open_wxglade = True
   139         if not self.GetCTRoot().CheckProjectPathPerm():
    93         if not self.GetCTRoot().CheckProjectPathPerm():