My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

g29_auto.py 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #!/usr/bin/python3
  2. # This file is for preprocessing gcode and the new G29 Autobedleveling from Marlin
  3. # It will analyse the first 2 Layer and return the maximum size for this part
  4. # After this it will replace with g29_keyword = ';MarlinG29Script' with the new G29 LRFB
  5. # the new file will be created in the same folder.
  6. # your gcode-file/folder
  7. folder = './'
  8. my_file = 'test.gcode'
  9. # this is the minimum of G1 instructions which should be between 2 different heights
  10. min_g1 = 3
  11. # maximum number of lines to parse, I don't want to parse the complete file
  12. # only the first plane is we are interested in
  13. max_g1 = 1000
  14. # g29 keyword
  15. g29_keyword = ';MarlinG29Script'
  16. # output filename
  17. output_file = folder + 'g29_' + my_file
  18. # offset makes the plane a little bit bigger
  19. offset_x = 10
  20. offset_y = 10
  21. probing_points = 3 # points x points
  22. # other stuff
  23. min_x = 500
  24. min_y = min_x
  25. max_x = -500
  26. max_y = max_x
  27. last_z = 0.001
  28. layer = 0
  29. lines_of_g1 = 0
  30. gcode = []
  31. def has_g1(line):
  32. return line[:2].upper() == "G1"
  33. def find_axis(line, axis):
  34. found = False
  35. number = ""
  36. for char in line:
  37. if found:
  38. if char == ".":
  39. number += char
  40. else:
  41. try:
  42. int(char)
  43. number += char
  44. except ValueError:
  45. break
  46. else:
  47. found = char.upper() == axis.upper()
  48. try:
  49. return float(number)
  50. except ValueError:
  51. return None
  52. def set_mima(line):
  53. global min_x, max_x, min_y, max_y, last_z
  54. current_x = find_axis(line, 'x')
  55. current_y = find_axis(line, 'y')
  56. if current_x is not None:
  57. min_x = min(current_x, min_x)
  58. max_x = max(current_x, max_x)
  59. if current_y is not None:
  60. min_y = min(current_y, min_y)
  61. max_y = max(current_y, max_y)
  62. return min_x, max_x, min_y, max_y
  63. def find_z(gcode, start_at_line=0):
  64. for i in range(start_at_line, len(gcode)):
  65. my_z = find_axis(gcode[i], 'Z')
  66. if my_z is not None:
  67. return my_z, i
  68. def z_parse(gcode, start_at_line=0, end_at_line=0):
  69. i = start_at_line
  70. all_z = []
  71. line_between_z = []
  72. z_at_line = []
  73. # last_z = 0
  74. last_i = -1
  75. while len(gcode) > i:
  76. try:
  77. z, i = find_z(gcode, i + 1)
  78. except TypeError:
  79. break
  80. all_z.append(z)
  81. z_at_line.append(i)
  82. line_between_z.append(i - last_i - 1)
  83. # last_z = z
  84. last_i = i
  85. if 0 < end_at_line <= i:
  86. break
  87. # print('{}:{}'.format(last_z, last_i))
  88. line_between_z = line_between_z[1:]
  89. return all_z, line_between_z, z_at_line
  90. def get_lines(gcode, minimum):
  91. i = 0
  92. all_z, line_between_z, z_at_line = z_parse(gcode, end_at_line=max_g1)
  93. for count in line_between_z:
  94. i += 1
  95. if count > minimum:
  96. return z_at_line[i - 1], z_at_line[i]
  97. with open(folder+my_file, 'r') as file:
  98. lines = 0
  99. for line in file:
  100. lines += 1
  101. if lines > 1000:
  102. break
  103. if has_g1(line):
  104. gcode.append(line)
  105. file.close()
  106. start, end = get_lines(gcode, min_g1)
  107. for i in range(start, end):
  108. set_mima(gcode[i])
  109. min_x = int(min_x) - offset_x
  110. max_x = int(max_x) + offset_x
  111. min_y = int(min_y) - offset_y
  112. max_y = int(max_y) + offset_y
  113. new_command = 'G29 L{0} R{1} F{2} B{3} P{4}\n'.format(min_x,
  114. max_x,
  115. min_y,
  116. max_y,
  117. probing_points)
  118. out_file = open(output_file, 'w')
  119. print('out_file open')
  120. input_file = open(my_file, 'r')
  121. print('input_file open')
  122. for line in input_file:
  123. if line[:len(g29_keyword)] == g29_keyword:
  124. out_file.write(new_command)
  125. print('write new_command')
  126. else:
  127. out_file.write(line)
  128. file.close()
  129. out_file.close()