CSmplMeasure.cs 54 KB

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