-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
1280 lines (1020 loc) · 50.5 KB
/
index.html
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<header>
<title>Online CNC Machine</title>
<!--<script src="https://code.jquery.com/jquery-3.2.1.js"></script>-->
<!-- Latest compiled and minified JavaScript -->
<!-- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> -->
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script id="2d-vertex-shader" type="x-shader/x-vertex" src="shaders/workpiece.v">
attribute vec3 a_position;
attribute float a_normal;
varying vec3 in_fragCoord;
varying vec3 in_normal;
varying vec3 worldPos;
//uniform vec3 workpieceDims;
uniform mat4 modelMatrix;
uniform mat4 viewMatrix;
uniform mat4 projMatrix;
uniform vec2 screenRatio;
void main()
{
in_fragCoord = (modelMatrix*vec4(a_position, 1)).xyz;
if(a_normal < 0.5)
{
in_normal = vec3(-1, 0, 0);
}
else if(a_normal < 1.5)
{
in_normal = vec3(1, 0, 0);
}
else if(a_normal < 2.5)
{
in_normal = vec3(0, -1, 0);
}
else if(a_normal < 3.5)
{
in_normal = vec3(0, 1, 0);
}
else if(a_normal < 4.5)
{
in_normal = vec3(0, 0, -1);
}
else// if(a_normal < 5.5)
{
in_normal = vec3(0, 0, 1);
}
worldPos = a_position;
//in_normal = mat3(modelMatrix) * in_normal;
//in_fragCoord = projMatrix * viewMatrix * modelMatrix * vec4(a_position, 1);
gl_Position = projMatrix * viewMatrix * modelMatrix * vec4(a_position, 1);// + vec4(-0.5, -0.5, -3, 0)
in_fragCoord = gl_Position.xyz;
}
</script>
<script id="2d-fragment-shader" type="x-shader/x-fragment" src="shaders/workpiece.f">
precision mediump float;
varying vec3 in_fragCoord;
varying vec3 in_normal;
varying vec3 worldPos;
uniform vec3 workpieceDims;
void main(void)
{
vec3 lightDir = vec3(0.707, 0, 0.407);
vec3 xyzAmp = (worldPos / workpieceDims)*0.7 + vec3(0.3);
float illumination = clamp((dot(lightDir, in_normal)/2.0+0.5)*0.4+0.6, 0.6, 1.0);
float bright = illumination * xyzAmp.z;
vec3 colour = vec3(0.2 + 0.5*xyzAmp.x, 0.5 + 0.5*xyzAmp.y, 0.9)*bright;
gl_FragColor = vec4(colour, 1);
}
</script>
<script id="2d-vertex-shader-cylinder" type="x-shader/x-vertex" src="shaders/cylinder.v">
attribute vec3 a_position;
attribute vec3 a_normal;
varying vec3 in_fragCoord;
varying vec3 in_normal;
uniform mat4 modelMatrix;
uniform mat4 viewMatrix;
uniform mat4 projMatrix;
void main()
{
in_normal = normalize(mat3(modelMatrix) * a_normal);
gl_Position = projMatrix * viewMatrix * modelMatrix * vec4(a_position, 1);
in_fragCoord = gl_Position.xyz;
}
</script>
<script id="2d-fragment-shader-cylinder" type="x-shader/x-fragment" src="shaders/cylinder.f">
precision mediump float;
uniform vec3 colour;
varying vec3 in_fragCoord;
varying vec3 in_normal;
void main(void)
{
vec3 lightDir = vec3(0.707, 0, 0.407);
float bright = clamp((dot(lightDir, in_normal)/2.0+0.5)*0.4+0.6, 0.6, 1.0);
gl_FragColor = vec4(colour*bright, 1);
}
</script>
<script src="gutil.js" type="text/javascript"></script>
<script type="text/javascript">
var gl; // A global variable for the WebGL context
var glt = new GLT(); //A global variable to handle view, projection and model matrices
var canvas, textCanvas;
</script>
<script src="arrow.js" type="text/javascript"></script>
<script src="sphere.js" type="text/javascript"></script>
<script src="cylinder.js" type="text/javascript"></script>
<script src="FAUNC.js" type="text/javascript"></script>
<script src="voxel.js" type="text/javascript"></script>
<script src="input.js" type="text/javascript"></script>
<script type="text/javascript">
var defaultCodeStyler = new CNCCodeStyler();
function CNCSIMSettings()
{
this.showCoordinates = true;
this.displayCoordinateText = true;
this.simulationSpeed = 111;
this.openTab = 'code';
this.fieldOfView = 60;
this.moveSpeed = 30;
this.g0speed = 100;
}
var SETTINGS = new CNCSIMSettings();
{
var savedSettings = localStorage.getItem("settings");
if(savedSettings != null)
{
SETTINGS = JSON.parse(savedSettings);
if(SETTINGS.showCoordinates == undefined) SETTINGS.openTab = true;
if(SETTINGS.displayCoordinateText == undefined) SETTINGS.openTab = true;
if(SETTINGS.simulationSpeed == undefined) SETTINGS.openTab = 50;
if(SETTINGS.openTab == undefined) SETTINGS.openTab = "code";
if(SETTINGS.fieldOfView == undefined) SETTINGS.fieldOfView = 60;
if(SETTINGS.moveSpeed == undefined) SETTINGS.moveSpeed = 30;
if(SETTINGS.g0speed == undefined) SETTINGS.g0speed = 100;
}
}
function saveSettings()
{
localStorage.setItem("settings", JSON.stringify(SETTINGS));
}
Math.signum = function(d)
{
if(d < 0) return -1;
if(d > 0) return 1;
return 0;
}
function clamp(v, min, max)
{
if(v < min) return min;
if(v > max) return max;
return v;
}
var theCamera = new Camera();
// Function that grabs a shader stored as a page script
// and compiles it based on the MIME type.
//
// This script is all over the web, and may have originated here:
// http://learningwebgl.com/
function getShader(gl, id)
{
var shaderScript = document.getElementById(id);
if (!shaderScript)
{
return null;
}
var str = "";
var k = shaderScript.firstChild;
while (k)
{
if (k.nodeType == 3)
str += k.textContent;
k = k.nextSibling;
}
var shader;
if (shaderScript.type == "x-shader/x-fragment")
{
shader = gl.createShader(gl.FRAGMENT_SHADER);
}
else if (shaderScript.type == "x-shader/x-vertex")
{
shader = gl.createShader(gl.VERTEX_SHADER);
}
else
{
return null;
}
gl.shaderSource(shader, str);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS))
{
document.getElementById("heading").innerHTML = "COMPILERR: " + gl.getShaderInfoLog(shader);
//alert(gl.getShaderInfoLog(shader));
return null;
}
return shader;
}
function reset()
{
workpiece.reset();
workpiece.updateModelData(false);
angleX = 0;
}
function STOPSIMULATION()
{
reset();
currentMachine = new FAUNC();
}
var defaultShader;
var cylinderShader;
var workpiece;
var cylinder = new CylinderModel();
var arrow = new VertexNormalModel(arrowData);
var sphere = new VertexNormalModel(sphereData);
var lastDownTarget;
function start()
{
theResizeFunc();
canvas = document.getElementById('glCanvas');
textCanvas = document.getElementById('textCanvas');
textCanvasCtx = textCanvas.getContext("2d");
textCanvas.addEventListener('mousemove', onMouseMove);
textCanvas.addEventListener('mouseup', onMouseUp);
textCanvas.addEventListener('mousedown', onMouseDown);
textCanvas.addEventListener('touchmove', onMouseMove);
textCanvas.addEventListener('touchup', onMouseUp);
textCanvas.addEventListener('touchdown', onMouseDown);
document.addEventListener('keydown', onKeyDown, false);
document.addEventListener('keyup', onKeyUp, false);
// Initialize the GL context
gl = initWebGL(canvas);
// Only continue if WebGL is available and working
if (!gl)
{
return;
}
gl.viewport(0, 0, canvas.width, canvas.height);
// Set clear color to black, fully opaque
gl.clearColor(1.0, 0.0, 0.0, 1.0);
// Enable depth testing
gl.enable(gl.DEPTH_TEST);
// Near things obscure far things
gl.depthFunc(gl.LEQUAL);
// Clear the color as well as the depth buffer.
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
cylinder.genBuffers(gl);
arrow.genBuffers(gl);
sphere.genBuffers(gl);
cylinderShader = new CylinderShader(gl);
defaultShader = new WorkpieceShader(gl);
defaultShader.useShader();
workpiece = new Workpiece();
workpiece.generate(120, 120, 20, 0.2);
workpiece.initGL();
workpiece.updateModelData(true);
theCamera.posX = workpiece.xSize / 2;
theCamera.posY = -20;//workpiece.ySize / 2;
theCamera.posZ = workpiece.zSize + 20;
frameStart = Date.now();
renderLoop();
}
var DELTA_TIME_FACTOR = 0;
var ManualVelocities = [0, 0, 0];
var angleX = 0, angleY = 0;
var time = 0, frameStart = Date.now(), fps = 0, fpstime = 0, fpsCache = 0;
var frame = 0;
var tool = new Tool();
var simulationSpeed = 1.0;
var currentMachine = new FAUNC();
var measureFromPoint = new Vec3(0, 0, 0);
function renderAxis(message, x, y, z, scale)
{
cylinderShader.useShader();
glt.glLoadIdentity();
glt.glScale(scale, scale, scale);
glt.glTranslate(x, y, z);
cylinderShader.setColour([1, 0, 0]);
cylinderShader.loadGLTMatrices(glt);
arrow.render(gl, cylinderShader.vertexAttributeLoc, cylinderShader.normalAttributeLoc);
glt.glLoadIdentity();
glt.glScale(scale, scale, scale);
glt.glRotate(90, 0, 0, 1);
glt.glTranslate(x, y, z);
cylinderShader.setColour([0, 1, 0]);
cylinderShader.loadGLTMatrices(glt);
arrow.render(gl, cylinderShader.vertexAttributeLoc, cylinderShader.normalAttributeLoc);
glt.glLoadIdentity();
glt.glScale(scale, scale, scale);
glt.glRotate(90, 0, -1, 0);
glt.glTranslate(x, y, z);
cylinderShader.setColour([0, 0, 1]);
cylinderShader.loadGLTMatrices(glt);
arrow.render(gl, cylinderShader.vertexAttributeLoc, cylinderShader.normalAttributeLoc);
if(message != null)
{
glt.glLoadIdentity();
var screenCoord = glt.convertToScreenSpace(new Vec4([x, y, z, 1]));
if(screenCoord.data[0] > -1 && screenCoord.data[0] < 1 && screenCoord.data[1] > -1 && screenCoord.data[1] < 1 && screenCoord.data[2] < 0.9999)
{
//console.log(screenCoord.data[2]);
var pixelX = (screenCoord.data[0] * 0.5 + 0.5) * this.textCanvas.width;
var pixelY = (-screenCoord.data[1] * 0.5 + 0.5) * this.textCanvas.height;
//console.log(pixelX + "," + pixelY);
textCanvasCtx.fillText(message,pixelX,pixelY);
}
}
}
function renderLoop()
{
var elapsedtime = (Date.now() - frameStart)/1000.0;
frameStart = Date.now();
DELTA_TIME_FACTOR = elapsedtime / 0.017;
if(DELTA_TIME_FACTOR > 5)
{
console.log("Skipping Ticks: " + DELTA_TIME_FACTOR);
DELTA_TIME_FACTOR = 0.5;
elapsedtime = DELTA_TIME_FACTOR * 0.017;
}
if(frame == 0) DELTA_TIME_FACTOR = 0.5;
var simulationTime = elapsedtime * SETTINGS.simulationSpeed;
updateInput();
{
var maxSimDt = 0.5;
var count = Math.ceil(simulationTime / maxSimDt);
var dt = simulationTime / count;
for(var a = 0; a < count; a++)
{
currentMachine.state.pos[0] += ManualVelocities[0] * dt;
currentMachine.state.pos[1] += ManualVelocities[1] * dt;
currentMachine.state.pos[2] += ManualVelocities[2] * dt;
currentMachine.update(dt);
tool.posX = currentMachine.state.pos[0];
tool.posY = currentMachine.state.pos[1];
tool.posZ = currentMachine.state.pos[2];
tool.toolRadius = currentMachine.state.currentTool.d / 2.0;
tool.roundedTip = currentMachine.state.currentTool.t == 'drill';
workpiece.cut(tool);
}
}
if(frame % 2 == 0)
{
workpiece.updateModelData();
updateSimulationSpeed();
if(measureFromPoint != null)
{
var dx = theCamera.posX - measureFromPoint.data[0];
var dy = theCamera.posY - measureFromPoint.data[1];
var dz = theCamera.posZ - measureFromPoint.data[2];
var dist = Math.sqrt(dx*dx + dy*dy + dz*dz);
document.getElementById("measDX").innerHTML = Number(dx).toFixed(2);
document.getElementById("measDY").innerHTML = Number(dy).toFixed(2);
document.getElementById("measDZ").innerHTML = Number(dz).toFixed(2);
document.getElementById("measD").innerHTML = Number(dist).toFixed(2);
}
}
theCamera.movementSpeed = 0.01 * SETTINGS.moveSpeed / 30.0;
theCamera.update();
{
var framespeed = 1.0;
time += framespeed*elapsedtime;
fps++;
fpstime += elapsedtime;
if(fpstime>=1.0){
fpstime -= 1.0;
fpsCache = fps;
fps = 0;
}
}
gl.viewport(0, 0, canvas.width, canvas.height);
gl.clearColor(0.9, 0.9, 0.95, 1.0);
// Enable depth testing
gl.enable(gl.DEPTH_TEST);
// Clear the color as well as the depth buffer.
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
angleY += (currentMachine.state.spindleSpeed * 6) * simulationTime * currentMachine.state.spindleDirection;
textCanvasCtx.clearRect(0, 0, textCanvasCtx.canvas.width, textCanvasCtx.canvas.height);
textCanvasCtx.font="20px Georgia";
glt.glProjectionMatrixMode();
glt.glLoadIdentity();
//Rotate the viewing frustrum so that X is to the left, Y is into the screen and Z is up. Yay!
glt.glRotate(-90, 1, 0, 0);
glt.glPerspective(SETTINGS.fieldOfView, canvas.width / canvas.height, 0.5, 1000);
glt.glViewMatrixMode();
glt.glLoadIdentity();
theCamera.transform();
glt.glModelMatrixMode();
glt.glLoadIdentity();
defaultShader.useShader();
defaultShader.loadGLTMatrices(glt);
workpiece.render(defaultShader);
glt.glLoadIdentity();
glt.glRotate(90, 1, 0, 0);
glt.glRotate(angleY, 0, 0, 1);
glt.glScale(tool.toolRadius, tool.toolRadius, currentMachine.state.currentTool.l);
glt.glTranslate(tool.posX, tool.posY, tool.posZ);
cylinderShader.useShader();
cylinderShader.loadGLTMatrices(glt);
cylinderShader.setColour([0.6, 0.6, 0.6]);
cylinder.render(gl, cylinderShader.vertexAttributeLoc, cylinderShader.normalAttributeLoc);
if(measureFromPoint != null)
{
glt.glLoadIdentity();
glt.glScale(1, 1, 1);
glt.glTranslate(measureFromPoint.data[0], measureFromPoint.data[1], measureFromPoint.data[2]);
cylinderShader.loadGLTMatrices(glt);
cylinderShader.setColour([0.2, 0.5, 0.9]);
sphere.render(gl, cylinderShader.vertexAttributeLoc, cylinderShader.normalAttributeLoc);
}
if(SETTINGS.showCoordinates)
{
var msg1 = "Tool Home";
var msg2 = "Workpiece Zero";
if(!SETTINGS.displayCoordinateText)
{
msg1 = null;
msg2 = null;
}
renderAxis(msg1, currentMachine.state.toolHome[0], currentMachine.state.toolHome[1], currentMachine.state.toolHome[2], 10);
renderAxis(msg2, currentMachine.state.toolHome[0]-currentMachine.state.zeroOffset[0], currentMachine.state.toolHome[1]-currentMachine.state.zeroOffset[1], currentMachine.state.toolHome[2]-currentMachine.state.zeroOffset[2], 10);
}
textCanvasCtx.fillText("FPS: " + fpsCache,10,50);
frame++;
document.getElementById("SpindleSpeed").innerHTML = (currentMachine.state.spindleSpeed).toFixed(3);
document.getElementById("FeedRate").innerHTML = (currentMachine.state.feedRate * 60).toFixed(3);
document.getElementById("XPos").innerHTML = (currentMachine.state.pos[0] + currentMachine.state.toolHome[0] - currentMachine.state.zeroOffset[0]).toFixed(3);
document.getElementById("YPos").innerHTML = (currentMachine.state.pos[1] + currentMachine.state.toolHome[1] - currentMachine.state.zeroOffset[1]).toFixed(3);
document.getElementById("ZPos").innerHTML = (currentMachine.state.pos[2] + currentMachine.state.toolHome[2] - currentMachine.state.zeroOffset[2]).toFixed(3);
window.requestAnimationFrame(renderLoop, canvas);
}
function initWebGL(canvas)
{
gl = null;
// Try to grab the standard context. If it fails, fallback to experimental.
gl = canvas.getContext('webgl') || canvas.getContext('experimental-webgl');
// If we don't have a GL context, give up now
if (!gl)
{
alert('Unable to initialize WebGL. Your browser may not support it :(');
}
return gl;
}
var splitPos = 400;
var theResizeFunc = function()
{
var html = document.getElementsByTagName("html")[0];
var canvas = document.getElementById("glCanvas");
var textCanvas = document.getElementById("textCanvas");
var canvasContainer = document.getElementById("canvasContainer");
var cncCode = document.getElementById("cncCodeDiv");
var splitBar = document.getElementById("dragBar");
var viewport = document.getElementById("viewport");
var controlCenteringContainer = document.getElementById("controlCenteringContainer");
var width = html.clientWidth;
var height = window.innerHeight;
if(splitPos > width - 300)
{
splitPos = width-300;
}
else if(splitPos < 20)
{
splitPos = 20;
}
var splitBarWidth = 20;
var canvasWidth = splitPos;
var infoWidth = width - canvasWidth - splitBarWidth;
{
canvas.width = canvasWidth;
canvas.height = height;
textCanvas.width = canvasWidth;
textCanvas.height = height;
canvasContainer.style.width = canvasWidth + "px";
canvasContainer.style.height = height + "px";
}
{
splitBar.style.width = splitBarWidth + "px";
splitBar.style.height = height + "px";
}
{
cncCode.style.width = infoWidth + "px";
cncCode.style.height = height + "px";
}
{
viewport.style.height = height + "px";
}
}
function selectMeasurePoint()
{
measureFromPoint.data[0] = theCamera.posX;
measureFromPoint.data[1] = theCamera.posY;
measureFromPoint.data[2] = theCamera.posZ;
}
function setSetting(element)
{
if(element != null)
{
var settingName = element.getAttribute("data-mysetting");
if(settingName != null && settingName != undefined)
{
if(element.tagName.toLowerCase() == "input")
{
var inputType = element.getAttribute("type").toLowerCase();
if(inputType == "checkbox")
{
SETTINGS[settingName] = element.checked;
saveSettings();
}
else if(inputType == "range")
{
SETTINGS[settingName] = element.value;
saveSettings();
}
else if(inputType == "number")
{
SETTINGS[settingName] = element.value;
saveSettings();
}
}
}
}
}
function showsection(sectionId, event)
{
var els = document.getElementsByClassName("cncSection");
for(var i = 0; i < els.length; i++)
{
var element = els[i];
var theId = element.getAttribute("data-sectionid");
//console.log(theId == sectionId);
if(theId == sectionId)
{
element.style.display = "block";
}
else
{
element.style.display = "none";
}
}
{
var btns = document.getElementsByClassName("cncTab");
for(var i = 0; i < btns.length; i++)
{
if(btns[i].getAttribute("data-section") == sectionId)
{
if(!btns[i].classList.contains("active")) btns[i].classList.add("active");
}
else
{
btns[i].classList.remove("active");
}
}
}
// if(event !== undefined && event !== null)
// {
// var buttons = document.getElementsByClassName("cncTab");
// for(var i = 0; i < buttons.length; i++)
// {
// if(buttons[i] == event.target)
// {
// if(!buttons[i].classList.contains("active")) buttons[i].classList.add("active");
// }
// else
// {
// buttons[i].classList.remove("active");
// }
// }
// }
SETTINGS.openTab = sectionId;
saveSettings();
}
setTimeout(
function()
{
var html = document.getElementsByTagName("html")[0];
splitPos = html.clientWidth - 20 - 300;
showsection(SETTINGS.openTab);
theResizeFunc();
}, 0.2);
window.onresize = theResizeFunc;
function execute(event)
{
var code = document.getElementById("cncCodeTextArea").value;
currentMachine = new FAUNC();
currentMachine.parseCommandBuffer(code);
{
var maxBits = (750*750);
var xSize = currentMachine.definedWorkpiece[0];
var ySize = currentMachine.definedWorkpiece[1];
var zSize = currentMachine.definedWorkpiece[2];
var resolution = (xSize*ySize) / maxBits;
if(resolution < 0.2) resolution = 0.2;
workpiece.generate(xSize, ySize, zSize, resolution);
workpiece.initGL();
workpiece.updateModelData(true);
}
var html = currentMachine.toHTML();
document.getElementById("GCODEOUTPUT").innerHTML = html;
showsection("simulate");
theResizeFunc();
}
function updateSimulationSpeed()
{
var simulationSpeed = Number(document.getElementById("simSpeedSlider").value);
document.getElementById("simSpeed").innerHTML = (simulationSpeed).toFixed(0);
}
function fillExample(name)
{
document.getElementById("cncCodeTextArea").value = document.getElementById(name).value;
}
</script>
<style type="text/css">
html
{
margin: 0px;
padding: 0px;
text-size-adjust: 0%;
}
body
{
display:inline;
margin: 0px;
padding: 0px;
}
.btnmanual
{
float: left;
width: 38px;
height: 38px;
margin: 1px;
padding: 7px;
padding-left: 11px;
padding-top: 5px;
font-size: 24px;
}
.plus
{
background-color: #4caf50;
}
.minus
{
background-color: #ff9800;
}
.controllerButton
{
width: 31px;
height: 31px;
cursor: pointer;
float:left;
}
.controllerRow
{
width: 94px;
}
</style>
</header>
<body onload="start()" style="">
<div id="heading"></div>
<div id="info"></div>
<div id="viewport" onmousedown="onMouseDownGeneric(event)" onmousemove="onMouseMoveGeneric(event)" onmouseup="onMouseUpGeneric(event)">
<div id="canvasContainer" class="container" style="position: relative; float: left; padding: 0px; margin: 0px; height: 940px; overflow: hidden;">
<canvas id="glCanvas" width="1400" height="800" style="padding: 0px; margin:0px;">
Your browser doesn't appear to support the
<code><canvas></code> element.
</canvas>
<canvas id="textCanvas" style="position: absolute; left: 0px; top: 0px; z-index: 10;"></canvas>
</div>
<div id="dragBar" style="float: left; height: 900px; width: 20px; background-color:#999999;" >
</div>
<div id="cncCodeDiv" style="float: left; width: 400px; padding: 5px;">
<div style="margin: auto;">
<div class="btn-group" style="width: 269px;">
<button type="button" class="btn btn-default cncTab" data-section="code" onclick="showsection('code', event)">Code</button>
<button type="button" class="btn btn-info cncTab" data-section="settings" onclick="showsection('settings', event)">Settings</button>
<button type="button" class="btn btn-success cncTab" data-section="simulate" onclick="showsection('simulate', event)">Simulate</button>
</div>
<div style="background-color: #888888 ; width: 100%; height: 1px; margin-top: 2px; margin-bottom:2px;"></div>
</div>
<div class="cncSection" data-sectionid="code">
<input type="hidden" id="example1" value="##workpiece 270 200 20 0 0 0
##toolhome -50 0 60
##tool 1 12 20 mill
##tool 2 20 30 drill
##tool 3 12 20 drill
##tool 4 6 10 drill
O0005; WEEK 6 TUTE
G90 G21 G92 X-50 Y0 Z40; Programming in absolute coordinates; Set home pos
;PROGRAM 1
T01 M11; Select 12mm End Milling Tool
G10 P01 R6.0; Set register 1, to radius of 6mm
;CIRCLE
G90 G00 X200 Y130 Z2; Go to center of circle
G01 Z-2.5 M03 S1200 F50.0; Move down to circle base. Turn on spindle at 1200RPM
G01 X239 D01; Move to right side of circle. Enable cutter compensation, left side
G03 X161 R39.0; Cut top semicircle
G03 X239 R39.0; Cut bottom semicircle
G01 X200 G40; Move away from circle edge, disable compensation
G01 Z-5; Move down for final circle cut. Now repeat
G01 X239 D01; Move to right side of circle. Enable cutter compensation, left side
G03 X161 R39.0; Cut top semicircle
G03 X239 R39.0; Cut bottom semicircle
G01 X220 G40; Move away from circle edge, disable compenstation
G01 X200 Y130 Z-5
X228
G03 X172 R28
G03 X228 R28
G01 X218
G03 X182 R18
G03 X218 R18
G01 X208
G03 X192 R8
G03 X208 R8
G90 G00 Z5;
;HEX
G00 X80 Y130 Z5
G01 Z-2.5 M03 S1200 F50.0; Move down to hex 2.5mm below surface. Turn on spindle at 1200RPM
G16; Enable polar coordinates
G93 I80 J130; Set polar coordinates origin
G01 X46.2 Y0; Move to right side of hex, enable cutter compensation, left side
G91 M98 P0001 L6; Move to each point of the hex shape
G90 G00 X20 Y0 G40; Move away from edge, disable compensation
G01 Z-5; Move down to second layer
G90 G01 X46.2 Y0; Move to right side of hex, enable cutter compensation, left side
G91 M98 P0001 L6; Move to each point of the hex shape
G90 G00 X20 Y0 G40; Move away from edge, disable compensation
;MIDDLE
G90 G01 X35 Y0; Move to right side of hex, enable cutter compensation, left side
G91 M98 P0001 L6; Move to each point of the hex shape
G90 G00 X20 Y0 G40; Move away from edge, disable compensation
G90 G01 X25 Y0; Move to right side of hex, enable cutter compensation, left side
G91 M98 P0001 L6; Move to each point of the hex shape
G90 G00 X20 Y0 G40; Move away from edge, disable compensation
G90 G01 X15 Y0; Move to right side of hex, enable cutter compensation, left side
G91 M98 P0001 L6; Move to each point of the hex shape
G90 G00 X20 Y0 G40; Move away from edge, disable compensation
G90 G01 X6 Y0; Move to right side of hex, enable cutter compensation, left side
G91 M98 P0001 L6; Move to each point of the hex shape
G90 G00 X20 Y0 G40; Move away from edge, disable compensation
G15; Disable polar coordinates.
G00 Z5; Move away from part
;HOLES
G00 X20 Y40 Z5; Move to first bottom left hole
T2 M11; Select 20mm drill
G98 G90 G81 X20 Y40 R1 Z-22 F50.0; Drill Hole. Retract to initial Z, absolute coords, 3mm/min feed
G81 X20 Y170 R1 Z-22; Top Left
G81 X140 Y170 R1 Z-22; Top Middle
G81 X140 Y90 R1 Z-22; Bottom Middle
G81 X200 Y130 R1 Z-22; Center Middle
T3 M11; Select 12mm drill
G16; Enable polar coordinates
G90 G93 I200 J130; Set polar coordinates origin
G00 X33 Y0 Z5; Move to first hole
G98 G91 G81 Y45 R1 Z-22 L8; Drill all 8 holes
T4 M11; Select 6mm drill
G90 G00 X42 Y0 Z5
G98 G91 G81 Y90 R1 Z-22 L4; Drill all 4 holes
G15; Disable polar coordinates.
G90 G00 Z5; Move away from part
;PROGRAM 2
T1 M11; Reselect end mill, 12mm
G00 X-10 Y40 Z5; Move to left side of part
G00 Z-3 M03 S1200 F50.0; Move down depth of cut, 3mm. Set spindle speed
G91 M98 P0002 L7; Cut contour, 7 times (7*3mm = 21mm > 20mm)
G90 G00 Z5 M05 M09; Move up away from part, turn off coolant and spindle
M30; DONE!
O0001; (HEX Sub Program)
G01 X0 Y60;
M99;
O0002; (Contour Sub Program)
G90 G01 X0 D01; Enter part, enable cutter compensation, right side
G91 G03 X40 R20; Cut bottom circle, 180 deg, CCW
G01 Y24;
G02 X20 Y20 R20; Cut inner edge
G01 X45
X35 Y-14
X60
G03 Y120 R60; Cut right circle edge
G01 X-180; Top edge
G03 X-20 Y-20 R20; Top left corner
G01 Y-130; Left edge
G00 X-10; Exit part
G00 Z-3; Move down 3mm, ready for next cut
M99;
">
<input type="hidden" id="example2" value="##workpiece 260 190 20 0 0 0