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