SwInterlock.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using Aitex.Core.RT.IOCore;
  10. namespace Aitex.Core.Backend
  11. {
  12. public partial class SwInterlock : UserControl
  13. {
  14. private List<string> _di = new List<string>();
  15. private List<string> _do = new List<string>();
  16. private bool _loaded = false;
  17. private bool _editable;
  18. Dictionary<string, Dictionary<string, string>> _limit = new Dictionary<string, Dictionary<string, string>>();
  19. private List<Tuple<int, int, string>> _diList;
  20. private List<Tuple<int, int, string>> _doList;
  21. public SwInterlock()
  22. {
  23. InitializeComponent();
  24. _diList = IO.GetIONameList(IOType.DI);
  25. _doList = IO.GetIONameList(IOType.DO);
  26. foreach (var tuple in _diList)
  27. {
  28. _di.Add(string.Format("DI-{0} {1}", tuple.Item1, tuple.Item3));
  29. }
  30. Random _rd = new Random();
  31. for(int i=0; i<_doList.Count; i++)
  32. {
  33. _do.Add(string.Format("DO-{0}", i));
  34. _limit[_do[i]] = new Dictionary<string, string>();
  35. for (int k=0; k<_di.Count; k++)
  36. {
  37. int mod = _rd.Next(100) % 3;
  38. _limit[_do[i]][_di[k]] = mod==0 ? "*" : (mod==1 ? true.ToString() : false.ToString());
  39. }
  40. }
  41. }
  42. /// <summary>
  43. /// 重新读取PmInterlock.cfg文件
  44. /// </summary>
  45. /// <param name="sender"></param>
  46. /// <param name="e"></param>
  47. private void buttonReRead_Click(object sender, EventArgs e)
  48. {
  49. if (MessageBox.Show(string.Format("是否重新读取互锁配置文件?"),
  50. "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  51. {
  52. string reason;
  53. if (IO.ReloadInterlockConfig(out reason))
  54. {
  55. MessageBox.Show("读取互锁文件成功", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
  56. }
  57. else
  58. {
  59. MessageBox.Show("读取互锁文件失败:\n" + reason, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
  60. }
  61. }
  62. }
  63. private void btnSave_Click(object sender, EventArgs e)
  64. {
  65. }
  66. private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
  67. {
  68. var sel = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
  69. if (!checkBox1.Checked)
  70. {
  71. sel.Style.SelectionBackColor = sel.Style.BackColor;
  72. sel.Style.SelectionForeColor = Color.Black;
  73. return;
  74. }
  75. if (e.RowIndex < 0 || e.ColumnIndex < 1)
  76. return;
  77. string value = _limit[_do[e.ColumnIndex-1]][_di[e.RowIndex]];
  78. //MessageBox.Show(value);
  79. if (value.ToLower() == "true")
  80. {
  81. var cell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
  82. cell.Value = "False";
  83. cell.Style.BackColor = Color.Tomato;
  84. cell.Style.SelectionBackColor = Color.Tomato;
  85. cell.Style.SelectionForeColor = Color.Black;
  86. _limit[_do[e.ColumnIndex - 1]][_di[e.RowIndex]] = "False";
  87. }else if (value.ToLower() == "false")
  88. {
  89. var cell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
  90. cell.Value = "*";
  91. cell.Style.BackColor = Color.GhostWhite;
  92. cell.Style.SelectionBackColor = Color.GhostWhite;
  93. cell.Style.SelectionForeColor = Color.Black;
  94. _limit[_do[e.ColumnIndex - 1]][_di[e.RowIndex]] = "*";
  95. }else if (value.ToLower() == "*")
  96. {
  97. var cell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
  98. cell.Value = "True";
  99. cell.Style.BackColor = Color.GreenYellow;
  100. cell.Style.SelectionBackColor = Color.GreenYellow;
  101. cell.Style.SelectionForeColor = Color.Black;
  102. _limit[_do[e.ColumnIndex - 1]][_di[e.RowIndex]] = "True";
  103. }
  104. }
  105. private void dataGridView1_SelectionChanged(object sender, EventArgs e)
  106. {
  107. for (int i=0; i<dataGridView1.RowCount; i++)
  108. {
  109. var cell = dataGridView1.Rows[i].Cells[0];
  110. if (i == dataGridView1.SelectedCells[0].RowIndex)
  111. {
  112. cell.Style.BackColor = Color.Blue;
  113. }
  114. else
  115. {
  116. cell.Style.BackColor = Color.GhostWhite;
  117. }
  118. }
  119. }
  120. private void SwInterlock_Load(object sender, EventArgs e)
  121. {
  122. if (_loaded)
  123. return;
  124. _loaded = true;
  125. this.dataGridView1.RowHeadersVisible = false;
  126. DataGridViewTextBoxColumn first = new DataGridViewTextBoxColumn();
  127. first.ReadOnly = true;
  128. first.Frozen = true;
  129. first.HeaderText = @"\";
  130. first.Width = 120;
  131. first.HeaderText = "*:无关联";//\r\nX:必须False\r\nO:必须True";
  132. first.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
  133. first.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
  134. first.SortMode = DataGridViewColumnSortMode.NotSortable;
  135. first.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
  136. this.dataGridView1.Columns.Add(first);
  137. for (int i = 0; i < _do.Count; i++)
  138. {
  139. DataGridViewButtonColumn col = new DataGridViewButtonColumn();
  140. // 设定列的名字
  141. col.Name = _do[i];
  142. // 在所有按钮上表示"查看详情"
  143. col.UseColumnTextForButtonValue = false;
  144. col.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
  145. col.Text = i.ToString();
  146. col.FlatStyle = FlatStyle.Popup;
  147. col.DefaultCellStyle.ForeColor = Color.Navy;
  148. col.DefaultCellStyle.BackColor = Color.Yellow;
  149. // 设置列标题
  150. col.HeaderText = string.Format("{0}\r\n0->1\r\n关->开\r\nFalse->True", _do[i]);
  151. col.Width = 100;
  152. col.Tag = _limit[_do[i]];
  153. this.dataGridView1.Columns.Add(col);
  154. }
  155. for (int j = 0; j < _di.Count; j++)
  156. {
  157. int rowIndex = this.dataGridView1.Rows.Add(_di[j]);
  158. this.dataGridView1.Rows[rowIndex].Height = 30;
  159. }
  160. for (int i = 0; i < dataGridView1.RowCount; i++)
  161. {
  162. for (int j = 1; j < dataGridView1.Rows[i].Cells.Count; j++)
  163. {
  164. var cell = dataGridView1.Rows[i].Cells[j];
  165. var text = _limit[_do[j - 1]][_di[i]];
  166. cell.Value = text.ToLower()=="true" ? "True" : (text.ToLower()=="false" ? "False" : "*") ;
  167. if (text == "*")
  168. cell.Style.BackColor = Color.GhostWhite;
  169. else if (text.ToLower() == "true")
  170. cell.Style.BackColor = Color.GreenYellow;
  171. else if (text.ToLower() == "false")
  172. cell.Style.BackColor = Color.Tomato;
  173. }
  174. }
  175. }
  176. private void checkBox1_CheckedChanged(object sender, EventArgs e)
  177. {
  178. _editable = checkBox1.Checked;
  179. }
  180. }
  181. }