合并修改

This commit is contained in:
2025-04-30 16:13:13 +08:00
294 changed files with 60700 additions and 6623 deletions

View File

@ -1,19 +1,12 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using AntdUI;
using DH.Commons.Base;
using DH.Commons.Enums;
using DH.Devices.Vision;
using DH.Devices.PLC;
using XKRS.CanFly;
using static AntdUI.Table;
using static System.Windows.Forms.AxHost;
using static DH.Commons.Enums.EnumHelper;
namespace DHSoftware.Views
@ -21,88 +14,11 @@ namespace DHSoftware.Views
public partial class DetectControl : UserControl
{
Window window;
// DetectionConfig detectionConfig;
private DetectionConfig _config;
List<RelatedCamera> relatedCameras = new List<RelatedCamera>();
AntList<PreTreatParam> PreTreatList;
AntList<PreTreatParam> PreOutTreatList;
AntList<DetectionLable> DetectionLableList;
AntList<SizeTreatParam> SizeLableList;
PreTreatParam curPreTreat;
PreTreatParam curPreOutTreat;
DetectionLable curDetectionLable;
SizeTreatParam SizeParamLable;
public DetectionConfig detectionConfig
{
get => _config;
set
{
if (_config != null)
{
_config.PropertyChanged -= Config_PropertyChanged; // 解绑旧的
}
_config = value;
if (_config != null)
{
_config.PropertyChanged += Config_PropertyChanged; // 绑定新的
UpdateUI(); // 初始化 UI
}
}
}
private void Config_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
// 监听所有属性变化
switch (e.PropertyName)
{
case nameof(DetectionConfig.IsAddStation):
swIsAddStation.Checked = _config.IsAddStation;
break;
case nameof(DetectionConfig.IsEnabled):
swIsEnabled.Checked = _config.IsEnabled;
break;
case nameof(DetectionConfig.ModelType):
stDetectType.SelectedIndex = (int)_config.ModelType-1;
break;
case nameof(DetectionConfig.ModelconfThreshold):
if (iptScore.InvokeRequired)
{
iptScore.Invoke(() => iptScore.Text = _config.ModelconfThreshold.ToString());
}
else
{
iptScore.Text = _config.ModelconfThreshold.ToString();
}
break;
case nameof(DetectionConfig.PreTreatCollects):
break;
}
}
private void UpdateUI()
{
if (_config == null) return;
swIsAddStation.Checked = _config.IsAddStation;
swIsEnabled.Checked = _config.IsEnabled;
stDetectType.SelectedIndex = (int)_config.ModelType - 1;
iptScore.Text = _config.ModelconfThreshold.ToString();
iptModelPath.Text = _config.ModelPath;
swtPre.Checked = _config.IsPreEnabled;
iptPrePath.Text = _config.HalconAlgorithemPath_Pre;
// textBoxName.Text = _config.Name;
// checkBoxEnableGPU.Checked = _config.IsEnableGPU;
}
public DetectControl(Window _window)
DetectionConfig detectionConfig;
public DetectControl(Window _window, DetectionConfig _detection)
{
window = _window;
detectionConfig = _detection;
InitializeComponent();
//this.detectionConfig = detectionConfig;
//初始化表格列头
@ -216,7 +132,7 @@ namespace DHSoftware.Views
private void BindEventHandler()
{
//预处理
btnPath.Click += btnPath_Click;
btnPrePath.Click += btnPath_Click;
btnPreAdd.Click += BtnPreAdd_Click; ;
btnPreDelete.Click += BtnPreDelete_Click; ;
preTable.CellClick += PreTable_CellClick; ;
@ -248,12 +164,11 @@ namespace DHSoftware.Views
if (e.Record is DetectionLable DetectionLable)
{
curDetectionLable = DetectionLable;
switch (buttontext)
{
//暂不支持进入整行编辑,只支持指定单元格编辑,推荐使用弹窗或抽屉编辑整行数据
case "编辑":
var form = new DetectionLableEdit(window, curDetectionLable) { Size = new Size(500, 300) };
var form = new DetectionLableEdit(window, DetectionLable) { Size = new Size(500, 300) };
AntdUI.Drawer.open(new AntdUI.Drawer.Config(window, form)
{
OnLoad = () =>
@ -269,7 +184,7 @@ namespace DHSoftware.Views
case "删除":
var result = Modal.open(window, "删除警告!", "确认要删除选择的数据吗?", TType.Warn);
if (result == DialogResult.OK)
DetectionLableList.Remove(DetectionLable);
detectionConfig.DetectionLableList.Remove(DetectionLable);
break;
}
@ -283,12 +198,11 @@ namespace DHSoftware.Views
if (e.Record is SizeTreatParam sizeTreat)
{
SizeParamLable = sizeTreat;
switch (buttontext)
{
//暂不支持进入整行编辑,只支持指定单元格编辑,推荐使用弹窗或抽屉编辑整行数据
case "编辑":
var form = new SizeLabelEdit(window, SizeParamLable) { Size = new Size(500, 300) };
var form = new SizeLabelEdit(window, sizeTreat) { Size = new Size(500, 300) };
AntdUI.Drawer.open(new AntdUI.Drawer.Config(window, form)
{
OnLoad = () =>
@ -306,11 +220,10 @@ namespace DHSoftware.Views
case "删除":
var result = Modal.open(window, "删除警告!", "确认要删除选择的数据吗?", TType.Warn);
if (result == DialogResult.OK)
SizeLableList.Remove(sizeTreat);
UpdatePreTreatCollectsFromSizeTable();
detectionConfig.SizeTreatParamList.Remove(sizeTreat);
break;
case "进行测量":
var sizeType = ((int)SizeParamLable.PreType).ToString();
var sizeType = ((int)sizeTreat.PreType).ToString();
// 根据测量类型打开不同的窗口
switch (sizeType)
@ -350,7 +263,7 @@ namespace DHSoftware.Views
private void BtnLableDelete_Click(object? sender, EventArgs e)
{
if (DetectionLableList.Count == 0 || !DetectionLableList.Any(x => x.Selected))
if (detectionConfig.DetectionLableList.Count == 0 || !detectionConfig.DetectionLableList.Any(x => x.Selected))
{
AntdUI.Message.warn(window, "请选择要删除的行!", autoClose: 3);
return;
@ -360,12 +273,12 @@ namespace DHSoftware.Views
if (result == DialogResult.OK)
{
// 使用反转for循环删除主列表中选中的项
for (int i = DetectionLableList.Count - 1; i >= 0; i--)
for (int i = detectionConfig.DetectionLableList.Count - 1; i >= 0; i--)
{
// 删除选中的主列表项
if (DetectionLableList[i].Selected)
if (detectionConfig.DetectionLableList[i].Selected)
{
DetectionLableList.RemoveAt(i);
detectionConfig.DetectionLableList.RemoveAt(i);
}
}
// 提示删除完成
@ -389,13 +302,13 @@ namespace DHSoftware.Views
});
if (form.submit)
{
DetectionLableList.Add(detectionLable);
detectionConfig.DetectionLableList.Add(detectionLable);
}
}
private void BtnSizeDelete_Click(object? sender, EventArgs e)
{
if (SizeLableList.Count == 0 || !SizeLableList.Any(x => x.Selected))
if (detectionConfig.SizeTreatParamList.Count == 0 || !detectionConfig.SizeTreatParamList.Any(x => x.Selected))
{
AntdUI.Message.warn(window, "请选择要删除的行!", autoClose: 3);
return;
@ -405,12 +318,12 @@ namespace DHSoftware.Views
if (result == DialogResult.OK)
{
// 使用反转for循环删除主列表中选中的项
for (int i = SizeLableList.Count - 1; i >= 0; i--)
for (int i = detectionConfig.SizeTreatParamList.Count - 1; i >= 0; i--)
{
// 删除选中的主列表项
if (SizeLableList[i].Selected)
if (detectionConfig.SizeTreatParamList[i].Selected)
{
SizeLableList.RemoveAt(i);
detectionConfig.SizeTreatParamList.RemoveAt(i);
}
}
// 提示删除完成
@ -439,7 +352,7 @@ namespace DHSoftware.Views
});
if (form.submit)
{
SizeLableList.Add(SizeParamLable);
detectionConfig.SizeTreatParamList.Add(SizeParamLable);
}
}
@ -448,22 +361,48 @@ namespace DHSoftware.Views
}
private void BtnCorrelatedCamera_Click(object? sender, EventArgs e)
{
var form = new CorrelatedCameraEdit(window, relatedCameras) { Size = new Size(500, 400) };
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)
{
flowPanel1.Controls.Clear();
InitRelatedCamera();
detectionConfig.CameraCollects=relatedCameras;
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();
}
}
}
}
@ -488,8 +427,8 @@ namespace DHSoftware.Views
{
string filePath = openFileDialog.FileName;
iptModelPath.Text = filePath;
_config.ModelPath = filePath;
iptDetectPath.Text = filePath;
}
}
}
@ -500,12 +439,11 @@ namespace DHSoftware.Views
if (e.Record is PreTreatParam PreTreat)
{
curPreOutTreat = PreTreat;
switch (buttontext)
{
//暂不支持进入整行编辑,只支持指定单元格编辑,推荐使用弹窗或抽屉编辑整行数据
case "编辑":
var form = new PreTreatEdit(window, curPreOutTreat) { Size = new Size(500, 300) };
var form = new PreTreatEdit(window, PreTreat) { Size = new Size(500, 300) };
AntdUI.Drawer.open(new AntdUI.Drawer.Config(window, form)
{
OnLoad = () =>
@ -521,7 +459,7 @@ namespace DHSoftware.Views
case "删除":
var result = Modal.open(window, "删除警告!", "确认要删除选择的数据吗?", TType.Warn);
if (result == DialogResult.OK)
PreOutTreatList.Remove(PreTreat);
detectionConfig.OUTPreTreatParams.Remove(PreTreat);
break;
}
@ -530,7 +468,7 @@ namespace DHSoftware.Views
private void BtnOutDelete_Click(object? sender, EventArgs e)
{
if (PreOutTreatList.Count == 0 || !PreOutTreatList.Any(x => x.Selected))
if (detectionConfig.OUTPreTreatParams.Count == 0 || !detectionConfig.OUTPreTreatParams.Any(x => x.Selected))
{
AntdUI.Message.warn(window, "请选择要删除的行!", autoClose: 3);
return;
@ -540,12 +478,12 @@ namespace DHSoftware.Views
if (result == DialogResult.OK)
{
// 使用反转for循环删除主列表中选中的项
for (int i = PreOutTreatList.Count - 1; i >= 0; i--)
for (int i = detectionConfig.OUTPreTreatParams.Count - 1; i >= 0; i--)
{
// 删除选中的主列表项
if (PreOutTreatList[i].Selected)
if (detectionConfig.OUTPreTreatParams[i].Selected)
{
PreOutTreatList.RemoveAt(i);
detectionConfig.OUTPreTreatParams.RemoveAt(i);
}
}
// 提示删除完成
@ -570,7 +508,7 @@ namespace DHSoftware.Views
});
if (form.submit)
{
PreOutTreatList.Add(preParam);
detectionConfig.OUTPreTreatParams.Add(preParam);
}
}
@ -580,12 +518,11 @@ namespace DHSoftware.Views
if (e.Record is PreTreatParam PreTreat)
{
curPreTreat = PreTreat;
switch (buttontext)
{
//暂不支持进入整行编辑,只支持指定单元格编辑,推荐使用弹窗或抽屉编辑整行数据
case "编辑":
var form = new PreTreatEdit(window, curPreTreat) { Size = new Size(500, 300) };
var form = new PreTreatEdit(window, PreTreat) { Size = new Size(500, 300) };
AntdUI.Drawer.open(new AntdUI.Drawer.Config(window, form)
{
OnLoad = () =>
@ -601,7 +538,7 @@ namespace DHSoftware.Views
case "删除":
var result = Modal.open(window, "删除警告!", "确认要删除选择的数据吗?", TType.Warn);
if (result == DialogResult.OK)
PreTreatList.Remove(PreTreat);
detectionConfig.PreTreatParams.Remove(PreTreat);
break;
}
@ -616,7 +553,7 @@ namespace DHSoftware.Views
private void BtnPreDelete_Click(object? sender, EventArgs e)
{
if (PreTreatList.Count == 0 || !PreTreatList.Any(x => x.Selected))
if (detectionConfig.PreTreatParams.Count == 0 || !detectionConfig.PreTreatParams.Any(x => x.Selected))
{
AntdUI.Message.warn(window, "请选择要删除的行!", autoClose: 3);
return;
@ -626,12 +563,12 @@ namespace DHSoftware.Views
if (result == DialogResult.OK)
{
// 使用反转for循环删除主列表中选中的项
for (int i = PreTreatList.Count - 1; i >= 0; i--)
for (int i = detectionConfig.PreTreatParams.Count - 1; i >= 0; i--)
{
// 删除选中的主列表项
if (PreTreatList[i].Selected)
if (detectionConfig.PreTreatParams[i].Selected)
{
PreTreatList.RemoveAt(i);
detectionConfig.PreTreatParams.RemoveAt(i);
}
}
// 提示删除完成
@ -655,7 +592,7 @@ namespace DHSoftware.Views
});
if (form.submit)
{
PreTreatList.Add(preParam);
detectionConfig.PreTreatParams.Add(preParam);
}
}
@ -697,74 +634,98 @@ namespace DHSoftware.Views
}
}
private void InitData()
{
relatedCameras = detectionConfig.CameraCollects;
InitRelatedCamera();
// swIsAddStation. = detectionConfig.IsAddStation;
PreTreatList = new AntList<PreTreatParam>();
foreach (var item in detectionConfig.PreTreatParams)
stDetectType.Items.Clear();
foreach (ModelType value in Enum.GetValues(typeof(ModelType)))
{
PreTreatParam lable = item;
lable.CellLinks = new CellLink[] {
new CellButton(Guid.NewGuid().ToString(),"编辑",TTypeMini.Primary),
new CellButton(Guid.NewGuid().ToString(),"删除",TTypeMini.Error),
};
PreTreatList.Add(lable);
stDetectType.Items.Add(value.ToString());
}
preTable.Binding(PreTreatList);
preTable.Binding(detectionConfig.PreTreatParams);
PreOutTable.Binding(detectionConfig.OUTPreTreatParams);
lableTable.Binding(detectionConfig.DetectionLableList);
SizeTable.Binding(detectionConfig.SizeTreatParamList);
PreOutTreatList = new AntList<PreTreatParam>();
foreach (var item in detectionConfig.OUTPreTreatParams)
if (detectionConfig.PreTreatParams.Count > 0)
{
PreTreatParam lable = item;
lable.CellLinks = new CellLink[] {
new CellButton(Guid.NewGuid().ToString(),"编辑",TTypeMini.Primary),
new CellButton(Guid.NewGuid().ToString(),"删除",TTypeMini.Error),
};
PreOutTreatList.Add(lable);
foreach (var item in detectionConfig.PreTreatParams)
{
item.CellLinks = new CellLink[] {
new CellButton(Guid.NewGuid().ToString(), "编辑", TTypeMini.Primary) ,
new CellButton(Guid.NewGuid().ToString(), "删除", TTypeMini.Error)
};
}
}
PreOutTable.Binding(PreOutTreatList);
DetectionLableList = new AntList<DetectionLable>();
foreach (var item in detectionConfig.DetectionLableList)
if (detectionConfig.OUTPreTreatParams.Count > 0)
{
DetectionLable lable = item;
lable.CellLinks = new CellLink[] {
new CellButton(Guid.NewGuid().ToString(),"编辑",TTypeMini.Primary),
new CellButton(Guid.NewGuid().ToString(),"删除",TTypeMini.Error),
};
DetectionLableList.Add(lable);
foreach (var item in detectionConfig.OUTPreTreatParams)
{
item.CellLinks = new CellLink[] {
new CellButton(Guid.NewGuid().ToString(), "编辑", TTypeMini.Primary) ,
new CellButton(Guid.NewGuid().ToString(), "删除", TTypeMini.Error)
};
}
}
lableTable.Binding(DetectionLableList);
// SizeLableList = new AntList<SizeTreatParam>(_config.PreTreatCollects);
foreach (var item in _config.PreTreatCollects)
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),
new CellButton(Guid.NewGuid().ToString(),"进行测量",TTypeMini.Primary)
};
// SizeLableList.Add(item);
item.CellLinks = new CellLink[] {
new CellButton(Guid.NewGuid().ToString(), "编辑", TTypeMini.Primary) ,
new CellButton(Guid.NewGuid().ToString(), "删除", TTypeMini.Error)
};
}
}
// 将 List<PreTreatCollect> 转换为 AntList<PreTreatCollect>
SizeLableList = new AntList<SizeTreatParam>(_config.PreTreatCollects);
if (detectionConfig.SizeTreatParamList.Count > 0)
{
foreach (var item in detectionConfig.SizeTreatParamList)
{
// 绑定转换后的数据到 SizeTable
SizeTable.Binding(SizeLableList);
//SizeTable.Binding(_config.PreTreatCollects);
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);
}
@ -788,9 +749,9 @@ namespace DHSoftware.Views
lableTable.Columns = new ColumnCollection() {
new ColumnCheck("Selected"){Fixed = true},
new Column("LabelName", "名称", ColumnAlign.Center),
new Column("MinSource", "最小得分",ColumnAlign.Center),
new Column("MaxArea", "最大得分", ColumnAlign.Center),
new Column("MinSource", "最小面积",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)
@ -831,42 +792,6 @@ namespace DHSoftware.Views
}
}
}
List<KeyValuePair<string, int>> MLModelTypes = GetFilteredEnumDescriptionsAndValues<MLModelType>();
List<KeyValuePair<string, int>> SizeEnum = GetFilteredEnumDescriptionsAndValues<SizeEnum>();
private void tabPage2_Click(object sender, EventArgs e)
{
}
private void DetectControl_Load(object sender, EventArgs e)
{
InitData();
}
public static List<KeyValuePair<string, int>> GetFilteredEnumDescriptionsAndValues<T>() where T : Enum
{
return Enum.GetValues(typeof(T))
.Cast<T>()
.Select(e =>
{
// 获取枚举的 Description 属性,如果没有,则使用枚举的名称
var description = e.GetType()
.GetField(e.ToString())
?.GetCustomAttribute<DescriptionAttribute>()
?.Description ?? e.ToString();
// 返回枚举的描述和对应的整数值
return new KeyValuePair<string, int>(description, Convert.ToInt32(e));
})
.ToList();
}
}
}