FrmAutoFindPos.cs 18.3 KB
using OnlineStore.Common;
using OnlineStore.DeviceLibrary;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OnlineStore.XLRStore
{
    internal partial class FrmAutoFindPos : FrmBase
    {
        internal FrmAutoFindPos()
        {
            CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
            boxEquip = StoreManager.XLRStore.boxEquip;
            chkBoxCloseCam.Checked = bool.Parse(Common.ConfigAppSettings.GetValue(Setting_Init.CloseCamDetect));
            SetState(false);
        }
        BoxEquip boxEquip;
        string posSide = "AA";
        private void SetState(bool state)
        {
            tabControl1.Enabled = state;
            chkBoxCloseCam.Enabled = state;
        }
        /// <summary>
        /// 保存参数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            boxEquip.Config.Drawer_Rows = int.Parse(txtDrawerRows.Text);
            boxEquip.Config.Drawer_Columns = int.Parse(txtDrawerColumns.Text);
            boxEquip.Config.Rows_In_Drawer = int.Parse(txtRowsInDrawer.Text);
            boxEquip.Config.Cols_In_Drawer = int.Parse(txtColsInDrawer.Text);
            boxEquip.Config.Column_Spacing = int.Parse(txtColumnSpacing.Text);
            boxEquip.Config.Row_Spacing = int.Parse(txtRowSpacing.Text);

            boxEquip.Config.FindPos_Drawer_X_Error = int.Parse(txtMark_X_err.Text);
            boxEquip.Config.FindPos_Drawer_Y_Error = int.Parse(txtMark_Y_err.Text);
            boxEquip.Config.Validate_Drawer_X_Error = int.Parse(txtMarkX_err_Validate.Text);
            boxEquip.Config.Validate_Drawer_Y_Error = int.Parse(txtMarkY_err_Validate.Text);
            if (posSide.Equals("AA"))
            {
                boxEquip.Config.PixelToPulse_A_X = int.Parse(txtCoffX.Text);
                boxEquip.Config.PixelToPulse_A_Y = int.Parse(txtCoofY.Text);
                boxEquip.Config.FindPos_Drawer_A_X = float.Parse(txtMark_X.Text);
                boxEquip.Config.FindPos_Drawer_A_Y = float.Parse(txtMark_Y.Text);
                boxEquip.Config.Validate_Drawer_A_X = float.Parse(txtMarkX_Validate.Text);
                boxEquip.Config.Validate_Drawer_A_Y = float.Parse(txtMarkY_Validate.Text);
            }
            else
            {
                boxEquip.Config.PixelToPulse_B_X = int.Parse(txtCoffX.Text);
                boxEquip.Config.PixelToPulse_B_Y = int.Parse(txtCoofY.Text);
                boxEquip.Config.FindPos_Drawer_B_X = float.Parse(txtMark_X.Text);
                boxEquip.Config.FindPos_Drawer_B_Y = float.Parse(txtMark_Y.Text);
                boxEquip.Config.Validate_Drawer_B_X = float.Parse(txtMarkX_Validate.Text);
                boxEquip.Config.Validate_Drawer_B_Y = float.Parse(txtMarkY_Validate.Text);
            }
            StoreManager.SaveBoxConfig(boxEquip.Config);
        }

        private void FrmAutoFindPos_Load(object sender, EventArgs e)
        {
            txtDrawerRows.Text = boxEquip.Config.Drawer_Rows.ToString();
            txtDrawerColumns.Text = boxEquip.Config.Drawer_Columns.ToString();
            txtRowsInDrawer.Text = boxEquip.Config.Rows_In_Drawer.ToString();
            txtColsInDrawer.Text = boxEquip.Config.Cols_In_Drawer.ToString();
            txtColumnSpacing.Text = boxEquip.Config.Column_Spacing.ToString();
            txtRowSpacing.Text = boxEquip.Config.Row_Spacing.ToString();


            txtMark_X_err.Text = boxEquip.Config.FindPos_Drawer_X_Error.ToString();
            txtMark_Y_err.Text = boxEquip.Config.FindPos_Drawer_Y_Error.ToString();
            txtMarkX_err_Validate.Text = boxEquip.Config.Validate_Drawer_X_Error.ToString();
            txtMarkY_err_Validate.Text = boxEquip.Config.Validate_Drawer_Y_Error.ToString();
            UpdateData();

        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            posSide = "AA";
            UpdateData();
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            posSide = "BB";
            UpdateData();
        }
        private void UpdateData()
        {
            if (posSide.Equals("AA"))
            {
                txtMark_X.Text = boxEquip.Config.FindPos_Drawer_A_X.ToString();
                txtMark_Y.Text = boxEquip.Config.FindPos_Drawer_A_Y.ToString();
                txtMarkX_Validate.Text = boxEquip.Config.Validate_Drawer_A_X.ToString();
                txtMarkY_Validate.Text = boxEquip.Config.Validate_Drawer_A_Y.ToString();
                txtCoffX.Text = boxEquip.Config.PixelToPulse_A_X.ToString();
                txtCoofY.Text = boxEquip.Config.PixelToPulse_A_Y.ToString();
            }
            else
            {
                txtMark_X.Text = boxEquip.Config.FindPos_Drawer_B_X.ToString();
                txtMark_Y.Text = boxEquip.Config.FindPos_Drawer_B_Y.ToString();
                txtMarkX_Validate.Text = boxEquip.Config.Validate_Drawer_B_X.ToString();
                txtMarkY_Validate.Text = boxEquip.Config.Validate_Drawer_B_Y.ToString();
                txtCoffX.Text = boxEquip.Config.PixelToPulse_B_X.ToString();
                txtCoofY.Text = boxEquip.Config.PixelToPulse_B_Y.ToString();
            }
            groupBox5.Text = string.Format("更新其他抽屉内的库位,数据源为:{0}面,{1}层,{2}列", posSide, txtCurRow.Text, txtCurCol.Text);
            groupBox6.Text = string.Format("当前抽屉:{0}面,{1}层,{2}列", posSide, txtCurRow.Text, txtCurCol.Text);
            groupBox7.Text = string.Format("当前抽屉:{0}面,{1}层,{2}列", posSide, txtCurRow.Text, txtCurCol.Text);
        }
        int rows_in_drawer = 0;
        int colmns_in_drawer = 0;
        bool isBreak = false;

        private void button9_Click(object sender, EventArgs e)
        {
            btnStartVision.Enabled = false;
            btnPauseVision.Enabled = true;
            timer1.Enabled = true;
            timer1.Start();
            StringBuilder head2 = new StringBuilder(boxEquip.CID + posSide + txtStartRow.Text.PadLeft(2, '0') + txtStartCol.Text.PadLeft(2, '0') + "0101");
            boxEquip.autoFindPosMode = true;
            boxEquip.StartFindPos(new InOutParam(new InOutPosInfo("AutoFindPos", head2.ToString())));

        }

        private void button10_Click(object sender, EventArgs e)
        {
            boxEquip.autoFindPos.SetParam(int.Parse(txtCurRow.Text), int.Parse(txtCurCol.Text), int.Parse(txtStartRow.Text),int.Parse(txtStartCol.Text),float.Parse(txtCoffX.Text), float.Parse(txtCoofY.Text));
            StringBuilder head2 = new StringBuilder(boxEquip.CID + posSide + txtStartRow.Text.PadLeft(2, '0') + txtStartCol.Text.PadLeft(2, '0'));
            head2.Append("0101");
            LineMoveP lineMoveP = new LineMoveP();
            lineMoveP.UpdatePosById(head2.ToString());
            boxEquip.autoFindPos.PullUpdown_Diff_mid = lineMoveP.PullAxis_Updown_P2 - lineMoveP.PullAxis_Updown_P4;
            boxEquip.autoFindPos.PullUpdown_Diff_high = lineMoveP.PullAxis_Updown_P3 - lineMoveP.PullAxis_Updown_P4;
            boxEquip.autoFindPos.UpdownAxis_Diff_Upper_Low = lineMoveP.Updown_P7_P13 - lineMoveP.Updown_P6_P12;
            boxEquip.autoFindPos.Startposition_MoveAxis = lineMoveP.MoveAxis_P3;
            boxEquip.autoFindPos.Startposition_PullUpdpwnAxis = lineMoveP.PullAxis_Updown_P4;
            boxEquip.autoFindPos.Startposition_UpDownAxis = lineMoveP.Updown_P6_P12;
            int.TryParse(txtRowsInDrawer.Text, out rows_in_drawer);
            int.TryParse(txtColsInDrawer.Text, out colmns_in_drawer);
            boxEquip.autoFindPos.RowsInDraw = rows_in_drawer;
            boxEquip.autoFindPos.ColumnsInDraw = colmns_in_drawer;
            btnStartVision.Enabled = true;
            boxEquip.autoFindPosMode = false;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            label18.Text = $"当前抽屉的行:{boxEquip.autoFindPos.CurRow},列:{boxEquip.autoFindPos.CurColumn}";
        }

        private void button11_Click(object sender, EventArgs e)
        {
            if (btnPauseVision.Text.Equals("暂停"))
            {
                boxEquip.autoFindPosMode = false;
                btnPauseVision.Text = "继续";
                btnStopVision.Enabled = true;

            }
            else
            {
                boxEquip.autoFindPosMode = true;
                btnPauseVision.Text = "暂停";
                btnStopVision.Enabled = false;
            }
        }

        private void button12_Click(object sender, EventArgs e)
        {
            boxEquip.FindPosEnd();
            btnPauseVision.Enabled = false;
            btnStopVision.Enabled = false;
        }
        /// <summary>
        /// 取像并获取mark点坐标
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAcquireImage1_Click(object sender, EventArgs e)
        {
            Bitmap bitmap = null;
            bool rtn = false;
            eyemLib_Sharp.EyemLibDemo.EyemOcsFXYR eyemOcsFXYR;
            if (posSide.Equals("AA"))
            {
                bitmap = boxEquip.AcqImage("box_A");
            }
            else
            {
                bitmap = boxEquip.AcqImage("box_B");
            }
            pictureBox1.Image = null;
            if (bitmap != null)
            {
                int res = AutoFindPos.GetMarkInfo(bitmap, boxEquip.CID + posSide + "_Manual", string.Format("{0}", DateTime.Now.ToString("yyyyMMddhhmmss")), out eyemOcsFXYR, out Bitmap bitmap1);
                if (bitmap1 != null)
                    pictureBox1.Image = bitmap1;
                else
                    pictureBox1.Image = bitmap;
                if (res == 0)
                {
                    txtMark_X.Text = eyemOcsFXYR.fX.ToString("f2");
                    txtMark_Y.Text = eyemOcsFXYR.fY.ToString("f2");
                    lblResult.Text = "获取mark点信息成功";
                    lblResult.BackColor = Color.Green;
                }
                else
                {
                    lblResult.Text = "获取mark点信息失败,异常码:" + res;
                    lblResult.BackColor = Color.Red;
                }
            }
            else
            {
                lblResult.Text = "取像失败,请检查相机参数";
                lblResult.BackColor = Color.Red;
            }
        }


        private void checkBoxOpenAuto_CheckedChanged(object sender, EventArgs e)
        {
            SetState(checkBoxOpenAuto.Checked);
        }

        private void chkBoxCloseCam_CheckedChanged(object sender, EventArgs e)
        {
            boxEquip.CloseCamDetect = chkBoxCloseCam.Checked;
            Common.ConfigAppSettings.SaveValue(Setting_Init.CloseCamDetect, boxEquip.CloseCamDetect.ToString());
        }
        /// <summary>
        /// 选择抽屉并更新到其他抽屉
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            int.TryParse(txtRowsInDrawer.Text, out rows_in_drawer);
            int.TryParse(txtColsInDrawer.Text, out colmns_in_drawer);

            progressBar1.Maximum = boxEquip.Config.Drawer_Rows * boxEquip.Config.Drawer_Columns * rows_in_drawer * colmns_in_drawer;
            progressBar1.Minimum = 0;
            int count = 0;
            //位置配置
            string appPath = Application.StartupPath;
            //如果总配置文件存在,保存到总的配置文件
            string positionConfigFile = appPath + ConfigAppSettings.GetValue(Setting_Init.ConfigPath_BoxPosition);
            if (!File.Exists(positionConfigFile))
            {
                string nameStr = boxEquip.DeviceID.ToString();
                positionConfigFile = positionConfigFile.Replace(".csv", "_" + nameStr + ".csv");
            }
            string info = "";
            bool ok = false;
            isBreak = false;
            Task.Factory.StartNew(new Action(() =>
            {

                for (int i = 1; i <= boxEquip.Config.Drawer_Rows; i++)
                {
                    for (int j = 1; j <= boxEquip.Config.Drawer_Columns; j++)
                    {
                        for (int d = 1; d <= rows_in_drawer; d++)
                        {
                            for (int k = 1; k <= colmns_in_drawer; k++)
                            {
                                StringBuilder head1 = new StringBuilder(boxEquip.CID + posSide);
                                head1.Append(i.ToString().PadLeft(2, '0'));
                                head1.Append(j.ToString().PadLeft(2, '0'));
                                head1.Append(d.ToString().PadLeft(2, '0'));
                                head1.Append(k.ToString().PadLeft(2, '0'));
                                BoxPosition ktkPosition = CSVPositionReader<BoxPosition>.GetPositon(head1.ToString());
                                StringBuilder head2 = new StringBuilder(boxEquip.CID + posSide + txtCurRow.Text.PadLeft(2, '0') + txtCurCol.Text.PadLeft(2, '0'));
                                head2.Append(d.ToString().PadLeft(2, '0'));
                                head2.Append(k.ToString().PadLeft(2, '0'));
                                BoxPosition srcPosition = CSVPositionReader<BoxPosition>.GetPositon(head2.ToString());
                                if (ktkPosition != null && srcPosition != null)
                                {
                                    ktkPosition.ComAxis_AB_P2 = srcPosition.ComAxis_AB_P2;
                                    ktkPosition.ComAxis_AB_P3 = srcPosition.ComAxis_AB_P3;
                                    ktkPosition.PullAxis_Inout_P2_P4 = srcPosition.PullAxis_Inout_P2_P4;
                                    ktkPosition.PullAxis_Inout_P3_P5 = srcPosition.PullAxis_Inout_P3_P5;
                                    ktkPosition.XAxis_AB_P3 = srcPosition.XAxis_AB_P3;
                                    bool result = CSVPositionReader<BoxPosition>.SavePostion(positionConfigFile, ktkPosition);
                                    if (result)
                                        info = $"{head2.ToString()}->{head1.ToString()} 成功...{(count * 100 / progressBar1.Maximum).ToString("d")}%";
                                    else
                                        info = $"{head2.ToString()}->{head1.ToString()} 保存失败...{(count * 100 / progressBar1.Maximum).ToString("d")}%";
                                    ok = result;
                                }
                                else if (ktkPosition == null)
                                {
                                    info = $"{head2.ToString()}->{head1.ToString()} 失败,目标仓位不存在...{(count * 100 / progressBar1.Maximum).ToString("d")}%";
                                    ok = false;
                                }
                                else if (srcPosition == null)
                                {
                                    info = $"{head2.ToString()}->{head1.ToString()} 失败,源仓位不存在...{(count * 100 / progressBar1.Maximum).ToString("d")}%";
                                    ok = false;
                                }
                                label8.Text = info;
                                label8.BackColor = ok ? Color.Green : Color.Red;
                                count++;
                                progressBar1.Value = count; ;
                                System.Threading.Thread.Sleep(50);
                                if (isBreak)
                                    return;
                            }
                        }

                    }
                }
                info = $"更新完成!";
                label8.Text = info;
                label8.BackColor = Color.Green;
            }));
        }
        /// <summary>
        /// 文本框内容变化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtChanged(object sender, EventArgs e)
        {
            groupBox5.Text = string.Format("更新其他抽屉内的库位,数据源为:{0}面,{1}层,{2}列", posSide, txtCurRow.Text, txtCurCol.Text);
            groupBox6.Text = string.Format("当前抽屉:{0}面,{1}层,{2}列", posSide, txtCurRow.Text, txtCurCol.Text);
            groupBox7.Text = string.Format("当前抽屉:{0}面,{1}层,{2}列", posSide, txtCurRow.Text, txtCurCol.Text);
        }

        private void btnStopUpdate_Click(object sender, EventArgs e)
        {
            isBreak = true;
        }

        private void btnAcquireImage2_Click(object sender, EventArgs e)
        {
            Bitmap bitmap = null;
            eyemLib_Sharp.EyemLibDemo.EyemOcsFXYR eyemOcsFXYR;
            if (posSide.Equals("AA"))
            {
                bitmap = boxEquip.AcqImage("box_A");
            }
            else
            {
                bitmap = boxEquip.AcqImage("box_B");
            }
            pictureBox1.Image = null;
            if (bitmap != null)
            {
                int res = AutoFindPos.GetMarkInfo(bitmap, boxEquip.CID + posSide + "_Manual", string.Format("{0}", DateTime.Now.ToString("yyyyMMddhhmmss")), out eyemOcsFXYR, out Bitmap bitmap1);
                if (bitmap1 != null)
                    pictureBox2.Image = bitmap1;
                else
                    pictureBox2.Image = bitmap;
                if (res == 0)
                {
                    txtMarkX_Validate.Text = eyemOcsFXYR.fX.ToString("f2");
                    txtMarkY_Validate.Text = eyemOcsFXYR.fY.ToString("f2");
                    lblResult.Text = "获取mark点信息成功";
                    lblResult.BackColor = Color.Green;
                }
                else
                {
                    lblResult.Text = "获取mark点信息失败,异常码:" + res;
                    lblResult.BackColor = Color.Red;
                }
            }
            else
            {
                lblResult.Text = "取像失败,请检查相机参数";
                lblResult.BackColor = Color.Red;
            }
        }

        private void btnSaveMarkInfo2_Click(object sender, EventArgs e)
        {

        }
    }
}