CSmplMeasure.cs 54 KB

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