FrmAOICheck.cs 11.0 KB

using AccAOI.camera;
using AOI;
using HalconDotNet;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using TSA_V.Common;
using TSA_V.DeviceLibrary;
using System.Linq;

namespace TSA_V
{
    public partial class FrmAOICheck : FrmBase
    {
        private List<ResultBean> ResultList = new List<ResultBean>();
        private    AOI.AoiProject CurrProject = null;
        private DateTime showTime = DateTime.Now;
        private DateTime endTime = DateTime.Now;
        private bool IsCheck = false;
        /// <summary>
        /// 检测结果,0=未知,1=OK,2=NG
        /// </summary>
        private int  CheckResult =0;
        private int waitSeconds = 1;
        public FrmAOICheck(AoiProject currAoi )
        {
            InitializeComponent();
            CurrProject = currAoi;
        }
        private void FrmCamera_Load(object sender, EventArgs e)
        {
            FormStatus(false);
            timer1.Start();
            lblCurrWorkTime.Visible = true;
            //默认显示程序配置的 图片
        
            if (CurrProject == null)
            {
                this.Close();
                return;
            }
            lblCurrWorkTime.Text = ResourceCulture.GetString("{0}秒后自动检测", "{0}秒后自动检测", waitSeconds.ToString());
            //  this.picResult.Image = CurrProject.standardImage;
        }

        private void FormStatus(bool open)
        {
        }
        private bool isAutoClose = false;
        private void UpdateTextResult(Label label, int result)
        {
            if (result.Equals(0))
            {
                label.ForeColor = Color.Orange;
                label.Text = "?";
            }
            else if (result.Equals(1))
            {
                label.Text = "OK";
                label.ForeColor = Color.Green;
            }
            else if (result.Equals(2))
            {
                label.Text = "NG";
                label.ForeColor = Color.Red;
            }
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            btnRun.Visible = !IsCheck;
           
            TimeSpan span = DateTime.Now - showTime;
            if (!IsCheck)
            { 
                int times = waitSeconds - (int)(span.TotalSeconds);
                if (times > 0)
                { 
                    if (times <= 1)
                    {
                        lblCurrWorkTime.Text = ResourceCulture.GetString("开始自动检测", "开始自动检测") + "......";
                    }else
                    {
                        lblCurrWorkTime.Text = ResourceCulture.GetString("{0}秒后自动检测", "{0}秒后自动检测", times.ToString());
                    }
                    lblCurrWorkTime.Visible = true;
                }
                else
                {
                    lblCurrWorkTime.Text = ResourceCulture.GetString("开始自动检测", "开始自动检测");
                    lblCurrWorkTime.Visible = true;
                    CheckResult = AoiCheck();
                    MesUtil.SetAOIResult(CheckResult);
                    UpdateTextResult(lblResult, CheckResult);
                    if (CheckResult.Equals(1))
                    {
                        isAutoClose = true;
                    }
                    else
                    {
                        isAutoClose = false;
                    }
                }
            }
            else
            {
                lblCurrWorkTime.Text = ResourceCulture.GetString("请点击完成按钮结束检测", "请点击完成按钮结束检测", 0);
                lblCurrWorkTime.Visible = true;
                if (isAutoClose)
                {
                    TimeSpan closeSpan = DateTime.Now - endTime;
                    int times = 3 - (int)(closeSpan.TotalSeconds);
                    if (times < 0)
                    {
                        lblCurrWorkTime.Text = ResourceCulture.GetString("{0}秒后自动完成", "{0}秒后自动完成", 0);
                        lblCurrWorkTime.Visible = true;
                        this.Close();
                    }
                    else
                    {
                        lblCurrWorkTime.Text = ResourceCulture.GetString("{0}秒后自动完成", "{0}秒后自动完成", times.ToString());
                        lblCurrWorkTime.Visible = true;
                    }
                }
            }
        }

