Skip to content

Commit 5302b07

Browse files
committed
Ability to force trying to read as binary if zero facet read as text
1 parent 13414f6 commit 5302b07

1 file changed

Lines changed: 15 additions & 8 deletions

File tree

Source/STL/STLDocument.cs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,22 +147,29 @@ public static bool IsBinary(Stream stream)
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>
149149
/// <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)
150+
public static STLDocument Read(Stream stream,bool tryBinaryIfTextFailed=false)
151151
{
152152
//Determine if the stream contains a text-based or binary-based <see cref="STLDocument"/>, and then read it.
153-
if (IsText(stream))
153+
var isText = IsText(stream);
154+
STLDocument textStlDocument = null;
155+
if (isText)
154156
{
155157
using (StreamReader reader = new StreamReader(stream, Encoding.ASCII, true, DefaultBufferSize, true))
156158
{
157-
return Read(reader);
159+
textStlDocument = Read(reader);
158160
}
161+
162+
if (textStlDocument.Facets.Count > 0 || !tryBinaryIfTextFailed) return textStlDocument;
163+
stream.Seek(0, SeekOrigin.Begin);
159164
}
160-
else
165+
166+
//Try binary if zero Facets were read and tryBinaryIfTextFailed==true
167+
using (BinaryReader reader = new BinaryReader(stream, Encoding.ASCII, true))
161168
{
162-
using (BinaryReader reader = new BinaryReader(stream, Encoding.ASCII, true))
163-
{
164-
return Read(reader);
165-
}
169+
var binaryStlDocument = Read(reader);
170+
171+
//return text reading result if binary reading also failed and tryBinaryIfTextFailed==true
172+
return (binaryStlDocument.Facets.Count>0 || !isText)?binaryStlDocument:textStlDocument;
166173
}
167174
}
168175

0 commit comments

Comments
 (0)