123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Drawing;
- using System.Data;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using Aitex.Core.RT.IOCore;
- namespace Aitex.Core.Backend
- {
- public partial class SwInterlock : UserControl
- {
- private List<string> _di = new List<string>();
- private List<string> _do = new List<string>();
- private bool _loaded = false;
- private bool _editable;
- Dictionary<string, Dictionary<string, string>> _limit = new Dictionary<string, Dictionary<string, string>>();
-
- private List<Tuple<int, int, string>> _diList;
- private List<Tuple<int, int, string>> _doList;
- public SwInterlock()
- {
- InitializeComponent();
- _diList = IO.GetIONameList(IOType.DI);
- _doList = IO.GetIONameList(IOType.DO);
- foreach (var tuple in _diList)
- {
- _di.Add(string.Format("DI-{0} {1}", tuple.Item1, tuple.Item3));
- }
-
- Random _rd = new Random();
- for(int i=0; i<_doList.Count; i++)
- {
- _do.Add(string.Format("DO-{0}", i));
- _limit[_do[i]] = new Dictionary<string, string>();
- for (int k=0; k<_di.Count; k++)
- {
- int mod = _rd.Next(100) % 3;
- _limit[_do[i]][_di[k]] = mod==0 ? "*" : (mod==1 ? true.ToString() : false.ToString());
- }
- }
- }
- /// <summary>
- /// 重新读取PmInterlock.cfg文件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void buttonReRead_Click(object sender, EventArgs e)
- {
- if (MessageBox.Show(string.Format("是否重新读取互锁配置文件?"),
- "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
- {
- string reason;
- if (IO.ReloadInterlockConfig(out reason))
- {
- MessageBox.Show("读取互锁文件成功", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- else
- {
- MessageBox.Show("读取互锁文件失败:\n" + reason, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- }
-
- private void btnSave_Click(object sender, EventArgs e)
- {
- }
- private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
- {
- var sel = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
- if (!checkBox1.Checked)
- {
- sel.Style.SelectionBackColor = sel.Style.BackColor;
- sel.Style.SelectionForeColor = Color.Black;
- return;
- }
- if (e.RowIndex < 0 || e.ColumnIndex < 1)
- return;
- string value = _limit[_do[e.ColumnIndex-1]][_di[e.RowIndex]];
- //MessageBox.Show(value);
- if (value.ToLower() == "true")
- {
- var cell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
- cell.Value = "False";
-
- cell.Style.BackColor = Color.Tomato;
- cell.Style.SelectionBackColor = Color.Tomato;
- cell.Style.SelectionForeColor = Color.Black;
- _limit[_do[e.ColumnIndex - 1]][_di[e.RowIndex]] = "False";
- }else if (value.ToLower() == "false")
- {
- var cell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
- cell.Value = "*";
-
- cell.Style.BackColor = Color.GhostWhite;
- cell.Style.SelectionBackColor = Color.GhostWhite;
- cell.Style.SelectionForeColor = Color.Black;
- _limit[_do[e.ColumnIndex - 1]][_di[e.RowIndex]] = "*";
- }else if (value.ToLower() == "*")
- {
- var cell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
- cell.Value = "True";
-
- cell.Style.BackColor = Color.GreenYellow;
-
- cell.Style.SelectionBackColor = Color.GreenYellow;
- cell.Style.SelectionForeColor = Color.Black;
- _limit[_do[e.ColumnIndex - 1]][_di[e.RowIndex]] = "True";
- }
- }
- private void dataGridView1_SelectionChanged(object sender, EventArgs e)
- {
- for (int i=0; i<dataGridView1.RowCount; i++)
- {
- var cell = dataGridView1.Rows[i].Cells[0];
- if (i == dataGridView1.SelectedCells[0].RowIndex)
- {
- cell.Style.BackColor = Color.Blue;
- }
- else
- {
- cell.Style.BackColor = Color.GhostWhite;
- }
- }
- }
- private void SwInterlock_Load(object sender, EventArgs e)
- {
- if (_loaded)
- return;
- _loaded = true;
- this.dataGridView1.RowHeadersVisible = false;
-
-
- DataGridViewTextBoxColumn first = new DataGridViewTextBoxColumn();
- first.ReadOnly = true;
- first.Frozen = true;
- first.HeaderText = @"\";
- first.Width = 120;
- first.HeaderText = "*:无关联";//\r\nX:必须False\r\nO:必须True";
- first.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
- first.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
- first.SortMode = DataGridViewColumnSortMode.NotSortable;
- first.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
- this.dataGridView1.Columns.Add(first);
- for (int i = 0; i < _do.Count; i++)
- {
- DataGridViewButtonColumn col = new DataGridViewButtonColumn();
- // 设定列的名字
- col.Name = _do[i];
- // 在所有按钮上表示"查看详情"
- col.UseColumnTextForButtonValue = false;
- col.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
- col.Text = i.ToString();
- col.FlatStyle = FlatStyle.Popup;
- col.DefaultCellStyle.ForeColor = Color.Navy;
- col.DefaultCellStyle.BackColor = Color.Yellow;
- // 设置列标题
- col.HeaderText = string.Format("{0}\r\n0->1\r\n关->开\r\nFalse->True", _do[i]);
- col.Width = 100;
- col.Tag = _limit[_do[i]];
- this.dataGridView1.Columns.Add(col);
- }
- for (int j = 0; j < _di.Count; j++)
- {
- int rowIndex = this.dataGridView1.Rows.Add(_di[j]);
- this.dataGridView1.Rows[rowIndex].Height = 30;
-
- }
- for (int i = 0; i < dataGridView1.RowCount; i++)
- {
- for (int j = 1; j < dataGridView1.Rows[i].Cells.Count; j++)
- {
- var cell = dataGridView1.Rows[i].Cells[j];
- var text = _limit[_do[j - 1]][_di[i]];
- cell.Value = text.ToLower()=="true" ? "True" : (text.ToLower()=="false" ? "False" : "*") ;
- if (text == "*")
- cell.Style.BackColor = Color.GhostWhite;
- else if (text.ToLower() == "true")
- cell.Style.BackColor = Color.GreenYellow;
- else if (text.ToLower() == "false")
- cell.Style.BackColor = Color.Tomato;
-
- }
- }
- }
- private void checkBox1_CheckedChanged(object sender, EventArgs e)
- {
- _editable = checkBox1.Checked;
- }
- }
- }
|