Rendering result of GCode in 3D

I wrote a tool that can read GCode and create a 3D OpenSCAD model of the resulting object you get after machining is finished. You can find it here on Github: pvdbrand/cnc-3d-gcode-viewer.

There’s an example of a hook I designed and used with EstlCam to generate the gcode:

example

You can see details like the inside corners, the holding tabs, how far into negative X and Y you are milling, etc.

The preview function in EstlCam and various online gcode viewers only show you a line for the toolpath. While useful, I always find it hard to see what the actual result will look like. Also, you can export the 3D model to STL and use your favorite CAD program to verify dimensions if you want.

How it works

Basically, the script reads all movement commands from a gcode file (including arcs, which are interpolated to line segments). The script takes the convex hull of the tool model at the start of a movement and at the end position of a movement. Then it takes the union of all movements. This gives you the shape of all the material that has been removed. So the final step is to take the stock model and subtract the removed material from it, leaving you with a model of the material that is left over after milling.

The model of the end mill is currently just a cylinder with a configurable diameter, and the model of the stock is just a box with configurable dimensions. It’s quite easy to model anything you want though.

10 Likes

That is awesome. The cylinder model allows to different radii for the two ends. So it should be easy to do a vbit. A round over may take a bit more resources, and a complicated stl carve may overwhelm a machine, but this is pretty neat.

Yes, V-bits, ball nose, any shape is possible.

I have not tried very large gcode files yet. I would expect OpenSCAD to bog down on large files, but given enough time it might work for large files.

The OpenSCAD preview mode is relatively fast, but zooming and moving around is a bit laggy. When you render it, the final differencing operation is quite slow, takes about a minute for the crown.

This is neat. I recall seeing something similar before: Preview gcode in OpenSCAD

I really wish openscad weren’t so slow. I get the feeling their underlying geometry library doesn’t use the best algorithms. But I assume it’s a ton of work to try to improve.

The speed really limits the ability to do certain things. I tried transforming a model to add a draft angle for casting, using a minkowski operation, and it eventually worked but it would not be feasible for everyday use on arbitrarily complex models.

I had not seen this before (obviously). It’s actually the exact same idea, and a very similar implementation!

The geometry library of OpenSCAD is supposed to be very accurate. It should not have any floating point issues. That’s probably why it’s slow.

Minkowski is notorious indeed. I would like to use it make nice rounded edges on things, but it’s just too slow to use indeed.

I would love to love openscad. But CAD is just too tangible for me. There are some other programming approaches. FreeCAD has an api for designing in python. I have used it, but things like adding things to this face or adding a roundover on this edge is just too easy to do with a mouse, and it would take a long time to do the same thing with text.

Yeah that’s not how designing things in OpenSCAD works. When I design something, I never think about edges or faces.

I usually start with roughly building out the outside of the object. For example, a simple box for the hook in the picture above. Then I subtract stuff that shouldn’t be there. For example, “I need a hole here, so let’s subtract a cylinder at these coordinates”. I don’t care to which faces the hole is aligned, I just make the cylinder much larger than the part itself. I use absolute coordinates for most things.

I must say that I quite dislike the OpenSCAD language. I built my own wrapper in Haskell. Haskell is a fairly complicated language, but this is really a niche where it works absolutely beautifully.

Also, I actually never really got the hang of using CAD properly. So that may be part of the reason why I prefer OpenSCAD :slight_smile:

Cool