diff --git a/Views/UserControl/Viewport3D.xaml.cs b/Views/UserControl/Viewport3D.xaml.cs index 38c7872..5dd7932 100644 --- a/Views/UserControl/Viewport3D.xaml.cs +++ b/Views/UserControl/Viewport3D.xaml.cs @@ -218,7 +218,7 @@ public partial class Viewport3D ViewportManager.DoubleClickSelect = !false; break; case "BtnShow3DView": - + UnityHelper.GenerateRender(ViewportManager.ViewportTriangle.First().TriangleCode, "123"); break; } diff --git a/Views/UserControl/ViewportData/Helper/UnityHelper.cs b/Views/UserControl/ViewportData/Helper/UnityHelper.cs index 10dc8fd..4c9c28b 100644 --- a/Views/UserControl/ViewportData/Helper/UnityHelper.cs +++ b/Views/UserControl/ViewportData/Helper/UnityHelper.cs @@ -1,18 +1,163 @@ namespace SparkClient.Views.UserControl.ViewportData.Helper; - +using System; +using System.Diagnostics; +using System.IO; +using System.Threading; +using System.Windows.Forms; public class UnityHelper { - private static string _image_path = AppDomain.CurrentDomain.BaseDirectory + "/temp/image/"; + //unity渲染结果(运行前清理) + private static string _image_path = AppDomain.CurrentDomain.BaseDirectory + @"Unity\image\"; + //unity渲染模型缓存(运行前清理) + private static string _stl_path = AppDomain.CurrentDomain.BaseDirectory + @"Unity\stl\"; + //用于存放unity渲染插件(版本发布独立更新) + private static string _unity_path = AppDomain.CurrentDomain.BaseDirectory + @"Unity\core\"; /*** * 1、根据模型信息和钻石编码生成签名作为stl文件名(导出:ViewportHelperPro.ExportModelsToStl(null, path+filename)) * 2、判断同名STL文件是否存在 存在跳到 7 - * 3、 清理temp下,image、stl两个目录 + * 3、 清理Unity下,image、stl两个目录 * 4、导出模型stl文件,记录完整的路径paramA * 5、执行导出命令 xx.exe --model=_stl_path+paramA --output=_image_path --type=0/1 0:心图(0.png) 1:箭图(1.png) * 6、等待命令结束 * 7、判断图片文件是否存在 --->>不存在:提示生成失败,请重试, --->>不存在并且从2过来的,跳到1 * 8、展示图片弹窗,并注以说明:心箭图为模拟渲染,实际请以切工镜观测为准。 * 9、关闭 + * + * 图片展示框左侧展示心图0.png,右侧展示箭图1.png 同时展示在一起 */ + public static void GenerateRender(string modelInfo, string diamondCode) + { + string fileName = GenerateSignature(modelInfo, diamondCode) + ".stl"; + string fullStlPath = Path.Combine(_stl_path, fileName); + + // 1. 检查STL文件是否存在 + if (File.Exists(fullStlPath)) + { + if (ValidateImages()) + { + ShowImages(); + return; + } + } + + // 2. 清理Unity相关目录 + CleanDirectory(_image_path); + CleanDirectory(_stl_path); + + // 3. 导出模型为STL + ExportModelsToStl(fullStlPath); + + // 4. 执行Unity渲染命令两次 + ExecuteUnityRendering(fileName, 0); // 渲染心图 + ExecuteUnityRendering(fileName, 1); // 渲染箭图 + + // 5. 验证图片是否生成 + if (ValidateImages()) + { + ShowImages(); + } + else + { + MessageBox.Show("生成失败,请重试。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private static string GenerateSignature(string modelInfo, string diamondCode) + { + // 模拟生成签名逻辑,可以自定义为更复杂的算法 + return modelInfo.GetHashCode() + "_" + diamondCode.GetHashCode(); + } + + private static void CleanDirectory(string path) + { + if (Directory.Exists(path)) + { + Directory.Delete(path, true); + } + Directory.CreateDirectory(path); + } + + private static void ExportModelsToStl(string path) + { + // 模拟模型导出逻辑 + // 调用ViewportHelperPro.ExportModelsToStl(null, path); + // Console.WriteLine("模型已导出为STL: " + path); + ViewportHelperPro.ExportModelsToStl(null, path); + } + + private static void ExecuteUnityRendering(string fileName, int type) + { + string arguments = $"--model={_stl_path + fileName} --output={_image_path} --type={type}"; + string exePath = Path.Combine(_unity_path, "RayTracing.exe"); + + if (!File.Exists(exePath)) + { + throw new FileNotFoundException("Unity渲染插件未找到", exePath); + } + + using (Process process = new Process()) + { + process.StartInfo.FileName = exePath; + process.StartInfo.Arguments = arguments; + process.StartInfo.UseShellExecute = false; + process.StartInfo.CreateNoWindow = true; + process.Start(); + process.WaitForExit(); + + if (process.ExitCode != 0) + { + throw new Exception("Unity渲染进程异常退出。"); + } + } + } + + private static bool ValidateImages() + { + string heartImage = Path.Combine(_image_path, "0.png"); + string arrowImage = Path.Combine(_image_path, "1.png"); + + return File.Exists(heartImage) && File.Exists(arrowImage); + } + + private static void ShowImages() + { + // 创建一个简单的图片展示窗口 + Form form = new Form + { + Text = "心箭图模拟渲染", + Width = 800, + Height = 600 + }; + + PictureBox heartBox = new PictureBox + { + ImageLocation = Path.Combine(_image_path, "0.png"), + SizeMode = PictureBoxSizeMode.Zoom, + Dock = DockStyle.Left, + Width = 400 + }; + + PictureBox arrowBox = new PictureBox + { + ImageLocation = Path.Combine(_image_path, "1.png"), + SizeMode = PictureBoxSizeMode.Zoom, + Dock = DockStyle.Right, + Width = 400 + }; + + form.Controls.Add(heartBox); + form.Controls.Add(arrowBox); + + Label label = new Label + { + Text = "心箭图为模拟渲染,实际请以切工镜观测为准。", + Dock = DockStyle.Bottom, + Height = 30, + TextAlign = System.Drawing.ContentAlignment.MiddleCenter + }; + + form.Controls.Add(label); + form.ShowDialog(); + } } \ No newline at end of file