Loading 3d Models

STL Files

Stereolithography CAD files or STL is a 3d file format common in rapid prototyping, 3d printing, and computer aided design. STL files come in in two forms, an ASCII version as well as a Binary version. Loading and saving of STL files for both binary and ASCII version is supported via the Qkmaxware.Geometry.IO.StlSerializer class.

ASCII STL

To load an ASCII encoded STL, use the Deserialize method of an StlSerializer object with a TextReader as the argument.

using System.IO;
using Qkmaxware.Geometry.IO;

public class Program {
    public static void Main(string[] args) {
        var serializer = new StlSerializer();

        using (var reader = new StreamReader("myfile.stl")) {
            var mesh = serializer.Deserialize(reader);
        }
    }
}

Binary STL

To load a binary encoded STL, use the Deserialize method of an StlSerializer object with a BinaryReader as the argument.

using System.IO;
using Qkmaxware.Geometry.IO;

public class Program {
    public static void Main(string[] args) {
        var serializer = new StlSerializer();

        using (var stream = new FileStream("myfile.stl", FileMode.Open))
        using (var reader = new BinaryReader(stream)) {
            var mesh = serializer.Deserialize(reader);
        }
    }
}

Generic STL

Since STL files can be either ASCII or Binary encoded, loading of an STL file depends on selecting the correct encoding. To aid in loading a generic STL file in which it is not known if the file is ASCII or Binary, the StlSerializer object has a utility method IsStlAscii which can be used to help determine if a given file is in the ASCII format or not.

using System.IO;
using Qkmaxware.Geometry.IO;

public class Program {
    public static void Main(string[] args) {
        var serializer = new StlSerializer();
        var file = "myfile.stl"

        IMesh mesh = null;
        if (serializer.IsStlAscii(file)) {
            using (var reader = new StreamReader(file)) {
                mesh = serializer.Deserialize(reader);
            }
        } else {
            using (var stream = new FileStream(file, FileMode.Open))
            using (var reader = new BinaryReader(stream)) {
                mesh = serializer.Deserialize(reader);
            }
        }
    }
}

Alternatively the StlSerializer class also has a DeserializeFromFile method which will do the same job as the above code given the path to an STL file on the hard drive.

Wavefront Obj Files

Wavefront Obj files are another common file format. Unlike STL and other formats, OBJ files are only available as an ASCII encoded format. Loading and saving of OBJ files is supported via the Qkmaxware.Geometry.IO.ObjSerializer class.

ASCII Obj

To load an ASCII encoded OBJ, use the Deserialize method of an ObjSerializer object with a TextReader as the argument.

using System.IO;
using Qkmaxware.Geometry.IO;

public class Program {
    public static void Main(string[] args) {
        var serializer = new ObjSerializer();

        using (var reader = new StreamReader("myfile.obj")) {
            var mesh = serializer.Deserialize(reader);
        }
    }
}