forked from nraynaud/webgcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
195 lines (187 loc) · 6.57 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
<!DOCTYPE html >
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="chrome=1"/>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>g-code simulator</title>
<script src="webapp/libs/jquery.min.js"></script>
<script src="webapp/libs/jquery.flot.min.js"></script>
<script src="webapp/libs/jquery.flot.resize.js"></script>
<script src="webapp/libs/Three.js"></script>
<script src="webapp/libs/TrackballControls.js"></script>
<script src="webapp/libs/jsparse.js"></script>
<script src="webapp/cnc/geometry.js"></script>
<script src="webapp/cnc/simulation.js"></script>
<script src="webapp/cnc/parser.js"></script>
<style>
.editBlock {
position: relative;
float: left;
width: 49%;
height: 400px;
padding: 1px;
margin: 0;
text-align: right;
}
.editBlock textarea {
width: 100%;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
height: 350px;
}
.chart {
position: relative;
clear: both;
width: 100%;
height: 200px;
}
#container {
float: right;
background: #000;
width: 50%;
height: 400px;
}
</style>
</head>
<body>
<h1>G-Code Q'n'dirty toolpath simulator</h1>
<p>
Paste your g-code in the left window and see the 3D preview of your toolpath on the right.<br>
The right pane is interactive, drag it to change the point of view.
</p>
<p>
<a href="https://github.com/nraynaud/webgcode/tree/gh-pages">https://github.com/nraynaud/webgcode/tree/gh-pages</a>
</p>
<div id="container"></div>
<div class="editBlock">
<textarea id="codebox">
G0 Y10 Z-5
G1 Z-10
G1 Y20
G02 X10 Y30 R10
G1 X30
G2 X40 Y20 R10
G1 Y10
G2 X30 Y0 R10
G1 X10
G2 X0 Y10 Z-15 R10 (yeah spiral !)
G3 X-10 Y20 R-10 (yeah, long arc !)
G3 X0 Y10 I10 (center)
G91 G1 X10 Z10
G3 Y10 R5 Z3 (circle in incremental)
Y10 R5 Z3 (again, testing modal state)
G20 G0 X1 (one inch to the right)
G3 X-1 R1 (radius in inches)
G3 X1 Z0.3 I0.5 J0.5 (I,J in inches)
G21 (back to mm)
G80 X10 (do nothing)
G90
G0 X30 Y30 Z30
G18 (X-Z plane)
G3 Z40 I0 J5
G19 (Y-Z plane)
G3 Z50 I0 J5
G17 (back to X-Y plane)
</textarea>
<button onclick="evaluateCode();">Simulate</button>
</div>
<div class="chart" id="chart1"></div>
<div class="chart" id="chart2"></div>
<div class="chart" id="chart3"></div>
<script>
function getURLParameter(name) {
return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search) || [, ""])[1].replace(/\+/g, '%20')) || null;
}
</script>
<script>
var WIDTH = 600;
var HEIGHT = 400;
var mouse = new THREE.Vector2(), INTERSECTED;
projector = new THREE.Projector();
raycaster = new THREE.Raycaster();
var $container = $('#container');
var renderer = new THREE.WebGLRenderer({antialias: true});
var camera = new THREE.PerspectiveCamera(45, WIDTH / HEIGHT, 0.1, 20000);
var scene = new THREE.Scene();
camera.position.x = 30;
camera.position.y = 30;
camera.position.z = 30;
camera.up.set(0, 0, 1);
renderer.setSize(WIDTH, HEIGHT);
$(window).resize(function () {
camera.aspect = $container.width() / $container.height();
camera.updateProjectionMatrix();
renderer.setSize($container.width(), $container.height());
renderer.render(scene, camera);
});
$container.append(renderer.domElement);
scene.add(camera);
controls = new THREE.TrackballControls(camera, $container[0]);
controls.rotateSpeed = 1.0;
controls.zoomSpeed = 1.2;
controls.panSpeed = 0.8;
controls.noZoom = false;
controls.noPan = false;
controls.staticMoving = true;
controls.dynamicDampingFactor = 0.3;
controls.minDistance = 3;
controls.keys = [ 65, 83, 68 ];
controls.addEventListener('change', function () {
renderer.render(scene, camera);
});
function animate() {
requestAnimationFrame(animate);
controls.update();
}
var mesh = null;
var contourMesh = null;
var contourLines = [];
var planeGeometry = new THREE.PlaneGeometry(10, 10, 5, 5);
scene.add(new THREE.Mesh(planeGeometry, new THREE.MeshBasicMaterial({wireframe: true, color: 0x00CC00})));
function createAxis(x, y, z, color) {
var geom = new THREE.Geometry();
geom.vertices.push(new THREE.Vector3(0, 0, 0));
geom.vertices.push(new THREE.Vector3(x, y, z));
return new THREE.Line(geom, new THREE.LineBasicMaterial({color: color}));
}
var axes = new THREE.Object3D();
axes.add(createAxis(10, 0, 0, 0xFF0000));
axes.add(createAxis(0, 10, 0, 0x00FF00));
axes.add(createAxis(0, 0, 10, 0x0000FF));
scene.add(axes);
var tool = new THREE.Object3D();
var toolbit = new THREE.Mesh(new THREE.CylinderGeometry(2, 2, 10, 20, 2, false), new THREE.MeshPhongMaterial({emissive: 0xEF0000, specular: 0x0F0000, shininess: 204, color: 0xF0F0F0, opacity: 0.5, transparent: true}));
toolbit.translateY(5);
var spindle = new THREE.Mesh(new THREE.CylinderGeometry(4, 4, 15, 25, 2, false), new THREE.MeshPhongMaterial({emissive: 0xEFEFEF, specular: 0x0F0F0F, shininess: 204, color: 0xF0F0F0, opacity: 0.5, transparent: true}));
spindle.translateY(17.5);
tool.applyMatrix(new THREE.Matrix4().makeRotationX(Math.PI / 2));
tool.add(toolbit);
tool.add(spindle);
tool.matrixAutoUpdate = true;
scene.add(tool);
animate();
</script>
<script>
window.addEventListener("message", function (event) {
if (event.data['type'] == 'gimme program') {
evaluateCode();
event.source.postMessage({type: 'program', program: $('#codebox').val()}, event.origin);
}
if (event.data['type'] == 'toolPosition') {
var pos = event.data['position'];
$('#toolPos').attr('translation', [pos.x, pos.y, pos.z].join(' '));
$('#toolPos').attr('render', true);
tool.position.setX(pos.x);
tool.position.setY(pos.y);
tool.position.setZ(pos.z);
tool.traverse(function (child) {
child.visible = true;
});
renderer.render(scene, camera);
}
}, false);
evaluateCode();
</script>
</body>
</html>