public override void Close()
 {
     try
     {
         base.Close();
         MCreateShapeModel.Close();
         MCreateShapeModel = null;
         MFindShapeMode.Close();
         MFindShapeMode = null;
         MGrabPointSetting.Close();
         MGrabPointSetting = null;
         MPZT_Detection.Close();
         MPZT_Detection = null;
         MGrab_Intervene.Close();
         MGrab_Intervene = null;
         MLM_DP.Close();
         MLM_DP = null;
         MFangDai.Close();
         MFangDai = null;
         MLanQuan.Close();
         MLanQuan      = null;
         NotifyExcInfo = null;
     }
     catch (Exception ex)
     {
         Util.WriteLog(this.GetType(), ex);
         Util.Notify("工具删除异常");
     }
 }
 //序列化
 public override void SerializeCheck()
 {
     NotifyExcInfo = null;
     MCreateShapeModel.SerializeCheck();
     MFindShapeMode.SerializeCheck();
     MGrabPointSetting.SerializeCheck();
     MPZT_Detection.SerializeCheck();
     MGrab_Intervene.SerializeCheck();
     MLM_DP.SerializeCheck();
     MFangDai.SerializeCheck();
     MLanQuan.SerializeCheck();
     using (Stream objectStream = new MemoryStream())
     {
         System.Runtime.Serialization.IFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
         formatter.Serialize(objectStream, this);
     }
 }
        //主执行程序
        protected override void RunAct()
        {
            base.RunAct();
            if (!engineIsnitial)
            {
                MyEngine = new HDevEngine();
                InitialEngine();
            }
            #region 初始化
            RuningFinish  = false;
            base.Result   = string.Empty;
            base.IsOk     = false;
            base.isRealOk = false;
            //功能块
            bFindShapeMode         = false;
            bGrabPointSetting      = false;
            bPZT_Detection_Result  = false;
            bGrab_Intervene_Result = false;
            bLM_DP_Result          = false;
            bFangDai_Result        = false;
            bLanQuan_Result        = false;
            #endregion

            if (ImageTestIn == null || !ImageTestIn.IsInitialized())
            {
                return;
            }
            HImage R, G, B;
            R = ImageTestIn.Decompose3(out G, out B);

            #region 创建模板
            if (MCreateShapeModel.hShapeModel == null || !MCreateShapeModel.hShapeModel.IsInitialized())
            {
                if (!MCreateShapeModel.CreateShapeModelAct(ImageRefIn))
                {
                    Util.Notify("创建模板异常");
                    if (NotifyExcInfo != null)
                    {
                        NotifyExcInfo("创建模板异常");
                    }
                    return;
                }
            }
            #endregion

            HTuple t1;
            HOperatorSet.CountSeconds(out t1);

            #region 模板匹配

            bFindShapeMode = MFindShapeMode.FindShapeModeAct(ImageRefIn, MCreateShapeModel, ImageTestIn);
            HTuple t2;
            HOperatorSet.CountSeconds(out t2);
            double time = (t2 - t1).D * 1000;
            Util.Notify("模板匹配用时:" + time.ToString("F2") + "ms");
            if (NotifyExcInfo != null)
            {
                NotifyExcInfo("模板匹配用时:" + time.ToString("F2") + "ms");
            }

            if (!bFindShapeMode)
            {
                Util.Notify("查找模板异常");
                if (NotifyExcInfo != null)
                {
                    NotifyExcInfo("查找模板异常");
                }
                return;
            }
            List <HHomMat2D> homMat2Ds = new List <HHomMat2D>();
            homMat2Ds = MFindShapeMode.GetHHomMat2Ds();

            if (MFindShapeMode.row == null || MFindShapeMode.row.Length < 1 || homMat2Ds == null || homMat2Ds.Count < 1)
            {
                if (NotifyExcInfo != null)
                {
                    NotifyExcInfo("未找到模板");
                }
                base.IsOk     = true;
                base.isRealOk = true;
                return;
            }
            if (bFindShapeMode)
            {
                StringBuilder strb = new StringBuilder();
                strb.Append("查找结果:");
                strb.Append(Environment.NewLine);
                int count = MFindShapeMode.row_temp.Length;
                for (int i = 0; i < count; i++)
                {
                    HTuple phi;
                    HOperatorSet.TupleDeg(MFindShapeMode.angle_temp[i], out phi);
                    strb.Append("第" + (i + 1) + "个:\r\n");
                    string mes = string.Format(
                        "Row:{0:F2}\r\n" + "Col:{1:F2}\r\n" + "角度:{2:F2}\r\n" + "得分:{3:F2}"
                        , MFindShapeMode.row_temp[i].D, MFindShapeMode.column_temp[i].D, phi.D, MFindShapeMode.score_temp[i].D);
                    strb.Append(mes);
                    strb.Append(Environment.NewLine);
                }

                int blob_feature_lengt = MFindShapeMode.LG_Area.Length;
                for (int i = 0; i < blob_feature_lengt; i++)
                {
                    strb.Append("第" + (i + 1) + "个:\r\n");
                    string mes = string.Format(
                        "螺杆面积:{0:F2}\r\n" + "螺杆长:{1:F2}\r\n" + "垫片宽:{2:F2}\r\n"
                        , MFindShapeMode.LG_Area[i].D, MFindShapeMode.LG_lenght[i].D, MFindShapeMode.DP_width[i].D);
                    strb.Append(mes);
                    strb.Append(Environment.NewLine);
                }

                if (NotifyExcInfo != null)
                {
                    NotifyExcInfo(strb.ToString());
                }
                Util.Notify(strb.ToString());
            }



            #endregion



            #region 抓取点
            bGrabPointSetting = mGrabPointSetting.setTarget(homMat2Ds);
            if (!bGrabPointSetting)
            {
                Util.Notify("抓取点异常");
                if (NotifyExcInfo != null)
                {
                    NotifyExcInfo("抓取点异常");
                }
            }
            else
            {
                StringBuilder strd = new StringBuilder();
                for (int i = 0; i < MGrabPointSetting.GrabRowTarget.Length; i++)
                {
                    string mes = string.Format("抓取点像素坐标:" + "第" + (i + 1) + "个像素坐标:\r\n" +
                                               "Row: {0:F2}\r\n" +
                                               "Col: {1:F2}"
                                               , MGrabPointSetting.GrabRowTarget[i].D, MGrabPointSetting.GrabColTarget[i].D);
                    strd.Append(mes);
                    strd.Append(Environment.NewLine);
                }
                Util.Notify(strd.ToString());
                if (NotifyExcInfo != null)
                {
                    NotifyExcInfo(strd.ToString());
                }
            }
            #endregion

            HTuple t3;
            HOperatorSet.CountSeconds(out t3);
            time = (t3 - t2).D * 1000;
            Util.Notify("抓取点用时:" + time.ToString("F2") + "ms");
            if (NotifyExcInfo != null)
            {
                NotifyExcInfo("抓取点用时:" + time.ToString("F2") + "ms");
            }

            #region 膨胀套检测
            if (bPZT_Detection_Enble)
            {
                bPZT_Detection_Result = MPZT_Detection.PZT_Detection_Act(R, homMat2Ds, MFindShapeMode.angle);
                if (bPZT_Detection_Result)
                {
                    StringBuilder strb = new StringBuilder();
                    strb.Append("膨胀套:");
                    strb.Append(Environment.NewLine);
                    int count = MPZT_Detection.dist_PZT.Length;
                    for (int i = 0; i < count; i++)
                    {
                        string mes = string.Format("第" + (i + 1) + "个:\r\n" +
                                                   "长度: {0:F2}"
                                                   , MPZT_Detection.dist_PZT[i].D);
                        strb.Append(mes);
                        strb.Append(Environment.NewLine);
                    }
                    if (NotifyExcInfo != null)
                    {
                        NotifyExcInfo(strb.ToString());
                    }
                    Util.Notify(strb.ToString());
                }
                else
                {
                    Util.Notify("膨胀套检测异常");
                    if (NotifyExcInfo != null)
                    {
                        NotifyExcInfo("膨胀套检测异常");
                    }
                }
            }
            else
            {
                bPZT_Detection_Result = true;
            }

            #endregion

            HTuple t4;
            HOperatorSet.CountSeconds(out t4);
            time = (t4 - t3).D * 1000;
            Util.Notify("膨胀套用时:" + time.ToString("F2"));
            if (NotifyExcInfo != null)
            {
                NotifyExcInfo("膨胀套用时:" + time.ToString("F2") + "ms");
            }

            #region 抓取干涉
            if (bGrab_Intervene_Enble)
            {
                bGrab_Intervene_Result = MGrab_Intervene.Grab_Intervene_Act(G, homMat2Ds);
                if (bGrab_Intervene_Result)
                {
                    StringBuilder strb = new StringBuilder();
                    strb.Append("抓取干涉:");
                    strb.Append(Environment.NewLine);
                    int count = MGrab_Intervene.grabInterveneOkNg.Length;
                    for (int i = 0; i < count; i++)
                    {
                        string mes = string.Format("第" + (i + 1) + "个:\r\n" +
                                                   "是否干涉: {0}"
                                                   , MGrab_Intervene.grabInterveneOkNg[i].I > 0 ? "不干涉" : "干涉");
                        strb.Append(mes);
                        strb.Append(Environment.NewLine);
                    }
                    if (NotifyExcInfo != null)
                    {
                        NotifyExcInfo(strb.ToString());
                    }
                    Util.Notify(strb.ToString());
                }
                else
                {
                    Util.Notify("抓取干涉检测异常");
                    if (NotifyExcInfo != null)
                    {
                        NotifyExcInfo("抓取干涉检测异常");
                    }
                }
            }
            else
            {
                bGrab_Intervene_Result = true;
            }

            #endregion

            HTuple t5;
            HOperatorSet.CountSeconds(out t5);
            time = (t5 - t4).D * 1000;
            Util.Notify("抓取干涉用时:" + time.ToString("F2") + "ms");
            if (NotifyExcInfo != null)
            {
                NotifyExcInfo("抓取干涉用时:" + time.ToString("F2") + "ms");
            }


            #region 螺母垫片有无检测
            if (bLM_DP_Enble)
            {
                bLM_DP_Result = MLM_DP.LM_DP_Act(G, homMat2Ds);
                if (bLM_DP_Result)
                {
                    StringBuilder strb = new StringBuilder();
                    strb.Append("螺母垫片:");
                    strb.Append(Environment.NewLine);
                    int count = MLM_DP.lM_DP_OkNg.Length;
                    for (int i = 0; i < count; i++)
                    {
                        string mes = string.Format("第" + (i + 1) + "个:\r\n" +
                                                   "螺母垫片有无: {0}", MLM_DP.lM_DP_OkNg[i].I > 0 ? "有" : "无");
                        strb.Append(mes);
                        strb.Append(Environment.NewLine);
                        string temp = string.Format("第" + (i + 1) + "个:\r\n" +
                                                    "区块数量{0}; 最大区块面积{1}", MLM_DP.lM_DP_Num[i].I.ToString(), MLM_DP.lM_DP_Area[i].I.ToString());
                        strb.Append(temp);
                        strb.Append(Environment.NewLine);
                    }
                    if (NotifyExcInfo != null)
                    {
                        NotifyExcInfo(strb.ToString());
                    }
                    Util.Notify(strb.ToString());
                }
                else
                {
                    Util.Notify("螺母垫片有无: 检测异常");
                    if (NotifyExcInfo != null)
                    {
                        NotifyExcInfo("螺母垫片有无: 检测异常");
                    }
                }
            }
            else
            {
                bLM_DP_Result = true;
            }
            #endregion

            HTuple t6;
            HOperatorSet.CountSeconds(out t6);
            time = (t6 - t5).D * 1000;
            Util.Notify("螺母垫片用时:" + time.ToString("F2") + "ms");
            if (NotifyExcInfo != null)
            {
                NotifyExcInfo("螺母垫片用时:" + time.ToString("F2") + "ms");
            }

            #region 防呆
            if (bFangDai_Enable)
            {
                bFangDai_Result = MFangDai.FangDai_Act(G, homMat2Ds);
                if (bFangDai_Result)
                {
                    StringBuilder strb = new StringBuilder();
                    strb.Append("防呆:");
                    strb.Append(Environment.NewLine);
                    int count = MFangDai.Area.Length;
                    for (int i = 0; i < count; i++)
                    {
                        string mes = string.Format("第" + (i + 1) + "个:\r\n" +
                                                   "面积: {0:F2}"

                                                   , MFangDai.Area[i].D);
                        strb.Append(mes);
                        strb.Append(Environment.NewLine);
                    }
                    if (NotifyExcInfo != null)
                    {
                        NotifyExcInfo(strb.ToString());
                    }
                    Util.Notify(strb.ToString());
                }
                else
                {
                    Util.Notify("防呆检测异常");
                    if (NotifyExcInfo != null)
                    {
                        NotifyExcInfo("防呆检测异常");
                    }
                }
            }
            else
            {
                bFangDai_Result = true;
            }
            #endregion
            HTuple t7;
            HOperatorSet.CountSeconds(out t7);
            time = (t7 - t6).D * 1000;
            Util.Notify("防呆用时:" + time.ToString("F2") + "ms");
            if (NotifyExcInfo != null)
            {
                NotifyExcInfo("防呆用时:" + time.ToString("F2") + "ms");
            }

            #region 蓝圈
            if (bLanQuan_Enable)
            {
                bLanQuan_Result = MLanQuan.LanQuan_Act(R, B, homMat2Ds);
                if (bLanQuan_Result)
                {
                    StringBuilder strb = new StringBuilder();
                    strb.Append("蓝圈:");
                    strb.Append(Environment.NewLine);
                    int count = MLanQuan.Area.Length;
                    for (int i = 0; i < count; i++)
                    {
                        string mes = string.Format("第" + (i + 1) + "个:\r\n" +
                                                   "面积: {0:F2}\r\n"
                                                   , MLanQuan.Area[i].D);
                        strb.Append(mes);
                        strb.Append(Environment.NewLine);
                    }
                    if (NotifyExcInfo != null)
                    {
                        NotifyExcInfo(strb.ToString());
                    }
                    Util.Notify(strb.ToString());
                }
                else
                {
                    Util.Notify("蓝圈检测异常");
                    if (NotifyExcInfo != null)
                    {
                        NotifyExcInfo("蓝圈检测异常");
                    }
                }
            }
            else
            {
                bLanQuan_Result = true;
            }
            #endregion
            HTuple t8;
            HOperatorSet.CountSeconds(out t8);
            time = (t8 - t7).D * 1000;
            Util.Notify("蓝圈用时:" + time.ToString("F2") + "ms");
            if (NotifyExcInfo != null)
            {
                NotifyExcInfo("蓝圈用时:" + time.ToString("F2") + "ms");
            }


            RuningFinish  = true;
            base.IsOk     = bFindShapeMode && bGrabPointSetting && bPZT_Detection_Result && bGrab_Intervene_Result && bLM_DP_Result && bFangDai_Result && bLanQuan_Result;
            base.isRealOk = true;
            if (R != null && R.IsInitialized())
            {
                R.Dispose();
            }
            if (G != null && G.IsInitialized())
            {
                G.Dispose();
            }
            if (B != null && B.IsInitialized())
            {
                B.Dispose();
            }
        }
        public override void ShowResult(HWndCtrl viewCtrl)
        {
            if (!bFindShapeMode)
            {
                return;
            }
            if (MFindShapeMode.row == null || MFindShapeMode.row.Length < 1)
            {
                return;
            }
            if (RuningFinish == false)
            {
                return;
            }
            MFindShapeMode.ShowResult(viewCtrl);
            if (bGrabPointSetting)
            {
                MGrabPointSetting.ShowGrabPoint(viewCtrl);
            }
            if (bPZT_Detection_Result && bPZT_Detection_Enble)
            {
                MPZT_Detection.Show(viewCtrl);
            }

            if (bLM_DP_Enble && bLM_DP_Result)
            {
                MLM_DP.Show(viewCtrl);
            }

            if (bGrab_Intervene_Result && bGrab_Intervene_Enble)
            {
                MGrab_Intervene.Show(viewCtrl);
            }
            if (bFangDai_Enable && bFangDai_Result)
            {
                MFangDai.Show(viewCtrl);
            }
            if (bLanQuan_Enable && bLanQuan_Result)
            {
                MLanQuan.Show(viewCtrl);
            }

            string temp = GetSendResult();

            if (NotifyExcInfo != null)
            {
                NotifyExcInfo("发送结果:" + temp);
            }

            if (row_Send != null && row_Send.Length > 0)
            {
                for (int i = 0; i < row_Send.Length; i++)
                {
                    if (id_Send[i].I == 1)
                    {
                        viewCtrl.AddText("OK", (int)(row_Send[i].D), (int)(col_Send[i].D), 80, "green");
                    }
                }
            }

            if (row_NG == null || row_NG.Length < 1)
            {
                return;
            }
            for (int i = 0; i < row_NG.Length; i++)
            {
                viewCtrl.AddText("NG:" + NG_Reason[i].S, (int)(row_NG[i].D), (int)(col_NG[i].D), 40, "red");
            }
        }