Skip to content

Commit 216a338

Browse files
Merge pull request #17 from jinek/master
Ability to try as binary also in case of zero facets
2 parents 3d008d2 + 946fb93 commit 216a338

1 file changed

Lines changed: 16 additions & 8 deletions

File tree

Source/STL/STLDocument.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,23 +146,31 @@ public static bool IsBinary(Stream stream)
146146
/// <summary>Reads the <see cref="STLDocument"/> contained within the <paramref name="stream"/> into a new <see cref="STLDocument"/>.</summary>
147147
/// <remarks>This method will determine how to read the <see cref="STLDocument"/> (whether to read it as text or binary data).</remarks>
148148
/// <param name="stream">The stream which contains the STL data.</param>
149+
/// <param name="tryBinaryIfTextFailed">Set to true to try read as binary if reading as text results in zero facets</param>
149150
/// <returns>An <see cref="STLDocument"/> representing the data contained in the stream or null if the stream is empty.</returns>
150-
public static STLDocument Read(Stream stream)
151+
public static STLDocument Read(Stream stream,bool tryBinaryIfTextFailed=false)
151152
{
152153
//Determine if the stream contains a text-based or binary-based <see cref="STLDocument"/>, and then read it.
153-
if (IsText(stream))
154+
var isText = IsText(stream);
155+
STLDocument textStlDocument = null;
156+
if (isText)
154157
{
155158
using (StreamReader reader = new StreamReader(stream, Encoding.ASCII, true, DefaultBufferSize, true))
156159
{
157-
return Read(reader);
160+
textStlDocument = Read(reader);
158161
}
162+
163+
if (textStlDocument.Facets.Count > 0 || !tryBinaryIfTextFailed) return textStlDocument;
164+
stream.Seek(0, SeekOrigin.Begin);
159165
}
160-
else
166+
167+
//Try binary if zero Facets were read and tryBinaryIfTextFailed==true
168+
using (BinaryReader reader = new BinaryReader(stream, Encoding.ASCII, true))
161169
{
162-
using (BinaryReader reader = new BinaryReader(stream, Encoding.ASCII, true))
163-
{
164-
return Read(reader);
165-
}
170+
var binaryStlDocument = Read(reader);
171+
172+
//return text reading result if binary reading also failed and tryBinaryIfTextFailed==true
173+
return (binaryStlDocument.Facets.Count>0 || !isText)?binaryStlDocument:textStlDocument;
166174
}
167175
}
168176

0 commit comments

Comments
 (0)