Repository navigation
Expand file tree
/
Copy patheggbot_gconvert.py
More file actions
118 lines (97 loc) · 4.77 KB
/
Copy patheggbot_gconvert.py
File metadata and controls
118 lines (97 loc) · 4.77 KB
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
import sys
import os
import argparse
COMMAND_PEN_DOWN_TEMPLATE = "M3 S31 (Pen down)\nG4 P{wait_time:.2f} (wait)"
COMMAND_PEN_UP_TEMPLATE = "M3 S90 (Pen up)\nG4 P{wait_time:.2f} (wait)"
GCODE_EXTENSIONS = (".gcode", ".ngc")
class ConversionStats(object):
def __init__(self):
self.g_command_count = 0
self.path_count = 0
def increment_g_command_count(self):
self.g_command_count = self.g_command_count + 1
def increment_path_count(self):
self.path_count = self.path_count + 1
def __str__(self):
return "Number of paths: {}\nNumber of G-commands: {}".format(self.path_count, self.g_command_count)
def preprocess_lines(lines, wait_time):
lines_without_m = []
for line in lines:
if line[0].upper() not in 'M%': #remove initial M-commands
lines_without_m.append(line.rstrip())
stats = ConversionStats()
COMMAND_PEN_DOWN = COMMAND_PEN_DOWN_TEMPLATE.format(wait_time=wait_time)
COMMAND_PEN_UP = COMMAND_PEN_UP_TEMPLATE.format(wait_time=wait_time)
processed_lines = []
processed_lines.append('%\n')
pen_is_down = False;
for line in lines_without_m:
line_words = line.upper().split()
new_line_words = []
if len(line_words)>0 and line_words[0][0] == 'G' and int(line_words[0][1:]) <= 9: # its a gcode motion line
stats.increment_g_command_count()
for word in line_words:
is_pen_command = (word[0] == 'Z')
if is_pen_command:
pen_should_be_down = (float(word[1:]) <= 0.0)
if pen_should_be_down and not pen_is_down:
stats.increment_path_count()
processed_lines.append(COMMAND_PEN_DOWN)
elif not pen_should_be_down and pen_is_down:
processed_lines.append(COMMAND_PEN_UP)
pen_is_down = pen_should_be_down
else:
new_line_words.append(word)
if len(new_line_words) > 2 or (len(new_line_words) > 1 and new_line_words[1][0] != 'F'): # get rid of empty feed command
processed_lines.append(" ".join(new_line_words))
else:
processed_lines.append(line)
processed_lines.append('(M5 fake stop spindle)')
processed_lines.append('%\n')
return processed_lines, stats
def preprocess_file(infile_path, outfile_path, wait_time):
infile_lines = []
with open(infile_path, 'r') as infile:
for fileline in infile:
infile_lines.append(fileline)
outfile_lines, stats = preprocess_lines(infile_lines, wait_time)
with open(outfile_path, 'w') as outfile:
outfile.write('\n'.join(outfile_lines))
return stats
def main():
parser = argparse.ArgumentParser(description="Converts gcode files from inkscape so they can be used by an eggbot (e.g. replaces z-movements with pen movements)",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("filename", help="The .gcode file to convert to eggbot compatible gcode")
parser.add_argument("--force", "-f", action="store_true", help="Force usage of input file even if wrong extension, and output file even if it will be overwritten.")
parser.add_argument("--wait-time", "-w", default=0.25, type=float, help="Time to wait between pen up/down commands.")
parser.add_argument("--output", "-o", default=argparse.SUPPRESS, help="Output file name to use. Derived from input file if omitted.")
parser.add_argument("--no-stat", action="store_true", help="Do not print stats collected during conversion.")
ns = parser.parse_args()
# Get passed arguments
infile_path = ns.filename
force = ns.force
wait_time = ns.wait_time
print_stats = not ns.no_stat
# Check input file
infile_basename, infile_extension = os.path.splitext(infile_path)
if infile_extension not in GCODE_EXTENSIONS and not force:
print("Wrong input file extension. Exiting", file=sys.stderr)
return
# Get output file path
if "output" in ns:
outfile_path = ns.output
else:
outfile_path = infile_basename + ".eggbot" + infile_extension
print("Output file: '{}'".format(outfile_path), file=sys.stderr)
# Check output file
output_exists = os.path.isfile(outfile_path)
if output_exists and force:
print("The output file already exists and will be overwritten.", file=sys.stderr)
elif output_exists and not force:
print("The output file already exists. Exiting", file=sys.stderr)
return
stats = preprocess_file(infile_path, outfile_path, wait_time)
if print_stats:
print(stats)
if __name__=="__main__":
main()