.stl file format of 3D Systems Stereolithography: C++/STL 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

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

Using Kaitai Struct in C++/STL usually consists of 3 steps.

  1. We need to create an STL input stream (std::istream).
    • One can open a stream for reading from a local file:
      #include <fstream>
      
      std::ifstream is("path/to/local/file.stl", std::ifstream::binary);
    • Or one can prepare a stream for reading from existing std::string str:
      #include <sstream>
      
      std::istringstream is(str);
    • Or one can parse arbitrary char* buffer in memory, given that we know its size:
      #include <sstream>
      
      const char buf[] = { ... };
      std::string str(buf, sizeof buf);
      std::istringstream is(str);
  2. We need to wrap our input stream into Kaitai stream:
    #include <kaitai/kaitaistream.h>
    
    kaitai::kstream ks(&is);
  3. And finally, we can invoke the parsing:
    stl_t data(&ks);

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

data.header() // => get header

C++/STL source code to parse .stl file format of 3D Systems Stereolithography

stl.h

#ifndef STL_H_
#define STL_H_

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

#include "kaitai/kaitaistruct.h"

#include <stdint.h>
#include <vector>

#if KAITAI_STRUCT_VERSION < 7000L
#error "Incompatible Kaitai Struct C++/STL API: version 0.7 or later is required"
#endif

/**
 * 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_t : public kaitai::kstruct {

public:
    class triangle_t;
    class vec3d_t;

    stl_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent = 0, stl_t* p__root = 0);

private:
    void _read();

public:
    ~stl_t();

    /**
     * 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_t : public kaitai::kstruct {

    public:

        triangle_t(kaitai::kstream* p__io, stl_t* p__parent = 0, stl_t* p__root = 0);

    private:
        void _read();

    public:
        ~triangle_t();

    private:
        vec3d_t* m_normal;
        std::vector<vec3d_t*>* m_vertices;
        stl_t* m__root;
        stl_t* m__parent;

    public:
        vec3d_t* normal() const { return m_normal; }
        std::vector<vec3d_t*>* vertices() const { return m_vertices; }
        stl_t* _root() const { return m__root; }
        stl_t* _parent() const { return m__parent; }
    };

    class vec3d_t : public kaitai::kstruct {

    public:

        vec3d_t(kaitai::kstream* p__io, stl_t::triangle_t* p__parent = 0, stl_t* p__root = 0);

    private:
        void _read();

    public:
        ~vec3d_t();

    private:
        float m_x;
        float m_y;
        float m_z;
        stl_t* m__root;
        stl_t::triangle_t* m__parent;

    public:
        float x() const { return m_x; }
        float y() const { return m_y; }
        float z() const { return m_z; }
        stl_t* _root() const { return m__root; }
        stl_t::triangle_t* _parent() const { return m__parent; }
    };

private:
    std::string m_header;
    uint32_t m_num_triangles;
    std::vector<triangle_t*>* m_triangles;
    stl_t* m__root;
    kaitai::kstruct* m__parent;

public:
    std::string header() const { return m_header; }
    uint32_t num_triangles() const { return m_num_triangles; }
    std::vector<triangle_t*>* triangles() const { return m_triangles; }
    stl_t* _root() const { return m__root; }
    kaitai::kstruct* _parent() const { return m__parent; }
};

#endif  // STL_H_

stl.cpp

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

#include "stl.h"



stl_t::stl_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent, stl_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = this;
    _read();
}

void stl_t::_read() {
    m_header = m__io->read_bytes(80);
    m_num_triangles = m__io->read_u4le();
    int l_triangles = num_triangles();
    m_triangles = new std::vector<triangle_t*>();
    m_triangles->reserve(l_triangles);
    for (int i = 0; i < l_triangles; i++) {
        m_triangles->push_back(new triangle_t(m__io, this, m__root));
    }
}

stl_t::~stl_t() {
    for (std::vector<triangle_t*>::iterator it = m_triangles->begin(); it != m_triangles->end(); ++it) {
        delete *it;
    }
    delete m_triangles;
}

stl_t::triangle_t::triangle_t(kaitai::kstream* p__io, stl_t* p__parent, stl_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    _read();
}

void stl_t::triangle_t::_read() {
    m_normal = new vec3d_t(m__io, this, m__root);
    int l_vertices = 3;
    m_vertices = new std::vector<vec3d_t*>();
    m_vertices->reserve(l_vertices);
    for (int i = 0; i < l_vertices; i++) {
        m_vertices->push_back(new vec3d_t(m__io, this, m__root));
    }
}

stl_t::triangle_t::~triangle_t() {
    delete m_normal;
    for (std::vector<vec3d_t*>::iterator it = m_vertices->begin(); it != m_vertices->end(); ++it) {
        delete *it;
    }
    delete m_vertices;
}

stl_t::vec3d_t::vec3d_t(kaitai::kstream* p__io, stl_t::triangle_t* p__parent, stl_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    _read();
}

void stl_t::vec3d_t::_read() {
    m_x = m__io->read_f4le();
    m_y = m__io->read_f4le();
    m_z = m__io->read_f4le();
}

stl_t::vec3d_t::~vec3d_t() {
}