版博士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.
 
 
 
 

253 lines
8.2 KiB

  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.Text;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. using System.Windows.Forms;
  16. using System.Xml.Linq;
  17. using static ProductionControl.Device.SizeLib;
  18. namespace ProductionControl.UI
  19. {
  20. public partial class UISizeLib : UserControl
  21. {
  22. SynchronizationContext SyncContext = null;
  23. public Action<string> GetParamsEvent;
  24. //
  25. private SizeLibProp prop = new SizeLibProp();
  26. private SizeLib dev;
  27. public UISizeLib()
  28. {
  29. InitializeComponent();
  30. propertyGrid1.PropertyValueChanged += propertyGrid1_PropertyValueChanged;
  31. refreshUIState();
  32. //获取UI线程同步上下文
  33. SyncContext = SynchronizationContext.Current;
  34. //init();
  35. }
  36. private string szLog;
  37. private void refreshUIState()
  38. {
  39. foreach (ToolStripItem item in this.toolStrip1.Items)
  40. {
  41. if (item.Text == "打开设备")
  42. item.Visible = (dev == null || !dev.IsInit);
  43. else
  44. this.propertyGrid1.Enabled = item.Enabled = !(dev == null || !dev.IsInit);
  45. }
  46. }
  47. public void init()
  48. {
  49. this.tbtnSave.Visible = tssSave.Visible = (GetParamsEvent != null);
  50. this.propertyGrid1.SelectedObject = prop;
  51. dev = new SizeLib();
  52. dev.WarningEvent = (level, info) =>
  53. {
  54. szLog = $"({level}){info}";
  55. txtLog.Text = $"({level}){info}";
  56. };
  57. //DATA
  58. dev.finishEvent += new System.Action<SizeTask>((task) =>
  59. {
  60. prop.PT1 = task.PT1;
  61. prop.PT2 = task.PT2;
  62. prop.Shanxian = task.Shanxian;
  63. //prop.RowP= task.RowP;
  64. prop.Circle_Ymm = task.Circle_Ymm;
  65. prop.Circle_Xmm = task.Circle_Xmm;
  66. prop.offsetX = task.offsetX;
  67. prop.offsetY = task.offsetY;
  68. prop.MarkPointList = task.MarkPointList;
  69. prop.resultInfo = task.isSucceed ? "成功" : task.resultInfo;
  70. this.refreshUI();
  71. });
  72. if (!dev.start(Config.SizeEnginePath))
  73. {
  74. this.closeDev();
  75. return;
  76. }
  77. refreshUIState();
  78. }
  79. public string getParamsData()
  80. {
  81. return prop.serialize();
  82. }
  83. public void setParamsData(string json)
  84. {
  85. if (json == "") return;
  86. prop.deserialize(json);
  87. this.propertyGrid1.Refresh();
  88. }
  89. private void refreshUI()
  90. {
  91. SyncContext.Post(m =>
  92. {
  93. var result = m as string;
  94. propertyGrid1.Refresh();
  95. //tbtnJogOnOff.Text = (prop.AxState == AxisState.STA_AX_EXT_JOG) ? "关闭Jog" : "开启Jog";
  96. //tbtnLeft.Enabled = tbtnRight.Enabled = (prop.AxState == AxisState.STA_AX_EXT_JOG);
  97. }, "异步操作完成结果");
  98. }
  99. private void closeDev()
  100. {
  101. try
  102. {
  103. if (dev == null)
  104. return;
  105. dev.stop();
  106. }
  107. catch (Exception ex)
  108. {
  109. refreshUIState();
  110. MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  111. }
  112. }
  113. protected override void OnHandleDestroyed(EventArgs e)
  114. {
  115. base.OnHandleDestroyed(e);
  116. // 在此添加需要手动释放资源的代码
  117. this.closeDev();
  118. }
  119. private void tbtnRun_Click(object sender, EventArgs e)
  120. {
  121. try
  122. {
  123. prop.resultInfo = "";
  124. dev.add(new SizeTask()
  125. {
  126. engineName = prop.EngineName,
  127. file_path = prop.BmpPath,
  128. //bmp =read2Bmp(prop.BmpPath),
  129. index=prop.Index,
  130. //finishEvent = (res) =>
  131. //{
  132. //}
  133. });
  134. }
  135. catch(Exception ex)
  136. {
  137. MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  138. }
  139. }
  140. private void tbtnExport_Click(object sender, EventArgs e)
  141. {
  142. string filePath = FileUtil.saveAsFile($"SizeLib Config.json", "JSON|*.json");
  143. if (filePath != "")
  144. {
  145. string jsonText = prop.serialize();
  146. File.WriteAllText(filePath, jsonText);
  147. MessageBox.Show("保存成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
  148. }
  149. }
  150. private void tbtnImport_Click(object sender, EventArgs e)
  151. {
  152. string filePath = FileUtil.selectFile("JSON|*.json");
  153. if (filePath != "")
  154. {
  155. string jsonText = File.ReadAllText(filePath);
  156. prop.deserialize(jsonText);
  157. this.propertyGrid1.Refresh();
  158. //this.propertyGrid1.SelectedObject= prop;
  159. //MessageBox.Show("加载成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
  160. }
  161. }
  162. private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
  163. {
  164. //其中包含了两个重要的属性:OldValue和ChangeItem。
  165. //ChangeItem是“PropertyDescriptorGridEntry”类型。一般可以通过ChangeItem.PropertyDescriptor.ComponentType查找到属性的实际类型。
  166. //而通过ChangeItem的Label,则可以查看到当前在属性编辑输入框中显示的值。
  167. string className = e.ChangedItem.PropertyDescriptor.ComponentType.Name;
  168. string propertyName = e.ChangedItem.PropertyDescriptor.Name;
  169. var oldValue = e.OldValue;
  170. var newValue = e.ChangedItem.Value;
  171. switch (propertyName)
  172. {
  173. //case "BmpPath":
  174. // if(newValue)
  175. // {
  176. // prop.BmpPath = (int)oldValue;
  177. // this.refreshUI();
  178. // }
  179. // else
  180. // {
  181. // dev.getDigitalValue((int)newValue);
  182. // }
  183. // break;
  184. //case "DigitalValue":
  185. // if ((int)newValue < 0 || (int)newValue > 100)
  186. // {
  187. // prop.DigitalValue = (int)oldValue;
  188. // this.refreshUI();
  189. // }
  190. // break;
  191. }
  192. }
  193. private void tbtnSave_Click(object sender, EventArgs e)
  194. {
  195. GetParamsEvent?.Invoke(prop.serialize());
  196. }
  197. private void tsbtnOpenDev_Click(object sender, EventArgs e)
  198. {
  199. this.init();
  200. }
  201. private void tbtnDebug_Click(object sender, EventArgs e)
  202. {
  203. if (dev.IsInit)
  204. {
  205. if (!dev.setDebug)
  206. {
  207. dev.setDebug=true;
  208. }
  209. else
  210. {
  211. dev.setDebug = false ;
  212. }
  213. tbtnDebug.Text = dev.setDebug?"关闭调试" : "打开调试";
  214. }
  215. }
  216. private Bitmap read2Bmp(string path)
  217. {
  218. FileStream fs = new FileStream(path, FileMode.Open);
  219. byte[] bmpdata = new byte[fs.Length];
  220. fs.Read(bmpdata, 0, bmpdata.Length);
  221. Bitmap bmp = new Bitmap(fs);
  222. fs.Close();
  223. return bmp;
  224. }
  225. //2023- 10-27 新增图纸选点
  226. private void tbtnGetPos_Click(object sender, EventArgs e)
  227. {
  228. //FrmGetPosByPic frm = new FrmGetPosByPic(prop);
  229. //frm.ShowDialog();
  230. }
  231. }
  232. }