-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirstlayer_onmesh2D.py
198 lines (148 loc) · 4.79 KB
/
firstlayer_onmesh2D.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
#Compiled on IDLE 3.9.2 ( Python version 3.9.2)
import numpy as np
import matplotlib.pyplot as plt
import re
#---------------------------------------------------------------------------------------
# printer.cfg parser
tag_found = False
tag = '#*# [bed_mesh default]'
separator = ','
j=0
u=0
try:
f = open('printer.cfg',encoding="utf8")
except:
print('ERROR: printer.cfg not found in local directory')
exit()
# Get mesh x,y dimensions
counter = 0
for line in f:
if line.startswith('mesh_min:'):
pattern = r"#\*#|\s|mesh_min\:|\="
buf = re.sub(pattern, '', line)
y_mesh_start = float(buf.split(separator, 1)[1])
x_mesh_start = float(buf.split(separator, 1)[0])
counter+=1
if line.startswith('mesh_max:'):
pattern = r"#\*#|\s|mesh_max\:|\="
buf = re.sub(pattern, '', line)
y_mesh_end = float(buf.split(separator, 1)[1])
x_mesh_end = float(buf.split(separator, 1)[0])
counter+=1
if tag in line:
tag_found = True
tag = '%&/'
print('Bed mesh´s name found !\n')
if tag_found and 'y_count' in line:
pattern = r"#\*#|\s|y_count|="
buf = re.sub(pattern, '', line)
mesh_y = int(buf)
counter+=1
if tag_found and 'x_count' in line:
pattern = r"#\*#|\s|x_count|="
buf = re.sub(pattern, '', line)
mesh_x = int(buf)
counter+=1
if counter == 4:
break
if tag_found == False:
print('ERROR: Bed mesh´s name not found in printer.cfg')
exit()
# Get mesh z heights
try:
f = open('printer.cfg',encoding="utf8")
except:
print('ERROR: printer.cfg not found in local directory')
exit()
tag = '#*# [bed_mesh default]'
tag_found = False
pattern = r"#\*#|\s"
zb=np.zeros((mesh_x,mesh_y), dtype=float)
for line in f:
if tag in line:
tag_found = True
#Do not edit this tag.
tag = '%&/'
if tag_found and ',' in line:
buf = re.sub(pattern, '', line)
j=0
for i in buf.split(','):
zb[u,j]=i
j+=1
u+=1
if u == mesh_y:
break
# printer.cfg parser end
#---------------------------------------------------------------------------------------
# GCODE file parser
try:
f = open('problem.gcode',encoding="utf8")
except:
print('ERROR: problem.gcode not found in local directory')
exit()
#start_tag = ';TYPE:Skirt'
#start_tag ='; Internal perimeter'
start_tag = ';WIDTH:'
tag_found = False
#end_tag =';LAYER:1'
end_tag =';LAYER_CHANGE'
layer_tag=0
pattern = r"G1\sX|E.*$|\s|;.*$"
separator_e = 'Y'
gx = []
gy = []
for line in f:
if start_tag in line:
tag_found = True
#Do not edit this start_tag.
start_tag = '@€¬'
#if tag_found and line.startswith(';LAYER_CHANGE'):
if tag_found and line.startswith(end_tag):
layer_tag += 1
if layer_tag == 2:
break
if tag_found and line.startswith('G1 X') and "E" in line:
if "F" in line:
continue
buf = re.sub(pattern, '', line)
bufY = buf.split(separator_e, 1)[1]
bufX = buf.split(separator_e, 1)[0]
gx.append(float(bufX))
gy.append(float(bufY))
if not tag_found:
print ('ERROR: start_tag tag not found! \nAnalyze your problem.gcode and give me a valid start_tag')
exit()
if layer_tag != 2:
print('ERROR: end_tag not found! \nAnalyze your problem.gcode and give me a valid end_tag ')
exit()
# GCODE parser end
#---------------------------------------------------------------------------------------
xb = []
yb = []
xb.append(round(x_mesh_start,1))
x_iterations = mesh_x - 1
x_delta = (x_mesh_end - x_mesh_start)/x_iterations
for i in range(x_iterations):
x_mesh_start=round(x_mesh_start+x_delta,1)
xb.append(x_mesh_start)
yb.append(y_mesh_start)
y_iterations = mesh_y - 1
y_delta = (y_mesh_end - y_mesh_start)/y_iterations
for i in range(y_iterations):
y_mesh_start=round(y_mesh_start+y_delta,1)
yb.append(y_mesh_start)
xb,yb = np.meshgrid(xb,yb)
fig = plt.figure()
plt.plot(gx,gy)
plt.plot(xb,yb , linewidth=0.1, marker='o',markersize=2)
plt.plot(np.transpose(xb), np.transpose(yb), linewidth=0.1)
plt.title('Klipper´s bed mesh grid & Z height nodes [mm]\n', fontweight ="bold")
plt.xlabel('X-FRONT',fontsize =12)
plt.ylabel('Y-LEFT',fontsize =12)
#[row,collumn]
#first_element = z[0,0]
for j in range (mesh_y):
for i in range(mesh_x):
plt.text(xb[i,j], yb[i,j], zb[i,j], color='black',fontsize =14)
#fig.canvas.manager.full_screen_toggle()
plt.show()