diff --git a/Language/zh_CN.xaml b/Language/zh_CN.xaml index 02263e1..b024cf1 100644 --- a/Language/zh_CN.xaml +++ b/Language/zh_CN.xaml @@ -148,6 +148,7 @@ 保存成功 保存失败 选择路径 + 重置网格线 星辉钻石检测系统 diff --git a/ViewModel/Grading/DiamondSelectVM.cs b/ViewModel/Grading/DiamondSelectVM.cs index 9f44f4b..0e40784 100644 --- a/ViewModel/Grading/DiamondSelectVM.cs +++ b/ViewModel/Grading/DiamondSelectVM.cs @@ -21,6 +21,7 @@ using Newtonsoft.Json.Linq; using SparkClient.Model.Common; using Color = System.Windows.Media.Color; using MessageBox = SparkClient.Views.Dialog.MessageBox; +using System.Runtime.CompilerServices; namespace SparkClient.ViewModel.Grading; @@ -276,6 +277,11 @@ public class DiamondSelectVM : BaseViewModel }); } } + catch (DirectoryNotFoundException ex) + { + loading.Close(); + new MessageBox().Show(MultilingualHelper.getString("SavePathIsnotExists")); + } finally { } @@ -315,160 +321,172 @@ public class DiamondSelectVM : BaseViewModel /// public async void DoStartGrading(object param) { - if (param != null) - { - AlgorithmResultEntity parameter = new AlgorithmResultEntity(); - JsonImport jsonImport = new JsonImport(); - //jsonImport.FilePath.Text = "D:\\项目\\TEST240.json"; - bool? a = jsonImport.ShowDialog(); - if (a ?? false) + LoadingDialog loading = new LoadingDialog(); + try { + if (param != null) { - string fileName = jsonImport.FilePath.Text; - string[] lines = File.ReadAllLines(fileName); - StringBuilder stringBuilder = new StringBuilder(); + AlgorithmResultEntity parameter = new AlgorithmResultEntity(); + JsonImport jsonImport = new JsonImport(); + //jsonImport.FilePath.Text = "D:\\项目\\TEST240.json"; + bool? a = jsonImport.ShowDialog(); + if (a ?? false) + { + string fileName = jsonImport.FilePath.Text; + string[] lines = File.ReadAllLines(fileName); + StringBuilder stringBuilder = new StringBuilder(); - foreach (var line in lines) + foreach (var line in lines) + { + stringBuilder.Append(line); + } + parameter = JsonConvert.DeserializeObject(stringBuilder.ToString()); + } + else { - stringBuilder.Append(line); + return; } - parameter = JsonConvert.DeserializeObject(stringBuilder.ToString()); - } - else - { - return; - } - LoadingDialog loading = new LoadingDialog(); - var tcs = new TaskCompletionSource(); - var progressTask = Task.Run(() => Application.Current.Dispatcher.Invoke(() => { - loading.Closed += (s, e) => tcs.SetResult(true); - loading.ShowDialog(); - } - )); - await Task.Run(async () => - { - for (int i = 0; i <= 100; i++) + + var tcs = new TaskCompletionSource(); + var progressTask = Task.Run(() => Application.Current.Dispatcher.Invoke(() => { + loading.Closed += (s, e) => tcs.SetResult(true); + loading.ShowDialog(); + } + )); + await Task.Run(async () => { - // 模拟耗时操作 - //System.Threading.Thread.Sleep(50); // 休眠50毫秒 - await Task.Delay(5); - // 更新进度条的值(需要在UI线程上执行) - loading.Dispatcher.Invoke(() => + for (int i = 0; i <= 100; i++) { - loading.setValue(i); - }); + // 模拟耗时操作 + //System.Threading.Thread.Sleep(50); // 休眠50毫秒 + await Task.Delay(5); + // 更新进度条的值(需要在UI线程上执行) + loading.Dispatcher.Invoke(() => + { + loading.setValue(i); + }); - } - // SocResultEntity socResolt = new SocResultEntity(); - // AlgorithmResultEntity parameter = new AlgorithmResultEntity(); - // // 启动soc - // socResolt = DoSoc(); - // // 启动算法 - // parameter = DoAlgorithm(); + } + // SocResultEntity socResolt = new SocResultEntity(); + // AlgorithmResultEntity parameter = new AlgorithmResultEntity(); + // // 启动soc + // socResolt = DoSoc(); + // // 启动算法 + // parameter = DoAlgorithm(); - //parameter = DoSoc(); - parameter.Standard = getStandardName(); - string value = param.ToString() ?? ""; - if (value != null && value.Split(" ").Length == 3) - { - parameter.Shape = value.Split(" ")[0]; - parameter.CrownType = value.Split(" ")[1]; - parameter.PavType = value.Split(" ")[2]; - } - parameter.DiamondCode = DiamondCode; - GradingResult(parameter); - }); - await Task.Delay(5); + //parameter = DoSoc(); + parameter.Standard = getStandardName(); + string value = param.ToString() ?? ""; + if (value != null && value.Split(" ").Length == 3) + { + parameter.Shape = value.Split(" ")[0]; + parameter.CrownType = value.Split(" ")[1]; + parameter.PavType = value.Split(" ")[2]; + } + parameter.DiamondCode = DiamondCode; + GradingResult(parameter); + }); + await Task.Delay(5); + loading.Close(); + } + } + catch (DirectoryNotFoundException ex) + { loading.Close(); + new MessageBox().Show(MultilingualHelper.getString("SavePathIsnotExists")); + } + catch (Exception ex) + { + new MessageBox().Show($"{MultilingualHelper.getString("ApplicationError")}{ex.Message}"); + Logger.Error($"全局异常捕获:{ex.Message}", ex); } - } /// - /// 处理算法调用失败时的逻辑,包括保存图片到历史记录文件夹 - /// - /// 图片文件路径的 JSON 字符串 - /// - private static readonly ILog Logger = LogManager.GetLogger(typeof(AlgorithmServer)); - private void HandleAlgorithmFailure(string image_files) - { + /// 处理算法调用失败时的逻辑,包括保存图片到历史记录文件夹 + /// + /// 图片文件路径的 JSON 字符串 + /// + private static readonly ILog Logger = LogManager.GetLogger(typeof(AlgorithmServer)); + private void HandleAlgorithmFailure(string image_files) + { - // 从配置文件中读取 imageHistoryPath - // 定义历史记录文件夹路径 - string? imageHistoryPath = ConfigurationManager.AppSettings["ImageHistoryPath"]; - if (string.IsNullOrEmpty(imageHistoryPath)) - { - Logger.Error("ImageHistoryPath is not configured in AppSettings."); - return; - } + // 从配置文件中读取 imageHistoryPath + // 定义历史记录文件夹路径 + string? imageHistoryPath = ConfigurationManager.AppSettings["ImageHistoryPath"]; + if (string.IsNullOrEmpty(imageHistoryPath)) + { + Logger.Error("ImageHistoryPath is not configured in AppSettings."); + return; + } - // 生成时间戳,格式为 yyyyMMddHHmmss - string timestamp = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss"); - // 组合新的文件夹路径 - string newFolderPath = Path.Combine(imageHistoryPath, $"image-{timestamp}"); + // 生成时间戳,格式为 yyyyMMddHHmmss + string timestamp = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss"); + // 组合新的文件夹路径 + string newFolderPath = Path.Combine(imageHistoryPath, $"image-{timestamp}"); - // 检查 D 盘内存空间 - DriveInfo dDrive = new DriveInfo("D"); - long requiredSpace = 10L * 1024 * 1024 * 1024; // 10GB - if (dDrive.TotalFreeSpace < requiredSpace) + // 检查 D 盘内存空间 + DriveInfo dDrive = new DriveInfo("D"); + long requiredSpace = 10L * 1024 * 1024 * 1024; // 10GB + if (dDrive.TotalFreeSpace < requiredSpace) + { + // 如果 D 盘空间不足 10GB,删除最早创建的10个文件夹 + DirectoryInfo historyDir = new DirectoryInfo(imageHistoryPath); // 获取历史记录文件夹的信息 + DirectoryInfo[] subDirs = historyDir.GetDirectories(); // 获取历史记录文件夹中的所有子文件夹 + if (subDirs.Length > 0) { - // 如果 D 盘空间不足 10GB,删除最早创建的10个文件夹 - DirectoryInfo historyDir = new DirectoryInfo(imageHistoryPath); // 获取历史记录文件夹的信息 - DirectoryInfo[] subDirs = historyDir.GetDirectories(); // 获取历史记录文件夹中的所有子文件夹 - if (subDirs.Length > 0) - { - // 按创建时间排序子文件夹 - var orderedSubDirs = subDirs.OrderBy(d => d.CreationTime).ToList(); - int foldersToDelete = Math.Min(10, orderedSubDirs.Count); + // 按创建时间排序子文件夹 + var orderedSubDirs = subDirs.OrderBy(d => d.CreationTime).ToList(); + int foldersToDelete = Math.Min(10, orderedSubDirs.Count); - // 删除最早的 10 个文件夹,如果不够 10 个则全删掉 - for (int i = 0; i < foldersToDelete; i++) - { - orderedSubDirs[i].Delete(true); - } + // 删除最早的 10 个文件夹,如果不够 10 个则全删掉 + for (int i = 0; i < foldersToDelete; i++) + { + orderedSubDirs[i].Delete(true); } } + } - // 创建新文件夹 - Directory.CreateDirectory(newFolderPath); + // 创建新文件夹 + Directory.CreateDirectory(newFolderPath); - // 保存图片到新文件夹 - try - { - // 解析 image_files JSON 数组 - JArray imageFilesArray = JArray.Parse(image_files); - string[] imageFiles = imageFilesArray.Select(token => token.ToString()).ToArray(); - - string? imageBasePath = ConfigurationManager.AppSettings["ImageFileBasePath"]; // 图片根目录 - if (string.IsNullOrEmpty(imageBasePath)) - { - Logger.Error("ImageFileBasePath is not configured in AppSettings."); - return; - } + // 保存图片到新文件夹 + try + { + // 解析 image_files JSON 数组 + JArray imageFilesArray = JArray.Parse(image_files); + string[] imageFiles = imageFilesArray.Select(token => token.ToString()).ToArray(); - foreach (string imageFile in imageFiles) - { - // 获取文件名 - string fileName = Path.GetFileName(imageFile); - // 构建完整的源文件路径 - string sourcePath = Path.Combine(imageBasePath, imageFile); - // 组合目标路径 - string destinationPath = Path.Combine(newFolderPath, fileName); - // 复制文件到目标路径,如果目标文件已存在则覆盖 - File.Copy(sourcePath, destinationPath, true); - } - } - catch (JsonException ex) + string? imageBasePath = ConfigurationManager.AppSettings["ImageFileBasePath"]; // 图片根目录 + if (string.IsNullOrEmpty(imageBasePath)) { - // 记录日志或处理异常 - Logger.Error($"Error parsing image_files JSON: {ex.Message}"); - Logger.Error($"Stack Trace: {ex.StackTrace}"); + Logger.Error("ImageFileBasePath is not configured in AppSettings."); + return; } - catch (Exception ex) + + foreach (string imageFile in imageFiles) { - // 记录其他异常 - Logger.Error($"Error saving images: {ex.Message}"); - Logger.Error($"Stack Trace: {ex.StackTrace}"); + // 获取文件名 + string fileName = Path.GetFileName(imageFile); + // 构建完整的源文件路径 + string sourcePath = Path.Combine(imageBasePath, imageFile); + // 组合目标路径 + string destinationPath = Path.Combine(newFolderPath, fileName); + // 复制文件到目标路径,如果目标文件已存在则覆盖 + File.Copy(sourcePath, destinationPath, true); } } + catch (JsonException ex) + { + // 记录日志或处理异常 + Logger.Error($"Error parsing image_files JSON: {ex.Message}"); + Logger.Error($"Stack Trace: {ex.StackTrace}"); + } + catch (Exception ex) + { + // 记录其他异常 + Logger.Error($"Error saving images: {ex.Message}"); + Logger.Error($"Stack Trace: {ex.StackTrace}"); + } + } /// /// 跳转至检测结果 /// diff --git a/ViewModel/Grading/GradingResultVM.cs b/ViewModel/Grading/GradingResultVM.cs index 7531fa6..00401b3 100644 --- a/ViewModel/Grading/GradingResultVM.cs +++ b/ViewModel/Grading/GradingResultVM.cs @@ -190,8 +190,8 @@ public class GradingResultVM : BaseViewModel } catch (Exception ex) { - new MessageBox().Show($"{MultilingualHelper.getString("ApplicationError")}{ex.Message}"); Logger.Error($"全局异常捕获:{ex.Message}", ex); + throw ex; } } @@ -749,10 +749,11 @@ public class GradingResultVM : BaseViewModel } else { - if (!File.Exists(getFilePath())) + if (!Directory.Exists(getFilePath())) { - new MessageBox().Show(MultilingualHelper.getString("SavePathIsnotExists")); - return; + //new MessageBox().Show(MultilingualHelper.getString("SavePathIsnotExists")); + //return; + throw new DirectoryNotFoundException(getFilePath()); } string fullPath = Path.Combine(getFilePath(), this.DiamondCode); if (FileSaveEnabled("Txt")) @@ -796,7 +797,7 @@ public class GradingResultVM : BaseViewModel } private string getFilePath() { - string defultFilePath = "D://DTest//"; + string defultFilePath = ""; string sql = $"SELECT SETTING_P FROM SETTING WHERE SETTING_ID = 'FilePath'"; DataTable data = DataBaseHelper.ExecuteQuery(sql); if (data!= null && data.Rows.Count>0) @@ -813,7 +814,6 @@ public class GradingResultVM : BaseViewModel } catch (Exception ex) { - new MessageBox().Show(MultilingualHelper.getString("SavePathIsnotExists")); Logger.Error($"全局异常捕获:{ex.Message}", ex); } return defultFilePath; diff --git a/Views/UserControl/Viewport3D.xaml b/Views/UserControl/Viewport3D.xaml index 532b5eb..eba0fbc 100644 --- a/Views/UserControl/Viewport3D.xaml +++ b/Views/UserControl/Viewport3D.xaml @@ -72,6 +72,7 @@ + viewport.Items.Remove(item)); } private static Dictionary moveLines = new Dictionary(); + public static void InitMeshLines() + { + moveLines = new(); + } /// /// 网状参考线 /// @@ -1290,10 +1294,6 @@ public class ViewportHelperPro /// public static List ShowMeshLines(bool isCrown = true, double thickness = 0.2) { - if (isCrown) - { - moveLines = new(); - } List lines = new(); var Y = -0.01F; if (!isCrown) @@ -1438,7 +1438,7 @@ public class ViewportHelperPro // 计算直线的新位置 var transform = new TranslateTransform3D(projectedVector); var newTransform = new MatrixTransform3D(transform.Value * initialTransform.Value); - var newProjectedVector = new Vector3D(newTransform.Matrix.OffsetX ,Y, newTransform.Matrix.OffsetZ); + var newProjectedVector = new Vector3D(newTransform.Matrix.OffsetX ,0, newTransform.Matrix.OffsetZ); var newPosition1 = line.Geometry.Positions[0].ToVector3D(); newPosition1.X += newTransform.Matrix.OffsetX; newPosition1.Z += newTransform.Matrix.OffsetZ; diff --git a/Views/UserControl/ViewportData/Helper/ViewportManager.cs b/Views/UserControl/ViewportData/Helper/ViewportManager.cs index ab304b4..f33c2dc 100644 --- a/Views/UserControl/ViewportData/Helper/ViewportManager.cs +++ b/Views/UserControl/ViewportData/Helper/ViewportManager.cs @@ -1,3 +1,4 @@ +using System.Linq; using System.Text.Json.Nodes; using System.Windows.Controls; using System.Windows.Media.Media3D; @@ -178,9 +179,9 @@ public class ViewportManager } ChooseAddModels.Clear(); } - - public static void ResetMeshLines() + public static void InitMeshLines() { + ViewportHelperPro.InitMeshLines(); MainModelMeshLines_C.Clear(); MainModelMeshLines_C.AddRange(ViewportHelperPro.ShowMeshLines()); MainModelCircleLine_C = ViewportHelperPro.ShowCircleLine(); @@ -188,6 +189,25 @@ public class ViewportManager MainModelMeshLines_P.AddRange(ViewportHelperPro.ShowMeshLines(false)); MainModelCircleLine_P = ViewportHelperPro.ShowCircleLine(false); } + public static void ResetMeshLines() + { + if (_viewport.Items.Contains(MainModelMeshLines_C[0])) + { + ViewportManager.ShowMeshLines(false, true); + MainModelMeshLines_C.Clear(); + MainModelMeshLines_C.AddRange(ViewportHelperPro.ShowMeshLines()); + MainModelCircleLine_C = ViewportHelperPro.ShowCircleLine(); + ViewportManager.ShowMeshLines(true, true); + } + if (_viewport.Items.Contains(MainModelMeshLines_P[0])) + { + ViewportManager.ShowMeshLines(false, false); + MainModelMeshLines_P.Clear(); + MainModelMeshLines_P.AddRange(ViewportHelperPro.ShowMeshLines(false)); + MainModelCircleLine_P = ViewportHelperPro.ShowCircleLine(false); + ViewportManager.ShowMeshLines(true, false); + } + } /// /// 模拟面选中 /// @@ -321,7 +341,7 @@ public class ViewportManager _viewport.Camera.UpDirection = new Vector3D(0, -1, 0); _viewport.RenderHost.MSAA = MSAALevel.Maximum; // TODO: - ResetMeshLines(); + InitMeshLines(); } /// @@ -497,7 +517,6 @@ public class ViewportManager _viewport.Items.Remove(MainModelCircleLine_C); MainModelMeshLines_P.ForEach(e => _viewport.Items.Remove(e)); _viewport.Items.Remove(MainModelCircleLine_P); - ResetMeshLines(); } }