FrmTSAV.cs 33.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942
using System;
using System.Drawing;
using System.Windows.Forms;
using MetroFramework.Forms;
using TSA_V.DeviceLibrary;
using TSA_V.Common; 
using System.Timers;
using log4net;
using System.Reflection;
using System.IO;
using System.Threading;
using System.Collections.Generic;
using TSA_V.LoadCSVLibrary;
using PUSICANLibrary;
using System.Runtime.InteropServices;
using AOI;
using System.Threading.Tasks;

namespace TSA_V
{
    public partial class FrmWork : FrmBase
    {
        public FrmWork()
        {
            InitializeComponent();
        }
        private bool isInitOk = false;
        private BoardInfo board = BoardManager.CurrBoard;
        private PointDisplay display = new PointDisplay();
        private List<SMTPointInfo> workSmtList = new List<SMTPointInfo>();
        private void FrmWelding_Load(object sender, EventArgs e)
        {
            btnCamera.Visible = TSAVBean.IsNeedAOI;
            TSAVBean.Work.WorkType = 1;
            LanguageProcess();
            BoardInfo board = BoardManager.CurrBoard;
             
            if (board != null)
            {
                this.Text = "Smart Workstation:" + board.boardName;
                workSmtList = board.GetSmtList();
                preIndex = -1;
                smtPoint = new SMTPointInfo();

                timerShowForm.Stop();
                if (workSmtList.Count > 0)
                {
                    smtPoint = workSmtList[0];
                }
                ShowMsg();

                picBoard.Image = board.GetImage();
                loadPictureBoxSize(board);
                LoadPoint();
                txtAuToTime.Text = TSAVBean.AuToModeSeconds.ToString();
                if (TSAVBean.WorkMode.Equals(0))
                {
                    radioButton1.Checked = true;
                    AutoVisiable(false);
                }
                else
                {
                    radioButton2.Checked = true;
                    AutoVisiable(true);
                }
            }
            else
            {
                LogUtil.info("配置程序之后才能工作!");
                this.Close();
            }
            if (TSAVBean.IsNeedAOI)
            {
                LoadAoi();
                if (CurrProject == null)
                {
                    this.Close();
                }
            }
            if (!IsSet)
            {
                TSAVBean.InputCodeEvent += TSAVBean_InputCodeEvent;
                IsSet = true;
            }
            isInitOk = true; 
            //  string ip = ConfigAppSettings.GetValue(Setting_Init.StatusServerIp);
            //      bool result = StatusClient.instance.Connect();
            //   LogUtil.info("连接状态服务器【" + ip + "】【" + result + "】【" + StatusClient.instance.ErrInfo + "】"); 
            //  StatusClient.instance.SendNew(Color.Green, TSAVBean.Name + "进入待机状态");
        }

