轮胎外观检测添加思谋语义分割模型检测工具
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
using LB_VisionProcesses.Forms;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace LB_VisionProcesses.Forms
{
    public partial class GlobalVarForm : Form
    {
        public bool isSave = false;
        private string fullPath = string.Empty;
        public GlobalVarForm(string path = "")
        {
            InitializeComponent();
            fullPath = path;
        }
        private void GlobalVarForm_Load(object sender, EventArgs e)
        {
            var sortedKeys = IProcess.dicGlobalVars.Keys
                .OrderBy(k => k, new NaturalStringComparer())
                .ToList();
            int index = 0;
            foreach (var key in sortedKeys)
            {
                GlobalVarControl control = new GlobalVarControl(key, IProcess.dicGlobalVars[key]);
                control.Size = new Size(this.controlsPanel.Size.Width, control.Size.Height);
                control.Location = new Point(0, control.Size.Height * index);
                this.controlsPanel.Controls.Add(control);
                index++;
            }
        }
        private void controlsPanel_SizeChanged(object sender, EventArgs e)
        {
            int index = 0;
            foreach (var control in controlsPanel.Controls)
            {
                if (control is GlobalVarControl globalVarControl)
                {
                    globalVarControl.Size = new Size(this.controlsPanel.Size.Width, globalVarControl.Size.Height);
                    globalVarControl.Location = new Point(0, globalVarControl.Size.Height * index);
                    index++;
                }
            }
        }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                string name = "全局变量";
                while (IProcess.dicGlobalVars.ContainsKey(name))
                    name += "(副本)";
                GlobalVarControl control = new GlobalVarControl(name);
                control.Size = new Size(this.controlsPanel.Size.Width, control.Size.Height);
                control.Location = new Point(0, control.Size.Height * IProcess.dicGlobalVars.Count);
                this.controlsPanel.Controls.Add(control);
                IProcess.dicGlobalVars.TryAdd(name, "");
            }
            catch (Exception ex) { Debug.WriteLine($"btnAdd_Click失败:{ex.Message}【{ex.StackTrace}】"); }
        }
        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                List<GlobalVarControl> removeControls = new List<GlobalVarControl>();
                foreach (var control in controlsPanel.Controls)
                {
                    if (control is GlobalVarControl globalVarControl && globalVarControl.isSelected)
                        removeControls.Add(globalVarControl);
                }
                foreach (var control in removeControls)
                    controlsPanel.Controls.Remove(control);
            }
            catch (Exception ex) { Debug.WriteLine($"btnDelete_Click失败:{ex.Message}【{ex.StackTrace}】"); }
        }
        private void GlobalVarForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(fullPath))
                {
                    e.Cancel = false;  //确认关闭窗体
                    return;
                }
                DialogResult res = MessageBox.Show("是否保存?", "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);  //保存结果信息
                /// 参数1:显示文本,参数2:标题,参数3:按键类型,参数4:显示图标
                if (res != DialogResult.Yes && res != DialogResult.No)  //取消
                {
                    e.Cancel = true;  //取消关闭窗体
                    return;
                }
                if (res == DialogResult.Yes)  //保存
                {
                    IProcess.dicGlobalVars.Clear();
                    foreach (var control in controlsPanel.Controls)
                    {
                        if (control is GlobalVarControl globalVarControl)
                            IProcess.dicGlobalVars.AddOrUpdate(globalVarControl.Key, globalVarControl.Value
                                , (key, oldValue) => globalVarControl.Value);
                    }
                    string strJson = string.Empty;
                    var settings = new JsonSerializerSettings
                    {
                        Formatting = Newtonsoft.Json.Formatting.Indented,
                        // 自定义缩进(4空格)
                        ContractResolver = new DefaultContractResolver
                        {
                            NamingStrategy = new CamelCaseNamingStrategy()
                        }
                    };
                    strJson = JsonConvert.SerializeObject(IProcess.dicGlobalVars, settings);
                    string filePath = Path.GetDirectoryName(fullPath);
                    //判断文件夹是否存在,防呆输入为文件名称
                    if (!Directory.Exists(filePath))
                    {
                        try
                        {
                            Directory.CreateDirectory(filePath);
                        }
                        catch (Exception)
                        { }
                    }
                    File.WriteAllText(fullPath, strJson, Encoding.UTF8);
                    e.Cancel = false;  //确认关闭窗体
                    return;
                }
                e.Cancel = false;  //确认关闭窗体
                return;
            }
            catch { }
            finally { }
        }
    }
}