FrmWork.cs 31.5 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
using System;
using System.Drawing;
using System.Windows.Forms;

using URSoldering.DeviceLibrary;
using URSoldering.Common; 
using System.Timers; 
using System.IO;
using URSoldering.LoadCSVLibrary;
using UserFromControl;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using HalconDotNet;
using System.Threading;
using System.Threading.Tasks;
using System.Linq;

namespace URSoldering.Client
{
    public partial class FrmWork : FrmBase
    {
        //private static string VideoCameraName = ConfigAppSettings.GetValue(Setting_Init.VideoCameraName); 
        private URSolderingRobot Robot = null;
        public FrmWork()
        {
            InitializeComponent();
        }
        Dictionary<string, IOTextControl> DIControlList = new Dictionary<string, IOTextControl>();
        Dictionary<string, IOTextControl> DOControlList = new Dictionary<string, IOTextControl>();
        private bool isInitOk = false;
      
        private void FrmWelding_Load(object sender, EventArgs e)
        {
            
            lblCodeResult.Visible = false;
            lblAOIResult.Visible = false;
            LoadListPoint();
            
            Robot = RobotManager.SolderingRobot;
            WeldRobotBean.AOICheckFun += AOICheck; 
            //Robot.GetCodeFun += GetCode;
            LogUtil.logBox = this.logBox;
            LogUtil.showCount = 1;
            this.WindowState = FormWindowState.Maximized;

            this.chbHigh.Checked = WeldRobotBean.isHighSpeed;
             
            Robot = RobotManager.SolderingRobot;
            //StartRun();
            isInitOk = true;

            if (BoardManager.CurrBoard == null)
            {
                MessageBox.Show("请先配置程序!");
                this.Close();
            }
            this.Text = "程序【" + BoardManager.CurrBoard.boardName + "】工作";
            LoadPoint(BoardManager.CurrBoard, -1);
             
            this.btnChange.Focus();
            btnStart.Text = "启动";
            btnStart.Enabled = true;
            btnWeld.Enabled = false;
            btnStop.Enabled = false;
            Robot.IsAutoRun = chbXunHuan.Checked;
            LoadAOI();
        }
        
        private int ShowPointIndex = 5;
        private void LoadCountPoint(bool isToday)
        {
            List<WorkLogInfo> allList =new List<WorkLogInfo>( WorkCountManager.CountObj.WorkLogList);
            chartLine.Series[0].Points.Clear();
            chartSolder.Series[0].Points.Clear();
            
            for (int i = 0; i < ShowPointIndex; i++)
            {

                DateTime time = DateTime.Now.AddDays(i - ShowPointIndex + 1);
                string xValue = time.ToString("MM-dd");
                int weldC = 0;
                int solderC = 0;
                if (isToday)
                {
                    List<WorkLogInfo> list = (from m in allList where m.getTimeStr().Equals(xValue) select m).ToList<WorkLogInfo>();
                    if (list.Count > 0)
                    {
                        weldC = list[0].WeldCount;
                        solderC = list[0].WeldPointCount;
                    }
                    if (i.Equals(ShowPointIndex-1))
                    {
                        xValue = "今天";
                    }
                    else if (i.Equals(ShowPointIndex-2))
                    {
                        xValue = "昨天";
                    }
                }
                else
                {
                    DateTime startTime = FormUtil.GetMondayDate(DateTime.Now).AddDays((i - ShowPointIndex + 1) * 7);
                    DateTime endTime = FormUtil.GetSundayDate(DateTime.Now).AddDays((i - ShowPointIndex +1) * 7);
                    List<WorkLogInfo> list = (from m in allList where m.DayTime>=startTime&&m.DayTime<=endTime select m).ToList<WorkLogInfo>();
                    foreach(WorkLogInfo obj in list)
                    {
                        weldC+=obj.WeldCount;
                        solderC += obj.WeldPointCount;
                    } 
                    xValue = endTime.ToString("MM-dd");
                    if (i.Equals(4))
                    {
                        xValue = "本周";
                    }
                    else if (i.Equals(3))
                    {
                        xValue = "上周";
                    }

                } 
                chartLine.Series[0].Points.AddXY(xValue, weldC);
                chartSolder.Series[0].Points.AddXY(xValue, solderC);
                chartLine.Series[0].Points[i].IsValueShownAsLabel = true;
                chartSolder.Series[0].Points[i].IsValueShownAsLabel = true;
            }
            UpdatePersent(isToday);
        }
       
