I was reading a discussion board one day (sorry, forgot which one) that was talking about slicers and how slicers could slice a model. Then I thought, interesting, I could probably write some code to slice through a model. It doesn't sound that hard! So yeah, that's what started it. Of course, I'm a so-so perl programmer, so don't expect much but I figured it might be fun to try.
So, stop right there! Who does this sort of thing? Yeah, I'm weird, I know it, my wife tells me all the time! Hopefully she means weird in a good way I'd like to hope. ;-P
So, I first wrote code to import a binary STL (it doesn't do ASCII STL yet). So that part gets all of the triangles into a big array. You can see the STL format here: https://en.wikipedia.org/wiki/STL_%28file_format%29
It then iterates through z planes from bottom up by some layer height.
Then it looks at each triangle. If all of the triangle sides are on the current z plane, it just draws the triangle on the current plane buffer. If one triangle point is above the current z and the other two below, it figures out where each of the two triangle side intersects the plane by rise over run and proportion of the one z point to the plane to figure the proportion of x distance to go and y distance to go to find the intersection point. Once it gets the two intersection points of the two lines passing through the plane, it draws a line between those two points. Once it does that for all triangles that pass through the current z height plane, that gives the outline slice of the model at that z height. Cool!
Then, the program prints out that z height planes as a layer ping to the output dir to show that we have at least gotten this far with the program. Then it generates the walls, and infill and the supports.
Anyway, neato, it sort of actually works. When I first started it was just a mental exercise to see if I could actually figure out how to get slices of an object. Now, low and behold, it can actually do it and make good enough gcode to actually allow my 3d printer to print a part! No I probably won't use my own slicer as my daily driver. It's still just a mental exercise but it's always cool when something hard that you've set out to do actually ends up working even if it's not the bestest evar!
I included an STL model because it gives you something to try and because Thingiverse won't let me publish a program without including some model.
If you need to install Perl, Strawberry Perl is what I use - it's free: http://strawberryperl.com/
Further out ambitious goals (it would be neat if...):
- Have it handle multiple colors.
- Have it add wipe towers.
- Have it handle variable layer heights.
- Grow up and be a real boy.
If you like it, give it a "like", and please remember to give me credit if you're using it. And if you're feeling generous give me some sort of tip to support my Diet Dr Pepper habit.
Thanks be to my Savior Jesus for the fun I've had so far!
20260414 Update: I was playing with claude code and gave it this slicer code and made it better. It now outputs gcode and can do supports. (they won't be easy to pull off hey it does supports) I'm not sure how useful this is since most 3d printers these days have their own slicer anyway but it was a fun exercise anyway. I have not actually tried it but I was able to load it into a gcode viewer and it it seems ok.
20260414b Update: Added some optimizations so it sped it up from >7 hours to 1.5 hours and some other stuff.
I'm trying to print the test weird now. Yay! It actually printed on my 3d printer! Neato! Yeah, for those with exquisite taste and a dash of snobbery, you'll notice that there is a bit of stringing and the supports need to have a smaller attachment point to make them easier to remove. Yeah, well, probably so but isn't it glorious it made it this far!? After printing, the supports did come off easier than I expected but could still be better (see pics).
Here's some features it has now:
- STL parsing, binary
- Contour slicing + chaining into polylines
- Multi-wall perimeter generation via polygon offset
- Top/bottom solid layer detection via per-layer bitmap masks
- Grid infill with density control
- Tree-shape supports with 60° overhang detection, truly-new suspended-feature detection, union-mask base with brim, hollow octagon trunks with overlap dedup
- Model brim
- Model XY centering on a configurable bed
- Per-layer PNG previews
- Full Marlin gcode output with proper M82 absolute extrusion, prime line, retract/z-hop, first-layer Z, realistic extrusion math
- Greedy nearest-neighbor path planning with short-travel skip
- Separate print vs infill speed
- Config file support with INI format
Update: Added it to github: https://github.com/swirlingbrain/sb_slicer
As always use at your own risk.
태그
모델 소스
