Skip to content

Commit 163e667

Browse files
committed
load printer models & options from json
1 parent 8eeb9b2 commit 163e667

File tree

4 files changed

+69
-49
lines changed

4 files changed

+69
-49
lines changed

Print-Cost-Calculation.xlsx

10.8 KB
Binary file not shown.

index.html

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111
<meta name="robots" content="index">
1212
<meta http-equiv="expires" content="43200">
1313

14+
<link id="favicon" rel="icon" type="image/png" href="assets/logo.png">
15+
<link rel="shortcut icon" type="image/png" href="assets/logo.png">
16+
<link rel="apple-touch-icon" href="assets/logo.png">
17+
<link rel="apple-touch-icon" sizes="152x152" href="assets/logo.png">
18+
<link rel="apple-touch-icon" sizes="180x180" href="assets/logo.png">
19+
<link rel="apple-touch-icon" sizes="167x167" href="assets/logo.png">
20+
1421
<meta property="og:title" content="3D Print Cost Calculator">
1522
<meta property="og:description" content="3D Print Cost Calculator">
1623
<meta property="og:url" content="https://3d.michivonah.ch/">
@@ -48,11 +55,6 @@ <h1>3D Print<br>Cost Calculator</h1>
4855
<label for="input-printer-model">Printer Model</label>
4956
<select name="input-printer-model" id="input-printer-model" class="input-field">
5057
<option value="other" selected disabled>Select printer model</option>
51-
<option value="bambulab-a1">BambuLab A1</option>
52-
<option value="bambulab-a1-combo">BambuLab A1 Combo</option>
53-
<option value="bambulab-p1s">BambuLab P1S</option>
54-
<option value="bambulab-p1s-combo">BambuLab P1S Combo</option>
55-
<option value="other">Other</option>
5658
</select>
5759
</div>
5860