        private void UpdatePersent(bool isToday)
        {
            int ok = (int)Math.Round(WorkCountManager.CountObj.TodayPercent());
            int notOk = 100 - ok;
            List<WorkLogInfo> allList = new List<WorkLogInfo>(WorkCountManager.CountObj.WorkLogList);
            chartPersent.Series[0].Points[0].SetValueY(ok);
            chartPersent.Series[0].Points[1].SetValueY(notOk);
            chartPersent.Series[0].Points[0].IsValueShownAsLabel = true;
            chartPersent.Series[0].Points[1].IsValueShownAsLabel = true;
            chartPersent.Series[0].Points[0].Color = Color.DarkGreen;
            chartPersent.Series[0].Points[1].Color = Color.Red;
            int weldCount = 0;
            int solderCount = 0;
            if (!isToday)
            {
                DateTime startTime = FormUtil.GetMondayDate(DateTime.Now);
                DateTime endTime = FormUtil.GetSundayDate(DateTime.Now);
                List<WorkLogInfo> list = (from m in allList where m.DayTime >= startTime && m.DayTime <= endTime select m).ToList<WorkLogInfo>();
                foreach (WorkLogInfo obj in list)
                {
                    weldCount += obj.WeldCount;
                    solderCount += obj.WeldPointCount;
                }
            }
            else
            {
                List<WorkLogInfo> list = (from m in allList where m.getTimeStr().Equals(DateTime.Now.ToString("MM-dd")) select m).ToList<WorkLogInfo>();
                if (list.Count > 0)
                {
                    weldCount = list[0].WeldCount;
                    solderCount = list[0].WeldPointCount;
                }
            }

            chartLine.Series[0].Points[ShowPointIndex - 1].SetValueY(weldCount);
            chartSolder.Series[0].Points[ShowPointIndex - 1].SetValueY(solderCount);
        }

    
        private string aoiCheckFile = "";
        private string currAOIFile = "";
        private void LoadAOI()
        {
            //位置配置到文件中
            string appPath = Application.StartupPath;
            string strFilePathName = ConfigAppSettings.GetValue(Setting_Init.AOIFileConfig);  
            aoiCheckFile = appPath + strFilePathName; 
            LoadAOIFile(aoiCheckFile);
        }

        private bool LoadAOIFile(string filePath)
        {
            if (currAOIFile.Equals(filePath))
            {
                return true ;
            }
            if (File.Exists(filePath))
            {
                this.axCKVisionCtrl1.UnloadConfigure();
                Thread.Sleep(100);
                try
                {
                    currAOIFile = filePath;
                    this.axCKVisionCtrl1.LoadConfigure(filePath);
                    this.axCKVisionCtrl1.ZoomView(2);
                    LogUtil.info("成功加载程序:" + filePath);
                    return true;
                }
                catch (Exception ex)
                {
                    LogUtil.error("加载程序【" + filePath + "】出错:" + ex.ToString());
                }
            }
            else
            {
                LogUtil.error("未找到AOI程序文件:" + filePath);
            }return false;
        }

