RobotPosControl.cs 9.2 KB
using DeviceLibrary;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using Robot.UR;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TheMachine
{
    using crc = OnlineStore.CodeResourceControl;
    public partial class RobotPosControl : UserControl
    {
        private Robot_Config _Config;
        public Robot_Config Config
        {
            get { return _Config; }
            set
            {
                if (value == null)
                    return;
                _Config = value;
            }
        }
        public RobotPosControl()
        {
            InitializeComponent();
            this.Tag = "not";
            crc.LanguageChangeEvent += Crc_LanguageChangeEvent;
            tableLayoutPanel1.GetType().GetProperty("DoubleBuffered", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)
.SetValue(tableLayoutPanel1, true, null);
            cb_loadweight.SelectedIndex = 0;
        }

        private void Crc_LanguageChangeEvent(object sender, EventArgs e)
        {
            if (!this.Created)
                return;
            this.Invoke((EventHandler)delegate
            {
                try
                {
                    LoadPos(_aCStorePosition);
                }
                catch (Exception ee)
                {
                    MessageBox.Show("Crc_LanguageChangeEvent LoadPos:" + ee.ToString());
                }
            });



           
        }
        private RobotPosition _aCStorePosition;
        public void LoadPos(RobotPosition aCStorePosition)
        {
            if (aCStorePosition == null)
                return;
            _aCStorePosition = aCStorePosition;

            tableLayoutPanel1.SuspendLayout();
            int maxrow = tableLayoutPanel1.Height / 37;
            tableLayoutPanel1.Controls.Clear();
            this.tableLayoutPanel1.RowStyles.Clear();
            //this.tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType., 26));
            int r = 0;
            int c = 0;
            int lastSubType = 0;
            Random random = new Random(1);
            Color color = Color.Black;

            var type = typeof(RobotPosition);
            PropertyInfo[] props = type.GetProperties();
            foreach (PropertyInfo prop in props)
            {
                var editable = prop.GetCustomAttributes(typeof(EditableAttribute));
                if (editable.Count() == 0)
                    continue;

                var asixno = ((EditableAttribute)editable.First()).Axisno;
                var attrib = prop.GetCustomAttributes(typeof(CSVAttribute));
                if (attrib.Count() == 0)
                    continue;

                var FieldName = ((CSVAttribute)attrib.First()).FieldName;
                var ProValue = prop.GetValue(aCStorePosition).ToString();

                if (asixno != lastSubType)
                {

                    lastSubType = asixno;
                    color = Color.FromArgb(random.Next(30, 150), random.Next(30, 150), random.Next(30, 150));

                }

                Button button = new Button();
                button.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
                button.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                button.Name = prop.Name;
                button.Size = new System.Drawing.Size(225, 27);
                button.Text = crc.GetString(prop.Name, FieldName);
                button.Click += Button_Click;
                button.ForeColor = color;
                button.Tag = asixno;
                tableLayoutPanel1.Controls.Add(button, c, r);

                TextBox textBox = new TextBox();
                textBox.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                textBox.Name = prop.Name;
                textBox.Margin = new Padding(4, 8, 0, 0);
                textBox.Size = new System.Drawing.Size(180, 23);
                textBox.Text = ProValue;
                textBox.KeyPress += TextBox_KeyPress;
                textBox.TextChanged += TextBox_TextChanged;
                

                tableLayoutPanel1.Controls.Add(textBox, c + 1, r);


                textBox.Tag = prop.PropertyType.Name;



                tableLayoutPanel1.SetColumnSpan(textBox, 2);


                r++;
                if (r > maxrow)
                {
                    r = 0;
                    c += 3;
                }
            }
            if (r <= maxrow + 1)
            {
                Label button1 = new Label();
                tableLayoutPanel1.Controls.Add(button1, c, r++);
            }
            tableLayoutPanel1.ResumeLayout(true);
        }

        private void TextBox_TextChanged(object sender, EventArgs e)
        {
            var lastlen = lastvalue.Length;
            var s = (sender as TextBox);
            if (s.Tag.ToString() == "Int32")
            {
                if (!int.TryParse(s.Text, out _))
                    s.Text = lastvalue;
                //s.SelectionStart = lastselectindex;
            }
            else if (s.Tag.ToString() == "Double")
            {
                if (!double.TryParse(s.Text, out _))
                    s.Text = lastvalue;
                //s.SelectionStart = lastselectindex;
            }
            
        }

        string lastvalue = "";
        int lastselectindex = 0;
        private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            var s = (sender as TextBox);
            if (s.Tag.ToString() == "Int32")
            {
                if (int.TryParse(s.Text, out _))
                    lastvalue = s.Text;
            }
            else if (s.Tag.ToString() == "Double")
            {
                if (double.TryParse(s.Text, out _))
                    lastvalue = s.Text;
            }
            lastselectindex = s.SelectionStart;
        }
        private void Button_Click(object sender, EventArgs e)
        {
            var cc = tableLayoutPanel1.Controls.Find(((Button)sender).Name, false);
            

            var axis = getConfigMoveAxis((int)((Button)sender).Tag);
            var targetpos = int.Parse(cc[1].Text);
            axis.SendMoveCmd(targetpos, axis.SpeedRate,false, RobotHelper.LoadRateParam[cb_loadweight.SelectedIndex]);
            LogUtil.info("手动点击机器人移动命令:" + axis.RobotIp + " - " + targetpos+ "-"+ cb_loadweight.SelectedIndex);
        }
        URRobotControl getConfigMoveAxis(int Axisid)
        {
            if (_aCStorePosition.PositionNum.StartsWith("MI1"))
                return RobotManage.Robot_MI1;
            else if (_aCStorePosition.PositionNum.StartsWith("MI2"))
                return RobotManage.Robot_MI2;
            else if (_aCStorePosition.PositionNum.StartsWith("CI"))
                return RobotManage.Robot_CI;

            return RobotManage.Robot_MI1;
        }

        private void ConfigControl_Load(object sender, EventArgs e)
        {
            OnlineStore.CodeResourceControl.LanguageChangeEvent += CodeResourceControl_LanguageChange;
            CodeResourceControl_LanguageChange(null,null);
        }

        private void CodeResourceControl_LanguageChange(object sender, EventArgs e)
        {
            OnlineStore.CodeResourceControl.LanguageProcess(this);
        }

        private void btnSavePos_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < tableLayoutPanel1.Controls.Count; i++)
            {
                if (tableLayoutPanel1.Controls[i].GetType().Name != "TextBox")
                    continue;

                TextBox textBox = (TextBox)tableLayoutPanel1.Controls[i];
                PropertyInfo pi = _aCStorePosition.GetType().GetProperty(textBox.Name);
                if (pi != null)
                {
                    //var cc = Config.configList.Find(new Predicate<ConfigBase>((c) => { return c.ProName == textBox.Name; }));
                    //if (cc != null)
                    //    cc.ProValue = textBox.Text;
                    if (pi.PropertyType.Name == "Int32")
                        pi.SetValue(_aCStorePosition, int.Parse(textBox.Text));
                    else if (pi.PropertyType.Name == "Double")
                        pi.SetValue(_aCStorePosition, double.Parse(textBox.Text));
                    else
                        pi.SetValue(_aCStorePosition, textBox.Text);
                }
            }

            var positionConfigFile = Path.Combine(Application.StartupPath, "Config\\MI1Postion.csv");
            var positionConfigFile1 = Path.Combine(Application.StartupPath, "Config\\MI2Postion.csv");
            var positionConfigFile3 = Path.Combine(Application.StartupPath, "Config\\CIPostion.csv");


            bool result = CSVPositionReader<RobotPosition>.SavePostion(positionConfigFile, _aCStorePosition);
            result = CSVPositionReader<RobotPosition>.SavePostion(positionConfigFile1, _aCStorePosition);
            result = CSVPositionReader<RobotPosition>.SavePostion(positionConfigFile3, _aCStorePosition);
        }
    }
}