DHDHSoftware/DHSoftware/Views/DetectControl.cs

660 lines
27 KiB
C#

using System.ComponentModel;
using System.Data;
using System.Reflection;
using AntdUI;
using DH.Commons.Base;
using DH.Commons.Enums;
using DH.Devices.PLC;
using XKRS.CanFly;
using static System.Windows.Forms.AxHost;
using static DH.Commons.Enums.EnumHelper;
namespace DHSoftware.Views
{
public partial class DetectControl : UserControl
{
Window window;
DetectionConfig detectionConfig;
public DetectControl(Window _window,DetectionConfig _detection)
{
window = _window;
detectionConfig = _detection;
InitializeComponent();
//初始化表格列头
InitTableColumns();
InitData();
BindEventHandler();
}
private void BindEventHandler()
{
//预处理
btnPrePath.Click += btnPath_Click;
btnPreAdd.Click += BtnPreAdd_Click; ;
btnPreDelete.Click += BtnPreDelete_Click; ;
preTable.CellClick += PreTable_CellClick; ;
preTable.CellButtonClick += PreTable_CellButtonClick;
btnOutAdd.Click += BtnOutAdd_Click; ;
btnOutDelete.Click += BtnOutDelete_Click; ;
PreOutTable.CellButtonClick += PreOutTable_CellButtonClick; ;
//中处理
btnPath2.Click += BtnPath2_Click;
btnCorrelatedCamera.Click += BtnCorrelatedCamera_Click;
btnPic.Click += BtnPic_Click;
btnLableAdd.Click += BtnLableAdd_Click;
btnLableDelete.Click += BtnLableDelete_Click;
lableTable.CellButtonClick += LableTable_CellButtonClick;
btnSizeAdd.Click += BtnSizeAdd_Click;
btnSizeDel.Click += BtnSizeDelete_Click;
SizeTable.CellButtonClick += SizeTable_CellButtonClick;
}
private void LableTable_CellButtonClick(object sender, TableButtonEventArgs e)
{
var buttontext = e.Btn.Text;
if (e.Record is DetectionLable DetectionLable)
{
switch (buttontext)
{
//暂不支持进入整行编辑,只支持指定单元格编辑,推荐使用弹窗或抽屉编辑整行数据
case "编辑":
var form = new DetectionLableEdit(window, DetectionLable) { Size = new Size(500, 300) };
AntdUI.Drawer.open(new AntdUI.Drawer.Config(window, form)
{
OnLoad = () =>
{
AntdUI.Message.info(window, "进入编辑", autoClose: 1);
},
OnClose = () =>
{
AntdUI.Message.info(window, "结束编辑", autoClose: 1);
}
});
break;
case "删除":
var result = Modal.open(window, "删除警告!", "确认要删除选择的数据吗?", TType.Warn);
if (result == DialogResult.OK)
detectionConfig.DetectionLableList.Remove(DetectionLable);
break;
}
}
}
private void SizeTable_CellButtonClick(object sender, TableButtonEventArgs e)
{
var buttontext = e.Btn.Text;
if (e.Record is SizeTreatParam sizeTreat)
{
switch (buttontext)
{
//暂不支持进入整行编辑,只支持指定单元格编辑,推荐使用弹窗或抽屉编辑整行数据
case "编辑":
var form = new SizeLabelEdit(window, sizeTreat) { Size = new Size(500, 300) };
AntdUI.Drawer.open(new AntdUI.Drawer.Config(window, form)
{
OnLoad = () =>
{
AntdUI.Message.info(window, "进入编辑", autoClose: 1);
},
OnClose = () =>
{
AntdUI.Message.info(window, "结束编辑", autoClose: 1);
}
});
break;
case "删除":
var result = Modal.open(window, "删除警告!", "确认要删除选择的数据吗?", TType.Warn);
if (result == DialogResult.OK)
detectionConfig.SizeTreatParamList.Remove(sizeTreat);
break;
case "进行测量":
var sizeType = ((int)sizeTreat.PreType).ToString();
// 根据测量类型打开不同的窗口
switch (sizeType)
{
case "1":
case "2":
case "3":
case "4":
case "5":
FrmMain3 frmMain3 = new FrmMain3(sizeType);
frmMain3.ShowDialog();
if (!string.IsNullOrEmpty(frmMain3.inputtext))
{
sizeTreat.ResultShow = frmMain3.inputtext;
}
if (!string.IsNullOrEmpty(frmMain3.outtext))
{
sizeTreat.OutResultShow = frmMain3.outtext;
}
break;
default:
MessageBox.Show("未定义的测量类型!");
break;
}
//使用clone可以防止table中的image被修改
//Preview.open(new Preview.Config(window, (Image)SizeParamLable.CellImages[0].Image.Clone()));
break;
}
}
}
private void BtnLableDelete_Click(object? sender, EventArgs e)
{
if (detectionConfig.DetectionLableList.Count == 0 || !detectionConfig.DetectionLableList.Any(x => x.Selected))
{
AntdUI.Message.warn(window, "请选择要删除的行!", autoClose: 3);
return;
}
var result = Modal.open(window, "删除警告!", "确认要删除选择的数据吗?", TType.Warn);
if (result == DialogResult.OK)
{
// 使用反转for循环删除主列表中选中的项
for (int i = detectionConfig.DetectionLableList.Count - 1; i >= 0; i--)
{
// 删除选中的主列表项
if (detectionConfig.DetectionLableList[i].Selected)
{
detectionConfig.DetectionLableList.RemoveAt(i);
}
}
// 提示删除完成
AntdUI.Message.success(window, "删除成功!", autoClose: 3);
}
}
private void BtnLableAdd_Click(object? sender, EventArgs e)
{
DetectionLable detectionLable = new DetectionLable()
{
CellLinks = new CellLink[] {
new CellButton(Guid.NewGuid().ToString(),"编辑",TTypeMini.Primary),
new CellButton(Guid.NewGuid().ToString(),"删除",TTypeMini.Error),
}
};
var form = new DetectionLableEdit(window, detectionLable) { Size = new Size(450, 500) };
AntdUI.Modal.open(new AntdUI.Modal.Config(window, "", form, TType.None)
{
BtnHeight = 0,
});
if (form.submit)
{
detectionConfig.DetectionLableList.Add(detectionLable);
}
}
private void BtnSizeDelete_Click(object? sender, EventArgs e)
{
if (detectionConfig.SizeTreatParamList.Count == 0 || !detectionConfig.SizeTreatParamList.Any(x => x.Selected))
{
AntdUI.Message.warn(window, "请选择要删除的行!", autoClose: 3);
return;
}
var result = Modal.open(window, "删除警告!", "确认要删除选择的数据吗?", TType.Warn);
if (result == DialogResult.OK)
{
// 使用反转for循环删除主列表中选中的项
for (int i = detectionConfig.SizeTreatParamList.Count - 1; i >= 0; i--)
{
// 删除选中的主列表项
if (detectionConfig.SizeTreatParamList[i].Selected)
{
detectionConfig.SizeTreatParamList.RemoveAt(i);
}
}
// 提示删除完成
AntdUI.Message.success(window, "删除成功!", autoClose: 3);
}
}
private void BtnSizeAdd_Click(object? sender, EventArgs e)
{
SizeTreatParam SizeParamLable = new SizeTreatParam()
{
//CellBadge = new CellBadge(SizeEnum.Circle.GetEnumDescription()),
CellLinks = new CellLink[] {
new CellButton(Guid.NewGuid().ToString(),"编辑",TTypeMini.Primary),
new CellButton(Guid.NewGuid().ToString(),"删除",TTypeMini.Error),
new CellButton(Guid.NewGuid().ToString(),"进行测量",TTypeMini.Primary)
}
};
var form = new SizeLabelEdit(window, SizeParamLable) { Size = new Size(450, 500) };
AntdUI.Modal.open(new AntdUI.Modal.Config(window, "", form, TType.None)
{
BtnHeight = 0,
});
if (form.submit)
{
detectionConfig.SizeTreatParamList.Add(SizeParamLable);
}
}
private void BtnPic_Click(object? sender, EventArgs e)
{
}
private void BtnCorrelatedCamera_Click(object? sender, EventArgs e)
{
var form = new CorrelatedCameraEdit(window, detectionConfig.CameraCollects) { Size = new Size(500, 400) };
AntdUI.Modal.open(new AntdUI.Modal.Config(window, "", form, TType.None)
{
BtnHeight = 0,
});
if (form.submit)
{
flowCameraPanel.Controls.Clear();
if (detectionConfig.CameraCollects.Count > 0)
{
foreach (var item in detectionConfig.CameraCollects)
{
var control = new AntdUI.Tag()
{
Font = new System.Drawing.Font("Microsoft YaHei UI", 9F),
Size = new Size(90, 42),
Text = item.CameraSourceId,
CloseIcon = true
};
control.CloseChanged += (sender, e) =>
{
var tag = sender as Tag;
foreach (var item in detectionConfig.CameraCollects)
{
if (item.CameraSourceId.Equals(tag.Text))
{
detectionConfig.CameraCollects.Remove(item);
break;
}
}
return true;
};
// 通过主窗口设置DPI控制添加控件保持缩放比例
window.AutoDpi(control);
flowCameraPanel.Controls.Add(control);
control.BringToFront();
}
}
}
}
private bool Control_CloseChanged(object sender, EventArgs e)
{
throw new NotImplementedException();
}
private void BtnPath2_Click(object? sender, EventArgs e)
{
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
// 设置对话框标题
openFileDialog.Title = "选择中处理文件";
// 限制文件后缀为 .onnx、trt
openFileDialog.Filter = "中处理文件 (*.onnx, *.trt)|*.onnx;*.trt";
// 禁止多选
openFileDialog.Multiselect = false;
// 显示对话框并等待用户操作
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
string filePath = openFileDialog.FileName;
iptDetectPath.Text = filePath;
}
}
}
private void PreOutTable_CellButtonClick(object sender, TableButtonEventArgs e)
{
var buttontext = e.Btn.Text;
if (e.Record is PreTreatParam PreTreat)
{
switch (buttontext)
{
//暂不支持进入整行编辑,只支持指定单元格编辑,推荐使用弹窗或抽屉编辑整行数据
case "编辑":
var form = new PreTreatEdit(window, PreTreat) { Size = new Size(500, 300) };
AntdUI.Drawer.open(new AntdUI.Drawer.Config(window, form)
{
OnLoad = () =>
{
AntdUI.Message.info(window, "进入编辑", autoClose: 1);
},
OnClose = () =>
{
AntdUI.Message.info(window, "结束编辑", autoClose: 1);
}
});
break;
case "删除":
var result = Modal.open(window, "删除警告!", "确认要删除选择的数据吗?", TType.Warn);
if (result == DialogResult.OK)
detectionConfig.OUTPreTreatParams.Remove(PreTreat);
break;
}
}
}
private void BtnOutDelete_Click(object? sender, EventArgs e)
{
if (detectionConfig.OUTPreTreatParams.Count == 0 || !detectionConfig.OUTPreTreatParams.Any(x => x.Selected))
{
AntdUI.Message.warn(window, "请选择要删除的行!", autoClose: 3);
return;
}
var result = Modal.open(window, "删除警告!", "确认要删除选择的数据吗?", TType.Warn);
if (result == DialogResult.OK)
{
// 使用反转for循环删除主列表中选中的项
for (int i = detectionConfig.OUTPreTreatParams.Count - 1; i >= 0; i--)
{
// 删除选中的主列表项
if (detectionConfig.OUTPreTreatParams[i].Selected)
{
detectionConfig.OUTPreTreatParams.RemoveAt(i);
}
}
// 提示删除完成
AntdUI.Message.success(window, "删除成功!", autoClose: 3);
}
}
private void BtnOutAdd_Click(object? sender, EventArgs e)
{
PreTreatParam preParam = new PreTreatParam()
{
CellLinks = new CellLink[] {
new CellButton(Guid.NewGuid().ToString(),"编辑",TTypeMini.Primary),
new CellButton(Guid.NewGuid().ToString(),"删除",TTypeMini.Error),
}
};
var form = new PreTreatEdit(window, preParam) { Size = new Size(400, 300) };
AntdUI.Modal.open(new AntdUI.Modal.Config(window, "", form, TType.None)
{
BtnHeight = 0,
});
if (form.submit)
{
detectionConfig.OUTPreTreatParams.Add(preParam);
}
}
private void PreTable_CellButtonClick(object sender, TableButtonEventArgs e)
{
var buttontext = e.Btn.Text;
if (e.Record is PreTreatParam PreTreat)
{
switch (buttontext)
{
//暂不支持进入整行编辑,只支持指定单元格编辑,推荐使用弹窗或抽屉编辑整行数据
case "编辑":
var form = new PreTreatEdit(window, PreTreat) { Size = new Size(500, 300) };
AntdUI.Drawer.open(new AntdUI.Drawer.Config(window, form)
{
OnLoad = () =>
{
AntdUI.Message.info(window, "进入编辑", autoClose: 1);
},
OnClose = () =>
{
AntdUI.Message.info(window, "结束编辑", autoClose: 1);
}
});
break;
case "删除":
var result = Modal.open(window, "删除警告!", "确认要删除选择的数据吗?", TType.Warn);
if (result == DialogResult.OK)
detectionConfig.PreTreatParams.Remove(PreTreat);
break;
}
}
}
private void PreTable_CellClick(object sender, TableClickEventArgs e)
{
}
private void BtnPreDelete_Click(object? sender, EventArgs e)
{
if (detectionConfig.PreTreatParams.Count == 0 || !detectionConfig.PreTreatParams.Any(x => x.Selected))
{
AntdUI.Message.warn(window, "请选择要删除的行!", autoClose: 3);
return;
}
var result = Modal.open(window, "删除警告!", "确认要删除选择的数据吗?", TType.Warn);
if (result == DialogResult.OK)
{
// 使用反转for循环删除主列表中选中的项
for (int i = detectionConfig.PreTreatParams.Count - 1; i >= 0; i--)
{
// 删除选中的主列表项
if (detectionConfig.PreTreatParams[i].Selected)
{
detectionConfig.PreTreatParams.RemoveAt(i);
}
}
// 提示删除完成
AntdUI.Message.success(window, "删除成功!", autoClose: 3);
}
}
private void BtnPreAdd_Click(object? sender, EventArgs e)
{
PreTreatParam preParam = new PreTreatParam()
{
CellLinks = new CellLink[] {
new CellButton(Guid.NewGuid().ToString(),"编辑",TTypeMini.Primary),
new CellButton(Guid.NewGuid().ToString(),"删除",TTypeMini.Error),
}
};
var form = new PreTreatEdit(window, preParam) { Size = new Size(400, 300) };
AntdUI.Modal.open(new AntdUI.Modal.Config(window, "", form, TType.None)
{
BtnHeight = 0,
});
if (form.submit)
{
detectionConfig.PreTreatParams.Add(preParam);
}
}
private void InitData()
{
stDetectType.Items.Clear();
foreach (ModelType value in Enum.GetValues(typeof(ModelType)))
{
stDetectType.Items.Add(value.ToString());
}
preTable.Binding(detectionConfig.PreTreatParams);
PreOutTable.Binding(detectionConfig.OUTPreTreatParams);
lableTable.Binding(detectionConfig.DetectionLableList);
SizeTable.Binding(detectionConfig.SizeTreatParamList);
if (detectionConfig.PreTreatParams.Count > 0)
{
foreach (var item in detectionConfig.PreTreatParams)
{
item.CellLinks = new CellLink[] {
new CellButton(Guid.NewGuid().ToString(), "编辑", TTypeMini.Primary) ,
new CellButton(Guid.NewGuid().ToString(), "删除", TTypeMini.Error)
};
}
}
if (detectionConfig.OUTPreTreatParams.Count > 0)
{
foreach (var item in detectionConfig.OUTPreTreatParams)
{
item.CellLinks = new CellLink[] {
new CellButton(Guid.NewGuid().ToString(), "编辑", TTypeMini.Primary) ,
new CellButton(Guid.NewGuid().ToString(), "删除", TTypeMini.Error)
};
}
}
if (detectionConfig.DetectionLableList.Count > 0)
{
foreach (var item in detectionConfig.DetectionLableList)
{
item.CellLinks = new CellLink[] {
new CellButton(Guid.NewGuid().ToString(), "编辑", TTypeMini.Primary) ,
new CellButton(Guid.NewGuid().ToString(), "删除", TTypeMini.Error)
};
}
}
if (detectionConfig.SizeTreatParamList.Count > 0)
{
foreach (var item in detectionConfig.SizeTreatParamList)
{
item.CellLinks = new CellLink[] {
new CellButton(Guid.NewGuid().ToString(), "编辑", TTypeMini.Primary) ,
new CellButton(Guid.NewGuid().ToString(), "删除", TTypeMini.Error),
new CellButton(Guid.NewGuid().ToString(),"进行测量",TTypeMini.Primary)
};
}
}
if (detectionConfig.CameraCollects.Count > 0)
{
foreach (var item in detectionConfig.CameraCollects)
{
var control = new AntdUI.Tag()
{
Font = new System.Drawing.Font("Microsoft YaHei UI", 9F),
Size = new Size(90, 42),
Text = item.CameraSourceId,
CloseIcon = true
};
window.AutoDpi(control);
flowCameraPanel.Controls.Add(control);
control.BringToFront();
}
}
//1
sthPreStatus.DataBindings.Add("Checked", detectionConfig, "IsPreEnabled", true, DataSourceUpdateMode.OnPropertyChanged);
iptPrePath.DataBindings.Add("Text", detectionConfig, "HalconAlgorithemPath_Pre", true, DataSourceUpdateMode.OnPropertyChanged);
//2
sthDetectStatus.DataBindings.Add("Checked", detectionConfig, "IsEnabled", true, DataSourceUpdateMode.OnPropertyChanged);
sthStation.DataBindings.Add("Checked", detectionConfig, "IsAddStation", true, DataSourceUpdateMode.OnPropertyChanged);
stDetectType.DataBindings.Add("Text", detectionConfig, "ModelType",true, DataSourceUpdateMode.OnPropertyChanged);
iptConfidence.DataBindings.Add("Text", detectionConfig, "ModelconfThreshold", true, DataSourceUpdateMode.OnPropertyChanged);
iptDetectPath.DataBindings.Add("Text", detectionConfig, "ModelPath", true, DataSourceUpdateMode.OnPropertyChanged);
sthOKOriginal.DataBindings.Add("Checked", detectionConfig, "SaveOKOriginal", true, DataSourceUpdateMode.OnPropertyChanged);
sthNGOriginal.DataBindings.Add("Checked", detectionConfig, "SaveNGOriginal", true, DataSourceUpdateMode.OnPropertyChanged);
sthOKDetect.DataBindings.Add("Checked", detectionConfig, "SaveOKDetect", true, DataSourceUpdateMode.OnPropertyChanged);
sthDetectNG.DataBindings.Add("Checked", detectionConfig, "SaveNGDetect", true, DataSourceUpdateMode.OnPropertyChanged);
}
private void InitTableColumns()
{
preTable.Columns = new ColumnCollection() {
new ColumnCheck("Selected"){Fixed = true},
new Column("Name", "名称", ColumnAlign.Center),
new Column("Value", "值",ColumnAlign.Center),
new Column("CellLinks", "操作", ColumnAlign.Center)
};
PreOutTable.Columns = new ColumnCollection() {
new ColumnCheck("Selected"){Fixed = true},
new Column("Name", "名称", ColumnAlign.Center),
new Column("Value", "值",ColumnAlign.Center),
new Column("CellLinks", "操作", ColumnAlign.Center)
};
lableTable.Columns = new ColumnCollection() {
new ColumnCheck("Selected"){Fixed = true},
new Column("LabelName", "名称", ColumnAlign.Center),
new Column("MinScore", "最小得分",ColumnAlign.Center),
new Column("MaxScore", "最大得分", ColumnAlign.Center),
new Column("MinArea", "最小面积",ColumnAlign.Center),
new Column("MaxArea", "最大面积", ColumnAlign.Center),
new Column("ResultState", "结果", ColumnAlign.Center),
new Column("CellLinks", "操作", ColumnAlign.Center)
};
SizeTable.Columns = new ColumnCollection() {
new ColumnCheck("Selected"){Fixed = true},
new ColumnSwitch("IsEnable", "是否启用", ColumnAlign.Center),
new Column("PreName", "测量名称",ColumnAlign.Center),
new Column("PreType", "测量类型", ColumnAlign.Center),
new Column("PrePix", "阈值", ColumnAlign.Center),
new Column("ResultShow", "输入参数", ColumnAlign.Center),
new Column("OutResultShow", "输出参数", ColumnAlign.Center),
new Column("CellLinks", "操作", ColumnAlign.Center)
};
}
private void btnPath_Click(object? sender, EventArgs e)
{
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
// 设置对话框标题
openFileDialog.Title = "选择预处理文件";
// 限制文件后缀为 .hdvp
openFileDialog.Filter = "中处理 (*.hdvp)|*.hdvp";
// 禁止多选
openFileDialog.Multiselect = false;
// 显示对话框并等待用户操作
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
string filePath = openFileDialog.FileName;
iptPrePath.Text = filePath;
}
}
}
}
}