//-------------------------------------------------------------------------------------------------- bool IBodyImporter.DoImport(string fileName, out IEnumerable <Body> bodies) { bodies = null; try { using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { IStlReader reader; // Binary or Ascii? var tempBuffer = new byte[80]; fs.Read(tempBuffer, 0, 80); var header = Encoding.ASCII.GetString(tempBuffer).TrimStart(' '); if (header.StartsWith("solid", StringComparison.InvariantCultureIgnoreCase)) { reader = new StlAsciiReader(); } else { reader = new StlBinaryReader(); } var predictedTriangleCount = reader.Init(fs); // Read facets var vertices = new List <Pnt>(predictedTriangleCount * 3); while (reader.MoveNext()) { vertices.Add(reader.CurrentVertices[0]); vertices.Add(reader.CurrentVertices[1]); vertices.Add(reader.CurrentVertices[2]); } // Create Shape var face = TriangulationHelper.CreateFaceFromTriangulation(new TriangulationData(null, vertices.ToArray())); var body = Body.Create(Mesh.Create(face)); body.Name = Path.GetFileNameWithoutExtension(fileName); bodies = new[] { body }; // Cleanup fs.Close(); } return(true); } catch (Exception e) { Messages.Exception($"Exception occured while importing {fileName}.", e); return(false); } }
private static Polygon3[] ReadFacetsFromFile(string stlFilePath, bool isStlAscii) { StlReader reader; if (isStlAscii) { reader = new StlAsciiReader(); } else { reader = new StlBinaryReader(); } return(reader.Read(stlFilePath)); }
public void TestBinaryReader() { Solid solid; using (var str = Utils.OpenDataStream("Binary.stl")) { var reader = new StlBinaryReader(str); solid = reader.ReadSolid(); } Assert.NotNull(solid); Assert.Equal(SolidFormat.Binary, solid.Format); Assert.Equal(12, solid.Facets.Count); foreach (Facet facet in solid.Facets) { Assert.Equal(3, facet.Vertices.Count); } Assert.Throws <ArgumentNullException>(() => new StlBinaryReader(null)); }
public void TestBinaryReader() { Solid solid; using (var str = Utils.OpenDataStream("Binary.stl")) { var reader = new StlBinaryReader(str); solid = reader.ReadSolid(); } Assert.NotNull(solid); Assert.Equal(SolidFormat.Binary, solid.Format); Assert.Equal(12, solid.Facets.Count); foreach (Facet facet in solid.Facets) Assert.Equal(3, facet.Vertices.Count); Assert.Throws<ArgumentNullException>(() => new StlBinaryReader(null)); }