        private AOI.AoiProject CurrProject = null;
        private void LoadAoi()
        {
            try
            {
                Thread.Sleep(1);
                string currProjectName = BoardManager.CurrBoard.GetAoiFileName();
                if (File.Exists(currProjectName))
                {
                    string msg = "";
                    CurrProject = AoiProject.Load(currProjectName, out msg);
                    if (!msg.Equals(""))
                    {
                        LogUtil.error("加载项目" + currProjectName + "失败:\r\n" + msg);
                        CurrProject = null;
                    }
                    else
                    {
                        LogUtil.error("加载项目" + currProjectName + "成功");
                    }
                }
                else
                {
                    LogUtil.error("加载AOI项目" + currProjectName + ",文件不存在");
                    CurrProject = null;
                }
            }
            catch (Exception ex)
            {
                LogUtil.error("加载aoi项目出错:" + ex.ToString());
            }
        }
        private void AutoVisiable(bool isShow)
        {
            txtAuToTime.Visible = isShow;
            btnUpdateAutoTime.Visible = isShow;
            label7.Visible = isShow;
            label8.Visible = isShow;
        }
        private void LoadPoint()
        {
            if (smtPoint == null)
            {
                return;
            }
            if (pointXMap.ContainsKey(smtPoint.pointNum))
            {
                preShowPointX = pointXMap[smtPoint.pointNum];
                preShowPointY = pointYMap[smtPoint.pointNum];
            }
            else
            { //画图
                int width = Convert.ToInt32(board.boardWidth);
                int height = Convert.ToInt32(board.boardLength);
                Graphics grfx = picBoard.CreateGraphics();
                preShowPointX = Math.Abs(smtPoint.PositionX) * picBoard.Width / height;
                preShowPointY = Math.Abs(smtPoint.PositionY) * picBoard.Width / height;
            }
            Crop(Brushes.Red);
            if (timerShowForm.Enabled.Equals(false))
            {
                timerShowForm.Start();
            }
        }
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (btnPrePoint.Enabled.Equals(true) && btnNextPoint.Enabled.Equals(true))
            {
                // 按快捷键Ctrl+S执行按钮的点击事件方法
                if (keyData.Equals(Keys.Up) || keyData.Equals(Keys.MButton | Keys.Space))
                {
                    btnPrePoint.PerformClick();
                    return true;
                }
                if (keyData.Equals(Keys.Down) || keyData.Equals(Keys.Back | Keys.Space))
                {
                    this.btnNextPoint.PerformClick();
                    return true;
                }
            }
            return base.ProcessCmdKey(ref msg, keyData); // 其他键按默认处理 
        }
        private void StartRun()
        {
            if (TSAVBean.Status <= TSAVStatus.Wait)
            {
                LogUtil.info("开始启动设备");
                string str = TSAVBean.StartRun();
                if (!str.Equals(""))
                {
                    lblMsg.Text = ResourceCulture.GetString(ResourceCulture.StartError, "启动失败") + ":" + str;
                    LogUtil.error("启动失败:" + str);
                    MessageBox.Show(ResourceCulture.GetString(ResourceCulture.StartError, "启动失败:") + str);
                } 
            }
            timer.Start();
        }


        private Dictionary<int, double> pointXMap = new Dictionary<int, double>();
        private Dictionary<int, double> pointYMap = new Dictionary<int, double>();
        private Bitmap bitmap = null;
        private void loadPictureBoxSize(BoardInfo board)
        {
            display.LoadWorkFromPoint(board, smtPoint,true);
            updatePicPointSize(picBoard.Width, picBoard.Height);
            pointXMap = display.pointXMap;
            pointYMap = display.pointYMap;

        }
        private void updatePicPointSize(int pWidth, int pHeight)
        {
            pWidth = pWidth * 100;
            pHeight = pHeight * 100;
            bool ischange = false;
            if (pWidth > panPoint.Width)
            {
                picPoint.Height = pHeight * panPoint.Width / pWidth;
                picPoint.Width = panPoint.Width;
                ischange = true;
            }
            if (picPoint.Height > panPoint.Height)
            {
                picPoint.Width = pWidth * panPoint.Height / pHeight;
                picPoint.Height = panPoint.Height;
                ischange = true;
            }
            if (!ischange)
            {
                picPoint.Height = pHeight;
                picPoint.Width = pWidth;
            }
        } 
        private void timer_Tick(object sender, EventArgs e)
        {
            try
            {                
                TimeSpan ss = DateTime.Now - LastTime;
                if (ss.TotalSeconds > TSAVBean.AuToModeSeconds)
                {
                    LastTime = DateTime.Now;
                    int value = ConfigAppSettings.GetIntValue("CodeRun");
                    if (value.Equals(1))
                    {
                        btnNextPoint_Click(null, null);
                    }
                }
                FormStatus();
                lblMsg.Text = TSAVBean.GetShowMsg();

                if (TSAVBean.Status.Equals(TSAVStatus.Runing))
                {
                    //如果没有开始工作,自动开始工作
                    //if (!TSAVBean.Work.IsWorking && btnStartWorking.Enabled)
                    //{
                    //    btnStartWorking_Click(null, null);
                    //}
                    if (!preIndex.Equals(TSAVBean.Work.currIndex) && (TSAVBean.Work.currPoint != null))
                    {
                        ShowTSAVPoint();
                    }
                    if (TSAVBean.Work.IsLastP())
                    {
                        ShowAOI();
                    }
                } 
            }catch (Exception ex)
            {
                LogUtil.error(Name+ "timer_Tick 出错:" + ex.ToString());
            }
        }
        private DateTime LastTime = DateTime.Now;

