CSmplPreviewMeasure.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. 
  2. using OTSCLRINTERFACE;
  3. using OTSDataType;
  4. using OTSModelSharp;
  5. using OTSModelSharp.ServiceCenter;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Drawing;
  9. using System.Threading;
  10. using static OTSDataType.otsdataconst;
  11. using CHoleBSEImgsList = System.Collections.Generic.List<OTSDataType.CHoleBSEImg>;
  12. namespace OTSMeasureApp._1_OTSMeasure.Measure._3_MeasureFlow
  13. {
  14. internal class CSmplPreviewMeasure
  15. {
  16. protected static NLog.Logger log;
  17. protected CMeasure m_pMsrThread;
  18. CHoleBSEImgsList m_listHoleBSEImg;
  19. protected ISemController m_SemHardwareMgr;
  20. protected IScanController m_ScanHardwareMgr;
  21. private List<Point> m_listFieldCenter;
  22. public CSmplPreviewMeasure(CMeasure a_msrThread, List<Point> listFieldCenter)
  23. {
  24. m_pMsrThread = a_msrThread;
  25. log = NLog.LogManager.GetCurrentClassLogger();
  26. var imgwidth =a_msrThread.m_pMeasureParam.GetDefaultParam().GetImageScanParam().GetImageResolutionSize().cx;
  27. var imgheight = a_msrThread.m_pMeasureParam.GetDefaultParam().GetImageScanParam().GetImageResolutionSize().cy;
  28. m_SemHardwareMgr = SemController.GetSEMController();
  29. m_ScanHardwareMgr = ScanController.GetScanController();
  30. m_listFieldCenter = listFieldCenter;
  31. m_listHoleBSEImg = new CHoleBSEImgsList();
  32. }
  33. private class SEMStateObject
  34. {
  35. private PointF pos;
  36. private double workingDistance;
  37. private double magnification;
  38. public PointF Pos { get => pos; set => pos = value; }
  39. public double WorkingDistance { get => workingDistance; set => workingDistance = value; }
  40. public double Magnification { get => magnification; set => magnification = value; }
  41. }
  42. private class AutoResetSEMControl : IDisposable
  43. {
  44. CSmplPreviewMeasure sem;
  45. private SEMStateObject semState = null;
  46. public AutoResetSEMControl(SEMStateObject s, CSmplPreviewMeasure a_sem)
  47. {
  48. semState = s;
  49. sem=a_sem;
  50. }
  51. public SEMStateObject SemState
  52. {
  53. get => semState;
  54. set => semState = value;
  55. }
  56. public void Dispose()
  57. {
  58. if (semState != null)
  59. {
  60. sem.m_SemHardwareMgr.SetMagnification(semState.Magnification);
  61. Thread.Sleep(100);
  62. sem.MoveSEMToPoint(semState.Pos);
  63. Thread.Sleep(100);
  64. sem.m_SemHardwareMgr.SetWorkingDistance(semState.WorkingDistance);
  65. }
  66. log.Warn("Set SEM Exteral Off!");
  67. sem.m_SemHardwareMgr.SetScanExternal(false);
  68. }
  69. }
  70. bool MoveSEMToPoint(System.Drawing.PointF a_poi)
  71. {
  72. // get SEM controller
  73. var pSEMController = m_SemHardwareMgr;
  74. PointF a_SEMpt = new Point();
  75. CSEMStageData a_pCSEMStageData = m_pMsrThread.m_pMeasureParam.GetDefaultParam().GetStageDataParam();
  76. int hardWareDelay = a_pCSEMStageData.GetHardWareDelay();
  77. if (!a_pCSEMStageData.ConvertOTSToSEMCoord(a_poi, ref a_SEMpt))
  78. {
  79. return false;
  80. }
  81. log.Info("Begin to move SEM stage to OTScoord:" + a_poi.X + "," + a_poi.Y);
  82. log.Info("Begin to move SEM stage to " + a_SEMpt.X + "," + a_SEMpt.Y);
  83. // move SEM to the position (rotation 0)
  84. if (!pSEMController.MoveSEMToPoint(a_SEMpt.X, a_SEMpt.Y))
  85. {
  86. log.Error("MoveSEMToPoint: failed to call MoveSEMToPoint method.");
  87. return false;
  88. }
  89. if (hardWareDelay > 0)
  90. {
  91. Thread.Sleep(hardWareDelay);
  92. }
  93. return true;
  94. }
  95. void HolePreviewThreadOver()
  96. {
  97. ST_MSTMsg MsrMsg = new ST_MSTMsg(m_pMsrThread.GetMsrThreadStatusobj());
  98. MsrMsg.InitThreadOverMsg();
  99. m_pMsrThread. SendHolePreviewMessageToMeasureGUI(MsrMsg);
  100. }
  101. public void DoHolePreview()
  102. {
  103. m_pMsrThread.GetMsrThreadStatusobj().SetStatus(OTS_MSR_THREAD_STATUS.INPROCESS);
  104. ST_MSTMsg MsgMsrStart = new ST_MSTMsg(m_pMsrThread.GetMsrThreadStatusobj());
  105. MsgMsrStart.InitHolePreThreadInProcessMsg();
  106. m_pMsrThread.SendHolePreviewMessageToMeasureGUI(MsgMsrStart);
  107. // connect hardware
  108. if (!m_SemHardwareMgr.Connect())
  109. {
  110. // failed to connect hardware
  111. m_pMsrThread.SetMsrLoopStatus(otsdataconst.OTS_MSR_THREAD_STATUS.FAILED);
  112. HolePreviewThreadOver();
  113. }
  114. ShotPreviewImage();
  115. // check if measurement is successful
  116. if (m_pMsrThread.GetMsrThreadStatusobj().GetStatus() == OTS_MSR_THREAD_STATUS.STOPPED)
  117. {
  118. // record end time
  119. m_pMsrThread.GetMsrThreadStatusobj().ComputeTime(OTS_THREAD_TIME_TYPE.STOPPED);
  120. // measurement stopped
  121. m_pMsrThread. SetMsrLoopStatus(otsdataconst.OTS_MSR_THREAD_STATUS.STOPPED);
  122. // update thread measure status class, let the main thread know that this sample measurement stopped
  123. ST_MSTMsg MsgSmpStop = new ST_MSTMsg(m_pMsrThread.GetMsrThreadStatusobj());
  124. MsgSmpStop.InitHolePreThreadStoppedMsg();
  125. m_pMsrThread.SendHolePreviewMessageToMeasureGUI(MsgSmpStop);
  126. HolePreviewThreadOver();
  127. return;
  128. }
  129. // update thread measure status class, let the main thread know that this sample measurement successes
  130. ST_MSTMsg MsgSmpSuccess = new ST_MSTMsg(m_pMsrThread.GetMsrThreadStatusobj());
  131. MsgSmpSuccess.InitHolePreThreadSucceedMsg();
  132. m_pMsrThread. SendHolePreviewMessageToMeasureGUI(MsgSmpSuccess);
  133. // measurement completed
  134. m_pMsrThread. SetMsrLoopStatus(otsdataconst.OTS_MSR_THREAD_STATUS.COMPLETED);
  135. HolePreviewThreadOver();
  136. }
  137. public void ShotPreviewImage()
  138. {
  139. SEMStateObject originalStateObj= new SEMStateObject();
  140. //----------memorize the state of sem
  141. double posX = 0, posY = 0, posR = 0;
  142. m_SemHardwareMgr.GetSemPositionXY(ref posX, ref posY, ref posR);
  143. Point pos = new Point((int)posX, (int)posY);
  144. PointF otsPos = new Point(0, 0);
  145. CSEMStageData a_pCSEMStageData = m_pMsrThread.m_pMeasureParam.GetDefaultParam().GetStageDataParam();
  146. if (!a_pCSEMStageData.ConvertSEMToOTSCoord(pos, ref otsPos))
  147. {
  148. return;
  149. }
  150. originalStateObj.Pos = otsPos;
  151. double originalWd = 0;
  152. double originalMag = 0;
  153. m_SemHardwareMgr.GetWorkingDistance(ref originalWd);
  154. m_SemHardwareMgr.GetMagnification(ref originalMag );
  155. originalStateObj.WorkingDistance = originalWd;
  156. originalStateObj.Magnification = Math.Max(originalMag, 1.0); // ensure magnification is not zero
  157. using (AutoResetSEMControl autoReset = new AutoResetSEMControl(originalStateObj,this))
  158. {
  159. var pSEMCtrl = m_pMsrThread.GetSEMController();
  160. double dMinMag = a_pCSEMStageData.GetMinMag();
  161. // get SEM controller to set magnification and working distance
  162. if (!pSEMCtrl.SetMagnification(dMinMag))
  163. {
  164. log.Error("DoHolePreview: fail to set magnification.");
  165. }
  166. // go through each field
  167. for (int i = 0; i < m_listFieldCenter.Count; ++i)
  168. {// check and break if stop button is clicked
  169. if (m_pMsrThread.IsMeasureStopped())
  170. {// measure stopped
  171. log.Trace("DoHolePreview: measure thread is stopped.");
  172. return;
  173. }
  174. // check if sample measurement completes
  175. COTSImgScanPrm pScanParam = m_pMsrThread.m_pMeasureParam.GetDefaultParam().GetImageScanParam();
  176. int nTotalFieldSize = m_listFieldCenter.Count;
  177. // get a field center
  178. System.Drawing.Point poiFieldCentre = m_listFieldCenter[i];
  179. // move SEM to the field center
  180. if (!MoveSEMToPoint(poiFieldCentre))
  181. {// failed to move SEM to the position
  182. log.Error("DoHolePreview: failed to move SEM to the field centre point.");
  183. return;
  184. }
  185. // set the BSE scan param
  186. if (!InitHoleBSEParam())
  187. {
  188. log.Error("DoHolePreview: fail to set BSE param.");
  189. }
  190. // take BSE image for the fields
  191. CBSEImgClr pBSEIamge = m_ScanHardwareMgr.AcquireBSEImage();
  192. if (pBSEIamge == null)
  193. {
  194. log.Error("DoHolePreview: failed to acquire a BSE image.");
  195. return;
  196. }
  197. //BSEData
  198. ST_MSTMsg MsgFieldBSE = new ST_MSTMsg();
  199. MsgFieldBSE.InitHolePreBSEDataMsg(pBSEIamge, poiFieldCentre);
  200. m_pMsrThread.SendHolePreviewMessageToMeasureGUI(MsgFieldBSE);
  201. //save the result to project file
  202. Rectangle oImageRect = (Rectangle)pBSEIamge.GetImageRect();
  203. CHoleBSEImg pHoleBSEImg = new CHoleBSEImg(oImageRect, poiFieldCentre);
  204. pHoleBSEImg.SetImageData(pBSEIamge.GetImageDataPtr(), oImageRect.Width, oImageRect.Height);
  205. m_listHoleBSEImg.Add(pHoleBSEImg);
  206. }
  207. ST_MSTMsg MsgSmplEnd = new ST_MSTMsg();
  208. MsgSmplEnd.InitHolePreSampleEndMsg();
  209. m_pMsrThread.SendHolePreviewMessageToMeasureGUI(MsgSmplEnd);
  210. }
  211. }
  212. bool InitHoleBSEParam()
  213. {
  214. // get scan controller
  215. var pScanController = m_ScanHardwareMgr;
  216. // scan parameters
  217. var pMsrParam = m_pMsrThread.m_pMeasureParam.GetDefaultParam();
  218. var pImgScanParam = pMsrParam.GetImageScanParam();
  219. // get image size
  220. var nImageSizeId = pImgScanParam.GetImageResulotion();
  221. int nResulotionId = RESOLUTION_ID_FIRST_TIE + (int)nImageSizeId;
  222. Size sizePixelImage = RESOLUTION_VALUE[nResulotionId];
  223. if (!pScanController.Init())
  224. {
  225. log.Error("SetBSEParam: failed to get scan control.");
  226. return false;
  227. }
  228. // set dwell time
  229. if (!pScanController.SetDwellTime(DwellTimeLevel.Low))
  230. {
  231. return false;
  232. }
  233. // set image size
  234. if (!pScanController.SetImageSize(sizePixelImage.Width / 2, sizePixelImage.Height / 2))
  235. {
  236. // failed to set dwell time
  237. log.Error("SetBSEParam: failed to set ImageSize");
  238. return false;
  239. }
  240. return true;
  241. }
  242. }
  243. }