STL file format: How to convert from ascii to binary

Convert STL files from ascii to binary

STL (an abbreviation of “stereolithography”) is a file format native to the stereolithography CAD software.

For a full description of the specification refer to the following links:

Purpose:

Since the introduction of the 3D-Printing technology, the stereolithy (STL) file format has become very popular. In general, there are 2 possible file-formats : ascii and binary STL files.

The most common format is ascii, although it consumes a lot more disc space and also takes significant more time to read in comarison to its binary counterpart.

The file size ratio between ascii to binary STL file is about 5 : 1 (average). The processing time (open a STL file with a slicer-software) could also be signficant, especially for big models with complex geometry.

In short words: it makes sence to use the binary STL format all the way down in your workflow.

Web Services:

There is a web service available which offers a binary convertion on-line: http://www.meshconvert.com/de.html

After some tests with the web-service - but mainly after finishing my own software solution - I found out that the converted binary file shows some geometry losses.

The following pictures shows some problems with small corner radii (which are lost in the binary result file):

binarySTL_missing_corner_radii.png

The web service might be quite handy to convert a file on the go just to see, how the geometry looks like, but has some drawbacks:

  • file quality of the produced output file (?),
  • cannot be integrated in the workflow process,
  • not possible to batch-convert more than one file at once.

Use cases for binary STL files:

  • CAD / Catia:

    As already mentioned, the CAD program should produce binary STL files at the very 1st place. For Catia, I created a CATScript macro, which offers an add-in replacement for the native Catia Save As STL dialog.

  • Web:

    There is a three.js bases web viewer available, which works best with binary STL files, thus to reduce band width and to give a better user experience.

  • Storage file format:

    Binary STL files simply save disk space on your (expensive) file server and also helps to tear down CO2 pollution ( :=) ).

Software Solution:

After checking out the STL specification, it looks like the STL binary format is fairly simple and easy to generate. For the programming, I usually use Tcl/Tk as my favorite language which is also quite easy to program too.

With the following sub-functions it worked out pretty well to write the vertex data to file:

    proc ConvertVertexToBinary {x y z} {
        
        set x [expr {double($x)}]
        set y [expr {double($y)}]
        set z [expr {double($z)}]
        
        # REAL32
        # the tcl documentation says:
        # r: This form stores the single-precision floating point numbers
        # in little-endian order. This conversion only produces meaningful output
        # when used on machines which use the IEEE floating point representation
        # (very common, but not universal.)
        #
        return [binary format r3 [list $x $y $z]]
    }
    
    proc ConvertNumToBinary {type num} {
        
        switch -- $type {
            "UINT8"  { return [eval binary format c $num] }
            "UINT16" { return [eval binary format s $num] }
            "UINT32" { return [eval binary format i $num] }
        }
    }

    proc CheckMachineByteOrder {} {
        if { $::tcl_platform(byteOrder) != "littleEndian" } {
            return -code error "your platform's byteOrder does not support \"littleEndian\""
        }
        return 1
    }

Once the program was finished and tested, the converted binary STL file looked pretty o.k. (and does not show any missing geoemtry):

binarySTL_example.png

Download:

There is a binary available for windows, which can be downloaded from here:


Size / byte: Name:
3655071 STLConverter_windows_executable.zip


Installation notes & usage:

  • The binary is self-independent, does not need to be installed, just copy the executable in a directory or onto your desktop (or create a link onto your desktop).

  • After that, you can use command line arguments or just drag&drop an ascii STL file onto the program’s icon. The binary output file will be stored in the same path as the original file and will get the same name but with trailing -binary.stl naming convention.

  • For Catia users, there is also a CATScript macro available. So, if you are interestet to integrate this solution into your CAD environment, feel free to contact me.

License & permission to use:

© 2018, Johann Oberdorfer - Engineering Support | CAD | Software

This software is distributed under the BSD license. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the BSD License for more details.


comments powered by Disqus