CSmplPreviewMeasure.cs 13 KB

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