2019-05-07 20:54:01 -05:00
|
|
|
#!/usr/bin/env python
|
2015-03-13 01:44:20 -05:00
|
|
|
|
2015-03-11 17:53:28 -05:00
|
|
|
# This file is for preprocessing gcode and the new G29 Autobedleveling from Marlin
|
|
|
|
# It will analyse the first 2 Layer and return the maximum size for this part
|
|
|
|
# After this it will replace with g29_keyword = ';MarlinG29Script' with the new G29 LRFB
|
|
|
|
# the new file will be created in the same folder.
|
|
|
|
|
2019-05-07 20:45:01 -05:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2015-03-11 17:53:28 -05:00
|
|
|
# your gcode-file/folder
|
|
|
|
folder = './'
|
|
|
|
my_file = 'test.gcode'
|
|
|
|
|
|
|
|
# this is the minimum of G1 instructions which should be between 2 different heights
|
|
|
|
min_g1 = 3
|
|
|
|
|
2015-03-16 08:26:59 -05:00
|
|
|
# maximum number of lines to parse, I don't want to parse the complete file
|
2015-03-11 17:53:28 -05:00
|
|
|
# only the first plane is we are interested in
|
2015-03-16 09:13:16 -05:00
|
|
|
max_g1 = 100000000
|
2015-03-11 17:53:28 -05:00
|
|
|
|
|
|
|
# g29 keyword
|
2015-03-16 08:26:59 -05:00
|
|
|
g29_keyword = 'g29'
|
|
|
|
g29_keyword = g29_keyword.upper()
|
2015-03-11 17:53:28 -05:00
|
|
|
|
|
|
|
# output filename
|
|
|
|
output_file = folder + 'g29_' + my_file
|
2015-03-16 08:26:59 -05:00
|
|
|
# input filename
|
|
|
|
input_file = folder + my_file
|
2015-03-11 17:53:28 -05:00
|
|
|
|
2015-03-16 09:13:16 -05:00
|
|
|
# minimum scan size
|
|
|
|
min_size = 40
|
2015-03-11 17:53:28 -05:00
|
|
|
probing_points = 3 # points x points
|
|
|
|
|
|
|
|
# other stuff
|
|
|
|
min_x = 500
|
|
|
|
min_y = min_x
|
|
|
|
max_x = -500
|
|
|
|
max_y = max_x
|
|
|
|
last_z = 0.001
|
|
|
|
|
|
|
|
layer = 0
|
|
|
|
lines_of_g1 = 0
|
|
|
|
|
|
|
|
gcode = []
|
|
|
|
|
|
|
|
|
2015-03-16 08:26:59 -05:00
|
|
|
# return only g1-lines
|
2015-03-13 01:31:27 -05:00
|
|
|
def has_g1(line):
|
2015-03-11 17:53:28 -05:00
|
|
|
return line[:2].upper() == "G1"
|
|
|
|
|
|
|
|
|
2015-03-16 08:26:59 -05:00
|
|
|
# find position in g1 (x,y,z)
|
2015-03-13 01:31:27 -05:00
|
|
|
def find_axis(line, axis):
|
2015-03-11 17:53:28 -05:00
|
|
|
found = False
|
|
|
|
number = ""
|
|
|
|
for char in line:
|
|
|
|
if found:
|
|
|
|
if char == ".":
|
|
|
|
number += char
|
2015-03-16 08:26:59 -05:00
|
|
|
elif char == "-":
|
|
|
|
number += char
|
2015-03-11 17:53:28 -05:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
int(char)
|
|
|
|
number += char
|
|
|
|
except ValueError:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
found = char.upper() == axis.upper()
|
|
|
|
try:
|
|
|
|
return float(number)
|
|
|
|
except ValueError:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2015-03-16 08:26:59 -05:00
|
|
|
# save the min or max-values for each axis
|
2015-03-11 17:53:28 -05:00
|
|
|
def set_mima(line):
|
|
|
|
global min_x, max_x, min_y, max_y, last_z
|
|
|
|
|
2015-03-13 01:31:27 -05:00
|
|
|
current_x = find_axis(line, 'x')
|
|
|
|
current_y = find_axis(line, 'y')
|
2015-03-11 17:53:28 -05:00
|
|
|
|
|
|
|
if current_x is not None:
|
|
|
|
min_x = min(current_x, min_x)
|
|
|
|
max_x = max(current_x, max_x)
|
|
|
|
if current_y is not None:
|
|
|
|
min_y = min(current_y, min_y)
|
|
|
|
max_y = max(current_y, max_y)
|
|
|
|
|
|
|
|
return min_x, max_x, min_y, max_y
|
|
|
|
|
|
|
|
|
2015-03-16 08:26:59 -05:00
|
|
|
# find z in the code and return it
|
2015-03-11 17:53:28 -05:00
|
|
|
def find_z(gcode, start_at_line=0):
|
|
|
|
for i in range(start_at_line, len(gcode)):
|
2015-03-13 01:31:27 -05:00
|
|
|
my_z = find_axis(gcode[i], 'Z')
|
2015-03-11 17:53:28 -05:00
|
|
|
if my_z is not None:
|
|
|
|
return my_z, i
|
|
|
|
|
|
|
|
|
|
|
|
def z_parse(gcode, start_at_line=0, end_at_line=0):
|
|
|
|
i = start_at_line
|
|
|
|
all_z = []
|
|
|
|
line_between_z = []
|
|
|
|
z_at_line = []
|
|
|
|
# last_z = 0
|
|
|
|
last_i = -1
|
|
|
|
|
|
|
|
while len(gcode) > i:
|
|
|
|
try:
|
|
|
|
z, i = find_z(gcode, i + 1)
|
|
|
|
except TypeError:
|
|
|
|
break
|
|
|
|
|
|
|
|
all_z.append(z)
|
|
|
|
z_at_line.append(i)
|
2015-03-16 08:26:59 -05:00
|
|
|
temp_line = i - last_i -1
|
2015-03-11 17:53:28 -05:00
|
|
|
line_between_z.append(i - last_i - 1)
|
|
|
|
# last_z = z
|
|
|
|
last_i = i
|
2015-03-16 08:26:59 -05:00
|
|
|
if 0 < end_at_line <= i or temp_line >= min_g1:
|
|
|
|
# print('break at line {} at heigth {}'.format(i, z))
|
2015-03-11 17:53:28 -05:00
|
|
|
break
|
|
|
|
|
|
|
|
line_between_z = line_between_z[1:]
|
|
|
|
return all_z, line_between_z, z_at_line
|
|
|
|
|
|
|
|
|
2015-03-16 08:26:59 -05:00
|
|
|
# get the lines which should be the first layer
|
2015-03-11 17:53:28 -05:00
|
|
|
def get_lines(gcode, minimum):
|
|
|
|
i = 0
|
|
|
|
all_z, line_between_z, z_at_line = z_parse(gcode, end_at_line=max_g1)
|
|
|
|
for count in line_between_z:
|
|
|
|
i += 1
|
|
|
|
if count > minimum:
|
2015-03-16 08:26:59 -05:00
|
|
|
# print('layer: {}:{}'.format(z_at_line[i-1], z_at_line[i]))
|
2015-03-11 17:53:28 -05:00
|
|
|
return z_at_line[i - 1], z_at_line[i]
|
|
|
|
|
|
|
|
|
2015-03-16 08:26:59 -05:00
|
|
|
with open(input_file, 'r') as file:
|
2015-03-11 17:53:28 -05:00
|
|
|
lines = 0
|
|
|
|
for line in file:
|
|
|
|
lines += 1
|
|
|
|
if lines > 1000:
|
|
|
|
break
|
2015-03-13 01:31:27 -05:00
|
|
|
if has_g1(line):
|
2015-03-11 17:53:28 -05:00
|
|
|
gcode.append(line)
|
|
|
|
file.close()
|
|
|
|
|
|
|
|
start, end = get_lines(gcode, min_g1)
|
|
|
|
for i in range(start, end):
|
|
|
|
set_mima(gcode[i])
|
|
|
|
|
2015-03-16 08:26:59 -05:00
|
|
|
print('x_min:{} x_max:{}\ny_min:{} y_max:{}'.format(min_x, max_x, min_y, max_y))
|
|
|
|
|
2015-03-16 09:13:16 -05:00
|
|
|
# resize min/max - values for minimum scan
|
|
|
|
if max_x - min_x < min_size:
|
|
|
|
offset_x = int((min_size - (max_x - min_x)) / 2 + 0.5) # int round up
|
|
|
|
# print('min_x! with {}'.format(int(max_x - min_x)))
|
|
|
|
min_x = int(min_x) - offset_x
|
|
|
|
max_x = int(max_x) + offset_x
|
|
|
|
if max_y - min_y < min_size:
|
|
|
|
offset_y = int((min_size - (max_y - min_y)) / 2 + 0.5) # int round up
|
|
|
|
# print('min_y! with {}'.format(int(max_y - min_y)))
|
|
|
|
min_y = int(min_y) - offset_y
|
|
|
|
max_y = int(max_y) + offset_y
|
|
|
|
|
2015-03-11 17:53:28 -05:00
|
|
|
|
|
|
|
new_command = 'G29 L{0} R{1} F{2} B{3} P{4}\n'.format(min_x,
|
|
|
|
max_x,
|
|
|
|
min_y,
|
|
|
|
max_y,
|
|
|
|
probing_points)
|
|
|
|
|
|
|
|
out_file = open(output_file, 'w')
|
2015-03-16 08:26:59 -05:00
|
|
|
in_file = open(input_file, 'r')
|
2015-03-11 17:53:28 -05:00
|
|
|
|
2015-03-16 08:26:59 -05:00
|
|
|
for line in in_file:
|
|
|
|
if line[:len(g29_keyword)].upper() == g29_keyword:
|
2015-03-11 17:53:28 -05:00
|
|
|
out_file.write(new_command)
|
2015-03-16 08:26:59 -05:00
|
|
|
print('write G29')
|
2015-03-11 17:53:28 -05:00
|
|
|
else:
|
|
|
|
out_file.write(line)
|
|
|
|
|
|
|
|
file.close()
|
|
|
|
out_file.close()
|
2015-03-16 08:26:59 -05:00
|
|
|
|
|
|
|
print('auto G29 finished')
|