Now uses FileSystem API for downloading the STL file

This commit is contained in:
Joost Nieuwenhuijse 2012-02-13 18:19:37 +01:00
parent 3b4e1fd1a6
commit 8b51bd6b68
3 changed files with 161 additions and 54 deletions

View file

@ -8,7 +8,7 @@
body {
font: 14px/20px 'Helvetica Neue Light', HelveticaNeue-Light, 'Helvetica Neue', Helvetica, Arial, sans-serif;
max-width: 800px;
max-width: 820px;
margin: 0 auto;
padding: 10px;
}
@ -36,8 +36,12 @@ canvas { cursor: move; }
</style>
<script>
var gProcessor=null;
// Show all exceptions to the user:
OpenJsCad.AlertUserOfUncaughtExceptions();
function onload()
{
gProcessor = new OpenJsCad.Processor(document.getElementById("viewer"));

View file

@ -329,6 +329,41 @@ OpenJsCad.Processor = function(containerdiv, onchange) {
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 = {
createElements: function() {
while(this.containerdiv.children.length > 0)
@ -419,12 +454,17 @@ OpenJsCad.Processor.prototype = {
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.clearViewer();
this.setError("");
this.processing = true;
this.statusspan.innerHTML = "Processing, please wait...";
this.filename = filename;
var that = this;
this.worker = OpenJsCad.javaScriptToSolidASync(script, function(err, csg) {
that.processing = false;
@ -456,7 +496,7 @@ OpenJsCad.Processor.prototype = {
return this.processing;
},
clearStl: function() {
clearStl1: function() {
if(this.hasstl)
{
this.hasstl = false;
@ -467,7 +507,7 @@ OpenJsCad.Processor.prototype = {
}
},
generateStl: function() {
generateStl1: function() {
this.clearStl();
if(this.validcsg)
{
@ -479,4 +519,72 @@ OpenJsCad.Processor.prototype = {
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");}
);
}
},
};

View file

@ -8,6 +8,7 @@
body {
font: 14px/20px 'Helvetica Neue Light', HelveticaNeue-Light, 'Helvetica Neue', Helvetica, Arial, sans-serif;
max-width: 820px;
}
pre, code, textarea {
@ -55,19 +56,17 @@ canvas { cursor: move; }
</style>
<script>
var gCurrentFile = null;
var gCurrentFile = null;
var gProcessor=null;
// Show all exceptions to the user:
OpenJsCad.AlertUserOfUncaughtExceptions();
function onload()
{
try
{
gProcessor = new OpenJsCad.Processor(document.getElementById("viewer"));
setupDragDrop();
} catch (e) {
alert(e.toString());
}
gProcessor = new OpenJsCad.Processor(document.getElementById("viewer"));
setupDragDrop();
}
function setupDragDrop()
@ -76,7 +75,7 @@ function setupDragDrop()
if (window.File && window.FileReader && window.FileList) {
// Great success! All the File APIs are supported.
} 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');
dropZone.addEventListener('dragover', function(evt) {
@ -89,32 +88,27 @@ function setupDragDrop()
function handleFileSelect(evt)
{
try
evt.stopPropagation();
evt.preventDefault();
if(!evt.dataTransfer) throw new Error("Not a datatransfer (1)");
if(!evt.dataTransfer.files) throw new Error("Not a datatransfer (2)");
if(evt.dataTransfer.files.length != 1)
{
evt.stopPropagation();
evt.preventDefault();
if(!evt.dataTransfer) throw new Error("Not a datatransfer (1)");
if(!evt.dataTransfer.files) throw new Error("Not a datatransfer (2)");
if(evt.dataTransfer.files.length != 1)
{
throw new Error("Please drop a single .jscad file");
}
var file = evt.dataTransfer.files[0];
if(!file.name.match(/\.jscad$/i))
{
throw new Error("Please drop a file with .jscad extension");
}
if(file.size == 0)
{
throw new Error("You have dropped an empty file");
}
gCurrentFile = file;
gPreviousModificationTime = "";
fileChanged();
} catch (e) {
alert(e.toString());
throw new Error("Please drop a single .jscad file");
}
var file = evt.dataTransfer.files[0];
if(!file.name.match(/\.jscad$/i))
{
throw new Error("Please drop a file with .jscad extension");
}
if(file.size == 0)
{
throw new Error("You have dropped an empty file");
}
gCurrentFile = file;
gPreviousModificationTime = "";
fileChanged();
}
function fileChanged()
@ -144,33 +138,34 @@ function parseFile()
var txt = evt.target.result;
};
reader.onloadend = function(evt) {
try
if (evt.target.readyState == FileReader.DONE)
{
if (evt.target.readyState == FileReader.DONE)
var jscadscript = evt.target.result;
if(jscadscript == "")
{
var jscadscript = evt.target.result;
if(jscadscript == "")
if(document.location.toString().match(/^file\:\//i))
{
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(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
{
if(gProcessor)
{
gProcessor.setJsCad(jscadscript);
}
}
{
throw new Error("Could not read file.");
}
}
else
{
throw new Error("Failed to read file");
if(gProcessor) gProcessor.clearViewer();
{
if(gProcessor)
{
var filename = gCurrentFile.name;
filename = filename.replace(/^.*\/([^\/]*)$/, "$1");
gProcessor.setJsCad(jscadscript, filename);
}
}
}
catch(e)
else
{
alert(e.toString());
throw new Error("Failed to read file");
if(gProcessor) gProcessor.clearViewer();
}
};
reader.readAsText(gCurrentFile, "UTF-8");