轮胎外观检测添加思谋语义分割模型检测工具
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
using LB_VisionProcesses.Alogrithms;
using LB_VisionProcesses.Alogrithms.Halcon;
using LB_VisionProcesses.Processes;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace LB_VisionProcesses
{
    public partial class ProcessEditForm : Form
    {
        public UserControl userControl = new UserControl();
        IProcess OriProcess = null;
        string fullPath = string.Empty;
        public bool hasChanged = false;
 
        public ProcessEditForm(IProcess subject = null, string fullPath = "")
        {
            InitializeComponent();
            this.fullPath = fullPath;
            this.OriProcess = subject;
        }
 
        private void ProcessEditForm_Load(object sender, EventArgs e)
        {
            IProcess subject = OriProcess;
            if (subject != null)
            {
                // 一些定制化的Tool编辑控件额外操作
                //利用反射创建实例,命名规范:Tool+Edit,命名空间也要相同
                string ClassName = subject.GetType().FullName + "Edit";
                // 获取编辑控件类型
                Type type = Type.GetType(ClassName)
                    ?? Assembly.GetExecutingAssembly().GetTypes()
                              .FirstOrDefault(t => t.Name == ClassName);
 
                if (type != null)
                {
                    // 使用Activator.CreateInstance带参数创建
                    userControl = (UserControl)Activator.CreateInstance(type, subject);
 
                    if (userControl == null)
                        return;
                }
                else if (File.Exists(fullPath))
                {
                    try
                    {
                        this.Text = Path.GetFileNameWithoutExtension(fullPath);
                        this.fullPath = fullPath;
                        // 读取 JSON 文件内容
                        string jsonContent = this.Text;
                        if (File.Exists(fullPath))
                            jsonContent = File.ReadAllText(fullPath);
 
                        // 显示在 TextBox 上
                        panel.Controls.Clear();
                        panel.Controls.Add(this.textBox);
                        this.textBox.Dock = DockStyle.Fill;
                        this.textBox.Font = new Font("宋体", 12F, FontStyle.Regular, GraphicsUnit.Point, 134);
                        this.textBox.Multiline = true;
                        this.textBox.ScrollBars = ScrollBars.Both;
                        this.textBox.Text = jsonContent;
                    }
                    catch { }
                    return;
                }
                panel.Controls.Clear();
                panel.Controls.Add(userControl);
                userControl.Dock = DockStyle.Fill;
                OriProcess = subject;
                this.Text = subject.strProcessName;
 
            }
        }
 
        private void ProcessEditForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(fullPath) || OriProcess == null)
                {
                    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 && OriProcess != null)  //保存VPP
                {
                    //利用反射创建实例,命名规范:Tool+Edit
                    string ClassName = OriProcess.GetType().FullName + "Edit";
                    // 获取编辑控件类型
                    Type type = Type.GetType(ClassName)
                        ?? Assembly.GetExecutingAssembly().GetTypes()
                                  .FirstOrDefault(t => t.Name == ClassName);
 
                    if (type != null && userControl is TAlgorithmEdit)
                    {
                        ((TAlgorithmEdit)userControl).UpdataInputs();
                        (((TAlgorithmEdit)userControl).Subject).Save(Path.GetDirectoryName(fullPath));
                        OriProcess = ((TAlgorithmEdit)userControl).Subject;
                    }
                    else
                    {
                        //没有找到对应的编辑控件类型,直接保存JSON
                        // 获取 TextBox 中的内容
                        string jsonContent = this.textBox.Text;
                        if (!string.IsNullOrEmpty(jsonContent))
                        {
                            // 校验 JSON 格式是否正确
                            var obj = JsonConvert.DeserializeObject(jsonContent);
                            // 保存到文件,覆盖原文件
                            File.WriteAllText(fullPath, jsonContent);
                        }
                    }
 
                    hasChanged = true;
                    e.Cancel = false;  //确认关闭窗体
                    return;
                }
 
                if (OriProcess != null)
                {
                    if (userControl is TAlgorithmEdit edit)
                        edit.Dispose();
 
                    OriProcess.Dispose();
                }
 
                e.Cancel = false;  //确认关闭窗体
                return;
            }
            catch { }
            finally
            {
                if (OriProcess != null && !e.Cancel)
                {
                    userControl.Dispose();
                    OriProcess.Dispose();
                }
            }
        }
    }
}