ConfigControl.cs 11.2 KB
using OnlineStore;
using DeviceLibrary;
using OnlineStore.Common;
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 TheMachine
{
    using crc = OnlineStore.CodeResourceControl;
    public partial class ConfigControl : UserControl
    {

        private DeviceConfig _Config;
        public DeviceConfig Config
        {
            get { return _Config; }
            set
            {
                if (value == null)
                    return;
                _Config = value;

                LoadPosList();
            }
        }

        public StoreMachine Machine { get; set; }

        public ConfigControl()
        {            
            InitializeComponent();
            this.Tag = "not";
            crc.LanguageChangeEvent += Crc_LanguageChangeEvent;
        }

        private void Crc_LanguageChangeEvent(object sender, EventArgs e)
        {
            if (Config == null)
                return;
            LoadPosList();
        }

        void LoadPosList()
        {
            if (!this.Created)
                return;

            if (this.InvokeRequired)
            {
                this.Invoke((EventHandler)delegate
                {
                    try
                    {
                        LoadPosList();
                    }
                    catch (Exception ee)
                    {
                        MessageBox.Show("LoadPosList:" + ee.ToString());
                    }
                });
                return;
            }

            tableLayoutPanel1.SuspendLayout();
            int maxrow = tableLayoutPanel1.Height / 52;
            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;
            foreach (ConfigBase configBase in Config.configList)
            {
                if (configBase.SubType < 10)
                    continue;

                if (configBase.SubType != lastSubType)
                {
                    if (lastSubType > 0)
                    {
                        //r++;
                        //tableLayoutPanel1.Controls.Add(new Label(), c, r++);
                    }
                    lastSubType = configBase.SubType;
                    color = Color.FromArgb(random.Next(30, 150), random.Next(30, 150), random.Next(30, 150));

                }

                
                var tests = Config.GetType().GetProperty(configBase.ProName+"_speed");
                bool hasSpeed = tests!=null;
                //this.tableLayoutPanel1.RowCount++;
                if (hasSpeed)//(configBase.SubType < 30)
                {
                    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, 45);
                    button.Text = crc.GetString(configBase.ProName,configBase.Explain);
                    button.Click += Button_Click;
                    button.ForeColor = color;
                    button.Tag = configBase.SubType-10;
                    tableLayoutPanel1.Controls.Add(button, c, r);
                }
                else {
                    Label button = new Label();
                    button.Anchor = (AnchorStyles)(AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom);
                    button.Name = configBase.ProName;
                    button.Text = crc.GetString(configBase.ProName, configBase.Explain);
                    button.AutoSize = false;
                    button.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
                    button.ForeColor = color;
                    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 = configBase.ProName;
                textBox.Margin = new Padding(4, 8, 0, 0);
                textBox.Size = new System.Drawing.Size(180, 23);
                textBox.Text = configBase.ProValue;
                textBox.KeyPress += TextBox_KeyPress;
                textBox.TextChanged += TextBox_TextChanged;

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

                PropertyInfo pi = Config.GetType().GetProperty(configBase.ProName);
                if (pi != null)
                {
                    textBox.Tag = pi.PropertyType.Name;
                }

                if (hasSpeed)//(configBase.SubType < 30)
                {
                    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(textBox2, c + 2, r);
                    textBox.Tag = "Int32";

                }
                else {
                    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)
        {
            if (!Setting_Init.Device_Axis_Manual_Control_AtRunning && Machine.isRunning)
            {
                MessageBox.Show(crc.GetString("Res0210","系统正在运行,不能手动控制伺服"));
                return;
            }
            var cc = tableLayoutPanel1.Controls.Find(((Button)sender).Name, false);
            var speed = tableLayoutPanel1.Controls.Find(((Button)sender).Name+ "_speed", false);

            var axis = getConfigMoveAxis((int)((Button)sender).Tag);
            ConfigMoveAxis configMoveAxis = axis.Config;
            var targetpos = int.Parse(cc[1].Text);
            if (!axis.IsSafe(targetpos, out string msg))
            {
                MessageBox.Show(msg);
                return;
            }
            var targetSpeed = int.Parse(speed[0].Text);
            var AddSpeed = configMoveAxis.AddSpeed > 0 ? configMoveAxis.AddSpeed : targetSpeed * 4;
            var DelSpeed = configMoveAxis.DelSpeed > 0 ? configMoveAxis.DelSpeed : targetSpeed * 4;
            LogUtil.info($"手动点击:{((Button)sender).Text},pos:{targetpos}");
            AxisManager.AbsMove("", configMoveAxis.GetAxisValue(), targetpos, targetSpeed, AddSpeed, DelSpeed);//, configMoveAxis.AddSpeed, configMoveAxis.DelSpeed);
            
        }
        AxisBean getConfigMoveAxis(int Axisid)
        {
            //foreach (ConfigMoveAxis configMoveAxis in Config.moveAxisList)
            //{
            //    if (configMoveAxis.GetAxisValue() == Axisid)
            //        return configMoveAxis;
            //}
            foreach (AxisBean configMoveAxis in AxisBean.List[Machine.MachineSide])
            {
                if (configMoveAxis.Config.GetAxisValue() == Axisid)
                    return configMoveAxis;
            }
            return null;
        }

        private void ConfigControl_Load(object sender, EventArgs e)
        {
            if (DesignMode)
                return;
            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 = Config.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(Config, int.Parse(textBox.Text));
                    else if (pi.PropertyType.Name == "Double")
                        pi.SetValue(Config, double.Parse(textBox.Text));
                    else
                        pi.SetValue(Config, textBox.Text);
                }
            }
            CSVConfigReader.SaveConfig(Config.ConfigFilePath, Config);
            Config.DIList.Clear();
            Config.DOList.Clear();
            Config = (Store_ConfigBase)CSVConfigReader.LoadConfig<SIO_Type>(Config);
        }
    }
}