three.js
three.js copied to clipboard
Feature Request: GCode Exporter
ThreeJS is widely used across a range of web based tools for 3D Printing, CNC, etc. and utilizes the STLLoader / GCodeLoader and STLExporter for a variety of operations.
However, GCode is the necessary file type that 3D Printers actually read, where an exporter would drastically enhance the potential capabilities of these tools. Ultimately allowing for 3D model conversions from a number of other file types, although most commonly from an STL import -> GCODE export.
For a more detailed explanation of G-Code commands... http://marlinfw.org/docs/gcode/G000-G001.html https://reprap.org/wiki/G-code
As a whole, the primary command is "G1" which is simply used to move between coordinates. I have attached a sample G-Code file of a cube for reference. cube.zip
Isn't using using 3MF more future-proof? According to this article:
At the moment, your CAD software usually works with other formats than .stl for a variety of reasons. Most construction software can actually output .stl files though. Your printing software takes the .stl files and slices them, in most cases to G-code format; these sliced files which can have numerous endings, such as .mpt, .mpf .nc and others. The .3mf format will put an end to the variety of files needed for the whole process of construction, modification and manufacturing of 3D models, according to the consortium.
3MF will likely become more widely adopted in the next couple of years, but it's barely a consideration for the average person right now. The most popular 3D Model file sharing sites like Thingiverse and MyMiniFactory have hundreds of thousands of freely available designs, almost all posted as STL files.
You download the STL file you want to print, plug it in the slicer software and it spits out a GCODE file your 3D Printer can use.
Over time I could see the format change and be replaced by something like 3MF, but we're a long way out from that still. Certain software (ex: Cura) and sites (ex: PrusaPrinters) have recently added support, but the actual usage of this hasn't really caught much attention.
So basically you're asking us to write a slicer in js, right? Doesn't sound super easy, but maybe someone would like to give it a go.
As there is already a G-Code Loader for ThreeJS, I was under the impression it would be relatively straight forward to make an exporter as well. I'm sure that is a naive way to look at it, but was hopeful.
I have very limited experience working in 3D space, can only comment in regards to G-Code itself. In a nutshell though, it basically picks a starting vertex at the base of the mesh and moves around the X/Y axis, writing each individual point to file with the "G91 X0.0 Y0.0" move command. When it gets back to the start point, it raises the Z axis by whatever increment is specified (layer height) with "G91 Z0.0" for example, and then repeats until completed.
The only complexities I can really see would be Start/End scripts. Basically a set of various commands inserted in the head/foot of the file that run before and after the print. I would imagine just taking a parameter array of startCommands and endCommands would be sufficient.
Sounds like you are halfway there then. Would you like to give it a go using the loader as reference?
@LetsPrint3D has there been any progress on this since 2019? I'm also very interested in this feature and willing to help out if possible!
We now have three-mesh-bvh
which should make things much easier:
https://gkjohnson.github.io/three-mesh-bvh/example/bundle/clippedEdges.html
Okay thanks @mrdoob I will take a closer look at it over the weekend. It still feels like an enormous task to generate machine usable Gcode from this but I will press onward.
Currently im exporting my scene to an STL or OBJ file and then slicing it with software like Cura or GoSlice. But I want to be able to do it all in the browser for a more seamless user experience. Considering the size of the 3D printing industry im surprised how hard it is proving to find a decent slicing library for the browser.
/cc @gkjohnson
I know nothing of the GCode format but from the loader example it looks like it's just a series of nozzle movements for each 3d printed layer of the object. As @mrdoob mentioned the three-mesh-bvh example can be used to very quickly derive the series of outline edges for each layer and then they can be sorted into coherent sets of connected lines for each layer.
The hard part, though, seems like optimizing the line order for speed, safely generating the paths to move the printhead between points to start / stop printing, and generating an internal structure for hollow models so they don't collapse or are at least solid. For that last point three-mesh-bvh can be used to determine the inside and outside of the model quickly (see the voxelization example) which may help. It would be worth looking into how some existing tools generate the structures for this.
@yomboprime may have some more insight here since they've worked on generating 3d print files previously, as well.
@gkjohnson You're right, a GCode slicer (FDM technology) is way more complex than a resin print slicer (which is what I did on that tweet). There are a lot of parameters involved in generating GCode. Just take a look at the configuration menus of Prusa Slicer or Cura Slicer. For many years new complex algorithms have been developed, like new infill patterns and the recent Arachne technique. They're way beyond my capabilities.
Okay @mrdoob i've taken a closer look at this and three-mesh-bvh definitely looks like an essential tool for anyone wanting to develop Gcode slicing software in JS. I'm also interested in three-bvh-csg since the tool im working on already uses csg, @gkjohnson thanks for these!
@yomboprime your MinceSlicer is essentially what i'm looking for except with FDM support and without the GUI. I have an Ender 5 Pro and I want to be able to export directly from my three.js scene to a Gcode that I can use on it. Thus eliminating the need for intermediary file formats like STL and OBJ as well as other slicing software like Cura.
It's definitely possible, if you start with very simple objects like a small cube or cylinder. Then gradually scale up the complexity and number of settings you can support, but undoubtedly an enormous task with many unforeseen challenges!