CSmplMeasure.cs 55 KB

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