        private void LoadListPoint()
        {
            this.listPoint.Columns.Clear();
            ColumnHeader chName = new ColumnHeader();
            chName.Text = "焊点名称";   //设置列标题
            chName.Width = 250;    //设置列宽度
            chName.TextAlign = HorizontalAlignment.Left;   //设置列的对齐方式
            this.listPoint.Columns.Add(chName);    //将列头添加到ListView控件。
            
            ColumnHeader chStatus = new ColumnHeader();
            chStatus.Text = "焊接温度";   //设置列标题
            chStatus.Width = 200;    //设置列宽度
            chStatus.TextAlign = HorizontalAlignment.Left;   //设置列的对齐方式
            this.listPoint.Columns.Add(chStatus);    //将列头添加到ListView控件。

            ColumnHeader presendWire = new ColumnHeader();
            presendWire.Text = "预送丝量";   //设置列标题
            presendWire.Width = 200;    //设置列宽度
            presendWire.TextAlign = HorizontalAlignment.Left;   //设置列的对齐方式
            this.listPoint.Columns.Add(presendWire);    //将列头添加到ListView控件。

            ColumnHeader sendWire = new ColumnHeader();
            sendWire.Text = "送丝量";   //设置列标题
            sendWire.Width = 200;    //设置列宽度
            sendWire.TextAlign = HorizontalAlignment.Left;   //设置列的对齐方式
            this.listPoint.Columns.Add(sendWire);    //将列头添加到ListView控件。

            ColumnHeader status = new ColumnHeader();
            status.Text = "状态";   //设置列标题
            status.Width = 180;    //设置列宽度
            status.TextAlign = HorizontalAlignment.Left;   //设置列的对齐方式
            this.listPoint.Columns.Add(status);    //将列头添加到ListView控件。
        }
        private int preIndex = -2;
        private void LoadPoint(BoardInfo board, int currWeldIndex)
        {
            try
            {
                if (board == null)
                {
                    return;
                }
                int id = 0;
                try
                {
                    id = Convert.ToInt32(listPoint.Tag);
                }
                catch (Exception ex)
                {
                }
                if (id != board.boardId || currWeldIndex == -3 || currWeldIndex .Equals( -1)||(board.pointList.Count!= listPoint.Items.Count))
                {
                    this.lblPointCount.Text = board.pointList.Count.ToString();
                    this.lblProName.Text = board.boardName;
                    this.lblCode.Text = board.WareCode;
                    this.lblAgvTime.Text = FormUtil.GetSpanStr(board.WeldTime);
                    this.lblWeldTime.Text = FormUtil.GetSpanStr(board.WeldTime);
                    this.Text = "工作状态【程序:"+board.boardName+"】";
                    listPoint.Tag = board.boardId;
                    listPoint.Items.Clear();
                    int i = 0;
                    foreach (WeldPointInfo point in board.pointList)
                    {
                        ListViewItem lvi = new ListViewItem();
                        lvi.ImageIndex = i;

                        lvi.Text = point.pointName;
                        lvi.SubItems.Add(point.weldTemperature.ToString() + "°C");
                        lvi.SubItems.Add((point.startSendWireSpeed * point.startSendWireTime).ToString() + "mm");
                        lvi.SubItems.Add((point.sendWireTime * point.sendWireSpeed).ToString() + "mm");
                        lvi.SubItems.Add("--");
                        this.listPoint.Items.Add(lvi);
                        i++;
                    }
                }
                if (preIndex.Equals(currWeldIndex))
                {
                    return;
                }
                if (currWeldIndex >= -1)
                {
                    int statusIndex = 4;
                    preIndex = currWeldIndex;
                    for (int i = 0; i < board.pointList.Count; i++)
                    {
                        if (i < currWeldIndex)
                        {
                            listPoint.Items[i].BackColor = Color.LightGreen;
                            this.listPoint.Items[i].SubItems[statusIndex].Text = "完成";
                        }
                        else if (i.Equals(currWeldIndex))
                        {

                            if (WeldRobotBean.alarmType.Equals(AlarmType.None))
                            {
                                listPoint.Items[i].BackColor = Color.Yellow;
                                this.listPoint.Items[i].SubItems[statusIndex].Text = "焊接中";
                            }
                            else
                            {
                                listPoint.Items[i].BackColor = Color.Red;
                                this.listPoint.Items[i].SubItems[statusIndex].Text = "焊接中";
                            }
                        }
                        else
                        {
                            listPoint.Items[i].BackColor = Color.White;
                            this.listPoint.Items[i].SubItems[statusIndex].Text = "--";
                        }
                    }
                }
            }catch(Exception ex)
            {
                LogUtil.error(ex.ToString());
            }
        }
          
        private void btnReadIO_Click(object sender, EventArgs e)
        {
            ReadIOList();
        }

