using HalconDotNet;
|
using LB_SmartVision.Forms.Pages.SettingPage;
|
using LB_SmartVision.ProcessRun;
|
using LB_VisionControl;
|
|
|
namespace LB_SmartVision.Forms.Pages.ProcessPage
|
{
|
public partial class ProcessControl : UserControl
|
{
|
UserHSmartWindowControl UserHSmartWindowControl = new UserHSmartWindowControl();
|
|
ProcessRunBll ProcessRunBll
|
{
|
get
|
{
|
if (GlobalVar.dicProcesses.ContainsKey(_Layout.ProcessName))
|
return GlobalVar.dicProcesses[_Layout.ProcessName];
|
else
|
return null;
|
}
|
}
|
|
Layout _Layout { get; set; } = new Layout();
|
|
public ProcessControl()
|
{
|
InitializeComponent();
|
}
|
|
public ProcessControl(Layout layout)
|
{
|
InitializeComponent();
|
_Layout = layout;
|
}
|
|
private void ProcessControl_Load(object sender, EventArgs e)
|
{
|
this.panel1.Controls.Add(this.UserHSmartWindowControl);
|
this.UserHSmartWindowControl.Dock = DockStyle.Fill;
|
|
SetTitle(_Layout.ProcessName);
|
|
if (ProcessRunBll != null)
|
this.label1.Text = $"总数:{ProcessRunBll.total}";
|
this.isClosed = false ;
|
var TaskPhotoContinue = Task.Factory.StartNew(() =>
|
{
|
ThreadCircleRun();
|
});
|
}
|
|
public void SetTitle(string title)
|
{
|
// 如果当前不是 UI 线程,则通过 Invoke 将操作调度到 UI 线程
|
if (this.InvokeRequired)
|
{
|
this.BeginInvoke(new Action<string>((title) =>
|
{
|
this.lblTitle.Text = title;
|
}), title);
|
}
|
else
|
{
|
// 如果已经在 UI 线程上,直接更新
|
this.lblTitle.Text = title;
|
}
|
}
|
|
public void ClearObj()
|
{
|
if (this.InvokeRequired)
|
{
|
this.BeginInvoke(new Action(() =>
|
{
|
UserHSmartWindowControl.ClearObj();
|
}));
|
}
|
else
|
UserHSmartWindowControl.ClearObj();
|
}
|
|
/// <summary>
|
/// 异步显示图片
|
/// </summary>
|
/// <param name="ho_image"></param>
|
public void ShowHoImage(HObject ho_image)
|
{
|
UserHSmartWindowControl.hImage = ho_image;
|
|
if (this.InvokeRequired)
|
{
|
this.BeginInvoke(new Action(() =>
|
{
|
this.label1.Text = $"总数:{ProcessRunBll.total}";
|
}));
|
}
|
else
|
this.label1.Text = $"总数:{ProcessRunBll.total}";
|
}
|
|
public bool Run(out string msg)
|
{
|
if (ProcessRunBll == null)
|
{
|
msg = $"流程{_Layout.ProcessName}不存在";
|
return false;
|
}
|
|
if (isCircleRuning || ProcessRunBll.bRuning)
|
ProcessRunBll.LogInfo($"{ProcessRunBll.Name}正在运行", LogInfoType.ERROR);
|
|
ProcessRunBll.Run();
|
|
msg = ProcessRunBll.Msg;
|
return ProcessRunBll.Result;
|
}
|
|
private void btnRun_Click(object sender, EventArgs e)
|
{
|
this.BeginInvoke(new Action(() =>
|
{
|
string msg = string.Empty;
|
bool result = false;
|
try
|
{
|
if (ProcessRunBll == null)
|
return;
|
|
ProcessRunBll.LogInfo(string.Format("流程[{0}]开始运行", this.ProcessRunBll.Name), LogInfoType.INFO);
|
|
ClearObj();
|
result = Run(out msg);
|
|
if (ProcessRunBll.GetImage(_Layout, out _, out HObject RecordImage))
|
ShowHoImage(RecordImage);
|
}
|
catch { msg = "运行出现了异常"; }
|
|
ProcessRunBll.LogInfo(string.Format("流程[{0}]运行完成,结果:{1}", this.ProcessRunBll.Name, msg)
|
, result ? LogInfoType.PASS : LogInfoType.ERROR);
|
}));
|
}
|
|
|
CancellationTokenSource ctsTask = new CancellationTokenSource();
|
bool isCircleRuning = false;
|
private void btnCircleRun_Click(object sender, EventArgs e)
|
{
|
try
|
{
|
isCircleRuning = !isCircleRuning;
|
btnRun.Enabled = !isCircleRuning;
|
|
if (isCircleRuning)
|
{
|
this.mAutoResetEvent.Set();
|
}
|
else
|
{
|
isCircleRuning = false;
|
}
|
if (!isCircleRuning)
|
{
|
ProcessRunBll.LogInfo($"{ProcessRunBll.Name}关闭连续运行", LogInfoType.PASS);
|
return;
|
}
|
|
ProcessRunBll.LogInfo($"{ProcessRunBll.Name}开启连续运行", LogInfoType.WARN);
|
}
|
catch { }
|
}
|
private AutoResetEvent mAutoResetEvent = new AutoResetEvent(false);
|
bool isClosed = false;
|
|
void ThreadCircleRun()
|
{
|
while (!isClosed)
|
{
|
try
|
{
|
if (isCircleRuning == false)
|
{
|
this.mAutoResetEvent.WaitOne();
|
}
|
|
ProcessRunBll.LogInfo($"{ProcessRunBll.Name}开始运行", LogInfoType.WARN);
|
ClearObj();
|
bool result = ProcessRunBll.Run();
|
string msg = result ? "运行成功" : ProcessRunBll.Msg;
|
|
ProcessRunBll.LogInfo($"{ProcessRunBll.Name}运行结束,结果为:{msg}"
|
, result ? LogInfoType.PASS : LogInfoType.ERROR);
|
|
if (ProcessRunBll.GetImage(_Layout, out _, out HObject RecordImage))
|
ShowHoImage(RecordImage);
|
}
|
catch { }
|
Thread.Sleep(33);
|
}
|
}
|
|
System.Windows.Forms.ToolTip TotalToolTip = new System.Windows.Forms.ToolTip();
|
|
private void lblCount_DoubleClick(object sender, EventArgs e)
|
{
|
this.BeginInvoke(() =>
|
{
|
ProcessRunBll.ClearTotal();
|
this.label1.Text = $"总数:{ProcessRunBll.total}";
|
});
|
}
|
|
private void lblCount_MouseHover(object sender, EventArgs e)
|
{
|
// 显示ToolTip(相对于控件的位置)
|
TotalToolTip.Show($"直通率:{ProcessRunBll.Rate_OK.ToString("F3")}", (Control)sender,
|
((Control)sender).Width / 2, // x偏移
|
-20, // y偏移(负值表示上方)
|
2000); // 显示时间(ms)
|
}
|
}
|
}
|