XYMoveControl.cs 6.4 KB
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TSA_V.Common;
using TSA_V.DeviceLibrary;

namespace UserFromControl
{
    public partial class XYMoveControl : UserControl
    {
        public XYMoveControl()
        {
            InitializeComponent();
        }
        public string GroupName
        {
            get { return group.Text; }
            set
            {
                group.Text = value;
            }
        }

        public string CurrPosition
        {
            get { return lblCurrPosition.Text; }
            set
            {
                lblCurrPosition.Text = value;
            }
        }

        public double XValue
        {
            set
            {
                txtRobotX.Text = value.ToString();
            }
            get
            {
                return FormUtil.getDoubleValue(txtRobotX);
            }
        }

        public void DisMoveAndTestBtn()
        {
            btnMove.Visible = false;
            btnUpdate.Visible = false;
        }

        public double YValue
        {
            set
            {
                txtRobotY.Text = value.ToString();
            }
            get
            {
                return FormUtil.getDoubleValue(txtRobotY);
            }
        }
        private void btnUp_Click(object sender, EventArgs e)
        {
            TSAVBean.YStepMove(speedList[preValue] * yChangeValue);
        }
        private void btnDown_Click(object sender, EventArgs e)
        {
            TSAVBean.YStepMove(0 - speedList[preValue] * yChangeValue);
        }
        private void btnLeft_Click(object sender, EventArgs e)
        {
            TSAVBean.XStepMove(0 - speedList[preValue] * xChangeValue);
        }
        private void btnRight_Click(object sender, EventArgs e)
        {
            TSAVBean.XStepMove(speedList[preValue] * xChangeValue);
        }
        //private double stepValue =(double) ConfigAppSettings.GetNumValue(Setting_Init.XYStepValue);
        private double xChangeValue = ConfigAppSettings.GetDoubleValue(Setting_Init.XAxis_Change);
        private double yChangeValue = ConfigAppSettings.GetDoubleValue(Setting_Init.YAxis_Change);
        //private double GetStepValue()
        //{
        //    return stepValue / this.trackBar1.Maximum * trackBar1.Value;
        //}

        private static int preValue = 5;
        private void trackBar1_ValueChanged(object sender, EventArgs e)
        {
            preValue = trackBar1.Value;
            double value =speedList[preValue];
            string stepStr = UserControlResource.GetString(UserControlResource.StepValue,"步进值");
            lblSpeedText.Text = stepStr+ ":" +value+"mm";
            //TSAVBean.XYStepValue = value;
        }
        public void UpdatePosition()
        {
            txtRobotX.Text = TSAVBean.Axis_X.LastVoltage.ToString();
            txtRobotY.Text = TSAVBean.Axis_Y.LastVoltage.ToString();
        }
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            UpdatePosition();
        }

        private void btnMove_Click(object sender, EventArgs e)
        {
            //移动测试
            double x = FormUtil.getDoubleValue(txtRobotX);
            double y = FormUtil.getDoubleValue(txtRobotY);
            //判断位置是否超出范围
            if (!TSAVBean.IsValidPosition(x, y))
            {
                MessageBox.Show( "Position:" + x + "," + y + "Invalid,Please check the configuration ?", "", MessageBoxButtons.OK);
            }
            else
            {
                TSAVBean.Axis_X.SlowSetVoltage(x);
                TSAVBean.Axis_Y.SlowSetVoltage(y);
            }
        }

        public void MoveToPoint()
        {
            if (btnMove.Enabled)
            {
                btnMove_Click(null, null);
            }
        }

        private Dictionary<int, double> speedList = new Dictionary<int, double>();
        private void XYMoveControl_Load(object sender, EventArgs e)
        {
            speedList.Add(1, 0.5);
            speedList.Add(2, 1);
            speedList.Add(3, 2);
            speedList.Add(4, 5);
            speedList.Add(5, 10);
            speedList.Add(6, 15);
            speedList.Add(7, 20);
            speedList.Add(8, 25);
            speedList.Add(9, 30);
            speedList.Add(10, 50);

            List<double> valueList = new List<double>();
            int index = valueList.IndexOf(preValue);
            trackBar1.Value = preValue;
        }

        public bool ProcessKey(Keys keyData)
        {
            // 按快捷键Ctrl+S执行按钮的点击事件方法
            if (btnUp.Enabled.Equals(true) && btnDown.Enabled.Equals(true) && btnLeft.Enabled.Equals(true) && btnRight.Enabled.Equals(true))
            {
                if (keyData.Equals(Keys.Up) || keyData.Equals(Keys.MButton | Keys.Space))
                {
                    btnUp.PerformClick();
                    return true;
                }
                else if (keyData.Equals(Keys.Down) || keyData.Equals(Keys.Back | Keys.Space))
                {
                    this.btnDown.PerformClick();
                    return true;
                }
                else if (keyData.Equals(Keys.Left) || keyData.Equals(Keys.LButton | Keys.MButton | Keys.Space))
                {
                    this.btnLeft.PerformClick();
                    return true;
                }
                else if (keyData.Equals(Keys.Right) || keyData.Equals(Keys.LButton | Keys.RButton | Keys.MButton | Keys.Space))
                {
                    this.btnRight.PerformClick();
                    return true;
                }
            }return false;
        }

        public void UpdateStatus()
        { 
            if (TSAVBean.Axis_X.isRun)
            {
                btnLeft.Enabled = true;
                btnRight.Enabled = true;
            }
            if (TSAVBean.Axis_Y.isRun)
            {
                btnUp.Enabled = true;
                btnDown.Enabled = true;
            }
            if (TSAVBean.Axis_X.isRun && TSAVBean.Axis_Y.isRun)
            {
                double x = TSAVBean.Axis_X.LastVoltage;
                double y = TSAVBean.Axis_Y.LastVoltage;
                lblCurrPosition.Text = "  X:" + x + ",Y:" + y;
            }
        }
    }
}