Repository navigation
Expand file tree
/
Copy pathMainViewModel.cs
More file actions
353 lines (305 loc) · 12.2 KB
/
Copy pathMainViewModel.cs
File metadata and controls
353 lines (305 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using Microsoft.Win32;
using PartMeasurement.Models;
using PartMeasurement.Services;
using VM.Core;
using VMControls.Interface;
namespace PartMeasurement.ViewModels
{
public class MainViewModel : BaseViewModel, IDisposable
{
private readonly VmService _vmService;
private readonly ExportService _exportService;
private readonly List<MeasurementResult> _history = new List<MeasurementResult>();
private volatile bool _isRunning;
public MainViewModel()
{
_vmService = new VmService();
_exportService = new ExportService();
_vmService.OnStatusChanged += OnVmStatusChanged;
LoadSolutionCommand = new RelayCommand(ExecuteLoadSolution);
RunMeasureCommand = new RelayCommand(ExecuteRunMeasure, _ => _vmService.IsSolutionLoaded && !_isRunning);
ExportCsvCommand = new RelayCommand(ExecuteExportCsv, _ => MeasurementResults.Count > 0);
ExportTxtCommand = new RelayCommand(ExecuteExportTxt, _ => MeasurementResults.Count > 0);
ClearResultsCommand = new RelayCommand(ExecuteClearResults, _ => MeasurementResults.Count > 0);
OpenHistoryCommand = new RelayCommand(ExecuteOpenHistory);
}
private void OnVmStatusChanged(string message)
{
var dispatcher = Application.Current?.Dispatcher;
if (dispatcher != null && !dispatcher.CheckAccess())
dispatcher.BeginInvoke(new Action(() => StatusMessage = message));
else
StatusMessage = message;
}
public ICommand LoadSolutionCommand { get; }
public ICommand RunMeasureCommand { get; }
public ICommand ExportCsvCommand { get; }
public ICommand ExportTxtCommand { get; }
public ICommand ClearResultsCommand { get; }
public ICommand OpenHistoryCommand { get; }
private string _statusMessage = "就绪";
public string StatusMessage
{
get => _statusMessage;
set => SetProperty(ref _statusMessage, value);
}
private string _solutionName = string.Empty;
public string SolutionName
{
get => _solutionName;
set => SetProperty(ref _solutionName, value);
}
private string _saveDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
public string SaveDirectory
{
get => _saveDirectory;
set => SetProperty(ref _saveDirectory, value);
}
private string _imageName = string.Empty;
public string ImageName
{
get => _imageName;
set => SetProperty(ref _imageName, value);
}
private int _measureCount;
public int MeasureCount
{
get => _measureCount;
set => SetProperty(ref _measureCount, value);
}
private int _passCount;
public int PassCount
{
get => _passCount;
set => SetProperty(ref _passCount, value);
}
private int _failCount;
public int FailCount
{
get => _failCount;
set => SetProperty(ref _failCount, value);
}
private IVmModule _renderModuleSource;
public IVmModule RenderModuleSource
{
get => _renderModuleSource;
set => SetProperty(ref _renderModuleSource, value);
}
public ObservableCollection<MeasurementResult> MeasurementResults { get; }
= new ObservableCollection<MeasurementResult>();
public ObservableCollection<MeasureItem> CurrentMeasureItems { get; }
= new ObservableCollection<MeasureItem>();
public List<MeasurementResult> GetHistory()
{
return new List<MeasurementResult>(_history);
}
private void ExecuteLoadSolution(object parameter)
{
var dialog = new OpenFileDialog
{
Title = "选择VisionMaster方案文件",
Filter = "VM方案文件|*.sol",
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
};
if (dialog.ShowDialog() == true)
{
if (_vmService.LoadSolution(dialog.FileName))
{
SolutionName = Path.GetFileNameWithoutExtension(dialog.FileName);
var renderModule = _vmService.GetProcedure("流程1");
if (renderModule != null)
{
RenderModuleSource = renderModule;
}
CommandManager.InvalidateRequerySuggested();
}
else
{
MessageBox.Show("方案加载失败,请检查文件路径和VM SDK环境", "错误",
MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
private async void ExecuteRunMeasure(object parameter)
{
if (_isRunning) return;
_isRunning = true;
CommandManager.InvalidateRequerySuggested();
try
{
StatusMessage = "正在执行测量...";
var success = await Task.Run(() => _vmService.Run());
if (!success)
{
StatusMessage = "测量执行失败";
return;
}
var procedure = _vmService.GetProcedure("流程1");
var result = new MeasurementResult
{
MeasureTime = DateTime.Now,
SolutionName = SolutionName,
ImageName = string.IsNullOrEmpty(ImageName)
? $"Image_{DateTime.Now:yyyyMMdd_HHmmss}"
: ImageName
};
if (procedure != null)
{
RenderModuleSource = procedure;
ExtractMeasureItems(procedure, result);
}
result.IsPassed = result.Items.Count > 0 && result.Items.All(i => i.IsPassed);
MeasurementResults.Insert(0, result);
_history.Add(result);
UpdateStatistics();
CurrentMeasureItems.Clear();
foreach (var item in result.Items)
{
CurrentMeasureItems.Add(item);
}
StatusMessage = result.IsPassed
? $"测量完成 - 判定: 合格 (OK) - 共{result.Items.Count}项"
: $"测量完成 - 判定: 不合格 (NG) - 共{result.Items.Count}项";
CommandManager.InvalidateRequerySuggested();
}
catch (Exception ex)
{
StatusMessage = $"测量异常: {ex.Message}";
MessageBox.Show($"执行测量失败: {ex.Message}", "错误",
MessageBoxButton.OK, MessageBoxImage.Error);
}
finally
{
_isRunning = false;
CommandManager.InvalidateRequerySuggested();
}
}
private void ExtractMeasureItems(VmProcedure procedure, MeasurementResult result)
{
try
{
var outputInfos = procedure.ModuResult.GetAllOutputNameInfo();
if (outputInfos == null)
return;
foreach (var info in outputInfos)
{
var typeName = info.TypeName.ToString();
if (typeName.Contains("点") || typeName.Contains("图像") ||
typeName.Contains("Point") || typeName.Contains("Image"))
continue;
try
{
var valueFloat = procedure.ModuResult.GetOutputFloat(info.Name);
var valueStr = valueFloat.ToString();
if (string.IsNullOrEmpty(valueStr))
continue;
if (double.TryParse(valueStr, out double value))
{
var item = new MeasureItem
{
Name = info.Name,
Value = value,
NominalValue = value,
UpperTolerance = 0.01,
LowerTolerance = 0.01
};
if (info.Name.Contains("直径") || info.Name.Contains("距离"))
{
item.Unit = "mm";
}
else if (info.Name.Contains("角度"))
{
item.Unit = "°";
}
else
{
item.Unit = "mm";
}
result.Items.Add(item);
}
}
catch
{
}
}
}
catch (Exception ex)
{
StatusMessage = $"提取测量结果时发生错误: {ex.Message}";
}
}
private void UpdateStatistics()
{
MeasureCount = _history.Count;
PassCount = _history.Count(r => r.IsPassed);
FailCount = _history.Count(r => !r.IsPassed);
}
private void ExecuteExportCsv(object parameter)
{
var dialog = new SaveFileDialog
{
Title = "导出CSV文件",
Filter = "CSV文件|*.csv",
FileName = $"测量结果_{DateTime.Now:yyyyMMdd_HHmmss}.csv",
InitialDirectory = SaveDirectory
};
if (dialog.ShowDialog() == true)
{
_exportService.ExportToCsv(dialog.FileName, MeasurementResults.ToList());
StatusMessage = $"CSV导出成功: {dialog.FileName}";
MessageBox.Show($"数据已成功导出到:\n{dialog.FileName}", "导出成功",
MessageBoxButton.OK, MessageBoxImage.Information);
}
}
private void ExecuteExportTxt(object parameter)
{
var dialog = new SaveFileDialog
{
Title = "导出测量报告",
Filter = "文本文件|*.txt",
FileName = $"测量报告_{DateTime.Now:yyyyMMdd_HHmmss}.txt",
InitialDirectory = SaveDirectory
};
if (dialog.ShowDialog() == true)
{
_exportService.ExportToTxt(dialog.FileName, MeasurementResults.ToList());
StatusMessage = $"报告导出成功: {dialog.FileName}";
MessageBox.Show($"报告已成功导出到:\n{dialog.FileName}", "导出成功",
MessageBoxButton.OK, MessageBoxImage.Information);
}
}
private void ExecuteClearResults(object parameter)
{
if (MessageBox.Show("确定要清除当前所有测量结果吗?", "确认",
MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
{
MeasurementResults.Clear();
CurrentMeasureItems.Clear();
MeasureCount = 0;
PassCount = 0;
FailCount = 0;
StatusMessage = "测量结果已清除";
CommandManager.InvalidateRequerySuggested();
}
}
private void ExecuteOpenHistory(object parameter)
{
var historyWindow = new Views.HistoryWindow(_history);
historyWindow.Owner = Application.Current.MainWindow;
historyWindow.ShowDialog();
}
public void Dispose()
{
_vmService.OnStatusChanged -= OnVmStatusChanged;
_vmService.Dispose();
}
}
}