main.js

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,38 @@
11
// add auto calculate on update
2-
document.addEventListener('DOMContentLoaded', function(){
2+
document.addEventListener('DOMContentLoaded', async function(){
33
let fields = document.getElementsByClassName('input-field');
44
for (const field of fields){
55
field.addEventListener('change', showResult);
66
}
77

88
document.getElementById("input-total-cost").addEventListener('click', copyToClipboard);
9+
10+
await loadPrinterModels();
911
});
1012

11-
// calculate cost
12-
function calculateCost(){
13-
const bufferFactor = 1.1; // add buffer on top of filament usage
14-
const filamentPrice = 20; // price per 1kg spool
15-
const hourlyMachineCost = 0.15; // the machine cost per hour (used for maintenance/repair/energy)
13+
// load settings from json
14+
async function loadSettings(path = "options.json"){
15+
try{
16+
const response = await fetch(path);
17+
if (!response.ok) {
18+
throw new Error('Network response was not ok');
19+
}
20+
const settings = await response.json();
21+
return settings;
22+
}
23+
catch(error){
24+
console.log(`Got error while trying to load the configuration data: ${error}`);
25+
return {"message":"error"};
26+
}
27+
}
1628

17-
const printerModel = document.getElementById("input-printer-model").value;
29+
// calculate cost
30+
async function calculateCost(){
31+
const settings = await loadSettings();
32+
const bufferFactor = settings.filamentBufferFactor; // add buffer on top of filament usage
33+
const filamentPrice = settings.filamentPrices["PLA"].spoolPrice; // price per 1kg spool
34+
const printerModel = document.getElementById("input-printer-model").value || other; // the selected printer model
35+
const hourlyMachineCost = settings.printerModels[printerModel].hourlyMachineCost; // the machine cost per hour (used for maintenance/repair/energy)
1836

1937
const filamentUsage = parseFloat(document.getElementById("input-total-filament-usage").value || 0); // used filament in grams
2038
const printDuration = parseFloat(document.getElementById("input-print-duration").value || 0); // print duration in minutes
@@ -31,12 +49,27 @@ function calculateCost(){
3149
return round;
3250
}
3351

52+
// load printers into select field
53+
async function loadPrinterModels(){
54+
const settings = await loadSettings();
55+
const printerModels = settings.printerModels;
56+
const printerSelect = document.getElementById('input-printer-model');
57+
58+
for (const [id, model] of Object.entries(printerModels)){
59+
const option = document.createElement('option');
60+
option.value = id;
61+
option.textContent = model.name;
62+
printerSelect.appendChild(option);
63+
}
64+
}
65+
3466
// show cost from calculation
35-
function showResult(){
36-
const currencyIcon = "CHF"; // the icon of your currency
67+
async function showResult(){
68+
const settings = await loadSettings();
69+
const currencyIcon = settings.currencyIcon; // the icon of your currency
3770

3871
let resultField = document.getElementById("input-total-cost");
39-
resultField.value = `${calculateCost()} ${currencyIcon}`;
72+
resultField.value = `${await calculateCost()} ${currencyIcon}`;
4073
}
4174

4275
// Copy to clipboard

options.json

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,98 +3,83 @@
33
"machineCostBufferFactor":1.3,
44
"energyPriceKwh":32.66,
55
"currencyIcon":"CHF",
6-
"filamentPrices":[
7-
{
8-
"type":"PLA",
6+
"filamentPrices":{
7+
"PLA":{
98
"spoolPrice":20.00
109
},
11-
{
12-
"type":"PETG",
10+
"PETG":{
1311
"spoolPrice":20.00
1412
},
15-
{
16-
"type":"ABS",
13+
"ABS":{
1714
"spoolPrice":22.00
1815
},
19-
{
20-
"type":"PC",
16+
"PC":{
2117
"spoolPrice":35.00
2218
},
23-
{
24-
"type":"TPU",
19+
"TPU":{
2520
"spoolPrice":36.00
2621
}
27-
],
28-
"printerModels":[
29-
{
30-
"id":"bambulab-a1-mini",
22+
},
23+
"printerModels":{
24+
"bambulab-a1-mini":{
3125
"name":"BambuLab A1 Mini",
3226
"brand":"BambuLab",
3327
"price":270,
3428
"hourlyMachineCost":0.029
3529
},
36-
{
37-
"id":"bambulab-a1-mini-combo",
30+
"bambulab-a1-mini-combo":{
3831
"name":"BambuLab A1 Mini Combo",
3932
"brand":"BambuLab",
4033
"price":410,
4134
"hourlyMachineCost":0.044
4235
},
43-
{
44-
"id":"bambulab-a1",
36+
"bambulab-a1":{
4537
"name":"BambuLab A1",
4638
"brand":"BambuLab",
4739
"price":360,
4840
"hourlyMachineCost":0.038
4941
},
50-
{
51-
"id":"bambulab-a1-combo",
42+
"bambulab-a1-combo":{
5243
"name":"BambuLab A1 Combo",
5344
"brand":"BambuLab",
5445
"price":500,
5546
"hourlyMachineCost":0.053
5647
},
57-
{
58-
"id":"bambulab-p1p",
48+
"bambulab-p1p":{
5949
"name":"BambuLab P1P",
6050
"brand":"BambuLab",
6151
"price":660,
6252
"hourlyMachineCost":0.070
6353
},
64-
{
65-
"id":"bambulab-p1s",
54+
"bambulab-p1s":{
6655
"name":"BambuLab P1S",
6756
"brand":"BambuLab",
6857
"price":630,
6958
"hourlyMachineCost":0.067
7059
},
71-
{
72-
"id":"bambulab-p1s-combo",
60+
"bambulab-p1s-combo":{
7361
"name":"BambuLab P1S Combo",
7462
"brand":"BambuLab",
7563
"price":840,
7664
"hourlyMachineCost":0.089
7765
},
78-
{
79-
"id":"bambulab-x1c",
66+
"bambulab-x1c":{
8067
"name":"BambuLab X1C",
8168
"brand":"BambuLab",
8269
"price":1130,
8370
"hourlyMachineCost":0.120
8471
},
85-
{
86-
"id":"bambulab-x1c-combo",
72+
"bambulab-x1c-combo":{
8773
"name":"BambuLab X1C Combo",
8874
"brand":"BambuLab",
8975
"price":1370,
9076
"hourlyMachineCost":0.146
9177
},
92-
{
93-
"id":"other",
78+
"other":{
9479
"name":"Other",
9580
"brand":"Other",
9681
"price":1000,
9782
"hourlyMachineCost":0.15
9883
}
99-
]
84+
}
10085
}

0 commit comments

Comments
 (0)