版博士V2.0程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

164 lines
5.4 KiB

  1. using MaiMuAOI.SysCtrl;
  2. using MaiMuControl.Device.SenSorDev;
  3. using Newtonsoft.Json;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.Drawing;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. using System.Windows.Forms;
  15. using System.Xml.Linq;
  16. namespace ProductionControl.UI
  17. {
  18. public partial class UIHeightDev : UserControl
  19. {
  20. SynchronizationContext SyncContext = null;
  21. public Action<string> GetParamsEvent;
  22. //
  23. private HeightDevProp prop = new HeightDevProp(ConfMgr.Instance.SysConfigParams.ThicknessIP,ConfMgr.Instance.SysConfigParams.ThicknessPort);
  24. private ValueSensorDev _dev;
  25. public UIHeightDev(ValueSensorDev sensorDev)
  26. {
  27. InitializeComponent();
  28. _dev = sensorDev;
  29. propertyGrid1.PropertyValueChanged += propertyGrid1_PropertyValueChanged;
  30. refreshUIState();
  31. //获取UI线程同步上下文
  32. SyncContext = SynchronizationContext.Current;
  33. //init();
  34. }
  35. private void refreshUIState()
  36. {
  37. foreach (ToolStripItem item in this.toolStrip1.Items)
  38. {
  39. if (item.Text == "打开设备")
  40. item.Visible = (_dev == null);
  41. else
  42. this.propertyGrid1.Enabled = item.Enabled = !(_dev == null);
  43. }
  44. }
  45. public void init()
  46. {
  47. this.tbtnSave.Visible = tssSave.Visible = (GetParamsEvent != null);
  48. this.propertyGrid1.SelectedObject = prop;
  49. refreshUIState();
  50. }
  51. public string getParamsData()
  52. {
  53. return prop.serialize();
  54. }
  55. public void setParamsData(string json)
  56. {
  57. if (json == "") return;
  58. prop.deserialize(json);
  59. this.propertyGrid1.Refresh();
  60. }
  61. private void refreshUI()
  62. {
  63. SyncContext.Post(m =>
  64. {
  65. var result = m as string;
  66. propertyGrid1.Refresh();
  67. //tbtnJogOnOff.Text = (prop.AxState == AxisState.STA_AX_EXT_JOG) ? "关闭Jog" : "开启Jog";
  68. //tbtnLeft.Enabled = tbtnRight.Enabled = (prop.AxState == AxisState.STA_AX_EXT_JOG);
  69. }, "异步操作完成结果");
  70. }
  71. protected override void OnHandleDestroyed(EventArgs e)
  72. {
  73. base.OnHandleDestroyed(e);
  74. // 在此添加需要手动释放资源的代码
  75. }
  76. private void tbtnRun_Click(object sender, EventArgs e)
  77. {
  78. try
  79. {
  80. //var res=dev.setDigitalValue(prop.ChannelIndex, prop.DigitalValue);
  81. //if (!res)
  82. // throw new Exception("设置失败!");
  83. }
  84. catch(Exception ex)
  85. {
  86. MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  87. }
  88. }
  89. private void tbtnExport_Click(object sender, EventArgs e)
  90. {
  91. string filePath = ConfMgr.SaveAsFile($"Light配置.json", "JSON|*.json");
  92. if (filePath != "")
  93. {
  94. string jsonText = prop.serialize();
  95. File.WriteAllText(filePath, jsonText);
  96. MessageBox.Show("保存成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
  97. }
  98. }
  99. private void tbtnImport_Click(object sender, EventArgs e)
  100. {
  101. string filePath = ConfMgr.SelectFile("JSON|*.json");
  102. if (filePath != "")
  103. {
  104. string jsonText = File.ReadAllText(filePath);
  105. prop.deserialize(jsonText);
  106. this.propertyGrid1.Refresh();
  107. //this.propertyGrid1.SelectedObject= prop;
  108. //MessageBox.Show("加载成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
  109. }
  110. }
  111. private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
  112. {
  113. //其中包含了两个重要的属性:OldValue和ChangeItem。
  114. //ChangeItem是“PropertyDescriptorGridEntry”类型。一般可以通过ChangeItem.PropertyDescriptor.ComponentType查找到属性的实际类型。
  115. //而通过ChangeItem的Label,则可以查看到当前在属性编辑输入框中显示的值。
  116. string className = e.ChangedItem.PropertyDescriptor.ComponentType.Name;
  117. string propertyName = e.ChangedItem.PropertyDescriptor.Name;
  118. var oldValue = e.OldValue;
  119. var newValue = e.ChangedItem.Value;
  120. switch (propertyName)
  121. {
  122. //case "ChannelIndex":
  123. // if((int)newValue <1 || (int)newValue >4)
  124. // {
  125. // prop.ChannelIndex = (int)oldValue;
  126. // this.refreshUI();
  127. // }
  128. // else
  129. // {
  130. // dev.getDigitalValue((int)newValue);
  131. // }
  132. // break;
  133. }
  134. }
  135. private void tbtnSave_Click(object sender, EventArgs e)
  136. {
  137. GetParamsEvent?.Invoke(prop.serialize());
  138. }
  139. private void tsbtnOpenDev_Click(object sender, EventArgs e)
  140. {
  141. this.init();
  142. }
  143. }
  144. }