CMeasure.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. using System.Collections.Generic;
  2. using OTSDataType;
  3. using System;
  4. using System.Drawing;
  5. using static OTSDataType.otsdataconst;
  6. using OTSModelSharp.ImageProcess;
  7. using OTSModelSharp.ServiceCenter;
  8. using System.Threading;
  9. using OTSCLRINTERFACE;
  10. using OTSMeasureApp._1_OTSMeasure.Measure._3_MeasureFlow;
  11. namespace OTSModelSharp
  12. {
  13. // enum and struct used for send message to App
  14. public class CMeasure
  15. {
  16. private const string UNTITLED_FILE_NAME = "Untitled";
  17. public delegate void ProgressEventHandler(ST_MSTMsg msg);
  18. public event ProgressEventHandler ProgressEvent;
  19. public event ProgressEventHandler HolePreviewEvent;
  20. COTSMsrPrjResultData m_pProjData;
  21. List< COTSSample> m_listMeasurableSamples=new List<COTSSample>();
  22. string m_strWorkingFolder;
  23. readonly CMsrThreadStatus m_ThreadStatus;
  24. readonly ISemController m_SemController;
  25. protected static NLog.Logger loger = NLog.LogManager.GetCurrentClassLogger();
  26. public SortedDictionary<string, CSmplMeasure> mapSmplMsr = new SortedDictionary<string, CSmplMeasure>();//use this map to hold all the smplMeasure object
  27. public List<COTSSample> GetListMeasurableSamples()
  28. {
  29. return m_listMeasurableSamples;
  30. }
  31. public void SetListMeasurableSamples(List<COTSSample> value)
  32. {
  33. m_listMeasurableSamples = value;
  34. }
  35. public CMeasure()
  36. {
  37. m_strWorkingFolder = "";
  38. m_ThreadStatus = new CMsrThreadStatus();
  39. m_SemController = SemController.GetSEMController();
  40. }
  41. public COTSMsrPrjResultData GetProjResultData()
  42. {
  43. return m_pProjData;
  44. }
  45. public ISemController GetSEMController()
  46. {
  47. // get SEM, scanner and x-ray controller via hardware manager
  48. return m_SemController;
  49. }
  50. public CMsrThreadStatus GetMsrThreadStatus() { return m_ThreadStatus; }
  51. public void Init(COTSMsrPrjResultData a_pProjMgrFile)
  52. {
  53. m_pProjData = a_pProjMgrFile;
  54. SetListMeasurableSamples(a_pProjMgrFile.GetSampleList());
  55. return ;
  56. }
  57. void ThreadOver()
  58. {
  59. ST_MSTMsg MsrMsg = new ST_MSTMsg(m_ThreadStatus);
  60. MsrMsg.InitThreadOverMsg();
  61. SendMessageToMeasureGUI(MsrMsg);
  62. if (m_ThreadStatus.GetStatus() == otsdataconst.OTS_MSR_THREAD_STATUS.FAILED || m_ThreadStatus.GetStatus() == otsdataconst.OTS_MSR_THREAD_STATUS.COMPLETED)
  63. {
  64. if (m_pProjData.GetGenParam().AutoBeamOff)
  65. {
  66. m_SemController.SetSemBeamBlank(false); //true?
  67. Thread.Sleep(2000);
  68. m_SemController.RunHIGH_VACUUM();
  69. Thread.Sleep(10000);
  70. m_SemController.StopImageAcquisition();
  71. }
  72. }
  73. }
  74. void HolePreviewThreadOver()
  75. {
  76. ST_MSTMsg MsrMsg = new ST_MSTMsg(m_ThreadStatus);
  77. MsrMsg.InitThreadOverMsg();
  78. SendHolePreviewMessageToMeasureGUI(MsrMsg);
  79. }
  80. void SetWorkingFolderStrAndSaveCurrentSettings()
  81. {
  82. // get project file pathname
  83. string strSettingFilePathName = m_pProjData.GetPathName();
  84. strSettingFilePathName.Trim();
  85. if (strSettingFilePathName == "")
  86. {
  87. loger .Error("SetWorkingFolderStr: project file pathname is an empty string");
  88. return ;
  89. }
  90. else if (strSettingFilePathName==UNTITLED_FILE_NAME)
  91. {
  92. loger .Error ("SetWorkingFolderStr: project file pathname is an invalid string");
  93. return ;
  94. }
  95. // working folder string
  96. m_strWorkingFolder = FileHelper.GetFolderName(strSettingFilePathName);
  97. m_pProjData.Save();
  98. return ;
  99. }
  100. public void SendMessageToMeasureGUI(ST_MSTMsg msg)
  101. {
  102. ProgressEvent(msg);
  103. }
  104. public void SendHolePreviewMessageToMeasureGUI(ST_MSTMsg msg)
  105. {
  106. HolePreviewEvent(msg);
  107. }
  108. public bool IsMeasureStopped()
  109. {
  110. return m_ThreadStatus.GetStatus() == OTS_MSR_THREAD_STATUS.STOPPED;
  111. }
  112. public bool IsMeasureRunning() { return m_ThreadStatus.GetStatus() == OTS_MSR_THREAD_STATUS.INPROCESS; }
  113. public bool IsMeasureFailed() { return m_ThreadStatus.GetStatus() == OTS_MSR_THREAD_STATUS.FAILED; }
  114. public bool IsMeasureCompleted() { return m_ThreadStatus.GetStatus() == OTS_MSR_THREAD_STATUS.COMPLETED; }
  115. public void DoMeasure()
  116. {
  117. // start measurement, creat thread measure status class, let the main thread know that measurement started
  118. m_ThreadStatus.SetStartTime(System.DateTime.Now);
  119. ST_MSTMsg MsgMsrStart = new ST_MSTMsg(m_ThreadStatus);
  120. MsgMsrStart.InitThreadStartMsg();
  121. SendMessageToMeasureGUI (MsgMsrStart);
  122. loger.Info("Measurement started!");
  123. // connect hardware
  124. loger.Info("Connect SEM!");
  125. if (!m_SemController.Connect())
  126. {
  127. loger .Error("DoMeasure: failed to connect hardware.");
  128. m_ThreadStatus.SetStatus(OTS_MSR_THREAD_STATUS.FAILED);
  129. m_ThreadStatus.ComputeTime(OTS_THREAD_TIME_TYPE.STOPPED);
  130. ThreadOver();
  131. return;
  132. }
  133. // set working directory which is the same directory of the setting file
  134. SetWorkingFolderStrAndSaveCurrentSettings();
  135. List <string > listMeasuredSamples = m_ThreadStatus.GetCompletedSamples();
  136. // got through measure list
  137. foreach (var pSample in GetListMeasurableSamples())
  138. {// check and break if stop button is clicked
  139. if (m_ThreadStatus.GetStatus()== OTS_MSR_THREAD_STATUS.STOPPED )
  140. {
  141. // stop button clicked
  142. loger .Info("DoMeasure: stop button is clicked.");
  143. // record end time
  144. m_ThreadStatus.ComputeTime(OTS_THREAD_TIME_TYPE.STOPPED);
  145. ThreadOver();
  146. return;
  147. }
  148. if (m_ThreadStatus.GetStatus() == OTS_MSR_THREAD_STATUS.PAUSED)
  149. {
  150. // stop button clicked
  151. loger.Info("DoMeasure: stop button is clicked.");
  152. // record end time
  153. m_ThreadStatus.ComputeTime(OTS_THREAD_TIME_TYPE.STOPPED);
  154. while (m_ThreadStatus.GetStatus() == OTS_MSR_THREAD_STATUS.PAUSED)
  155. {
  156. Thread.Sleep(300);
  157. }
  158. //return;
  159. }
  160. if (!pSample.GetSwitch())
  161. {
  162. continue;
  163. }
  164. var sta = pSample.GetMsrStatus().GetStatus();
  165. if (sta == OTS_MSR_SAMPLE_STATUS.SUCCESSED || sta == OTS_MSR_SAMPLE_STATUS.STOPPED)
  166. {
  167. continue;
  168. }
  169. CSmplMeasure pSmplMeasure;
  170. if (!mapSmplMsr.ContainsKey(pSample.GetName()))
  171. {// create a sample measure object for the sample
  172. switch (m_pProjData.SystemTypeId)
  173. {
  174. case OTS_SysType_ID.IncA:
  175. pSmplMeasure = new CSmplMeasureIncA(m_strWorkingFolder, pSample);
  176. break;
  177. case OTS_SysType_ID.CleannessA:
  178. pSmplMeasure = new CSmplMeasureCleanliness(m_strWorkingFolder, pSample);
  179. break;
  180. default:
  181. pSmplMeasure = new CSmplMeasureIncA(m_strWorkingFolder, pSample);
  182. break;
  183. }
  184. // set measure thread
  185. pSmplMeasure.SetMsrThread(this);
  186. mapSmplMsr[pSample.GetName()] = pSmplMeasure;
  187. }
  188. else
  189. {
  190. pSmplMeasure = mapSmplMsr[pSample.GetName()];
  191. pSample.GetMsrStatus().SetStatus(OTS_MSR_SAMPLE_STATUS.INPROCESS);
  192. m_ThreadStatus.SetStatus(OTS_MSR_THREAD_STATUS.INPROCESS);
  193. }
  194. m_pProjData.SetWorkingSampleByName(pSample.GetName());
  195. pSmplMeasure.DoMeasureForOneSample();
  196. if (pSample.GetMsrStatus().GetStatus() == OTS_MSR_SAMPLE_STATUS.STOPPED)
  197. {// record end time
  198. m_ThreadStatus.SetStatus(OTS_MSR_THREAD_STATUS.STOPPED);
  199. m_ThreadStatus.ComputeTime(OTS_THREAD_TIME_TYPE.STOPPED);
  200. // update thread measure status class, let the main thread know that this sample measurement stopped
  201. ST_MSTMsg MsgSmpStop = new ST_MSTMsg(m_ThreadStatus);
  202. MsgSmpStop.InitThreadStoppedMsg();
  203. SendMessageToMeasureGUI(MsgSmpStop);
  204. ThreadOver();
  205. break;
  206. }
  207. if (pSample.GetMsrStatus().GetStatus() == OTS_MSR_SAMPLE_STATUS.FAILED)
  208. {
  209. // measurement failed
  210. m_ThreadStatus.SetStatus(OTS_MSR_THREAD_STATUS.FAILED);
  211. // record end time
  212. m_ThreadStatus.ComputeTime(OTS_THREAD_TIME_TYPE.STOPPED);
  213. // update thread measure status class, let the main thread know that this sample measurement failed
  214. ST_MSTMsg MsgSmpFailed=new ST_MSTMsg(m_ThreadStatus);
  215. MsgSmpFailed.InitThreadFailedMsg();
  216. SendMessageToMeasureGUI(MsgSmpFailed);
  217. ThreadOver();
  218. return;
  219. }
  220. // update thread measure status class, let the main thread know that this sample measurement successes
  221. ST_MSTMsg MsgSmpSuccess = new ST_MSTMsg(m_ThreadStatus);
  222. MsgSmpSuccess.InitThreadSucceedMsg();
  223. SendMessageToMeasureGUI(MsgSmpSuccess);
  224. // continue to the next sample
  225. listMeasuredSamples.Add(pSample.GetName());
  226. }
  227. // measurement completed
  228. m_ThreadStatus.SetStatus(OTS_MSR_THREAD_STATUS.COMPLETED);
  229. // record end time
  230. m_ThreadStatus.ComputeTime(OTS_THREAD_TIME_TYPE.STOPPED);
  231. ThreadOver();
  232. }
  233. // hole preview
  234. public void DoHolePreview()
  235. {
  236. // start measurement, creat thread measure status class, let the main thread know that measurement started
  237. // set measure status to in-process
  238. //record time
  239. m_ThreadStatus.ComputeTime(OTS_THREAD_TIME_TYPE.START);
  240. DateTime timeStart = m_ThreadStatus.GetStartTime();
  241. ST_MSTMsg MsgMsrStart = new ST_MSTMsg(m_ThreadStatus);
  242. MsgMsrStart.InitHolePreThreadInProcessMsg();
  243. SendHolePreviewMessageToMeasureGUI(MsgMsrStart);
  244. // connect hardware
  245. if (!m_SemController.Connect())
  246. {
  247. // failed to connect hardware
  248. SetMsrLoopStatus(otsdataconst.OTS_MSR_THREAD_STATUS.FAILED);
  249. m_ThreadStatus.ComputeTime(OTS_THREAD_TIME_TYPE.STOPPED);
  250. HolePreviewThreadOver();
  251. }
  252. var a_pMeasureArea = m_pProjData.GetWorkingSample().GetMsrDomain();
  253. COTSSample pSampleHole = CreateHoleSample(a_pMeasureArea);
  254. // create a sample measure object for the sample
  255. CSmplMeasure pSmplMeasure = new CSmplMeasure(m_strWorkingFolder, m_pProjData.GetWorkingSample());
  256. // set measure thread
  257. pSmplMeasure.SetMsrThread(this);
  258. // update thread measure status class, let the main thread know that this sample measurement starts
  259. pSmplMeasure.SetHolePreviewSample(pSampleHole);
  260. // do measure
  261. pSmplMeasure.DoHolePreview();
  262. // check if measurement is successful
  263. if (pSampleHole.GetMsrStatus().GetStatus() == OTS_MSR_SAMPLE_STATUS.STOPPED)
  264. {
  265. // record end time
  266. m_ThreadStatus.ComputeTime(OTS_THREAD_TIME_TYPE.STOPPED);
  267. // measurement stopped
  268. SetMsrLoopStatus(otsdataconst.OTS_MSR_THREAD_STATUS.STOPPED);
  269. // update thread measure status class, let the main thread know that this sample measurement stopped
  270. ST_MSTMsg MsgSmpStop = new ST_MSTMsg(m_ThreadStatus);
  271. MsgSmpStop.InitHolePreThreadStoppedMsg();
  272. SendHolePreviewMessageToMeasureGUI(MsgSmpStop);
  273. HolePreviewThreadOver();
  274. return;
  275. }
  276. else if (pSampleHole.GetMsrStatus().GetStatus() == OTS_MSR_SAMPLE_STATUS.FAILED)
  277. {
  278. // measurement failed
  279. SetMsrLoopStatus(otsdataconst.OTS_MSR_THREAD_STATUS.FAILED);
  280. // record end time
  281. m_ThreadStatus.ComputeTime(OTS_THREAD_TIME_TYPE.STOPPED);
  282. // update thread measure status class, let the main thread know that this sample measurement failed
  283. ST_MSTMsg MsgSmpFailed = new ST_MSTMsg(m_ThreadStatus);
  284. MsgSmpFailed.InitHolePreThreadFailedMsg();
  285. SendHolePreviewMessageToMeasureGUI(MsgSmpFailed);
  286. HolePreviewThreadOver();
  287. return;
  288. }
  289. // record end time
  290. m_ThreadStatus.ComputeTime(OTS_THREAD_TIME_TYPE.STOPPED);
  291. // update thread measure status class, let the main thread know that this sample measurement successes
  292. ST_MSTMsg MsgSmpSuccess = new ST_MSTMsg(m_ThreadStatus);
  293. MsgSmpSuccess.InitHolePreThreadSucceedMsg();
  294. SendHolePreviewMessageToMeasureGUI(MsgSmpSuccess);
  295. // measurement completed
  296. SetMsrLoopStatus(otsdataconst.OTS_MSR_THREAD_STATUS.COMPLETED);
  297. // record end time
  298. m_ThreadStatus.ComputeTime(OTS_THREAD_TIME_TYPE.STOPPED);
  299. HolePreviewThreadOver();
  300. }
  301. public COTSSample CreateHoleSample(CDomain a_pMsrArea)
  302. {
  303. COTSSample pHoleSample = new COTSSample();
  304. pHoleSample.SetMsrDomain(a_pMsrArea);
  305. // get min magnification
  306. CSEMStageData pSEMStageData = m_pProjData.GetSEMStageData();
  307. double dMinMag = pSEMStageData.GetMinMag();
  308. // get scan field size 100
  309. int nScanFieldSize100 = pSEMStageData.GetScanFieldSize100();
  310. // get working distance
  311. double dWorkingDistance = 0.0;
  312. if (!GetSEMWorkingDistanceFromHW(ref dWorkingDistance))
  313. {
  314. return null;
  315. }
  316. CSEMDataMsr poSEMDataMsr = new CSEMDataMsr();
  317. poSEMDataMsr.SetScanFieldSize100(nScanFieldSize100);
  318. poSEMDataMsr.SetWorkingDistance(dWorkingDistance);
  319. poSEMDataMsr.SetMagnification(dMinMag);
  320. pHoleSample.SetSEMDataMsr(poSEMDataMsr);
  321. // Set image scan param
  322. COTSImgScanPrm poImageScanParam = new COTSImgScanPrm();
  323. poImageScanParam.SetStopMode(((int)OTS_MEASURE_STOP_MODE.CoverMode).ToString());
  324. poImageScanParam.SetStartImageMode(OTS_GET_IMAGE_MODE.Zshape);
  325. poImageScanParam.SetScanImageSpeed(OTS_IMAGE_SCANSPEED_OPTIONS.low);
  326. CSampleParam poMsrParams = pHoleSample.GetMsrParams();
  327. poImageScanParam.SetImageResulotion(GetListMeasurableSamples()[0].GetMsrParams().GetImageScanParam().GetImageResulotion());//由于各样品分辨率应该一致,故此处没有读取选取的特定样品孔样品
  328. poMsrParams.SetImageScanParam(poImageScanParam);
  329. pHoleSample.SetMsrParams(poMsrParams);
  330. return pHoleSample;
  331. }
  332. public bool GetSEMWorkingDistanceFromHW(ref double a_dWorkingDistance)
  333. {
  334. m_SemController.GetWorkingDistance(ref a_dWorkingDistance);
  335. return true;
  336. }
  337. // measure status
  338. public void SetMsrLoopStatus(otsdataconst.OTS_MSR_THREAD_STATUS a_nMsrLoopStatus)
  339. {
  340. m_ThreadStatus.SetStatus( a_nMsrLoopStatus);
  341. }
  342. }
  343. }