forked from nraynaud/webgcode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
create.js
114 lines (98 loc) · 2.95 KB
/
create.js
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
make_element = function(tag, opts={}){
var el = document.createElement(tag)
if (opts["class"]) {el.setAttribute("class", opts["class"])}
if (opts["id"]) {el.setAttribute("id", opts["id"])}
if (opts["href"]) {el.setAttribute("href", opts["href"])}
if (opts["text"]) {el.textContent = opts["text"]}
return el}
QueryString = function () {
var query_string = {}
var query = window.location.search.substring(1)
var vars = query.split("&")
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=")
if (typeof query_string[pair[0]] === "undefined") {
query_string[pair[0]] = decodeURIComponent(pair[1])
} else if (typeof query_string[pair[0]] === "string") {
var arr = [ query_string[pair[0]],decodeURIComponent(pair[1]) ];
query_string[pair[0]] = arr
} else {
query_string[pair[0]].push(decodeURIComponent(pair[1]))}}
return query_string}
get_file = function(url, cb){
var xmlhttp = new XMLHttpRequest()
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var res = this.response
cb(res)}}
xmlhttp.addEventListener("progress", function(e){
if (e.lengthComputable) {
var n = e.loaded / e.total;
}
})
xmlhttp.open("GET", url, true)
xmlhttp.responseType = "arraybuffer";
xmlhttp.send()}
unzip = function(blob, cb){
zip.createReader(new zip.BlobReader(blob), function(reader) {
reader.getEntries(function(entries) {
if (entries.length) {
entries[0].getData(new zip.BlobWriter(), function(blob) {
cb(blob)
reader.close(function() {
})
}, function(current, total) {
})
}
})
}, function(error) {
console.warn(error)
})
}
lines = function(s){
return s.split(/\s*[\r\n]+\s*/g);
}
SETTINGS = {
absoluteExtrusion:false,
feedrateMultiplyer:100,
filamentDiameter:1.75,
firmwareRetractLength:2,
firmwareRetractSpeed:50,
firmwareRetractZhop:0,
firmwareUnretractLength:2,
firmwareUnretractSpeed:50,
maxJerk:[10, 10, 1, 10],
maxPrintAcceleration: [1000, 1000, 100, 10000],
maxSpeed: [100, 100, 10, 100],
maxTravelAcceleration: [1000, 1000, 100, 10000],
timeScale: 1.01}
plot = function(data){
gcodeLines = lines(data)
app.set('code', data)
app.launchSimulation()
gcodeProcessorWorker.postMessage([gcodeLines, SETTINGS]);
}
window.onload = function(e){
get_file(QueryString()["gcode"], function(res){
var blob = new Blob([res], { type: "application/zip" })
unzip(blob, function(res){
var reader = new FileReader();
reader.onload = function() {
var res = reader.result
plot(res)
}
reader.readAsBinaryString(res);
})
})
}
setview3d = function(e){
document.body.classList.remove("v2d")
}
setview2d = function(e){
document.body.classList.add("v2d")
}
analysis = function(data){
console.log(data)
var grams = parseFloat(data.filamentUsage) * 2.4 * 1.23
document.querySelector("#filament-usage").innerText = grams.toFixed(1) + " grams"
}