rlm@11: #!/usr/bin/python rlm@11: rlm@11: # === Graster Streaming Script === rlm@11: # rlm@11: # This script is invoked by g-code M101 at the rlm@11: # beginning of every graster job. It figures out rlm@11: # the name of the mask file and forks a process rlm@11: # to stream it to the HAL while AXIS executes the rlm@11: # motion code. rlm@11: # rlm@11: # This file must be symlinked to the ~/emc2/nc_files dir rlm@11: rlm@11: import emc, os, sys, threading, gtk, time, popen2, signal, select rlm@11: rlm@11: os.chdir("/home/hacklab/emc2/configs/hacklab-engraver3") rlm@11: ini = emc.ini("hacklab-engraver3.ini") rlm@11: emc.nmlfile = ini.find("EMC","NML_FILE") rlm@11: s = emc.stat() rlm@11: s.poll() rlm@11: ngc = s.file rlm@11: (base,ext) = os.path.splitext(ngc) rlm@11: gmask = base+".gmask" rlm@11: if not os.path.exists(gmask): rlm@11: gmask = ngc+".gmask" rlm@11: if not os.path.exists(gmask): rlm@11: os.system('zenity --error --text "Mask file \''+base+'.gmask\' not found. Laser disabled for this job."') rlm@11: sys.exit(os.EX_NOINPUT) rlm@11: rlm@11: rlm@11: def make_pbar(): rlm@11: (path,fn) = os.path.split(gmask) rlm@11: message = "Streaming '"+fn+"'\n\n" + \ rlm@11: "This dialog should complete and close itself some time before\n" + \ rlm@11: "your job is done. If you stop the job, wait for this dialog\n" + \ rlm@11: "to go away before starting another one. If it won't go away,\n" + \ rlm@11: "click 'Cancel' and kill any processes containing 'M101'.\n" rlm@11: return popen2.Popen3('zenity --progress --auto-close --auto-kill --title "Graster Streamer" --text "'+message+'"') rlm@11: rlm@11: pbar = make_pbar() rlm@11: rlm@11: total_lines = 0 rlm@11: fd = open(gmask,'r') rlm@11: for x in fd.readlines(): rlm@11: total_lines += 1 rlm@11: fd.close() rlm@11: rlm@11: total_lines = float(total_lines) rlm@11: count = 0 rlm@11: rlm@11: print "Streaming '"+gmask+"'..." rlm@11: pid = os.fork() rlm@11: rlm@11: if pid: rlm@11: time.sleep(2) rlm@11: sys.exit(os.EX_OK) rlm@11: else: rlm@11: os.setsid() rlm@11: fin = open(gmask,'r') rlm@11: fout = os.popen('halstreamer','w') rlm@11: try: rlm@11: for line in fin.readlines(): rlm@11: #r,w,e = select.select((),(fout,),(fout,),0.1) rlm@11: if pbar.poll() != -1: raise rlm@11: rlm@11: #if len(w) > 0: rlm@11: fout.write(line) rlm@11: count += 1 rlm@11: #elif len(e) > 0: rlm@11: # os.system("zenity --error --text 'Error writing to halstreamer. Job may not finish.'") rlm@11: # raise rlm@11: rlm@11: pbar.tochild.write(str(int(100.0*count/total_lines))+"\n") rlm@11: rlm@11: finally: rlm@11: print "...cleaning up..." rlm@11: #fin.close() rlm@11: #fout.close() rlm@11: pbar.tochild.close() rlm@11: pbar.fromchild.close() rlm@11: #os.kill(pbar.pid,signal.SIGINT) rlm@11: #os.kill(pbar.pid,signal.SIGKILL) rlm@11: print "...done!" rlm@11: os._exit(os.EX_OK) rlm@11: #sys.exit(os.EX_OK) rlm@11: