def OnCTNSave(self, from_project_path=None): if from_project_path is not None: shutil.copyfile(self._getWXGLADEpath(from_project_path), self._getWXGLADEpath()) return PythonFileCTNMixin.OnCTNSave(self, from_project_path)
def CTNGenerate_C(self, buildpath, locations): 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.append({ "name": node.getAttribute("name"), "class": node.getAttribute("class"), "handlers": [ hnode.firstChild.data for hnode in node.getElementsByTagName("handler") ] }) hmipyfile_path = os.path.join(self._getBuildPath(), "hmi.py") if wx.Platform == '__WXMSW__': wxgfile_path = "\"%s\"" % wxgfile_path wxghmipyfile_path = "\"%s\"" % hmipyfile_path else: wxghmipyfile_path = hmipyfile_path self.launch_wxglade( ['-o', wxghmipyfile_path, '-g', 'python', wxgfile_path], wait=True) hmipyfile = open(hmipyfile_path, 'r') define_hmi = hmipyfile.read().decode('utf-8') hmipyfile.close() else: define_hmi = "" declare_hmi = "\n".join([ "%(name)s = None\n" % x + "\n".join([ "%(class)s.%(h)s = %(h)s" % dict(x, h=h) for h in x['handlers'] ]) for x in hmi_frames ]) global_hmi = ("global %s\n" % ",".join([x["name"] for x in hmi_frames]) if len(hmi_frames) > 0 else "") init_hmi = "\n".join([ """\ def OnCloseFrame(evt): wx.MessageBox(_("Please stop PLC to close")) %(name)s = %(class)s(None) %(name)s.Bind(wx.EVT_CLOSE, OnCloseFrame) %(name)s.Show() """ % x for x in hmi_frames ]) cleanup_hmi = "\n".join([ "if %(name)s is not None: %(name)s.Destroy()" % x for x in hmi_frames ]) self.PreSectionsTexts = { "globals": define_hmi, "start": global_hmi, "stop": global_hmi + cleanup_hmi } self.PostSectionsTexts = { "globals": declare_hmi, "start": init_hmi, } return PythonFileCTNMixin.CTNGenerate_C(self, buildpath, locations)
def CTNGenerate_C(self, buildpath, locations): # list containing description of all objects declared in wxglade hmi_objects = [] # list containing only description of the main frame object main_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: name = node.getAttribute("name") wxglade_object_desc = { "name": name, "class": node.getAttribute("class"), "handlers": [ hnode.firstChild.data for hnode in node.getElementsByTagName("handler") ] } hmi_objects.append(wxglade_object_desc) if name == self.CTNName(): main_frames.append(wxglade_object_desc) hmipyfile_path = os.path.join(self._getBuildPath(), "hmi.py") if wx.Platform == '__WXMSW__': wxgfile_path = "\"%s\"" % wxgfile_path wxghmipyfile_path = "\"%s\"" % hmipyfile_path else: wxghmipyfile_path = hmipyfile_path self.launch_wxglade( ['-o', wxghmipyfile_path, '-g', 'python', wxgfile_path], wait=True) hmipyfile = open(hmipyfile_path, 'r') define_hmi = hmipyfile.read().decode('utf-8') hmipyfile.close() else: define_hmi = "" global_hmi = ("global %s\n" % ",".join([x["name"] for x in main_frames]) if len(main_frames) > 0 else "") declare_hmi = \ "\n".join(["%(name)s = None\n" % x for x in main_frames]) + \ "\n".join(["\n".join(["%(class)s.%(h)s = %(h)s" % dict(x, h=h) for h in x['handlers']]) for x in hmi_objects]) + """\ def OnCloseFrame(evt): wx.MessageBox(_("Please stop PLC to close")) def InitHMI(): """ + global_hmi + "\n" + "\n".join(["""\ %(name)s = %(class)s(None) %(name)s.Bind(wx.EVT_CLOSE, OnCloseFrame) %(name)s.Show() """ % x for x in main_frames]) + """\ def CleanupHMI(): """ + global_hmi + "\n" + "\n".join(["""\ if %(name)s is not None: %(name)s.Destroy() """ % x for x in main_frames]) self.PreSectionsTexts = { "globals": define_hmi, "start": global_hmi, "stop": "CleanupHMI()\n" } self.PostSectionsTexts = { "globals": declare_hmi, "start": "InitHMI()\n", } if len(main_frames) == 0 and \ len(getattr(self.CodeFile, "start").getanyText().strip()) == 0: self.GetCTRoot().logger.write_warning( _("Warning: WxGlade HMI has no object with name identical to extension name, and no python code is provided in start section to create object.\n" )) return PythonFileCTNMixin.CTNGenerate_C(self, buildpath, locations)