I am trying to create a node app in which I need to calculate the volume of the stl file which is uploaded by the user. I am using express and multer to handle the file uploading process. I am using three.js to calculate the volume of the stl file. However, when I add the volume code inside app.get(), it doesn’t work. I have tried the code on plain js and it works perfectly and outputs the volume. I am not sure if the code gets executed when running on node as nothing gets printed in the terminal console. I need this code to work on the server side as I don’t want the client to see this code and manipulate it. Please help me with this. Here is my current code:
app.get(’/’, function (req, res) {
console.log(“Hello!”); //This line gets printed
res.render(‘pages/index’);
});
app.post(’/upload’, upload.single(‘stl_file’), (req, res) => { //stl_file is the name attribute of input tag in html
var loader = new STLLoader();
loader.load(req.file, function (geometry) { Code to calculate volume
console.log("stl volume is " + volume); //This line does not get printed Code to calculate size
let stlSize = size;
console.log(stlSize); //This line does not get printed
});
return res.json({ status: ‘OK’});
});
@drcmda thanks for the answer. Could me help me with the code? I am new to three.js and still cannot figure out what exactly needs to be done. Thank you.
i did, the code above parses an existing arraybuffer. Loader().load fetches a file first via xhrrequest and then parses. so by using parse you avoid the fetching. you can load a file as an arraybuffer using the node api.
I am currently trying to parse the file using this code but am getting a error:
"RangeError: Offset is outside the bounds of the DataView"
var enc = new TextEncoder();
var arrayBuffer = enc.encode(req.file).buffer;
console.log(arrayBuffer);
var loader = new STLLoader();
loader.parse(arrayBuffer, function (geometry) {
Code to calculate volume
console.log("stl volume is " + volume); //This line does not get printed
Code to calculate size
let stlSize = size;
console.log(stlSize); //This line does not get printed
});
return res.json({ status: ‘OK’});
});
});
I was working on an Electron app with React that loads STL files from a local drive.
In Electron’s Main thread I’ve loaded files like this:
...
data = fs.readFileSync('/Users/luka/Downloads/cube.stl', 'ascii');
...
Then through IPC (two-way communication between Main and Renderer threads), I’ve returned data to Renderer thread and used STLLoader’s parse() method to load the data into the scene like this:
you can also integrate it into suspense, which would allow that component to interop with everything else, and the eco system. this is what all r3f loaders do, they all use suspend-react.
for instance, if you pair it with any interop component, like drei/center, it needs to know when loading has finished. it executes bounds calculation in useLayoutEffect. without suspense uLE will fire immediately after render, but now it will fire once your STL is there:
<Center>
<Model loadStl={foo} />
</Center>
you can also re-use the model now because the data is cached.