f-gcode fixer
by kthornbloom
HTML
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
<div class="main">
<div class="main-left">
<h2>
Original Code
</h2>
<textarea name="" id="usergcode" cols="30" rows="10">
(1001)
(T2 D=0.58 CR=0 - ZMIN=-1.8 - flat end mill)
G90 G94
G17
G21
(When using Fusion 360 for Personal Use, the feedrate of)
(rapid moves is reduced to match the feedrate of cutting)
(moves, which can increase machining time. Unrestricted rapid)
(moves are available with a Fusion 360 Subscription.)
G28 G91 Z0
G90
(Trace1)
T2
S5000 M3
G54
G0 X32.5 Y-22.787
Z15
G1 Z5 F1000
Z-0.05 F333.3
X-32.5 F1000
Z5
X32.5 Y-44.295
Z-0.05 F333.3
X-32.5 F1000
Z5
Y-22.787
Z-0.3 F333.3
X32.5 F1000
Z5
X-32.5 Y-44.295
Z-0.3 F333.3
X32.5 F1000
Z5
Y-22.787
Z-0.55 F333.3
X-32.5 F1000
Z5
X32.5 Y-44.295
Z-0.55 F333.3
X-32.5 F1000
Z5
Y-22.787
Z-0.8 F333.3
X32.5 F1000
Z5
X-32.5 Y-44.295
Z-0.8 F333.3
X32.5 F1000
Z5
Y-22.787
Z-1.05 F333.3
X-32.5 F1000
Z5
X32.5 Y-44.295
Z-1.05 F333.3
X-32.5 F1000
Z5
Y-22.787
Z-1.3 F333.3
X32.5 F1000
Z5
X-32.5 Y-44.295
Z-1.3 F333.3
X32.5 F1000
Z5
Y-22.787
Z-1.55 F333.3
X-32.5 F1000
Z5
X32.5 Y-44.295
Z-1.55 F333.3
X-32.5 F1000
Z5
Y-22.787
Z-1.8 F333.3
X32.5 F1000
Z5
X-32.5 Y-44.295
Z-1.8 F333.3
X32.5 F1000
Z5
Z15
G28 G91 Z0
G90
G28 G91 X0 Y0
G90
M5
M30
</textarea>
<h2>
<a href="#" id="copy" class="btn btn-light btn-sm">Copy</a>
Modified Code
</h2>
<textarea name="" id="modifiedgcode" cols="30" rows="10" placeholder="Modified code will be generated here"></textarea>
</div>
<div class="main-right">
<h1>
F-gcode Fixer
</h1>
<p>
</p>
<form>
<div class="mb-3 form-check">
<input type="checkbox" class="form-check-input" id="restorerapids" checked>
<label class="form-check-label" for="restorerapids">Restore Rapids</label>
</div>
<div class="mb-3 form-check">
<input type="checkbox" class="form-check-input" id="starthoming" checked>
<label class="form-check-label" for="starthoming">Remove Homing...
SCSS
* {
box-sizing: border-box;
}
.main {
display: flex;
min-height: 100vh;
}
.main-left {
display: flex;
flex-direction: column;
width: 70%;
}
.main h2 {
background: #39689d;
color: #c8d6ff;
font-size: 18px;
padding: .825em;
margin-bottom: 0;
position: relative;
}
.main h2:last-of-type {
background: #399d7d;
color: #daffff;
}
.main-right {
padding: 1.5em;
width: 30%;
}
#usergcode,
#modifiedgcode {
background: #3e444c;
background-size: 100% 32px;
border: none;
color: #fff;
font-family: monospace, sans-serif;
padding: 1em;
height: calc(50% - 51px);
width: 100%;
white-space: pre;
}
#copy {
position: absolute;
right: 10px;
top: 50%;
transform: translateY(-50%);
}
JavaScript
const processBtn = document.getElementById('process-btn');
var filteredArray = '';
// Submit Form
processBtn.addEventListener('click', function(e){
e.preventDefault();
const originalTextarea = document.getElementById('usergcode'),
modifiedTextarea = document.getElementById('modifiedgcode'),
opt1 = document.getElementById('restorerapids'),
opt2 = document.getElementById('starthoming'),
opt3 = document.getElementById('endhoming'),
opt4 = document.getElementById('zfirst'),
opt5 = parseInt(document.getElementById('clearanceHeight').value),
opt6 = parseInt(document.getElementById('retractHeight').value),
zMax = opt5 + opt6,
original = originalTextarea.value;
var originalAsArray = original.split(/\r?\n/),
restoreRapids = false,
removeStarthome = false,
removeEndhome = false,
moveZfirst = false,
retractHeight = opt5.value;
// check settings
if(opt1.checked){restoreRapids = true}
if(opt2.checked){removeStarthome = true}
if(opt3.checked){removeEndhome = true}
if(opt4.checked){moveZfirst = true}
if(removeStarthome){
// kill first G28 G91 Z0
var filteredArray = originalAsArray.filter(e => e !== 'G28 G91 Z0');
}
if(removeEndhome){
// kill everything after the last lift to Z15 or whatev
let endHomeindex = filteredArray.findLastIndex((element) => element == 'Z'+zMax);
if(endHomeindex && zMax){
filteredArray.splice(endHomeindex + 1);
} else {
alert("End home not found, or heights missing");
}
}
if(moveZfirst){
// Move the first Z15 to before the first X/Y move
let firstZ = filteredArray.findIndex((element) => element == 'Z'+zMax);
if(firstZ){
filteredArray[firstZ] = 'G0 '+filteredArray[firstZ];
var element = filteredArray[firstZ];
filteredArray.splice(firstZ, 1);
filteredArray.splice((parseInt(firstZ - 1)), 0, element);
} else {
alert("No initial Z...