加载界面、程序关闭界面、单例模式

This commit is contained in:
2025-03-24 18:21:21 +08:00
parent 126db6bf91
commit 2b32e1a649
12 changed files with 807 additions and 199 deletions

View File

@ -41,13 +41,8 @@ namespace DHSoftware
public partial class MainWindow : AntdUI.Window
{
private UserControl currControl;
private bool isUpdatingTabs = false;//用于阻止Tabs更新
private bool isLight = true;
private int testCounter = 1;
Dictionary<string, List<string>> _cameraRelatedDetectionDict = null;
private string _loginName;
@ -187,21 +182,28 @@ namespace DHSoftware
}
/// <summary>
/// 窗体对象实例
/// </summary>
private static MainWindow _instance;
internal static MainWindow Instance
{
get
{
if (_instance == null)
_instance = new MainWindow();
return _instance;
}
}
SegmentedItem itemToHide;
private void InitData()
{
itemToHide = segmented1.Items[4];
segmented1.Items.Remove(itemToHide);
//加载方案
LoadScheme();
}
private void LoadScheme()
public void LoadScheme()
{
try
{
@ -244,6 +246,123 @@ namespace DHSoftware
AntdUI.Message.error(this, ex.Message, autoClose: 3);
}
}
public void ConnectCamera()
{
Cameras.Clear();
HKCameras.Clear();
if (ConfigModel.CameraBaseList.Count > 0)
{
for (int i = 0; i < ConfigModel.CameraBaseList.Count; i++)
{
var cameraBase = ConfigModel.CameraBaseList[i];
if (cameraBase.CamType == EnumCamType.Do3think)
{
Do3ThinkCamera cam = new Do3ThinkCamera();
cam.CameraName = cameraBase.CameraName;
cam.CameraIP = cameraBase.CameraIP;
cam.IsEnabled = cameraBase.IsEnabled;
Cameras.Add(cam);
//cam.CameraConnect();
cam.OnHImageOutput += OnCameraHImageOutput;
}
else if (cameraBase.CamType == EnumCamType.hik)
{
HikVisionCamera cam = new HikVisionCamera();
cam.CameraName = cameraBase.CameraName;
cam.CameraIP = cameraBase.CameraIP;
cam.IsEnabled = cameraBase.IsEnabled;
HKCameras.Add(cam);
// cam.CameraConnect();
cam.OnHImageOutput += OnCameraHImageOutput;
}
}
}
}
public void ConnectPLC()
{
if (ConfigModel.PLCBaseList.Count > 0)
{
for (int i = 0; i < ConfigModel.PLCBaseList.Count; i++)
{
var plcBase = ConfigModel.PLCBaseList[i];
if (plcBase.PLCType == EnumPLCType.XC网口)
{
PLC.IP = plcBase.IP;
PLC.Enable = plcBase.Enable;
PLC.PLCName = plcBase.PLCName;
PLC.PLCItemList = plcBase.PLCItemList;
PLC.Port = plcBase.Port;
PLC.PLCConnect();
}
}
}
}
public void InitModel()
{
Dectection.Clear();
_cameraRelatedDetectionDict = new();
if (ConfigModel.DetectionList.Count > 0)
{
for (int i = 0; i < ConfigModel.DetectionList.Count; i++)
{
DetectionConfig detectionConfig = ConfigModel.DetectionList[i];
var detection = ConfigModel.DetectionList[i];
detectionConfig.CameraCollects = detection.CameraCollects;
detectionConfig.ModelconfThreshold = detection.ModelconfThreshold;
detectionConfig.ModelWidth = detection.ModelWidth;
detectionConfig.ModelHeight = detection.ModelHeight;
detectionConfig.In_lable_path = detection.In_lable_path;
detectionConfig.IsEnabled = detection.IsEnabled;
detectionConfig.ShowLocation.X = detection.ShowLocation.X;
detectionConfig.ShowLocation.Y = detection.ShowLocation.Y;
DetectionConfigs.Add(detectionConfig);
}
}
DetectionConfigs.ForEach(detection =>
{
detection.CameraCollects.ForEach(cam =>
{
List<string> Dets = new List<string>
{
detection.Id
};
if (!_cameraRelatedDetectionDict.ContainsKey(cam.CameraSourceId))
{
_cameraRelatedDetectionDict.Add(cam.CameraSourceId, Dets);
}
else
{
_cameraRelatedDetectionDict[cam.CameraSourceId].Add(detection.Id);
}
}
);
});
string inferenceDevice = "CPU";
//
_visionEngine = new SimboVisionDriver();
_visionEngine.DetectionConfigs = DetectionConfigs;
//初始化模型 加载模型
_visionEngine.Init();
CtrlVisionRunBase ctrlVisionRun = new CtrlVisionRunBase(_visionEngine);
tabImgDisplay.Controls.Add(ctrlVisionRun);
}
private void BindEventHandler()
{
@ -378,19 +497,46 @@ namespace DHSoftware
{
}
private void MainWindow_FormClosed(object sender, FormClosedEventArgs e)
private bool _isClosing = false; // 状态标志
private void MainWindow_FormClosing(object sender, FormClosingEventArgs e)
{
foreach (var camera in Cameras)
{
camera.CameraDisConnect();
if (_isClosing) return;
_isClosing = true;
// 取消默认关闭行为
e.Cancel = true;
// 立即隐藏主窗口
this.Hide();
// 显示关闭界面
CloseWindow.Instance.Show();
Thread.Sleep(200);
try
{
// 执行关闭操作
foreach (var camera in Cameras)
{
camera.CameraDisConnect();
}
foreach (var camera in HKCameras)
{
camera.CameraDisConnect();
}
PLC.PLCDisConnect();
CloseWindow.Instance.Close();// 关闭提示窗口
//Application.Exit();
System.Environment.Exit(0);
}
foreach (var camera in HKCameras)
{
//var hk as HikVisionCamera;
camera.CameraDisConnect();
catch (Exception ex)
{
CloseWindow.Instance.Close();
System.Environment.Exit(0);
}
PLC.PLCDisConnect();
}
private void segmented1_SelectIndexChanged(object sender, EventArgs e)
@ -435,115 +581,7 @@ namespace DHSoftware
public DateTime startTime;
private void HandleStartButton()
{
Cameras.Clear();
HKCameras.Clear();
Dectection.Clear();
_cameraRelatedDetectionDict = new();
//初始化相机 链接相机
if (ConfigModel.CameraBaseList.Count > 0)
{
for (int i = 0; i <ConfigModel.CameraBaseList.Count; i++)
{
var cameraBase = ConfigModel.CameraBaseList[i];
if(cameraBase.CamType == EnumCamType.Do3think)
{
Do3ThinkCamera cam = new Do3ThinkCamera();
cam.CameraName = cameraBase.CameraName;
cam.CameraIP = cameraBase.CameraIP;
cam.IsEnabled = cameraBase.IsEnabled;
Cameras.Add(cam);
//cam.CameraConnect();
cam.OnHImageOutput += OnCameraHImageOutput;
}
else if(cameraBase.CamType == EnumCamType.hik)
{
HikVisionCamera cam = new HikVisionCamera();
cam.CameraName = cameraBase.CameraName;
cam.CameraIP = cameraBase.CameraIP;
cam.IsEnabled=cameraBase.IsEnabled;
HKCameras.Add(cam);
// cam.CameraConnect();
cam.OnHImageOutput += OnCameraHImageOutput;
}
}
}
if(ConfigModel.PLCBaseList.Count > 0)
{
for (int i = 0; i < ConfigModel.PLCBaseList.Count; i++)
{
var plcBase = ConfigModel.PLCBaseList[i];
if(plcBase.PLCType==EnumPLCType.XC网口)
{
PLC.IP = plcBase.IP;
PLC.Enable = plcBase.Enable;
PLC.PLCName = plcBase.PLCName;
PLC.PLCItemList=plcBase.PLCItemList;
PLC.Port= plcBase.Port;
PLC.PLCConnect();
}
}
}
if (ConfigModel.DetectionList.Count > 0)
{
for (int i = 0; i < ConfigModel.DetectionList.Count; i++)
{
DetectionConfig detectionConfig = ConfigModel.DetectionList[i];
var detection = ConfigModel.DetectionList[i];
detectionConfig.CameraCollects = detection.CameraCollects;
detectionConfig.ModelconfThreshold = detection.ModelconfThreshold;
detectionConfig.ModelWidth = detection.ModelWidth;
detectionConfig.ModelHeight = detection.ModelHeight;
detectionConfig.In_lable_path = detection.In_lable_path;
detectionConfig.IsEnabled = detection.IsEnabled;
detectionConfig.ShowLocation.X = detection.ShowLocation.X;
detectionConfig.ShowLocation.Y = detection.ShowLocation.Y;
DetectionConfigs.Add(detectionConfig);
}
}
DetectionConfigs.ForEach(detection =>
{
detection.CameraCollects.ForEach(cam =>
{
List<string> Dets = new List<string>
{
detection.Id
};
if (!_cameraRelatedDetectionDict.ContainsKey(cam.CameraSourceId))
{
_cameraRelatedDetectionDict.Add(cam.CameraSourceId, Dets);
}
else
{
_cameraRelatedDetectionDict[cam.CameraSourceId].Add(detection.Id);
}
}
);
});
string inferenceDevice = "CPU";
//
_visionEngine = new SimboVisionDriver();
_visionEngine.DetectionConfigs = DetectionConfigs;
//初始化模型 加载模型
_visionEngine.Init();
CtrlVisionRunBase ctrlVisionRun = new CtrlVisionRunBase(_visionEngine);
tabImgDisplay.Controls.Add(ctrlVisionRun);
StartProcess();
}
private static int currentRegister = 411; // 初始为 D411
private void StartProcess()
@ -1435,45 +1473,16 @@ namespace DHSoftware
private void HandleSettingsButton()
{
// Add the code for the "设置" button click here
// MessageBox.Show("设置按钮按下");
List<CameraBase> listCamBase = new List<CameraBase>();
CameraBase cam1 = new CameraBase
{
CameraName = "Cam1",
CameraIP = "192.168.1.101",
SerialNumber = "1000001",
Gain = 60,
Exposure = 10,
RotateImage = 0,
IsEnabled = false,
};
CameraBase cam2 = new CameraBase
{
CameraName = "Cam2",
CameraIP = "192.168.1.101",
SerialNumber = "1000002",
Gain = 60,
Exposure = 10,
RotateImage = 0,
IsEnabled = false,
};
listCamBase.Add(cam1);
listCamBase.Add(cam2);
SettingWindow settingWindow = new SettingWindow();
settingWindow.Show();
//s.cameras = listCamBase;
//s.Show();
SettingWindow.Instance.Show();
}
private void HandleLoginButton()
{
// Add the code for the "登录" button click here
LoginWindow loginWindow = new LoginWindow();
loginWindow.Owner = this;
loginWindow.Show();
LoginWindow.Instance.Owner = this;
LoginWindow.Instance.Show();
}
private void splitter1_SplitterMoved(object sender, SplitterEventArgs e)