        private bool isShowAOI = false;
        private void ShowAOI()
        {
            if (isShowAOI)
            {
                return;
            }
            isShowAOI = true;
            try
            {
                //判断是否需要AOI检测 
                if (TSAVBean.IsNeedAOI && TSAVBean.Work.IsShowAOI.Equals(false))
                {
                    TSAVBean.Work.IsShowAOI = true;
                    LogUtil.info("焊接完成,清理投影内容,弹出 提示框提示进入AOI检测");

                    FrmProjectorScreen.instance.ClearPoint();
                    //IOManager.IOMove(IOManager.Camera_Led, IO_VALUE.HIGH);
                    FrmAOICheck frm = new FrmAOICheck(CurrProject);
                    frm.ShowDialog();
                    //   IOManager.IOMove(IOManager.Camera_Led, IO_VALUE.LOW);

                }
                else
                {
                    TSAVBean.Work.StopWork();
                }
            }
            catch (Exception ex)
            {
                LogUtil.error("显示AOI出错:" + ex.ToString());
            }
            isShowAOI = false;
        }

        private void btnStartWorking_Click(object sender, EventArgs e)
        {
            LogUtil.info(Name + "点击:" + btnStartWorking.Text + ",自动启动循环测试模式");
            btnStartWorking.Enabled = false;
            //开始工作
            TSAVBean.StartWork(BoardManager.CurrBoard);
            WorkInfo.IsCycleDebug = true;
            btnStartWorking.Enabled = true; 
        }

        private void FrmWelding_FormClosing(object sender, FormClosingEventArgs e)
        {
            //StatusClient.instance.SendNew(Color.Green, TSAVBean.Name + "退出工作状态");

            FrmProjectorScreen.instance.ClearPoint();
            this.timer.Enabled = false;
            this.timerShowForm.Enabled = false;

            if (TSAVBean.Status > TSAVStatus.Reset)
            {
                TSAVBean.StopWork();
            }
            TSAVBean.ExitWork(); 
        }
        private void FormStatus()
        {
            btnSave.Enabled = true;
            btnGoHome.Enabled = true;
            if (TSAVBean.Status <= (TSAVStatus.Wait))
            {
                btnStart.Visible = true;
                btnGoHome.Visible = false;
            }
            else
            {
                btnStart.Visible = false;
                btnGoHome.Visible = true;
            }

            if (TSAVBean.Status.Equals(TSAVStatus.Runing)&&(!TSAVBean.IsInSuddenDown))
            {

                btnStartWorking.Enabled = !(TSAVBean.Work.IsWorking);
                btnNextPoint.Enabled = true;
                btnPrePoint.Enabled = true;
            }
            else
            {
                btnStartWorking.Enabled = false;
                if (TSAVBean.IsDebug.Equals(false))
                {
                    btnNextPoint.Enabled = false;
                    btnPrePoint.Enabled = false;
                }
            }
           
        }
        private void btnStart_Click(object sender, EventArgs e)
        {
            StartRun();
            FormStatus();
        }

