/// <summary> /// Create a combined .stl file from all the .stl files that are on the current printer platform. /// </summary> /// <param name="fname">File path and name of the combined composition to save</param> public void SaveComposition(string fname) { int n = 0; foreach (STL stl in this.main.listSTLObjects.Items) { n += stl.list.Count; } STLTriangle[] triList2 = new STLTriangle[n]; int p = 0; foreach (STL stl in this.main.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 (this.AssertVector3NotNaN(t.normal) && this.AssertVector3NotNaN(t.p1) && this.AssertVector3NotNaN(t.p2) && this.AssertVector3NotNaN(t.p3) && this.AssertMinDistance(t.p1, t.p2) && this.AssertMinDistance(t.p1, t.p3) && this.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 (this.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(); }
private void saveComposition(string fname) { int n = 0; foreach (STL stl in listSTLObjects.Items) { n += stl.list.Count; } STLTriangle[] triList = 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); triList[p++] = t; } } // 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); 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(); fs.Close(); }
private void saveComposition(string fname) { int n = 0; foreach (STL stl in listSTLObjects.Items) { n += stl.list.Count; } STLTriangle[] triList = 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); triList[p++] = t; } } // 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); 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(); 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(); }