        private void ReadIOList()
        {
            foreach (string key in DIControlList.Keys)
            {
                IOTextControl control = DIControlList[key];
                int iov = (int)KNDManager.GetIOValue(RobotManager.robotConfig.RobotDIList[key]);
                if (iov != control.IOValue)
                {
                    control.IOValue = iov;
                    control.ShowData();
                }
            }
            foreach (string key in this.DOControlList.Keys)
            {
                IOTextControl control = DOControlList[key];
                int iov = (int)KNDManager.GetIOValue(RobotManager.robotConfig.RobotDOList[key]);
                if (iov != control.IOValue)
                {
                    control.IOValue = iov;
                    control.ShowData();
                }
            }
        }
        private void StartRun()
        {
            if (Robot.Status <= RobotStatus.Wait)
            {
                LogUtil.info("开始启动机器人");
                bool result = Robot.StartRun();
                if (!result)
                {
                    lblMsg.Text = "启动失败:" + Robot.WarnMsg;
                    LogUtil.error("启动失败:" + Robot.WarnMsg); 
                }
                else
                {
                    btnStart.Text = "复位";
                    btnWeld.Enabled = true;
                    btnStop.Enabled = true; 
                } 
                timer.Start();  
            }else
            {
                Robot.RobotReset();
                btnStart.Text = "复位";
            }
        }
          
        private void timer_Tick(object sender, EventArgs e)
        {
            ActualTime(null, null);
            ReadIOList();
            //判断是否是进入
            if (DateTime.Now.ToString("MM-dd").Equals(chartLine.Series[0].Points[ShowPointIndex - 1].XValue))
            {
                UpdatePersent(btnChange.Text.Equals("每周统计"));
            }else
            {
                LoadCountPoint(btnChange.Text.Equals("每周统计"));
            }
        }
        private void UpdateCheckResult(Label lblshowMsg,  int result)
        {
            if (result.Equals(0))
            {
                lblshowMsg.Text = "";
            }
            else if (result.Equals(1))
            {
                if (!lblshowMsg.Text.Equals("OK"))
                {
                    lblshowMsg.Text = "OK";
                    lblshowMsg.ForeColor = Color.Green;
                }
            }
            else if (result.Equals(2))
            {
                if (!lblshowMsg.Text.Equals("NG"))
                {
                    lblshowMsg.Text = "NG";
                    lblshowMsg.ForeColor = Color.Red;
                }
            }
        }
       
        private void ActualTime(object sender, ElapsedEventArgs e)
        { 
            if (Robot.Status.Equals(RobotStatus.Wait))
            {
                lblRobot.Text = "未启动";
            }
            else if (Robot.Status.Equals(RobotStatus.Reset))
            {
                lblRobot.Text = "复位中";
            }
            else if (Robot.Status.Equals(RobotStatus.Runing))
            {
                lblRobot.Text = "运行中";
            }
            else if (Robot.Status.Equals(RobotStatus.Busy))
            {
                lblRobot.Text = "忙碌中";
            }
            else if (Robot.Status.Equals(RobotStatus.LineMove))
            {
                lblRobot.Text = "流水线转动中";
            }
            lblRobotWarnMsg.Text = Robot.WarnMsg;
            this.lblAgvTime.Text =FormUtil.GetSpanStr( BoardManager.CurrBoard.WeldTime);
            //this.lblAgvTime.Text = "--:--:--";
            try
            {
                if (WeldRobotBean.ISRun == false)
                {
                    if (lblMsg.Text.Equals(""))
                    {
                        lblMsg.Text = "未启动。。。";
                    } 
                    return;
                }

                if (WeldRobotBean.WarnMsg.Equals("").Equals(false))
                {
                    lblMsg.Text = WeldRobotBean.WarnMsg;
                }
               else  if (WeldRobotBean.WeldMoveStep.moveType.Equals(MoveType.GoHome))
                {
                    btnWeld.Enabled = false;
                    lblMsg.ForeColor = Color.Green;
                    lblMsg.Text = "复位中";
                    this.listPoint.Items.Clear();
                    //LoadPoint(WeldRobotBean.WeldMoveStep.currBoard, -1);
                }
                else if (WeldRobotBean.WeldMoveStep.moveType.Equals(MoveType.Weld))
                {
                    btnWeld.Enabled = false;
                    lblMsg.ForeColor = Color.Green;
                    lblMsg.Text = "程序【" + WeldRobotBean.WeldMoveStep.currBoard.boardName + "】焊接中";
                    LoadPoint(WeldRobotBean.WeldMoveStep.currBoard, WeldRobotBean.WeldMoveStep.CurrPointIndex);
                    TimeSpan sapn = DateTime.Now - Robot.LastStartWeldTime;
                    this.lblWeldTime.Text = FormUtil.GetSpanStr(sapn);
                }else if (WeldRobotBean.WeldMoveStep.moveType.Equals(MoveType.None))
                {
                    btnWeld.Enabled = true ;
                    LoadPoint(WeldRobotBean.WeldMoveStep.currBoard, -1);
                    lblMsg.ForeColor = Color.Green;
                    lblMsg.Text = "当前未焊接";
                }else
                {
                    btnWeld.Enabled = false;
                    //LoadPoint(WeldRobotBean.WeldMoveStep.currBoard, -1);
                    this.listPoint.Items.Clear();
                    lblMsg.ForeColor = Color.Green;
                    lblMsg.Text = "烙铁头清洗中";
                }
            }
            catch (Exception ex)
            {
                LogUtil.error("状态读取错误:" + ex.ToString());
            } 
        }

