CSmplMeasure.cs 54 KB

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