AxisControl.cs 5.5 KB
using DeviceLibrary;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AutoScanAndLabel
{
    public partial class AxisControl : UserControl
    {
        public AxisControl()
        {
            InitializeComponent();
        }

        private void AxisControl_Load(object sender, EventArgs e)
        {
            axisMoveControl1.LoadData(RobotManage.mainMachine);
            LoadPosList();
        }

        void LoadPosList() {
            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;
            foreach (ConfigBase configBase in RobotManage.Config.configList) {
                if (configBase.SubType < 10 || configBase.ProType != "PRO")
                    continue;
                
                if (configBase.SubType != lastSubType)
                {
                    lastSubType = configBase.SubType;
                    color = Color.FromArgb(random.Next(30,150), random.Next(30, 150), random.Next(30, 150));
                }
                //this.tableLayoutPanel1.RowCount++;
                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 = configBase.ProName;
                button.Size = new System.Drawing.Size(225, 34);
                button.Text = configBase.Explain;
                button.Click += Button_Click;
                button.ForeColor = color;
                TextBox textBox = new TextBox();
                textBox.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                textBox.Name = configBase.ProName;
                textBox.Margin = new Padding(4, 8, 0, 0);
                textBox.Size = new System.Drawing.Size(80, 23);
                textBox.Text = configBase.ProValue;
                textBox.Tag = configBase.SubType - 10;
                textBox.KeyPress += TextBox_KeyPress;
                textBox.TextChanged += TextBox_TextChanged;

                TextBox textBox2 = new TextBox();
                textBox2.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                textBox2.Name = configBase.ProName+"_speed";
                textBox2.Margin = new Padding(4, 8, 0, 0);
                textBox2.Size = new System.Drawing.Size(80, 23);
                textBox2.Text = configBase.TargetSpeed.ToString();
                textBox2.Tag = configBase.SubType - 10;
                textBox2.KeyPress += TextBox_KeyPress;
                textBox2.TextChanged += TextBox_TextChanged;

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


                if (r > 12)
                {
                    r = 0;
                    c += 3;
                }
            }
        }

        private void TextBox_TextChanged(object sender, EventArgs e)
        {
            var s = (sender as TextBox).Text;
            if (!int.TryParse(s, out _))
            {
                (sender as TextBox).Text = lastvalue;
            }
        }

        string lastvalue = "";
        private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            var s = (sender as TextBox).Text;
            if (int.TryParse(s, out _))
                lastvalue = s;
        }


        private void Button_Click(object sender, EventArgs e)
        {
            var cc = tableLayoutPanel1.Controls.Find(((Button)sender).Name,false);
            //Console.WriteLine(cc[1].Name);
            //Console.WriteLine(cc[1].Tag);
            //Console.WriteLine(cc[1].Text);

            ConfigMoveAxis configMoveAxis = getConfigMoveAxis((int)cc[1].Tag);

            AxisManager.AbsMove("", configMoveAxis.GetAxisValue(), int.Parse(cc[1].Text), configMoveAxis.TargetSpeed, configMoveAxis.AddSpeed, configMoveAxis.DelSpeed);
        }

        ConfigMoveAxis getConfigMoveAxis(int Axisid) {
            foreach (ConfigMoveAxis configMoveAxis in RobotManage.Config.moveAxisList) {
                if (configMoveAxis.GetAxisValue() == Axisid)
                    return configMoveAxis;
            }
            return null;
        }
        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= RobotManage.Config.GetType().GetProperty(textBox.Name);
                pi.SetValue(RobotManage.Config, int.Parse(textBox.Text));
            }
            CSVConfigReader.SaveConfig(RobotManage.Config.ConfigFilePath, RobotManage.Config);
        }
    }
}