-
Notifications
You must be signed in to change notification settings - Fork 24
/
SVGtoSTL.html
executable file
·364 lines (338 loc) · 14.7 KB
/
SVGtoSTL.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
<html>
<head>
<title>Convert SVG to STL</title>
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<script src="js/external/jquery-2.1.4.min.js"></script>
<!-- WebGL Geometry and rendering library -->
<script src="js/external/three.js"></script>
<script src="js/external/OrbitControls.js"></script>
<!-- Turn an SVG path into a three.js geometry -->
<script src="js/external/d3-threeD.js"></script>
<!-- Write a three.js geometry into ASCII STL -->
<script src="js/external/STLExporter.js"></script>
<!-- "Flatten" an SVG document by applying all transforms to the paths -->
<script src="js/external/flatten.js"></script>
<!-- Constructive Solid Geometry -->
<script src="js/external/ThreeCSG.js"></script>
<!-- In-memory file reads and writes -->
<script src="js/external/FileSaver.js"></script>
<!-- Javascript color-picker -->
<script src='js/external/spectrum.js'></script>
<link rel='stylesheet' href='css/spectrum.css'/>
<!-- Code that pulls all of the above together -->
<script src="js/SVGtoSTL.js"></script>
<!-- Make it pretty -->
<link rel="stylesheet" type="text/css" href="css/style.css"/>
</head>
<body>
<div id="header">
<div class="uploadDiv">
<div class="fileUpload button">
<span><img src="img/image.svg"/> Upload SVG</span>
<input type="file" class="upload" name="files[]" accept="image/svg+xml"/>
</div>
</div>
<div class="downloadDiv">
<div class="download button">
<img src="img/install.svg"/> Download STL
</div>
</div>
</div>
<div id="content">
<div id="svgCanvas" class="svgCanvas">
<img id="uploadedSVG" src="#"/>
</div>
<div id="optionsDiv" class="optionsDiv">
<link rel="stylesheet" href="css/form-green.css" type="text/css" />
<!-- Start Formoid form-->
<form class="formoid-solid-light-green" style="background-color:#FFFFFF;font-size:14px;font-family:'Roboto',Arial,Helvetica,sans-serif;color:#34495E;max-width:480px;min-width:150px" method="post">
<div class="title"><h2>Options</h2></div>
<div class="element-input">
<label class="title" for="typeWidth">Image Size (mm)</label>
<div class="item-cont">
<input class="large" type="text" name="typeSize" id="typeSize" placeholder="Image Size (mm)" value="60"/>
</label>
<span class="icon-place"></span>
</div>
</div>
<div class="element-input">
<label class="title"></label>
<label class="title" for="typeDepth">Depth of Type (mm)</label>
<div class="item-cont">
<input class="large" type="text" name="typeDepth" id="typeDepth" placeholder="Depth of Type (mm)" value="3"/>
<span class="icon-place"></span>
</div>
</div>
<div class="element-checkbox">
<label class="title"></label>
<div class="column column1">
<label>
<input type="checkbox" name="checkbox[]" id="invertType" value="Invert Type"/>
<span>Invert Type</span>
</label>
</div>
<span class="clearfix"></span>
</div>
<div class="element-checkbox">
<label class="title"></label>
<div class="column column1">
<label>
<input type="checkbox" name="checkbox[]" id="bevel" value="Flare Type"/>
<span>Flare Type</span>
</label>
</div>
<span class="clearfix"></span>
</div>
<div class="element-checkbox">
<label class="title"></label>
<div class="column column1">
<label>
<input type="checkbox" name="checkbox[]" id="reverseWO" value="Reverse Winding Order"/>
<span>Reverse Winding-order</span>
</label>
</div>
<span class="clearfix"></span>
</div>
<div class="element-checkbox">
<label class="title"></label>
<div class="column column1">
<label>
<input type="checkbox" name="checkbox[]" id="wantBasePlate" value="Use Base Plate" checked/>
<span>Use Base Plate</span>
</label>
</div>
<span class="clearfix"></span>
</div>
<div class="element-radio">
<label class="title baseShape">Base Plate Shape</label>
<div class="column column1">
<label>
<input type="radio" name="baseShape" value="Rectangular" checked/>
<span>Rectangular</span>
</label>
<label>
<input type="radio" name="baseShape" value="Circular"/>
<span>Circular</span>
</label>
</div>
<span class="clearfix"></span>
</div>
<div class="element-input">
<label class="title"></label>
<label class="title baseDepth" for="baseDepth">Depth of Base (mm)</label>
<div class="item-cont">
<input class="large" type="text" name="baseDepth" id="baseDepth" placeholder="Depth of Base (mm)" value="5"/>
<span class="icon-place"></span>
</div>
</div>
<div class="element-input">
<label class="title"></label>
<label class="title buffer" for="buffer">Buffer (mm)</label>
<div class="item-cont">
<input class="large" type="text" name="buffer" id="buffer" placeholder="Buffer (mm)" value="5"/>
<span class="icon-place"></span>
</div>
</div>
<div class="element-checkbox">
<label class="title">Render Options</label>
<div class="column column1">
<label>
<input type="checkbox" name="checkbox1[]" id="showWireFrame" value="Show Wire Frame"/>
<span>Show Wire Frame</span>
</label>
<label>
<input type="checkbox" name="checkbox1[]" id="showEdges" value="Show Edges" checked/>
<span>Show Edges</span>
</label>
<label>
<input type="checkbox" name="checkbox1[]" id="showNormals" value="Show Normals"/>
<span>Show Normals</span>
</label>
</div>
<span class="clearfix"></span>
</div>
<div class="element-input">
<label class="title">Color</label>
<input type="text" id="colorPicker"/>
</div>
<div class="submit">
<input type="submit" value="Re-render"/>
</div>
</form>
<script type="text/javascript" src="js/form-green.js"></script>
<!-- Stop Formoid form-->
</div>
<div id="stlCanvas" class="stlCanvas">
</div>
</div>
<!-- In-memory file read and parsing -->
<script>
$(document).ready( function () {
// When asked to upload an SVG file, use this handler
$(".upload").on("change", handleFileSelect);
// Initialize the color picker
$("#colorPicker").spectrum({ color: "#5d9dea" });
// Handler for form submit
$("form").submit(function(e){
e.preventDefault();
// Numeric validations would go here
clearGroup(group);
renderObject(svgPaths, scene, group, getFormSelections());
return true;
});
// Disable some options that don't apply when base plate is off
$("input#wantBasePlate").change(function() {
$("input[name=baseShape]").prop( "disabled", !this.checked );
$("label.baseShape").toggleClass("disabled");
$("input#bevel").prop( "disabled", !this.checked );
$("input#baseDepth").prop( "disabled", !this.checked );
$("label.baseDepth").toggleClass("disabled");
$("input#buffer").prop( "disabled", !this.checked );
$("label.buffer").toggleClass("disabled");
$("input#basePlateShape").prop( "disabled", !this.checked );
$("input#bevel").prop( "disabled", !this.checked );
});
// When asked to export 3D scene to STL file, do so,
// using the uploaded filename with a changed extension
$(".download").on("click", function(){ saveSTL(scene, fileName.replace(/\.[^\.]*$/, "")); });
});
// Check for webgl support
function webgl() {
try {
var canvas = document.createElement( 'canvas' ); return !! ( window.WebGLRenderingContext && ( canvas.getContext( 'webgl' ) || canvas.getContext( 'experimental-webgl' ) ) );
} catch ( e ) {
return false;
}
}
// Check for FileReader API support
function fileapi() {
return window.File && window.FileReader && window.FileList && window.Blob;
}
// Message to be built if webgl/fileapi support isn't present
function notSupportedMessage(element) {
$(element).empty(); // Clear the element
$(element).append($("<div class='container'><div class='notsupported'></div></div>"));
$(".notsupported").append($("<p class='big'>NOPE.</p>"));
$(".notsupported").append($("<p class='small'>Your browser doesn't seem to support WebGL and/or the FileReader API.</p>"));
$(".notsupported").append($("<p class='small'>Try a recent browser like <a href='https://www.google.com/chrome/browser/desktop' target='_blank'>Chrome</a> or <a href='https://www.mozilla.org/en-US/firefox' target='_blank'>Firefox</a>.</p>"));
}
// First check if support is present for the functions we'll use
if(!webgl() || !fileapi()) {
// Print a message that the browser doesn't support what we need
notSupportedMessage($("body"));
}
// Remember the file name of the uploaded SVG
// we'll re-use this with a ".stl" extension at download time
var fileName;
// The 'path' tags from the SVG are all we need to keep.
var svgPaths = [];
// Pull options from the form
function getFormSelections() {
return {
typeSize: Math.abs(Number($("input#typeSize").val())),
typeDepth: Number($("input#typeDepth").val()),
wantInvertedType: $("input#invertType").prop("checked"),
svgWindingIsCW: $("input#reverseWO").prop("checked"),
bevelEnabled: $("input#bevel").prop("checked"),
wantBasePlate: $("input#wantBasePlate").prop("checked"),
basePlateShape: $("input[name=baseShape]:checked").val(),
baseDepth: Math.abs(Number($("input#baseDepth").val())),
baseBuffer: Math.abs(Number($("input#buffer").val())),
wantWireFrame: $("input#showWireFrame").prop("checked"),
wantEdges: $("input#showEdges").prop("checked"),
wantNormals: $("input#showNormals").prop("checked"),
objectColor: $("input#colorPicker").spectrum("get").toHexString()
};
}
function handleFileSelect(evt) {
var file = evt.target.files[0]; // FileList object
// Only process image files.
if (file.type!="image/svg+xml") {
return;
}
// To render the SVG on-screen
var readerURL = new FileReader();
// To parse the xml within the SVG file
var readerString = new FileReader();
readerURL.onload = function (e) {
$('#uploadedSVG').attr('src', e.target.result);
$('#uploadedSVG').fadeIn();
}
// Closure to capture the file information.
readerString.onload = (function(uploadedFile) {
return function(e) {
console.log("Parsing SVG paths...");
// Get the paths out of the SVG
var svgDoc = $.parseXML(e.target.result);
// "Flatten" the SVG by applying all transforms to shapes and paths
flatten(svgDoc.children[0]);
// Write to global paths variable. Is there an easy way to return from a closure?
svgPaths = $("path", svgDoc).map(function(){return $(this).attr("d");}).get();
// Store the file name in a global
fileName = uploadedFile.name;
// In case this is the second file uploaded.
clearGroup(group);
// Render the SVG for the first time
renderObject(svgPaths, scene, group, getFormSelections());
};
})(file);
// Read in the svg file (in-memory)
console.log("Attempting to read file '"+file.name+"'...");
// Reads the SVG contents into a string
readerString.readAsText(evt.target.files[0]);
// Reads the SVG as a blob, to be referenced via URL
readerURL.readAsDataURL(evt.target.files[0]);
};
</script>
<!-- Set up the 3D canvas, with rotation controls -->
<script>
var camera, scene, group, renderer, controls;
if(webgl()) {
init();
$("#stlCanvas").append(renderer.domElement);
animate();
}
function init() {
var divWidth = (window.innerWidth/2) - 100;
var divHeight = (window.innerHeight) - 60;
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setClearColor( 0xe0e0e0 );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( divWidth, divHeight );
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera( 50, divWidth/divHeight, 1, 1000 );
camera.position.set( 0, -200, 200 );
controls = new THREE.OrbitControls( camera, renderer.domElement );
controls.minDistance = 50;
controls.maxDistance = 200;
scene.add( new THREE.AmbientLight( 0x222222 ) );
/// direct light
var light = new THREE.DirectionalLight( 0x222222 );
light.position.set( 0.75, 0.75, 1.0 ).normalize();
scene.add( light );
light = new THREE.PointLight( 0x222222 );
light.position.copy( camera.position );
scene.add( light );
/// backgroup grids
var helper = new THREE.GridHelper( 70, 10 );
helper.rotation.x = Math.PI / 2;
scene.add( helper );
group= new THREE.Group();
scene.add( group );
window.addEventListener( 'resize', onWindowResize, false );
function onWindowResize(){
var divWidth = (window.innerWidth/2) - 100;
var divHeight = (window.innerHeight) - 60;
camera.aspect = divWidth / divHeight;
camera.updateProjectionMatrix();
renderer.setSize( divWidth, divHeight );
}
}
function animate() {
requestAnimationFrame( animate );
controls.update();
renderer.render( scene, camera );
}
</script>
</body>
</html>