Browse Source

allow negative values

and some more comments
Wurstnase 9 years ago
parent
commit
14f0250853
1 changed files with 25 additions and 12 deletions
  1. 25
    12
      Marlin/scripts/g29_auto.py

+ 25
- 12
Marlin/scripts/g29_auto.py View File

@@ -12,15 +12,18 @@ my_file = 'test.gcode'
12 12
 # this is the minimum of G1 instructions which should be between 2 different heights
13 13
 min_g1 = 3
14 14
 
15
-#  maximum number of lines to parse, I don't want to parse the complete file
15
+# maximum number of lines to parse, I don't want to parse the complete file
16 16
 # only the first plane is we are interested in
17 17
 max_g1 = 1000
18 18
 
19 19
 # g29 keyword
20
-g29_keyword = ';MarlinG29Script'
20
+g29_keyword = 'g29'
21
+g29_keyword = g29_keyword.upper()
21 22
 
22 23
 # output filename
23 24
 output_file = folder + 'g29_' + my_file
25
+# input filename
26
+input_file = folder + my_file
24 27
 
25 28
 # offset makes the plane a little bit bigger
26 29
 offset_x = 10
@@ -40,10 +43,12 @@ lines_of_g1 = 0
40 43
 gcode = []
41 44
 
42 45
 
46
+# return only g1-lines
43 47
 def has_g1(line):
44 48
     return line[:2].upper() == "G1"
45 49
 
46 50
 
51
+# find position in g1 (x,y,z)
47 52
 def find_axis(line, axis):
48 53
     found = False
49 54
     number = ""
@@ -51,6 +56,8 @@ def find_axis(line, axis):
51 56
         if found:
52 57
             if char == ".":
53 58
                 number += char
59
+            elif char == "-":
60
+                number += char
54 61
             else:
55 62
                 try:
56 63
                     int(char)
@@ -65,6 +72,7 @@ def find_axis(line, axis):
65 72
         return None
66 73
 
67 74
 
75
+# save the min or max-values for each axis
68 76
 def set_mima(line):
69 77
     global min_x, max_x, min_y, max_y, last_z
70 78
 
@@ -81,6 +89,7 @@ def set_mima(line):
81 89
     return min_x, max_x, min_y, max_y
82 90
 
83 91
 
92
+# find z in the code and return it
84 93
 def find_z(gcode, start_at_line=0):
85 94
     for i in range(start_at_line, len(gcode)):
86 95
         my_z = find_axis(gcode[i], 'Z')
@@ -104,27 +113,30 @@ def z_parse(gcode, start_at_line=0, end_at_line=0):
104 113
 
105 114
         all_z.append(z)
106 115
         z_at_line.append(i)
116
+        temp_line = i - last_i -1
107 117
         line_between_z.append(i - last_i - 1)
108 118
         # last_z = z
109 119
         last_i = i
110
-        if 0 < end_at_line <= i:
120
+        if 0 < end_at_line <= i or temp_line >= min_g1:
121
+            # print('break at line {} at heigth {}'.format(i, z))
111 122
             break
112
-            # print('{}:{}'.format(last_z, last_i))
113 123
 
114 124
     line_between_z = line_between_z[1:]
115 125
     return all_z, line_between_z, z_at_line
116 126
 
117 127
 
128
+# get the lines which should be the first layer
118 129
 def get_lines(gcode, minimum):
119 130
     i = 0
120 131
     all_z, line_between_z, z_at_line = z_parse(gcode, end_at_line=max_g1)
121 132
     for count in line_between_z:
122 133
         i += 1
123 134
         if count > minimum:
135
+            # print('layer: {}:{}'.format(z_at_line[i-1], z_at_line[i]))
124 136
             return z_at_line[i - 1], z_at_line[i]
125 137
 
126 138
 
127
-with open(folder+my_file, 'r') as file:
139
+with open(input_file, 'r') as file:
128 140
     lines = 0
129 141
     for line in file:
130 142
         lines += 1
@@ -138,6 +150,8 @@ start, end = get_lines(gcode, min_g1)
138 150
 for i in range(start, end):
139 151
     set_mima(gcode[i])
140 152
 
153
+print('x_min:{} x_max:{}\ny_min:{} y_max:{}'.format(min_x, max_x, min_y, max_y))
154
+
141 155
 min_x = int(min_x) - offset_x
142 156
 max_x = int(max_x) + offset_x
143 157
 min_y = int(min_y) - offset_y
@@ -149,18 +163,17 @@ new_command = 'G29 L{0} R{1} F{2} B{3} P{4}\n'.format(min_x,
149 163
                                                       max_y,
150 164
                                                       probing_points)
151 165
 
152
-
153 166
 out_file = open(output_file, 'w')
154
-print('out_file open')
155
-input_file = open(my_file, 'r')
156
-print('input_file open')
167
+in_file = open(input_file, 'r')
157 168
 
158
-for line in input_file:
159
-    if line[:len(g29_keyword)] == g29_keyword:
169
+for line in in_file:
170
+    if line[:len(g29_keyword)].upper() == g29_keyword:
160 171
         out_file.write(new_command)
161
-        print('write new_command')
172
+        print('write G29')
162 173
     else:
163 174
         out_file.write(line)
164 175
 
165 176
 file.close()
166 177
 out_file.close()
178
+
179
+print('auto G29 finished')

Loading…
Cancel
Save