        private void FrmWelding_FormClosing(object sender, FormClosingEventArgs e)
        {
            //如果正在运行中,且还有没有焊接完成的
            if ( Robot.NeedWork())
            {
                //提示是否需要 退出
                DialogResult dialogResult = MessageBox.Show("还有未完成的工作,是否停止设备运行?","提示",MessageBoxButtons.YesNo);
                if (dialogResult.Equals(DialogResult.No))
                {
                    return;
                }
            } 
            LogUtil.logBox = null;
            this.timer.Enabled = false; 
            if (Robot.Status > RobotStatus.Wait)
            {
                Robot.StopRun();
            }
            axCKVisionCtrl1.UnloadConfigure();
        }

        private void btnStart_Click(object sender, EventArgs e)
        { 
            lblAOIResult.Visible = false;
            lblCodeResult.Visible = false;
            StartRun();
        } 

        private void btnStop_Click(object sender, EventArgs e)
        {
            lblAOIResult.Visible = false;
            lblCodeResult.Visible = false;
            if (Robot.Status > RobotStatus.Wait)
            {
                //如果正在运行中,且还有没有焊接完成的
                if (Robot.NeedWork())
                {
                    //提示是否需要 退出
                    DialogResult dialogResult = MessageBox.Show("还有未完成的工作,是否停止设备运行?", "提示", MessageBoxButtons.YesNo);
                    if (dialogResult.Equals(DialogResult.No))
                    {
                        return;
                    }
                } 
                Robot.StopRun(); 
                btnStart.Text = "启动";
                
            }
        } 
        private void chbHigh_CheckedChanged(object sender, EventArgs e)
        {
            if (!isInitOk)
            { return; }

            if (chbHigh.Checked)
            {
                DialogResult result = MessageBox.Show("确定切换到高速运行?", "提示", MessageBoxButtons.YesNo);
                if (result.Equals(DialogResult.Yes))
                {
                    WeldRobotBean.isHighSpeed = true;
                    ConfigAppSettings.SaveValue(Setting_Init.Robot_IsHigh, 1);
                }
                else
                {
                    chbHigh.Checked = false;
                }
            }
            else
            {
                WeldRobotBean.isHighSpeed = false;
                ConfigAppSettings.SaveValue(Setting_Init.Robot_IsHigh, 0);
            }
        }

        private void btnGoHome_Click(object sender, EventArgs e)
        {
            btnGoHome.Enabled = false;
            if (WeldRobotBean.ISRun)
            { 
                string msg = WeldRobotBean.GoHome();
                if (!msg.Equals(""))
                {
                    MessageBox.Show("操作失败:" + msg);
                }
            }
            btnGoHome.Enabled = true; 
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            btnStop_Click(null, null);
            timer.Stop();
         
            this.Close();
        }

