CMeasureThreadWrapper.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. using System;
  2. using System.ComponentModel;
  3. using OTSModelSharp;
  4. using OTSDataType;
  5. using System.Collections.Generic;
  6. using static OTSDataType.otsdataconst;
  7. using System.Drawing;
  8. using OTSMeasureApp._1_OTSMeasure.Measure._3_MeasureFlow;
  9. namespace OTSMeasureApp
  10. {
  11. public class CMeasureThreadWrapper
  12. {
  13. //后台线程
  14. public static System.ComponentModel.BackgroundWorker m_bgWorker;
  15. OTSIncAMeasureAppForm m_MeasureApp = null;
  16. OTSMeasureDisplayResult m_OTSMeasureResult = null;
  17. public CMeasure m_measure = null;
  18. NLog.Logger log ;
  19. #region 构造方法
  20. public CMeasureThreadWrapper(OTSIncAMeasureAppForm MeasureApp)
  21. {
  22. log = NLog.LogManager.GetCurrentClassLogger();
  23. //m_ProjData = MeasureApp.m_ProjRstData;
  24. m_MeasureApp = MeasureApp;
  25. m_bgWorker = new BackgroundWorker();
  26. //设置报告更新是否可用
  27. m_bgWorker.WorkerReportsProgress = true;
  28. //设置支持取消操作是否可用
  29. m_bgWorker.WorkerSupportsCancellation = true;
  30. m_bgWorker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);
  31. m_bgWorker.WorkerSupportsCancellation = true;
  32. m_OTSMeasureResult = new OTSMeasureDisplayResult(m_MeasureApp,m_bgWorker);
  33. m_measure = new CMeasure();
  34. m_measure.ProgressEvent += M_MsrThread_ProgressEvent;
  35. m_measure.HolePreviewEvent += M_measure_HolePreviewEvent;
  36. }
  37. private void M_MsrThread_ProgressEvent(ST_MSTMsg MSTMsg)
  38. {
  39. ExecuteMeasureMsg(MSTMsg);
  40. }
  41. private void M_measure_HolePreviewEvent(ST_MSTMsg msg)
  42. {
  43. ExecuteHolePreviewMsg(msg);
  44. }
  45. private void ExecuteHolePreviewMsg(ST_MSTMsg MSTMsg)
  46. {
  47. switch (MSTMsg.iMsgType)
  48. {
  49. case ENUM_MSG_TYPE.MTHREADSTATUS:
  50. if (MSTMsg.STMThreadStu.iMsrStatu == otsdataconst.OTS_MSR_THREAD_STATUS.INPROCESS)
  51. {
  52. m_MeasureApp.SetStartMeasureRibbonStatus();
  53. }
  54. else if (MSTMsg.STMThreadStu.iMsrStatu == otsdataconst.OTS_MSR_THREAD_STATUS.STOPPED)
  55. {
  56. m_MeasureApp.SetStopMeasureRibbonStatus();
  57. }
  58. else if (MSTMsg.STMThreadStu.iMsrStatu == otsdataconst.OTS_MSR_THREAD_STATUS.FAILED)
  59. {
  60. m_MeasureApp.SetStopMeasureRibbonStatus();
  61. }
  62. else if (MSTMsg.STMThreadStu.iMsrStatu == otsdataconst.OTS_MSR_THREAD_STATUS.COMPLETED)
  63. {
  64. m_MeasureApp.SetStopMeasureRibbonStatus();
  65. }
  66. break;
  67. case ENUM_MSG_TYPE.MSAMPLESTATUS:
  68. break;
  69. case ENUM_MSG_TYPE.MSAMPLERESULT:
  70. if (MSTMsg.STMSampleRetData.iRetDataType == MSAMPLE_RET.BSE_DATA)
  71. {
  72. int iHeight = MSTMsg.STMSampleRetData.BSEData.iBSEDataHeight;
  73. int iWidth = MSTMsg.STMSampleRetData.BSEData.iBSEDataWidth;
  74. if (iHeight > 0 && iWidth > 0)
  75. {
  76. //获取显示BSE的图片数据
  77. byte[] ImageData = MSTMsg.STMSampleRetData.BSEData.lpBSEData;
  78. //图片宽度
  79. int width = MSTMsg.STMSampleRetData.BSEData.iBSEDataWidth;
  80. //图片高度
  81. int height = MSTMsg.STMSampleRetData.BSEData.iBSEDataHeight;
  82. //Field位置
  83. Point fieldPos = MSTMsg.STMSampleRetData.BSEData.pos;
  84. int fieldid = MSTMsg.STMSampleRetData.BSEData.fieldId;
  85. //设置样品孔中BSE图像信息
  86. m_MeasureApp.m_SamplepaceWindow.AddHoleBSEImageData(ImageData, width, height, fieldPos, fieldid);
  87. }
  88. else
  89. {
  90. log.Error("(OTSGetMreRetThread) BSEDataHeight = " + iHeight.ToString() + "BSEDataWidth = " + iWidth.ToString() + " Failed");
  91. }
  92. }
  93. break;
  94. }
  95. }
  96. public bool IsHavingNotCompleteSamples()
  97. {
  98. foreach(var s in m_measure.GetListMeasurableSamples())
  99. {
  100. if (s.GetMsrStatus().GetStatus() == OTS_MSR_SAMPLE_STATUS.PAUSED)
  101. {
  102. return true;
  103. }
  104. }
  105. return false;
  106. }
  107. #endregion
  108. #region 启动测量线程
  109. public void StartBGWorkThread()
  110. {
  111. m_bgWorker.RunWorkerAsync();
  112. }
  113. #endregion
  114. #region 线程是否在运行
  115. /// <summary>
  116. /// True: 线程正在运行
  117. /// </summary>
  118. /// <returns></returns>
  119. public bool BGWorkThreadIsRunning()
  120. {
  121. if (m_measure.GetMsrThreadStatusobj().GetStatus() == OTS_MSR_THREAD_STATUS.INPROCESS)
  122. {
  123. return true;
  124. }
  125. return false;
  126. }
  127. public bool BgWorkIsPaused()
  128. {
  129. if (m_measure.GetMsrThreadStatusobj().GetStatus() == OTS_MSR_THREAD_STATUS.PAUSED)
  130. {
  131. return true;
  132. }
  133. return false;
  134. }
  135. #endregion
  136. #region 线程挂起
  137. /// <summary>
  138. /// 线程挂起
  139. /// </summary>
  140. /// <param name="sender"></param>
  141. /// <param name="e"></param>
  142. public void SetWorkThreadPending()
  143. {
  144. m_bgWorker.CancelAsync();
  145. m_bgWorker = null;
  146. }
  147. #endregion
  148. #region 后台进程开始工作_DoWork
  149. /// <summary>
  150. /// 后台进程开始工作 /// </summary>
  151. /// <param name="sender"></param>
  152. /// <param name="e"></param>
  153. private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
  154. {
  155. InitMeasureThread(m_MeasureApp.m_ProjParam);
  156. m_measure.SetMsrLoopStatus(otsdataconst.OTS_MSR_THREAD_STATUS.INPROCESS);
  157. m_measure.DoMeasure();
  158. }
  159. public void InitMeasureThread(COTSMeasureParam a_ProjData)
  160. {
  161. m_measure.Init(a_ProjData);
  162. }
  163. private void ExecuteMeasureMsg(ST_MSTMsg MSTMsg)
  164. {
  165. ShowCurrentFieldPointMessage(MSTMsg.STMSampleRetData.SMsrField.FieldPos);
  166. switch (MSTMsg.iMsgType)
  167. {
  168. case ENUM_MSG_TYPE.MTHREADSTATUS:
  169. if (MSTMsg.STMThreadStu.iMsrStatu == otsdataconst.OTS_MSR_THREAD_STATUS.INPROCESS)
  170. {
  171. m_MeasureApp.SetStartMeasureRibbonStatus();
  172. }
  173. else if (MSTMsg.STMThreadStu.iMsrStatu == otsdataconst.OTS_MSR_THREAD_STATUS.STOPPED)
  174. {
  175. m_MeasureApp.SetStopMeasureRibbonStatus();
  176. GetMsrRetData(ref MSTMsg.STMThreadStu.SMsrCompleteData);
  177. m_OTSMeasureResult.MeasureStopped(MSTMsg);
  178. }
  179. else if (MSTMsg.STMThreadStu.iMsrStatu == otsdataconst.OTS_MSR_THREAD_STATUS.FAILED)
  180. {
  181. m_MeasureApp.SetStopMeasureRibbonStatus();
  182. GetMsrRetData(ref MSTMsg.STMThreadStu.SMsrCompleteData);
  183. m_OTSMeasureResult.MeasureFailed(MSTMsg);
  184. log.Error("(OTSGetMreRetThread) MSTMsg.STMThreadStu.iMThreadStatus = RET_MEASURE_THREAD_STATUS.FAILED");
  185. }
  186. else if (MSTMsg.STMThreadStu.iMsrStatu == otsdataconst.OTS_MSR_THREAD_STATUS.COMPLETED)
  187. {
  188. m_MeasureApp.SetStopMeasureRibbonStatus();
  189. GetMsrRetData(ref MSTMsg.STMThreadStu.SMsrCompleteData);
  190. m_OTSMeasureResult.MeasureComplete(MSTMsg);
  191. }
  192. else if (MSTMsg.STMThreadStu.iMsrStatu == otsdataconst.OTS_MSR_THREAD_STATUS.PAUSED)
  193. {
  194. m_MeasureApp.SetPauseMeasureRibbonStatus();
  195. }
  196. else
  197. {
  198. log.Error("(OTSGetMreRetThread) MSTMsg.STMThreadStu.iMThreadStatus =" + MSTMsg.STMThreadStu.iMsrStatu.ToString() + "Failed");
  199. }
  200. break;
  201. case ENUM_MSG_TYPE.MSAMPLESTATUS:
  202. if (MSTMsg.STMSampleStu.iMsrSampleStatu == OTSDataType.OTS_MSR_SAMPLE_STATUS.INPROCESS) // 开始测量样品
  203. {
  204. string csName = MSTMsg.STMSampleStu.cSampleName;
  205. m_MeasureApp.SwitchDisplayMeasureSample(csName);
  206. StartMsrSample(MSTMsg);
  207. }
  208. else if (MSTMsg.STMSampleStu.iMsrSampleStatu == OTSDataType.OTS_MSR_SAMPLE_STATUS.STOPPED)
  209. {
  210. StopMeasure(MSTMsg);
  211. }
  212. else if (MSTMsg.STMSampleStu.iMsrSampleStatu == OTSDataType.OTS_MSR_SAMPLE_STATUS.FAILED)
  213. {
  214. log.Error("(OTSGetMreRetThread) MSTMsg.STMSampleStu.iMeasureSampleStatus = RET_MEASURE_SAMPLE_STATUS.FAILED Failed");
  215. }
  216. else if (MSTMsg.STMSampleStu.iMsrSampleStatu == OTSDataType.OTS_MSR_SAMPLE_STATUS.SUCCESSED)
  217. {
  218. log.Info("Sample measure succeeded");
  219. }
  220. else if(MSTMsg.STMSampleStu.iMsrSampleStatu == OTSDataType.OTS_MSR_SAMPLE_STATUS.PAUSED)
  221. {
  222. m_MeasureApp.SetPauseMeasureRibbonStatus();
  223. log.Warn("This sample measure is paused!");
  224. }
  225. break;
  226. case ENUM_MSG_TYPE.MSAMPLERESULT:
  227. if (MSTMsg.STMSampleRetData.iRetDataType ==MSAMPLE_RET.BSE_DATA)
  228. {
  229. int iHeight = MSTMsg.STMSampleRetData.BSEData.iBSEDataHeight;
  230. int iWidth = MSTMsg.STMSampleRetData.BSEData.iBSEDataWidth;
  231. if (iHeight > 0 && iWidth > 0)
  232. {
  233. byte[] bsedata = MSTMsg.STMSampleRetData.BSEData.lpBSEData;
  234. m_OTSMeasureResult.Dis_Field_BSE_DATA(bsedata,iWidth,iHeight);
  235. }
  236. else
  237. {
  238. log.Error("(OTSGetMreRetThread) BSEDataHeight = " + iHeight.ToString() + "BSEDataWidth = " + iWidth.ToString() + " Failed");
  239. }
  240. }else
  241. if (MSTMsg.STMSampleRetData.iRetDataType == MSAMPLE_RET.REMOVEBG_DATA)
  242. {
  243. int iHeight = MSTMsg.STMSampleRetData.BSEData.iBSEDataHeight;
  244. int iWidth = MSTMsg.STMSampleRetData.BSEData.iBSEDataWidth;
  245. if (iHeight > 0 && iWidth > 0)
  246. {
  247. Bitmap bsedata = MSTMsg.STMSampleRetData.BSEData.coloredImg;
  248. m_OTSMeasureResult.Dis_Field_BSE_REMOVEBG_DATA(bsedata);
  249. }
  250. else
  251. {
  252. log.Error("(OTSGetMreRetThread) BSEDataHeight = " + iHeight.ToString() + "BSEDataWidth = " + iWidth.ToString() + " Failed");
  253. }
  254. }
  255. else if (MSTMsg.STMSampleRetData.iRetDataType == MSAMPLE_RET.FIELD_DATA)
  256. {
  257. this.GetSMsrUsedTime(ref MSTMsg.STMSampleRetData.SFieldData.TUsedTime);
  258. m_OTSMeasureResult.Field_DATA(MSTMsg);
  259. }
  260. else if (MSTMsg.STMSampleRetData.iRetDataType == MSAMPLE_RET.START_MSR_FIELD)
  261. {
  262. m_OTSMeasureResult.Field_START_MSR(MSTMsg);
  263. }
  264. else
  265. {
  266. log.Error("(OTSGetMreRetThread) MSTMsg.STMSampleRetData.iRetDataType =" + MSTMsg.STMSampleRetData.iRetDataType.ToString() + "Failed");
  267. }
  268. break;
  269. }
  270. }
  271. #region privte part
  272. private void ShowCurrentFieldPointMessage(Point currPoint)
  273. {
  274. //当前测量帧图视场坐标---------------------------------------------------------------------------------------
  275. Point lsp = currPoint;
  276. if (lsp.X != 0 || lsp.Y != 0)
  277. {
  278. string STSemCoordinate = "Current frame coordinates X:" + lsp.X.ToString() + " Y:" + lsp.Y.ToString();
  279. //显示当前视场 XY轴
  280. m_MeasureApp.ShowSemCoordvAL(STSemCoordinate);
  281. }
  282. //------------------------------------------------------------------------------------------------------------
  283. }
  284. //处理测量线程停止消息
  285. private void StopMeasure(ST_MSTMsg SMsrData)
  286. {
  287. m_MeasureApp.SetStopMeasureRibbonStatus();
  288. GetMsrRetData(ref SMsrData.STMThreadStu.SMsrCompleteData);
  289. m_OTSMeasureResult.MeasureStopped(SMsrData);
  290. }
  291. private void StartMsrSample(ST_MSTMsg SMsrData)
  292. {
  293. DateTime SampleMsrStartTime = new DateTime();
  294. m_MeasureApp.m_ProjParam.GetMsrSampleStartTime(ref SampleMsrStartTime);
  295. SMsrData.STMSampleStu.csSampleMsrStartTime = SampleMsrStartTime.ToString("yyyy-MM-dd HH:mm:ss");
  296. string csSName = "";
  297. List<PointF> FieldList = new List<PointF>();
  298. m_MeasureApp.m_ProjParam.GetBeforeCompleteField(ref csSName, ref FieldList);
  299. string csSampleName = SMsrData.STMSampleStu.cSampleName;
  300. if (csSName == csSampleName)
  301. {
  302. SMsrData.STMSampleStu.BCompleteFieldList = FieldList;
  303. }
  304. m_OTSMeasureResult.SampleStart(SMsrData);
  305. }
  306. private void GetSMsrUsedTime(ref TimeSpan MsrUsedTime)
  307. {
  308. TimeSpan UsedTime = new TimeSpan();
  309. if (!m_MeasureApp.m_ProjParam.GetMsrSampleUsedTime(ref UsedTime))
  310. {
  311. return;
  312. }
  313. MsrUsedTime = UsedTime;
  314. }
  315. #endregion
  316. //停止测量
  317. public void SetMeasureThreadStatus(otsdataconst.OTS_MSR_THREAD_STATUS ThreadStatus)
  318. {
  319. //设置测量线程状态
  320. m_measure.SetMsrLoopStatus(ThreadStatus);
  321. }
  322. #endregion
  323. public bool ConnectSEM()
  324. {
  325. return m_measure.GetSEMController().Connect();
  326. }
  327. public void DisconnectSEM()
  328. {
  329. m_measure.GetSEMController().DisConnect();
  330. }
  331. public void StopXrayAcquisition()
  332. {
  333. m_measure.GetSEMController().StopXrayAcquisition();
  334. }
  335. public bool IsSEMConnected()
  336. {
  337. return m_measure.GetSEMController().IsConnected();
  338. }
  339. public bool GetMsrRetData(ref SMSR_COMPLETE_DATA MsrCompleteData)
  340. {
  341. try
  342. {
  343. List<COTSSample> MeasureCompleteSampleList = new List<COTSSample>();
  344. MeasureCompleteSampleList = m_measure.GetListMeasurableSamples();
  345. DateTime DTime = new DateTime();
  346. DTime = (DateTime)m_measure.GetMsrThreadStatusobj().GetStartTime();
  347. MsrCompleteData.csMsrStartTime = DTime.ToString("yyyy-MM-dd HH:mm:ss");
  348. MsrCompleteData.MsrUsedTime = (TimeSpan)m_measure.GetMsrThreadStatusobj().GetUsedTime();
  349. DTime = (DateTime)m_measure.GetMsrThreadStatusobj().GetEndTime();
  350. MsrCompleteData.csMsrEndTime = DTime.ToString("yyyy-MM-dd HH:mm:ss");
  351. MsrCompleteData.iMsrCompleteSampleCount = MeasureCompleteSampleList.Count;
  352. MsrCompleteData.iMsrCompleteFieldCount = 0;
  353. MsrCompleteData.iParticleCount = 0;
  354. for (int i = 0; i < MsrCompleteData.iMsrCompleteSampleCount; i++)
  355. {
  356. MsrCompleteData.iMsrCompleteFieldCount += MeasureCompleteSampleList[i].GetMsrStatus().GetCompletedFields();
  357. List<CMsrResultItem> MsrRetClr = MeasureCompleteSampleList[i].GetMsrResults().GetResultItems();
  358. for (int k = 0; k < MsrRetClr.Count; k++)
  359. {
  360. MsrCompleteData.iParticleCount += (int)MsrRetClr[k].GetNumber();
  361. }
  362. }
  363. return true;
  364. }
  365. catch (Exception ex)
  366. {
  367. log.Error(ex.Message.ToString());
  368. return false;
  369. }
  370. }
  371. }
  372. }