uc_boxdebug.cs 8.8 KB
using DeviceLibrary;
using log4net;
using OnlineStore;
using OnlineStore.Common;
using OnlineStore.DeviceLibrary;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections;
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.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TheMachine
{
    public partial class uc_boxdebug : UserControl
    {
        private Robot_Config _Config;
        public Robot_Config Config
        {
            get { return _Config; }
            set
            {
                if (value == null)
                    return;
                _Config = value;
                robotPosControl1.Config = _Config;
            }
        }

        //如果总配置文件存在,保存到总的配置文件
        static string appPath = Application.StartupPath;
        class PosState
        {
            public int InStoreCheck = 0;
            public int OutStoreCheck = 0;
        }
        public uc_boxdebug()
        {
            InitializeComponent();
        }
        Dictionary<string, RobotPosition> data;
        public void Init(Dictionary<string, RobotPosition> _data)
        {
            data = _data;
            FillBoxPos();
            //timer1.Enabled = true;
        }
        DataGridViewCellStyle GetStyle(string posid) {
            return dgv_none;
        }
        class PosPos {
            public string Posid;
            public int index;
            public int row;
            public int col;
        }
        int PosCompare(string a, string b) {
            if (a.StartsWith("fix"))
                return -1;
            if (b.StartsWith("fix"))
                return 1;
            

            string[] pa = a.Split('_');
            string[] pb = b.Split('_');

            if (int.TryParse(pa[1], out _))
                pa[1] += "A";
            if (int.TryParse(pb[1], out _))
                pb[1] += "A";

            var pa1 = pa[1][0] + (pa[1][1].Equals('A') ? "1" : "2");
            var pb1 = pb[1][0] + (pb[1][1].Equals('A') ? "1" : "2");

            var pa2 = pa[2];
            var pb2 = pb[2];
            if (pa[2].Length == 2)
            {
                if (pa[2][1].Equals('A') || pa[2][1].Equals('B'))
                {
                    pa2 = (pa[2][1].Equals('A') ? "1" : "2") + pa[2][0];
                }
                else if (pa[2][1].Equals('1') || pa[2][1].Equals('2'))
                {
                    pa2 = (pa[2][1].Equals('1') ? "1" : "2") + pa[2][0];
                }
            }

            if (pb[2].Length == 2)
            {
                if ((pb[2][1].Equals('A') || pb[2][1].Equals('B')))
                {
                    pb2 = (pb[2][1].Equals('A') ? "1" : "2") + pb[2][0];
                }
                else if ((pb[2][1].Equals('1') || pb[2][1].Equals('2')))
                {
                    pb2 = (pb[2][1].Equals('1') ? "1" : "2") + pb[2][0];
                }
            }

            var isa = Regex.Replace(pa[1], @"[^\d]*", "") + pa1 + pa2 + pa[3].PadLeft(2, '0');
            var isb = Regex.Replace(pb[1], @"[^\d]*", "") + pb1 + pb2 + pb[3].PadLeft(2, '0');

            if (int.Parse(isa) == int.Parse(isb))
                return 0;
            else if (int.Parse(isa) > int.Parse(isb))
                return 1;
            else
                return -1;
        }

        static Font font = new Font("宋体",10);
        DataGridViewCellStyle dgv_ok = new DataGridViewCellStyle() { BackColor = Color.LightGreen, Font = font };
        DataGridViewCellStyle dgv_oin = new DataGridViewCellStyle() { BackColor = Color.LightGray, Font = font };
        DataGridViewCellStyle dgv_oout = new DataGridViewCellStyle() { BackColor = Color.LightSeaGreen, Font = font };
        DataGridViewCellStyle dgv_none = new DataGridViewCellStyle() { BackColor = Color.White,Font= font };
        void FillBoxPos()
        {
            dataGridView1.Columns.Clear();
            dataGridView1.Rows.Clear();

            int colcount = 9;

            for (int i = 0; i < colcount; i++)
            {
                dataGridView1.Columns.Add("Col" + i, "Col" + i);
            }
            for (int i = 0; i < dataGridView1.Columns.Count; i++)
            {
                dataGridView1.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                dataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
            }
            dataGridView1.Rows.Add();
            int currentRowIndex = 0;
            int currentColIndex = -1;
            string lastAB = "";
            List<string> datakeys = data.Keys.ToList();
            for (int i = 0; i < data.Count; i++)
            {
                string posname = datakeys[i];
                string memo = "";
                if (data.ContainsKey(posname))
                {
                    memo = data[posname].Memo;
                }
                
                string ab = memo.Substring(0, 2);

                if (posname.StartsWith("LabelP") || posname.StartsWith("RFIDP"))
                {
                    ab = "aa";
                }

                var showname = posname;
                if (ab != lastAB)
                {
                    currentRowIndex = 0;
                    currentColIndex++;
                    lastAB = ab;
                    //if (posname.StartsWith("L"))
                    //{
                    //    dataGridView1.Columns[currentColIndex].HeaderText = "(" + posname.Substring(0, 2) + "";
                    //}
                    //else
                    {
                        dataGridView1.Columns[currentColIndex].HeaderText = crc.GetString("Res0031","取放料点");

                    }
                }
                //if (!posname.StartsWith("L"))
                {

                    if (memo.IndexOf("_") >= 0)
                    {
                        var id = memo.IndexOf("_");
                        var m = memo.Substring(0,id);
                        var m2 = memo.Substring(id, memo.Length-id);
                        showname = posname + "(" + crc.GetString(m, m)+ m2 + ")";
                    }
                    else
                        showname = posname + "(" + crc.GetString(memo, memo) + ")";
                }

                var currentRow = dataGridView1.Rows[currentRowIndex];


                currentRow.Cells[currentColIndex].Value = showname;
                currentRow.Cells[currentColIndex].Tag = posname;
                currentRow.Cells[currentColIndex].Style = GetStyle(posname);

                currentRowIndex++;
                if (currentRowIndex >= dataGridView1.Rows.Count)
                    dataGridView1.Rows.Add();
            }
            for (int i = 0; i < dataGridView1.Columns.Count; i++)
            {
                if (dataGridView1.Rows[0].Cells[i].Value == null)
                {
                    dataGridView1.Columns[i].Width = 0;
                }
            }
            if (dataGridView1.Rows[0].Cells[dataGridView1.Columns.Count - 1].Value != null)
            {
                dataGridView1.Rows[0].Cells[dataGridView1.Columns.Count - 1].Selected = true;
            }
            else if (dataGridView1.Rows[0].Cells[dataGridView1.Columns.Count - 2].Value != null)
            {
                dataGridView1.Rows[0].Cells[dataGridView1.Columns.Count - 2].Selected = true;
            }
            dataGridView1.Rows.RemoveAt(dataGridView1.Rows.Count - 1);
            if (dataGridView1.SelectedCells.Count > 0)
                setData(dataGridView1.SelectedCells[0].Tag.ToString());
            else
                setData(data.Keys.ToList().Last());
            

        }
        void setData(string posid)
        {
            RobotPosition ktkPosition = null;
            cmbPosition.Text = posid;
            if (data.ContainsKey(posid))
            {
                ktkPosition = data[posid];
                robotPosControl1.LoadPos(ktkPosition);
            }

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (!Visible)
                return;

        }
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex<0 || dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Tag == null)
                return;
            var posid = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Tag.ToString();
            setData(posid);
        }
        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex < 0 || dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Tag == null)
                return;
            var posid = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Tag.ToString();
            setData(posid);
        }
    }
}