        private void btnSendWireRecover_Click(object sender, EventArgs e)
        {
            //bool isNext = true;
            //if (WeldRobotBean.IsNeedMsg())
            //{
            //    DialogResult result = MessageBox.Show("消除卡丝报警后是否继续焊接?\r\n选择是:继续焊接\r\n选择否:当前焊点重新焊接", "提示",
            //        MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
            //    if (result.Equals(DialogResult.Yes))
            //    {
            //        isNext = true;
            //    }
            //    else
            //    {
            //        isNext = false;
            //    }
            //}

            //显示提示框,是跳过当前焊点还是从新开始送丝 
            WeldRobotBean.SendWireRecover( );
        }
       
        private void btnClear_Click(object sender, EventArgs e)
        {
            //this.logBox.Clear();
            LogUtil.ClearLog();
        } 
        private void btnEndSoldering_Click(object sender, EventArgs e)
        {
            if (WeldRobotBean.WeldMoveStep.moveType.Equals(MoveType.Weld))
            {
                WeldRobotBean.StopWeld();
            }
        } 
      

        private void GetCode()
        {
            //try
            //{
            //    this.label1.Text = "视觉/扫码";

            //    lblCodeResult.Text = "焊点自动识别中。。。";
            //    lblCodeResult.ForeColor = Color.Blue;
            //    lblCodeResult.Visible = true;

            //    LoadAOIFile(aoiCheckFile);
            //    //lblAOIResult.Visible = false;

            //    for (int i = 0; i < 5; i++)
            //    {
            //        bool result = RunAOI(2);
            //        //if (result)
            //        //{
            //            string code = getResult("Code",101);
            //        if (!code.Equals(""))
            //        {
            //            Robot.WareCode = code;
            //            lblCodeResult.ForeColor = Color.Blue;
            //            lblCodeResult.Text = "识别成功,共找到【" + BoardManager.CurrBoard.pointList.Count + "】个焊点";
            //            LogUtil.info("扫码成功:" + code);
            //            lblCodeResult.Visible = true;

            //            UpdateBoard(code);
            //            break;
            //        }
            //        else
            //        {
            //            lblCodeResult.ForeColor = Color.Red;
            //            lblCodeResult.Text = "焊点识别失败,即将重新识别";
            //            LogUtil.info("扫码失败,即将重新扫码");
            //            lblCodeResult.Visible = true;
            //        }
            //        //}
            //        Thread.Sleep(500);
            //    }
            //    lblCodeResult.ForeColor = Color.Blue;
            //    lblCodeResult.Text = "识别成功,共找到【" + BoardManager.CurrBoard.pointList.Count + "】个焊点";
            //    lblCodeResult.Visible = true;
            //    if (lblCodeResult.Visible.Equals(false))
            //    {
            //        lblCodeResult.ForeColor = Color.Blue;
            //        //lblCodeResult.Text = "扫码成功:1000001" ; 
            //        LogUtil.info("扫码成功:1000001");
            //    }
            //}
            //catch (Exception ex)
            //{
            //    LogUtil.error("读取条码出错:" + ex.ToString());
            //}
        }
       
        private void UpdateBoard(string code)
        {
            //string[] str = code.Split('@');
            //if (str.Length >= 1)
            //{
            //    string boardCode = str[0];
            //    if (BoardManager.CurrBoard.WareCode.Equals(boardCode))
            //    {
            //        return;
            //    }
            //    else
            //    {
            //        BoardInfo board = BoardManager.getBoardByCode(boardCode);
            //        if (board == null)
            //        {
            //            LogUtil.error("条码【"+code+"】未找到匹配的程序,使用之前的程序【"+BoardManager.CurrBoard.boardName+"】");
            //        }
            //        else
            //        {
            //            LogUtil.error("条码【" + code + "】切换新程序【" + board.boardName + "】");
            //            BoardManager.UpdateCurrBoard(board.boardId);
            //            LoadPoint(board, -3);
            //        } 
            //    }
            //} 
        }

