This commit is contained in:
2025-03-21 08:51:20 +08:00
parent 0dedff36fd
commit 9a5d3be528
70 changed files with 4542 additions and 2207 deletions

View File

@ -1,8 +1,10 @@
using AntdUI;
using AntdUI.Svg;
using DH.Commons.Base;
using DH.Commons.Enums;
using DH.Commons.Helper;
using DH.Commons.Models;
using DH.Devices.Camera;
using DH.Devices.Devices;
using DH.Devices.Motion;
using DH.Devices.PLC;
using DH.Devices.Vision;
@ -40,20 +42,15 @@ namespace DHSoftware
private UserControl currControl;
private bool isUpdatingTabs = false;//用于阻止Tabs更新
private bool isLight = true;
// 定义一个语言切换事件
public event EventHandler LanguageChanged;
private System.Windows.Forms.Timer refreshTimer;
private int testCounter = 1;
Dictionary<string, List<string>> _cameraRelatedDetectionDict = null;
public MainWindow()
{
InitializeComponent();
refreshTimer = new System.Windows.Forms.Timer();
refreshTimer.Interval = 1000; // 1秒间隔
refreshTimer.Tick += RefreshTimer_Tick;
//refreshTimer.Start();
//初始化数据
InitData();
@ -65,162 +62,185 @@ namespace DHSoftware
userControlFrm.Dock = DockStyle.Fill;
tabPage2.Controls.Add(userControlFrm);
// List<Camera> Cameras = new List<Camera>
//{
// new Camera
// {
// DeviceName = "Cam1",
// Alias = "相机1",
// ImagePath = @"D:\1.jpeg"
// },
// new Camera
// {
// DeviceName = "Cam2",
// Alias = "相机2",
// ImagePath = @"D:\2.jpeg"
// },
// new Camera
// {
// DeviceName = "Cam3",
// Alias = "相机3",
// ImagePath = @"D:\3.jpeg"
// },
// new Camera
// {
// DeviceName = "Cam4",
// Alias = "相机4",
// ImagePath = @"D:\4.jpeg"
// },
// new Camera
// {
// DeviceName = "Cam5",
// Alias = "相机5",
// ImagePath = @"D:\5.jpeg"
// }
//};
// if (Cameras.Count > 0)
// {
// tabImgDisplay.Controls.Clear();
// foreach (var cam in Cameras)
// {
// AntdUI.TabPage tabPage = new AntdUI.TabPage();
// tabPage.Name = $"tab{cam.DeviceName}";
// tabPage.Text = cam.Alias;
// //ImgDisplayControl imgDisplayControl = new ImgDisplayControl();
// //imgDisplayControl.Name = $"img{cam.DeviceName}";
// //imgDisplayControl.Dock = DockStyle.Fill;
// //tabPage.Controls.Add(imgDisplayControl);
// PictureBox pictureBox = new PictureBox();
// pictureBox.Name = $"pic{cam.DeviceName}";
// pictureBox.Dock = DockStyle.Fill;
// pictureBox.SizeMode = PictureBoxSizeMode.Zoom;
// tabPage.Controls.Add(pictureBox);
// tabImgDisplay.Pages.Add(tabPage);
// }
// }
}
private void RefreshTimer_Tick(object sender, EventArgs e)
{
//// 获取相机1的控件通过控件名称查找
//var targetControl = FindControlRecursive(tabImgDisplay, "picCam1") as PictureBox;
//if (targetControl != null)
//{
// // 生成测试路径(示例路径)
// string testPath = $@"D:\{testCounter}.png"; // 循环1-5的图片
// testCounter++;
// // 加载并显示图片
// targetControl.Image = Image.FromFile(testPath);
// if (testCounter == 5)
// {
// testCounter = 1;
// }
// targetControl.Parent.Invalidate();
//}
}
// 递归查找控件的方法
private Control FindControlRecursive(Control parent, string name)
{
if (parent.Name == name) return parent;
foreach (Control child in parent.Controls)
{
var found = FindControlRecursive(child, name);
if (found != null) return found;
}
return null;
}
// 触发事件
protected virtual void OnLanguageChanged(EventArgs e)
{
LanguageChanged?.Invoke(this, e);
}
private void InitData()
{
//根据系统亮暗初始化一次
isLight = ThemeHelper.IsLightMode();
button_color.Toggle = !isLight;
ThemeHelper.SetColorMode(this, isLight);
//初始化消息弹出位置
Config.ShowInWindow = true;
//加载方案
LoadScheme();
}
private void LoadScheme()
{
try
{
//方案配置初始化
SchemeHelper.Initialize();
//读取方案列表
List<string> list = SchemeHelper.GetAllSchemes();
string CurrentScheme = "默认方案";
//如果是空,新增默认数据
if (list == null || list.Count <= 0)
{
list = new() { CurrentScheme };
//显示到方案列表
sltProjects.Items.Clear();
sltProjects.Items.Add(CurrentScheme);
//保存到方案配置
SchemeHelper.AddScheme(CurrentScheme);
SchemeHelper.SetCurrentScheme(CurrentScheme);
//新构建配置文件
ConfigHelper.InitializeScheme(CurrentScheme);
sltProjects.SelectedIndex = 0;
}
else
{
foreach (string s in list)
{
sltProjects.Items.Add(s);
}
CurrentScheme = SchemeHelper.GetCurrentScheme();
sltProjects.SelectedValue = CurrentScheme;
}
SystemModel.CurrentScheme = CurrentScheme;
//加载当前方案配置
ConfigHelper.LoadConfig();
}
catch (Exception ex)
{
AntdUI.Message.error(this, ex.Message, autoClose: 3);
}
}
private void BindEventHandler()
{
buttonSZ.Click += ButtonSZ_Click;
button_color.Click += Button_color_Click;
btnAddProject.Click += BtnAddProject_Click;
btnLoadProject.Click += BtnLoadProject_Click;
btnDeleteProject.Click += BtnDeleteProject_Click;
//监听系统深浅色变化
SystemEvents.UserPreferenceChanged += SystemEvents_UserPreferenceChanged;
}
private void SystemEvents_UserPreferenceChanged(object sender, UserPreferenceChangedEventArgs e)
private void BtnDeleteProject_Click(object? sender, EventArgs e)
{
if (e.Category == UserPreferenceCategory.General)
try
{
isLight = ThemeHelper.IsLightMode();
button_color.Toggle = !isLight;
ThemeHelper.SetColorMode(this, isLight);
if (sltProjects.Items.Count == 0 || sltProjects.SelectedIndex == -1) return;
var result = AntdUI.Modal.open(this, "删除警告!", "确认要删除该方案吗?", TType.Warn);
if (result == DialogResult.OK)
{
int selectedIndex = sltProjects.SelectedIndex;
// 删除当前选中项
SchemeHelper.DeleteScheme(sltProjects.Text);
ConfigHelper.DeleteSchemeConfig(sltProjects.Text);
AntdUI.Message.success(this, $"删除方案{sltProjects.Text}成功!", autoClose: 3);
sltProjects.Items.RemoveAt(selectedIndex);
// 自动选择下一个(如果存在)
if (sltProjects.Items.Count > 0)
{
// 如果删除的不是最后一项,则选中原位置的新项,否则选中最后一项
sltProjects.SelectedIndex = selectedIndex < sltProjects.Items.Count
? selectedIndex
: sltProjects.Items.Count - 1;
SystemModel.CurrentScheme = sltProjects.Text;
SchemeHelper.SetCurrentScheme(SystemModel.CurrentScheme);
//加载当前方案配置
ConfigHelper.LoadConfig();
}
else
{
sltProjects.SelectedIndex = -1; // 清空选择
AntdUI.Modal.open(this, "空方案警告!", "当前方案全部删除,需重启程序!", TType.Warn);
}
}
}
catch (Exception ex)
{
AntdUI.Message.error(this, ex.Message, autoClose: 3);
}
}
private void Button_color_Click(object sender, EventArgs e)
private void BtnLoadProject_Click(object? sender, EventArgs e)
{
isLight = !isLight;
//这里使用了Toggle属性切换图标
button_color.Toggle = !isLight;
ThemeHelper.SetColorMode(this, isLight);
try
{
if (sltProjects.Items.Count == 0 || sltProjects.SelectedIndex == -1) return;
if (SystemModel.CurrentScheme == sltProjects.Text)
{
AntdUI.Message.warn(this, "当前已是该方案,无需重复载入!", autoClose: 3);
return;
}
//修改当前软件当前方案
SystemModel.CurrentScheme = sltProjects.Text;
//修改配置当前方案
SchemeHelper.SetCurrentScheme(SystemModel.CurrentScheme);
//将配置文件替换为当前方案
ConfigHelper.LoadConfig();
AntdUI.Message.success(this, $"载入方案{SystemModel.CurrentScheme}成功!", autoClose: 3);
}
catch (Exception ex) {
AntdUI.Message.error(this, ex.Message, autoClose: 3);
}
}
private void ButtonSZ_Click(object sender, EventArgs e)
private void BtnAddProject_Click(object? sender, EventArgs e)
{
}
try
{
var form = new AddSchemeControl(this, "新增方案操作") { Size = new System.Drawing.Size(400, 300) };
AntdUI.Modal.open(new AntdUI.Modal.Config(this, "", form, TType.None)
{
BtnHeight = 0,
});
if (form.submit)
{
string SchemeName = form.SchemeName;
//保存到方案配置
SchemeHelper.AddScheme(SchemeName);
if (form.NullScheme)
{
//新构建配置文件
ConfigHelper.InitializeScheme(SchemeName);
}
else
{
//派生当前方案
ConfigHelper.DeriveScheme(SchemeName);
}
//刷新方案列表
sltProjects.Items.Clear();
List<string> list = SchemeHelper.GetAllSchemes();
foreach (string s in list)
{
sltProjects.Items.Add(s);
}
string CurrentScheme = SchemeHelper.GetCurrentScheme();
sltProjects.SelectedValue = CurrentScheme;
AntdUI.Message.success(this, $"新增方案{SchemeName}成功!", autoClose: 3);
}
}
catch (Exception ex)
{
AntdUI.Message.error(this, ex.Message, autoClose: 3);
}
}
public List<Camera> HKCameras { get; } = new List<Camera>();
public List<Do3ThinkCamera> Cameras { get; } = new List<Do3ThinkCamera>();
@ -272,6 +292,7 @@ namespace DHSoftware
default:
break;
}
segmented1.SelectIndex = -1;
}
public bool CurrentMachine = false;
public volatile int ProductNum_Total = 0;
@ -317,14 +338,14 @@ namespace DHSoftware
var det1 = new DetectionConfig("相机1", MLModelType.ObjectDetection, @"D:\PROJECTS\X015\Vision\Cam1.onnx", false, "Cam1");
var det2 = new DetectionConfig("相机2", MLModelType.ObjectDetection, @"D:\PROJECTS\X015\Vision\Cam2.onnx", false, "Cam2");
var det3 = new DetectionConfig("相机3", MLModelType.ObjectDetection, @"D:\PROJECTS\X015\Vision\Cam3.onnx", false, "Cam3");
var det4 = new DetectionConfig("相机4", MLModelType.ObjectDetection, @"D:\PROJECTS\X015\Vision\Cam4.onnx", false, "Cam4");
var det5 = new DetectionConfig("相机5", MLModelType.ObjectDetection, @"D:\PROJECTS\X015\Vision\Cam5.onnx", false, "Cam5");
var det6 = new DetectionConfig("相机6", MLModelType.ObjectDetection, @"D:\PROJECTS\X015\Vision\Cam6.onnx", false, "Cam6");
var det7 = new DetectionConfig("相机7", MLModelType.ObjectDetection, @"D:\PROJECTS\X015\Vision\Cam7.onnx", false, "Cam7");
var det8 = new DetectionConfig("相机8", MLModelType.ObjectDetection, @"D:\PROJECTS\X015\Vision\Cam8.onnx", false, "Cam8");
var det1 = new DetectionConfig("相机1", ModelType., @"D:\PROJECTS\X015\Vision\Cam1.onnx", false, "Cam1");
var det2 = new DetectionConfig("相机2", ModelType., @"D:\PROJECTS\X015\Vision\Cam2.onnx", false, "Cam2");
var det3 = new DetectionConfig("相机3", ModelType., @"D:\PROJECTS\X015\Vision\Cam3.onnx", false, "Cam3");
var det4 = new DetectionConfig("相机4", ModelType., @"D:\PROJECTS\X015\Vision\Cam4.onnx", false, "Cam4");
var det5 = new DetectionConfig("相机5", ModelType., @"D:\PROJECTS\X015\Vision\Cam5.onnx", false, "Cam5");
var det6 = new DetectionConfig("相机6", ModelType., @"D:\PROJECTS\X015\Vision\Cam6.onnx", false, "Cam6");
var det7 = new DetectionConfig("相机7", ModelType., @"D:\PROJECTS\X015\Vision\Cam7.onnx", false, "Cam7");
var det8 = new DetectionConfig("相机8", ModelType., @"D:\PROJECTS\X015\Vision\Cam8.onnx", false, "Cam8");
List<RelatedCamera> CameraCollects = new List<RelatedCamera>();
CameraCollects.Add(new RelatedCamera("Cam1"));
List<RelatedCamera> CameraCollects2 = new List<RelatedCamera>();
@ -349,7 +370,7 @@ namespace DHSoftware
det1.ModelconfThreshold = Conf;
det1.ModelWidth = 640;
det1.ModelHeight = 640;
det1.in_lable_path = "D:\\PROJECTS\\X015\\Vision\\Cam1.txt";
det1.In_lable_path = "D:\\PROJECTS\\X015\\Vision\\Cam1.txt";
det1.IsEnabled = true;
det1.ShowLocation.X = 1;
det1.ShowLocation.Y = 1;
@ -358,7 +379,7 @@ namespace DHSoftware
det2.ModelconfThreshold = Conf;
det2.ModelWidth = 640;
det2.ModelHeight = 640;
det2.in_lable_path = "D:\\PROJECTS\\X015\\Vision\\Cam2.txt";
det2.In_lable_path = "D:\\PROJECTS\\X015\\Vision\\Cam2.txt";
det2.IsEnabled = true;
det2.ShowLocation.X = 2;
det2.ShowLocation.Y = 1;
@ -367,7 +388,7 @@ namespace DHSoftware
det3.ModelconfThreshold = Conf;
det3.ModelWidth = 640;
det3.ModelHeight = 640;
det3.in_lable_path = "D:\\PROJECTS\\X015\\Vision\\Cam3.txt";
det3.In_lable_path = "D:\\PROJECTS\\X015\\Vision\\Cam3.txt";
det3.IsEnabled = true;
det3.ShowLocation.X = 3;
det3.ShowLocation.Y = 1;
@ -376,7 +397,7 @@ namespace DHSoftware
det4.ModelconfThreshold = Conf;
det4.ModelWidth = 640;
det4.ModelHeight = 640;
det4.in_lable_path = "D:\\PROJECTS\\X015\\Vision\\Cam4.txt";
det4.In_lable_path = "D:\\PROJECTS\\X015\\Vision\\Cam4.txt";
det4.IsEnabled = true;
det4.ShowLocation.X = 4;
det4.ShowLocation.Y = 1;
@ -387,7 +408,7 @@ namespace DHSoftware
det5.ModelconfThreshold = Conf;
det5.ModelWidth = 640;
det5.ModelHeight = 640;
det5.in_lable_path = "D:\\PROJECTS\\X015\\Vision\\Cam5.txt";
det5.In_lable_path = "D:\\PROJECTS\\X015\\Vision\\Cam5.txt";
det5.IsEnabled = true;
det5.ShowLocation.X = 1;
det5.ShowLocation.Y = 2;
@ -396,7 +417,7 @@ namespace DHSoftware
det6.ModelconfThreshold = Conf;
det6.ModelWidth = 640;
det6.ModelHeight = 640;
det6.in_lable_path = "D:\\PROJECTS\\X015\\Vision\\Cam6.txt";
det6.In_lable_path = "D:\\PROJECTS\\X015\\Vision\\Cam6.txt";
det6.IsEnabled = true;
det6.ShowLocation.X = 2;
det6.ShowLocation.Y = 2;
@ -405,7 +426,7 @@ namespace DHSoftware
det7.ModelconfThreshold = Conf;
det7.ModelWidth = 640;
det7.ModelHeight = 640;
det7.in_lable_path = "D:\\PROJECTS\\X015\\Vision\\Cam7.txt";
det7.In_lable_path = "D:\\PROJECTS\\X015\\Vision\\Cam7.txt";
det7.IsEnabled = true;
det7.ShowLocation.X = 3;
det7.ShowLocation.Y = 2;
@ -414,7 +435,7 @@ namespace DHSoftware
det8.ModelconfThreshold = Conf;
det8.ModelWidth = 640;
det8.ModelHeight = 640;
det8.in_lable_path = "D:\\PROJECTS\\X015\\Vision\\Cam8.txt";
det8.In_lable_path = "D:\\PROJECTS\\X015\\Vision\\Cam8.txt";
det8.IsEnabled = true;
det8.ShowLocation.X = 4;
det8.ShowLocation.Y = 2;
@ -1020,7 +1041,7 @@ namespace DHSoftware
listCamBase.Add(cam1);
listCamBase.Add(cam2);
SettingWindow1 settingWindow = new SettingWindow1();
SettingWindow settingWindow = new SettingWindow();
settingWindow.Show();
//s.cameras = listCamBase;
//s.Show();