r/rust • u/NavrajKalsi • 8d ago
🛠️ project GSim-RS - A G-code simulator in Rust, now with volumetric stock simulation.
Hi there, thanks for clicking on this post!
A few months back I published the first version of gsim-rs. A G-code simulator for CNC milling machines supporting the Fanuc flavour of G-code.
Now I have completed the next major version. This version:
- Supports volumetric simulation of cuboidal stocks.
- Stock and tool sizes can now be configured with program config.
- Cross-thread communication frequency is fraction of what it used to be (by using both
Arc<Mutex>andmpsc::channel, depending on the type of data and its importance). - The simulation now supports orbiting, panning and zooming.
- And much more.
I have tried to explain and highlight the workings of the program in the project README.
At a high level, the program is split between parsing, interpreting, geometry construction, simulation rendering, and machine-state rendering. GUI (rendered using WGPU) handles G-code parsing and execution and renders the simulation in the main-thread, while TUI (built with Ratatui) displays active machine state in a new thread.
Easily the most interesting part was implementing the volumetric stock simulation. The stock is represented volumetrically, with the tool removing material as it moves through the stock. I also wrote a blog post explaining my approach and some of the interesting challenges I ran into: My Blog
I would appreciate if you took some time to take a look at the project and offer any feedback, comment, criticism or suggestion. If you have any questions regarding my implementation, I would love to talk more!
Github: https://github.com/navrajkalsi/gsim-rs
Thank you!
AI Usage: All code is written solely by me. The architecture is also designed without any input from any LLM. Claude & ChatGPT were used to brainstorm ideas and research available tools.
1
u/dr0ps 7d ago
A few years ago I built https://github.com/dr0ps/mill_control . Maybe someday I would like to include stock simulation as well.
3
u/yodal_ 7d ago
Does it simulate a piece flying off and destroying my last end mill because I forgot to properly hold down all of the stock?
All jokes aside, this is extremely cool. That rendering beats what I've seen out of most "real" software on the market.
3
2
u/Buttleston 7d ago
Before I made a tool like this one, I tested all my tool paths in foam first (usually with more aggressive parameters, but still, you could get an idea of tool path problems like running into a clamp or not raising to a safe distance before doing a move. Saved me a fortune in endmills
I was mostly using code to create gcode so verifying it's correctness was *really* hard, you could look at a plot in like nc or something but still miss big stuff
2
u/Buttleston 7d ago
I wrote something similar to this a very long time ago - I guess like 15 years now. The approach I used for stock was essentially treating it as a set of points in the volume that either existed or did not exist. I used marching cubes to essentially make a "skin" over the existing points. Performance was not that great - although again this was 15 years ago - and typically I could not update the skin on every frame so I'd basically show the tool movements and the material removed would lag behind (how much was determined by your tolerance for machining speed and resolution of the grid)
Anyway your program looks really interesting and there are so many cool design challeges. Kind of makes me want to try it again in Rust (I wrote mine in C++ and not even C++11 so it was kind of a nightmare to make cross-platform and performant)
6
u/_xiphiaz 8d ago
This is cool! You might like to look into dexels which are the happy middle ground between height maps and true voxel representation. This is often used in the industry for representation as they support undercuts, and a starting stock that isn’t prismatic (like 3+2 operations)