File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import argparse
2+ import os
13from gcodeparser import GcodeParser
24
5+
36if __name__ == '__main__' :
4- with open ('3DBenchy.gcode' , 'r' ) as f :
7+ parser = argparse .ArgumentParser (
8+ prog = 'GcodeParser' ,
9+ description = 'Converts a gcode file into an array of Python objects representing each command' ,
10+ )
11+
12+ parser .add_argument (
13+ 'input_file' ,
14+ help = 'Path to the gcode file to be parsed' ,
15+ )
16+
17+ parser .add_argument (
18+ '-o' ,
19+ '--output_file' ,
20+ help = 'Path to the output file where the parsed gcode will be saved. Defaults to stdout.' ,
21+ default = '-' ,
22+ )
23+
24+ args = parser .parse_args ()
25+
26+ with open (os .path .expanduser (args .input_file ), 'r' ) as f :
527 gcode = f .read ()
628 parsed_gcode = GcodeParser (gcode )
29+ if args .output_file == '-' :
30+ for line in parsed_gcode .lines :
31+ print (line )
32+ else :
33+ with open (os .path .expanduser (args .output_file ), 'w' ) as f :
34+ for line in parsed_gcode .lines :
35+ f .write (str (line ) + '\n ' )
736 pass
Original file line number Diff line number Diff line change @@ -129,4 +129,25 @@ def test_multi_line():
129129 assert get_lines ('G91\n G1 X-10 Y20 ; inline comment\n G1 Z0.5\n T1\n M350 T100' ) == lines
130130 assert get_lines ('G91G1 X-10 Y20;inline comment\n G1 Z0.5\n T1M350 T100' ) == lines
131131 assert get_lines (' \t G91\n \t G1\t X-10 Y20 \t ;\t inline comment\n G1 Z0.5\n T1\n M350 T100' ) == lines
132- assert get_lines ('G91\n G1 X-10 Y20 ; inline comment\n ; comment to be excluded\n G1 Z0.5\n T1\n M350 T100' , include_comments = False ) == lines
132+ assert get_lines ('G91\n G1 X-10 Y20 ; inline comment\n ; comment to be excluded\n G1 Z0.5\n T1\n M350 T100' ,
133+ include_comments = False ) == lines
134+ assert get_lines ('G91 G1 X-10 Y20 ; inline comment\n ; comment to be excluded\n G1 Z0.5\n T1\n M350 T100' ,
135+ include_comments = False ) == lines
136+
137+
138+ def test_multi_line2 ():
139+ """ We want to ignore things that look like gcode in the comments
140+ """
141+ lines = [
142+ GcodeLine (
143+ command = ('G' , 91 ),
144+ params = {},
145+ comment = '' ,
146+ ),
147+ GcodeLine (
148+ command = ('G' , 1 ),
149+ params = {'X' : 100 },
150+ comment = 'comment G90'
151+ )
152+ ]
153+ assert get_lines ('G91 G1 X100 ; comment G90' ) == lines
You can’t perform that action at this time.
0 commit comments