private void saveComposition(string fname) { int n = 0; foreach (STL stl in listSTLObjects.Items) { n += stl.list.Count; } STLTriangle[] triList2 = new STLTriangle[n]; int p = 0; foreach (STL stl in listSTLObjects.Items) { stl.UpdateMatrix(); foreach (STLTriangle t2 in stl.list) { STLTriangle t = new STLTriangle(); t.p1 = new Vector3(); t.p2 = new Vector3(); t.p3 = new Vector3(); t.normal = new Vector3(); stl.TransformPoint(ref t2.p1, out t.p1.X, out t.p1.Y, out t.p1.Z); stl.TransformPoint(ref t2.p2, out t.p2.X, out t.p2.Y, out t.p2.Z); stl.TransformPoint(ref t2.p3, out t.p3.X, out t.p3.Y, out t.p3.Z); // Compute normal from p1-p3 float ax = t.p2.X - t.p1.X; float ay = t.p2.Y - t.p1.Y; float az = t.p2.Z - t.p1.Z; float bx = t.p3.X - t.p1.X; float by = t.p3.Y - t.p1.Y; float bz = t.p3.Z - t.p1.Z; t.normal.X = ay * bz - az * by; t.normal.Y = az * bx - ax * bz; t.normal.Z = ax * by - ay * bx; Vector3.Normalize(ref t.normal, out t.normal); if (AssertVector3NotNaN(t.normal) && AssertVector3NotNaN(t.p1) && AssertVector3NotNaN(t.p2) && AssertVector3NotNaN(t.p3) && AssertMinDistance(t.p1, t.p2) && AssertMinDistance(t.p1, t.p3) && AssertMinDistance(t.p2, t.p3)) { triList2[p++] = t; } } } n = p; STLTriangle[] triList = new STLTriangle[n]; for (int i = 0; i < n; i++) { triList[i] = triList2[i]; } // STL should have increasing z for faster slicing Array.Sort <STLTriangle>(triList, triList[0]); // Write file in binary STL format FileStream fs = File.Open(fname, FileMode.Create); if (writeSTLBinary) { BinaryWriter w = new BinaryWriter(fs); int i; for (i = 0; i < 20; i++) { w.Write((int)0); } w.Write(n); for (i = 0; i < n; i++) { STLTriangle t = triList[i]; w.Write(t.normal.X); w.Write(t.normal.Y); w.Write(t.normal.Z); w.Write(t.p1.X); w.Write(t.p1.Y); w.Write(t.p1.Z); w.Write(t.p2.X); w.Write(t.p2.Y); w.Write(t.p2.Z); w.Write(t.p3.X); w.Write(t.p3.Y); w.Write(t.p3.Z); w.Write((short)0); } w.Close(); } else { TextWriter w = new EnglishStreamWriter(fs); w.WriteLine("solid RepetierHost"); for (int i = 0; i < n; i++) { STLTriangle t = triList[i]; w.Write(" facet normal "); w.Write(t.normal.X); w.Write(" "); w.Write(t.normal.Y); w.Write(" "); w.WriteLine(t.normal.Z); w.WriteLine(" outer loop"); w.Write(" vertex "); w.Write(t.p1.X); w.Write(" "); w.Write(t.p1.Y); w.Write(" "); w.WriteLine(t.p1.Z); w.Write(" vertex "); w.Write(t.p2.X); w.Write(" "); w.Write(t.p2.Y); w.Write(" "); w.WriteLine(t.p2.Z); w.Write(" vertex "); w.Write(t.p3.X); w.Write(" "); w.Write(t.p3.Y); w.Write(" "); w.WriteLine(t.p3.Z); w.WriteLine(" endloop"); w.WriteLine(" endfacet"); } w.WriteLine("endsolid RepetierHost"); w.Close(); } fs.Close(); }
public void exportObj(string filename, bool withNormals) { FileStream fs = File.Open(filename, FileMode.Create); TextWriter w = new EnglishStreamWriter(fs); w.WriteLine("# exported by Repetier-Host"); foreach (TopoVertex v in vertices) { w.Write("v "); w.Write(v.pos.x); w.Write(" "); w.Write(v.pos.y); w.Write(" "); w.WriteLine(v.pos.z); } int idx = 1; if (withNormals) { foreach (TopoTriangle t in triangles) { w.Write("vn "); w.Write(t.normal.x); w.Write(" "); w.Write(t.normal.y); w.Write(" "); w.WriteLine(t.normal.z); } } foreach (TopoTriangle t in triangles) { w.Write("f "); w.Write(t.vertices[0].id); if (withNormals) { w.Write("//"); w.Write(idx); } w.Write(" "); w.Write(t.vertices[1].id); if (withNormals) { w.Write("//"); w.Write(idx); } w.Write(" "); w.Write(t.vertices[2].id); if (withNormals) { w.Write("//"); w.Write(idx); } w.WriteLine(); } w.Close(); fs.Close(); }
public void exportSTL(string filename, bool binary) { FileStream fs = File.Open(filename, FileMode.Create); if (binary) { BinaryWriter w = new BinaryWriter(fs); int i; for (i = 0; i < 20; i++) w.Write((int)0); w.Write(triangles.Count); foreach (TopoTriangle t in triangles) { w.Write((float)t.normal.x); w.Write((float)t.normal.y); w.Write((float)t.normal.z); for (i = 0; i < 3; i++) { w.Write((float)t.vertices[i].pos.x); w.Write((float)t.vertices[i].pos.y); w.Write((float)t.vertices[i].pos.z); } w.Write((short)0); } w.Close(); } else { TextWriter w = new EnglishStreamWriter(fs); w.WriteLine("solid RepetierHost"); foreach (TopoTriangle t in triangles) { w.Write(" facet normal "); w.Write(t.normal.x); w.Write(" "); w.Write(t.normal.y); w.Write(" "); w.WriteLine(t.normal.z); w.WriteLine(" outer loop"); w.Write(" vertex "); w.Write(t.vertices[0].pos.x); w.Write(" "); w.Write(t.vertices[0].pos.y); w.Write(" "); w.WriteLine(t.vertices[0].pos.z); w.Write(" vertex "); w.Write(t.vertices[1].pos.x); w.Write(" "); w.Write(t.vertices[1].pos.y); w.Write(" "); w.WriteLine(t.vertices[1].pos.z); w.Write(" vertex "); w.Write(t.vertices[2].pos.x); w.Write(" "); w.Write(t.vertices[2].pos.y); w.Write(" "); w.WriteLine(t.vertices[2].pos.z); w.WriteLine(" endloop"); w.WriteLine(" endfacet"); } w.WriteLine("endsolid RepetierHost"); w.Close(); } fs.Close(); }
private void saveComposition(string fname) { int n = 0; foreach (STL stl in listSTLObjects.Items) { n += stl.list.Count; } STLTriangle[] triList2 = new STLTriangle[n]; int p = 0; foreach (STL stl in listSTLObjects.Items) { stl.UpdateMatrix(); foreach (STLTriangle t2 in stl.list) { STLTriangle t = new STLTriangle(); t.p1 = new Vector3(); t.p2 = new Vector3(); t.p3 = new Vector3(); t.normal = new Vector3(); stl.TransformPoint(ref t2.p1, out t.p1.X, out t.p1.Y, out t.p1.Z); stl.TransformPoint(ref t2.p2, out t.p2.X, out t.p2.Y, out t.p2.Z); stl.TransformPoint(ref t2.p3, out t.p3.X, out t.p3.Y, out t.p3.Z); // Compute normal from p1-p3 float ax = t.p2.X - t.p1.X; float ay = t.p2.Y - t.p1.Y; float az = t.p2.Z - t.p1.Z; float bx = t.p3.X - t.p1.X; float by = t.p3.Y - t.p1.Y; float bz = t.p3.Z - t.p1.Z; t.normal.X = ay * bz - az * by; t.normal.Y = az * bx - ax * bz; t.normal.Z = ax * by - ay * bx; Vector3.Normalize(ref t.normal, out t.normal); if (AssertVector3NotNaN(t.normal) && AssertVector3NotNaN(t.p1) && AssertVector3NotNaN(t.p2) && AssertVector3NotNaN(t.p3) && AssertMinDistance(t.p1, t.p2) && AssertMinDistance(t.p1, t.p3) && AssertMinDistance(t.p2, t.p3)) { triList2[p++] = t; } } } n = p; STLTriangle[] triList = new STLTriangle[n]; for (int i = 0; i < n; i++) triList[i] = triList2[i]; // STL should have increasing z for faster slicing Array.Sort<STLTriangle>(triList, triList[0]); // Write file in binary STL format FileStream fs = File.Open(fname, FileMode.Create); if (writeSTLBinary) { BinaryWriter w = new BinaryWriter(fs); int i; for (i = 0; i < 20; i++) w.Write((int)0); w.Write(n); for (i = 0; i < n; i++) { STLTriangle t = triList[i]; w.Write(t.normal.X); w.Write(t.normal.Y); w.Write(t.normal.Z); w.Write(t.p1.X); w.Write(t.p1.Y); w.Write(t.p1.Z); w.Write(t.p2.X); w.Write(t.p2.Y); w.Write(t.p2.Z); w.Write(t.p3.X); w.Write(t.p3.Y); w.Write(t.p3.Z); w.Write((short)0); } w.Close(); } else { TextWriter w = new EnglishStreamWriter(fs); w.WriteLine("solid RepetierHost"); for (int i = 0; i < n; i++) { STLTriangle t = triList[i]; w.Write(" facet normal "); w.Write(t.normal.X); w.Write(" "); w.Write(t.normal.Y); w.Write(" "); w.WriteLine(t.normal.Z); w.WriteLine(" outer loop"); w.Write(" vertex "); w.Write(t.p1.X); w.Write(" "); w.Write(t.p1.Y); w.Write(" "); w.WriteLine(t.p1.Z); w.Write(" vertex "); w.Write(t.p2.X); w.Write(" "); w.Write(t.p2.Y); w.Write(" "); w.WriteLine(t.p2.Z); w.Write(" vertex "); w.Write(t.p3.X); w.Write(" "); w.Write(t.p3.Y); w.Write(" "); w.WriteLine(t.p3.Z); w.WriteLine(" endloop"); w.WriteLine(" endfacet"); } w.WriteLine("endsolid RepetierHost"); w.Close(); } fs.Close(); }