How to get STL as bytes from a cadquery.Workplane without writing them to a file?

234 views
Skip to first unread message

Jaakko

unread,
Dec 18, 2021, 12:22:18 PM12/18/21
to CadQuery
Hello all!
I am making a web application (with Svelte front and Django back) and I am trying to implement following functionality:
User inputs measurements, client sends them to the server and server runs cadquery script and returns an STL file to the client where it can be previewed in browser and saved to users computer.

Does anyone know how can generate the STL file bytes from the cadquery Workplane without writing them to a file?


Thank you in advance
Jaakko

Jaakko

unread,
Dec 19, 2021, 6:17:52 AM12/19/21
to CadQuery
Also if anyone has any ideas or suggestions of an efficient way of transmitting the 3D data from cadquery running in the server to the client I would be interested to hear!

Lorenz

unread,
Dec 19, 2021, 11:05:34 AM12/19/21
to CadQuery
Hi Jaako,


> how can generate the STL file bytes from the cadquery Workplane without writing them to a file?

Export with exportType STL uses OCP.StlAPI.StlAPI_Writer.  The StlAPI_Writer expects a string argument for filename so I don't see an easy way to avoid this without writing a temp file.

The AMF exportType also currently writes to file but with some minor Python code changes to the AmfWriter you could support writing to IO stream.

Regarding display in the browser, I would suggest to take a look at jupyter_tools.py.  Disclaimer - I have not build such an app.  My advice is based on using cadquery with jupyter lab.  As a jupyter lab user I know that cadquery natively supports display in the browser.  I think the HTML docs use the same (VTK) method for display in the browser as well (see cq_directive.py).


Lorenz

Roger Maitland

unread,
Dec 19, 2021, 3:31:47 PM12/19/21
to Lorenz, CadQuery
Hi Jaakko,

CadHub sounds very similar to what you're trying to do - you might want to look at their code to see how they do it: https://github.com/Irev-Dev/cadhub

Cheers,
Roger

--
cadquery home: https://github.com/CadQuery/cadquery
post issues at https://github.com/CadQuery/cadquery/issues
run it at home at : https://github.com/CadQuery/CQ-editor
---
You received this message because you are subscribed to the Google Groups "CadQuery" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cadquery+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cadquery/f1b169f3-b315-4f73-8f21-d90fc2f10f84n%40googlegroups.com.

Jaakko

unread,
Dec 22, 2021, 12:57:37 PM12/22/21
to CadQuery
Thank you for the advice,

I ended up just writing the stl to temporary file.
I used StlAPI().Write_s(shape, filename, False) method for writing the stl because it writes the STL in binary format
and used
tempfile.NamedTemporaryFile() to create the temporary file.
In the front end I use ThreeJS library for displaying the 3D-model.

Jeremy Wright

unread,
Dec 27, 2021, 2:37:03 PM12/27/21
to CadQuery
The CadHub integration of CadQuery uses cq-cli, which allows the output of the STL text to the stdout stream.


So that might be worth a look.
Reply all
Reply to author
Forward
0 new messages