轮胎外观检测添加思谋语义分割模型检测工具
C3204
2026-03-30 06c627ec032b3f3876fd7db8a3ff0ff1a6614fa2
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
using Newtonsoft.Json;
using System.Data;
using System.Diagnostics;
 
namespace LB_SmartVision.Forms.Pages
{
    public partial class RunSettingPage : UserControl
    {
        ControlStateManager controlStateManager { get; set; }
 
        public RunSettingPage()
        {
            InitializeComponent();
            controlStateManager = new ControlStateManager(tableLayoutPanel);
            lstControls.Items.Clear();
            foreach (Control control in GetAllControls(tableLayoutPanel))
            {
                if (control is TextBox || control is CheckBox || control is ComboBox)
                {
                    if (!string.IsNullOrEmpty(control.Name))
                    {
                        lstControls.Items.Add(control.Name);
                    }
                }
            }
        }
 
        // 获取所有子控件(递归)
        private IEnumerable<Control> GetAllControls(Control control)
        {
            var controls = control.Controls.Cast<Control>();
            return controls.SelectMany(ctrl => GetAllControls(ctrl)).Concat(controls);
        }
 
        // 保存所有控件状态(递归处理容器控件)
        public void SaveControlsState(TableLayoutPanel tablePanel)
        {
            GlobalVar.ControlStates.Clear();
            SaveControlsRecursive(tablePanel);
        }
 
        private void SaveControlsRecursive(Control parentControl)
        {
            foreach (Control control in parentControl.Controls)
            {
                // 递归处理容器控件
                if (control.HasChildren && !(control is ComboBox)) // ComboBox有Children但不是容器控件
                {
                    SaveControlsRecursive(control);
                    continue;
                }
                string key = $"{control.Name}_{control.GetType().Name}";
                switch (control)
                {
                    case TextBox textBox:
                        {
                            GlobalVar.ControlStates[key] = textBox.Text;
                            break;
                        }
                    case CheckBox checkBox:
                        {
                            GlobalVar.ControlStates[key] = checkBox.Checked;
                            break;
                        }
                    case ComboBox comboBox:
                        {
                            var comboData = new
                            {
                                Items = comboBox.Items.Cast<object>().ToList(),
                                SelectedIndex = comboBox.SelectedIndex
                            };
                            GlobalVar.ControlStates[key] = comboData;
                            break;
                        }
                }
            }
        }
 
        // 加载控件状态(递归处理容器控件)
        public void LoadControlsState(TableLayoutPanel tablePanel)
        {
            LoadControlsRecursive(tablePanel);
        }
 
