-
Notifications
You must be signed in to change notification settings - Fork 1
/
gcodeTransformer.py
280 lines (227 loc) · 9.16 KB
/
gcodeTransformer.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import argparse
import sys
import re
import io
########################################################
arg = argparse.ArgumentParser(add_help=False)
arg.add_argument ('-f', '--file', required=True, help='input file name (required)')
arg.add_argument ('-o', '--output_file', default='', help='output_file = output file name (if not specified, it will return the code generated)')
arg.add_argument ('-x', '--move_x', default=0, help='move_x = X offset in mm')
arg.add_argument ('-y', '--move_y', default=0, help='move_y = B offset in mm')
arg.add_argument ('-r', '--rotate', default=0, help='rotate = rotate angle (times 90)')
arg.add_argument ('-k', '--keep_space', nargs='?', help='to keep empty space around gcode (page size) on rotate if not will gcode will fit to the axe')
arg.add_argument ('-w', '--width', default=0, help='page width require if --keep_space used')
arg.add_argument ('-h', '--height', default=0, help='page height require if --keep_space used')
arg.add_argument ('-m', '--mirror', default=0, help='mirror = axis mirroring (axis name)')
arg.add_argument ('-s', '--scale', default=0, help='scale = percentage scale, negative is supported')
arg.add_argument ('-c', '--combine', default=0, help='combine = combine code (example 2x3x10 or 1x2x8 ... where the first number on X is the second number on Y is the third indent) ')
arg.add_argument ('-rd', '--round', default=-1, help='round X and Y value, can be combined with another operation or used alone')
arg.parse_args()
input_file = arg.parse_args().file
output_file = arg.parse_args().output_file
move_x = int(arg.parse_args().move_x)
move_y = int(arg.parse_args().move_y)
rotate = int(arg.parse_args().rotate)
width = int(float(arg.parse_args().width)) # to prevent error on flot value
height = int(float(arg.parse_args().height))
mirror = arg.parse_args().mirror
scale = int(arg.parse_args().scale)
combine = arg.parse_args().combine
roundIt = int(arg.parse_args().round)
keepSpace = False
if '-k' in sys.argv or '--keep_space' in sys.argv:
if width <= 0 or height <= 0:
raise ValueError('width and height required!')
keepSpace = True
# ==============================================================================
with open(input_file) as file:
comma = ';'
data = file.read()
if comma in data:
data = data.replace(';','')
else:
comma = ''
# ==============================================================================
def roundUp(x):
if roundIt > -1:
return round(x,roundIt)
return x
def sizes(i_data):
max_x = max_y = min_x = min_y = 0
for row in data.split('\n'):
for b in row.split():
if b.find('X') != -1:
x = float(b.replace('X', ''))
if x > max_x:
max_x = x
if x < min_x:
min_x = x
if b.find('Y') != -1:
y = float(b.replace('Y', ''))
if y > max_y:
max_y = y
if y < min_y:
min_y = y
size_x = max_x - min_x
size_y = max_y - min_y
return [max_x, max_y, min_x, min_y, size_x, size_y]
# ==============================================================================
def rotate_90(i_data):
n_data = ''
for row in i_data.split('\n'):
n_row = []
for b in row.split():
if b.find('X') != -1:
n_val = roundUp(-float(b.replace('X', ''))+g_size[4])
n_b = '{axis}{val}'.format(axis='Y', val=n_val)
elif b.find('Y') != -1:
val = b.replace('Y', '')
n_b = '{axis}{val}'.format(axis='X', val=roundUp(float(val)))
else:
n_b = b
n_row.append(n_b)
n_data += "{n_r}\n".format(n_r=' '.join(n_row))
return n_data
def rotate_270(i_data):
n_data = ''
for row in i_data.split('\n'):
X = re.findall(r'X([0-9\.e\-]+)', row, re.IGNORECASE)
Y = re.findall(r'Y([0-9\.e\-]+)', row, re.IGNORECASE)
if len(X)>0 and len(Y)>0:
x = roundUp(-float(Y[0])+g_size[5])
y = roundUp(float(X[0]))
row = re.sub(r"X[0-9\.e\-]+", f"X{x}", row)
row = re.sub(r"Y[0-9\.e\-]+", f"Y{y}", row)
n_data += f"{row}\n"
return n_data
def rotate_180(i_data):
n_data = ''
for row in i_data.split('\n'):
X = re.findall(r'X([0-9\.e\-]+)', row, re.IGNORECASE)
Y = re.findall(r'Y([0-9\.e\-]+)', row, re.IGNORECASE)
if len(X)>0 and len(Y)>0:
x = roundUp(-float(X[0])+g_size[4])
y = roundUp(-float(Y[0])+g_size[5])
row = re.sub(r"X[0-9\.e\-]+", f"X{x}", row)
row = re.sub(r"Y[0-9\.e\-]+", f"Y{y}", row)
n_data += f"{row}\n"
return n_data
if rotate != 0:
if keepSpace:
frame = f"\nG1 F1000 X{width} Y0 TEST\nG1 X{width} Y{height} TEST\nG1 X0 Y{height} TEST\nG1 X0 Y0 TEST"
data = f"{data}{frame}"
g_size = sizes(data)
if rotate == -90 or rotate == 270:
data = rotate_270(data)
elif abs(rotate) == 180:
data = rotate_180(data)
elif rotate == 90 or rotate == -270:
data = rotate_90(data)
if keepSpace:
# clean up
data = re.sub(r"G1.+?(?=TEST)TEST\n?", "", data)
if rotate%90 != 0 or abs(rotate) > 360:
raise ValueError('angle should be a multiple of 90!')
# ==============================================================================
def mirror_xy(i_data, xy):
n_data = ''
for row in i_data.split('\n'):
n_row = []
for b in row.split():
if b.find('X') != -1:
if xy == 'X':
n_val = roundUp(-float(b.replace('X', ''))+g_size[4])
n_b = 'X{val}'.format(val=n_val)
elif b.find('Y') != -1:
if xy == 'Y':
n_val = roundUp(-float(b.replace('Y', ''))+g_size[5])
n_b = 'Y{val}'.format(val=n_val)
else:
n_b = b
n_row.append(n_b)
n_data += "{n_r}\n".format(n_r=' '.join(n_row))
return n_data
if mirror != 0:
g_size = sizes(data)
data = mirror_xy(data, mirror)
# ==============================================================================
def scale_code(i_data, rate):
rate = float(rate / 100)
n_data = ''
for row in i_data.split('\n'):
n_row = []
for b in row.split():
if b.find('X') != -1:
x = float(b.replace('X', ''))
n_b = 'X{n_x}'.format(n_x=roundUp((x - g_size[2]) * rate + x))
elif b.find('Y') != -1:
y = float(b.replace('Y', ''))
n_b = 'Y{n_y}'.format(n_y=roundUp((y - g_size[3]) * rate + y))
else:
n_b = b
n_row.append(n_b)
n_data += "{n_r}\n".format(n_r=' '.join(n_row))
return n_data
if scale != 0:
g_size = sizes(data)
data = scale_code(data, scale)
# ==============================================================================
def move_xy(i_data, x, y):
n_data = ''
for row in i_data.split('\n'):
n_row = []
for b in row.split():
if b.find('X') != -1:
n_b = 'X{v}'.format(v=roundUp(float(b.replace('X', ''))+x))
elif b.find('Y') != -1:
n_b = 'Y{v}'.format(v=roundUp(float(b.replace('Y', ''))+y))
else:
n_b = b
n_row.append(n_b)
n_data += "{n_r}\n".format(n_r=' '.join(n_row))
return n_data
if move_x != 0 or move_y != 0:
data = move_xy(data, move_x, move_y)
# ==============================================================================
def combine_gcode(i_data, x, y, offset):
out_data = ""
g_size = sizes(data)
for i in range(int(x)):
for j in range(int(y)):
m_x = int((g_size[4] + int(offset)) * i)
m_y = int((g_size[5] + int(offset)) * j)
out_data += move_xy(i_data, m_x, m_y)
if move_x != 0 or move_y != 0:
out_data = move_xy(data, move_x, move_y)
return out_data
if combine != 0:
params=combine.split('x')
data = combine_gcode(data, params[0], params[1], params[2])
# ==============================================================================
def roundUpOnly(i_data):
n_data = ''
for row in i_data.split('\n'):
n_row = []
for b in row.split():
if b.find('X') != -1:
x = float(b.replace('X', ''))
n_b = 'X{n_x}'.format(n_x=round(x, roundIt))
elif b.find('Y') != -1:
y = float(b.replace('Y', ''))
n_b = 'Y{n_y}'.format(n_y=round(y, roundIt))
else:
n_b = b
n_row.append(n_b)
n_data += "{n_r}\n".format(n_r=' '.join(n_row))
return n_data
if roundIt > -1 and not any(i != 0 for i in [move_x, move_y, rotate, mirror, scale, combine]):
data = roundUpOnly(data)
# ==============================================================================
if comma:
data = data.replace('\n', f"{comma}\n").replace('\n;','').replace('\n\n','\n')
data = re.sub(r";\n$", ";", data)
if not output_file:
print(data)
else:
f = io.open(output_file, 'w', newline='\n')
f.write(data)