版博士V2.0程序
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using Advantech.Motion;
  2. using Newtonsoft.Json;
  3. using ProductionControl.Device;
  4. using ProductionControl.Utils;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.Data;
  9. using System.Drawing;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Security.Cryptography;
  13. using System.Text;
  14. using System.Threading;
  15. using System.Threading.Tasks;
  16. using System.Windows.Forms;
  17. using System.Xml.Linq;
  18. namespace ProductionControl.UI
  19. {
  20. public partial class UIIFLib : UserControl
  21. {
  22. SynchronizationContext SyncContext = null;
  23. public Action<string> GetParamsEvent;
  24. //
  25. private IFProp prop = new IFProp();
  26. public UIIFLib()
  27. {
  28. InitializeComponent();
  29. propertyGrid1.PropertyValueChanged += propertyGrid1_PropertyValueChanged;
  30. //获取UI线程同步上下文
  31. SyncContext = SynchronizationContext.Current;
  32. init();
  33. }
  34. public void init()
  35. {
  36. this.tbtnSave.Visible = tssSave.Visible = (GetParamsEvent != null);
  37. txtLog.Text = "";
  38. this.propertyGrid1.SelectedObject = prop;
  39. }
  40. public string getParamsData()
  41. {
  42. return prop.serialize();
  43. }
  44. public void setParamsData(string json)
  45. {
  46. if (json == "") return;
  47. prop.deserialize(json);
  48. this.propertyGrid1.Refresh();
  49. }
  50. private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
  51. {
  52. //其中包含了两个重要的属性:OldValue和ChangeItem。
  53. //ChangeItem是“PropertyDescriptorGridEntry”类型。一般可以通过ChangeItem.PropertyDescriptor.ComponentType查找到属性的实际类型。
  54. //而通过ChangeItem的Label,则可以查看到当前在属性编辑输入框中显示的值。
  55. string className = e.ChangedItem.PropertyDescriptor.ComponentType.Name;
  56. string propertyName = e.ChangedItem.PropertyDescriptor.Name;
  57. var oldValue = e.OldValue;
  58. var newValue = e.ChangedItem.Value;
  59. switch (propertyName)
  60. {
  61. case "GotoStepIndex":
  62. if ((int)newValue < 1)
  63. {
  64. prop.GotoStepIndex = 1;
  65. this.refreshUI();
  66. }
  67. break;
  68. case "LimitNum":
  69. if ((int)newValue < 1)
  70. {
  71. prop.LimitNum = 1;
  72. this.refreshUI();
  73. }
  74. break;
  75. }
  76. }
  77. private void refreshUI()
  78. {
  79. SyncContext.Post(m =>
  80. {
  81. var result = m as string;
  82. propertyGrid1.Refresh();
  83. //tbtnJogOnOff.Text = (prop.AxState == AxisState.STA_AX_EXT_JOG) ? "关闭Jog" : "开启Jog";
  84. //tbtnLeft.Enabled = tbtnRight.Enabled = (prop.AxState == AxisState.STA_AX_EXT_JOG);
  85. }, "异步操作完成结果");
  86. }
  87. private void tbtnSave_Click(object sender, EventArgs e)
  88. {
  89. GetParamsEvent?.Invoke(prop.serialize());
  90. }
  91. private void tbtnExport_Click(object sender, EventArgs e)
  92. {
  93. prop.UniqueId = DateTime.Now.Ticks;
  94. string filePath = FileUtil.saveAsFile($"IF配置.json", "JSON|*.json");
  95. if (filePath != "")
  96. {
  97. string jsonText = prop.serialize();
  98. File.WriteAllText(filePath, jsonText);
  99. MessageBox.Show("保存成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
  100. }
  101. }
  102. private void tbtnImport_Click(object sender, EventArgs e)
  103. {
  104. string filePath = FileUtil.selectFile("JSON|*.json");
  105. if (filePath != "")
  106. {
  107. string jsonText = File.ReadAllText(filePath);
  108. prop.deserialize(jsonText);
  109. this.propertyGrid1.Refresh();
  110. //this.propertyGrid1.SelectedObject= prop;
  111. //MessageBox.Show("加载成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
  112. }
  113. }
  114. }
  115. }