CMeasureThreadWrapper.cs 18 KB

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