Bladeren bron

增加视觉动态精度分析仪参数配置的持久化

引入配置文件加载与保存功能,支持在对话框打开时自动加载参数(点位A/B、选中流程、机器人速度、测试次数等),关闭时自动保存。新增配置数据模型,采用 JSON 序列化,提升用户体验,实现参数持久化。
孝锋 徐 7 maanden geleden
bovenliggende
commit
1dd951baa3
1 gewijzigde bestanden met toevoegingen van 136 en 4 verwijderingen
  1. 136 4
      TeamAAS-VM/ViewModels/Product/VisionDynamicAccuracyAnalyzerViewModel.cs

+ 136 - 4
TeamAAS-VM/ViewModels/Product/VisionDynamicAccuracyAnalyzerViewModel.cs

@@ -1,6 +1,7 @@
 using Cognex.VisionPro;
 using Cognex.VisionPro;
 using Cognex.VisionPro.ToolBlock;
 using Cognex.VisionPro.ToolBlock;
 using MaterialDesignThemes.Wpf;
 using MaterialDesignThemes.Wpf;
+using Newtonsoft.Json;
 using OxyPlot;
 using OxyPlot;
 using Prism.Commands;
 using Prism.Commands;
 using Prism.Events;
 using Prism.Events;
@@ -1390,14 +1391,13 @@ namespace TeamAAS_VP.ViewModels.Product
         public void OnDialogClosed()
         public void OnDialogClosed()
         {
         {
             _cts?.Cancel();
             _cts?.Cancel();
+            SaveConfig();
         }
         }
 
 
         public void OnDialogOpened(IDialogParameters parameters)
         public void OnDialogOpened(IDialogParameters parameters)
         {
         {
             SelectProduct = parameters.GetValue<ProductModel>("SelectProduct");
             SelectProduct = parameters.GetValue<ProductModel>("SelectProduct");
             Robot = parameters.GetValue<IRobot>("Robot");
             Robot = parameters.GetValue<IRobot>("Robot");
-            //Camera = _cameraService.GetCamera(SelectProcedure.CameraId);
-            //VisionTool = parameters.GetValue<CogToolBlock>("ToolBlock");
             ProcedureModels = new ObservableCollection<ProcedureModel>();
             ProcedureModels = new ObservableCollection<ProcedureModel>();
             foreach (var item in SelectProduct.CameraProcedures)
             foreach (var item in SelectProduct.CameraProcedures)
             {
             {
@@ -1406,10 +1406,142 @@ namespace TeamAAS_VP.ViewModels.Product
                     ProcedureModels.Add(item1);
                     ProcedureModels.Add(item1);
                 }
                 }
             }
             }
-            //VisionTool.Run();
-            //TestResult = CreateResultDataTable(VisionTool.Outputs);
+            LoadConfig();
         }
         }
         #endregion
         #endregion
+
+        /// <summary>
+        /// 加载配置
+        /// </summary>
+        private void LoadConfig()
+        {
+            //配置文件路径:FilePath.ProductsPath//productName//VisionDynamicAccuracyAnalyzerConfig.json
+            string configPath = System.IO.Path.Combine(FilePath.ProductsPath, SelectProduct.Name, "VisionDynamicAccuracyAnalyzerConfig.json");
+            if (System.IO.File.Exists(configPath))
+            {
+                try
+                {
+                    string json = System.IO.File.ReadAllText(configPath);
+                    var ConfigModel = JsonConvert.DeserializeObject<VisionDynamicAccuracyAnalyzerConfigModel>(json);
+                    //加载点位A、B
+                    PointA = ConfigModel.PointA;
+                    PointB = ConfigModel.PointB;
+                    //加载选中视觉流程
+                    var procedure = ProcedureModels.FirstOrDefault(p => p.Id == ConfigModel.SelectProcedureId);
+                    if (procedure != null)
+                    {
+                        SelectProcedure = procedure;
+                    }
+                    MoveSpeed = ConfigModel.RobotMoveSpeed;
+                    RepeatCount = ConfigModel.TestCount;
+                }
+                catch (Exception ex)
+                {
+                    LogHelper.WriteLogError("加载视觉动态精度分析仪配置时出错", ex);
+                }
+            }
+        }
+
+        /// <summary>
+        /// 保存配置
+        /// </summary>
+        private void SaveConfig()
+        {
+            try
+            {
+                //配置文件路径:FilePath.ProductsPath//productName//VisionDynamicAccuracyAnalyzerConfig.json
+                string configPath = System.IO.Path.Combine(FilePath.ProductsPath, SelectProduct.Name, "VisionDynamicAccuracyAnalyzerConfig.json");
+                var ConfigModel = new VisionDynamicAccuracyAnalyzerConfigModel
+                {
+                    PointA = PointA,
+                    PointB = PointB,
+                    SelectProcedureId = SelectProcedure?.Id ?? Guid.Empty,
+                    SelectProcedureName = SelectProcedure?.Name ?? "",
+                    RobotMoveSpeed = MoveSpeed,
+                    TestCount = RepeatCount
+                };
+                //如果配置文件夹不存在则创建
+                var configDir = System.IO.Path.GetDirectoryName(configPath);
+                if (!Directory.Exists(configDir))
+                {
+                     Directory.CreateDirectory(configDir);
+                }
+
+                string json = JsonConvert.SerializeObject(ConfigModel, Formatting.Indented);
+                System.IO.File.WriteAllText(configPath, json);
+            }
+            catch (Exception ex)
+            {
+                LogHelper.WriteLogError("保存视觉动态精度分析仪配置时出错", ex);
+            }
+        }
+
+        //配置参数模型
+        public class VisionDynamicAccuracyAnalyzerConfigModel : BindableBase
+        {
+            private int _RobotMoveSpeed = 100;
+            /// <summary>
+            /// 机器人移动速度
+            /// </summary>
+            public int RobotMoveSpeed
+            {
+                get { return _RobotMoveSpeed; }
+                set { SetProperty(ref _RobotMoveSpeed, value); }
+            }
+            private int _TestCount = 10;
+            /// <summary>
+            /// 测试次数
+            /// </summary>
+            public int TestCount
+            {
+                get { return _TestCount; }
+                set { SetProperty(ref _TestCount, value); }
+            }
+
+            //点位A
+            private RPoint _PointA;
+            /// <summary>
+            /// 点位A
+            /// </summary>
+            public RPoint PointA
+            {
+                get { return _PointA; }
+                set { SetProperty(ref _PointA, value); }
+            }
+
+            //点位B
+            private RPoint _PointB;
+            /// <summary>
+            /// 点位B
+            /// </summary>
+            public RPoint PointB
+            {
+                get { return _PointB; }
+                set { SetProperty(ref _PointB, value); }
+            }
+
+            private Guid _SelectProcedureId;
+            /// <summary>
+            /// 选中的视觉流程ID
+            /// </summary>
+            public Guid SelectProcedureId
+            {
+                get { return _SelectProcedureId; }
+                set { SetProperty(ref _SelectProcedureId, value); }
+            }
+
+            private string _SelectProcedureName;
+            /// <summary>
+            /// 选中的视觉流程Name
+            /// </summary>
+            public string SelectProcedureName
+            {
+                get { return _SelectProcedureName; }
+                set { SetProperty(ref _SelectProcedureName, value); }
+            }
+
+        }
+
     }
     }
 
 
     public class DynamicTestResult : BindableBase
     public class DynamicTestResult : BindableBase