CSmplMeasure.cs 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444
  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. int nStopMode = (int)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.NOT_IDENTIFIED)//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. switch (nStopMode)
  295. {
  296. case (int)OTS_MEASURE_STOP_MODE.CoverMode:
  297. // completed fields number
  298. if (nCompeltedField < a_nTotalFields)
  299. {
  300. bRet = false;
  301. }
  302. break;
  303. case (int)OTS_MEASURE_STOP_MODE.FieldMode:
  304. if (nCompeltedField < nStopField)
  305. {
  306. bRet = false;
  307. }
  308. break;
  309. case (int)OTS_MEASURE_STOP_MODE.ParticleMode:
  310. if (nNumParticle < nParticlAim)
  311. {
  312. bRet = false;
  313. }
  314. break;
  315. case (int)OTS_MEASURE_STOP_MODE.TimeMode:
  316. if (nUsedTime < nMeasTimeAim)
  317. {
  318. bRet = false;
  319. }
  320. break;
  321. default:
  322. break;
  323. }
  324. return bRet;
  325. }
  326. // move SEM to the point
  327. bool MoveSEMToPoint(System.Drawing.Point a_poi)
  328. {
  329. // get SEM controller
  330. var pSEMController = m_SemHardwareMgr;
  331. Point a_SEMpt = new Point();
  332. CSEMStageData a_pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  333. int hardWareDelay = a_pCSEMStageData.GetHardWareDelay();
  334. if (!a_pCSEMStageData.ConverOTSToSEMPoint(a_poi, ref a_SEMpt))
  335. {
  336. return false;
  337. }
  338. if (!pSEMController.IsConnected())
  339. {
  340. if (!pSEMController.Connect())
  341. {
  342. log.Error("MoveSEMToPoint: can't connect SEM.");
  343. return false;
  344. }
  345. }
  346. log.Info("Begin to move SEM stage to " + a_SEMpt.X + "," + a_SEMpt.Y);
  347. // move SEM to the position (rotation 0)
  348. if (!pSEMController.MoveSEMToPoint(a_SEMpt))
  349. {
  350. log.Error("MoveSEMToPoint: failed to call MoveSEMToPoint method.");
  351. return false;
  352. }
  353. Thread.Sleep(hardWareDelay);
  354. return true;
  355. }
  356. // Acquire a BSE image
  357. CBSEImgClr AcquireABSEImage()
  358. {
  359. // BSE image
  360. CBSEImgClr pBSEImage = null;
  361. // get scan controller
  362. var pScanController = m_ScanHardwareMgr;
  363. pBSEImage = pScanController.AcquireBSEImage(0, 0, 2);
  364. return pBSEImage;
  365. }
  366. //public virtual bool FieldImageProcess(Point fldCenter, CBSEImgClr imageData)
  367. //{
  368. // return true;
  369. //}
  370. public virtual void ClassifyFieldParticles()
  371. {
  372. return;
  373. }
  374. public virtual void ClassifyMergedParticles(List<COTSParticleClr> parts)
  375. {
  376. return;
  377. }
  378. private class AutoResetSEMControl:IDisposable
  379. {
  380. CSmplMeasure sm;
  381. public AutoResetSEMControl(CSmplMeasure s)
  382. {
  383. sm = s;
  384. }
  385. public void Dispose()
  386. {
  387. sm.SetSEMExteralOff();
  388. }
  389. }
  390. public void DoMeasureForOneSample()
  391. {
  392. using (AutoResetSEMControl autoReset = new AutoResetSEMControl(this)) //when this method exit ,the SetSEMExternalOff and ResetScan will be called automatically.
  393. {
  394. // let the main thread to know that this sample measurement starts
  395. var pStatus = m_Sample.GetMsrStatus();
  396. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.INPROCESS);
  397. // set current time to current time
  398. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.START);
  399. // let main App know that the sample begin to measure
  400. ST_MSTMsg MsgSmpStart = new ST_MSTMsg();
  401. MsgSmpStart.iMsgType = ENUM_MSG_TYPE.MSAMPLESTATUS;
  402. MsgSmpStart.STMSampleStu.iMsrSampleStatu = OTS_MSR_SAMPLE_STATUS.INPROCESS;
  403. MsgSmpStart.STMSampleStu.cSampleName = m_Sample.GetName();
  404. m_pMsrThread.SendMessageToMeasureApp(MsgSmpStart);
  405. log.Info(m_Sample.GetName() + " Measurement started!");
  406. // get SEM controller to set magnification and working distance
  407. if (!SetSEMDataMrs())
  408. {
  409. log.Error("DoMeasure: fail to set SEM data.");
  410. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  411. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  412. return;
  413. }
  414. // set the BSE scan param
  415. if (!SetBSEParam())
  416. {
  417. log.Error("DoMeasure: fail to set BSE param.");
  418. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  419. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  420. return;
  421. }
  422. var pSEMDataGnr = new CSEMDataGnr();
  423. log.Info("Get Kv, Brightness and Contrast!");
  424. m_SemHardwareMgr.GetSEMDataGnrFromHw(ref pSEMDataGnr);
  425. m_pSampleRstFile.SetSEMGnr(pSEMDataGnr);
  426. // record SEM data
  427. COTSMsrPrjResultData pProjMgrFile = m_pMsrThread.GetProjResultData();
  428. CSEMStageData pSEMStageData = pProjMgrFile.GetSEMStageData();
  429. m_pSampleRstFile.SetSEMStageData(pSEMStageData);
  430. // record stage
  431. CStage pStage = pProjMgrFile.GetStage();
  432. m_pSampleRstFile.SetSEMStage(pStage);
  433. // calculate field centers
  434. List<System.Drawing.Point> umMeasuredlistFieldCenter;
  435. List<System.Drawing.Point> alllistFieldCenter;
  436. if (!CalculateUnMeasuredFieldsCenters(out alllistFieldCenter,out umMeasuredlistFieldCenter))
  437. {// failed to calculate field centers
  438. log.Error("DoMeasure: failed to calculate field centers.");
  439. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  440. // record end time
  441. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  442. return;
  443. }
  444. //-----save the static measure result file data into xml file and the dynamic data of every field will be saved into sqlite database
  445. log.Info("Create result file!");
  446. if (!m_pSampleRstFile.CreateResultFiles())
  447. {// failed to call measure result file Save method
  448. log.Error("DoMeasure: failed to call measure result file Save method.");
  449. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  450. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  451. return;
  452. }
  453. //------
  454. int nNewFieldId = 0;
  455. int numOfAllFields = pStatus.GetCompletedFields() + umMeasuredlistFieldCenter.Count;//
  456. int completedFields = pStatus.GetCompletedFields();
  457. log.Info("Unmeasured fields:" + umMeasuredlistFieldCenter.Count);
  458. for (int i = 0; i < (int)umMeasuredlistFieldCenter.Count; ++i)
  459. {// check and break if stop button is clicked
  460. if (IsAborted())
  461. {// measure stopped
  462. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.STOPPED);
  463. // record end time
  464. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  465. //must wait for the saving data thread finished,or we'll get null pointer exception when we stop the measure process.
  466. while (fieldQueue.Count() > 0)
  467. {
  468. Thread.Sleep(100);
  469. }
  470. return;
  471. }
  472. // check if sample measurement completes
  473. COTSImgScanPrm pScanParam = m_Sample.GetMsrParams().GetImageScanParam();
  474. int nTotalFieldSize = (int)umMeasuredlistFieldCenter.Count;
  475. if (IsSampleOver(pScanParam, numOfAllFields))
  476. {
  477. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  478. break;
  479. }
  480. // get a field center
  481. System.Drawing.Point poiFieldCentre = umMeasuredlistFieldCenter[i];
  482. // update thread measure status class, let the main thread know that starts a new field
  483. ST_MSTMsg MsgFieldStart = new ST_MSTMsg();
  484. MsgFieldStart.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  485. MsgFieldStart.STMSampleRetData.iRetDataType = MSAMPLE_RET.START_MSR_FIELD;
  486. MsgFieldStart.STMSampleRetData.SMsrField.FieldPos = poiFieldCentre;
  487. m_pMsrThread.SendMessageToMeasureApp(MsgFieldStart);
  488. int fldNo = completedFields + i + 1;
  489. log.Warn("Current field:" + fldNo.ToString());
  490. // move SEM to the field center
  491. if (!MoveSEMToPoint(poiFieldCentre))
  492. {// failed to move SEM to the position
  493. log.Error("DoMeasure: failed to move SEM to the field centre point.");
  494. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  495. // record end time
  496. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  497. return;
  498. }
  499. log.Info("Begin to Acquire BSE image!");
  500. // take BSE image for the fields
  501. CBSEImgClr pBSEImg = AcquireABSEImage();
  502. // let the main thread to know that image process is completed
  503. if (pBSEImg == null)
  504. {
  505. log.Error("ImageProcess: can't get no background image.");
  506. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  507. return;
  508. }
  509. //BSEData
  510. ST_MSTMsg MsgFieldBSE = new ST_MSTMsg();
  511. MsgFieldBSE.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  512. MsgFieldBSE.STMSampleRetData.iRetDataType = MSAMPLE_RET.BSE_DATA;
  513. MsgFieldBSE.STMSampleRetData.BSEData.pos.X = poiFieldCentre.X;
  514. MsgFieldBSE.STMSampleRetData.BSEData.pos.Y = poiFieldCentre.Y;
  515. MsgFieldBSE.STMSampleRetData.BSEData.iBSEDataHeight = pBSEImg.GetHeight();
  516. MsgFieldBSE.STMSampleRetData.BSEData.iBSEDataWidth = pBSEImg.GetWidth();
  517. byte[] pImgData = pBSEImg.GetImageDataPtr();
  518. MsgFieldBSE.STMSampleRetData.BSEData.lpBSEData = pImgData;
  519. m_pMsrThread.SendMessageToMeasureApp(MsgFieldBSE);
  520. // get a new field id
  521. log.Info("Acquire BSE image success! Processing image...");
  522. // image process
  523. var rst = FieldImageProcess(poiFieldCentre, pBSEImg);
  524. if (rst == true)
  525. {
  526. // add the field into the field
  527. m_pSampleRstFile.AddAField(curFldData);
  528. }
  529. ++nNewFieldId;
  530. double measuredArea = 0; // this area should be the field area
  531. var a_pBSEImg = curFldData.GetBSEImage();
  532. double dPixelSize = m_Sample.CalculatePixelSize();
  533. measuredArea = a_pBSEImg.GetHeight() * a_pBSEImg.GetWidth()* dPixelSize * dPixelSize + 0.5; //Get measured area
  534. if (!CumulateFieldData(curFldData.GetListAnalysisParticles(), measuredArea))
  535. { // failed to call SaveFieldFile method
  536. log.Error("ImageProcess: call CumulateFieldData method.");
  537. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  538. return;
  539. }
  540. log.Info("Send field data to screen!");
  541. // completed fields
  542. pStatus.SetCompletedFields(pStatus.GetCompletedFields() + 1);
  543. // completed fieldCenter
  544. List<System.Drawing.Point> listCpltedCenter = pStatus.GetCompletedFieldsCenter();
  545. listCpltedCenter.Add(poiFieldCentre);
  546. //Field Data
  547. // record end time
  548. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  549. ST_MSTMsg MsgFieldEnd = new ST_MSTMsg();
  550. MsgFieldEnd.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  551. MsgFieldEnd.STMSampleRetData.iRetDataType = MSAMPLE_RET.FIELD_DATA;
  552. MsgFieldEnd.STMSampleRetData.SFieldData.iCompleteFieldCount = pStatus.GetCompletedFields();
  553. MsgFieldEnd.STMSampleRetData.SFieldData.iMeasureFieldCount = numOfAllFields;
  554. MsgFieldEnd.STMSampleRetData.SFieldData.iSParticleCount = (int)curFldData.GetListAnalysisParticles().Count;
  555. MsgFieldEnd.STMSampleRetData.SFieldData.FieldPos.X = Convert.ToInt32(poiFieldCentre.X);
  556. MsgFieldEnd.STMSampleRetData.SFieldData.FieldPos.Y = Convert.ToInt32(poiFieldCentre.Y);
  557. m_pMsrThread.SendMessageToMeasureApp(MsgFieldEnd);
  558. }
  559. while (bSaveThreadWorking)//wait untill all the field data has been saved.
  560. {
  561. Thread.Sleep(1000);
  562. log.Warn("db saving!");
  563. }
  564. //merging particles
  565. log.Info("Merging big particles which are crossing the field edge!");
  566. CImageHandler imgpro = new CImageHandler();
  567. int scanfldsize = m_Sample.GetSEMDataMsr().GetScanFieldSize();
  568. List<COTSParticleClr> mergedParticles = new List<COTSParticleClr>();
  569. double pixelSize = m_Sample.CalculatePixelSize();
  570. imgpro.MergeBigBoundaryParticles(m_pSampleRstFile.GetFieldData(), pixelSize, scanfldsize, m_Sample.GetResolutionSize(), ref mergedParticles);
  571. CalculateMergedPartProperty(mergedParticles,pixelSize);
  572. ClassifyMergedParticles(mergedParticles);
  573. log.Info("begin merged particle data db saving...");
  574. SaveMergedParticles(mergedParticles);
  575. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.COMPLT);
  576. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  577. // let main thread to know that this sample measurement completes
  578. ST_MSTMsg MsgSmplEnd = new ST_MSTMsg();
  579. MsgSmplEnd.iMsgType = ENUM_MSG_TYPE.MSAMPLESTATUS;
  580. MsgSmplEnd.STMSampleStu.iMsrSampleStatu = OTS_MSR_SAMPLE_STATUS.SUCCESSED;
  581. MsgSmplEnd.STMSampleStu.cSampleName = m_Sample.GetName();
  582. m_pMsrThread.SendMessageToMeasureApp(MsgSmplEnd);
  583. }
  584. }
  585. private void CalculateMergedPartProperty(List<COTSParticleClr> mergedParticles,double pixelSize)
  586. {
  587. var m_ImagePro = new CImageHandler();
  588. foreach (COTSParticleClr part in mergedParticles)
  589. {
  590. m_ImagePro.CalParticleImageProp(part, pixelSize);
  591. }
  592. }
  593. public bool FieldImageProcess(Point fldCenter, CBSEImgClr a_pBSEImg)
  594. {
  595. int nNewFieldId;
  596. nNewFieldId = m_pSampleRstFile.GetIdForANewField();
  597. // create a field
  598. curFldData = new COTSFieldData(a_pBSEImg, m_Sample.CalculatePixelSize());
  599. curFldData.SetId(nNewFieldId);
  600. curFldData.SetOTSPosition(fldCenter);
  601. CSEMStageData a_pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  602. Point semPos = new Point();
  603. a_pCSEMStageData.ConverOTSToSEMPoint(fldCenter, ref semPos);
  604. curFldData.SemPos = semPos;
  605. //first step:remove background of the bse image and compound all the finded particles.
  606. log.Info("Begin to process image and get all particles!");
  607. GetOriginalParticles();
  608. // second step :filter the finded particles.
  609. log.Info("Begin to filter particles!");
  610. FilterParticles(curFldData);
  611. CalculateParticleAbsolutPos();
  612. log.Info("Begin to Calculate the image property of every particle!");
  613. var analysisparts = curFldData.GetListAnalysisParticles();
  614. curFldData.CalParticleImageProp(analysisparts);//calculate particle image property such as feret diameter, DMAX etc.
  615. COTSXRayParam pXRayParam = m_Sample.GetMsrParams().GetXRayParam();
  616. if (pXRayParam.GetUsingXray() == true)
  617. {
  618. log.Info("Begin to collect particle's xray data!");
  619. Thread.Sleep(100);
  620. CollectParticlesXrayData(curFldData);
  621. Thread.Sleep(100);
  622. }
  623. log.Info("Begin to classify particles! particle num:" + curFldData.GetListAnalysisParticles().Count);
  624. ClassifyFieldParticles();
  625. // save field files
  626. m_Sample.GetMsrStatus().SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  627. StartSaveFileThread(curFldData);
  628. return true;
  629. }
  630. public void GetOriginalParticles()
  631. {
  632. // measure status
  633. CMsrSampleStatus pStatus = m_Sample.GetMsrStatus();
  634. // get image process parameter
  635. CSampleParam pMsrParam = m_Sample.GetMsrParams();
  636. COTSImageProcParam pImgProcessParam = pMsrParam.GetImageProcessParam();
  637. var specialPartsparam = pMsrParam.GetSpecialGrayRangeParam();
  638. var pixelsize = m_Sample.CalculatePixelSize();
  639. if (specialPartsparam.GetIsToRun())
  640. {
  641. List<CSpecialGrayRange> ranges = pMsrParam.GetSpecialGrayRangeParam().GetSpecialGreyRanges();
  642. foreach (var grayRange in ranges)
  643. {
  644. CIntRangeClr range = new CIntRangeClr(grayRange.range.GetStart(), grayRange.range.GetEnd());
  645. CDoubleRangeClr diaRange = new CDoubleRangeClr(grayRange.diameterRange.GetStart(), grayRange.diameterRange.GetEnd());
  646. curFldData.GetPartsBySpecialGray(range, diaRange, pixelsize, grayRange.ifCollectXray);
  647. }
  648. }
  649. var ifCollectxray = pMsrParam.GetXRayParam().GetUsingXray();
  650. // remove BES image background
  651. curFldData.RemoveImgBGAndGetParticles(pImgProcessParam, pixelsize, ifCollectxray);
  652. // check if this is an empty image
  653. if (curFldData.NoParticle())
  654. { // empty fields
  655. log.Info("ImageProcess: empty field.");
  656. }
  657. return;
  658. }
  659. public void FilterParticles(COTSFieldData fld)
  660. {
  661. if (fld.NoAnalysisParticle())
  662. {
  663. log.Warn("There's no analysis particles!");
  664. return;
  665. }
  666. CSampleParam pMsrParam = m_Sample.GetMsrParams();
  667. COTSImageProcParam pImgProcessParam = pMsrParam.GetImageProcessParam();
  668. double dPixelSize = m_Sample.CalculatePixelSize();
  669. curFldData.InitParticles(pImgProcessParam, dPixelSize);
  670. var listXray = curFldData.GetAllParticles().OrderByDescending(x => x.GetActualArea()).ToList();
  671. var listXray1 = new List<COTSParticleClr>();
  672. var pXRayParam = pMsrParam.GetXRayParam();
  673. if (listXray.Count > pXRayParam.GetXrayLimit())
  674. {
  675. for (var i = 0; i < pXRayParam.GetXrayLimit(); i++)
  676. {
  677. listXray1.Add(listXray[i]);
  678. }
  679. }
  680. else
  681. {
  682. listXray1 = listXray;
  683. }
  684. curFldData.SetListAnalysisParticles(listXray1);
  685. log.Info("Analysis particles:"+ listXray1.Count);
  686. //2) according to the quantify threshold size value saperate the analysis particles into two group :the bigparticles and the smallparticles.
  687. double quantifyThreshold = m_Sample.GetMsrParams().GetXRayParam().GetFeatureModeMinSize();
  688. var smallparts = fld.ListSmallParticles;
  689. var bigparts = fld.ListBigParticles;
  690. foreach (var part in listXray1)
  691. {
  692. double equalCircleDiameter = Math.Sqrt(part.GetActualArea() / 3.14159) * 2f;
  693. if (equalCircleDiameter < quantifyThreshold)
  694. {
  695. smallparts.Add(part);
  696. }
  697. else
  698. {
  699. bigparts.Add(part);
  700. }
  701. }
  702. fld.ListSmallParticles = smallparts;
  703. fld.ListBigParticles = bigparts;
  704. //fld.SmallParticlePercentage=percentage;
  705. log.Info("SmallQuantifyParts (<" + quantifyThreshold.ToString("f2") + "): " + smallparts.Count);
  706. log.Info("BigQuantifyParts (>=" + quantifyThreshold.ToString("f2") + "): " + bigparts.Count);
  707. return;
  708. }
  709. public void CollectParticlesXrayData(COTSFieldData fld)
  710. {
  711. // get x-ray parameters
  712. COTSXRayParam pXRayParam = m_Sample.GetMsrParams().GetXRayParam();
  713. // calculate search x-ray acquire time
  714. uint nXRayAQTime;
  715. List<COTSParticleClr> smallparts = new List<COTSParticleClr>();
  716. List<COTSParticleClr> bigparts = new List<COTSParticleClr>();
  717. foreach (var p in fld.ListSmallParticles)
  718. {
  719. if (p.IsXrayParticle())
  720. {
  721. smallparts.Add(p);
  722. }
  723. }
  724. foreach (var p in fld.ListBigParticles)
  725. {
  726. if (p.IsXrayParticle())
  727. {
  728. bigparts.Add(p);
  729. }
  730. }
  731. var pStatus = m_Sample.GetMsrStatus();
  732. curFldData.CreateXrayList(bigparts); // big particle using the full xray strategy.
  733. curFldData.CreateXrayList(smallparts); //small particle using the fast xray strategy
  734. if (bigparts.Count > 0)
  735. {
  736. var workmode = pXRayParam.GetScanMode();
  737. // get x-ray list (analysis) by particle features
  738. if (workmode == OTS_X_RAY_SCAN_MODE.FeatureMode)
  739. {
  740. nXRayAQTime = (uint)pXRayParam.GetMidAnalyAQTime();
  741. m_EDSHardwareMgr.GetXRayByFeatures(bigparts, nXRayAQTime, true);
  742. }
  743. else
  744. {
  745. nXRayAQTime = (uint)pXRayParam.GetMidAnalyAQTime();
  746. m_EDSHardwareMgr.GetXRayByPoints(bigparts, nXRayAQTime, true);
  747. }
  748. }
  749. if (smallparts.Count > 0)
  750. {
  751. nXRayAQTime = (uint)pXRayParam.GetFastXrayTime();
  752. // get x-ray list (analysis) by points
  753. m_EDSHardwareMgr.GetXRayByPoints(smallparts, nXRayAQTime, true);
  754. }
  755. return;
  756. }
  757. public void DoHolePreview(int a_nHoleID)
  758. {
  759. using (AutoResetSEMControl autoReset = new AutoResetSEMControl(this))
  760. {
  761. // let the main thread to know that this sample measurement starts
  762. CMsrSampleStatus pStatus = m_Sample.GetMsrStatus();
  763. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.INPROCESS);
  764. // set current time to current time
  765. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.START);
  766. // let main App know that the sample begin to measure
  767. ST_MSTMsg MsgSmpStart=new ST_MSTMsg();
  768. MsgSmpStart.iMsgType = ENUM_MSG_TYPE.MSAMPLESTATUS;
  769. MsgSmpStart.STMSampleStu.iMsrSampleStatu = OTS_MSR_SAMPLE_STATUS.INPROCESS;
  770. MsgSmpStart.STMSampleStu.cSampleName=m_Sample.GetName();
  771. // get SEM controller to set magnification and working distance
  772. if (!SetSEMDataMrs())
  773. {
  774. log.Error("DoHolePreview: fail to set SEM data.");
  775. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  776. // record end time
  777. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  778. return;
  779. }
  780. // set the BSE scan param
  781. if (!SetHoleBSEParam())
  782. {
  783. log.Error("DoMeasure: fail to set BSE param.");
  784. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  785. // record end time
  786. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  787. return;
  788. }
  789. // check if measurement is aborted
  790. if (IsAborted())
  791. {// measurement aborted
  792. log.Trace("DoHolePreview: measurement aborted before get SEM condition.");
  793. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.STOPPED);
  794. // record end time
  795. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  796. return;
  797. }
  798. // calculate field centers
  799. List<System.Drawing.Point> listFieldCenter=new List<System.Drawing.Point>();
  800. List<System.Drawing.Point> alllistFieldCenter = new List<System.Drawing.Point>();
  801. // listFieldCenter.clear();
  802. if (!CalculateUnMeasuredFieldsCenters(out alllistFieldCenter,out listFieldCenter))
  803. {// failed to calculate field centers
  804. log.Error("DoHolePreview: failed to calculate field centers.");
  805. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  806. // record end time
  807. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  808. return;
  809. }
  810. // go through each field
  811. int nNewFieldId = 0;
  812. for ( int i = 0; i < listFieldCenter.Count; ++i)
  813. {// check and break if stop button is clicked
  814. if (IsAborted())
  815. {// measure stopped
  816. log.Trace("DoHolePreview: measure thread is stopped.");
  817. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.STOPPED);
  818. // record end time
  819. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  820. return;
  821. }
  822. // check if sample measurement completes
  823. COTSImgScanPrm pScanParam = m_Sample.GetMsrParams().GetImageScanParam();
  824. int nTotalFieldSize = listFieldCenter.Count;
  825. if (IsSampleOver(pScanParam, nTotalFieldSize))
  826. {
  827. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  828. break;
  829. }
  830. // get a field center
  831. System.Drawing.Point poiFieldCentre = listFieldCenter[i];
  832. // update thread measure status class, let the main thread know that starts a new field
  833. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  834. ST_MSTMsg MsgFieldStart=new ST_MSTMsg();
  835. MsgFieldStart.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  836. MsgFieldStart.STMSampleRetData.iRetDataType = MSAMPLE_RET.START_MSR_FIELD;
  837. MsgFieldStart.STMSampleRetData.SMsrField.FieldPos = poiFieldCentre;
  838. m_pMsrThread.SendMessageToMeasureApp(MsgFieldStart);
  839. // move SEM to the field center
  840. if (!MoveSEMToPoint(poiFieldCentre))
  841. {// failed to move SEM to the position
  842. log.Error("DoHolePreview: failed to move SEM to the field centre point.");
  843. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  844. // record end time
  845. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  846. return;
  847. }
  848. // take BSE image for the fields
  849. CBSEImgClr pBSEIamge = AcquireABSEImage();
  850. if (pBSEIamge==null)
  851. {
  852. // failed to acquire a BSE image
  853. log.Error("DoHolePreview: failed to acquire a BSE image.");
  854. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  855. // record end time
  856. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  857. return;
  858. }
  859. //BSEData
  860. ST_MSTMsg MsgFieldBSE=new ST_MSTMsg();
  861. // memset(&MsgFieldBSE, 0, sizeof(ST_MSTMsg));
  862. MsgFieldBSE.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  863. MsgFieldBSE.STMSampleRetData.iRetDataType = MSAMPLE_RET.BSE_DATA;
  864. MsgFieldBSE.STMSampleRetData.BSEData.pos = poiFieldCentre;
  865. //MsgFieldBSE.STMSampleRetData.BSEData.pos.y = poiFieldCentre.y;
  866. MsgFieldBSE.STMSampleRetData.BSEData.iBSEDataHeight = pBSEIamge.GetHeight();
  867. MsgFieldBSE.STMSampleRetData.BSEData.iBSEDataWidth = pBSEIamge.GetWidth();
  868. byte[] pImgData = pBSEIamge.GetImageDataPtr();
  869. MsgFieldBSE.STMSampleRetData.BSEData.lpBSEData = pImgData;
  870. m_pMsrThread.SendMessageToMeasureApp(MsgFieldBSE);
  871. if (pStatus.GetStatus() != OTS_MSR_SAMPLE_STATUS.INPROCESS)
  872. {
  873. // measurement failed or stopped
  874. log.Error("DoHolePreview: measurement failed or stopped.");
  875. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  876. // record end time
  877. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  878. return;
  879. }
  880. //save the result to project file
  881. Rectangle oImageRect = (Rectangle)pBSEIamge.GetImageRect();
  882. CHoleBSEImg pHoleBSEImg =new CHoleBSEImg(oImageRect,a_nHoleID, poiFieldCentre);
  883. pHoleBSEImg.SetImageData( pBSEIamge.GetImageDataPtr(),oImageRect.Width,oImageRect.Height);
  884. m_listHoleBSEImg.Add(pHoleBSEImg);
  885. COTSMsrPrjResultData pProjMgrFile = m_pMsrThread.GetProjResultData();
  886. pProjMgrFile.SetHoleBESImgList(m_listHoleBSEImg,true);
  887. // prepare for the next
  888. ++nNewFieldId;
  889. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  890. ST_MSTMsg MsgFieldEnd=new ST_MSTMsg();
  891. MsgFieldEnd.iMsgType = ENUM_MSG_TYPE.MSAMPLERESULT;
  892. MsgFieldEnd.STMSampleRetData.iRetDataType = MSAMPLE_RET.FIELD_DATA;
  893. MsgFieldEnd.STMSampleRetData.SFieldData.iCompleteFieldCount = (i + 1);
  894. MsgFieldEnd.STMSampleRetData.SFieldData.iMeasureFieldCount = listFieldCenter.Count;
  895. MsgFieldEnd.STMSampleRetData.SFieldData.iSParticleCount = 0;
  896. MsgFieldEnd.STMSampleRetData.SFieldData.FieldPos = poiFieldCentre;
  897. m_pMsrThread.SendMessageToMeasureApp(MsgFieldEnd);
  898. //break;
  899. }
  900. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  901. //calculate measure time
  902. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  903. // let main thread to know that this sample measurement completes
  904. ST_MSTMsg MsgSmplEnd=new ST_MSTMsg();
  905. MsgSmplEnd.iMsgType = ENUM_MSG_TYPE.MTHREADSTATUS;
  906. MsgSmplEnd.STMThreadStu.iMsrStatu = OTS_MSR_THREAD_STATUS.COMPLETED;
  907. m_pMsrThread.SendMessageToMeasureApp(MsgSmplEnd);
  908. }
  909. }
  910. // Cumulate field data info
  911. public virtual bool CumulateFieldData( List<COTSParticleClr> listParticles, double a_nMeasuredArea)
  912. {
  913. // get measure result items of the sample
  914. CMsrDisplayResults pMsrResults = m_Sample.GetMsrResults();
  915. // cumulate field data info
  916. pMsrResults.CumulateMeasuredArea(a_nMeasuredArea);
  917. // cumulate ratio
  918. double dRatio = 10000 * pMsrResults.GetTotalParticleArea();
  919. dRatio = dRatio / pMsrResults.GetMeasuredArea();
  920. pMsrResults.SetRatio(dRatio);
  921. // go through the particles list
  922. foreach (COTSParticleClr pParticle in listParticles)
  923. {
  924. // create a measure result item
  925. pMsrResults.CumulateMeasureResult(pParticle);
  926. }
  927. // ok, return TRUE
  928. return true;
  929. }
  930. // hole BSE images list
  931. void SetHoleBESImgList(CHoleBSEImgsList a_listHoleBSEImg, bool a_bClear/* = TRUE*/)
  932. {// clear the hole BSE image list if necessary
  933. if (a_bClear)
  934. {
  935. m_listHoleBSEImg.Clear();
  936. }
  937. // go through the input list
  938. foreach (var pHoleBSEImg in a_listHoleBSEImg)
  939. {// add the new hole BSE image into HoleBSEImage list
  940. m_listHoleBSEImg.Add(pHoleBSEImg);
  941. }
  942. }
  943. public bool SaveMergedParticles(List<COTSParticleClr> mergedParts)
  944. {
  945. CIncAFileMgr pDBFileMgr = m_pSampleRstFile.DBFileMgr;
  946. var mergedpartdb = pDBFileMgr.GetMergedParticleDB();
  947. foreach (COTSParticleClr part in mergedParts)
  948. {
  949. mergedpartdb.SaveAParticle(part, part.GetXray(), (Point)part.GetAbsolutPos());
  950. }
  951. CPosXrayDBMgr pXrayDBMgr = pDBFileMgr.GetPosXrayDBMgr();
  952. CElementChemistryDB xraydb = pXrayDBMgr.GetElementChemistryDB();
  953. List<CPosXrayClr> ches = new List<CPosXrayClr>();
  954. foreach (COTSParticleClr part in mergedParts)
  955. {
  956. ches.Add(part.GetXray());
  957. }
  958. xraydb.SaveElementChemistriesList(ches);
  959. return true;
  960. }
  961. public bool SaveFieldData(COTSFieldData fldData, string filedFileFoler)
  962. {
  963. //loger.Warn("begin bitmap saving...");
  964. string strFieldFileFolder = filedFileFoler;
  965. CBSEImageFileMgr pBSEImgFileMgr = new CBSEImageFileMgr();
  966. pBSEImgFileMgr.SetBSEImg(fldData.GetBSEImage());
  967. int nId = fldData.GetId();
  968. string sFieldId;
  969. sFieldId = nId.ToString();
  970. string strBSEFilePathname = strFieldFileFolder + "\\" + m_pSampleRstFile.SMPL_MSR_RESULT_FIELDS_BSE + sFieldId + pBSEImgFileMgr.BMP_IMG_FILE_EXT;
  971. if (!pBSEImgFileMgr.SaveIntoBitmap(strBSEFilePathname))
  972. {
  973. log.Error("SaveFieldFiles: save BSE file failed.");
  974. return false;
  975. }
  976. // IncA Data list
  977. CIncAFileMgr pDBFileMgr = m_pSampleRstFile.DBFileMgr;
  978. pDBFileMgr.SaveStatusDataToDB();
  979. var fldDB = pDBFileMgr.GetFieldDB();
  980. log.Warn("Start saving particle data.");
  981. var fldcmd = fldDB.GetSavingAFieldcmdObj(fldData.GetId(), fldData.GetOTSPosition(), fldData.SemPos);
  982. List<KeyValuePair<string, System.Data.SQLite.SQLiteParameter[]>> fldcmds = new List<KeyValuePair<string, System.Data.SQLite.SQLiteParameter[]>>();
  983. fldcmds.Add(fldcmd);
  984. pDBFileMgr.ExecuteNonQueryBatch(ref fldcmds);
  985. //remomove the invalid particles
  986. var cmds = pDBFileMgr.GetSavingIncADataToDBCmds(curFldData.GetListAnalysisParticles(), fldData.GetOTSPosition());
  987. pDBFileMgr.ExecuteNonQueryBatch(ref cmds);
  988. CPosXrayDBMgr PosXrayDBMgr = pDBFileMgr.GetPosXrayDBMgr();
  989. var listAnalysisPosXray = new List<CPosXrayClr>();
  990. foreach (var p in curFldData.GetListXrayParticles())
  991. {
  992. listAnalysisPosXray.Add(p.GetXray());
  993. }
  994. var cmds1 = PosXrayDBMgr.GetSavingXrayCmds(listAnalysisPosXray, true);
  995. pDBFileMgr.ExecuteNonQueryBatch(ref cmds1);
  996. return true;
  997. }
  998. protected void SaveFieldMgrData()
  999. {
  1000. while (bSaveThreadWorking)
  1001. {
  1002. while (fieldQueue.Count() > 0)
  1003. {
  1004. COTSFieldData f = fieldQueue.Dequeue();
  1005. //save to disk first ,then pop . if the size is 0,then we know all the saving work is done.
  1006. SaveFieldData(f, m_pSampleRstFile.GetFieldFileSubFolderStr());
  1007. }
  1008. if (fieldQueue.Count() == 0)
  1009. {
  1010. bSaveThreadWorking = false; //must set this flag,so the main thread can know this thread has exited.
  1011. log.Warn("finished batch saving");
  1012. return;
  1013. }
  1014. }
  1015. return;
  1016. }
  1017. protected void StartSaveFileThread(COTSFieldData a_pFieldMgr)
  1018. {
  1019. fieldQueue.Enqueue(a_pFieldMgr);
  1020. if (fieldQueue.Count() > 0) //if there's data in the queue and the previous thread has finished then start a new thread.
  1021. {
  1022. if (bSaveThreadWorking == false)
  1023. {
  1024. bSaveThreadWorking = true;
  1025. m_thread_ptr = new System.Threading.Thread(this.SaveFieldMgrData);//m_thread_ptr = shared_ptr<thread>(new thread(&CSmplMeasureInc::SaveFieldMgrData, this));
  1026. m_thread_ptr.IsBackground = true;
  1027. m_thread_ptr.Start();
  1028. }
  1029. }
  1030. }
  1031. public void ParticleSpecialTreatment(COTSFieldData fld)
  1032. {
  1033. //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.
  1034. Dictionary<COTSParticleClr, int> mapPartXray = new Dictionary<COTSParticleClr, int>(); //std.map<COTSParticlePtr, int> mapPartXray = new std.map<COTSParticlePtr, int>();
  1035. double edsTime = 0; //to store the EDSTime returned from the engine.
  1036. var bigparts = fld.GetListXrayParticles();
  1037. var m_ClassifyEngine = new CClassifyEngine();
  1038. for (int i = 0; i < bigparts.Count(); i++)
  1039. {
  1040. var engine = m_ClassifyEngine.GetParticleEngine(m_Sample.GetMsrParams().GetSTDName());
  1041. var p = bigparts[i];
  1042. // 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.
  1043. //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.
  1044. edsTime = engine.IfNeedMaxEDS(p);
  1045. if (edsTime > 0)
  1046. {
  1047. mapPartXray[p] = i;
  1048. }
  1049. }
  1050. List<COTSParticleClr> partsMax = new List<COTSParticleClr>();
  1051. if (mapPartXray.Count() > 0)
  1052. {
  1053. foreach (var p in mapPartXray)
  1054. {
  1055. partsMax.Add(p.Key);
  1056. }
  1057. List<CPosXrayClr> maxEDSXrays = new List<CPosXrayClr>();
  1058. m_EDSHardwareMgr.GetXRayByFeatures(partsMax, edsTime, true);
  1059. }
  1060. }
  1061. public void CalculateParticleAbsolutPos()
  1062. {
  1063. double dPixelSize = m_Sample.CalculatePixelSize();
  1064. CSEMStageData pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  1065. foreach (var p in curFldData.GetListAnalysisParticles())
  1066. {
  1067. Point fldOtsPos = new Point(curFldData.OTSPos.X, curFldData.OTSPos.Y);
  1068. Point semPos = new Point();
  1069. pCSEMStageData.ConverOTSToSEMPoint(fldOtsPos, ref semPos);
  1070. p.SetAbsolutPos(semPos);
  1071. }
  1072. }
  1073. }
  1074. }