/// <summary> /// 儲存模型檔案,第一個參數指定路徑,第二個參數指定是否要儲存轉移後的模型 /// </summary> public void SaveModel(string filepath, bool isSavedTransform) { HelixToolkit.Wpf.SharpDX.MeshGeometry3D geometry = Geometry as HelixToolkit.Wpf.SharpDX.MeshGeometry3D; if (geometry == null) { return; } MeshGeometry3D mesh = new MeshGeometry3D { Positions = new Point3DCollection(), Normals = new Vector3DCollection(), TriangleIndices = new Int32Collection() }; foreach (Vector3 position in geometry.Positions) { mesh.Positions.Add(new Point3D(position.X, position.Y, position.Z)); } foreach (Vector3 normal in geometry.Normals) { mesh.Normals.Add(new Vector3D(normal.X, normal.Y, normal.Z)); } foreach (int triangleindice in geometry.TriangleIndices) { mesh.TriangleIndices.Add(triangleindice); } GeometryModel3D model = new GeometryModel3D { Geometry = mesh, }; StlExporter export = new StlExporter(); if (isSavedTransform) { model.Transform = Transform; string fileAfterTransform = System.IO.Path.GetFileNameWithoutExtension(SafeFileName); fileAfterTransform += "(transform).stl"; using (var fileStream = File.Create(System.IO.Path.Combine(filepath, fileAfterTransform))) { export.Export(model, fileStream); } return; } using (var fileStream = File.Create(System.IO.Path.Combine(filepath, SafeFileName))) { export.Export(model, fileStream); } }
public void Export(string path) { foreach (var namedModel in _viewPort.GetAllModels()) { var fileName = Path.Combine(path, namedModel.Key + ".stl"); using (var stream = new System.IO.FileStream(fileName, FileMode.Create)) { StlExporter.Export(namedModel.Value, stream); } } }
private static string CreateNewStlFile(GameObject geometryObject, bool isCollisionGeometry) { Debug.Log("Did not find an existing STL or DAE file for Geometry Mesh " + geometryObject.name + ". Exporting a new STL file.", geometryObject); string newMeshPath = UrdfExportPathHandler.GetNewMeshPath(geometryObject.name + ".stl"); StlExporter stlExporter = new StlExporter(newMeshPath, geometryObject, isCollisionGeometry); if (!stlExporter.Export()) { Debug.LogWarning("Mesh export for geometry " + geometryObject.name + " failed.", geometryObject); } return(newMeshPath); }
//用來輸入文字文件,然後輸出球的模型檔案 private void SaveBall() { string path = "D:\\Desktop\\新文字文件.txt"; try { string fileContent = File.ReadAllText(path); string[] contentArray = fileContent.Split((string[])null, StringSplitOptions.RemoveEmptyEntries); float[] pointInfo = Array.ConvertAll(contentArray, float.Parse); if (pointInfo.Length % 3 != 0) { throw new Exception(); } var ballContainer = new HelixToolkit.Wpf.SharpDX.MeshBuilder(); for (int i = 0; i < pointInfo.Length / 3; i++) { ballContainer.AddSphere(new Vector3(pointInfo[i * 3], pointInfo[i * 3 + 1], pointInfo[i * 3 + 2]), 1.5); } System.Windows.Media.Media3D.MeshGeometry3D ballMesh = new System.Windows.Media.Media3D.MeshGeometry3D { Positions = new Point3DCollection(), Normals = new Vector3DCollection(), TriangleIndices = new Int32Collection() }; HelixToolkit.Wpf.SharpDX.MeshGeometry3D geometry = ballContainer.ToMeshGeometry3D(); foreach (Vector3 position in geometry.Positions) { ballMesh.Positions.Add(new Point3D(position.X, position.Y, position.Z)); } foreach (Vector3 normal in geometry.Normals) { ballMesh.Normals.Add(new Vector3D(normal.X, normal.Y, normal.Z)); } foreach (int triangleindice in geometry.TriangleIndices) { ballMesh.TriangleIndices.Add(triangleindice); } System.Windows.Media.Media3D.GeometryModel3D ballModel = new System.Windows.Media.Media3D.GeometryModel3D { Geometry = ballMesh, }; Model3DGroup ballGroup = new Model3DGroup(); ballGroup.Children.Add(ballModel); StlExporter export1 = new StlExporter(); string name1 = "ball.stl"; using (var fileStream = File.Create("D:\\Desktop\\" + name1)) { export1.Export(ballGroup, fileStream); } } catch { System.Windows.MessageBox.Show("點的讀取錯誤"); } }
private void button_Click_2(object sender, RoutedEventArgs e) { //ball的模型 Model3DGroup ballGroup = new Model3DGroup(); foreach (BallModel model in MainViewModel.ProjData.BallCollection) { System.Windows.Media.Media3D.MeshGeometry3D ballMesh = new System.Windows.Media.Media3D.MeshGeometry3D { Positions = new Point3DCollection(), Normals = new Vector3DCollection(), TriangleIndices = new Int32Collection() }; HelixToolkit.Wpf.SharpDX.MeshGeometry3D geometry = model.Geometry as HelixToolkit.Wpf.SharpDX.MeshGeometry3D; if (geometry == null) { return; } foreach (Vector3 position in geometry.Positions) { ballMesh.Positions.Add(new Point3D(position.X, position.Y, position.Z)); } foreach (Vector3 normal in geometry.Normals) { ballMesh.Normals.Add(new Vector3D(normal.X, normal.Y, normal.Z)); } foreach (int triangleindice in geometry.TriangleIndices) { ballMesh.TriangleIndices.Add(triangleindice); } System.Windows.Media.Media3D.GeometryModel3D ballModel = new System.Windows.Media.Media3D.GeometryModel3D { Geometry = ballMesh, }; ballGroup.Children.Add(ballModel); } StlExporter export1 = new StlExporter(); string name1 = "ball.stl"; using (var fileStream = File.Create("D:\\Desktop\\test\\" + name1)) { export1.Export(ballGroup, fileStream); } //存球資料 FileStream fs = new FileStream("D:\\Desktop\\test\\balldata.txt", FileMode.Create); StreamWriter sw = new StreamWriter(fs); foreach (BallModel model in MainViewModel.ProjData.BallCollection) { sw.Write(model.BallCenter.X + " " + model.BallCenter.Y + " " + model.BallCenter.Z + "\r\n"); } //清空緩衝區 sw.Flush(); //關閉流 sw.Close(); fs.Close(); }