Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for binary STLs with 'solid' header and fix STLDocument #27

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 13 additions & 17 deletions src/STLDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,22 +125,7 @@ public void AppendFacets(IEnumerable<Facet> facets)
/// <returns>True if the <see cref="STLDocument"/> is text-based, otherwise false.</returns>
public static bool IsText(Stream stream)
{
if (stream == null) throw new NullReferenceException(nameof(stream));

const string solid = "solid";

byte[] buffer = new byte[5];
string header;

// Reset the stream to tbe beginning and read the first few bytes, then reset the stream to the beginning again.
stream.Seek(0, SeekOrigin.Begin);
stream.Read(buffer, 0, buffer.Length);
stream.Seek(0, SeekOrigin.Begin);

// Read the header as ASCII.
header = Encoding.ASCII.GetString(buffer);

return solid.Equals(header, StringComparison.InvariantCultureIgnoreCase);
return !IsBinary(stream);
}

/// <summary>Determines if the <see cref="STLDocument"/> contained within the <paramref name="stream"/> is binary-based.</summary>
Expand All @@ -149,7 +134,18 @@ public static bool IsText(Stream stream)
/// <returns>True if the <see cref="STLDocument"/> is binary-based, otherwise false.</returns>
public static bool IsBinary(Stream stream)
{
return !IsText(stream);
if (stream == null) throw new NullReferenceException(nameof(stream));

using var reader = new BinaryReader(stream, Encoding.Default, true);
JoshMcCullough marked this conversation as resolved.
Show resolved Hide resolved

stream.Seek(80, SeekOrigin.Begin);

const numTriangles = reader.ReadUInt32();
const expectedSize = 84 + numTriangles * 50;

stream.Seek(0, SeekOrigin.Begin);

return stream.Length == expectedSize;
}

/// <summary>Reads the <see cref="STLDocument"/> contained within the <paramref name="stream"/> into a new <see cref="STLDocument"/>.</summary>
Expand Down
Binary file added tests/Data/BinaryWithSolidHeader.stl
Binary file not shown.
13 changes: 13 additions & 0 deletions tests/STLDocumentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ public void Read__FromBinaryStream()
ValidateSTL(stlBinary);
}

[Fact]
public void Read__FromBinaryStreamWithSolidHeader()
{
STLDocument stlBinary;

using (var stream = GetData("BinaryWithSolidHeader.stl"))
{
stlBinary = STLDocument.Read(stream);
}

ValidateSTL(stlBinary);
}

[Fact]
public void Read__FromTextReader()
{
Expand Down