CMsrThread.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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.ServiceInterface;
  8. namespace OTSModelSharp
  9. {
  10. // enum and struct used for send message to App
  11. public enum ENUM_MSG_TYPE
  12. {
  13. MTHREADSTATUS = 1001,
  14. MSAMPLESTATUS = 1002,
  15. MSAMPLERESULT = 1003
  16. };
  17. public enum MSAMPLE_RET
  18. {
  19. BSE_DATA = 0,
  20. FIELD_DATA = 1,
  21. START_MSR_FIELD = 2
  22. };
  23. public struct SMSR_COMPLETE_DATA
  24. {
  25. public OTS_MSR_THREAD_STATUS MsrStatus;
  26. public string csMsrStartTime;
  27. public int iMsrCompleteSampleCount;
  28. public int iMsrCompleteFieldCount;
  29. public int iParticleCount;
  30. public TimeSpan MsrUsedTime;
  31. public string csMsrEndTime;
  32. };
  33. public struct STMThreadStatus
  34. {
  35. public OTS_MSR_THREAD_STATUS iMsrStatu; //OTS_MSR_THREAD_STATUS
  36. public string csMsrStartTime; //MSR_START_TIME
  37. public string csMsrEndTime; //MSR_END_TIME
  38. public SMSR_COMPLETE_DATA SMsrCompleteData;
  39. };
  40. public struct STMSampleStatus
  41. {
  42. public OTS_MSR_SAMPLE_STATUS iMsrSampleStatu; //OTS_MSR_SAMPLE_STATUS
  43. public string cSampleName;
  44. public string csSampleMsrStartTime;
  45. public List<Point> BCompleteFieldList;
  46. };
  47. public struct STMSampleResultData
  48. {
  49. public MSAMPLE_RET iRetDataType; //ENUM_MEASURE_SAMPLE_RESULT
  50. public struct RBSEDATA
  51. {
  52. public System.Drawing.Point pos;
  53. public int iBSEDataHeight;
  54. public int iBSEDataWidth;
  55. public byte[] lpBSEData;
  56. };
  57. public struct SAMPLEFIELDDATA
  58. {
  59. public System.Drawing.Point FieldPos;
  60. public int iMeasureFieldCount;
  61. public int iCompleteFieldCount;
  62. public int iSParticleCount; // Field particle count
  63. public TimeSpan TUsedTime;
  64. };
  65. public struct StartToMsrField
  66. {
  67. public System.Drawing.Point FieldPos;
  68. };
  69. public RBSEDATA BSEData;
  70. public SAMPLEFIELDDATA SFieldData;
  71. public StartToMsrField SMsrField;
  72. };
  73. public struct ST_MSTMsg
  74. {
  75. public ENUM_MSG_TYPE iMsgType;
  76. public STMThreadStatus STMThreadStu;
  77. public STMSampleStatus STMSampleStu;
  78. public STMSampleResultData STMSampleRetData;
  79. };
  80. public class CMsrThread
  81. {
  82. private const string UNTITLED_FILE_NAME = "Untitled";
  83. public delegate void ProgressEventHandler(ST_MSTMsg msg);
  84. public event ProgressEventHandler ProgressEvent;
  85. COTSMsrPrjResultData m_pProjData;
  86. List< COTSSample> m_listMeasurableSamples;
  87. string m_strWorkingFolder;
  88. CMsrThreadStatus m_ThreadStatus;
  89. SemController m_SemController;// there is no correspondense clr,so use this instead temporarilly
  90. protected static NLog.Logger loger = NLog.LogManager.GetCurrentClassLogger();
  91. public CMsrThread()
  92. {
  93. m_strWorkingFolder = "";
  94. m_ThreadStatus = new CMsrThreadStatus();
  95. m_SemController = new SemController();
  96. }
  97. public COTSMsrPrjResultData GetProjResultData()
  98. {
  99. return m_pProjData;
  100. }
  101. public ISemController GetSEMController()
  102. {
  103. // get SEM, scanner and x-ray controller via hardware manager
  104. return m_SemController;
  105. }
  106. public CMsrThreadStatus GetMsrThreadStatus() { return m_ThreadStatus; }
  107. public void Init(COTSMsrPrjResultData a_pProjMgrFile)
  108. {
  109. m_pProjData = a_pProjMgrFile;
  110. m_listMeasurableSamples = a_pProjMgrFile.GetSampleList() ;
  111. return ;
  112. }
  113. void ThreadOver()
  114. {
  115. DateTime timeEnd = m_ThreadStatus.GetEndTime();
  116. ST_MSTMsg MsrMsg = new ST_MSTMsg();
  117. MsrMsg.iMsgType = ENUM_MSG_TYPE.MTHREADSTATUS;
  118. MsrMsg.STMThreadStu.iMsrStatu = m_ThreadStatus.GetStatus();
  119. MsrMsg.STMThreadStu.csMsrEndTime = timeEnd.ToShortDateString();
  120. ProgressEvent(MsrMsg);
  121. }
  122. void SetWorkingFolderStr()
  123. {
  124. // get project file pathname
  125. string strSettingFilePathName = m_pProjData.GetPathName();
  126. strSettingFilePathName.Trim();
  127. if (strSettingFilePathName == "")
  128. {
  129. loger .Error("SetWorkingFolderStr: project file pathname is an empty string");
  130. return ;
  131. }
  132. else if (strSettingFilePathName==UNTITLED_FILE_NAME)
  133. {
  134. loger .Error ("SetWorkingFolderStr: project file pathname is an invalid string");
  135. return ;
  136. }
  137. // working folder string
  138. string strWorkingFolder =FileHelper.GetFolderName(strSettingFilePathName);
  139. // set working folder string
  140. m_strWorkingFolder = strWorkingFolder;
  141. // ok, return TRUE
  142. return ;
  143. }
  144. public void SendMessageToMeasureApp(ST_MSTMsg msg)
  145. {
  146. ProgressEvent(msg);
  147. }
  148. public bool IsMeasureStopped()
  149. {
  150. return m_ThreadStatus.GetStatus() == OTS_MSR_THREAD_STATUS.STOPPED;
  151. }
  152. public bool IsMeasureRunning() { return m_ThreadStatus.GetStatus() == OTS_MSR_THREAD_STATUS.INPROCESS; }
  153. public bool IsMeasureFailed() { return m_ThreadStatus.GetStatus() == OTS_MSR_THREAD_STATUS.FAILED; }
  154. public bool IsMeasureCompleted() { return m_ThreadStatus.GetStatus() == OTS_MSR_THREAD_STATUS.COMPLETED; }
  155. public void DoMeasure()
  156. {
  157. // start measurement, creat thread measure status class, let the main thread know that measurement started
  158. SortedDictionary<string , CSmplMeasure> mapSmplMsr=new SortedDictionary<string, CSmplMeasure>();//use this map to hold all the smplMeasure object
  159. m_ThreadStatus.SetStartTime(System.DateTime.Now);
  160. DateTime timeStart = m_ThreadStatus.GetStartTime();
  161. ST_MSTMsg MsgMsrStart=new ST_MSTMsg();
  162. MsgMsrStart.iMsgType = ENUM_MSG_TYPE.MTHREADSTATUS;
  163. MsgMsrStart.STMThreadStu.iMsrStatu = OTS_MSR_THREAD_STATUS.INPROCESS;
  164. MsgMsrStart.STMThreadStu.csMsrStartTime = timeStart.ToShortDateString();
  165. ProgressEvent (MsgMsrStart);
  166. loger.Info("Measurement started!");
  167. // connect hardware
  168. loger.Info("Connect SEM!");
  169. if (!m_SemController.Connect())
  170. {
  171. loger .Error("DoMeasure: failed to connect hardware.");
  172. m_ThreadStatus.SetStatus(OTS_MSR_THREAD_STATUS.FAILED);
  173. m_ThreadStatus.ComputeTime(OTS_THREAD_TIME_TYPE.STOPPED);
  174. ThreadOver();
  175. return;
  176. }
  177. // set working directory which is the same directory of the setting file
  178. SetWorkingFolderStr();
  179. List <string > listMeasuredSamples = m_ThreadStatus.GetCompletedSamples();
  180. // got through measure list
  181. foreach (var pSample in m_listMeasurableSamples)
  182. {// check and break if stop button is clicked
  183. if (m_ThreadStatus.GetStatus()== OTS_MSR_THREAD_STATUS.STOPPED )
  184. {
  185. // stop button clicked
  186. loger .Info("DoMeasure: stop button is clicked.");
  187. // record end time
  188. m_ThreadStatus.ComputeTime(OTS_THREAD_TIME_TYPE.STOPPED);
  189. ThreadOver();
  190. return;
  191. }
  192. if (!pSample.GetSwitch())
  193. {
  194. continue;
  195. }
  196. CSmplMeasure pSmplMeasure;
  197. if (!mapSmplMsr.ContainsKey(pSample.GetName()))
  198. {// create a sample measure object for the sample
  199. switch (m_pProjData.m_nPackId)
  200. {
  201. case OTS_SysType_ID.IncA:
  202. pSmplMeasure = new CSmplMeasureInclution(m_strWorkingFolder, pSample);
  203. break;
  204. case OTS_SysType_ID.CleanlinessA:
  205. pSmplMeasure = new CSmplMeasureCleanliness(m_strWorkingFolder, pSample);
  206. break;
  207. default:
  208. pSmplMeasure = new CSmplMeasureInclution(m_strWorkingFolder, pSample);
  209. break;
  210. }
  211. // set measure thread
  212. pSmplMeasure.SetMsrThread(this);
  213. mapSmplMsr[pSample.GetName()] = pSmplMeasure;
  214. }
  215. else
  216. {
  217. pSmplMeasure = mapSmplMsr[pSample.GetName()];
  218. pSample.GetMsrStatus().SetStatus(OTS_MSR_SAMPLE_STATUS.INPROCESS);
  219. m_ThreadStatus.SetStatus(OTS_MSR_THREAD_STATUS.INPROCESS);
  220. }
  221. pSmplMeasure.DoMeasureForOneSample();
  222. // check if measurement is successful
  223. if (pSample.GetMsrStatus().GetStatus() == OTS_MSR_SAMPLE_STATUS.STOPPED)
  224. {// record end time
  225. m_ThreadStatus.SetStatus(OTS_MSR_THREAD_STATUS.STOPPED);
  226. m_ThreadStatus.ComputeTime(OTS_THREAD_TIME_TYPE.STOPPED);
  227. // update thread measure status class, let the main thread know that this sample measurement stopped
  228. ST_MSTMsg MsgSmpStop = new ST_MSTMsg();
  229. MsgSmpStop.iMsgType = ENUM_MSG_TYPE.MSAMPLESTATUS;
  230. MsgSmpStop.STMThreadStu.iMsrStatu= OTS_MSR_THREAD_STATUS.STOPPED;
  231. MsgSmpStop.STMSampleStu.iMsrSampleStatu = OTS_MSR_SAMPLE_STATUS.STOPPED;
  232. MsgSmpStop.STMThreadStu.csMsrEndTime = DateTime .Now .ToShortDateString();
  233. MsgSmpStop.STMThreadStu.iMsrStatu= OTS_MSR_THREAD_STATUS.STOPPED;
  234. ProgressEvent(MsgSmpStop);
  235. ThreadOver();
  236. return;
  237. }
  238. else if (pSample.GetMsrStatus().GetStatus() == OTS_MSR_SAMPLE_STATUS.FAILED)
  239. {
  240. // measurement failed
  241. m_ThreadStatus.SetStatus(OTS_MSR_THREAD_STATUS.FAILED);
  242. // record end time
  243. m_ThreadStatus.ComputeTime(OTS_THREAD_TIME_TYPE.STOPPED);
  244. // update thread measure status class, let the main thread know that this sample measurement failed
  245. ST_MSTMsg MsgSmpFailed=new ST_MSTMsg();
  246. MsgSmpFailed.iMsgType = ENUM_MSG_TYPE.MSAMPLESTATUS;
  247. MsgSmpFailed.STMSampleStu.iMsrSampleStatu = OTS_MSR_SAMPLE_STATUS.FAILED;
  248. ProgressEvent(MsgSmpFailed);
  249. ThreadOver();
  250. return;
  251. }
  252. // record end time
  253. m_ThreadStatus.ComputeTime(OTS_THREAD_TIME_TYPE.STOPPED);
  254. // update thread measure status class, let the main thread know that this sample measurement successes
  255. ST_MSTMsg MsgSmpSuccess=new ST_MSTMsg();
  256. MsgSmpSuccess.iMsgType = ENUM_MSG_TYPE.MSAMPLESTATUS;
  257. MsgSmpSuccess.STMSampleStu.iMsrSampleStatu = OTS_MSR_SAMPLE_STATUS.SUCCESSED;
  258. ProgressEvent(MsgSmpSuccess);
  259. // continue to the next sample
  260. listMeasuredSamples.Add (pSample.GetName());
  261. }
  262. // measurement completed
  263. m_ThreadStatus.SetStatus(OTS_MSR_THREAD_STATUS.COMPLETED);
  264. // record end time
  265. m_ThreadStatus.ComputeTime(OTS_THREAD_TIME_TYPE.STOPPED);
  266. ThreadOver();
  267. }
  268. // hole preview
  269. public void DoHolePreview(int a_nHoleID, CDomain a_pMeasureArea)
  270. {
  271. // start measurement, creat thread measure status class, let the main thread know that measurement started
  272. // set measure status to in-process
  273. //record time
  274. m_ThreadStatus.ComputeTime(OTS_THREAD_TIME_TYPE.START);
  275. DateTime timeStart = m_ThreadStatus.GetStartTime();
  276. ST_MSTMsg MsgMsrStart = new ST_MSTMsg();
  277. MsgMsrStart.iMsgType = ENUM_MSG_TYPE.MTHREADSTATUS;
  278. MsgMsrStart.STMThreadStu.iMsrStatu =OTS_MSR_THREAD_STATUS.INPROCESS;
  279. SendMessageToMeasureApp(MsgMsrStart);
  280. // connect hardware
  281. if (!m_SemController.Connect())
  282. {
  283. // failed to connect hardware
  284. SetMsrLoopStatus(otsdataconst.OTS_MSR_THREAD_STATUS.FAILED);
  285. m_ThreadStatus.ComputeTime(OTS_THREAD_TIME_TYPE.STOPPED);
  286. ThreadOver();
  287. }
  288. COTSSample pSampleHole = CreateHoleSample(a_pMeasureArea);
  289. // create a sample measure object for the sample
  290. CSmplMeasure pSmplMeasure = new CSmplMeasureCleanliness(m_strWorkingFolder,pSampleHole);
  291. // set measure thread
  292. pSmplMeasure.SetMsrThread(this);
  293. // update thread measure status class, let the main thread know that this sample measurement starts
  294. // set working folder string
  295. pSmplMeasure.SetSample(pSampleHole);
  296. pSmplMeasure.SetWorkingFolder(m_strWorkingFolder);
  297. // do measure
  298. pSmplMeasure.DoHolePreview(a_nHoleID);
  299. // check if measurement is successful
  300. if (pSampleHole.GetMsrStatus().GetStatus() == OTS_MSR_SAMPLE_STATUS.STOPPED)
  301. {
  302. // record end time
  303. m_ThreadStatus.ComputeTime(OTS_THREAD_TIME_TYPE.STOPPED);
  304. // measurement stopped
  305. SetMsrLoopStatus(otsdataconst.OTS_MSR_THREAD_STATUS.STOPPED);
  306. // update thread measure status class, let the main thread know that this sample measurement stopped
  307. ST_MSTMsg MsgSmpStop = new ST_MSTMsg();
  308. MsgSmpStop.iMsgType = ENUM_MSG_TYPE.MSAMPLESTATUS;
  309. MsgSmpStop.STMSampleStu.iMsrSampleStatu = OTS_MSR_SAMPLE_STATUS.STOPPED;
  310. SendMessageToMeasureApp(MsgSmpStop);
  311. ThreadOver();
  312. return;
  313. }
  314. else if (pSampleHole.GetMsrStatus().GetStatus() == OTS_MSR_SAMPLE_STATUS.FAILED)
  315. {
  316. // measurement failed
  317. SetMsrLoopStatus(otsdataconst.OTS_MSR_THREAD_STATUS.FAILED);
  318. // record end time
  319. m_ThreadStatus.ComputeTime(OTS_THREAD_TIME_TYPE.STOPPED);
  320. // update thread measure status class, let the main thread know that this sample measurement failed
  321. ST_MSTMsg MsgSmpFailed = new ST_MSTMsg();
  322. MsgSmpFailed.iMsgType = ENUM_MSG_TYPE.MSAMPLESTATUS;
  323. MsgSmpFailed.STMSampleStu.iMsrSampleStatu = OTS_MSR_SAMPLE_STATUS.FAILED;
  324. SendMessageToMeasureApp(MsgSmpFailed);
  325. ThreadOver();
  326. return;
  327. }
  328. // record end time
  329. m_ThreadStatus.ComputeTime(OTS_THREAD_TIME_TYPE.STOPPED);
  330. // update thread measure status class, let the main thread know that this sample measurement successes
  331. ST_MSTMsg MsgSmpSuccess = new ST_MSTMsg();
  332. MsgSmpSuccess.iMsgType = ENUM_MSG_TYPE.MSAMPLESTATUS;
  333. MsgSmpSuccess.STMSampleStu.iMsrSampleStatu = OTS_MSR_SAMPLE_STATUS.SUCCESSED;
  334. SendMessageToMeasureApp(MsgSmpSuccess);
  335. // measurement completed
  336. SetMsrLoopStatus(otsdataconst.OTS_MSR_THREAD_STATUS.COMPLETED);
  337. // record end time
  338. m_ThreadStatus.ComputeTime(OTS_THREAD_TIME_TYPE.STOPPED);
  339. ThreadOver();
  340. }
  341. public COTSSample CreateHoleSample(CDomain a_pMsrArea)
  342. {
  343. COTSSample pHoleSample = new COTSSample();
  344. pHoleSample.SetMsrArea(a_pMsrArea);
  345. // get min magnification
  346. CSEMStageData pSEMStageData = m_pProjData.GetSEMStageData();
  347. double dMinMag = pSEMStageData.GetMinMag();
  348. // get scan field size 100
  349. int nScanFieldSize100 = pSEMStageData.GetScanFieldSize100();
  350. // get working distance
  351. double dWorkingDistance = 0.0;
  352. if (!GetSEMWorkingDistanceFromHW(ref dWorkingDistance))
  353. {
  354. return null;
  355. }
  356. CSEMDataMsr poSEMDataMsr = new CSEMDataMsr();
  357. poSEMDataMsr.SetScanFieldSize100(nScanFieldSize100);
  358. poSEMDataMsr.SetWorkingDistance(dWorkingDistance);
  359. poSEMDataMsr.SetMagnification(dMinMag);
  360. pHoleSample.SetSEMDataMsr(poSEMDataMsr);
  361. // Set image scan param
  362. COTSImgScanPrm poImageScanParam = new COTSImgScanPrm();
  363. poImageScanParam.SetStopMode(OTS_MEASURE_STOP_MODE.CoverMode);
  364. poImageScanParam.SetStartImageMode(OTS_GET_IMAGE_MODE.FROM_CENTER);
  365. poImageScanParam.SetScanImageSpeed(OTS_THREE_TIES_OPTIONS.TIE1);
  366. poImageScanParam.SetImagePixelSize(OTS_FIVE_TIES_OPTIONS.TIE1);
  367. CSampleParam poMsrParams = pHoleSample.GetMsrParams();
  368. poMsrParams.SetImageScanParam(poImageScanParam);
  369. pHoleSample.SetMsrParams(poMsrParams);
  370. return pHoleSample;
  371. }
  372. public bool GetSEMWorkingDistanceFromHW(ref double a_dWorkingDistance)
  373. {
  374. m_SemController.GetWorkingDistance(ref a_dWorkingDistance);
  375. return true;
  376. }
  377. // measure status
  378. public void SetMsrLoopStatus(otsdataconst.OTS_MSR_THREAD_STATUS a_nMsrLoopStatus)
  379. {
  380. if (a_nMsrLoopStatus >= otsdataconst.OTS_MSR_THREAD_STATUS.MIN && a_nMsrLoopStatus <= otsdataconst.OTS_MSR_THREAD_STATUS.MAX)
  381. {
  382. m_ThreadStatus.SetStatus( a_nMsrLoopStatus);
  383. }
  384. }
  385. }
  386. }