Hello,
I am a Junior Developer and I am interested to try and make a small app that calculates the volume (cm3) and overall dimensions of an stl file.
I have been trying to navigate the three.js docs, examples and forums but they are all too advanced.
My MVP is to have a use load an stl file and output the volume and dimensions.
Can anyone share an easy way to make this JavaScript?
My thoughts are I would need to load the stl file.
Check if it’s ASCII or Binary
Parse the file
Output the results.
Any guidance would be greatly appreciated.
Thank you
If you want to get the size of your model, you can use THREE.Box3() with its .getSize() method:
var obj = _your_loaded_model;
var size = new THREE.Vector3();
var box = new THREE.Box3().setFromObject( obj );
box.getSize(size);
console.log(size); // dimensions of your model are in the 'size' variable now
Thanks for the help again.
I have been trying to debug but when I try the getSize method I get the following error;
three.min.js:395 Uncaught TypeError: c.updateMatrixWorld is not a function
at Wa.expandByObject (three.min.js:395)
at Wa.setFromObject (three.min.js:392)
Here is my function I also tried the sample code you provided and got the same error;
function getSize(geometry) {
let size = new THREE.Vector3();
let box = new THREE.Box3().setFromObject(geometry);
return box.getSize(size);
}
Okay, another option is to use geometry’s bounding box:
var loader = new THREE.STLLoader();
loader.load(benchy, function(geometry) {
let calculatedStlVolume = Math.round(getVolume(geometry));
// console.log("stl volume is " + getVolume(geometry));
document.querySelector('#stlVolume').textContent = calculatedStlVolume;
let stlSize = getSize(geometry);
// then write it somewhere in your user interface or do something else :)
});
. . .
function getSize(geometry) {
let size = new THREE.Vector3();
geometry.computeBoundingBox();
geometry.boundingBox.getSize( size );
return size;
}
Last question, is it possible to load an stl file or path locally for the user on the client-side?
I am trying to get the stl file to be uploaded locally and then call the parsed data. https://github.com/3daddict/js-stl-parser/tree/v1 <- updated repo
I’ve been trying to implement the File API and FileReader but I have not been able to get it to work.
Here is my current code: https://github.com/3daddict/js-stl-parser/tree/dev
Can you take a look and let me know what multiple things I am doing wrong?