view graster/hacklab-engraver/M101 @ 15:8ad629298649

major refactoring
author Robert McIntyre <rlm@mit.edu>
date Sun, 29 Aug 2010 19:02:26 -0400
parents f952052e37b7
children
line wrap: on
line source
1 #!/usr/bin/python
3 # === Graster Streaming Script ===
4 #
5 # This script is invoked by g-code M101 at the
6 # beginning of every graster job. It figures out
7 # the name of the mask file and forks a process
8 # to stream it to the HAL while AXIS executes the
9 # motion code.
10 #
11 # This file must be symlinked to the ~/emc2/nc_files dir
13 import emc, os, sys, threading, gtk, time, popen2, signal, select
15 os.chdir("/home/hacklab/emc2/configs/hacklab-engraver3")
16 ini = emc.ini("hacklab-engraver3.ini")
17 emc.nmlfile = ini.find("EMC","NML_FILE")
18 s = emc.stat()
19 s.poll()
20 ngc = s.file
21 (base,ext) = os.path.splitext(ngc)
22 gmask = base+".gmask"
23 if not os.path.exists(gmask):
24 gmask = ngc+".gmask"
25 if not os.path.exists(gmask):
26 os.system('zenity --error --text "Mask file \''+base+'.gmask\' not found. Laser disabled for this job."')
27 sys.exit(os.EX_NOINPUT)
30 def make_pbar():
31 (path,fn) = os.path.split(gmask)
32 message = "Streaming '"+fn+"'\n\n" + \
33 "This dialog should complete and close itself some time before\n" + \
34 "your job is done. If you stop the job, wait for this dialog\n" + \
35 "to go away before starting another one. If it won't go away,\n" + \
36 "click 'Cancel' and kill any processes containing 'M101'.\n"
37 return popen2.Popen3('zenity --progress --auto-close --auto-kill --title "Graster Streamer" --text "'+message+'"')
39 pbar = make_pbar()
41 total_lines = 0
42 fd = open(gmask,'r')
43 for x in fd.readlines():
44 total_lines += 1
45 fd.close()
47 total_lines = float(total_lines)
48 count = 0
50 print "Streaming '"+gmask+"'..."
51 pid = os.fork()
53 if pid:
54 time.sleep(2)
55 sys.exit(os.EX_OK)
56 else:
57 os.setsid()
58 fin = open(gmask,'r')
59 fout = os.popen('halstreamer','w')
60 try:
61 for line in fin.readlines():
62 #r,w,e = select.select((),(fout,),(fout,),0.1)
63 if pbar.poll() != -1: raise
65 #if len(w) > 0:
66 fout.write(line)
67 count += 1
68 #elif len(e) > 0:
69 # os.system("zenity --error --text 'Error writing to halstreamer. Job may not finish.'")
70 # raise
72 pbar.tochild.write(str(int(100.0*count/total_lines))+"\n")
74 finally:
75 print "...cleaning up..."
76 #fin.close()
77 #fout.close()
78 pbar.tochild.close()
79 pbar.fromchild.close()
80 #os.kill(pbar.pid,signal.SIGINT)
81 #os.kill(pbar.pid,signal.SIGKILL)
82 print "...done!"
83 os._exit(os.EX_OK)
84 #sys.exit(os.EX_OK)