public void Write(StlFile file, Stream stream, bool asAscii) { if (asAscii) { WriteAscii(file, stream); } else { WriteBinary(file, stream); } }
/// <summary> /// Построение сетки /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Button1_Click(object sender, EventArgs e) { toolStripStatusLabel1.Text = @"Выполняется построение модели..."; toolStripProgressBar1.Value = 0; Refresh(); float pw = Convert.ToSingle(_layers[0].Dicom.PixelSpacingValue[0], CultureInfo.InvariantCulture.NumberFormat); float ph = Convert.ToSingle(_layers[0].Dicom.PixelSpacingValue[1], CultureInfo.InvariantCulture.NumberFormat); Volume volume = new Volume(_layers, pw, ph); var depth = _layers[0].Dicom.pixelDepth; var space = _layers[0].Dicom.pixeSpace; var t = MCCube.getTriangles(volume); int koeff = 18; for (int k = 0; k < t.Count - 2; k += 3) { var trg = new MyTriangle(t[k], t[k + 1], t[k + 2]); Vertex vrt1 = new Vertex(t[k].X * koeff, t[k].Z * koeff * (-1), t[k].Y * koeff); Vertex vrt2 = new Vertex(t[k + 1].X * koeff, t[k + 1].Z * koeff * (-1), t[k + 1].Y * koeff); Vertex vrt3 = new Vertex(t[k + 2].X * koeff, t[k + 2].Z * koeff * (-1), t[k + 2].Y * koeff); var trg2 = new MyTriangle(vrt1, vrt2, vrt3); _triangles.Add(trg2); } SystemSounds.Beep.Play(); MessageBox.Show("Done!!!"); toolStripStatusLabel1.Text = @"Реконструкция 3D модели позвонка завершена. Число треугольников:" + _triangles.Count.ToString(); Stream myStream = new FileStream(@"Stl\Vertebras\st1\L1.stl", FileMode.Create); StlFile stl = new StlFile { SolidName = "my", Triangles = _triangles }; stl.Save(myStream, true); myStream.Close(); btn_render_start.Enabled = true; btn_render_stop.Enabled = true; сохранитьToolStripMenuItem.Enabled = true; button2.Enabled = true; Refresh(); }
private void WriteAscii(StlFile file, Stream stream) { var writer = new StreamWriter(stream); writer.WriteLine(string.Format("solid {0}", file.SolidName)); foreach (var triangle in file.Triangles) { writer.WriteLine(string.Format(" facet normal {0}", NormalToString(triangle.Normal))); writer.WriteLine(" outer loop"); writer.WriteLine(string.Format(" vertex {0}", VertexToString(triangle.Vertex1))); writer.WriteLine(string.Format(" vertex {0}", VertexToString(triangle.Vertex2))); writer.WriteLine(string.Format(" vertex {0}", VertexToString(triangle.Vertex3))); writer.WriteLine(" endloop"); writer.WriteLine(" endfacet"); } writer.WriteLine(string.Format("endsolid {0}", file.SolidName)); writer.Flush(); }
/// <summary> /// Сохранение файлов /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void СохранитьToolStripMenuItem_Click(object sender, EventArgs e) { renderTimer.Stop(); saveFileDialog1 = new SaveFileDialog { Filter = @"Stl Files (*.stl)|*.stl|Mesh Files (*.mesh)|*.mesh|GMesh Files (*.msh)|*.msh", FilterIndex = 1, RestoreDirectory = true }; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { Stream myStream = saveFileDialog1.OpenFile(); switch (saveFileDialog1.FilterIndex) { case 1: StlFile stl = new StlFile { SolidName = "my", Triangles = _triangles }; stl.Save(myStream, false); break; case 2: //MeshWriter(new StreamWriter(myStream)); break; case 3: //GMeshWriter(new StreamWriter(myStream)); break; } myStream.Close(); System.Media.SystemSounds.Beep.Play(); MessageBox.Show(@"Файл сохранен!"); } }
private void WriteBinary(StlFile file, Stream stream) { var writer = new BinaryWriter(stream); // write header var header = new byte[80]; // can be a garbage value writer.Write(header); // write vertex count writer.Write((uint)file.Triangles.Count); // write triangles foreach (var triangle in file.Triangles) { writer.Write(triangle.Normal.X); writer.Write(triangle.Normal.Y); writer.Write(triangle.Normal.Z); writer.Write(triangle.Vertex1.X); writer.Write(triangle.Vertex1.Y); writer.Write(triangle.Vertex1.Z); writer.Write(triangle.Vertex2.X); writer.Write(triangle.Vertex2.Y); writer.Write(triangle.Vertex2.Z); writer.Write(triangle.Vertex3.X); writer.Write(triangle.Vertex3.Y); writer.Write(triangle.Vertex3.Z); writer.Write((ushort)0); // garbage value } writer.Flush(); }