FrmScanPutCom.cs 11.1 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 FrmScanPutCom : 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 string  currPn ="";
        private int currIndex = -1;
        private BoardInfo board;
        public FrmScanPutCom()
        {
            InitializeComponent();
        }
        private void FrmPutCom_Load(object sender, EventArgs e)
        {
            LanguageProcess();
            TSAVBean.IsInPut = true;
            Work.StartWork();
            SetScreen();
            timer1.Enabled = true;
            txtScanPn.Text = "";
            txtScanPn.Focus();
        }

        public bool  SetOperateInfo(BoardInfo board, string bomName, List<ComponetInfo> list)
        {
            BoardManager.CurrBoard = board;
            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 = new List<ComponetInfo>(list);

            //排序
            comList = (from m in comList orderby m.PN, 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;
            }
            ClearCom();
            if (!Setting_NInit.App_IsDebug)
            {
                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 ClearCom()
        {
            txtScanPn.Text = string.Empty;
            currPn = "";
            currCom = null;

            lblMoveStr.Text = "";
            this.lblPN.Text = "";
            this.numCount.Text = "";
            this.lblTagNo.Text = "";
            this.lblPosition.Text = "";
            this.lblDes.Text = "";
            txtScanPn.Focus();
            btnSaveCount.Visible = false ;
        }
        private int getIndex(string pn)
        {
            int index = -1;
            for(int i = 0; i < comList.Count; i++)
            {
                if (comList[i].PN.Equals(pn))
                {
                    index = i;
                    break;
                }
            }
            return index;
        }
        private void ShowCom()
        {
           
            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;
            btnSaveCount.Visible = true;
            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;
            }
 
            LogUtil.info("正在备料中:元器件库【" + bomName + "】位号【" + lblTagNo.Text + "】物料编号【" + currCom.PN + "】料盘编号【" + currCom.PositionNum + "】");
         
            if (currPosition == null)
            {
                MessageBox.Show(ResourceCulture.GetString(ResourceCulture.CanotFindPosition, "未找到物料编号【{0}】的坐标", currCom.PositionNum));
            }
            else if (TSAVBean.Status.Equals(TSAVStatus.Runing))
            {
                Work.MoveToBag(currPosition, currCom);
            }
            numCount.Focus();
        } 
        private void SaveCom()
        {

            int newCount = (int)numCount.Value;
            ComponetInfo com = comList[currIndex];
            com.ComCount = newCount.ToString();
            CSVBomManager.UpdateComponet(bomName, com);

            try
            {
                TSAVPosition position = null;
                position = CSVPositionReader<TSAVPosition>.GetPositonByNum(com.PositionNum);
                LabelInfo label = LedLabelController.GetLabel(position, com, false);
                LedLabelController.UpdateScreen(label);
            }
            catch (Exception ex)
            {
                LogUtil.error("备料后更新电子屏出错:" + ex.ToString());
            }
        } 

        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, EventArgs e)
        {
            SaveCom();
            ClearCom();
        }

        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 (currPn == "")
            {
                lblMoveStr.Text = "";
                txtScanPn.Focus();
            }
            else
            {
                numCount.Focus();

                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();
                        //点脚踏时保存。
                        btnSaveCount.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)
        { 
        }

        private void txtScanPn_KeyDown(object sender, KeyEventArgs e)
        {
            // 检测 Enter 键
            if (e.KeyCode == Keys.Enter)
            {
                if (currPn == "")
                {
                    string pn = txtScanPn.Text.Trim();
                    currIndex = getIndex(pn);
                    if (currIndex >= 0)
                    {
                        currPn = pn;
                        ShowCom();
                    }
                    else
                    {
                        //未找到对应的PN,请重新输入
                        currPn = "";
                        currIndex = -1;
                        MessageBox.Show(ResourceControl.GetString("NotFindPN", "未找到对应的PN={0},请重新扫码", new string[] { pn }));
                        txtScanPn.Clear();
                    }
                }
            }
        }
    }
}