using LB_VisionProcesses.Alogrithms;
|
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+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 上
|
pnl_ProcessEditForm.Controls.Clear();
|
pnl_ProcessEditForm.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;
|
}
|
pnl_ProcessEditForm.Controls.Clear();
|
pnl_ProcessEditForm.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();
|
}
|
}
|
}
|
}
|
}
|