forked from michaelfranzl/grbl-gui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgerbil_gui.py
executable file
·263 lines (222 loc) · 8.45 KB
/
gerbil_gui.py
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#!/usr/bin/python3
# copyright 2015/2016 Michael Franzl
# MIT License
import argparse
import logging
import sys
import time
from classes.session import Session
from classes.svg import SVG
from gerbil.gerbil import Gerbil
from lib import stipple
from lib import pixel2laser as p2l
from lib import gcodetools
from lib import utility
from classes.window import Ui_MainWindow
#from gi.repository import Gtk
from PyQt5.QtWidgets import (QApplication, QHBoxLayout, QMessageBox, QSlider, QWidget, QDialog, QMainWindow)
from classes.window import MainWindow
def main():
'''
This function does nothing else than parsing command line arguments.
It delegates to library functions accordingly.
'''
#log_format = '%(asctime)s %(levelname)s %(message)s'
#logging.basicConfig(level=logging.INFO, format=log_format)
logger = logging.getLogger('grbl_gui')
logger.setLevel(5)
parser = argparse.ArgumentParser(description='This program is a box full of useful CNC tools')
# Set up sub-commands like git uses them
subparsers = parser.add_subparsers(help="Available subcommands")
# define arguments for the 'stipple' subcommand
stipple_parser = subparsers.add_parser(
"stipple",
help="Creates stippling art for laser-engraving based on files created by third-party C++ programs voronoi_stippler and concorde.",
epilog="EXAMPLE: python ./grbl_gui.py stipple ./data/grace.crd ./data/grace.sol out.svg --weight ./data/grace.wgt"
)
stipple_parser.add_argument(
'crd_file',
metavar='COORD_FILE',
help='File containing point coordinates in concorde format. Use modified voronoi_stippler to generate it.'
)
stipple_parser.add_argument(
'idx_file',
metavar='INDEX_FILE',
help='File containing TSP indices in concorde format. Use concorde to solve the TSP problem.'
)
stipple_parser.add_argument(
'out_file',
metavar='OUT_FILE',
help='File to write the result to. Currently only .svg output is supported. Gcode output will be supported soon.'
)
stipple_parser.add_argument(
'--weight',
metavar='WEIGHT_FILE',
help='File containing weights for ealogger_ch point. Use modified voronoi_stippler to generate it.'
)
# define arguments for the 'p2l' (pixel2laser) subcommand
p2l_parser = subparsers.add_parser(
"pixel2laser",
help="Generate optimized Gcode from PNG for laser.",
epilog="EXAMPLE: python ./grbl_gui.py p2l ./data/pixel2laser.png p2l.ngc"
)
p2l_parser.add_argument(
'in_file',
metavar='IN_FILE',
help='PNG file to be read. Other file formats have not been tested.'
)
p2l_parser.add_argument(
'out_file',
metavar='OUT_FILE',
help='File to write the result to.'
)
# define arguments for the 'stream' subcommand
stream_parser = subparsers.add_parser("stream", help="Streams a gcode file to GRBL.")
stream_parser.add_argument(
'dev_node',
metavar='DEV_NODE',
help='Interface node in /dev file system. E.g. /dev/ttyACM0'
)
stream_parser.add_argument(
'gcodefile',
metavar='GCODE_FILE',
help='File to stream'
)
# define arguments for the 'bbox' subcommand
bbox_parser = subparsers.add_parser("bbox", help="Calculates the bounding box of a gcode file")
bbox_parser.add_argument(
'gcodefile',
metavar='GCODE_FILE',
help='File to find the bounding box for'
)
# This parent parser provides args for infile and outfile for less repetition
parent_parser = argparse.ArgumentParser(add_help=False)
parent_parser.add_argument(
'infile',
metavar='GCODE_INFILE',
help='File to read from'
)
parent_parser.add_argument(
'outfile',
metavar='GCODE_OUTFILE',
help='File to write results to'
)
# define arguments for the 'translate' subcommand
translate_parser = subparsers.add_parser("translate", help="Translates gcode by X, Y, Z offsets", parents=[parent_parser])
translate_parser.add_argument(
'offset_x',
metavar='OFFSET_X',
help='offset x'
)
translate_parser.add_argument(
'offset_y',
metavar='OFFSET_Y',
help='offset y'
)
translate_parser.add_argument(
'offset_z',
metavar='OFFSET_Z',
help='offset z'
)
# define arguments for the 'scale_factor' subcommand
scalefactor_parser = subparsers.add_parser("scale_factor", help="Scales gcode by X, Y, Z factors", parents=[parent_parser])
scalefactor_parser.add_argument(
'scale_x',
metavar='SCALE_X',
help='scale x'
)
scalefactor_parser.add_argument(
'scale_y',
metavar='SCALE_Y',
help='scale y'
)
scalefactor_parser.add_argument(
'scale_z',
metavar='SCALE_Z',
help='scale z'
)
# define arguments for the 'scale_factor' subcommand
scaleinto_parser = subparsers.add_parser("scale_into", help="Scales and translates gcode into a bbox specified by (0,0,-,width,height,depth)", parents=[parent_parser])
scaleinto_parser.add_argument(
'width',
metavar='WIDTH',
help='width in mm'
)
scaleinto_parser.add_argument(
'height',
metavar='HEIGHT',
help='height mm'
)
scaleinto_parser.add_argument(
'depth',
metavar='DEPTH',
help='depth in mm'
)
# define arguments for the 'scale' subcommand
to_origin_parser = subparsers.add_parser("2origin", help="Moves bottom left extremity of gcode to (0,0), z remains unaffected", parents=[parent_parser])
# define arguments for the 'gui' subcommand
gui_parser = subparsers.add_parser("gui", help="Start GUI")
gui_parser.add_argument(
'--path',
metavar='PATH',
default='/dev/ttyACM0',
help='e.g. /dev/ttyACM0'
)
gui_parser.add_argument(
'--baud',
metavar='BAUD',
help='e.g. 9600',
default=115200
)
args = parser.parse_args()
if len(sys.argv) < 2:
print("Please use the -h flag to see help")
raise SystemExit
subcmd = sys.argv[1]
# after all arguments have been parsed, delegate
if subcmd == "stipple":
stipple.do(args.crd_file, args.idx_file, args.weight, args.out_file)
elif subcmd == "pixel2laser":
gcode = p2l.do(args.in_file)
f = open(args.out_file, 'w')
f.write(gcode)
f.close()
elif subcmd == "stream":
grbl = Gerbil("grbl1", args.dev_node)
grbl.cnect()
time.sleep(1)
src = "out.ngc"
grbl.send(src)
elif subcmd == "bbox":
lines = utility.read_file_to_linearray(args.gcodefile)
bbox = gcodetools.get_bbox(lines)
print("BBOX: {}".format(bbox))
elif subcmd == "translate":
lines = utility.read_file_to_linearray(args.infile)
result = gcodetools.translate(lines, [float(args.offset_x), float(args.offset_y), float(args.offset_z)])
utility.write_file_from_linearray(result, args.outfile)
elif subcmd == "scale_factor":
lines = utility.read_file_to_linearray(args.infile)
result = gcodetools.scale_factor(lines, [float(args.scale_x), float(args.scale_y), float(args.scale_z)], False)
utility.write_file_from_linearray(result, args.outfile)
elif subcmd == "scale_into":
lines = utility.read_file_to_linearray(args.infile)
result = gcodetools.scale_into(lines, float(args.width), float(args.height), float(args.depth), False)
utility.write_file_from_linearray(result, args.outfile)
elif subcmd == "2origin":
lines = utility.read_file_to_linearray(args.infile)
result = gcodetools.move_to_origin(lines)
utility.write_file_from_linearray(result, args.outfile)
elif subcmd == "gui":
app = QApplication(sys.argv)
#styles = [line.strip() for line in open("stylesheet.css")]
#styles = " ".join(styles)
#app.setStyleSheet(styles)
window = MainWindow(args.path, int(args.baud))
#ui = Ui_MainWindow()
#ui.setupUi(window)
#window = Window()
window.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()