/// <summary> /// Parses a binary STL file. /// </summary> /// <remarks> /// Because ASCII STL files can become very large, a binary version of STL exists. /// A binary STL file has an 80 character header (which is generally ignored - but /// which should never begin with 'solid' because that will lead most software to assume /// that this is an ASCII STL file). Following the header is a 4 byte unsigned /// integer indicating the number of triangular facets in the file. Following that /// is data describing each triangle in turn. The file simply ends after the last triangle. /// Each triangle is described by twelve floating point numbers: three for the normal /// and then three for the X/Y/Z coordinate of each vertex - just as with the ASCII version /// of STL. After the twelve floats there is a two byte unsigned 'short' integer that /// is the 'attribute byte count' - in the standard format, this should be zero because most /// software does not understand anything else.( http://en.wikipedia.org/wiki/STL_(file_format) ) /// </remarks> /// <param name="file">Stream to parse</param> private void ParseBinary(StreamReader file) { BinaryReader binReader = new BinaryReader(file.BaseStream); // Set stream back to null binReader.BaseStream.Position = 0; char[] charBuf = new char[80]; // The first 80 bytes are trash binReader.Read(charBuf, 0, 80); // Next 4 bytes contain the count of the normal/3D-vertexes record int count = (int)binReader.ReadUInt32(); // Throw InvalidDataException if size does not fit // 84 Byte for header+count, count * (size of data = 50 Bytes) if (binReader.BaseStream.Length != (84 + count * 50)) { throw new InvalidDataException(); } try { // Read the records for (int i = 0; i < count; i++) { Vertex[] tmp = new Vertex[4] { new Vertex(0, 0, 0), new Vertex(0, 0, 0), new Vertex(0, 0, 0), new Vertex(0, 0, 0) }; // Normal/vertices // First one is the normal for (int j = 0; j < 3; j++) { tmp[3][j] = binReader.ReadSingle(); } // Next three are vertices for (int k = 0; k < 3; k++) { for (int h = 0; h < 3; h++) { tmp[k][h] = binReader.ReadSingle(); } tmp[k].Finish(); } // Last two bytes are only to fill up to 50 bytes binReader.Read(charBuf, 0, 2); triangleList.AddTriangle(new Triangle(tmp)); } } catch { MessageBox.Show("Parser-Error", "", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { binReader.Close(); triangleList.Finish(); } }
/// <summary> /// Returns a copy of the TriangleList. /// </summary> /// <returns>Copy of the TriangleList</returns> internal TriangleList Copy() { TriangleList tri = new TriangleList(); for (int i = 0; i < this.Count; i++) { tri.AddTriangle(this[i].Copy()); } tri.Finish(); return(tri); }