forked from LaserWeb/LaserWeb4
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcam-gcode.js
More file actions
246 lines (210 loc) · 10.5 KB
/
cam-gcode.js
File metadata and controls
246 lines (210 loc) · 10.5 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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// Copyright 2014, 2016 Todd Fleming
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
'use strict';
import { getLaserRasterGcodeFromOp, getLaserRasterMergeGcodeFromOp } from './cam-gcode-raster'
import { rawPathsToClipperPaths, union, xor } from './mesh';
import { humanFileSize } from './helpers';
import { strftime } from './strftime'//
import { GlobalStore } from '../index'
import queue from 'queue';
import hhmmss from 'hhmmss';
export const expandHookGCode = (operation) =>{
let state = GlobalStore().getState();
let macros = state.settings.macros || {};
let op=Object.assign({},operation)
let hooks = Object.keys(op).filter(i=>i.match(/^hook/gi))
hooks.forEach(hook => {
let keys = op[hook].split(',')
let gcode='';
if (keys.length){
keys.forEach(key=>{
if (macros[key]) gcode+=("\r\n; Macro ["+hook+"]: "+macros[key].label+"\r\n"+macros[key].gcode+"\r\n")
})
}
op[hook] = gcode;
})
return op;
}
export function getGcode(settings, documents, operations, documentCacheHolder, showAlert, done, progress) {
"use strict";
let starttime=new Date().getTime()
const QE = new queue();
QE.timeout = 3600 * 1000;
QE.concurrency = settings.gcodeConcurrency || 1;
console.log('Concurrency: ' + QE.concurrency)
const gcode = Array(operations.length);
const gauge = Array(operations.length*2).fill(0)
const workers = [];
let jobIndex = 0;
let fullGcode = "";
let startCode = "";
let endCode = "";
let laserOps = false;
let millOps = false;
console.log('Queueing ' + operations.length + ' operation(s)');
for (let opIndex = 0; opIndex < operations.length; ++opIndex) {
let op = expandHookGCode(operations[opIndex]);
const jobDone = (g, cb) => {
if (g !== "") { gcode[opIndex]=g;};
cb();
}
let invokeWebWorker = (ww, props, cb, jobIndex) => {
let peasant = new ww();
peasant.onmessage = (e) => {
let data = JSON.parse(e.data)
if (data.event == 'onDone') {
gauge[props.opIndex*2+1]=100;
progress(gauge)
jobDone(data.gcode, cb)
} else if (data.event == 'onProgress') {
gauge[props.opIndex*2+1]=data.progress;
progress(gauge)
} else {
data.errors.forEach((item) => {
showAlert(item.message, item.level)
})
QE.end()
}
}
workers.push(peasant)
peasant.postMessage(props)
}
let preflightPromise = (settings, documents, opIndex, op, workers) => {
return new Promise((resolve, reject) => {
let geometry = [];
let openGeometry = [];
let tabGeometry = [];
let filteredDocIds = [];
let docsWithImages = [];
let preflightWorker = require('./workers/cam-preflight.worker.js');
let preflight = new preflightWorker()
preflight.onmessage = (e) => {
let data = e.data;
if (data.event == 'onDone') {
if (data.geometry) geometry = data.geometry
if (data.openGeometry) openGeometry = data.openGeometry
if (data.tabGeometry) tabGeometry = data.tabGeometry
if (data.filteredDocIds) filteredDocIds = data.filteredDocIds
data.docsWithImages.forEach(_doc => {
let cache = documentCacheHolder.cache.get(_doc.id);
if (cache && cache.imageLoaded)
docsWithImages.push(Object.assign([], _doc, { image: cache.image }));
})
gauge[opIndex*2]=100;
resolve({ geometry, openGeometry, tabGeometry, filteredDocIds, docsWithImages })
} else if (data.event == 'onProgress') {
gauge[opIndex*2]=data.percent;
progress(gauge)
} else if (data.event == 'onError') {
reject(data)
}
}
workers.push(preflight)
preflight.postMessage({ settings, documents, opIndex, op, geometry, openGeometry, tabGeometry })
})
}
if (op.enabled) QE.push((cb) => {
console.log('Queueing Preflight: ' + op.type + "->" + opIndex);
preflightPromise(settings, documents, opIndex, op, workers)
.then((preflight) => {
let { geometry, openGeometry, tabGeometry, filteredDocIds, docsWithImages } = preflight;
console.log('Queueing Worker: ' + op.type + "->" + opIndex);
if (op.type === 'Laser Cut' || op.type === 'Laser Cut Inside' || op.type === 'Laser Cut Outside' || op.type === 'Laser Fill Path') {
laserOps = true;
if (startCode === "") startCode = settings.gcodeStart;
if (endCode === "") endCode = settings.gcodeEnd;
invokeWebWorker(require('./workers/cam-lasercut.worker.js'), { settings, opIndex, op, geometry, openGeometry, tabGeometry }, cb, jobIndex)
} else if (op.type === 'Laser Raster') {
laserOps = true;
if (startCode === "") startCode = settings.gcodeStart;
if (endCode === "") endCode = settings.gcodeEnd;
getLaserRasterGcodeFromOp(settings, opIndex, op, docsWithImages, showAlert, (gcode)=>{jobDone(gcode,cb)}, progress, jobIndex, QE.chunk, workers);
} else if (op.type === 'Laser Raster Merge') {
laserOps = true;
if (startCode === "") startCode = settings.gcodeStart;
if (endCode === "") endCode = settings.gcodeEnd;
getLaserRasterMergeGcodeFromOp(settings, documentCacheHolder, opIndex, op, filteredDocIds, showAlert, (gcode) => { jobDone(gcode, cb) }, progress, jobIndex, QE.chunk, workers);
} else if (op.type.substring(0, 5) === 'Mill ') {
millOps = true;
if (startCode === "") startCode = settings.gcodeMillStart;
if (endCode === "") endCode = settings.gcodeMillEnd;
if (startCode === "") startCode = settings.gcodeStart; // fallback if mill specific code not defined
if (endCode === "") endCode = settings.gcodeEnd;
invokeWebWorker(require('./workers/cam-mill.worker.js'), { settings, opIndex, op, geometry, openGeometry, tabGeometry }, cb, jobIndex)
} else if (op.type.substring(0, 6) === 'Lathe ') {
millOps = true;
if (startCode === "") startCode = settings.gcodeMillStart;
if (endCode === "") endCode = settings.gcodeMillEnd;
if (startCode === "") startCode = settings.gcodeStart; // fallback if mill specific code not defined
if (endCode === "") endCode = settings.gcodeEnd;
invokeWebWorker(require('./workers/cam-lathe.worker.js'), { settings, opIndex, op, geometry, openGeometry, tabGeometry }, cb, jobIndex)
} else {
showAlert("Unknown operation " + op.type, 'warning')
cb()
}
})
.catch((err) => {
showAlert(err.message, err.level)
QE.end()
})
})
} // opIndex
QE.total = QE.length
QE.chunk = 100 / QE.total
progress(0)
QE.on('success', (result, job) => {
jobIndex++
let p = parseInt(jobIndex * QE.chunk)
progress(p);
console.log('Completed a Worker: ' + jobIndex + ' of ' + QE.total);
})
QE.on('end', () => {
workers.forEach((ww) => {
ww.terminate();
})
})
QE.start((err) => {
progress(100)
let elapsed=(new Date().getTime()-starttime)/1000;
// process and prepend the header
if (settings.gcodeHeader.length > 0) {
let header = settings.gcodeHeader.split(/\r\n|\r|\n/g);
for (let i = 0; i < header.length; i++)
{
let line = header[i].replace("$VERSION", settings.__version)
.replace("$PROFILE", settings.__selectedProfile);
if (line.length > 0) {
fullGcode += "; " + strftime(line) + "\r\n";
} else {
fullGcode += "\r\n";
}
}
fullGcode += "\r\n";
}
if (laserOps && millOps) {
showAlert('<span className="help-block">Warning: Mixed operation types detected.</span><br/>Mixing laser and mill/lathe operations in the same job is not recommended; only use the generated code if you understand the consequences and are sure this is what you want.',"warning");
}
showAlert("Gcode generation complete, elapsed: " + hhmmss(elapsed) + String(Number(elapsed-Math.floor(elapsed)).toFixed(3)).substr(1), "info");
if (gcode.join() === "" ) {
fullGcode = "";
showAlert("Empty Gcode! Either there was an error during generation or the user cancelled generation.", "warning");
} else {
fullGcode += startCode + gcode.join('\r\n') + endCode;
showAlert("Generated Gcode size: " + fullGcode.length + " (" + humanFileSize(fullGcode.length) + ")", "info");
}
done(fullGcode);
})
return QE;
} // getGcode