        private int AoiCheck()
        {
            GC.Collect(0, GCCollectionMode.Forced);
            //GC.Collect();
            IsCheck = true;
            btnRun.Visible = false;
            lblError.Visible = false;
            lblInfo.Visible = false;
            int result = 2;
            try
            {
                if (CurrProject == null)
                {
                    return 0;
                }
                chbAllShow.Enabled = false;
                
                string camera = ConfigAppSettings.GetValue(Setting_Init.CameraName);
                camera = AccAOI.camera.CameraManager.hikNameList.ToArray().FirstOrDefault();
                Image currImage = CameraManager.GetCamerImage(camera);
                if (currImage != null)
                {
                    //Bitmap bitmap = new Bitmap(currImage);
                    Image outImage = null;
                    List<ResultBean> resultBean = CurrProject.CheckAll(currImage, out outImage);
                    currImage.Dispose();
                    ResultList = resultBean;
                    if (outImage != null)
                    {
                        //this.picResult.SelectNone();
                        //picResult.SizeMode = Acc.ImageBox.ImageBoxSizeMode.Fit;
                        this.picResult.Image = outImage;
                        //picResult.SizeMode = Acc.ImageBox.ImageBoxSizeMode.Normal;
                       // pictureBox1.Image = outImage;
                      
                    }
                    result = 1;
                    endTime = DateTime.Now;
                    string ngList = "NG:\r\n";
                    listErrorView.Items.Clear();
                    foreach (ResultBean bean in resultBean)
                    {
                        if (bean.result.Equals(false))
                        {
                            ngList += bean.MethodName + "\r\n";
                            result = 2;
                            //  break;
                            listErrorView.Items.Add(GetShowName(bean));
                        }else if (chbAllShow.Checked)
                        {

                            listErrorView.Items.Add(GetShowName(bean));
                        }
                    }
                    if (listErrorView.Items.Count > 0)
                    {
                        listErrorView.Items[0].Selected = true ;
                    }

                    chbAllShow.Enabled = true;
                    if (result.Equals(1))
                    {
                        lblError.Visible = false ;
                        lblInfo.Text = AccAOI.ControlUtil.GetResultsStr(resultBean);
                        lblInfo.Visible = true;
                        LogUtil.info("AOI检测OK:" + lblInfo.Text);
                    }
                    else
                    {
                        lblInfo.Visible = false;
                        lblError.Text = ngList;
                        lblError.Visible = true;
                        LogUtil.info("AOI检测NG:" + ngList);
                    }
                }
                else
                {
                    lblError.Text = ResourceCulture.GetString("获取相机图片失败", "获取相机图片失败");
                    lblError.Visible = true;
                    LogUtil.error("AOI检测:获取相机图片失败:" + CameraManager.ErrorMsg);
                    return 0;
                }
                chbAllShow.Enabled = true  ;
            }
            catch (Exception ex)
            {
                lblError.Text = ResourceCulture.GetString("执行检测失败", "执行检测失败");
                lblError.Visible = true;
                LogUtil.error("检测出错:" + ex.ToString());
            }
            return result;
        }
        private string GetShowName(ResultBean bean)
        {
            return (bean.result ? "✔ " : "✘ ") + bean.MethodName;
        }
        private string GetNameByText(string str)
        {
            string name= str.Substring(2, str.Length - 2);
            return name;
        }
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnRun_Click(object sender, EventArgs e)
        {
            CheckResult = AoiCheck();
            MesUtil.SetAOIResult(CheckResult);
            UpdateTextResult(lblResult, CheckResult); 
            if (CheckResult.Equals(1))
            {
                isAutoClose = true;
            }
            else
            {
                isAutoClose = false;
            }
        }

        private void FrmCameraAOI_Shown(object sender, EventArgs e)
        {
            showTime = DateTime.Now;
        }

        private void btnExit_Click_1(object sender, EventArgs e)
        {
            timer1.Stop();
            this.Close();
        }

        private void listErrorView_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listErrorView.SelectedItems != null && listErrorView.SelectedItems.Count > 0)
            {
                string selName = GetNameByText(listErrorView.SelectedItems[0].Text) ;
                List<ResultBean> beans = (from m in ResultList where m.MethodName.Equals(selName) select m).ToList<ResultBean>();
                if (beans.Count > 0)
                {
                    ResultBean bean = beans[0];
                    picStand.Image = bean.standardRoiImage;
                    picCurr.Image = bean.currentRoiImage;
                    picResult.SelectionRegion = bean.roiPath;

                    //Graphics g = picResult.CreateGraphics();
                    //g.DrawPath(new Pen(Color.Orange), bean.roiPath);
                    //g.Dispose();
                }
            }
        }

        private void chbAllShow_CheckedChanged(object sender, EventArgs e)
        {
            if (ResultList.Count <= 0)
            {
                return;
            }
            listErrorView.Items.Clear();
            foreach (ResultBean bean in ResultList)
            {
                if (bean.result.Equals(false))
                {
                    listErrorView.Items.Add(GetShowName(bean));
                }
                else if (chbAllShow.Checked)
                { 
                    listErrorView.Items.Add(GetShowName(bean));
                }
            }
            if (listErrorView.Items.Count > 0)
            {
                listErrorView.Items[0].Selected = true;
            }
            listErrorView_SelectedIndexChanged(null, null);
        }
    }
}