I am trying to save some data as an STL file for use on a 3D printer. The STL file has two forms: ASCII and Binary. The ASCII format is relatively easy to understand and create but most 3D printing services require it to be in binary format.
The information about STL Binary is explained on the Wikipedia page here: http://en.wikipedia.org/wiki/STL_(file_format)
I know that I will require the data to be in a byte array but I have no idea how to go about interpreting the information from Wikipedia and creating the byte array. This is what I would like help with.
The code I have so far simply saves an empty byte array:
byte[] bytes = null;
FileOutputStream stream = new FileOutputStream("test.stl");
try {
stream.write(bytes);
} finally {
stream.close();
}
If you start a new project on an up-to-date Java version, you should not hassle with OutputStreams. Use Channels and ByteBuffers instead.
This is the only API providing you with the little-endian support as required. Then match the datatypes: UINT8 means unsigned byte, UINT32 means unsigned int, REAL32 means float, UINT16 means unsigned short, REAL32[3] means three floats (i.e. an array)
You don’t have to worry about the unsigned nature of the data types as long as you don’t exceed the max values of the corresponding signed Java types.