CMeasure.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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 CMeasure
  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 CMeasure()
  92. {
  93. m_strWorkingFolder = "";
  94. m_ThreadStatus = new CMsrThreadStatus();
  95. m_SemController = SemController.GetSEMController();
  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.ToString("yyyy-MM-dd HH:mm:ss");
  120. ProgressEvent(MsrMsg);
  121. }
  122. void ThreadOverWithoutDisConnect()
  123. {
  124. DateTime timeEnd = m_ThreadStatus.GetEndTime();
  125. ST_MSTMsg MsrMsg = new ST_MSTMsg();
  126. MsrMsg.iMsgType = ENUM_MSG_TYPE.MTHREADSTATUS;
  127. MsrMsg.STMThreadStu.iMsrStatu = m_ThreadStatus.GetStatus();
  128. MsrMsg.STMThreadStu.csMsrEndTime = timeEnd.ToString("yyyy-MM-dd HH:mm:ss");
  129. ProgressEvent(MsrMsg);
  130. }
  131. void SetWorkingFolderStr()
  132. {
  133. // get project file pathname
  134. string strSettingFilePathName = m_pProjData.GetPathName();
  135. strSettingFilePathName.Trim();
  136. if (strSettingFilePathName == "")
  137. {
  138. loger .Error("SetWorkingFolderStr: project file pathname is an empty string");
  139. return ;
  140. }
  141. else if (strSettingFilePathName==UNTITLED_FILE_NAME)
  142. {
  143. loger .Error ("SetWorkingFolderStr: project file pathname is an invalid string");
  144. return ;
  145. }
  146. // working folder string
  147. m_strWorkingFolder = FileHelper.GetFolderName(strSettingFilePathName);
  148. return ;
  149. }
  150. public void SendMessageToMeasureApp(ST_MSTMsg msg)
  151. {
  152. ProgressEvent(msg);
  153. }
  154. public bool IsMeasureStopped()
  155. {
  156. return m_ThreadStatus.GetStatus() == OTS_MSR_THREAD_STATUS.STOPPED;
  157. }
  158. public bool IsMeasureRunning() { return m_ThreadStatus.GetStatus() == OTS_MSR_THREAD_STATUS.INPROCESS; }
  159. public bool IsMeasureFailed() { return m_ThreadStatus.GetStatus() == OTS_MSR_THREAD_STATUS.FAILED; }
  160. public bool IsMeasureCompleted() { return m_ThreadStatus.GetStatus() == OTS_MSR_THREAD_STATUS.COMPLETED; }
  161. public void DoMeasure()
  162. {
  163. // start measurement, creat thread measure status class, let the main thread know that measurement started
  164. SortedDictionary<string , CSmplMeasure> mapSmplMsr=new SortedDictionary<string, CSmplMeasure>();//use this map to hold all the smplMeasure object
  165. m_ThreadStatus.SetStartTime(System.DateTime.Now);
  166. DateTime timeStart = m_ThreadStatus.GetStartTime();
  167. ST_MSTMsg MsgMsrStart=new ST_MSTMsg();
  168. MsgMsrStart.iMsgType = ENUM_MSG_TYPE.MTHREADSTATUS;
  169. MsgMsrStart.STMThreadStu.iMsrStatu = OTS_MSR_THREAD_STATUS.INPROCESS;
  170. MsgMsrStart.STMThreadStu.csMsrStartTime = timeStart.ToShortDateString();
  171. ProgressEvent (MsgMsrStart);
  172. loger.Info("Measurement started!");
  173. // connect hardware
  174. loger.Info("Connect SEM!");
  175. if (!m_SemController.Connect())
  176. {
  177. loger .Error("DoMeasure: failed to connect hardware.");
  178. m_ThreadStatus.SetStatus(OTS_MSR_THREAD_STATUS.FAILED);
  179. m_ThreadStatus.ComputeTime(OTS_THREAD_TIME_TYPE.STOPPED);
  180. ThreadOver();
  181. return;
  182. }
  183. // set working directory which is the same directory of the setting file
  184. SetWorkingFolderStr();
  185. List <string > listMeasuredSamples = m_ThreadStatus.GetCompletedSamples();
  186. // got through measure list
  187. foreach (var pSample in m_listMeasurableSamples)
  188. {// check and break if stop button is clicked
  189. if (m_ThreadStatus.GetStatus()== OTS_MSR_THREAD_STATUS.STOPPED )
  190. {
  191. // stop button clicked
  192. loger .Info("DoMeasure: stop button is clicked.");
  193. // record end time
  194. m_ThreadStatus.ComputeTime(OTS_THREAD_TIME_TYPE.STOPPED);
  195. ThreadOver();
  196. return;
  197. }
  198. if (!pSample.GetSwitch())
  199. {
  200. continue;
  201. }
  202. CSmplMeasure pSmplMeasure;
  203. if (!mapSmplMsr.ContainsKey(pSample.GetName()))
  204. {// create a sample measure object for the sample
  205. switch (m_pProjData.m_nPackId)
  206. {
  207. case OTS_SysType_ID.IncA:
  208. pSmplMeasure = new CSmplMeasureInclution(m_strWorkingFolder, pSample);
  209. break;
  210. case OTS_SysType_ID.CleannessA:
  211. pSmplMeasure = new CSmplMeasureCleanliness(m_strWorkingFolder, pSample);
  212. break;
  213. default:
  214. pSmplMeasure = new CSmplMeasureInclution(m_strWorkingFolder, pSample);
  215. break;
  216. }
  217. // set measure thread
  218. pSmplMeasure.SetMsrThread(this);
  219. mapSmplMsr[pSample.GetName()] = pSmplMeasure;
  220. }
  221. else
  222. {
  223. pSmplMeasure = mapSmplMsr[pSample.GetName()];
  224. pSample.GetMsrStatus().SetStatus(OTS_MSR_SAMPLE_STATUS.INPROCESS);
  225. m_ThreadStatus.SetStatus(OTS_MSR_THREAD_STATUS.INPROCESS);
  226. }
  227. pSmplMeasure.DoMeasureForOneSample();
  228. // check if measurement is successful
  229. if (pSample.GetMsrStatus().GetStatus() == OTS_MSR_SAMPLE_STATUS.STOPPED)
  230. {// record end time
  231. m_ThreadStatus.SetStatus(OTS_MSR_THREAD_STATUS.STOPPED);
  232. m_ThreadStatus.ComputeTime(OTS_THREAD_TIME_TYPE.STOPPED);
  233. // update thread measure status class, let the main thread know that this sample measurement stopped
  234. ST_MSTMsg MsgSmpStop = new ST_MSTMsg();
  235. MsgSmpStop.iMsgType = ENUM_MSG_TYPE.MSAMPLESTATUS;
  236. MsgSmpStop.STMThreadStu.iMsrStatu= OTS_MSR_THREAD_STATUS.STOPPED;
  237. MsgSmpStop.STMSampleStu.iMsrSampleStatu = OTS_MSR_SAMPLE_STATUS.STOPPED;
  238. MsgSmpStop.STMThreadStu.csMsrEndTime = DateTime .Now .ToShortDateString();
  239. MsgSmpStop.STMThreadStu.iMsrStatu= OTS_MSR_THREAD_STATUS.STOPPED;
  240. ProgressEvent(MsgSmpStop);
  241. ThreadOver();
  242. return;
  243. }
  244. else if (pSample.GetMsrStatus().GetStatus() == OTS_MSR_SAMPLE_STATUS.FAILED)
  245. {
  246. // measurement failed
  247. m_ThreadStatus.SetStatus(OTS_MSR_THREAD_STATUS.FAILED);
  248. // record end time
  249. m_ThreadStatus.ComputeTime(OTS_THREAD_TIME_TYPE.STOPPED);
  250. // update thread measure status class, let the main thread know that this sample measurement failed
  251. ST_MSTMsg MsgSmpFailed=new ST_MSTMsg();
  252. MsgSmpFailed.iMsgType = ENUM_MSG_TYPE.MSAMPLESTATUS;
  253. MsgSmpFailed.STMSampleStu.iMsrSampleStatu = OTS_MSR_SAMPLE_STATUS.FAILED;
  254. ProgressEvent(MsgSmpFailed);
  255. ThreadOver();
  256. return;
  257. }
  258. // record end time
  259. m_ThreadStatus.ComputeTime(OTS_THREAD_TIME_TYPE.STOPPED);
  260. // update thread measure status class, let the main thread know that this sample measurement successes
  261. ST_MSTMsg MsgSmpSuccess=new ST_MSTMsg();
  262. MsgSmpSuccess.iMsgType = ENUM_MSG_TYPE.MSAMPLESTATUS;
  263. MsgSmpSuccess.STMSampleStu.iMsrSampleStatu = OTS_MSR_SAMPLE_STATUS.SUCCESSED;
  264. ProgressEvent(MsgSmpSuccess);
  265. // continue to the next sample
  266. listMeasuredSamples.Add (pSample.GetName());
  267. }
  268. // measurement completed
  269. m_ThreadStatus.SetStatus(OTS_MSR_THREAD_STATUS.COMPLETED);
  270. // record end time
  271. m_ThreadStatus.ComputeTime(OTS_THREAD_TIME_TYPE.STOPPED);
  272. ThreadOver();
  273. }
  274. //public bool DoReMeasure(List<Particle> particles, int imgscanspeed_index, int xrayscanmode_index, int scantime_count)
  275. //{
  276. // // got through measure list
  277. // foreach (var pSample in m_listMeasurableSamples)
  278. // {
  279. // // check and break if stop button is clicked
  280. // if (m_ThreadStatus.GetStatus() == OTS_MSR_THREAD_STATUS.STOPPED)
  281. // {
  282. // // stop button clicked
  283. // loger.Info("DoMeasure: stop button is clicked.");
  284. // // record end time
  285. // m_ThreadStatus.ComputeTime(OTS_THREAD_TIME_TYPE.STOPPED);
  286. // ThreadOver();
  287. // return false;
  288. // }
  289. // CSmplMeasure pSmplMeasure;
  290. // // create a sample measure object for the sample
  291. // switch (m_pProjData.m_nPackId)
  292. // {
  293. // case OTS_SysType_ID.IncA:
  294. // pSmplMeasure = new CSmplMeasureInclution(m_strWorkingFolder, pSample);
  295. // break;
  296. // case OTS_SysType_ID.CleannessA:
  297. // pSmplMeasure = new CSmplMeasureCleanliness(m_strWorkingFolder, pSample);
  298. // break;
  299. // default:
  300. // pSmplMeasure = new CSmplMeasureInclution(m_strWorkingFolder, pSample);
  301. // break;
  302. // }
  303. // // set measure thread
  304. // pSmplMeasure.SetMsrThread(this);
  305. // pSmplMeasure.DoMeasureForOneSample();
  306. // }
  307. // // measurement completed
  308. // m_ThreadStatus.SetStatus(OTS_MSR_THREAD_STATUS.COMPLETED);
  309. // // record end time
  310. // m_ThreadStatus.ComputeTime(OTS_THREAD_TIME_TYPE.STOPPED);
  311. // ThreadOver();
  312. // return true;
  313. //}
  314. // hole preview
  315. public void DoHolePreview(int a_nHoleID, CDomain a_pMeasureArea)
  316. {
  317. // start measurement, creat thread measure status class, let the main thread know that measurement started
  318. // set measure status to in-process
  319. //record time
  320. m_ThreadStatus.ComputeTime(OTS_THREAD_TIME_TYPE.START);
  321. DateTime timeStart = m_ThreadStatus.GetStartTime();
  322. ST_MSTMsg MsgMsrStart = new ST_MSTMsg();
  323. MsgMsrStart.iMsgType = ENUM_MSG_TYPE.MTHREADSTATUS;
  324. MsgMsrStart.STMThreadStu.iMsrStatu =OTS_MSR_THREAD_STATUS.INPROCESS;
  325. SendMessageToMeasureApp(MsgMsrStart);
  326. // connect hardware
  327. if (!m_SemController.Connect())
  328. {
  329. // failed to connect hardware
  330. SetMsrLoopStatus(otsdataconst.OTS_MSR_THREAD_STATUS.FAILED);
  331. m_ThreadStatus.ComputeTime(OTS_THREAD_TIME_TYPE.STOPPED);
  332. ThreadOverWithoutDisConnect();
  333. }
  334. COTSSample pSampleHole = CreateHoleSample(a_pMeasureArea);
  335. // create a sample measure object for the sample
  336. CSmplMeasure pSmplMeasure = new CSmplMeasureCleanliness(m_strWorkingFolder,pSampleHole);
  337. // set measure thread
  338. pSmplMeasure.SetMsrThread(this);
  339. // update thread measure status class, let the main thread know that this sample measurement starts
  340. // set working folder string
  341. pSmplMeasure.SetSample(pSampleHole);
  342. pSmplMeasure.SetWorkingFolder(m_strWorkingFolder);
  343. // do measure
  344. pSmplMeasure.DoHolePreview(a_nHoleID);
  345. // check if measurement is successful
  346. if (pSampleHole.GetMsrStatus().GetStatus() == OTS_MSR_SAMPLE_STATUS.STOPPED)
  347. {
  348. // record end time
  349. m_ThreadStatus.ComputeTime(OTS_THREAD_TIME_TYPE.STOPPED);
  350. // measurement stopped
  351. SetMsrLoopStatus(otsdataconst.OTS_MSR_THREAD_STATUS.STOPPED);
  352. // update thread measure status class, let the main thread know that this sample measurement stopped
  353. ST_MSTMsg MsgSmpStop = new ST_MSTMsg();
  354. MsgSmpStop.iMsgType = ENUM_MSG_TYPE.MSAMPLESTATUS;
  355. MsgSmpStop.STMSampleStu.iMsrSampleStatu = OTS_MSR_SAMPLE_STATUS.STOPPED;
  356. SendMessageToMeasureApp(MsgSmpStop);
  357. ThreadOverWithoutDisConnect();
  358. return;
  359. }
  360. else if (pSampleHole.GetMsrStatus().GetStatus() == OTS_MSR_SAMPLE_STATUS.FAILED)
  361. {
  362. // measurement failed
  363. SetMsrLoopStatus(otsdataconst.OTS_MSR_THREAD_STATUS.FAILED);
  364. // record end time
  365. m_ThreadStatus.ComputeTime(OTS_THREAD_TIME_TYPE.STOPPED);
  366. // update thread measure status class, let the main thread know that this sample measurement failed
  367. ST_MSTMsg MsgSmpFailed = new ST_MSTMsg();
  368. MsgSmpFailed.iMsgType = ENUM_MSG_TYPE.MSAMPLESTATUS;
  369. MsgSmpFailed.STMSampleStu.iMsrSampleStatu = OTS_MSR_SAMPLE_STATUS.FAILED;
  370. SendMessageToMeasureApp(MsgSmpFailed);
  371. ThreadOverWithoutDisConnect();
  372. return;
  373. }
  374. // record end time
  375. m_ThreadStatus.ComputeTime(OTS_THREAD_TIME_TYPE.STOPPED);
  376. // update thread measure status class, let the main thread know that this sample measurement successes
  377. ST_MSTMsg MsgSmpSuccess = new ST_MSTMsg();
  378. MsgSmpSuccess.iMsgType = ENUM_MSG_TYPE.MSAMPLESTATUS;
  379. MsgSmpSuccess.STMSampleStu.iMsrSampleStatu = OTS_MSR_SAMPLE_STATUS.SUCCESSED;
  380. SendMessageToMeasureApp(MsgSmpSuccess);
  381. // measurement completed
  382. SetMsrLoopStatus(otsdataconst.OTS_MSR_THREAD_STATUS.COMPLETED);
  383. // record end time
  384. m_ThreadStatus.ComputeTime(OTS_THREAD_TIME_TYPE.STOPPED);
  385. ThreadOverWithoutDisConnect();
  386. }
  387. public COTSSample CreateHoleSample(CDomain a_pMsrArea)
  388. {
  389. COTSSample pHoleSample = new COTSSample();
  390. pHoleSample.SetMsrArea(a_pMsrArea);
  391. // get min magnification
  392. CSEMStageData pSEMStageData = m_pProjData.GetSEMStageData();
  393. double dMinMag = pSEMStageData.GetMinMag();
  394. // get scan field size 100
  395. int nScanFieldSize100 = pSEMStageData.GetScanFieldSize100();
  396. // get working distance
  397. double dWorkingDistance = 0.0;
  398. if (!GetSEMWorkingDistanceFromHW(ref dWorkingDistance))
  399. {
  400. return null;
  401. }
  402. CSEMDataMsr poSEMDataMsr = new CSEMDataMsr();
  403. poSEMDataMsr.SetScanFieldSize100(nScanFieldSize100);
  404. poSEMDataMsr.SetWorkingDistance(dWorkingDistance);
  405. poSEMDataMsr.SetMagnification(dMinMag);
  406. pHoleSample.SetSEMDataMsr(poSEMDataMsr);
  407. // Set image scan param
  408. COTSImgScanPrm poImageScanParam = new COTSImgScanPrm();
  409. poImageScanParam.SetStopMode(((int)OTS_MEASURE_STOP_MODE.CoverMode).ToString());
  410. poImageScanParam.SetStartImageMode(OTS_GET_IMAGE_MODE.FROM_CENTER);
  411. poImageScanParam.SetScanImageSpeed(OTS_IMAGE_SCANSPEED_OPTIONS.low);
  412. //poImageScanParam.SetImagePixelSize(OTS_FIVE_TIES_OPTIONS.TIE1);
  413. CSampleParam poMsrParams = pHoleSample.GetMsrParams();
  414. poImageScanParam.SetImageResulotion(m_listMeasurableSamples[0].GetMsrParams().GetImageScanParam().GetImageResulotion());//由于各样品分辨率应该一致,故此处没有读取选取的特定样品孔样品
  415. poMsrParams.SetImageScanParam(poImageScanParam);
  416. pHoleSample.SetMsrParams(poMsrParams);
  417. return pHoleSample;
  418. }
  419. public bool GetSEMWorkingDistanceFromHW(ref double a_dWorkingDistance)
  420. {
  421. m_SemController.GetWorkingDistance(ref a_dWorkingDistance);
  422. return true;
  423. }
  424. // measure status
  425. public void SetMsrLoopStatus(otsdataconst.OTS_MSR_THREAD_STATUS a_nMsrLoopStatus)
  426. {
  427. m_ThreadStatus.SetStatus( a_nMsrLoopStatus);
  428. }
  429. }
  430. }