Here’s the source code that Pancake used to export Rhino Mesh to STL files. You may take it as a reference.
Mesh