public IOReadResult ReadFile(Stream stream, IMeshBuilder builder, ReadOptions options, ParsingMessagesHandler messages) { // detect binary STL BinaryReader binReader = new BinaryReader(stream); byte[] header = binReader.ReadBytes(80); bool bIsBinary = false; // if first 80 bytes contain non-text chars, probably a binary file if (Util.IsTextString(header) == false) { bIsBinary = true; } // if we don't see "solid" string in first 80 chars, probably binary if (bIsBinary == false) { string sText = System.Text.Encoding.ASCII.GetString(header); if (sText.Contains("solid") == false) { bIsBinary = true; } } stream.Seek(0, SeekOrigin.Begin); // reset stream STLReader reader = new STLReader(); reader.warningEvent += messages; IOReadResult result = (bIsBinary) ? reader.Read(new BinaryReader(stream), options, builder) : reader.Read(new StreamReader(stream), options, builder); return(result); }
public IOReadResult ReadFile(string sFilename, IMeshBuilder builder, ReadOptions options, ParsingMessagesHandler messages) { // detect binary STL FileStream stream = null; try { stream = File.Open(sFilename, FileMode.Open, FileAccess.Read); } catch (Exception e) { return(new IOReadResult(IOCode.FileAccessError, "Could not open file " + sFilename + " for reading : " + e.Message)); } BinaryReader binReader = new BinaryReader(stream); byte[] header = binReader.ReadBytes(80); bool bIsBinary = false; // if first 80 bytes contain non-text chars, probably a binary file if (Util.IsTextString(header) == false) { bIsBinary = true; } // if we don't see "solid" string in first 80 chars, probably binary if (bIsBinary == false) { string sText = System.Text.Encoding.ASCII.GetString(header); if (sText.Contains("solid") == false) { bIsBinary = true; } } stream.Seek(0, SeekOrigin.Begin); // reset stream STLReader reader = new STLReader(); reader.warningEvent += messages; IOReadResult result = (bIsBinary) ? reader.Read(new BinaryReader(stream), options, builder) : reader.Read(new StreamReader(stream), options, builder); stream.Close(); return(result); }