-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfixinator.htm
215 lines (191 loc) · 8.95 KB
/
fixinator.htm
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>GCODE Fixinator</title>
<script>
window.addEventListener('load', function () {
let fileArray;
let newFile;
document.getElementById('file').onchange = function(){
let file = this.files[0];
let reader = new FileReader();
reader.onload = function(progressEvent){
fileArray = this.result.split(/\r\n|\n/);
};
reader.readAsText(file);
};
/* clear warnings */
function clearErrorMessages() {
// warning messages
document.getElementById("fileWarning").classList.add("hidden");
document.getElementById("layerWarning").classList.add("hidden")
document.getElementById("layerHeightWarning").classList.add("hidden")
// boxes highlighted
document.getElementById("thickness").classList.remove("invalid");
document.getElementById("layer").classList.remove("invalid");
document.getElementById("height").classList.remove("invalid");
return;
}
function fixinate() {
// check for a missing file
if ( fileArray !==undefined && fileArray.length > 0) {
document.getElementById("fileWarning").classList.add("hidden");
} else {
document.getElementById("fileWarning").classList.remove("hidden");
return;
}
// Get height and/or layer height and thickness
z_height = parseFloat(document.getElementById("height").value);
num_layers = parseInt(document.getElementById("layer").value);
thickness = parseFloat(document.getElementById("thickness").value);
// Default to using number of layers and thickness
if (num_layers && thickness) {
if ( !(num_layers > 0)) { // check invalid and for null
document.getElementById("layer").classList.add("invalid");
document.getElementById("layerWarning").classList.remove("hidden")
z_height = null;
} else if ( !(thickness > 0)) {
document.getElementById("thickness").classList.add("invalid");
z_height = null;
// valid num_layers and valid thickness
} else {
clearErrorMessages();
z_height = thickness * num_layers;
}
// else use check the user entered z height
} else if ( !(z_height >= 0)) {
document.getElementById("height").classList.add("invalid");
return; // exit if there is no valid input
}
if (z_height >= 0) {
new_file = [];
let still_searching = true;
// read file
fileArray.forEach(line => {
// skip comments
if (still_searching) {
if (line.startsWith(';')) { // keep comments
new_file.push(line);
} else if (line.startsWith("G28")) { //DON'T let it home Z!
new_file.push("G28 X Y\n");
} else if (line.startsWith( "G204" )) { // remove acceleration change commands.They clutter the code.
return;
} else if (line.startsWith("G1") || line.startsWith("G0") || line.startsWith("G3") || line.startsWith( "G2" ) ) {
// Regex to capture just the Zxx.x height. Index 0 will be the whole thing (Z15.1),
// 1 will be just the height (15.1). The height is also named, "height".
const height_regex = /Z(?<height>\d+\.?\d*)/g;
result = height_regex.exec(line.toString());
if (!result || !result.groups.height) {
return;
}
height = parseFloat(result.groups.height);
console.log(height);
if (height) {
if (height >= z_height){
console.log("Found height to stop cutting at! That line was:");
console.log(line);
set_height = "G92 Z" + height + "\n";
console.log("Adding a line to tell the extruder that it's at height", set_height);
new_file.push(set_height);
new_file.push(line);
//from here on out, we want to save everything.
still_searching = false;
} else { // delete all move commands before Z height
return;
}
} else {
return; //was a move, but no Z...
}
} else { //default to writing the line. Heating up, status, ...
new_file.push(line);
}
}
else { //just plain copy everything once the z height has been matched
new_file.push(line);
}
});
newFile = new_file.join("\n");
} else {
// don't show the download button if the file isn't valid.
document.getElementById("downloadBtn").classList.add("hidden");
return;
}
document.getElementById("downloadBtn").classList.remove("hidden");
document.getElementById("downloadBtn").addEventListener("click", downloadFile, false);
}
document.getElementById("fixinateBtn").addEventListener("click", fixinate, false);
function downloadFile() {
filename = "fixinated_file.gcode";
type = "gcode";
var file = new Blob([newFile], {type: type});
var a = document.createElement("a"),
url = URL.createObjectURL(file);
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
setTimeout(function() {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 0);
}
})
</script>
<style>
section>* {
display: block;
margin-bottom: 15px;
}
section>h1 {
font-size: 2em;
}
section>div {
padding: 15px 20px 15px 0;
border-radius: 5px;
border: 1px solid black;
}
section input, section label, button {
margin-left: 5px;
}
label {
display: block;
margin-top: 8px;
margin-bottom: 0px;
}
.hidden {
display: none;
}
.invalid {
border-color: red;
}
.warning {
color: rgb(153, 15, 15);
}
</style>
</head>
<body>
<section>
<h1>GCODE Fixinator</h1>
<p id="fileWarning" class="hidden warning">Please choose a valid file 😊</p>
<p id="layerWarning" class="hidden warning">Please enter a valid layer number 😊</p>
<p id="layerHeightWarning" class="hidden warning">Please enter a valid layer height 😊</p>
<div>
<label for="height">Height (in mm)</label>
<input type="number" id="height"><br>
</div>
<div>
<label for="layer">Last Printed Layer</label>
<input type="number" id="layer"><br>
<label for="thickness">Layer Thickness (in mm)</label>
<input type="number" id="thickness"><br>
</div>
<label for="file">GCODE File</label>
<input type="file" id="file" accept=".gcode, .gco, .g, .ufp"><br>
<button id="fixinateBtn">Fixinate</button><br>
<button id="downloadBtn" class="hidden">Download</button>
</section>
</body>
</html>