FrmBoardPutCom.cs 9.7 KB
using PUSICANLibrary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TSA_V.Common;
using TSA_V.DeviceLibrary;
using TSA_V.LoadCSVLibrary;

namespace TSA_V
{
    public partial class FrmBoardPutCom : FrmBase
    {

        private string bomName = ""; 
        private StockInfo Work = new StockInfo();
        private TSAVPosition currPosition = null;
        ComponetInfo currCom = new ComponetInfo();
        private List<ComponetInfo> oldConList = new List<ComponetInfo>();
        private List<ComponetInfo> comList = null;
        private int currIndex = 0;
        private BoardInfo board;
        public FrmBoardPutCom()
        {
            InitializeComponent();
        }
        private void FrmPutCom_Load(object sender, EventArgs e)
        {
            LanguageProcess();
            TSAVBean.IsInPut = true;
            Work.StartWork();
            SetScreen();
            timer1.Enabled = true;
        }

        public bool  SetOperateInfo(BoardInfo board, string bomName, List<ComponetInfo> list)
        {
            this.board = board;
            oldConList = list;
            comList = new List<ComponetInfo>();
            List<string> positinList = new List<string>();
            foreach (ComponetInfo obj in list)
            {
                if (positinList.Contains(obj.PositionNum))
                {

                }
                else
                {
                    positinList.Add(obj.PositionNum);
                    comList.Add(obj);
                }
            }

            //排序
            comList = (from m in comList orderby m.GetSortPosition() ascending select m).ToList();
            this.bomName = bomName;
            this.Text =ResourceCulture.GetString(ResourceCulture.BLText, "程序【{0}】元器件库【{1}】备料中", board.boardName, bomName);
            LogUtil.info("开始备料:程序【" + board.boardName + "】元器件库【" + bomName + "】");
            if (comList.Count <= 0)
            {
                MessageBox.Show(ResourceCulture.GetString(ResourceCulture.ConotFindCom,"未找到需要备料的元器件!"));
                return false;
            }
            currIndex = 0;
            ShowCom();
            if (TSAVBean.Status <= TSAVStatus.Wait)
            {
                MessageBox.Show(ResourceCulture.GetString(ResourceCulture.DeviceNotOk, "设备未连接,无法备料!"));
                return false;
            }
            else if (TSAVBean.Status.Equals(TSAVStatus.Reset))
            {
                MessageBox.Show(ResourceCulture.GetString(ResourceCulture.DeviceInReset, "设备正在重置中,请稍后再备料!"));
                return false;
            }
            return true;
        }