        private void LoadControlsRecursive(Control parentControl)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action(() => LoadControlsRecursive(parentControl)));
                return;
            }
            foreach (Control control in parentControl.Controls)
            {
                if (control.HasChildren && !(control is ComboBox))
                {
                    LoadControlsRecursive(control);
                    continue;
                }
 
                string key = $"{control.Name}_{control.GetType().Name}";
 
                if (!GlobalVar.ControlStates.TryGetValue(key, out object value)) continue;
 
                switch (control)
                {
                    case TextBox textBox when value is string text:
                        {
                            textBox.Text = text;
                            break;
                        }
                    case CheckBox checkBox when value is bool isChecked:
                        {
                            checkBox.Checked = isChecked;
                            break;
                        }
                    case ComboBox comboBox when value != null:
                        {
                            try
                            {
                                // 动态解析ComboBox数据
                                var json = JsonConvert.SerializeObject(value);
                                var comboData = JsonConvert.DeserializeAnonymousType(json, new
                                {
                                    Items = new List<object>(),
                                    SelectedIndex = 0
                                });
 
                                comboBox.BeginUpdate();
                                comboBox.Items.Clear();
                                comboBox.Items.AddRange(comboData.Items.ToArray());
 
                                if (comboData.SelectedIndex >= 0 && comboData.SelectedIndex < comboBox.Items.Count)
                                {
                                    comboBox.SelectedIndex = comboData.SelectedIndex;
                                }
                                comboBox.EndUpdate();
                            }
                            catch (Exception ex)
                            {
                                Debug.WriteLine($"加载ComboBox数据失败: {ex.Message}");
                            }
                            break;
                        }
                }
            }
        }
 
        // 保存到JSON文件
        public void SaveToJson(string filePath = "")
        {
            try
            {
                SaveControlsState(tableLayoutPanel);
                if (string.IsNullOrEmpty(filePath))
                {
                    filePath = GlobalVar.allRunSettingStringPath;
                }
                var settings = new JsonSerializerSettings
                {
                    Formatting = Formatting.Indented,
                    TypeNameHandling = TypeNameHandling.Auto // 处理多态类型
                };
 
                string json = JsonConvert.SerializeObject(GlobalVar.ControlStates, settings);
                File.WriteAllText(filePath, json);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"保存失败: {ex.Message}", "异常");
            }
        }
 
        // 从JSON文件加载
        public void LoadFromJson(string filePath = "")
        {
            try
            {
                if (string.IsNullOrEmpty(filePath))
                {
                    filePath = GlobalVar.allRunSettingStringPath;
                }
                if (!File.Exists(filePath))
                {
                    SaveToJson(filePath);
                    Debug.WriteLine($"文件不存在,已创建新文件: {filePath}");
                }
 
                string json = File.ReadAllText(filePath);
                var settings = new JsonSerializerSettings
                {
                    TypeNameHandling = TypeNameHandling.Auto // 处理多态类型
                };
 
                GlobalVar.ControlStates = JsonConvert.DeserializeObject<Dictionary<string, object>>(json, settings);
                LoadControlsState(tableLayoutPanel);
 
                if (txtImagePath.Text.Trim() == "" || string.IsNullOrEmpty(txtImagePath.Text))
                {
                    // 如果没有设置图片路径,则使用默认路径
                    txtImagePath.Text = GlobalVar.strApplicationPath;
                    SaveToJson(filePath);
                    LoadControlsState(tableLayoutPanel);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"加载失败: {ex.Message}", "异常");
            }
        }
 
        private void btnTest_Click(object sender, EventArgs e)
        {
            string searchControlName = txtTestSearch.Text;
            if (searchControlName.StartsWith("txt"))
            {
                // 假设是 TextBox
                TextBox textBox = controlStateManager.GetControl<TextBox>(searchControlName);
                if (textBox != null)
                {
                    MessageBox.Show($"找到控件: {textBox.Name}, 文本内容: {textBox.Text}");
                }
                else
                {
                    MessageBox.Show($"未找到控件: {searchControlName}", "异常");
                }
            }
            else if (searchControlName.StartsWith("ckb"))
            {
                // 假设是 CheckBox
                CheckBox checkBox = controlStateManager.GetControl<CheckBox>(searchControlName);
                if (checkBox != null)
                {
                    MessageBox.Show($"找到控件: {checkBox.Name}, 状态: {checkBox.Checked}");
                }
                else
                {
                    MessageBox.Show($"未找到控件: {searchControlName}", "异常");
                }
            }
            else if (searchControlName.StartsWith("cmb"))
            {
                // 假设是 ComboBox
                ComboBox comboBox = controlStateManager.GetControl<ComboBox>(searchControlName);
                if (comboBox != null)
                {
                    string selectedText = controlStateManager.GetComboBoxText(searchControlName);
                    MessageBox.Show($"找到控件: {comboBox.Name}, 选中项: {selectedText}");
                }
                else
                {
                    MessageBox.Show($"未找到控件: {searchControlName}", "异常");
                }
            }
            else
            {
                MessageBox.Show("请输入有效的控件名称前缀(txt、ckb、cmb)", "异常");
            }
        }
 
        private void btnSave_Click(object sender, EventArgs e)
        {
            SaveToJson();
            MessageBox.Show("控件状态已保存");
        }
 
        private void btnLoad_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
 
            // 设置文件对话框的属性
            openFileDialog.Multiselect = false; // 不允许多选
            // 设置文件过滤器,支持多种文件类型
            openFileDialog.Filter = "Ini Files (*.json)|*.json|All Files (*.*)|*.*";
            // 显示文件对话框
            DialogResult result = openFileDialog.ShowDialog();
 
            // 处理对话框返回结果
            if (result == DialogResult.OK)
            {
                // 获取用户选择的文件名
                string[] selectedFiles = openFileDialog.FileNames;
                if (selectedFiles.Length > 0)
                {
                    if (File.Exists(selectedFiles[0]))
                    {
                        LoadFromJson(selectedFiles[0]);
                        MessageBox.Show("控件状态已加载");
                    }
                }
            }
        }
 
        private void txtImagePath_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            // 创建文件夹选择对话框
            using (var folderDialog = new FolderBrowserDialog())
            {
                folderDialog.Description = "请选择文件夹";
                folderDialog.ShowNewFolderButton = true; // 允许创建新文件夹
                folderDialog.RootFolder = Environment.SpecialFolder.MyComputer; // 起始目录
 
                // 显示对话框
                DialogResult result = folderDialog.ShowDialog();
 
                if (result == DialogResult.OK && !string.IsNullOrWhiteSpace(folderDialog.SelectedPath))
                {
                    string selectedFolder = folderDialog.SelectedPath;
                    txtImagePath.Text = selectedFolder; // 设置文本框的值为选择的文件夹路径
                }
            }
        }
 
        ToolTip pathToolTip = new ToolTip()
        {
            ToolTipIcon = ToolTipIcon.Info, // 设置提示图标
            UseAnimation = true, // 启用动画效果
            UseFading = true // 启用淡入淡出效果
        };
        private void txtImagePath_MouseHover(object sender, EventArgs e)
        {
            pathToolTip.Show(txtImagePath.Text
                , txtImagePath
                , txtImagePath.Width / 2, txtImagePath.Height // 显示在控件底部中间
                , 1000); // 显示1秒
        }
 
        private void txtImageQuality_TextChanged(object sender, EventArgs e)
        {
            if (int.TryParse(txtImageQuality.Text, out int quality))
            {
                if (quality < 0 || quality > 100)
                {
                    MessageBox.Show("图像质量必须在0到100之间!", "异常");
                    txtImageQuality.Text = "100"; // 重置为默认值
                }
            }
            else
            {
                MessageBox.Show("请输入有效的数字!", "异常");
                txtImageQuality.Text = "100"; // 重置为默认值
            }
        }
    }
}