Skip to content

Instantly share code, notes, and snippets.

@paulkaplan
Created January 24, 2013 21:56
Show Gist options
  • Save paulkaplan/4628312 to your computer and use it in GitHub Desktop.
Save paulkaplan/4628312 to your computer and use it in GitHub Desktop.
class STL
class Writer
attr_accessor :tris, :size
def initialize tris
@tris = tris
@size = tris.length*50 + 80 + 4
end
def write_stl_header f
80.times {|n| f << [0].pack("C") } #uint8
end
def write_number_tris f
f << [@size].pack("L") #uint32
end
def write_tris f
@tris.each do |t|
f << [0,0,0].pack("fff") # normals, dont matter
f << t[0].values.pack("fff") # 3 vertices
f << t[1].values.pack("fff")
f << t[2].values.pack("fff")
f << [0].pack("S") # random uint16 at the end of each tri
end
end
def save_as fname
File.open(fname, "wb") do |f|
write_stl_header(f)
write_number_tris(f)
write_tris(f)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment