.stl file format of 3D Systems Stereolithography: Ruby parsing library

STL files are used to represent simple 3D models, defined using triangular 3D faces.

Initially it was introduced as native format for 3D Systems Stereolithography CAD system, but due to its extreme simplicity, it was adopted by a wide range of 3D modelling, CAD, rapid prototyping and 3D printing applications as the simplest 3D model exchange format.

STL is extremely bare-bones format: there are no complex headers, no texture / color support, no units specifications, no distinct vertex arrays. Whole model is specified as a collection of triangular faces.

There are two versions of the format (text and binary), this spec describes binary version.

Application

3D Systems Stereolithography

File extension

stl

KS implementation details

License: CC0-1.0

References

This page hosts a formal specification of .stl file format of 3D Systems Stereolithography using Kaitai Struct. This specification can be automatically translated into a variety of programming languages to get a parsing library.

Usage

Parse a local file and get structure in memory:

data = Stl.from_file("path/to/local/file.stl")

Or parse structure from a string of bytes:

bytes = "\x00\x01\x02..."
data = Stl.new(Kaitai::Struct::Stream.new(bytes))

After that, one can get various attributes from the structure by invoking getter methods like:

data.header # => get header

Ruby source code to parse .stl file format of 3D Systems Stereolithography

stl.rb

# This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild

require 'kaitai/struct/struct'

unless Gem::Version.new(Kaitai::Struct::VERSION) >= Gem::Version.new('0.7')
  raise "Incompatible Kaitai Struct Ruby API: 0.7 or later is required, but you have #{Kaitai::Struct::VERSION}"
end


##
# STL files are used to represent simple 3D models, defined using
# triangular 3D faces.
# 
# Initially it was introduced as native format for 3D Systems
# Stereolithography CAD system, but due to its extreme simplicity, it
# was adopted by a wide range of 3D modelling, CAD, rapid prototyping
# and 3D printing applications as the simplest 3D model exchange
# format.
# 
# STL is extremely bare-bones format: there are no complex headers, no
# texture / color support, no units specifications, no distinct vertex
# arrays. Whole model is specified as a collection of triangular
# faces.
# 
# There are two versions of the format (text and binary), this spec
# describes binary version.
class Stl < Kaitai::Struct::Struct
  def initialize(_io, _parent = nil, _root = self)
    super(_io, _parent, _root)
    _read
  end

  def _read
    @header = @_io.read_bytes(80)
    @num_triangles = @_io.read_u4le
    @triangles = Array.new(num_triangles)
    (num_triangles).times { |i|
      @triangles[i] = Triangle.new(@_io, self, @_root)
    }
    self
  end

  ##
  # Each STL triangle is defined by its 3 points in 3D space and a
  # normal vector, which is generally used to determine where is
  # "inside" and "outside" of the model.
  class Triangle < Kaitai::Struct::Struct
    def initialize(_io, _parent = nil, _root = self)
      super(_io, _parent, _root)
      _read
    end

    def _read
      @normal = Vec3d.new(@_io, self, @_root)
      @vertices = Array.new(3)
      (3).times { |i|
        @vertices[i] = Vec3d.new(@_io, self, @_root)
      }
      self
    end
    attr_reader :normal
    attr_reader :vertices
  end
  class Vec3d < Kaitai::Struct::Struct
    def initialize(_io, _parent = nil, _root = self)
      super(_io, _parent, _root)
      _read
    end

    def _read
      @x = @_io.read_f4le
      @y = @_io.read_f4le
      @z = @_io.read_f4le
      self
    end
    attr_reader :x
    attr_reader :y
    attr_reader :z
  end
  attr_reader :header
  attr_reader :num_triangles
  attr_reader :triangles
end