        private void ShowCom()
        {
            if (currIndex < 0)
            {
                currIndex = 0;
            }
            if (currIndex >= comList.Count)
            {
                currIndex = comList.Count - 1;
            }

            currCom = comList[currIndex];
            currPosition = CSVPositionReader<TSAVPosition>.GetPositonByNum(currCom.PositionNum);
            this.lblPN.Text = currCom.PN;
            this.numCount.Text = currCom.ComCount.ToString();
            this.lblTagNo.Text = currCom.TagNo;
            this.lblPosition.Text = currCom.PositionNum;
            this.lblDes.Text = currCom.ComponentDes;
            if (String.IsNullOrEmpty(currCom.TagNo))
            {
                string tagNo = "";
                List<SMTPointInfo> smts = (from m in board.smtList where m.PN.Equals(currCom.PN) select m).ToList();
                foreach(SMTPointInfo smt in smts)
                {
                    tagNo += smt.TagNo + "  ";
                }
                lblTagNo.Text = tagNo;
            }
            if (CurrLanguage.Equals(ResourceCulture.China))
            {
                lblEndInfo.Text = "已完成" + currIndex + "个";
                this.lblLeftInfo.Text = "剩余" + (comList.Count - 1 - currIndex) + "个";
            }
            else
            {
                lblEndInfo.Text = "" + currIndex + "/" + (comList.Count - 1 - currIndex)   ;
                this.lblLeftInfo.Text = " ";
            }
            //lblEndInfo.Text = "已完成" + currIndex + "个";
            //this.lblLeftInfo.Text = "剩余" + (comList.Count - 1 - currIndex) + "个";
            LogUtil.info("正在备料中:元器件库【" + bomName + "】位号【" + lblTagNo.Text + "】物料编号【" + currCom.PN + "】料盘编号【" + currCom.PositionNum + "】");
            if (currIndex.Equals(0))
            {
                btnPre.Enabled = false;
                btnNext.Enabled = true;
            }
            else if (currIndex.Equals(comList.Count - 1))
            {
                btnPre.Enabled = true;
                btnNext.Enabled = false;
            }
            else
            {
                btnPre.Enabled = true;
                btnNext.Enabled = true;
            }
            if (currPosition == null)
            {
                MessageBox.Show(ResourceCulture.GetString(ResourceCulture.CanotFindPosition, "未找到物料编号【{0}】的坐标", currCom.PositionNum));
            }
            else if (TSAVBean.Status.Equals(TSAVStatus.Runing))
            {
                Work.MoveToBag(currPosition);
            }
        }
        private void btnPre_Click(object sender, EventArgs e)
        {
            if (!Work.IsWaitMove)
            {
                currIndex--;
                ShowCom();
            }
        }
        private void SaveCom()
        {

            int newCount = (int)numCount.Value;
            ComponetInfo com = comList[currIndex];
            com.ComCount = newCount;
            CSVBomManager.UpdateComponet(bomName, com);
        }
        private void btnNext_Click(object sender, EventArgs e)
        {
            if (!Work.IsWaitMove)
            {
                SaveCom();
                currIndex++;
                ShowCom(); 
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            // 按快捷键Ctrl+S执行按钮的点击事件方法

            if (keyData.Equals(Keys.Up) || keyData.Equals(Keys.MButton | Keys.Space))
            {
                if (btnPre.Enabled)
                {
                    btnPre.PerformClick();
                }
                return true;
            }
            else if (keyData.Equals(Keys.Down) || keyData.Equals(Keys.Back | Keys.Space))
            {
                if (btnNext.Enabled)
                {
                    this.btnNext.PerformClick();
                }
                return true;
            }
            else if (keyData.Equals(Keys.Left) || keyData.Equals(Keys.LButton | Keys.MButton | Keys.Space))
            {
                if (btnPre.Enabled)
                {
                    btnPre.PerformClick();
                }
                return true;
            }
            else if (keyData.Equals(Keys.Right) || keyData.Equals(Keys.LButton | Keys.RButton | Keys.MButton | Keys.Space))
            {
                if (btnNext.Enabled)
                {
                    this.btnNext.PerformClick();
                }
                return true;
            }

            return base.ProcessCmdKey(ref msg, keyData); // 其他键按默认处理 
        }
        private void btnSaveCount_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            SaveCom();
        }

        private void FrmPutCom_FormClosing(object sender, FormClosingEventArgs e)
        {
            LogUtil.info("结束备料:元器件库【" + bomName + "】");
            timer1.Enabled = false;
            if (TSAVBean.Status > TSAVStatus.Reset)
            {
                Work.StopWork();
            }
            TSAVBean.IsInPut = false;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (Work.IsWorking)
            {
                if (Work.IsWaitMove)
                {
                    lblMoveStr.Text =ResourceCulture.GetString( ResourceCulture.Move,"移动中");
                    CheckWorkWait();
                }
                else if (IOManager.IOValue(IOManager.Footrest_Single).Equals(IO_VALUE.HIGH))
                {
                    btnNext.PerformClick();
                }

                if (!Work.IsWaitMove)
                {
                    lblMoveStr.Text = ResourceCulture.GetString(ResourceCulture.Inplace, "已到位"); 
                }
            }
           
        }
        private void CheckWorkWait()
        {
            string waitMsg = "";
            List<WaitResultInfo> list = Work.waitList;
            //当等待超过一分钟时,需要打印提示 
            TimeSpan span = DateTime.Now - Work.LastSetpTime;

            bool isOk = WaitResultInfo.GetWaitResult(list, span,false, out waitMsg);
            if (isOk)
            {
                Work.EndWait();
            }
            else
            {
                if (span.TotalMinutes > 1)
                {
                    waitMsg = "等待【" + waitMsg + "】超时 已等待【" + Math.Round( span.TotalMinutes ,2)+ "】分钟,";
                    foreach (WaitResultInfo wait in list)
                    {
                        waitMsg = waitMsg + "\r\n" + wait.ToStr();
                    }
                    LogUtil.error(waitMsg);
                }
            }
        }

        private void FrmPutCom_Shown(object sender, EventArgs e)
        { 
        }

    }
}