-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgcode-studio.html
More file actions
390 lines (339 loc) · 11.2 KB
/
gcode-studio.html
File metadata and controls
390 lines (339 loc) · 11.2 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<html>
<head>
<title>G-Code Studio</title>
<style>
html { height: 100%; }
body { height: 100%; margin: 0; background: #222; }
#side { position: absolute; width: 50%; height: 66%; top: 0; left:0; }
#top { position: absolute; width: 50%; height: 66%; top: 0; right:0; }
#editor {
position: absolute; width: 92%; height: 33%; bottom: 0; left:0;
background-color: #1f1f1f;
opacity: .8;
display: none;
}
#editor .ace_gutter {
background-color: #222;
background-image: none;
border-right: none;
border-left: none;
color: #444;
}
#editor .ace_scroller {
box-shadow: inset 0 0 5px rgba(0,0,0,.5);
-webkit-filter: opacity(.8) sepia(100%) hue-rotate(150deg) grayscale(20%);
-moz-filter: opacity(.8) sepia(100%) hue-rotate(150deg) grayscale(20%);
}
#editor .ace_indent-guide {
opacity:0;
}
#editor .ace_gutter-cell {
background-color: #222;
}
#editor .ace_cursor {
color: #fff;
}
#buttons {
position: absolute;
width: 8%;
height: 33%;
bottom:0;
right: 0;
}
a, button {
display: block;
padding: 0;
margin: 0 auto 10px;
font-family: Andale Mono, Helvetica;
text-align: center;
text-decoration: none;
font-size: 3vw;
background: none;
border: 1px solid #9cf;
border-radius: 3px;
color: #9cf;
height: 40px;
width: 80px;
font-size: 1vw;
line-height: 38px;
position: relative;
opacity: .4;
transition: all .5s ease;
-webkit-transition: all .5s ease;
-moz-transition: all .5s ease;
}
a:hover, button:hover {
cursor: pointer;
background-color: #056;
color: #fff;
opacity: 1;
}
.file {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
opacity: 0;
}
.file:hover {
cursor: pointer;
}
</style>
<script src="http://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ace.js"></script>
</head>
<body>
<canvas id="side"></canvas>
<canvas id="top"></canvas>
<div id="editor">/* G-Code Studio
Enter transforms in this editor and press Command-Enter to execute them.
Each G0 or G1 command will be run through this code. Modifing any of the
variable will result in a modification to the corresponding G-Code. The
variables are:
X, Y, Z: The x, y and z destination of the print nozzle.
E: The extruder quantity at the end of this command.
G: 1 or 0 to indicate the type of movement.
context: set variables on this object to keep state between commands.
*/
</div>
<div id="buttons">
<button>open
<input type="file" class="file" id="open"/>
</button>
<a href="" id="save">save</a>
</div>
</body>
<script>
/* TODO:
- Select Top filter from front
- Workers for side rendering
- On screen error messaging
- Loading animation
- Emit gcode
- Delete line
*/
var renderer = Renderer([]);
initEditor(renderer);
document.querySelector('#open').addEventListener('change', function(e) {
var file;
if (e.target.files && (file=e.target.files[0]) && file.name.match(/\.gcode$/)) {
var reader = new FileReader();
renderer.setName(file.name);
reader.onload = compose(renderer.load,parseGcode, function(e) { return e.target.result; });
reader.readAsText(file);
}
}, false);
document.querySelector('#save').addEventListener('click', handler(renderer.save), false);
function parseGcode(code) {
var start = new Date();
var point = {};
var points = [];
lines(code, function(line) {
var m = line.match(/^G([01])\s+(.*?)(;.*)?$/m);
if (m) {
point = {G:parseInt(m[1]), X:point.X, Y:point.Y, Z:point.Z, E:point.E, F:point.F, keys:''};
m[2].replace(/(\w)(-?\d+(\.\d+)?)/g, function(m,k,n) {
point.keys += k;
point[k] = parseFloat(n); return '';
});
points.push(point);
} else {
points.push(line + '\n');
}
});
console.log('Parse:', new Date().getTime() - start.getTime() )
return points;
}
function Renderer(points) {
var rotateInterval;
var filename = '';
var transformed;
function update(points) {
transformed = points;
var mins = comparePoints(points.slice(10,points.length-10),undefMin);
var maxes = comparePoints(points.slice(10,points.length-10),undefMax);
var centers = keyMap(function(k) { return lerp(mins[k],maxes[k],.5)}, ['X','Y','Z']);
var XMARGIN = 60;
var YMARGIN = 100;
var top = initCanvas('top',2, -XMARGIN);
var xRange = maxes.X - mins.X;
var yRange = maxes.Y - mins.Y;
var theta = 0;
var SCALE = aspectScale( xRange, yRange, top.width - XMARGIN, top.height - 2*YMARGIN );
renderTop(top.ctx, points, SCALE, centers, mins, maxes);
var side = initCanvas('side',1,XMARGIN);
var zRange = maxes.Z - mins.Z;
var xyRange = Math.max(xRange, yRange);
var SCALE = aspectScale( xyRange, zRange, side.width - XMARGIN, side.height - 2*YMARGIN );
clearInterval(rotateInterval);
rotateInterval = setInterval(function() {
side.ctx.clearRect(-side.width/2 - XMARGIN,-side.height/2,side.width,side.height);
renderSide(side.ctx, points, SCALE, centers, mins, maxes, theta);
theta += Math.PI/60;
}, 200);
}
function load(newPoints) {
points = newPoints;
update(points);
}
function save() {
var gcode = transformed.map(function(line) {
if (typeof line == 'string')
return line;
return 'G'+line.G+' '+line.keys.split('').map(function(k) {return k+line[k].toFixed(5)}).join(' ')+'\n';
});
var a = document.createElement('a');
a.download = filename;
a.href = URL.createObjectURL(new Blob(gcode, {type:'text/plain'}));
a.click();
}
return {
points: function() { return points; },
update: update,
load: load,
save: save,
setName: function(name) { filename = name }
}
}
function initCanvas(id, scale, xOffset) {
var canvas = document.getElementById(id);
var w = canvas.width = canvas.offsetWidth * (scale||1);
var h = canvas.height = canvas.offsetHeight * (scale||1);
var ctx = canvas.getContext('2d');
ctx.translate( w/2 + xOffset, h/2);
return {ctx:ctx, width:w, height:h};
}
function renderSide(ctx, points, scale, centers, mins, maxes, theta) {
ctx.beginPath();
ctx.lineWidth = .15;
ctx.strokeStyle = 'rgba(0,200,255,.8)';
xComponent = Math.cos(theta) * scale;
yComponent = -Math.sin(theta) * scale;
for (var i=0,point; (point = points[i]) != null; i++) if (typeof point=='object') {
if (point.X != undefined && point.Y != undefined) {
ctx[point.G?'lineTo':'moveTo'](
(point.X-centers.X)*xComponent + (point.Y-centers.Y)*yComponent,
-(point.Z-centers.Z)*scale );
}
}
ctx.stroke();
renderHeightMetric( ctx, -ctx.canvas.width/2 - 40, maxes.Z, mins.Z, scale, 1 );
}
function renderTop(ctx, points, scale, centers, mins, maxes) {
ctx.beginPath();
ctx.lineWidth = .25;
ctx.strokeStyle = 'rgba(0,200,255,.4)';
for (var i=0,o={},point; (point = points[i]) != null; i++) if (typeof point=='object') {
if (point.X != undefined && point.Y != undefined)
ctx[point.G?'lineTo':'moveTo'](
(point.X-centers.X)*scale,
-(point.Y-centers.Y)*scale );
}
ctx.stroke();
renderHeightMetric( ctx, ctx.canvas.width/2 - 40, maxes.Y, mins.Y, scale, 2 );
renderWidthMetric( ctx, ctx.canvas.height/2 - 20, maxes.X, mins.X, scale, 2 );
}
function renderHeightMetric( ctx, x, top, bottom, scale, res ) {
var tick = 5 * res;
var upperY = (bottom-top)*scale/2, lowerY = (top-bottom)*scale/2;
ctx.beginPath();
ctx.moveTo(x, upperY ); ctx.lineTo(x, lowerY );
ctx.moveTo(x-tick, upperY ); ctx.lineTo(x+tick, upperY );
ctx.moveTo(x-tick, lowerY ); ctx.lineTo(x+tick, lowerY );
ctx.fillStyle = ctx.strokeStyle = 'rgba(0,200,255,.6)';
ctx.lineWidth = .5 * res;
ctx.stroke();
ctx.textAlign = 'center';
ctx.font = (10*res)+'px Andale Mono,Helvetica';
ctx.fillText( top.toFixed(1), x, upperY-8*res );
ctx.fillText( bottom.toFixed(1), x, lowerY+14*res );
}
function renderWidthMetric( ctx, y, right, left, scale, res ) {
var tick = 5 * res;
var leftX = (left-right)*scale/2, rightX = (right-left)*scale/2;
ctx.beginPath();
ctx.moveTo( leftX, y ); ctx.lineTo( rightX, y );
ctx.moveTo( leftX, y-tick ); ctx.lineTo( leftX, y+tick );
ctx.moveTo( rightX, y-tick ); ctx.lineTo( rightX, y+tick );
ctx.fillStyle = ctx.strokeStyle = 'rgba(0,200,255,.6)';
ctx.lineWidth = .5 * res;
ctx.stroke();
ctx.font = (10*res)+'px Andale Mono,Helvetica';
ctx.textAlign = 'right';
ctx.fillText( left.toFixed(1), leftX - 7*res, y + 2.5*res );
ctx.textAlign = 'left';
ctx.fillText( right.toFixed(1), rightX + 7*res, y + 2.5*res );
}
function initEditor(renderer) {
document.getElementById('editor').setAttribute('style', 'display:block;')
var editor = ace.edit("editor");
editor.setTheme("ace/theme/solarized_dark");
editor.getSession().setMode("ace/mode/javascript");
editor.setShowPrintMargin(false);
editor.getSession().setUseSoftTabs(true);
editor.setFontSize(14);
editor.commands.addCommand({
name: 'transform',
bindKey: {win: 'Cmd-Enter', mac: 'Cmd-Enter'},
exec: transform(renderer, editor)
});
editor.renderer.setPadding(15);
editor.renderer.setScrollMargin(15);
editor.clearSelection();
editor.session.setOption("useWorker", false);
}
function transform(renderer, editor) {
return function() {
var context = {};
try {
var transformer = new Function('X','Y','Z','E','F','G','context',
editor.getValue() +
'; return {X:X, Y:Y, Z:Z, E:E, F:F, G:G};');
var points = renderer.points();
for (var i=0,transformed=[],p; (p = points[i]) != null; i++) {
if (typeof p=='object') {
var newPoint = transformer(p.X,p.Y,p.Z,p.E,p.F,p.G,context);
newPoint.keys = p.keys;
transformed.push(newPoint);
} else {
transformed.push(p);
}
}
renderer.update(transformed);
} catch (e) {
// TODO: show error on screen
console.error( e.stack );
return;
}
}
}
function lines(str, cb) {
var re = /\s*\n\r?\s*/g, startIndex=0, m;
while (m = re.exec(str))
cb( str.substring(startIndex, startIndex = m.index) );
cb(str.substring( startIndex, str.length ) );
}
function aspectScale( iWidth, iHeight, oWidth, oHeight) {
return iWidth*oHeight > iHeight*oWidth ? oWidth/iWidth : oHeight/iHeight;
}
function keyMap(f,keys) { for (var i=0,o={},k; k=keys[i]; i++) o[k]=f(k); return o; }
function comparePoints(points,f) {
for (var i=0,o={},p; (p = points[i]) != null; i++) if (typeof p=='object') {
o.X = f(o.X, p.X);
o.Y = f(o.Y, p.Y);
o.Z = f(o.Z, p.Z);
}
return o;
}
function undefMax(a,b) { return a == null ? b : (b == null ? a : Math.max(a,b)); }
function undefMin(a,b) { return a == null ? b : (b == null ? a : Math.min(a,b)); }
function lerp(a,b,x) { return a + x*(b-a); }
function handler(f) { return function(e) { e.preventDefault(); f(e); } }
function compose(f1, f2, etc) {
if (arguments.length < 2) return f1;
var composed = compose.apply(null, Array.prototype.slice.call(arguments,0,arguments.length-1));
var last = arguments[arguments.length-1];
return function(x) { return composed(last(x)); }
}
</script>
</html>