        private void AOICheck()
        {
            try
            {
                this.label1.Text = "视觉/光学检测";
                LoadAOIFile(aoiCheckFile);
                lblCodeResult.Visible = false;
                RunAOI(1);
                int result = CKResult("Result",10);
                if (result.Equals(1))
                {
                    LogUtil.info("AOI检测OK"); 
                    lblAOIResult.ForeColor = Color.Blue;
                    lblAOIResult.Text = "检测OK";
                    WorkCountManager.AddOKCount();
                }
                else
                {
                    LogUtil.info("AOI检测NG"); 
                    lblAOIResult.ForeColor = Color.Red;
                    lblAOIResult.Text = "检测NG";
                    WorkCountManager.AddNGCount();
                }
                lblAOIResult.Visible = true;

            }catch(Exception ex)
            {
                LogUtil.error("AOI检测出错:" + ex.ToString());
            }
        }

        private bool RunAOI(int num)
        {
            try
            {
                axCKVisionCtrl1.Execute(num);
                axCKVisionCtrl1.ZoomView(2);
                axCKVisionCtrl1.Redraw();
                Thread.Sleep(1000);
                axCKVisionCtrl1.Execute(num);
                axCKVisionCtrl1.ZoomView(2);
                axCKVisionCtrl1.Redraw();
                return true;
            }
            catch (Exception ex)
            {
                LogUtil.error("Error: Could not extcute proc. Original error: " + ex.Message);
                return false;
            }
        }
        private int CKResult(string name,int dataId)
        { 
            string result = "";
            try
            {
                int idTool = axCKVisionCtrl1.GetTool(name);

                double value = 0.0;
                object objValue = new VariantWrapper(value);
                if (axCKVisionCtrl1.GetValue(idTool, dataId, 0, ref objValue) == true)
                {
                    result = objValue.ToString();
                }
                LogUtil.info("获取【" + name + "】:" + result);
            }
            catch (Exception ex)
            {
                LogUtil.error("获取【" + name + "】出错:" + ex.ToString());
            } 

            if (result.ToLower().Equals("true"))
            {
                return 1;
            }
            else if (result.ToLower().Equals("false"))
            {
                return 2;
            }
            else
            {
                return 0;
            }
        }
        //private string getResult(string name,int DataId)
        //{
        //    string result = "";
        //    try
        //    {
        //        int idTool = axCKVisionCtrl1.GetTool(name);
              
        //        string  value = "";
        //        object objValue = new VariantWrapper(value);
        //        if (axCKVisionCtrl1.GetValue(idTool, DataId, 0, ref objValue) == true)
        //        {
        //            result = objValue.ToString();
        //        }
        //        LogUtil.info("获取【" + name + "】:" + result);
        //    }
        //    catch (Exception ex)
        //    {
        //        LogUtil.error("获取【" + name + "】出错:" + ex.ToString());
        //    }
        //    return result;
        //} 
    

        private void btnLook_Click(object sender, EventArgs e)
        {
            FrmWorkCount frm = new FrmWorkCount();
            frm.ShowDialog();
        }

        private void chbXunHuan_CheckedChanged(object sender, EventArgs e)
        {
            Robot.IsAutoRun = chbXunHuan.Checked;
        } 
        
        private void button1_Click(object sender, EventArgs e)
        {
            Task.Factory.StartNew(delegate ()
            {
                AOICheck();
            });
        } 
        private void btnChange_Click(object sender, EventArgs e)
        {
            if (btnChange.Text.Equals("每周统计"))
            {
                LoadCountPoint(false);
                btnChange.Text = "每日统计";
            }
            else
            {
                LoadCountPoint(true);
                btnChange.Text = "每周统计";
            }
        }

        private void FrmWork_Shown(object sender, EventArgs e)
        { 
            LoadCountPoint(true);
        }

        private void btnWeld_Click(object sender, EventArgs e)
        {
            string msg = Robot.StartWeld();
            if (!msg.Equals(""))
            {
                MessageBox.Show("焊接失败:" + msg);
            }
        }
    }
}