        private void btnGoHome_Click(object sender, EventArgs e)
        {
            //btnGoHome.Enabled = false;

            if (TSAVBean.LineStep.moveType.Equals(1))
            {
                DialogResult result = MessageBox.Show(ResourceCulture.GetString(ResourceCulture.SureReset, "是否确定重置,重置后当前工作将结束"),
                    ResourceCulture.GetString(ResourceCulture.MsgTitle, "提示"), MessageBoxButtons.OKCancel);
                if (!result.Equals(DialogResult.OK))
                {
                    return;
                }
            }
            if (TSAVBean.Status > TSAVStatus.Wait)
            {
                TSAVBean.StopMove();
                TSAVBean.StartReset();
            }
            //btnGoHome.Enabled = true;

            FormStatus();
        }
        private void btnBack_Click(object sender, EventArgs e)
        {
            timerShowForm.Stop();
            timer.Stop();
            this.Close();
        }
        private void FrmWork_Shown(object sender, EventArgs e)
        {
            try
            { 
                SetSkin(this);
                //lblMsg.ForeColor = Color.Red;
                lblNotices.ForeColor = Color.Red;
                label2.ForeColor = Color.Red;
                lblComNotices.ForeColor = Color.Red;
                BoardInfo board = BoardManager.CurrBoard;
                label3.ForeColor = Color.Red;
                this.lblPointName.ForeColor = Color.Red;
                label4.ForeColor = Color.Red;
                lblPositionNum.ForeColor = Color.Red;

                display.SetPic(picBoard, panBoard);
                if (board != null)
                {
                    loadPictureBoxSize(board);
                    StartRun();
                }
                FrmProjectorScreen.instance.ClearPoint();
                timer.Start();
                LogUtil.info(Name + "  Shown end ");
            }
            catch (Exception ex)
            {
            }
        }
        private int iCropErr = 0;
        double defaultXxishu = 0.5;
        double defaultYxishu = 0.5;
        private int lineWidth = 4;
        int lineLength = 80;
        private void Crop(Brush color)
        {
            loadPictureBoxSize(BoardManager.CurrBoard);
            float width = picBoard.Width / 5;
            float height = picBoard.Height / 5;
            float defaultX = (float)(preShowPointX - width / 2);
            float defaultY = (float)(preShowPointY - width / 2);
            float X = (float)(preShowPointX - width / 2);
            float Y = (float)(preShowPointY - height / 2);

            //判断是否xy超过范围
            if (X < 0)
            {
                X = 0;
                //width = Math.Abs((float)preShowPointX * 2);
            }
            if (Y < 0)
            {
                Y = 0;
                //height = Math.Abs((float)preShowPointY * 2);
            }

            if (X + width > picBoard.Width)
            {
                X = picBoard.Width - width;
                //width = (float)(picBoard.Width - preShowPointX) * 2;
                //X = picBoard.Width - width;
            }
            if (Y + height > picBoard.Height)
            {
                Y = picBoard.Height - height;
                //height = (float)(picBoard.Height - preShowPointY) * 2;
                //Y = picBoard.Height - height;
            }
            //updatePicPointSize(Convert.ToInt32(width*100), Convert.ToInt32(height*100));
            updatePicPointSize(picBoard.Width, picBoard.Height);
            //画矩形
            Graphics picBoardGra = this.picBoard.CreateGraphics();
            Pen pen = new Pen(Color.Red, 3);
            picBoardGra.DrawRectangle(pen, X, Y, picBoard.Width / 5, picBoard.Height / 5);
            picBoardGra.Flush();
            picBoardGra.Dispose();
            defaultXxishu = (defaultX - (X - width / 2)) / width;
            defaultYxishu = (defaultY - (Y - width / 2)) / height;
            if (bitmap == null)
            {
                bitmap = (Bitmap)picBoard.Image;
            }
            X = bitmap.Width * X / picBoard.Width;
            Y = bitmap.Height * Y / picBoard.Height;
            width = bitmap.Width * width / picBoard.Width;
            height = bitmap.Height * height / picBoard.Height;
            Rectangle rec = new Rectangle((int)Math.Round(X), (int)Math.Round(Y), (int)Math.Round(width), (int)Math.Round(height));

            try
            { 
                //GC.WaitForPendingFinalizers();  
                this.picPoint.Image = bitmap.Clone(rec, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                //bitmap.Dispose();  
            }
            catch (Exception ex)
            {
                iCropErr++;
            }
            finally
            {
            }
            //int pontX = picPoint.Width / 2;
            //int pontY = picPoint.Height / 2;
            int pontX = Convert.ToInt32(picPoint.Width * defaultXxishu);
            int pontY = Convert.ToInt32(picPoint.Height * defaultYxishu);
            Graphics g = picPoint.CreateGraphics();
            //g.FillEllipse(color, pontX, pontY, pointWidh, pointWidh);

            lineLength = picPoint.Width < picPoint.Height ? picPoint.Width / 8 : picPoint.Height / 8;
            lineWidth = lineLength / 5;

            g.DrawLine(new Pen(Color.Red, lineWidth), pontX - lineWidth / 2, pontY - lineLength, pontX - lineWidth / 2, pontY + lineLength - lineWidth / 2);
            g.DrawLine(new Pen(Color.Red, lineWidth), pontX - lineLength, pontY - lineWidth / 2, pontX + lineLength - lineWidth / 2, pontY - lineWidth / 2);

            g.Flush();
            g.Dispose();
        }
        private void ShowMsg()
        {
            this.lblPointName.Text = " 暂无";
            this.lblComDes.Text = "暂无";
            this.lblComName.Text = "暂无";
            this.lblComNotices.Text = "暂无";
            if (smtPoint == null)
            {
                return;
            }
            if (!smtPoint.pointName.Equals(""))
            {
                lblPositionNum.Text = smtPoint.PositionNum;
                lblPartNum.Text = smtPoint.PartNum;
                lblPointName.Text = smtPoint.pointName;
                ComponetInfo com = CSVReaderBomManager.GetCom(BoardManager.CurrBoard.bomName, smtPoint.PartNum);
                TSAVPosition position = null;
                if (com != null)
                {
                    position = CSVPositionReader<TSAVPosition>.GetPositonByNum(com.PositionNum);
                }
                else
                {
                    position = CSVPositionReader<TSAVPosition>.GetPositonByNum(smtPoint.PositionNum);
                }
                if (position != null)
                {
                    lblPositionNum.Text = position.PositionNum;
                }
                if (com != null)
                {
                    lblComDes.Text = com.ComponentDes;
                    this.lblComName.Text = com.ComponentName;
                    lblCount.Text = com.ComCount.ToString();
                    txtCount.Text = com.ComCount.ToString();
                    lblComNotices.Text = com.Notes.ToString();
                    if (com.ComCount <= 0)
                    {
                        lblComNotices.Text = ResourceCulture.GetString("元器件数量不足,请及时补充", "元器件数量不足,请及时补充");
                    }
                }
                else
                {
                    string msg = ResourceCulture.GetString(ResourceCulture.DataLostMsg, "数据丢失");
                    lblComDes.Text = msg;
                    this.lblComName.Text = msg;
                    lblCount.Text = msg;
                    txtCount.Text = 0 + "";
                    lblComNotices.Text = msg;
                }
            }
            else
            {
                this.lblPositionNum.Text = ResourceCulture.GetString(ResourceCulture.NoData, "暂无");
            }
        }
        private void timerShowForm_Tick(object sender, EventArgs e)
        {
            if (smtPoint != null && smtPoint.PositionX > 0 && smtPoint.PositionY > 0)
            {
                //ShowMsg(); 
                Color color = Color.Yellow;
                if (preIsShow)
                {
                    color = Color.Yellow;
                    preIsShow = false;
                }
                else
                {
                    color = Color.Red;
                    preIsShow = true;
                }
                int pontX = Convert.ToInt32(picPoint.Width * defaultXxishu);
                int pontY = Convert.ToInt32(picPoint.Height * defaultYxishu);
                Graphics g = picPoint.CreateGraphics();
                g.DrawLine(new Pen(Color.FromArgb(255, color), lineWidth), pontX - lineWidth / 2, pontY - lineLength, pontX - lineWidth / 2, pontY + lineLength - lineWidth / 2);
                g.DrawLine(new Pen(Color.FromArgb(255, color), lineWidth), pontX - lineLength, pontY - lineWidth / 2, pontX + lineLength - lineWidth / 2, pontY - lineWidth / 2);

                g.Flush();
                g.Dispose();
                preIsShow = false;

            }
        }
        private int preIndex = -1;
        private SMTPointInfo smtPoint = new SMTPointInfo();
        private double preShowPointX = 0;
        private double preShowPointY = 0;
        private bool preIsShow = false;
        private void btnNextPoint_Click(object sender, EventArgs e)
        {
            //if (!TSAVBean.Work.IsWorking)
            //{
            //    return;
            //}
            if (TSAVBean.Status.Equals(TSAVStatus.Runing))
            {
                Next(true);
            }
            else
            {
                PreOrNext(true);
            }
        }

        private void Next(bool isNext)
        {
            TSAVBean.NextPoint(isNext, false);
            ShowTSAVPoint();
        }

        private void ShowTSAVPoint()
        {
            if (!TSAVBean.Work.IsWorking)
            {
                return;
            }
            preIndex = TSAVBean.Work.currIndex;
            smtPoint = TSAVBean.Work.currPoint;

            ShowMsg();
            if (pointXMap.ContainsKey(smtPoint.pointNum))
            {
                preShowPointX = pointXMap[smtPoint.pointNum];
                preShowPointY = pointYMap[smtPoint.pointNum];
            }
            else
            { //画图
                int width = Convert.ToInt32(board.boardWidth);
                int height = Convert.ToInt32(board.boardLength);
                Graphics grfx = picBoard.CreateGraphics();
                preShowPointX = Math.Abs(smtPoint.PositionX) * picBoard.Width / height;
                preShowPointY = Math.Abs(smtPoint.PositionY) * picBoard.Width / height;

            }

            int leftCount = workSmtList.Count - 1 - preIndex;
            if (preIndex.Equals(0))
            {
                lblLast.Visible = false;
                lblStart.Visible = true;
            }
            else if (leftCount <= 0)
            {
                lblStart.Visible = false;
                lblLast.Visible = true;
            }
            else
            {
                lblStart.Visible = false;
                lblLast.Visible = false;
            }
            if (leftCount > 0)
            {
                lblBoardPoint.Text = ResourceCulture.GetString(ResourceCulture.PointInfoMsg, "当前:第{0}步,剩余{1}步", (preIndex + 1).ToString(), leftCount);
                string btnText = ResourceCulture.GetString(this.ClassName + "_btnNextPoint_Text", "下一步");
                if (!btnNextPoint.Text.Equals(btnText))
                {
                    btnNextPoint.Text = btnText;
                }
            }
            else
            {
                lblBoardPoint.Text = ResourceCulture.GetString(ResourceCulture.PointInfoMsg2, "当前:第{0}步", (preIndex + 1).ToString());
                btnNextPoint.Text = ResourceCulture.GetString(ResourceCulture.NexProText, "下一个程序");
            }
            Crop(Brushes.Red);
            if (timerShowForm.Enabled.Equals(false))
            {
                timerShowForm.Start();
            }
        }

        private void btnStop_Click(object sender, EventArgs e)
        {           
            //Task.Factory.StartNew(delegate
            //{
            //    if (TSAVBean.Status > TSAVStatus.Reset)
            //    {
            //        TSAVBean.StopWork();
            //    }
            //    //OpInfo op = OpInfo.GetOpInfo(BoardManager.CurrBoard);
            //    //bool result = DB.db.AddHistory(op, out int id);
            //});
            this.Close();
        }

        private void btnPrePoint_Click(object sender, EventArgs e)
        {
            if (TSAVBean.Status.Equals(TSAVStatus.Runing))
            {
                Next(false);
            }
            else
            {
                PreOrNext(false);
            }
        }

        private void PreOrNext(bool isNext)
        {
            if (board != null && workSmtList.Count > 0)
            {
                if (isNext)
                {
                    preIndex++;

                    if (preIndex < 0)
                    {
                        preIndex = 0;
                    }
                }
                else
                {
                    preIndex--;

                    if (preIndex < 0)
                    {
                        preIndex = workSmtList.Count - 1;
                    }
                }

                if (workSmtList.Count <= preIndex)
                {
                    ShowAOI();
                    preIndex = 0;
                }
                else if (preIndex < 0)
                {
                    preIndex = workSmtList.Count - 1;
                }
                smtPoint = workSmtList[preIndex];
                ShowMsg();
                if (pointXMap.ContainsKey(smtPoint.pointNum))
                {
                    preShowPointX = pointXMap[smtPoint.pointNum];
                    preShowPointY = pointYMap[smtPoint.pointNum];
                }
                else
                { //画图
                    int width = Convert.ToInt32(board.boardWidth);
                    int height = Convert.ToInt32(board.boardLength);
                    Graphics grfx = picBoard.CreateGraphics();
                    preShowPointX = Math.Abs(smtPoint.PositionX) * picBoard.Width / height;
                    preShowPointY = Math.Abs(smtPoint.PositionY) * picBoard.Width / height;
                }

                FrmProjectorScreen.instance.ShowPoint(true, smtPoint);
                ComponetInfo com = CSVReaderBomManager.GetCom(this.board.bomName, smtPoint.PartNum);
                TSAVPosition position = null;
                if (com != null)
                {
                    position = CSVPositionReader<TSAVPosition>.GetPositonByNum(com.PositionNum);
                }
                else
                {
                    position = CSVPositionReader<TSAVPosition>.GetPositonByNum(smtPoint.PositionNum);
                }

                if (position == null)
                {
                    //ComponetInfo com = ComponentManager.getById(smtPoint.pointType);
                    lblPositionNum.Text = ResourceCulture.GetString(ResourceCulture.ComInsufficient, "元器件库存不足");
                }
                else
                {
                    lblPositionNum.Text = position.PositionNum;
                }
                int leftCount = workSmtList.Count - 1 - preIndex;
                if (preIndex.Equals(0))
                {
                    lblLast.Visible = false;
                    lblStart.Visible = true;
                }
                else if (leftCount <= 0)
                {
                    lblStart.Visible = false;
                    lblLast.Visible = true;
                }
                else
                {
                    lblStart.Visible = false;
                    lblLast.Visible = false;
                }

                if (leftCount > 0)
                {
                    lblBoardPoint.Text = ResourceCulture.GetString(ResourceCulture.PointInfoMsg, "当前:第{0}步,剩余{1}步", (preIndex + 1).ToString(), leftCount);
                }
                else
                {
                    lblBoardPoint.Text = ResourceCulture.GetString(ResourceCulture.PointInfoMsg2, "当前:第{0}步", (preIndex + 1).ToString());
                }
                Crop(Brushes.Red);
                if (timerShowForm.Enabled.Equals(false))
                {
                    timerShowForm.Start();
                }

            }
        }
        private void btnStopMove_Click(object sender, EventArgs e)
        {
            if (TSAVBean.IsNeedAOI)
            {
                LogUtil.info("点击【" + btnCamera.Text + "】,清理投影内容,进入AOI检测");
                FrmProjectorScreen.instance.ClearPoint();
                //    IOManager.IOMove(IOManager.Camera_Led, IO_VALUE.HIGH);
                FrmAOICheck frm = new FrmAOICheck(CurrProject);
                frm.ShowDialog();
                //    IOManager.IOMove(IOManager.Camera_Led, IO_VALUE.LOW); 
            }
        }


        private void btnUpateCount_Click(object sender, EventArgs e)
        {
            ShowUpdateCount(true);
        }
        private void ShowUpdateCount(bool isShow)
        {
            txtCount.Visible = isShow;
            btnSave.Visible = isShow;
            btnCancel.Visible = isShow;
            btnUpateCount.Visible = !isShow;
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (TSAVBean.Work == null || TSAVBean.Work.currBoard == null)
            {
                MessageBox.Show(ResourceCulture.GetString(ResourceCulture.DeviceNotOkMsg, "设备未连接!"));
                return;
            }
            string comName = lblComName.Text;
            int count = FormUtil.GetIntValue(txtCount);
            string bomName = TSAVBean.Work.currBoard.bomName;
            if (CSVReaderBomManager.UpdateCount(bomName, comName, count))
            {
                MessageBox.Show(ResourceCulture.GetString(ResourceCulture.UpdateCNumOk, "元器件【{0}】的数量已更新为【{1}】", comName, count));
            }
            txtCount.Visible = false;

            lblCount.Text = count.ToString();
            ShowUpdateCount(false);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            ShowUpdateCount(false);
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            TSAVBean.StartTest();
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (!isInitOk)
            {
                return;
            }
            if (TSAVBean.WorkMode.Equals(0))
            {
                if (radioButton2.Checked)
                {
                    AutoVisiable(true);
                    TSAVBean.WorkMode = 1;
                    ConfigAppSettings.SaveValue(Setting_Init.WorkMode, 1);
                    LogUtil.info("切换工作模式为:自动工作");
                }
                else
                {
                    AutoVisiable(false);
                }

            }
            else if (TSAVBean.WorkMode.Equals(1))
            {
                if (radioButton1.Checked)
                {
                    AutoVisiable(false);
                    TSAVBean.WorkMode = 0;
                    ConfigAppSettings.SaveValue(Setting_Init.WorkMode, 0);
                    LogUtil.info("切换工作模式为:脚踏工作");
                }
                else
                {
                    AutoVisiable(true);
                }
            }
        }

        private void btnUpdateAutoTime_Click(object sender, EventArgs e)
        {
            int value = FormUtil.GetIntValue(txtAuToTime);
            if (value <= 0)
            {
                MessageBox.Show(ResourceCulture.GetString(ResourceCulture.TimeNotVailid, "输入的间隔时间无效 "),
                    ResourceCulture.GetString(ResourceCulture.MsgTitle, "提示"));
                txtAuToTime.Text = TSAVBean.AuToModeSeconds.ToString();
                return;
            }
            if (TSAVBean.AuToModeSeconds.Equals(value))
            {
                MessageBox.Show(ResourceCulture.GetString(ResourceCulture.NotNeedUpdate, "时间间隔未改变,不需要更新 "), ResourceCulture.GetString(ResourceCulture.MsgTitle, "提示"));
                return;
            }

            TSAVBean.AuToModeSeconds = value;
            ConfigAppSettings.SaveValue(Setting_Init.AuToModeSeconds, value);
            MessageBox.Show(ResourceCulture.GetString(ResourceCulture.UpdateTOK, "已更新自动工作时间间隔为{0}秒 ", value)
                , ResourceCulture.GetString(ResourceCulture.MsgTitle, "提示"));
        }

        private void btnWorkInfo_Click(object sender, EventArgs e)
        {
            FrmWorkCount frm = new FrmWorkCount();
            frm.ShowDialog();
        }
        private static bool IsSet = false;
        private void TSAVBean_InputCodeEvent(int count)
        {
            MesUtil.ClearCode();
            FrmCodeInPut frmCode = new FrmCodeInPut();
            frmCode.SetCount(count);
            frmCode.ShowDialog();
        }

        private void btnCodeTest_Click(object sender, EventArgs e)
        {
            TSAVBean_InputCodeEvent(0);
        }
    }
}