CSmplMeasure.cs 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using OTSModelSharp.ServiceInterface;
  7. using static OTSDataType.otsdataconst;
  8. using OTSDataType;
  9. using System.Diagnostics;
  10. using System.Drawing;
  11. using System.Threading;
  12. using OTSModelSharp.DTLBase;
  13. using OTSCLRINTERFACE;
  14. using OTSModelSharp.ServiceCenter;
  15. using OTSMeasureApp._0_OTSModel.Measure.ParamData;
  16. namespace OTSModelSharp
  17. {
  18. using CHoleBSEImgsList = List<CHoleBSEImg>;
  19. class CSmplMeasure
  20. {
  21. protected static NLog.Logger log ;
  22. protected bool bSaveThreadWorking;
  23. protected System.Threading.Thread m_thread_ptr;
  24. protected COTSSample m_Sample;
  25. protected CMeasure m_pMsrThread;
  26. string m_strWorkingFolder;
  27. protected CSmplMsrResult m_pSampleRstFile;
  28. CHoleBSEImgsList m_listHoleBSEImg;
  29. //CFunExportClass loger;
  30. protected SemController m_SemHardwareMgr;
  31. protected ScanController m_ScanHardwareMgr;
  32. protected EDSController m_EDSHardwareMgr;
  33. protected Queue<COTSFieldData> fieldQueue=new Queue<COTSFieldData>();
  34. protected COTSFieldData curFldData;
  35. protected IClassifyEngine m_classifyEngine;
  36. public CSmplMeasure()
  37. {
  38. log = NLog.LogManager.GetCurrentClassLogger();
  39. m_pSampleRstFile = new CSmplMsrResult();
  40. m_SemHardwareMgr = SemController.GetSEMController();
  41. m_ScanHardwareMgr = ScanController.GetScanController();
  42. m_EDSHardwareMgr = EDSController.GetEDSController();
  43. m_listHoleBSEImg = new CHoleBSEImgsList();
  44. }
  45. public CSmplMeasure( string a_strWorkingFolder, COTSSample a_pSample)
  46. {
  47. log = NLog.LogManager.GetCurrentClassLogger();
  48. m_strWorkingFolder = a_strWorkingFolder;
  49. m_pSampleRstFile = new CSmplMsrResult( a_strWorkingFolder, a_pSample);// new CSmplMsrResultFile(m_strWorkingFolder,a_pSample);
  50. m_SemHardwareMgr = SemController.GetSEMController();
  51. m_ScanHardwareMgr = ScanController.GetScanController();
  52. m_EDSHardwareMgr = EDSController.GetEDSController();
  53. m_listHoleBSEImg = new CHoleBSEImgsList();
  54. m_Sample = a_pSample;
  55. }
  56. public void SetSample(COTSSample a_pSample)
  57. {
  58. m_Sample = a_pSample;
  59. m_pSampleRstFile.SetSample(a_pSample);
  60. }
  61. public void SetMsrThread(CMeasure mt)
  62. {
  63. m_pMsrThread = mt;
  64. }
  65. bool SetSEMDataMrs()
  66. {
  67. var pSEMDataMsr = m_Sample.GetSEMDataMsr();
  68. //ASSERT(pSEMDataMsr);
  69. double dMag = pSEMDataMsr.GetMagnification();
  70. double dWorkDis = pSEMDataMsr.GetWorkingDistance();
  71. //CSemBasePtr pSEMCtrlPtr = GetSEMControl();
  72. var pSEMCtrl = m_pMsrThread.GetSEMController();
  73. // get SEM controller
  74. if (!pSEMCtrl.IsConnected())
  75. {
  76. if (!pSEMCtrl.Connect())
  77. {
  78. log.Error("SetSEMDataMrs: can't connect SEM.");
  79. return false;
  80. }
  81. }
  82. log.Info("set magnification:" + dMag.ToString());
  83. log.Info("set Working Distance:" + dWorkDis.ToString());
  84. pSEMCtrl.SetMagnification(dMag);
  85. pSEMCtrl.SetWorkingDistance(dWorkDis);
  86. return true;
  87. }
  88. bool SetSEMExteralOn()
  89. {
  90. var pSEMCtrl = m_SemHardwareMgr;
  91. // get SEM controller
  92. if (!pSEMCtrl.IsConnected())
  93. {
  94. if (!pSEMCtrl.Connect())
  95. {
  96. log.Error("SetSEMExteralOn: can't connect SEM.");
  97. return false;
  98. }
  99. }
  100. log.Warn("Set Scan Exteral on!");
  101. pSEMCtrl.SetScanExternal(true);
  102. return true;
  103. }
  104. // set SEM external off
  105. bool SetSEMExteralOff()
  106. {
  107. var pSEMCtrlPtr = m_SemHardwareMgr;
  108. // get SEM controller
  109. if (!pSEMCtrlPtr.IsConnected())
  110. {
  111. if (!pSEMCtrlPtr.Connect())
  112. {
  113. log.Error("SetSEMExteralOff: can't connect SEM.");
  114. return false;
  115. }
  116. }
  117. log.Warn("Set SEM Exteral Off!");
  118. pSEMCtrlPtr.SetScanExternal(false);
  119. return true;
  120. }
  121. bool SetBSEParam()
  122. {
  123. // get SEM controller
  124. var pSEMController = m_SemHardwareMgr;
  125. // get scan controller
  126. var pScanController = m_ScanHardwareMgr;
  127. // scan parameters
  128. var pMsrParam = m_Sample.GetMsrParams();
  129. var pImgScanParam = pMsrParam.GetImageScanParam();
  130. // get image size
  131. var nImageSizeId = pImgScanParam.GetImageResulotion();
  132. int nResulotionId = RESOLUTION_ID_FIRST_TIE + (int)nImageSizeId;
  133. Size sizePixelImage = RESOLUTION_VALUE[nResulotionId];
  134. // get SEM controller
  135. if (!pSEMController.IsConnected())
  136. {
  137. if (!pSEMController.Connect())
  138. {
  139. log.Error("SetBSEParam: can't connect SEM.");
  140. return false;
  141. }
  142. }
  143. // get dwell time
  144. OTS_IMAGE_SCANSPEED_OPTIONS nDwellTime = pImgScanParam.GetScanImageSpeed();
  145. // convert dwell time to bruker dwell time (8, 16, 32)
  146. int nBrukerDwellTimeId=3;
  147. switch (nDwellTime)
  148. {
  149. case OTS_IMAGE_SCANSPEED_OPTIONS.low:
  150. nBrukerDwellTimeId = 3;
  151. break;
  152. case OTS_IMAGE_SCANSPEED_OPTIONS.meddium:
  153. nBrukerDwellTimeId = 4;
  154. break;
  155. case OTS_IMAGE_SCANSPEED_OPTIONS.high:
  156. nBrukerDwellTimeId = 5;
  157. break;
  158. default:
  159. nBrukerDwellTimeId = 3;
  160. break;
  161. }
  162. long nBrukerDwellTime = OTSDataType.otsdataconst.DWELLTIME_BRUKER_VALUES[nBrukerDwellTimeId];
  163. if (!pScanController.Init())
  164. {
  165. log.Error("SetBSEParam: failed to get scan control.");
  166. return false;
  167. }
  168. // set dwell time
  169. if (!pScanController.SetDwellTime(nBrukerDwellTime))
  170. {
  171. log.Error("SetBSEParam: failed to set dwell time (%d) for bruker system.", nBrukerDwellTime);
  172. return false;
  173. }
  174. // set image size
  175. if (!pScanController.SetImageSize(sizePixelImage.Width,sizePixelImage.Height))
  176. {
  177. // failed to set dwell time
  178. log.Error("SetBSEParam: failed to set dwell time (%d).", sizePixelImage.Height);
  179. return false;
  180. }
  181. return true;
  182. }
  183. bool SetHoleBSEParam()
  184. {
  185. // get SEM controller
  186. var pSEMController = m_SemHardwareMgr;
  187. // get scan controller
  188. var pScanController = m_ScanHardwareMgr;
  189. // scan parameters
  190. var pMsrParam = m_Sample.GetMsrParams();
  191. var pImgScanParam = pMsrParam.GetImageScanParam();
  192. // get image size
  193. var nImageSizeId = pImgScanParam.GetImageResulotion();
  194. int nResulotionId = RESOLUTION_ID_FIRST_TIE + (int)nImageSizeId;
  195. Size sizePixelImage = RESOLUTION_VALUE[nResulotionId];
  196. // get SEM controller
  197. if (!pSEMController.IsConnected())
  198. {
  199. if (!pSEMController.Connect())
  200. {
  201. log.Error("SetBSEParam: can't connect SEM.");
  202. return false;
  203. }
  204. }
  205. // get dwell time
  206. OTS_IMAGE_SCANSPEED_OPTIONS nDwellTime = pImgScanParam.GetScanImageSpeed();
  207. // convert dwell time to bruker dwell time (6, 16, 32)
  208. long nBrukerDwellTime = DWELLTIME_BRUKER_VALUES[2];// choose the third option, so the dwell time will be 4 . there's no need to change here. it shoud be a const.
  209. if (!pScanController.Init())
  210. {
  211. log.Error("SetBSEParam: failed to get scan control.");
  212. return false;
  213. }
  214. // set dwell time
  215. if (!pScanController.SetDwellTime(nBrukerDwellTime))
  216. {
  217. log.Error("SetBSEParam: failed to set dwell time (%d) for bruker system.", nBrukerDwellTime);
  218. return false;
  219. }
  220. // set image size
  221. if (!pScanController.SetImageSize(sizePixelImage.Width/2, sizePixelImage.Height/2))
  222. {
  223. // failed to set dwell time
  224. log.Error("SetBSEParam: failed to set dwell time (%d).", sizePixelImage.Height);
  225. return false;
  226. }
  227. return true;
  228. }
  229. public void SetWorkingFolder(String a_strWorkingFolder)
  230. {
  231. // add "\\" at the string end if it is not "\\"
  232. if (a_strWorkingFolder.PadRight(1)!="\\")
  233. {
  234. a_strWorkingFolder += "\\";
  235. }
  236. m_strWorkingFolder = a_strWorkingFolder + m_Sample.GetName() + "\\";
  237. }
  238. bool CalculateUnMeasuredFieldsCenters(out List<System.Drawing.Point> a_allpieldcenter,out List<System.Drawing.Point> a_listFieldCenter)
  239. {
  240. // sample measure parameters
  241. CSampleParam pMsrParam = m_Sample.GetMsrParams();
  242. COTSImgScanPrm poImageScanParam = pMsrParam.GetImageScanParam();
  243. CSEMDataMsr poSEMDataMsr = m_Sample.GetSEMDataMsr();
  244. CMsrSampleStatus pStatus = m_Sample.GetMsrStatus();
  245. // measured field centers list
  246. List<System.Drawing.Point> listCompletedCenter = pStatus.GetCompletedFieldsCenter();
  247. // field centers list manager
  248. CFieldPositionMgr pFieldMgr = new CFieldPositionMgr();
  249. // init field centers list manager
  250. if (!pFieldMgr.Init(m_Sample.GetMsrArea(), poImageScanParam, poSEMDataMsr, listCompletedCenter))
  251. {
  252. log.Error("CalculateFieldsCenters: failed to init field centres list manager.");
  253. a_listFieldCenter = new List<System.Drawing.Point>();
  254. a_allpieldcenter = new List<Point>();
  255. return false;
  256. }
  257. // get field centers list
  258. a_listFieldCenter = pFieldMgr.GetUnmeasuredFieldCentrePoints();// GetFieldCentrePoints();
  259. a_allpieldcenter = pFieldMgr.GetFieldCentrePoints();
  260. // ok, return TRUE
  261. return true;
  262. }
  263. protected bool IsAborted()
  264. {
  265. //Debug.Assert(m_pMsrThread == null);
  266. return m_pMsrThread.IsMeasureStopped();
  267. }
  268. bool IsSampleOver(COTSImgScanPrm a_pScanParam, int a_nTotalFields)
  269. {
  270. string sStopMode = a_pScanParam.GetStopMode();
  271. int nStopField = a_pScanParam.GetStopParamFields();
  272. // completed fields number
  273. CMsrSampleStatus pMsrSampleStatus = m_Sample.GetMsrStatus();
  274. int nCompeltedField = pMsrSampleStatus.GetCompletedFields();
  275. CMsrDisplayResults pMsrResults = m_Sample.GetMsrResults();
  276. List<CMsrResultItem> listMsrResult = pMsrResults.GetResultItems();
  277. int nNumParticle = 0;
  278. foreach (var pResult in listMsrResult)
  279. {
  280. if (pResult.GetTypeId() > (int)OTS_PARTCLE_TYPE.NO_ANALYSIS_X_RAY)//summarize the number of the identified particle in this condition
  281. {
  282. nNumParticle += (int)pResult.GetNumber();
  283. }
  284. }
  285. TimeSpan timeSpan = pMsrSampleStatus.GetUsedTime();
  286. int nDay = timeSpan.Days;
  287. int nHour = timeSpan.Hours;
  288. int nMin = timeSpan.Minutes;
  289. int nSec = timeSpan.Seconds;
  290. int nUsedTime = nSec + nMin * 60 + nHour * 3600 + nDay * 86400;
  291. int nParticlAim = a_pScanParam.GetStopParamParticles();
  292. int nMeasTimeAim = a_pScanParam.GetStopParamMeasTime();
  293. bool bRet = true;
  294. string[] str = sStopMode.Replace(" ", "").Split('+');
  295. for(int i=0;i< str.Length;i++)
  296. {
  297. switch (int.Parse(str[i].Split(':')[0]))
  298. {
  299. case (int)OTS_MEASURE_STOP_MODE.CoverMode:
  300. // completed fields number
  301. if (nCompeltedField < a_nTotalFields)
  302. {
  303. bRet = false;
  304. }
  305. break;
  306. case (int)OTS_MEASURE_STOP_MODE.FieldMode:
  307. if (nCompeltedField < nStopField)
  308. {
  309. bRet = false;
  310. }
  311. break;
  312. case (int)OTS_MEASURE_STOP_MODE.ParticleMode:
  313. if (nNumParticle < nParticlAim)
  314. {
  315. bRet = false;
  316. }
  317. break;
  318. case (int)OTS_MEASURE_STOP_MODE.TimeMode:
  319. if (nUsedTime < nMeasTimeAim)
  320. {
  321. bRet = false;
  322. }
  323. break;
  324. default:
  325. break;
  326. }
  327. }
  328. return bRet;
  329. }
  330. // move SEM to the point
  331. bool MoveSEMToPoint(System.Drawing.Point a_poi)
  332. {
  333. // get SEM controller
  334. var pSEMController = m_SemHardwareMgr;
  335. Point a_SEMpt = new Point();
  336. CSEMStageData a_pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  337. int hardWareDelay = a_pCSEMStageData.GetHardWareDelay();
  338. if (!a_pCSEMStageData.ConverOTSToSEMPoint(a_poi, ref a_SEMpt))
  339. {
  340. return false;
  341. }
  342. if (!pSEMController.IsConnected())
  343. {
  344. if (!pSEMController.Connect())
  345. {
  346. log.Error("MoveSEMToPoint: can't connect SEM.");
  347. return false;
  348. }
  349. }
  350. log.Info("Begin to move SEM stage to " + a_SEMpt.X + "," + a_SEMpt.Y);
  351. // move SEM to the position (rotation 0)
  352. if (!pSEMController.MoveSEMToPoint(a_SEMpt))
  353. {
  354. log.Error("MoveSEMToPoint: failed to call MoveSEMToPoint method.");
  355. return false;
  356. }
  357. Thread.Sleep(hardWareDelay);
  358. return true;
  359. }
  360. // Acquire a BSE image
  361. CBSEImgClr AcquireABSEImage()
  362. {
  363. // BSE image
  364. CBSEImgClr pBSEImage = null;
  365. // get scan controller
  366. var pScanController = m_ScanHardwareMgr;
  367. pBSEImage = pScanController.AcquireBSEImage(0, 0, 2);
  368. return pBSEImage;
  369. }
  370. //public virtual bool FieldImageProcess(Point fldCenter, CBSEImgClr imageData)
  371. //{
  372. // return true;
  373. //}
  374. public virtual void ClassifyFieldParticles()
  375. {
  376. return;
  377. }
  378. public virtual void ClassifyMergedParticles(List<COTSParticleClr> parts)
  379. {
  380. return;
  381. }
  382. private class AutoResetSEMControl:IDisposable
  383. {
  384. CSmplMeasure sm;
  385. public AutoResetSEMControl(CSmplMeasure s)
  386. {
  387. sm = s;
  388. }
  389. public void Dispose()
  390. {
  391. sm.SetSEMExteralOff();
  392. }
  393. }
  394. public void DoMeasureForOneSample()
  395. {
  396. using (AutoResetSEMControl autoReset = new AutoResetSEMControl(this)) //when this method exit ,the SetSEMExternalOff and ResetScan will be called automatically.
  397. {
  398. // let the main thread to know that this sample measurement starts
  399. var pStatus = m_Sample.GetMsrStatus();
  400. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.INPROCESS);
  401. // set current time to current time
  402. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.START);
  403. // let main App know that the sample begin to measure
  404. ST_MSTMsg MsgSmpStart = new ST_MSTMsg();
  405. MsgSmpStart.iMsgType = ENUM_MSG_TYPE.MSAMPLESTATUS;
  406. MsgSmpStart.STMSampleStu.iMsrSampleStatu = OTS_MSR_SAMPLE_STATUS.INPROCESS;
  407. MsgSmpStart.STMSampleStu.cSampleName = m_Sample.GetName();
  408. m_pMsrThread.SendMessageToMeasureApp(MsgSmpStart);
  409. log.Info(m_Sample.GetName() + " Measurement started!");
  410. // get SEM controller to set magnification and working distance
  411. if (!SetSEMDataMrs())
  412. {
  413. log.Error("DoMeasure: fail to set SEM data.");
  414. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  415. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  416. return;
  417. }
  418. // set the BSE scan param
  419. if (!SetBSEParam())
  420. {
  421. log.Error("DoMeasure: fail to set BSE param.");
  422. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  423. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  424. return;
  425. }
  426. var pSEMDataGnr = new CSEMDataGnr();
  427. log.Info("Get Kv, Brightness and Contrast!");
  428. m_SemHardwareMgr.GetSEMDataGnrFromHw(ref pSEMDataGnr);
  429. m_pSampleRstFile.SetSEMGnr(pSEMDataGnr);
  430. // record SEM data
  431. COTSMsrPrjResultData pProjMgrFile = m_pMsrThread.GetProjResultData();
  432. CSEMStageData pSEMStageData = pProjMgrFile.GetSEMStageData();
  433. m_pSampleRstFile.SetSEMStageData(pSEMStageData);
  434. // record stage
  435. CStage pStage = pProjMgrFile.GetStage();
  436. m_pSampleRstFile.SetSEMStage(pStage);
  437. // calculate field centers
  438. List<System.Drawing.Point> umMeasuredlistFieldCenter;
  439. List<System.Drawing.Point> alllistFieldCenter;
  440. if (!CalculateUnMeasuredFieldsCenters(out alllistFieldCenter,out umMeasuredlistFieldCenter))
  441. {// failed to calculate field centers
  442. log.Error("DoMeasure: failed to calculate field centers.");
  443. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  444. // record end time
  445. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  446. return;
  447. }
  448. //-----save the static measure result file data into xml file and the dynamic data of every field will be saved into sqlite database
  449. log.Info("Create result file!");
  450. if (!m_pSampleRstFile.CreateResultFiles())
  451. {// failed to call measure result file Save method
  452. log.Error("DoMeasure: failed to call measure result file Save method.");
  453. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  454. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  455. return;
  456. }
  457. //------
  458. int nNewFieldId = 0;
  459. int numOfAllFields = pStatus.GetCompletedFields() + umMeasuredlistFieldCenter.Count;//
  460. int completedFields = pStatus.GetCompletedFields();
  461. log.Info("Unmeasured fields:" + umMeasuredlistFieldCenter.Count);
  462. for (int i = 0; i < (int)umMeasuredlistFieldCenter.Count; ++i)
  463. {// check and break if stop button is clicked
  464. if (IsAborted())
  465. {// measure stopped
  466. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.STOPPED);
  467. // record end time
  468. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  469. //must wait for the saving data thread finished,or we'll get null pointer exception when we stop the measure process.
  470. while (fieldQueue.Count() > 0)
  471. {
  472. Thread.Sleep(100);
  473. }
  474. return;
  475. }
  476. // check if sample measurement completes
  477. COTSImgScanPrm pScanParam = m_Sample.GetMsrParams().GetImageScanParam();
  478. int nTotalFieldSize = (int)umMeasuredlistFieldCenter.Count;
  479. if (IsSampleOver(pScanParam, numOfAllFields))
  480. {
  481. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  482. break;
  483. }
  484. // get a field center
  485. System.Drawing.Point poiFieldCentre = umMeasuredlistFieldCenter[i];
  486. // update thread measure status class, let the main thread know that starts a new field
  487. ST_MSTMsg MsgFieldStart = new ST_MSTMsg();
  488. MsgFieldStart.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  489. MsgFieldStart.STMSampleRetData.iRetDataType = MSAMPLE_RET.START_MSR_FIELD;
  490. MsgFieldStart.STMSampleRetData.SMsrField.FieldPos = poiFieldCentre;
  491. m_pMsrThread.SendMessageToMeasureApp(MsgFieldStart);
  492. int fldNo = completedFields + i + 1;
  493. log.Warn("Current field:" + fldNo.ToString());
  494. // move SEM to the field center
  495. if (!MoveSEMToPoint(poiFieldCentre))
  496. {// failed to move SEM to the position
  497. log.Error("DoMeasure: failed to move SEM to the field centre point.");
  498. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  499. // record end time
  500. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  501. return;
  502. }
  503. log.Info("Begin to Acquire BSE image!");
  504. // take BSE image for the fields
  505. CBSEImgClr pBSEImg = AcquireABSEImage();
  506. // let the main thread to know that image process is completed
  507. if (pBSEImg == null)
  508. {
  509. log.Error("ImageProcess: can't get no background image.");
  510. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  511. return;
  512. }
  513. //BSEData
  514. ST_MSTMsg MsgFieldBSE = new ST_MSTMsg();
  515. MsgFieldBSE.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  516. MsgFieldBSE.STMSampleRetData.iRetDataType = MSAMPLE_RET.BSE_DATA;
  517. MsgFieldBSE.STMSampleRetData.BSEData.pos.X = poiFieldCentre.X;
  518. MsgFieldBSE.STMSampleRetData.BSEData.pos.Y = poiFieldCentre.Y;
  519. MsgFieldBSE.STMSampleRetData.BSEData.iBSEDataHeight = pBSEImg.GetHeight();
  520. MsgFieldBSE.STMSampleRetData.BSEData.iBSEDataWidth = pBSEImg.GetWidth();
  521. byte[] pImgData = pBSEImg.GetImageDataPtr();
  522. MsgFieldBSE.STMSampleRetData.BSEData.lpBSEData = pImgData;
  523. m_pMsrThread.SendMessageToMeasureApp(MsgFieldBSE);
  524. // get a new field id
  525. log.Info("Acquire BSE image success! Processing image...");
  526. // image process
  527. var rst = FieldImageProcess(poiFieldCentre, pBSEImg);
  528. if (rst == true)
  529. {
  530. // add the field into the field
  531. m_pSampleRstFile.AddAField(curFldData);
  532. }
  533. ++nNewFieldId;
  534. double measuredArea = 0; // this area should be the field area
  535. var a_pBSEImg = curFldData.GetBSEImage();
  536. double dPixelSize = m_Sample.CalculatePixelSize();
  537. measuredArea = a_pBSEImg.GetHeight() * a_pBSEImg.GetWidth()* dPixelSize * dPixelSize + 0.5; //Get measured area
  538. if (!CumulateFieldData(curFldData.GetListAnalysisParticles(), measuredArea))
  539. { // failed to call SaveFieldFile method
  540. log.Error("ImageProcess: call CumulateFieldData method.");
  541. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  542. return;
  543. }
  544. log.Info("Send field data to screen!");
  545. // completed fields
  546. pStatus.SetCompletedFields(pStatus.GetCompletedFields() + 1);
  547. // completed fieldCenter
  548. List<System.Drawing.Point> listCpltedCenter = pStatus.GetCompletedFieldsCenter();
  549. listCpltedCenter.Add(poiFieldCentre);
  550. //Field Data
  551. // record end time
  552. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  553. ST_MSTMsg MsgFieldEnd = new ST_MSTMsg();
  554. MsgFieldEnd.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  555. MsgFieldEnd.STMSampleRetData.iRetDataType = MSAMPLE_RET.FIELD_DATA;
  556. MsgFieldEnd.STMSampleRetData.SFieldData.iCompleteFieldCount = pStatus.GetCompletedFields();
  557. MsgFieldEnd.STMSampleRetData.SFieldData.iMeasureFieldCount = numOfAllFields;
  558. MsgFieldEnd.STMSampleRetData.SFieldData.iSParticleCount = (int)curFldData.GetListAnalysisParticles().Count;
  559. MsgFieldEnd.STMSampleRetData.SFieldData.FieldPos.X = Convert.ToInt32(poiFieldCentre.X);
  560. MsgFieldEnd.STMSampleRetData.SFieldData.FieldPos.Y = Convert.ToInt32(poiFieldCentre.Y);
  561. m_pMsrThread.SendMessageToMeasureApp(MsgFieldEnd);
  562. }
  563. while (bSaveThreadWorking)//wait untill all the field data has been saved.
  564. {
  565. Thread.Sleep(1000);
  566. log.Warn("db saving!");
  567. }
  568. //merging particles
  569. log.Info("Merging big particles which are crossing the field edge!");
  570. CImageHandler imgpro = new CImageHandler();
  571. int scanfldsize = m_Sample.GetSEMDataMsr().GetScanFieldSize();
  572. List<COTSParticleClr> mergedParticles = new List<COTSParticleClr>();
  573. double pixelSize = m_Sample.CalculatePixelSize();
  574. imgpro.MergeBigBoundaryParticles(m_pSampleRstFile.GetFieldData(), pixelSize, scanfldsize, m_Sample.GetResolutionSize(), ref mergedParticles);
  575. CalculateMergedPartProperty(mergedParticles,pixelSize);
  576. ClassifyMergedParticles(mergedParticles);
  577. log.Info("begin merged particle data db saving...");
  578. SaveMergedParticles(mergedParticles);
  579. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.COMPLT);
  580. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  581. // let main thread to know that this sample measurement completes
  582. ST_MSTMsg MsgSmplEnd = new ST_MSTMsg();
  583. MsgSmplEnd.iMsgType = ENUM_MSG_TYPE.MSAMPLESTATUS;
  584. MsgSmplEnd.STMSampleStu.iMsrSampleStatu = OTS_MSR_SAMPLE_STATUS.SUCCESSED;
  585. MsgSmplEnd.STMSampleStu.cSampleName = m_Sample.GetName();
  586. m_pMsrThread.SendMessageToMeasureApp(MsgSmplEnd);
  587. }
  588. }
  589. private void CalculateMergedPartProperty(List<COTSParticleClr> mergedParticles,double pixelSize)
  590. {
  591. var m_ImagePro = new CImageHandler();
  592. foreach (COTSParticleClr part in mergedParticles)
  593. {
  594. m_ImagePro.CalParticleImageProp(part, pixelSize);
  595. }
  596. }
  597. public bool FieldImageProcess(Point fldCenter, CBSEImgClr a_pBSEImg)
  598. {
  599. int nNewFieldId;
  600. nNewFieldId = m_pSampleRstFile.GetIdForANewField();
  601. // create a field
  602. curFldData = new COTSFieldData(a_pBSEImg, m_Sample.CalculatePixelSize());
  603. curFldData.SetId(nNewFieldId);
  604. curFldData.SetOTSPosition(fldCenter);
  605. CSEMStageData a_pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  606. Point semPos = new Point();
  607. a_pCSEMStageData.ConverOTSToSEMPoint(fldCenter, ref semPos);
  608. curFldData.SemPos = semPos;
  609. //first step:remove background of the bse image and compound all the finded particles.
  610. log.Info("Begin to process image and get all particles!");
  611. GetOriginalParticles();
  612. // second step :filter the finded particles.
  613. log.Info("Begin to filter particles!");
  614. FilterParticles(curFldData);
  615. CalculateParticleAbsolutPos();
  616. log.Info("Begin to Calculate the image property of every particle!");
  617. var analysisparts = curFldData.GetListAnalysisParticles();
  618. curFldData.CalParticleImageProp(analysisparts);//calculate particle image property such as feret diameter, DMAX etc.
  619. COTSXRayParam pXRayParam = m_Sample.GetMsrParams().GetXRayParam();
  620. if (pXRayParam.GetUsingXray() == true)
  621. {
  622. log.Info("Begin to collect particle's xray data!");
  623. Thread.Sleep(100);
  624. CollectParticlesXrayData(curFldData);
  625. Thread.Sleep(100);
  626. }
  627. log.Info("Begin to classify particles! particle num:" + curFldData.GetListAnalysisParticles().Count);
  628. ClassifyFieldParticles();
  629. // save field files
  630. m_Sample.GetMsrStatus().SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  631. StartSaveFileThread(curFldData);
  632. return true;
  633. }
  634. public void GetOriginalParticles()
  635. {
  636. // measure status
  637. CMsrSampleStatus pStatus = m_Sample.GetMsrStatus();
  638. // get image process parameter
  639. CSampleParam pMsrParam = m_Sample.GetMsrParams();
  640. COTSImageProcParam pImgProcessParam = pMsrParam.GetImageProcessParam();
  641. var specialPartsparam = pMsrParam.GetSpecialGrayRangeParam();
  642. var pixelsize = m_Sample.CalculatePixelSize();
  643. if (specialPartsparam.GetIsToRun())
  644. {
  645. List<CSpecialGrayRange> ranges = pMsrParam.GetSpecialGrayRangeParam().GetSpecialGreyRanges();
  646. foreach (var grayRange in ranges)
  647. {
  648. CIntRangeClr range = new CIntRangeClr(grayRange.range.GetStart(), grayRange.range.GetEnd());
  649. CDoubleRangeClr diaRange = new CDoubleRangeClr(grayRange.diameterRange.GetStart(), grayRange.diameterRange.GetEnd());
  650. curFldData.GetPartsBySpecialGray(range, diaRange, pixelsize, grayRange.ifCollectXray);
  651. }
  652. }
  653. var ifCollectxray = pMsrParam.GetXRayParam().GetUsingXray();
  654. // remove BES image background
  655. curFldData.RemoveImgBGAndGetParticles(pImgProcessParam, pixelsize, ifCollectxray);
  656. // check if this is an empty image
  657. if (curFldData.NoParticle())
  658. { // empty fields
  659. log.Info("ImageProcess: empty field.");
  660. }
  661. return;
  662. }
  663. public void FilterParticles(COTSFieldData fld)
  664. {
  665. if (fld.NoAnalysisParticle())
  666. {
  667. log.Warn("There's no analysis particles!");
  668. return;
  669. }
  670. CSampleParam pMsrParam = m_Sample.GetMsrParams();
  671. COTSImageProcParam pImgProcessParam = pMsrParam.GetImageProcessParam();
  672. double dPixelSize = m_Sample.CalculatePixelSize();
  673. curFldData.InitParticles(pImgProcessParam, dPixelSize);
  674. var listXray = curFldData.GetAllParticles().OrderByDescending(x => x.GetActualArea()).ToList();
  675. var listXray1 = new List<COTSParticleClr>();
  676. var pXRayParam = pMsrParam.GetXRayParam();
  677. if (listXray.Count > pXRayParam.GetXrayLimit())
  678. {
  679. for (var i = 0; i < pXRayParam.GetXrayLimit(); i++)
  680. {
  681. listXray1.Add(listXray[i]);
  682. }
  683. }
  684. else
  685. {
  686. listXray1 = listXray;
  687. }
  688. curFldData.SetListAnalysisParticles(listXray1);
  689. log.Info("Analysis particles:"+ listXray1.Count);
  690. //2) according to the quantify threshold size value saperate the analysis particles into two group :the bigparticles and the smallparticles.
  691. double quantifyThreshold = m_Sample.GetMsrParams().GetXRayParam().GetFeatureModeMinSize();
  692. var smallparts = fld.ListSmallParticles;
  693. var bigparts = fld.ListBigParticles;
  694. foreach (var part in listXray1)
  695. {
  696. double equalCircleDiameter = Math.Sqrt(part.GetActualArea() / 3.14159) * 2f;
  697. if (equalCircleDiameter < quantifyThreshold)
  698. {
  699. smallparts.Add(part);
  700. }
  701. else
  702. {
  703. bigparts.Add(part);
  704. }
  705. }
  706. fld.ListSmallParticles = smallparts;
  707. fld.ListBigParticles = bigparts;
  708. //fld.SmallParticlePercentage=percentage;
  709. log.Info("SmallQuantifyParts (<" + quantifyThreshold.ToString("f2") + "): " + smallparts.Count);
  710. log.Info("BigQuantifyParts (>=" + quantifyThreshold.ToString("f2") + "): " + bigparts.Count);
  711. return;
  712. }
  713. public void CollectParticlesXrayData(COTSFieldData fld)
  714. {
  715. // get x-ray parameters
  716. COTSXRayParam pXRayParam = m_Sample.GetMsrParams().GetXRayParam();
  717. // calculate search x-ray acquire time
  718. uint nXRayAQTime;
  719. List<COTSParticleClr> smallparts = new List<COTSParticleClr>();
  720. List<COTSParticleClr> bigparts = new List<COTSParticleClr>();
  721. foreach (var p in fld.ListSmallParticles)
  722. {
  723. if (p.IsXrayParticle())
  724. {
  725. smallparts.Add(p);
  726. }
  727. }
  728. foreach (var p in fld.ListBigParticles)
  729. {
  730. if (p.IsXrayParticle())
  731. {
  732. bigparts.Add(p);
  733. }
  734. }
  735. var pStatus = m_Sample.GetMsrStatus();
  736. curFldData.CreateXrayList(bigparts); // big particle using the full xray strategy.
  737. curFldData.CreateXrayList(smallparts); //small particle using the fast xray strategy
  738. if (bigparts.Count > 0)
  739. {
  740. var workmode = pXRayParam.GetScanMode();
  741. // get x-ray list (analysis) by particle features
  742. if (workmode == OTS_X_RAY_SCAN_MODE.FeatureMode)
  743. {
  744. nXRayAQTime = (uint)pXRayParam.GetMidAnalyAQTime();
  745. m_EDSHardwareMgr.GetXRayByFeatures(bigparts, nXRayAQTime, true);
  746. }
  747. else
  748. {
  749. nXRayAQTime = (uint)pXRayParam.GetMidAnalyAQTime();
  750. m_EDSHardwareMgr.GetXRayByPoints(bigparts, nXRayAQTime, true);
  751. }
  752. }
  753. if (smallparts.Count > 0)
  754. {
  755. nXRayAQTime = (uint)pXRayParam.GetFastXrayTime();
  756. // get x-ray list (analysis) by points
  757. m_EDSHardwareMgr.GetXRayByPoints(smallparts, nXRayAQTime, true);
  758. }
  759. return;
  760. }
  761. public void DoHolePreview(int a_nHoleID)
  762. {
  763. using (AutoResetSEMControl autoReset = new AutoResetSEMControl(this))
  764. {
  765. // let the main thread to know that this sample measurement starts
  766. CMsrSampleStatus pStatus = m_Sample.GetMsrStatus();
  767. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.INPROCESS);
  768. // set current time to current time
  769. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.START);
  770. // let main App know that the sample begin to measure
  771. ST_MSTMsg MsgSmpStart=new ST_MSTMsg();
  772. MsgSmpStart.iMsgType = ENUM_MSG_TYPE.MSAMPLESTATUS;
  773. MsgSmpStart.STMSampleStu.iMsrSampleStatu = OTS_MSR_SAMPLE_STATUS.INPROCESS;
  774. MsgSmpStart.STMSampleStu.cSampleName=m_Sample.GetName();
  775. // get SEM controller to set magnification and working distance
  776. if (!SetSEMDataMrs())
  777. {
  778. log.Error("DoHolePreview: fail to set SEM data.");
  779. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  780. // record end time
  781. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  782. return;
  783. }
  784. // set the BSE scan param
  785. if (!SetHoleBSEParam())
  786. {
  787. log.Error("DoMeasure: fail to set BSE param.");
  788. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  789. // record end time
  790. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  791. return;
  792. }
  793. // check if measurement is aborted
  794. if (IsAborted())
  795. {// measurement aborted
  796. log.Trace("DoHolePreview: measurement aborted before get SEM condition.");
  797. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.STOPPED);
  798. // record end time
  799. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  800. return;
  801. }
  802. // calculate field centers
  803. List<System.Drawing.Point> listFieldCenter=new List<System.Drawing.Point>();
  804. List<System.Drawing.Point> alllistFieldCenter = new List<System.Drawing.Point>();
  805. // listFieldCenter.clear();
  806. if (!CalculateUnMeasuredFieldsCenters(out alllistFieldCenter,out listFieldCenter))
  807. {// failed to calculate field centers
  808. log.Error("DoHolePreview: failed to calculate field centers.");
  809. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  810. // record end time
  811. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  812. return;
  813. }
  814. // go through each field
  815. int nNewFieldId = 0;
  816. for ( int i = 0; i < listFieldCenter.Count; ++i)
  817. {// check and break if stop button is clicked
  818. if (IsAborted())
  819. {// measure stopped
  820. log.Trace("DoHolePreview: measure thread is stopped.");
  821. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.STOPPED);
  822. // record end time
  823. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  824. return;
  825. }
  826. // check if sample measurement completes
  827. COTSImgScanPrm pScanParam = m_Sample.GetMsrParams().GetImageScanParam();
  828. int nTotalFieldSize = listFieldCenter.Count;
  829. if (IsSampleOver(pScanParam, nTotalFieldSize))
  830. {
  831. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  832. break;
  833. }
  834. // get a field center
  835. System.Drawing.Point poiFieldCentre = listFieldCenter[i];
  836. // update thread measure status class, let the main thread know that starts a new field
  837. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  838. ST_MSTMsg MsgFieldStart=new ST_MSTMsg();
  839. MsgFieldStart.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  840. MsgFieldStart.STMSampleRetData.iRetDataType = MSAMPLE_RET.START_MSR_FIELD;
  841. MsgFieldStart.STMSampleRetData.SMsrField.FieldPos = poiFieldCentre;
  842. m_pMsrThread.SendMessageToMeasureApp(MsgFieldStart);
  843. // move SEM to the field center
  844. if (!MoveSEMToPoint(poiFieldCentre))
  845. {// failed to move SEM to the position
  846. log.Error("DoHolePreview: failed to move SEM to the field centre point.");
  847. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  848. // record end time
  849. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  850. return;
  851. }
  852. // take BSE image for the fields
  853. CBSEImgClr pBSEIamge = AcquireABSEImage();
  854. if (pBSEIamge==null)
  855. {
  856. // failed to acquire a BSE image
  857. log.Error("DoHolePreview: failed to acquire a BSE image.");
  858. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  859. // record end time
  860. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  861. return;
  862. }
  863. //BSEData
  864. ST_MSTMsg MsgFieldBSE=new ST_MSTMsg();
  865. // memset(&MsgFieldBSE, 0, sizeof(ST_MSTMsg));
  866. MsgFieldBSE.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  867. MsgFieldBSE.STMSampleRetData.iRetDataType = MSAMPLE_RET.BSE_DATA;
  868. MsgFieldBSE.STMSampleRetData.BSEData.pos = poiFieldCentre;
  869. //MsgFieldBSE.STMSampleRetData.BSEData.pos.y = poiFieldCentre.y;
  870. MsgFieldBSE.STMSampleRetData.BSEData.iBSEDataHeight = pBSEIamge.GetHeight();
  871. MsgFieldBSE.STMSampleRetData.BSEData.iBSEDataWidth = pBSEIamge.GetWidth();
  872. byte[] pImgData = pBSEIamge.GetImageDataPtr();
  873. MsgFieldBSE.STMSampleRetData.BSEData.lpBSEData = pImgData;
  874. m_pMsrThread.SendMessageToMeasureApp(MsgFieldBSE);
  875. if (pStatus.GetStatus() != OTS_MSR_SAMPLE_STATUS.INPROCESS)
  876. {
  877. // measurement failed or stopped
  878. log.Error("DoHolePreview: measurement failed or stopped.");
  879. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  880. // record end time
  881. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  882. return;
  883. }
  884. //save the result to project file
  885. Rectangle oImageRect = (Rectangle)pBSEIamge.GetImageRect();
  886. CHoleBSEImg pHoleBSEImg =new CHoleBSEImg(oImageRect,a_nHoleID, poiFieldCentre);
  887. pHoleBSEImg.SetImageData( pBSEIamge.GetImageDataPtr(),oImageRect.Width,oImageRect.Height);
  888. m_listHoleBSEImg.Add(pHoleBSEImg);
  889. COTSMsrPrjResultData pProjMgrFile = m_pMsrThread.GetProjResultData();
  890. pProjMgrFile.SetHoleBESImgList(m_listHoleBSEImg,true);
  891. // prepare for the next
  892. ++nNewFieldId;
  893. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  894. ST_MSTMsg MsgFieldEnd=new ST_MSTMsg();
  895. MsgFieldEnd.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  896. MsgFieldEnd.STMSampleRetData.iRetDataType = MSAMPLE_RET.FIELD_DATA;
  897. MsgFieldEnd.STMSampleRetData.SFieldData.iCompleteFieldCount = (i + 1);
  898. MsgFieldEnd.STMSampleRetData.SFieldData.iMeasureFieldCount = listFieldCenter.Count;
  899. MsgFieldEnd.STMSampleRetData.SFieldData.iSParticleCount = 0;
  900. MsgFieldEnd.STMSampleRetData.SFieldData.FieldPos = poiFieldCentre;
  901. m_pMsrThread.SendMessageToMeasureApp(MsgFieldEnd);
  902. //break;
  903. }
  904. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  905. //calculate measure time
  906. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  907. // let main thread to know that this sample measurement completes
  908. ST_MSTMsg MsgSmplEnd=new ST_MSTMsg();
  909. MsgSmplEnd.iMsgType = ENUM_MSG_TYPE.MTHREADSTATUS;
  910. MsgSmplEnd.STMThreadStu.iMsrStatu = OTS_MSR_THREAD_STATUS.COMPLETED;
  911. m_pMsrThread.SendMessageToMeasureApp(MsgSmplEnd);
  912. }
  913. }
  914. // Cumulate field data info
  915. public virtual bool CumulateFieldData( List<COTSParticleClr> listParticles, double a_nMeasuredArea)
  916. {
  917. // get measure result items of the sample
  918. CMsrDisplayResults pMsrResults = m_Sample.GetMsrResults();
  919. // cumulate field data info
  920. pMsrResults.CumulateMeasuredArea(a_nMeasuredArea);
  921. // cumulate ratio
  922. double dRatio = 10000 * pMsrResults.GetTotalParticleArea();
  923. dRatio = dRatio / pMsrResults.GetMeasuredArea();
  924. pMsrResults.SetRatio(dRatio);
  925. // go through the particles list
  926. foreach (COTSParticleClr pParticle in listParticles)
  927. {
  928. // create a measure result item
  929. pMsrResults.CumulateMeasureResult(pParticle);
  930. }
  931. // ok, return TRUE
  932. return true;
  933. }
  934. // hole BSE images list
  935. void SetHoleBESImgList(CHoleBSEImgsList a_listHoleBSEImg, bool a_bClear/* = TRUE*/)
  936. {// clear the hole BSE image list if necessary
  937. if (a_bClear)
  938. {
  939. m_listHoleBSEImg.Clear();
  940. }
  941. // go through the input list
  942. foreach (var pHoleBSEImg in a_listHoleBSEImg)
  943. {// add the new hole BSE image into HoleBSEImage list
  944. m_listHoleBSEImg.Add(pHoleBSEImg);
  945. }
  946. }
  947. public bool SaveMergedParticles(List<COTSParticleClr> mergedParts)
  948. {
  949. CIncAFileMgr pDBFileMgr = m_pSampleRstFile.DBFileMgr;
  950. var mergedpartdb = pDBFileMgr.GetMergedParticleDB();
  951. foreach (COTSParticleClr part in mergedParts)
  952. {
  953. mergedpartdb.SaveAParticle(part, part.GetXray(), (Point)part.GetAbsolutPos());
  954. }
  955. CPosXrayDBMgr pXrayDBMgr = pDBFileMgr.GetPosXrayDBMgr();
  956. CElementChemistryDB xraydb = pXrayDBMgr.GetElementChemistryDB();
  957. List<CPosXrayClr> ches = new List<CPosXrayClr>();
  958. foreach (COTSParticleClr part in mergedParts)
  959. {
  960. ches.Add(part.GetXray());
  961. }
  962. xraydb.SaveElementChemistriesList(ches);
  963. return true;
  964. }
  965. public bool SaveFieldData(COTSFieldData fldData, string filedFileFoler)
  966. {
  967. //loger.Warn("begin bitmap saving...");
  968. string strFieldFileFolder = filedFileFoler;
  969. CBSEImageFileMgr pBSEImgFileMgr = new CBSEImageFileMgr();
  970. pBSEImgFileMgr.SetBSEImg(fldData.GetBSEImage());
  971. int nId = fldData.GetId();
  972. string sFieldId;
  973. sFieldId = nId.ToString();
  974. string strBSEFilePathname = strFieldFileFolder + "\\" + m_pSampleRstFile.SMPL_MSR_RESULT_FIELDS_BSE + sFieldId + pBSEImgFileMgr.BMP_IMG_FILE_EXT;
  975. if (!pBSEImgFileMgr.SaveIntoBitmap(strBSEFilePathname))
  976. {
  977. log.Error("SaveFieldFiles: save BSE file failed.");
  978. return false;
  979. }
  980. // IncA Data list
  981. CIncAFileMgr pDBFileMgr = m_pSampleRstFile.DBFileMgr;
  982. pDBFileMgr.SaveStatusDataToDB();
  983. var fldDB = pDBFileMgr.GetFieldDB();
  984. log.Warn("Start saving particle data.");
  985. var fldcmd = fldDB.GetSavingAFieldcmdObj(fldData.GetId(), fldData.GetOTSPosition(), fldData.SemPos);
  986. List<KeyValuePair<string, System.Data.SQLite.SQLiteParameter[]>> fldcmds = new List<KeyValuePair<string, System.Data.SQLite.SQLiteParameter[]>>();
  987. fldcmds.Add(fldcmd);
  988. pDBFileMgr.ExecuteNonQueryBatch(ref fldcmds);
  989. //remomove the invalid particles
  990. var cmds = pDBFileMgr.GetSavingIncADataToDBCmds(curFldData.GetListAnalysisParticles(), fldData.GetOTSPosition());
  991. pDBFileMgr.ExecuteNonQueryBatch(ref cmds);
  992. CPosXrayDBMgr PosXrayDBMgr = pDBFileMgr.GetPosXrayDBMgr();
  993. var listAnalysisPosXray = new List<CPosXrayClr>();
  994. foreach (var p in curFldData.GetListXrayParticles())
  995. {
  996. listAnalysisPosXray.Add(p.GetXray());
  997. }
  998. var cmds1 = PosXrayDBMgr.GetSavingXrayCmds(listAnalysisPosXray, true);
  999. pDBFileMgr.ExecuteNonQueryBatch(ref cmds1);
  1000. return true;
  1001. }
  1002. protected void SaveFieldMgrData()
  1003. {
  1004. while (bSaveThreadWorking)
  1005. {
  1006. while (fieldQueue.Count() > 0)
  1007. {
  1008. COTSFieldData f = fieldQueue.Dequeue();
  1009. //save to disk first ,then pop . if the size is 0,then we know all the saving work is done.
  1010. SaveFieldData(f, m_pSampleRstFile.GetFieldFileSubFolderStr());
  1011. }
  1012. if (fieldQueue.Count() == 0)
  1013. {
  1014. bSaveThreadWorking = false; //must set this flag,so the main thread can know this thread has exited.
  1015. log.Warn("finished batch saving");
  1016. return;
  1017. }
  1018. }
  1019. return;
  1020. }
  1021. protected void StartSaveFileThread(COTSFieldData a_pFieldMgr)
  1022. {
  1023. fieldQueue.Enqueue(a_pFieldMgr);
  1024. if (fieldQueue.Count() > 0) //if there's data in the queue and the previous thread has finished then start a new thread.
  1025. {
  1026. if (bSaveThreadWorking == false)
  1027. {
  1028. bSaveThreadWorking = true;
  1029. m_thread_ptr = new System.Threading.Thread(this.SaveFieldMgrData);//m_thread_ptr = shared_ptr<thread>(new thread(&CSmplMeasureInc::SaveFieldMgrData, this));
  1030. m_thread_ptr.IsBackground = true;
  1031. m_thread_ptr.Start();
  1032. }
  1033. }
  1034. }
  1035. public void ParticleSpecialTreatment(COTSFieldData fld)
  1036. {
  1037. //we adopt such a strategy here:if some particles satisfy the predefined condition then we go through the second collecting with max EDS time,so that we got a more acurate result.
  1038. Dictionary<COTSParticleClr, int> mapPartXray = new Dictionary<COTSParticleClr, int>(); //std.map<COTSParticlePtr, int> mapPartXray = new std.map<COTSParticlePtr, int>();
  1039. double edsTime = 0; //to store the EDSTime returned from the engine.
  1040. var bigparts = fld.GetListXrayParticles();
  1041. var m_ClassifyEngine = new CClassifyEngine();
  1042. for (int i = 0; i < bigparts.Count(); i++)
  1043. {
  1044. var engine = m_ClassifyEngine.GetParticleEngine(m_Sample.GetMsrParams().GetSTDName());
  1045. var p = bigparts[i];
  1046. // there will be very less particle satisfied the condition ,so we use only one MaxEDS time,though we set MaxEDSTime for every rule separately .Here we store the last one.
  1047. //if the particle satisfied the MaxEDS condition then we go through the second colleting.Here we record the particle and the EDSTime and the corresponding xray position.
  1048. edsTime = engine.IfNeedMaxEDS(p);
  1049. if (edsTime > 0)
  1050. {
  1051. mapPartXray[p] = i;
  1052. }
  1053. }
  1054. List<COTSParticleClr> partsMax = new List<COTSParticleClr>();
  1055. if (mapPartXray.Count() > 0)
  1056. {
  1057. foreach (var p in mapPartXray)
  1058. {
  1059. partsMax.Add(p.Key);
  1060. }
  1061. List<CPosXrayClr> maxEDSXrays = new List<CPosXrayClr>();
  1062. m_EDSHardwareMgr.GetXRayByFeatures(partsMax, edsTime, true);
  1063. }
  1064. }
  1065. public void CalculateParticleAbsolutPos()
  1066. {
  1067. double dPixelSize = m_Sample.CalculatePixelSize();
  1068. CSEMStageData pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  1069. foreach (var p in curFldData.GetListAnalysisParticles())
  1070. {
  1071. Point fldOtsPos = new Point(curFldData.OTSPos.X, curFldData.OTSPos.Y);
  1072. Point semPos = new Point();
  1073. pCSEMStageData.ConverOTSToSEMPoint(fldOtsPos, ref semPos);
  1074. p.SetAbsolutPos(semPos);
  1075. }
  1076. }
  1077. }
  1078. }