-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.js
More file actions
144 lines (126 loc) · 4.82 KB
/
worker.js
File metadata and controls
144 lines (126 loc) · 4.82 KB
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
function findLastPremeter(lines, startLine) {
let lastE = null, lastZ = null, lastX = null, lastY = null, lastL = null, nozzleTemp = null, bedTemp = null;
// 从起始行向前倒着查找(保持您原来的逻辑)
for (let i = startLine - 1; i >= 0; i--) {
// 如果所有值都找到就退出
if (lastE !== null && lastZ !== null && lastX !== null && lastY !== null && lastL !== null && nozzleTemp !== null && bedTemp !== null) {
break;
}
const line = lines[i].trim();
if (!line) continue;
//找层数
if (line.startsWith(';LAYER:') && lastL === null) {
lastL = line.replace(';LAYER:', "");
continue;
}
if (line.startsWith(';')) continue;
// 只处理运动指令
if (line.startsWith('G0') || line.startsWith('G1')) {
if (line.includes('E') && lastE === null) {
const match = line.match(/E([-]?\d+\.\d+)/);
if (match) lastE = match[1];
}
if (line.includes('Z') && lastZ === null) {
const match = line.match(/Z([-]?\d+\.\d+)/);
if (match) lastZ = match[1];
}
if (line.includes('X') && lastX === null) {
const match = line.match(/X([-]?\d+\.\d+)/);
if (match) lastX = match[1];
}
if (line.includes('Y') && lastY === null) {
const match = line.match(/Y([-]?\d+\.\d+)/);
if (match) lastY = match[1];
}
continue;
}
//提取温度
if ((line.startsWith('M104') || line.startsWith('M109')) && nozzleTemp === null) {
const match = line.match(/S(\d+)/);
if (match) nozzleTemp = match[1];
continue;
}
if ((line.startsWith('M140') || line.startsWith('M190')) && bedTemp === null) {
const match = line.match(/S(\d+)/);
if (match) bedTemp = match[1];
continue;
}
}
return {
e: lastE || "0.0",
z: lastZ || "0.0",
x: lastX || "0.0",
y: lastY || "0.0",
l: lastL || "0",
nT: nozzleTemp || "0",
bT: bedTemp || "0"
};
}
function processGcodeInBackground(content, startLine, originalFilename) {
try {
// 分割文件内容
const lines = content.split('\n');
// 验证行号
if (startLine < 1 || startLine > lines.length) {
throw new Error(`行号应在 1-${lines.length} 范围内`);
}
// 查找最后一个有效位置
const LastPremeter = findLastPremeter(lines, startLine);
// 生成新的G代码
const newGcode = generateNewGcode(
lines,
startLine,
LastPremeter
);
// 将结果回传给主线程
self.postMessage({
type: 'success',
fileName: `${`resume_${originalFilename.replace(".gcode","")}_from_line_${startLine}` || 'output'}.gcode`,
content: newGcode
});
} catch (e) {
self.postMessage({
type: 'error',
error: error.message
});
}
}
function generateNewGcode(lines, startLine, premeters) {
let newContent = '';
// 添加新的文件头
newContent += `; Regenerated G code - from line ${startLine} \n`;
newContent += 'M117 restart heating...\n';
if (premeters.bT !== "0") {
newContent += `M140 S${premeters.bT} ; set\n`;
newContent += `M190 S${premeters.bT} ; wait bed temperature\n`;
}
newContent += `M104 S${premeters.nT} ; set\n`;
newContent += `M109 S${premeters.nT} ; wait nozzle temperature\n`;
newContent += 'G92 Z0 ; reset Z\n';
newContent += 'G1 Z10 ; unstuck Z\n';
newContent += 'G28 X0 Y0 ; homing XY\n';
newContent += 'G1 Z0 ; moveback Z\n';
newContent += 'G28 Z0 ; homing Z\n';
newContent += `G92 E0 ;zero the extruded length\n`;
newContent += `G1 F200 E10 ;extrude 10mm of feed stock\n`;
newContent += 'M117 ready to print...\n';
// 移动到安全高度
const safeZ = parseFloat(premeters.z) + 5.0;
newContent += `G0 Z${safeZ.toFixed(3)} F3000 ; move Z to safe height\n`;
// 移动到最后位置
newContent += `G0 X${premeters.x} Y${premeters.y} F3000 ; move X Y to breakup point\n`;
// 设置挤出机位置
newContent += `G92 E${premeters.e} ; set Extruder\n`;
newContent += `M117 resume from ${startLine} ...\n`;
newContent += `G0 Z${premeters.z} F3000 ; move Z to breakup point\n`;
// 添加从指定行开始的内容
for (let i = startLine - 1; i < lines.length; i++) {
newContent += lines[i] + '\n';
}
return newContent;
}
// worker.js
self.onmessage = e => {
const { text, cursorLine, title } = e.data;
processGcodeInBackground(text, cursorLine, title)
};