FrmTool.cs 13.9 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.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OnlineStore.ACSingleStore
{
    public partial class FrmTool : FrmBase
    {
        private BoxAutoPoint autoP;
        private string LogName="自动对点位";
        
        private BoxBean Box = null; 
        //private PToolInfo workInfo = new PToolInfo();
        public FrmTool(BoxBean box, string ioIp, int ioIndex, string name)
        {
            InitializeComponent();
            this.Box = box;
            autoP = new BoxAutoPoint(box);


            this.Text = name + "_自动对点位 ";
            LogName = this.Text;
            FrmTool.CheckForIllegalCrossThreadCalls = false;
        }
        private int P3Offset = 0;
        private int P4Offset = 0;
        private int P5Offset = 0;
        private int P6Offset = 0;
        private void Form1_Load(object sender, EventArgs e)
        {
            AxisManager.instance.IsShowMsg = false;

            CheckForIllegalCrossThreadCalls = false;


            if (IOManager.instance == null)
            {
                IOManager.Init();
                IOManager.instance.ConnectionIOList(new List<string> { /*IoIp*/ });
            }

            P3Offset = ConfigAppSettings.GetIntValue(Setting_Init.Tool_P3_Offset);
            P4Offset = ConfigAppSettings.GetIntValue(Setting_Init.Tool_P4_Offset);
            P5Offset = ConfigAppSettings.GetIntValue(Setting_Init.Tool_P5_Offset);
            P6Offset = ConfigAppSettings.GetIntValue(Setting_Init.Tool_P6_Offset);
            int tSpeed = ConfigAppSettings.GetIntValue(Setting_Init.Tool_TargetSpeed);
            int tPosition = ConfigAppSettings.GetIntValue(Setting_Init.Tool_TargetPosition);

            if (P3Offset.Equals(0)) P3Offset = 60;
            if (P4Offset.Equals(0)) P4Offset = -60;
            if (P5Offset.Equals(0)) P5Offset = -60;
            if (P6Offset.Equals(0)) P6Offset = 60;
            if (tSpeed.Equals(0)) tSpeed = 20;
            if (tPosition.Equals(0)) tPosition = 60;

            txtP3Offset.Text = P3Offset.ToString();
            txtP4Offset.Text = P4Offset.ToString();
            txtP5Offset.Text = P5Offset.ToString();
            txtP6Offset.Text = P6Offset.ToString();
            txtSpeed.Text = tSpeed.ToString();
            txtTargetPosition.Text = tPosition.ToString();

            SaveConfig(tSpeed, tPosition);


            axisJogControl1.SetBoxBean(Box);
            int p = AxisManager.instance.GetActualtPosition(Box.Config.InOut_Axis.DeviceName, Box.Config.InOut_Axis.GetAxisValue());
            if (p > 0)
            {
                txtInout.Text = p.ToString();
            }

            p = AxisManager.instance.GetActualtPosition(Box.Config.Middle_Axis.DeviceName, Box.Config.Middle_Axis.GetAxisValue());
            if (p > 0)
            {
                DataGridViewRow view = new DataGridViewRow();
                view.CreateCells(dataGridView1);

                view.Cells[0].Value = p;
                dataGridView1.Rows.Add(view);
            } 
            timer1.Start();
            ioSingle.IOName = ResourceCulture.GetString("激光检测信号");
        }
         
        private void SaveConfig(int speed, int position)
        {
            //保存配置
            ConfigAppSettings.SaveValue(Setting_Init.Tool_P3_Offset, P3Offset);
            ConfigAppSettings.SaveValue(Setting_Init.Tool_P4_Offset, P4Offset);
            ConfigAppSettings.SaveValue(Setting_Init.Tool_P5_Offset, P5Offset);
            ConfigAppSettings.SaveValue(Setting_Init.Tool_P6_Offset, P6Offset);
            ConfigAppSettings.SaveValue(Setting_Init.Tool_TargetSpeed, speed);
            ConfigAppSettings.SaveValue(Setting_Init.Tool_TargetPosition, position);
        }
     
        private void workMoveStatus(bool isStart)
        {
            group1.Enabled = !isStart;
            group2.Enabled = !isStart;
            group3.Enabled = !isStart;
            group4.Enabled = !isStart;
            group5.Enabled = true;
            group6.Enabled = !isStart ;
            btnStart.Enabled = !isStart;
            btnStop.Enabled = isStart;
            axisJogControl1.Enabled = !isStart;
            txtSpeed.Enabled = !isStart;
            btnExit.Enabled = !isStart;

        }
        private void btnSdStop_Click(object sender, EventArgs e)
        {
            autoP.StopMove();
        }

      
        private bool isInProcesss = false;


        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                ioSingle.IOValue = (int)Box.IOValue(IO_Type.CheckPos);
                ioSingle.ShowData();
                if (autoP.IsStop())
                {
                    if (group1.Enabled.Equals(false))
                    {
                        workMoveStatus(false);
                    }
                    if (lblFileP.Text.Equals(""))
                    {
                        lblMoveInfo.Text = "请按步骤确认位置信息,然后点击“开始校对位置”按钮启动位置校准";
                    }
                    else
                    {
                        lblMoveInfo.Text = "点位校准已结束,请打开文件夹查看位置文件";
                    }
                }
                else
                {
                    if (group1.Enabled.Equals(true))
                    {
                        workMoveStatus(true);
                    }

                    lblMoveInfo.Text = autoP.CurrStr();
                }
            }
            catch (Exception ex)
            {
            }
        }

        private void FrmPositionTool_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!autoP.IsStop())
            {
                MessageBox.Show("请先停止位置校准,再退出界面","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                e.Cancel = true;
                return;
            }
            timer1.Stop();
         
            LogUtil.logBox = null; 
        }

        private void btnOpenFolder_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("Explorer.exe", autoP.paramInfo.FileTargetPath);
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnInoutP_Click(object sender, EventArgs e)
        {
            int p = AxisManager.instance.GetActualtPosition(Box.Config.InOut_Axis.DeviceName, Box.Config.InOut_Axis.GetAxisValue()); 
            txtInout.Text = p.ToString();
        }

        private void btnUpdownP_Click(object sender, EventArgs e)
        {
            int p = AxisManager.instance.GetActualtPosition(Box.Config.UpDown_Axis.DeviceName, Box.Config.UpDown_Axis.GetAxisValue());
            txtTargetPosition.Text = p.ToString();
        }

        private void btnAddMiddleP_Click(object sender, EventArgs e)
        {
            int p = AxisManager.instance.GetActualtPosition(Box.Config.Middle_Axis.DeviceName, Box.Config.Middle_Axis.GetAxisValue());
            
            DataGridViewRow view = new DataGridViewRow();
            view.CreateCells(dataGridView1);
            view.Cells[0].Value = dataGridView1.Rows.Count + 1;
            view.Cells[1].Value = p;
            dataGridView1.Rows.Add(view);

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1 && e.ColumnIndex >= 0)
            {
                string name = this.dataGridView1.Columns[e.ColumnIndex].Name;
                int rowIndex = e.RowIndex;
                if (name.Equals(this.Column_Del.Name))
                {
                    DialogResult dialogResult = MessageBox.Show("确认要删除该行数据吗?", "提示?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                    if (dialogResult.Equals(DialogResult.OK))
                    {
                        this.dataGridView1.Rows.RemoveAt(rowIndex);
                        for (int index = 0; index < dataGridView1.Rows.Count; index++)
                        {
                            dataGridView1.Rows[index].Cells[0].Value = (index + 1);
                        }
                    }
                }
            }
        }

        private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (e.RowIndex > -1 && e.ColumnIndex > -1)
            {
                DataGridView grid = (DataGridView)sender;
                grid.Rows[e.RowIndex].ErrorText = "";
                //这里最好用列名,而不是列索引号做判断
                if (grid.Columns[e.ColumnIndex].Name == Column_position.Name)
                {

                    Int32 newInteger = 0;
                    if (!int.TryParse(e.FormattedValue.ToString(), out newInteger))
                    {
                        e.Cancel = true;
                        grid.Rows[e.RowIndex].ErrorText = "位置只能输入整数,请输入正确的位置 !";
                        MessageBox.Show("位置只能输入整数,请输入正确的位置 !");
                        return;
                    }
                }
            }
        }


        #region 校准位置功能

        private void btnAbsMove_Click(object sender, EventArgs e)
        {
            int inoutP = FormUtil.GetIntValue(txtInout);
            if (inoutP <= 0)
            {
                MessageBox.Show("请输入正确的进出轴前进位置","提示");
                txtInout.Focus();
                return;
            }
            int speed = FormUtil.GetIntValue(txtSpeed);
            if (speed <= (0))
            {
                MessageBox.Show(ResourceCulture.GetString("请输入正确的速度"), "提示");
                txtSpeed.Focus();
                return;
            }
            int TPostion = FormUtil.GetIntValue(txtTargetPosition);
            if (TPostion <= 0)
            {
                MessageBox.Show("请输入正确的升降轴最高位置", "提示");
                txtTargetPosition.Focus();
                return;
            }

            P3Offset = FormUtil.GetIntValue(txtP3Offset);
            P4Offset = FormUtil.GetIntValue(txtP4Offset);
            P5Offset = FormUtil.GetIntValue(txtP5Offset);
            P6Offset = FormUtil.GetIntValue(txtP6Offset);

            SaveConfig(speed, TPostion);

            autoP.paramInfo = new PToolInfo();
            autoP.paramInfo.InoutTargetPosition = inoutP;
            autoP.paramInfo.UpdownTargetPosition = TPostion;
            autoP.paramInfo.UpdownSpeed = speed;
            autoP.paramInfo.MiddlePositionList = new List<int>();
            autoP.paramInfo.PositionList = new List<int>();
            autoP.paramInfo.P3Offset = P3Offset;
            autoP.paramInfo.P4Offset = P4Offset;
            autoP.paramInfo.P5Offset = P5Offset;
            autoP.paramInfo.P6Offset = P6Offset;
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                string p = row.Cells[Column_position.Index].Value.ToString();
                try
                {
                    int mP = Convert.ToInt32(p);
                    autoP.paramInfo.MiddlePositionList.Add(mP);
                }
                catch (Exception ex)
                {
                    LogUtil.error("旋转轴位置列表【" + p + "】不是整数,直接跳过");
                }
            }
            if (autoP.paramInfo.MiddlePositionList.Count <= 0)
            {
                MessageBox.Show("请至少输入一个旋转轴位置","提示");
                dataGridView1.Focus();
                return;
            }

            autoP.paramInfo.LastValue = IO_VALUE.LOW;

            string date = DateTime.Now.ToString("yyyyMMddHHmm");
            string filePath = Application.StartupPath + @"\position\"+ date + @"\";
            if (Directory.Exists(filePath))
            {
                DialogResult result = MessageBox.Show("文件夹【" + filePath + "】已存在,将清除文件夹内容,是否确定清除?",
                    "确认提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (!result.Equals(DialogResult.OK))
                {
                    return;
                }
            }
            else
            {
                Directory.CreateDirectory(filePath);
            }
            autoP.paramInfo.FileTargetPath = filePath; 
            string msg = autoP.paramInfo.ParamStr();
            DialogResult result1 = MessageBox.Show(msg, "请确认对点位参数", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (result1.Equals(DialogResult.OK))
            {
                autoP.StopMove();
                LogUtil.info(LogName + "开始校对位置:" + msg + " 启动定时器 "); 
                autoP.Start();
                lblFileP.Text = "目标文件夹:" + filePath;
            }
        }

        #endregion

        private void btnMHome_Click(object sender, EventArgs e)
        {
            LogUtil.info(LogName + "点击:旋转轴回原点");
            ConfigMoveAxis axis = Box.Config.Middle_Axis;
            AxisManager.instance.HomeMove(axis.DeviceName, axis.GetAxisValue(),axis.HomeHighSpeed,axis.HomeLowSpeed,axis.HomeAddSpeed);
        }

        private void btnMStop_Click(object sender, EventArgs e)
        {
            LogUtil.info(LogName + "点击:旋转轴停止");
            ConfigMoveAxis axis = Box.Config.Middle_Axis;
            AxisManager.instance.SuddenStop(axis.DeviceName, axis.GetAxisValue());
        }
    }

    public class ResourceCulture
    {
        internal static string GetString(string str)
        {
            return str;
        }
    }
}