Now uses FileSystem API for downloading the STL file
This commit is contained in:
parent
3b4e1fd1a6
commit
8b51bd6b68
|
@ -8,7 +8,7 @@
|
||||||
|
|
||||||
body {
|
body {
|
||||||
font: 14px/20px 'Helvetica Neue Light', HelveticaNeue-Light, 'Helvetica Neue', Helvetica, Arial, sans-serif;
|
font: 14px/20px 'Helvetica Neue Light', HelveticaNeue-Light, 'Helvetica Neue', Helvetica, Arial, sans-serif;
|
||||||
max-width: 800px;
|
max-width: 820px;
|
||||||
margin: 0 auto;
|
margin: 0 auto;
|
||||||
padding: 10px;
|
padding: 10px;
|
||||||
}
|
}
|
||||||
|
@ -36,8 +36,12 @@ canvas { cursor: move; }
|
||||||
|
|
||||||
</style>
|
</style>
|
||||||
<script>
|
<script>
|
||||||
|
|
||||||
var gProcessor=null;
|
var gProcessor=null;
|
||||||
|
|
||||||
|
// Show all exceptions to the user:
|
||||||
|
OpenJsCad.AlertUserOfUncaughtExceptions();
|
||||||
|
|
||||||
function onload()
|
function onload()
|
||||||
{
|
{
|
||||||
gProcessor = new OpenJsCad.Processor(document.getElementById("viewer"));
|
gProcessor = new OpenJsCad.Processor(document.getElementById("viewer"));
|
||||||
|
|
114
openjscad.js
114
openjscad.js
|
@ -329,6 +329,41 @@ OpenJsCad.Processor = function(containerdiv, onchange) {
|
||||||
this.createElements();
|
this.createElements();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
OpenJsCad.FileSystemApiErrorHandler = function(fileError, operation) {
|
||||||
|
var errormap = {
|
||||||
|
1: NOT_FOUND_ERR,
|
||||||
|
2: SECURITY_ERR,
|
||||||
|
3: ABORT_ERR,
|
||||||
|
4: NOT_READABLE_ERR,
|
||||||
|
5: ENCODING_ERR,
|
||||||
|
6: NO_MODIFICATION_ALLOWED_ERR,
|
||||||
|
7: INVALID_STATE_ERR,
|
||||||
|
8: SYNTAX_ERR,
|
||||||
|
9: INVALID_MODIFICATION_ERR,
|
||||||
|
10: QUOTA_EXCEEDED_ERR,
|
||||||
|
11: TYPE_MISMATCH_ERR,
|
||||||
|
12: PATH_EXISTS_ERR,
|
||||||
|
};
|
||||||
|
var errname;
|
||||||
|
if(fileError.code in errormap)
|
||||||
|
{
|
||||||
|
errname = errormap[fileError.code];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
errname = "Error #"+fileError.code;
|
||||||
|
}
|
||||||
|
var errtxt = "FileSystem API error: "+operation+" returned error "+errname;
|
||||||
|
throw new Error(errtxt);
|
||||||
|
};
|
||||||
|
|
||||||
|
OpenJsCad.AlertUserOfUncaughtExceptions = function() {
|
||||||
|
window.onerror = function(message, url, line) {
|
||||||
|
message = message.replace(/^Uncaught /i, "");
|
||||||
|
alert(message+"\n\n("+url+" line "+line+")");
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
OpenJsCad.Processor.prototype = {
|
OpenJsCad.Processor.prototype = {
|
||||||
createElements: function() {
|
createElements: function() {
|
||||||
while(this.containerdiv.children.length > 0)
|
while(this.containerdiv.children.length > 0)
|
||||||
|
@ -419,12 +454,17 @@ OpenJsCad.Processor.prototype = {
|
||||||
this.errordiv.style.display = (txt == "")? "none":"block";
|
this.errordiv.style.display = (txt == "")? "none":"block";
|
||||||
},
|
},
|
||||||
|
|
||||||
setJsCad: function(script) {
|
// script: javascript code
|
||||||
|
// filename: optional, the name of the .jscad file
|
||||||
|
setJsCad: function(script, filename) {
|
||||||
|
if(!filename) filename = "openjscad.jscad";
|
||||||
|
filename = filename.replace(/\.jscad$/i, "");
|
||||||
this.abort();
|
this.abort();
|
||||||
this.clearViewer();
|
this.clearViewer();
|
||||||
this.setError("");
|
this.setError("");
|
||||||
this.processing = true;
|
this.processing = true;
|
||||||
this.statusspan.innerHTML = "Processing, please wait...";
|
this.statusspan.innerHTML = "Processing, please wait...";
|
||||||
|
this.filename = filename;
|
||||||
var that = this;
|
var that = this;
|
||||||
this.worker = OpenJsCad.javaScriptToSolidASync(script, function(err, csg) {
|
this.worker = OpenJsCad.javaScriptToSolidASync(script, function(err, csg) {
|
||||||
that.processing = false;
|
that.processing = false;
|
||||||
|
@ -456,7 +496,7 @@ OpenJsCad.Processor.prototype = {
|
||||||
return this.processing;
|
return this.processing;
|
||||||
},
|
},
|
||||||
|
|
||||||
clearStl: function() {
|
clearStl1: function() {
|
||||||
if(this.hasstl)
|
if(this.hasstl)
|
||||||
{
|
{
|
||||||
this.hasstl = false;
|
this.hasstl = false;
|
||||||
|
@ -467,7 +507,7 @@ OpenJsCad.Processor.prototype = {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
generateStl: function() {
|
generateStl1: function() {
|
||||||
this.clearStl();
|
this.clearStl();
|
||||||
if(this.validcsg)
|
if(this.validcsg)
|
||||||
{
|
{
|
||||||
|
@ -479,4 +519,72 @@ OpenJsCad.Processor.prototype = {
|
||||||
if(this.onchange) this.onchange();
|
if(this.onchange) this.onchange();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
clearStl: function() {
|
||||||
|
if(this.hasstl)
|
||||||
|
{
|
||||||
|
this.hasstl = false;
|
||||||
|
if(that.stlDirEntry)
|
||||||
|
{
|
||||||
|
that.stlDirEntry.removeRecursively();
|
||||||
|
that.stlDirEntry=null;
|
||||||
|
}
|
||||||
|
this.enableItems();
|
||||||
|
if(this.onchange) this.onchange();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
generateStl: function() {
|
||||||
|
this.clearStl();
|
||||||
|
if(this.validcsg)
|
||||||
|
{
|
||||||
|
var stltxt = this.solid.toStlString();
|
||||||
|
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
|
||||||
|
window.BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder;
|
||||||
|
if(!window.requestFileSystem)
|
||||||
|
{
|
||||||
|
throw new Error("Your browser does not support the HTML5 FileSystem API");
|
||||||
|
}
|
||||||
|
if(!window.BlobBuilder)
|
||||||
|
{
|
||||||
|
throw new Error("Your browser does not support the HTML5 BlobBuilder API");
|
||||||
|
}
|
||||||
|
// create a random directory name:
|
||||||
|
var dirname = "OpenJsCadStlOutput_"+parseInt(Math.random()*1000000000, 10)+".stl";
|
||||||
|
var filename = this.filename+".stl";
|
||||||
|
var that = this;
|
||||||
|
window.requestFileSystem(TEMPORARY, 20*1024*1024, function(fs){
|
||||||
|
fs.root.getDirectory(dirname, {create: true, exclusive: true}, function(dirEntry) {
|
||||||
|
that.stlDirEntry = dirEntry;
|
||||||
|
dirEntry.getFile(filename, {create: true, exclusive: true}, function(fileEntry) {
|
||||||
|
fileEntry.createWriter(function(fileWriter) {
|
||||||
|
fileWriter.onwriteend = function(e) {
|
||||||
|
that.hasstl = true;
|
||||||
|
that.downloadStlLink.href = fileEntry.toURL();
|
||||||
|
that.enableItems();
|
||||||
|
if(that.onchange) that.onchange();
|
||||||
|
};
|
||||||
|
fileWriter.onerror = function(e) {
|
||||||
|
throw new Error('Write failed: ' + e.toString());
|
||||||
|
};
|
||||||
|
// Create a new Blob and write it to log.txt.
|
||||||
|
var bb = new window.BlobBuilder(); // Note: window.WebKitBlobBuilder in Chrome 12.
|
||||||
|
bb.append(stltxt);
|
||||||
|
fileWriter.write(bb.getBlob());
|
||||||
|
},
|
||||||
|
function(fileerror){OpenJsCad.FileSystemApiErrorHandler(fileerror, "createWriter");}
|
||||||
|
);
|
||||||
|
},
|
||||||
|
function(fileerror){OpenJsCad.FileSystemApiErrorHandler(fileerror, "getFile('"+filename+"')");}
|
||||||
|
);
|
||||||
|
},
|
||||||
|
function(fileerror){OpenJsCad.FileSystemApiErrorHandler(fileerror, "getDirectory('"+dirname+"')");}
|
||||||
|
);
|
||||||
|
},
|
||||||
|
function(fileerror){OpenJsCad.FileSystemApiErrorHandler(fileerror, "requestFileSystem");}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
|
@ -8,6 +8,7 @@
|
||||||
|
|
||||||
body {
|
body {
|
||||||
font: 14px/20px 'Helvetica Neue Light', HelveticaNeue-Light, 'Helvetica Neue', Helvetica, Arial, sans-serif;
|
font: 14px/20px 'Helvetica Neue Light', HelveticaNeue-Light, 'Helvetica Neue', Helvetica, Arial, sans-serif;
|
||||||
|
max-width: 820px;
|
||||||
}
|
}
|
||||||
|
|
||||||
pre, code, textarea {
|
pre, code, textarea {
|
||||||
|
@ -55,19 +56,17 @@ canvas { cursor: move; }
|
||||||
</style>
|
</style>
|
||||||
<script>
|
<script>
|
||||||
|
|
||||||
var gCurrentFile = null;
|
|
||||||
|
|
||||||
|
var gCurrentFile = null;
|
||||||
var gProcessor=null;
|
var gProcessor=null;
|
||||||
|
|
||||||
|
// Show all exceptions to the user:
|
||||||
|
OpenJsCad.AlertUserOfUncaughtExceptions();
|
||||||
|
|
||||||
function onload()
|
function onload()
|
||||||
{
|
{
|
||||||
try
|
|
||||||
{
|
|
||||||
gProcessor = new OpenJsCad.Processor(document.getElementById("viewer"));
|
gProcessor = new OpenJsCad.Processor(document.getElementById("viewer"));
|
||||||
setupDragDrop();
|
setupDragDrop();
|
||||||
} catch (e) {
|
|
||||||
alert(e.toString());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function setupDragDrop()
|
function setupDragDrop()
|
||||||
|
@ -76,7 +75,7 @@ function setupDragDrop()
|
||||||
if (window.File && window.FileReader && window.FileList) {
|
if (window.File && window.FileReader && window.FileList) {
|
||||||
// Great success! All the File APIs are supported.
|
// Great success! All the File APIs are supported.
|
||||||
} else {
|
} else {
|
||||||
alert("Error: Your browser does not fully support the HTML File API");
|
throw new Error("Error: Your browser does not fully support the HTML File API");
|
||||||
}
|
}
|
||||||
var dropZone = document.getElementById('filedropzone');
|
var dropZone = document.getElementById('filedropzone');
|
||||||
dropZone.addEventListener('dragover', function(evt) {
|
dropZone.addEventListener('dragover', function(evt) {
|
||||||
|
@ -89,8 +88,6 @@ function setupDragDrop()
|
||||||
|
|
||||||
function handleFileSelect(evt)
|
function handleFileSelect(evt)
|
||||||
{
|
{
|
||||||
try
|
|
||||||
{
|
|
||||||
evt.stopPropagation();
|
evt.stopPropagation();
|
||||||
evt.preventDefault();
|
evt.preventDefault();
|
||||||
|
|
||||||
|
@ -112,9 +109,6 @@ function handleFileSelect(evt)
|
||||||
gCurrentFile = file;
|
gCurrentFile = file;
|
||||||
gPreviousModificationTime = "";
|
gPreviousModificationTime = "";
|
||||||
fileChanged();
|
fileChanged();
|
||||||
} catch (e) {
|
|
||||||
alert(e.toString());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function fileChanged()
|
function fileChanged()
|
||||||
|
@ -144,21 +138,27 @@ function parseFile()
|
||||||
var txt = evt.target.result;
|
var txt = evt.target.result;
|
||||||
};
|
};
|
||||||
reader.onloadend = function(evt) {
|
reader.onloadend = function(evt) {
|
||||||
try
|
|
||||||
{
|
|
||||||
if (evt.target.readyState == FileReader.DONE)
|
if (evt.target.readyState == FileReader.DONE)
|
||||||
{
|
{
|
||||||
var jscadscript = evt.target.result;
|
var jscadscript = evt.target.result;
|
||||||
if(jscadscript == "")
|
if(jscadscript == "")
|
||||||
{
|
{
|
||||||
throw new Error("Could not read file. This may be due to security restrictions: in Google Chrome the File API only works if this html page is on a web server. Accessing this page from your hard drive does not work.");
|
if(document.location.toString().match(/^file\:\//i))
|
||||||
if(gProcessor) gProcessor.clearViewer();
|
{
|
||||||
|
throw new Error("Could not read file. You are using a local copy of OpenJsCad; if you are using Chrome, you need to launch it with the following command line option:\n\n--allow-file-access-from-files\n\notherwise the browser will not have access to uploaded files due to security restrictions.");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new Error("Could not read file.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if(gProcessor)
|
if(gProcessor)
|
||||||
{
|
{
|
||||||
gProcessor.setJsCad(jscadscript);
|
var filename = gCurrentFile.name;
|
||||||
|
filename = filename.replace(/^.*\/([^\/]*)$/, "$1");
|
||||||
|
gProcessor.setJsCad(jscadscript, filename);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -167,11 +167,6 @@ function parseFile()
|
||||||
throw new Error("Failed to read file");
|
throw new Error("Failed to read file");
|
||||||
if(gProcessor) gProcessor.clearViewer();
|
if(gProcessor) gProcessor.clearViewer();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
catch(e)
|
|
||||||
{
|
|
||||||
alert(e.toString());
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
reader.readAsText(gCurrentFile, "UTF-8");
|
reader.readAsText(gCurrentFile, "UTF-8");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue