Triangular is an easy-to-use Ruby library for reading, writing and manipulating Stereolithography (STL) files.
The main purpose of Triangular is to enable its users to quickly create new software for Rapid Prototyping and Personal Manufacturing applications. Triangular has many of the core functions needed in order to generate toolpaths for 3D printers and CNC Mills/Routers.
require "rubygems"
require "triangular"
# Open and parse an STL file
solid = Triangular.parse_file("test.stl")
# Set the units of measurement for the resulting solid to inches
solid.units = :inches
# Move the solid so that all of it's coordinates are in positive space (ie: greater than 0)
solid.align_to_origin!
# Get the bounding box of the solid
bounds = solid.bounds
# Create a section plane ('slice') through the solid on the XY plane at a Z height of 0.7
slice = solid.slice_at_z(0.7)
# Open a file for SVG output
File.open("slice.svg", "w+") do |file|
# Output the slice as an SVG document (correctly scaled according to the solid's units)
file.puts slice.to_svg(bounds[1].x, bounds[1].y, solid.units)
end
Add Triangular to your Gemfile:
gem 'triangular', '~>0.1.0'
And then execute:
bundle install
Or install it manually by entering the following on your command line:
gem install triangular
At the moment Triangular has not been optimized at all. The parser is a relatively naive one that was designed to be easy to read rather than performant. Once the feature-set of Triangular has stabilized I will be doing a pass over it in order to make it fast enough for production use. Right now it could definitely be improved.
For example here is some information about run-times when processing a 51Mb STL file:
solid = Triangular.parse("big_file.stl")
# 65 seconds
solid.align_to_origin!
# 8 seconds
solid.slice_at_z(1.0)
# 2 seconds
To get setup for local development of Triangular please run the following steps:
git clone git@github.com:aarongough/triangular.git
cd triangular
bundle install
Then run the specs to make sure everything is working!
bundle exec rspec
Author: Aaron Gough
Special thanks go out to Alkas Baybas for lending me his massive brain!
Copyright © 2022 Aaron Gough (http://aarongough.com), released under the MIT license