CSmplMeasure.cs 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using OTSModelSharp.ServiceCenter;
  5. using static OTSDataType.otsdataconst;
  6. using OTSDataType;
  7. using System.Drawing;
  8. using System.Threading;
  9. using OTSModelSharp.DTLBase;
  10. using OTSCLRINTERFACE;
  11. using OTSMeasureApp._0_OTSModel.OTSDataType;
  12. using System.Windows.Forms;
  13. using System.Data;
  14. using System.IO;
  15. using OTSMeasureApp._1_OTSMeasure.Measure._3_MeasureFlow;
  16. namespace OTSModelSharp
  17. {
  18. using CHoleBSEImgsList = List<CHoleBSEImg>;
  19. public 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 COTSSample m_HolePreviewSample;
  26. protected CMeasure m_pMsrThread;
  27. string m_strWorkingFolder;
  28. protected CSmplMsrResult m_pSampleRstFile;
  29. CHoleBSEImgsList m_listHoleBSEImg;
  30. protected ISemController m_SemHardwareMgr;
  31. protected IScanController m_ScanHardwareMgr;
  32. protected IEDSController m_EDSController;
  33. protected Queue<COTSFieldData> fieldQueue=new Queue<COTSFieldData>();
  34. protected IClassifyEngine m_classifyEngine;
  35. public CSmplMeasure( string a_strWorkingFolder, COTSSample a_pSample)
  36. {
  37. m_Sample = a_pSample;
  38. log = NLog.LogManager.GetCurrentClassLogger();
  39. var expC = m_Sample.GetMsrParams().GetXRayParam().GetAnalyExpCount();
  40. var imgwidth = m_Sample.GetMsrParams().GetImageScanParam().GetImageResolutionSize().cx;
  41. var imgheight = m_Sample.GetMsrParams().GetImageScanParam().GetImageResolutionSize().cy;
  42. m_strWorkingFolder = a_strWorkingFolder;
  43. m_pSampleRstFile = new CSmplMsrResult( a_strWorkingFolder, a_pSample);
  44. m_SemHardwareMgr = SemController.GetSEMController();
  45. m_ScanHardwareMgr = ScanController.GetScanController();
  46. m_EDSController = EDSController.GetEDSController(imgwidth,imgheight,expC);
  47. m_listHoleBSEImg = new CHoleBSEImgsList();
  48. m_Sample = a_pSample;
  49. }
  50. public void SetSample(COTSSample a_pSample)
  51. {
  52. m_Sample = a_pSample;
  53. m_pSampleRstFile.SetSample(a_pSample);
  54. }
  55. public COTSSample GetSample() { return m_Sample; }
  56. public void SetHolePreviewSample(COTSSample a_pSample)
  57. {
  58. m_HolePreviewSample = a_pSample;
  59. }
  60. public COTSSample GetHolePreviewSample() { return m_HolePreviewSample; }
  61. public void SetMsrThread(CMeasure mt)
  62. {
  63. m_pMsrThread = mt;
  64. }
  65. bool SetSEMDataMrs(COTSSample sample)
  66. {
  67. var pSEMDataMsr = sample.GetSEMDataMsr();
  68. double dMag = pSEMDataMsr.GetMagnification();
  69. double dWorkDis = pSEMDataMsr.GetWorkingDistance();
  70. var pSEMCtrl = m_pMsrThread.GetSEMController();
  71. pSEMCtrl.SetMagnification(dMag);
  72. pSEMCtrl.SetWorkingDistance(dWorkDis);
  73. return true;
  74. }
  75. bool SetSEMExteralOn()
  76. {
  77. var pSEMCtrl = m_SemHardwareMgr;
  78. log.Warn("Set Scan Exteral on!");
  79. pSEMCtrl.SetScanExternal(true);
  80. return true;
  81. }
  82. // set SEM external off
  83. bool SetSEMExteralOff()
  84. {
  85. var pSEMCtrlPtr = m_SemHardwareMgr;
  86. log.Warn("Set SEM Exteral Off!");
  87. pSEMCtrlPtr.SetScanExternal(false);
  88. return true;
  89. }
  90. bool SetBSEParam()
  91. {
  92. // get scan controller
  93. var pScanController = m_ScanHardwareMgr;
  94. // scan parameters
  95. var pMsrParam = m_Sample.GetMsrParams();
  96. var pImgScanParam = pMsrParam.GetImageScanParam();
  97. // get image size
  98. var nImageSizeId = pImgScanParam.GetImageResulotion();
  99. int nResulotionId = RESOLUTION_ID_FIRST_TIE + (int)nImageSizeId;
  100. Size sizePixelImage = RESOLUTION_VALUE[nResulotionId];
  101. // get dwell time
  102. OTS_IMAGE_SCANSPEED_OPTIONS nDwellTime = pImgScanParam.GetScanImageSpeed();
  103. // convert dwell time to bruker dwell time (8, 16, 32)
  104. int nBrukerDwellTimeId;
  105. switch (nDwellTime)
  106. {
  107. case OTS_IMAGE_SCANSPEED_OPTIONS.low:
  108. nBrukerDwellTimeId = 1;
  109. break;
  110. case OTS_IMAGE_SCANSPEED_OPTIONS.meddium:
  111. nBrukerDwellTimeId = 2;
  112. break;
  113. case OTS_IMAGE_SCANSPEED_OPTIONS.high:
  114. nBrukerDwellTimeId = 3;
  115. break;
  116. default:
  117. nBrukerDwellTimeId = 1;
  118. break;
  119. }
  120. long nBrukerDwellTime = OTSDataType.otsdataconst.DWELLTIME_BRUKER_VALUES[nBrukerDwellTimeId];
  121. if (!pScanController.Init())
  122. {
  123. log.Error("SetBSEParam: failed to get scan control.");
  124. return false;
  125. }
  126. // set dwell time
  127. if (!pScanController.SetDwellTime(nBrukerDwellTime))
  128. {
  129. log.Error("SetBSEParam: failed to set dwell time (%d) for bruker system.", nBrukerDwellTime);
  130. return false;
  131. }
  132. // set image size
  133. if (!pScanController.SetImageSize(sizePixelImage.Width,sizePixelImage.Height))
  134. {
  135. // failed to set dwell time
  136. log.Error("SetBSEParam: failed to set dwell time (%d).", sizePixelImage.Height);
  137. return false;
  138. }
  139. return true;
  140. }
  141. bool SetHoleBSEParam(COTSSample sample)
  142. {
  143. // get scan controller
  144. var pScanController = m_ScanHardwareMgr;
  145. // scan parameters
  146. var pMsrParam = sample.GetMsrParams();
  147. var pImgScanParam = pMsrParam.GetImageScanParam();
  148. // get image size
  149. var nImageSizeId = pImgScanParam.GetImageResulotion();
  150. int nResulotionId = RESOLUTION_ID_FIRST_TIE + (int)nImageSizeId;
  151. Size sizePixelImage = RESOLUTION_VALUE[nResulotionId];
  152. // get dwell time
  153. OTS_IMAGE_SCANSPEED_OPTIONS nDwellTime = pImgScanParam.GetScanImageSpeed();
  154. // convert dwell time to bruker dwell time (6, 16, 32)
  155. 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.
  156. if (!pScanController.Init())
  157. {
  158. log.Error("SetBSEParam: failed to get scan control.");
  159. return false;
  160. }
  161. // set dwell time
  162. if (!pScanController.SetDwellTime(nBrukerDwellTime))
  163. {
  164. log.Error("SetBSEParam: failed to set dwell time (%d) for bruker system.", nBrukerDwellTime);
  165. return false;
  166. }
  167. // set image size
  168. if (!pScanController.SetImageSize(sizePixelImage.Width/2, sizePixelImage.Height/2))
  169. {
  170. // failed to set dwell time
  171. log.Error("SetBSEParam: failed to set ImageSize");
  172. return false;
  173. }
  174. return true;
  175. }
  176. public void SetWorkingFolder(String a_strWorkingFolder)
  177. {
  178. // add "\\" at the string end if it is not "\\"
  179. if (a_strWorkingFolder.PadRight(1)!="\\")
  180. {
  181. a_strWorkingFolder += "\\";
  182. }
  183. m_strWorkingFolder = a_strWorkingFolder + m_Sample.GetName() + "\\";
  184. }
  185. bool CalculateUnMeasuredFieldsCenters(COTSSample sample, out List<System.Drawing.Point> a_allpieldcenter, out List<System.Drawing.Point> a_listUnMsrFieldCenter)
  186. {
  187. // sample measure parameters
  188. CSampleParam pMsrParam = sample.GetMsrParams();
  189. COTSImgScanPrm poImageScanParam = pMsrParam.GetImageScanParam();
  190. COTSImageProcParam pImgProcParam = pMsrParam.GetImageProcessParam();
  191. CSEMDataMsr poSEMDataMsr = sample.GetSEMDataMsr();
  192. CMsrSampleStatus pStatus = sample.GetMsrStatus();
  193. // measured field centers list
  194. List<System.Drawing.PointF> listCompletedCenter = pStatus.GetCompletedFieldsCenter();
  195. // field centers list manager
  196. CFieldPositionMgr pFieldMgr = new CFieldPositionMgr();
  197. // init field centers list manager
  198. if (!pFieldMgr.Init(sample.GetMsrDomain(), poImageScanParam, pImgProcParam, poSEMDataMsr, listCompletedCenter))
  199. {
  200. log.Error("CalculateFieldsCenters: failed to init field centres list manager.");
  201. a_listUnMsrFieldCenter = new List<System.Drawing.Point>();
  202. a_allpieldcenter = new List<Point>();
  203. return false;
  204. }
  205. // get field centers list
  206. a_listUnMsrFieldCenter = pFieldMgr.GetUnmeasuredFieldCentrePoints();// GetFieldCentrePoints();
  207. a_allpieldcenter = pFieldMgr.GetFieldCentrePoints();
  208. // ok, return TRUE
  209. return true;
  210. }
  211. protected bool IsAborted()
  212. {
  213. return m_pMsrThread.IsMeasureStopped();
  214. }
  215. protected bool IsPaused()
  216. {
  217. var statu = m_pMsrThread.GetMsrThreadStatus();
  218. if (statu.GetStatus() == OTS_MSR_THREAD_STATUS.PAUSED)
  219. {
  220. return true;
  221. }
  222. else
  223. {
  224. return false;
  225. }
  226. }
  227. bool IsSampleOver(COTSImgScanPrm a_pScanParam)
  228. {
  229. string sStopMode = a_pScanParam.GetStopMode();
  230. int nStopField = a_pScanParam.GetStopParamFields();
  231. // completed fields number
  232. CMsrSampleStatus pMsrSampleStatus = m_Sample.GetMsrStatus();
  233. int nCompeltedField = pMsrSampleStatus.GetCompletedFields();
  234. CMsrResultItems pMsrResults = m_Sample.GetMsrResults();
  235. List<CMsrResultItem> listMsrResult = pMsrResults.GetResultItems();
  236. int nNumParticle = 0;
  237. foreach (var pResult in listMsrResult)
  238. {
  239. if (pResult.GetTypeId() > (int)OTS_PARTICLE_TYPE.NOT_IDENTIFIED)//summarize the number of the identified particle in this condition
  240. {
  241. nNumParticle += (int)pResult.GetNumber();
  242. }
  243. }
  244. TimeSpan timeSpan = pMsrSampleStatus.GetUsedTime();
  245. int nDay = timeSpan.Days;
  246. int nHour = timeSpan.Hours;
  247. int nMin = timeSpan.Minutes;
  248. int nSec = timeSpan.Seconds;
  249. int nUsedTime = nSec + nMin * 60 + nHour * 3600 + nDay * 86400;
  250. int NMeasArea = (int)pMsrResults.GetMeasuredArea();
  251. int nParticlAim = a_pScanParam.GetStopParamParticles();
  252. int nMeasTimeAim = a_pScanParam.GetStopParamMeasTime();
  253. int NMeasAreaAim = a_pScanParam.GetStopParamArea()*1000000 ;
  254. bool bRet = false;
  255. string[] str = sStopMode.Replace(" ", "").Split('+');
  256. for(int i=0;i< str.Length;i++)
  257. {
  258. switch (int.Parse(str[i].Split(':')[0])-1)
  259. {
  260. case (int)OTS_MEASURE_STOP_MODE.CoverMode:
  261. // completed fields number
  262. if (nCompeltedField == m_Sample.GetFieldsData().Count)
  263. {
  264. bRet = true;
  265. }
  266. break;
  267. case (int)OTS_MEASURE_STOP_MODE.FieldMode:
  268. if (nCompeltedField >= nStopField)
  269. {
  270. bRet = true;
  271. }
  272. break;
  273. case (int)OTS_MEASURE_STOP_MODE.ParticleMode:
  274. if (nNumParticle >= nParticlAim)
  275. {
  276. bRet = true;
  277. }
  278. break;
  279. case (int)OTS_MEASURE_STOP_MODE.TimeMode:
  280. if (nUsedTime >= nMeasTimeAim)
  281. {
  282. bRet = true;
  283. }
  284. break;
  285. case (int)OTS_MEASURE_STOP_MODE.AreaMode:
  286. if (NMeasArea >= NMeasAreaAim)
  287. {
  288. bRet = true;
  289. }
  290. break;
  291. default:
  292. break;
  293. }
  294. }
  295. return bRet;
  296. }
  297. // move SEM to the point
  298. bool MoveSEMToPoint(System.Drawing.PointF a_poi)
  299. {
  300. // get SEM controller
  301. var pSEMController = m_SemHardwareMgr;
  302. PointF a_SEMpt = new Point();
  303. CSEMStageData a_pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  304. int hardWareDelay = a_pCSEMStageData.GetHardWareDelay();
  305. if (!a_pCSEMStageData.ConvertOTSToSEMCoord(a_poi, ref a_SEMpt))
  306. {
  307. return false;
  308. }
  309. log.Info("Begin to move SEM stage to OTScoord:" + a_poi.X + "," + a_poi.Y);
  310. log.Info("Begin to move SEM stage to " + a_SEMpt.X + "," + a_SEMpt.Y);
  311. // move SEM to the position (rotation 0)
  312. if (!pSEMController.MoveSEMToPoint(a_SEMpt.X, a_SEMpt.Y))
  313. {
  314. log.Error("MoveSEMToPoint: failed to call MoveSEMToPoint method.");
  315. return false;
  316. }
  317. CSampleParam pMsrParam = m_Sample.GetMsrParams();
  318. if (pMsrParam.SlopParam.IsUsingSlopParam)
  319. {
  320. double wd = pMsrParam.SlopParam.GetWD(a_SEMpt);
  321. double originWd = 0;
  322. pSEMController.GetWorkingDistance(ref originWd);
  323. if ((wd - originWd) > 2)
  324. {
  325. log.Warn("Working Distance is invalid,outof the moving scope(2cm) wd=" + wd.ToString("F2"));
  326. }
  327. else
  328. {
  329. Thread.Sleep(hardWareDelay);
  330. pSEMController.SetWorkingDistance(wd);
  331. }
  332. }
  333. Thread.Sleep(hardWareDelay);
  334. return true;
  335. }
  336. // Acquire a BSE image
  337. CBSEImgClr AcquireABSEImage()
  338. {
  339. // BSE image
  340. CBSEImgClr pBSEImage = null;
  341. // get scan controller
  342. var pScanController = m_ScanHardwareMgr;
  343. pBSEImage = pScanController.AcquireBSEImage();
  344. return pBSEImage;
  345. }
  346. public bool IsLowCounts(COTSParticleClr particle)
  347. {
  348. COTSXRayParam pXRayParam = m_Sample.GetMsrParams().GetXRayParam();
  349. if (pXRayParam.GetUsingXray() == true)
  350. {
  351. var thecount = particle.GetXray().GetTotalCount();
  352. var expect = pXRayParam.GetAnalyExpCount();
  353. if (thecount < expect)
  354. {
  355. particle.SetType((int)OTS_PARTICLE_TYPE.LOW_COUNT);
  356. particle.SetTypeColor("#000000");
  357. particle.SetTypeName("LowCounts");
  358. return true;
  359. }
  360. }
  361. return false;
  362. }
  363. public virtual void ClassifyFieldParticles(COTSFieldData curFldData)
  364. {
  365. return;
  366. }
  367. private class SEMStateObject
  368. {
  369. private PointF pos;
  370. private double workingDistance;
  371. private CSEMDataGnr semdata;
  372. private COTSSample originalSample;
  373. private double magnification;
  374. public PointF Pos { get => pos; set => pos = value; }
  375. public double WorkingDistance { get => workingDistance; set => workingDistance = value; }
  376. public CSEMDataGnr Semdata { get => semdata; set => semdata = value; }
  377. public double Magnification { get => magnification; set => magnification = value; }
  378. public COTSSample OriginalSample { get => originalSample; set => originalSample = value; }
  379. }
  380. private class AutoResetSEMControl:IDisposable
  381. {
  382. CSmplMeasure sm;
  383. private SEMStateObject semState=null;
  384. public AutoResetSEMControl(CSmplMeasure s)
  385. {
  386. sm = s;
  387. }
  388. public SEMStateObject SemState
  389. {
  390. get => semState;
  391. set => semState = value;
  392. }
  393. public void Dispose()
  394. {
  395. if (semState != null)
  396. {
  397. sm.SetBSEParam();
  398. sm.m_SemHardwareMgr.SetMagnification(semState.Magnification);
  399. Thread.Sleep(100);
  400. sm.MoveSEMToPoint(semState.Pos);
  401. Thread.Sleep(100);
  402. sm.m_SemHardwareMgr.SetWorkingDistance(semState.WorkingDistance);
  403. }
  404. sm.SetSEMExteralOff();
  405. }
  406. }
  407. private bool GetSEMDataGnrFromHw(ref CSEMDataGnr SemDataGnr)
  408. {
  409. double kv = 0, brightness = 0, contrast = 0;
  410. var hw = SemController.GetSEMController();
  411. hw.GetSemHighTension(ref kv);
  412. hw.GetSemBrightness(ref brightness);
  413. hw.GetSemContrast(ref contrast);
  414. SemDataGnr.SetValue(kv, brightness, contrast);
  415. return true;
  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(m_Sample);
  428. MsgSmpStart.InitSampleStartMsg();
  429. m_pMsrThread.SendMessageToMeasureGUI(MsgSmpStart);
  430. log.Info(m_Sample.GetName() + " Measurement started!");
  431. // get SEM controller to set magnification and working distance
  432. if (!SetSEMDataMrs(m_Sample))
  433. {
  434. log.Error("DoMeasure: fail to set SEM data.");
  435. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  436. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  437. return;
  438. }
  439. // set the BSE scan param
  440. if (!SetBSEParam())
  441. {
  442. log.Error("DoMeasure: fail to set BSE param.");
  443. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  444. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  445. return;
  446. }
  447. var pSEMDataGnr = new CSEMDataGnr();
  448. log.Info("Get Kv, Brightness and Contrast!");
  449. GetSEMDataGnrFromHw(ref pSEMDataGnr);
  450. m_pSampleRstFile.SetSEMGnr(pSEMDataGnr);
  451. // record SEM data
  452. COTSMsrPrjResultData pProjMgrFile = m_pMsrThread.GetProjResultData();
  453. CSEMStageData pSEMStageData = pProjMgrFile.GetSEMStageData();
  454. m_pSampleRstFile.SetSEMStageData(pSEMStageData);
  455. // record stage
  456. CStage pStage = pProjMgrFile.GetStage();
  457. m_pSampleRstFile.SetSEMStage(pStage);
  458. //-----save the static measure result file data into xml file and the dynamic data of every field will be saved into sqlite database
  459. log.Info("Create result file!");
  460. if (!m_pSampleRstFile.CreateResultFiles())
  461. {// failed to call measure result file Save method
  462. log.Error("DoMeasure: failed to call measure result file Save method.");
  463. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  464. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  465. return;
  466. }
  467. //------
  468. var FldDatas = m_Sample.GetFieldsData();
  469. if (m_EDSController.GetEDSType()==EDSTYPE.BRUKER && m_Sample.GetMsrParams().GetXRayParam().GetUseFilter())
  470. {
  471. List<string> KeyNameList = new List<string>();
  472. string DBAddress = Application.StartupPath + "\\Config\\SysData\\" + m_Sample.GetMsrParams().GetSTDName() + ".db";
  473. SQLiteHelper sQLiteHelper = new SQLiteHelper(DBAddress);//
  474. KeyNameList = sQLiteHelper.GetDBKeyElementList("ClassifySTD", "KeyElementList");
  475. m_EDSController.SetFilterKeyEleNames( KeyNameList);//will decide if do the quantification according to this keyelelist.
  476. }
  477. for (int i = 0; i < FldDatas.Count; ++i)
  478. {// check and break if stop button is clicked
  479. var curFld = FldDatas[i];
  480. if (curFld.GetIsMeasureComplete())
  481. {
  482. continue;
  483. }
  484. if (IsPaused())
  485. {// measure stopped
  486. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.PAUSED);
  487. // record end time
  488. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  489. //must wait for the saving data thread finished,or we'll get null pointer exception when we stop the measure process.
  490. while (fieldQueue.Count() > 0)
  491. {
  492. Thread.Sleep(100);
  493. }
  494. SetSEMExteralOff();
  495. // update thread measure status class, let the main thread know that this sample measurement stopped
  496. ST_MSTMsg MsgSmpStop = new ST_MSTMsg(m_Sample);
  497. MsgSmpStop.InitSamplePausedMsg();
  498. m_pMsrThread.SendMessageToMeasureGUI(MsgSmpStop);
  499. while (IsPaused())
  500. {
  501. Thread.Sleep(300);
  502. }
  503. }
  504. if (IsAborted())
  505. {// measure stopped
  506. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.STOPPED);
  507. // record end time
  508. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  509. //must wait for the saving data thread finished,or we'll get null pointer exception when we stop the measure process.
  510. while (fieldQueue.Count() > 0)
  511. {
  512. Thread.Sleep(100);
  513. }
  514. break;
  515. }
  516. // check if sample measurement completes
  517. COTSImgScanPrm pScanParam = m_Sample.GetMsrParams().GetImageScanParam();
  518. if (IsSampleOver(pScanParam))
  519. {
  520. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  521. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.COMPLT);
  522. break;
  523. }
  524. // get a field center
  525. System.Drawing.PointF poiFieldCentre = curFld.GetOTSPosition();
  526. // update thread measure status class, let the main thread know that starts a new field
  527. ST_MSTMsg MsgFieldStart = new ST_MSTMsg(m_Sample,curFld);
  528. MsgFieldStart.InitFieldStartMsg();
  529. m_pMsrThread.SendMessageToMeasureGUI(MsgFieldStart);
  530. int fldNo = curFld.GetId();
  531. log.Warn("Current field:" + fldNo.ToString());
  532. // move SEM to the field center
  533. if (!MoveSEMToPoint(poiFieldCentre))
  534. {// failed to move SEM to the position
  535. log.Error("DoMeasure: failed to move SEM to the field centre point.");
  536. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  537. // record end time
  538. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  539. return;
  540. }
  541. log.Info("Begin to Acquire BSE image!");
  542. // take BSE image for the fields
  543. CBSEImgClr pBSEImg = AcquireABSEImage();
  544. // let the main thread to know that image process is completed
  545. if (pBSEImg == null)
  546. {
  547. log.Error("ImageProcess: can't get no background image.");
  548. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  549. return;
  550. }
  551. curFld.SetBSEImage(pBSEImg);
  552. //BSEData
  553. ST_MSTMsg MsgFieldBSE = new ST_MSTMsg(m_Sample,curFld);
  554. MsgFieldBSE.InitFieldBSEImageMsg();
  555. m_pMsrThread.SendMessageToMeasureGUI(MsgFieldBSE);
  556. log.Info("Acquire BSE image success! Processing image...");
  557. // image process
  558. FieldImageProcess(curFld);
  559. COTSXRayParam pXRayParam = m_Sample.GetMsrParams().GetXRayParam();
  560. if (pXRayParam.GetUsingXray() == true)
  561. {
  562. CollectParticlesXrayData(curFld);
  563. }
  564. m_Sample.GetMsrStatus().SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  565. curFld.SetIsMeasureComplete(true);
  566. log.Info("Begin to classify particles! particle num:" + curFld.GetListAnalysisParticles().Count);
  567. ClassifyFieldParticles(curFld);
  568. //start db save
  569. StartSaveFileThread(ref curFld);
  570. SendFieldParticlesInfoToGUI(curFld, m_Sample.GetMsrStatus());
  571. }
  572. while (bSaveThreadWorking)//wait untill all the field data has been saved.
  573. {
  574. Thread.Sleep(1000);
  575. log.Warn("db saving!");
  576. }
  577. TheLastWorkOfSampleMeasure();
  578. }
  579. }
  580. public void TheLastWorkOfSampleMeasure()
  581. {
  582. COTSSample theSample = m_Sample;
  583. var pStatus = theSample.GetMsrStatus();
  584. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.COMPLT);
  585. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  586. // let main thread to know that this sample measurement completes
  587. ST_MSTMsg MsgSmplEnd = new ST_MSTMsg(m_Sample);
  588. MsgSmplEnd.InitSampleCompleteMsg();
  589. m_pMsrThread.SendMessageToMeasureGUI(MsgSmplEnd);
  590. string userdB = m_Sample.GetMsrParams().GetSTDName();
  591. if(userdB.ToLower() != "nostddb" && userdB.ToLower() != "nostddb.db")
  592. {
  593. try
  594. {
  595. string userdBpath= Application.StartupPath + "\\Config\\SysData\\" + m_Sample.GetMsrParams().GetSTDName() + ".db";
  596. File.Copy(userdBpath, m_pSampleRstFile.GetFieldFileSubFolderStr().Remove(m_pSampleRstFile.GetFieldFileSubFolderStr().IndexOf("FIELD_FILES")) + m_Sample.GetMsrParams().GetSTDName() + ".db", true);
  597. }
  598. catch(Exception ex)
  599. {
  600. MessageBox.Show("userdB copy failed:" + ex.ToString());
  601. }
  602. }
  603. }
  604. public bool DoMEasureForReMeasure()
  605. {
  606. return true;
  607. }
  608. public virtual void FieldImageProcess(COTSFieldData curFldData)
  609. {
  610. PointF fldCenter = curFldData.OTSPos;
  611. CSEMStageData a_pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  612. PointF semPos = new Point();
  613. a_pCSEMStageData.ConvertOTSToSEMCoord(fldCenter, ref semPos);
  614. curFldData.SetSemPos(semPos);
  615. //first step:remove background of the bse image and compound all the finded particles.
  616. log.Info("Begin to process image and get all particles!");
  617. GetOriginalParticles(ref curFldData);
  618. // second step :filter the finded particles.
  619. log.Info("Begin to filter particles!");
  620. FilterParticles(ref curFldData);
  621. CalculateParticleAbsolutPos(ref curFldData);
  622. log.Info("Begin to Calculate the image property of every particle!");
  623. var analysisparts = curFldData.GetListAnalysisParticles();
  624. curFldData.CalParticleImageProp(analysisparts);//calculate particle image property such as feret diameter, DMAX etc.
  625. return ;
  626. }
  627. private void FilterParticles(ref COTSFieldData curFldData)
  628. {
  629. var listXray = curFldData.GetAllParticles().OrderByDescending(x => x.GetActualArea()).ToList();
  630. var listXray1 = new List<COTSParticleClr>();
  631. var pXRayParam =m_Sample.GetMsrParams().GetXRayParam();
  632. if (listXray.Count > pXRayParam.GetXrayLimit())
  633. {
  634. for (var i = 0; i < pXRayParam.GetXrayLimit(); i++)
  635. {
  636. listXray1.Add(listXray[i]);
  637. }
  638. }
  639. else
  640. {
  641. listXray1 = listXray;
  642. }
  643. curFldData.SetListAnalysisParticles(listXray1);
  644. log.Info("Analysis particles:" + listXray1.Count);
  645. }
  646. public void GetOriginalParticles(ref COTSFieldData curFldData)
  647. {
  648. // get image process parameter
  649. CSampleParam pMsrParam = m_Sample.GetMsrParams();
  650. COTSImageProcParam pImgProcessParam = pMsrParam.GetImageProcessParam();
  651. var specialPartsparam = pMsrParam.GetSpecialGrayRangeParam();
  652. var pixelsize = m_Sample.CalculatePixelSize();
  653. if (specialPartsparam != null && specialPartsparam.GetIsToRun())
  654. {
  655. List<CSpecialGrayRange> ranges = pMsrParam.GetSpecialGrayRangeParam().GetSpecialGreyRanges();
  656. foreach (var grayRange in ranges)
  657. {
  658. CIntRangeClr range = new CIntRangeClr(grayRange.range.GetStart(), grayRange.range.GetEnd());
  659. CDoubleRangeClr diaRange = new CDoubleRangeClr(grayRange.diameterRange.GetStart(), grayRange.diameterRange.GetEnd());
  660. curFldData.GetPartsBySpecialGray(range, diaRange, pixelsize, grayRange.ifCollectXray);
  661. }
  662. }
  663. var ifCollectxray = pMsrParam.GetXRayParam().GetUsingXray();
  664. // remove BES image background
  665. curFldData.RemoveImgBGAndGetParticles(pImgProcessParam, pixelsize, ifCollectxray);
  666. // check if this is an empty image
  667. if (curFldData.NoParticle())
  668. { // empty fields
  669. log.Info("ImageProcess: empty field.");
  670. return;
  671. }
  672. double dPixelSize = m_Sample.CalculatePixelSize();
  673. curFldData.InitParticles(pImgProcessParam, dPixelSize);
  674. return;
  675. }
  676. public virtual void CollectParticlesXrayData(COTSFieldData curFldData)
  677. {
  678. var allParts = curFldData.GetListXrayParticles();
  679. var smallparts = new List<COTSParticleClr>();
  680. var bigparts = new List<COTSParticleClr>();
  681. double quantifyThreshold = m_Sample.GetMsrParams().GetXRayParam().GetFeatureModeMinSize();
  682. foreach (var part in allParts)
  683. {
  684. double equalCircleDiameter = Math.Sqrt(part.GetActualArea() / 3.14159) * 2f;
  685. if (equalCircleDiameter < quantifyThreshold)
  686. {
  687. smallparts.Add(part);
  688. }
  689. else
  690. {
  691. bigparts.Add(part);
  692. }
  693. }
  694. log.Info("SmallQuantifyParts (<" + quantifyThreshold.ToString("f2") + "): " + smallparts.Count);
  695. log.Info("BigQuantifyParts (>=" + quantifyThreshold.ToString("f2") + "): " + bigparts.Count);
  696. curFldData.CreateXrayList(smallparts);
  697. curFldData.CreateXrayList(bigparts);
  698. // get x-ray parameters
  699. COTSXRayParam pXRayParam = m_Sample.GetMsrParams().GetXRayParam();
  700. var workmode = pXRayParam.GetScanMode();
  701. if (workmode == OTS_X_RAY_SCAN_MODE.PointMode)
  702. {
  703. uint nXRayAQTime;
  704. if (bigparts.Count > 0)
  705. {
  706. nXRayAQTime = (uint)pXRayParam.GetMidAnalyAQTime();
  707. log.Info("Begin to collect xraydata:" + bigparts.Count +"(" +nXRayAQTime.ToString()+") on " + workmode.ToString());
  708. m_EDSController.GetXRayByParts(bigparts, nXRayAQTime, true);
  709. }
  710. if (smallparts.Count > 0)
  711. {
  712. Thread.Sleep(500);//add delay here,or else the eds system will halt.
  713. nXRayAQTime = (uint)pXRayParam.GetSmallPartXrayTime();
  714. log.Info("Begin to collect xraydata:" + smallparts.Count + "(" + nXRayAQTime.ToString() + ") on " + workmode.ToString());
  715. m_EDSController.GetXRayByParts(smallparts, nXRayAQTime, true);
  716. }
  717. }
  718. else if (workmode == OTS_X_RAY_SCAN_MODE.FeatureMode)
  719. {
  720. uint nXRayAQTime;
  721. if (bigparts.Count > 0)
  722. {
  723. log.Info("Begin to collect xraydata:" + bigparts.Count + " on " + OTS_X_RAY_SCAN_MODE.FeatureMode.ToString());
  724. nXRayAQTime = (uint)pXRayParam.GetMidAnalyAQTime();
  725. m_EDSController.GetXRayByFeatures(bigparts, nXRayAQTime, true);
  726. }
  727. if (smallparts.Count > 0)
  728. {
  729. Thread.Sleep(500);
  730. log.Info("Begin to collect xraydata:" + smallparts.Count + " on " + OTS_X_RAY_SCAN_MODE.PointMode.ToString());
  731. nXRayAQTime = (uint)pXRayParam.GetSmallPartXrayTime();
  732. m_EDSController.GetXRayByParts(smallparts, nXRayAQTime, true);
  733. }
  734. }
  735. return;
  736. }
  737. public virtual void QuantifyParticlesXrayData(COTSFieldData curFldData)
  738. {
  739. var parts = curFldData.GetListXrayParticles();
  740. foreach (var p in parts)
  741. {
  742. m_EDSController.QuantifyXrayByPart(p);
  743. }
  744. }
  745. public void DoHolePreview()
  746. {
  747. using (AutoResetSEMControl autoReset = new AutoResetSEMControl(this))
  748. {
  749. //----------memorize the state of sem
  750. var semstate = new SEMStateObject();
  751. double dMagnification = m_Sample.GetSEMDataMsr().GetMagnification();
  752. double wd = m_Sample.GetSEMDataMsr().GetWorkingDistance();
  753. ISemController sem = m_SemHardwareMgr;
  754. double posX=0, posY=0,posR=0;
  755. sem.GetSemPositionXY(ref posX, ref posY, ref posR);
  756. CSEMStageData a_pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  757. Point pos = new Point((int)posX, (int)posY);
  758. PointF otsPos = new Point(0, 0);
  759. if (!a_pCSEMStageData.ConvertSEMToOTSCoord(pos, ref otsPos))
  760. {
  761. return;
  762. }
  763. semstate.Pos = otsPos;
  764. semstate.Magnification = dMagnification;
  765. semstate.WorkingDistance = wd;
  766. autoReset.SemState = semstate;
  767. //-------------------------------------
  768. // let the main thread to know that this sample measurement starts
  769. CMsrSampleStatus pStatus = m_HolePreviewSample.GetMsrStatus();
  770. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.INPROCESS);
  771. // set current time to current time
  772. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.START);
  773. // get SEM controller to set magnification and working distance
  774. if (!SetSEMDataMrs(m_HolePreviewSample))
  775. {
  776. log.Error("DoHolePreview: fail to set SEM data.");
  777. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  778. // record end time
  779. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  780. return;
  781. }
  782. // set the BSE scan param
  783. if (!SetHoleBSEParam(m_HolePreviewSample))
  784. {
  785. log.Error("DoHolePreview: fail to set BSE param.");
  786. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  787. // record end time
  788. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  789. return;
  790. }
  791. // calculate field centers
  792. List<System.Drawing.Point> listFieldCenter;
  793. List<System.Drawing.Point> alllistFieldCenter ;
  794. if (!CalculateUnMeasuredFieldsCenters(m_HolePreviewSample, out alllistFieldCenter,out listFieldCenter))
  795. {// failed to calculate field centers
  796. log.Error("DoHolePreview: failed to calculate field centers.");
  797. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  798. // record end time
  799. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  800. return;
  801. }
  802. // go through each field
  803. //int nNewFieldId = 0;
  804. for ( int i = 0; i < listFieldCenter.Count; ++i)
  805. {// check and break if stop button is clicked
  806. if (IsAborted())
  807. {// measure stopped
  808. log.Trace("DoHolePreview: measure thread is stopped.");
  809. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.STOPPED);
  810. // record end time
  811. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  812. return;
  813. }
  814. // check if sample measurement completes
  815. COTSImgScanPrm pScanParam = m_Sample.GetMsrParams().GetImageScanParam();
  816. int nTotalFieldSize = listFieldCenter.Count;
  817. // get a field center
  818. System.Drawing.Point poiFieldCentre = listFieldCenter[i];
  819. // move SEM to the field center
  820. if (!MoveSEMToPoint(poiFieldCentre))
  821. {// failed to move SEM to the position
  822. log.Error("DoHolePreview: failed to move SEM to the field centre point.");
  823. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  824. // record end time
  825. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  826. return;
  827. }
  828. // take BSE image for the fields
  829. CBSEImgClr pBSEIamge = AcquireABSEImage();
  830. if (pBSEIamge==null)
  831. {
  832. // failed to acquire a BSE image
  833. log.Error("DoHolePreview: failed to acquire a BSE image.");
  834. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  835. // record end time
  836. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  837. return;
  838. }
  839. //BSEData
  840. ST_MSTMsg MsgFieldBSE=new ST_MSTMsg(m_HolePreviewSample);
  841. MsgFieldBSE.InitHolePreBSEDataMsg(pBSEIamge,poiFieldCentre);
  842. m_pMsrThread.SendHolePreviewMessageToMeasureGUI(MsgFieldBSE);
  843. if (pStatus.GetStatus() != OTS_MSR_SAMPLE_STATUS.INPROCESS)
  844. {
  845. // measurement failed or stopped
  846. log.Error("DoHolePreview: measurement failed or stopped.");
  847. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  848. // record end time
  849. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  850. return;
  851. }
  852. //save the result to project file
  853. Rectangle oImageRect = (Rectangle)pBSEIamge.GetImageRect();
  854. CHoleBSEImg pHoleBSEImg =new CHoleBSEImg(oImageRect,poiFieldCentre);
  855. pHoleBSEImg.SetImageData( pBSEIamge.GetImageDataPtr(),oImageRect.Width,oImageRect.Height);
  856. m_listHoleBSEImg.Add(pHoleBSEImg);
  857. }
  858. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  859. //calculate measure time
  860. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  861. // let main thread to know that this sample measurement completes
  862. ST_MSTMsg MsgSmplEnd=new ST_MSTMsg(m_HolePreviewSample);
  863. MsgSmplEnd.InitHolePreSampleEndMsg();
  864. m_pMsrThread.SendHolePreviewMessageToMeasureGUI(MsgSmplEnd);
  865. }
  866. }
  867. // Cumulate field data info
  868. public virtual bool CumulateFieldData( List<COTSParticleClr> listParticles, double a_nMeasuredArea)
  869. {
  870. // get measure result items of the sample
  871. CMsrResultItems pMsrResults = m_Sample.GetMsrResults();
  872. // cumulate field data info
  873. pMsrResults.CumulateMeasuredArea(a_nMeasuredArea);
  874. // cumulate ratio
  875. double dRatio = 10000 * pMsrResults.GetTotalParticleArea();
  876. dRatio = dRatio / pMsrResults.GetMeasuredArea();
  877. pMsrResults.SetRatio(dRatio);
  878. // go through the particles list
  879. foreach (COTSParticleClr pParticle in listParticles)
  880. {
  881. // create a measure result item
  882. pMsrResults.CumulateMeasureResult(pParticle);
  883. }
  884. m_Sample.SetMsrResults(pMsrResults);
  885. // ok, return TRUE
  886. return true;
  887. }
  888. public bool SaveFieldData(COTSFieldData fldData, string filedFileFoler)
  889. {
  890. string strFieldFileFolder = filedFileFoler;
  891. CBSEImageFileMgr pBSEImgFileMgr = new CBSEImageFileMgr();
  892. pBSEImgFileMgr.SetBSEImg(fldData.GetBSEImage());
  893. int nId = fldData.GetId();
  894. string sFieldId;
  895. sFieldId = nId.ToString();
  896. string strBSEFilePathname = strFieldFileFolder + "\\" + m_pSampleRstFile.SMPL_MSR_RESULT_FIELDS_BSE + sFieldId + pBSEImgFileMgr.BMP_IMG_FILE_EXT;
  897. if (!pBSEImgFileMgr.SaveIntoBitmap(strBSEFilePathname))
  898. {
  899. log.Error("SaveFieldFiles: save BSE file failed.");
  900. return false;
  901. }
  902. // IncA Data list
  903. CIncAFileMgr pDBFileMgr = m_pSampleRstFile.DBFileMgr;
  904. pDBFileMgr.SaveStatusDataToDB();
  905. var fldDB = pDBFileMgr.GetFieldDB();
  906. log.Warn("Start saving particle data.");
  907. var fldcmd = fldDB.GetSavingAFieldcmdObj(fldData.GetId(), fldData.GetOTSPosition(), fldData.GetSemPos());
  908. List<KeyValuePair<string, System.Data.SQLite.SQLiteParameter[]>> fldcmds = new List<KeyValuePair<string, System.Data.SQLite.SQLiteParameter[]>>();
  909. fldcmds.Add(fldcmd);
  910. pDBFileMgr.ExecuteNonQueryBatch(ref fldcmds);
  911. //remomove the invalid particles
  912. var cmds = pDBFileMgr.GetSavingParticleDataToDBCmds(fldData.GetListAnalysisParticles(), fldData.GetOTSPosition());
  913. pDBFileMgr.ExecuteNonQueryBatch(ref cmds);
  914. CPosXrayDBMgr PosXrayDBMgr = pDBFileMgr.GetPosXrayDBMgr();
  915. var listAnalysisPosXray = new List<CPosXrayClr>();
  916. foreach (var p in fldData.GetListXrayParticles())
  917. {
  918. listAnalysisPosXray.Add(p.GetXray());
  919. }
  920. var cmds1 = PosXrayDBMgr.GetSavingXrayCmds(listAnalysisPosXray, true);
  921. pDBFileMgr.ExecuteNonQueryBatch(ref cmds1);
  922. return true;
  923. }
  924. protected void SaveFieldMgrData()
  925. {
  926. while (bSaveThreadWorking)
  927. {
  928. while (fieldQueue.Count() > 0)
  929. {
  930. COTSFieldData f = fieldQueue.Dequeue();
  931. //save to disk first ,then pop . if the size is 0,then we know all the saving work is done.
  932. log.Info("Begin to save particles data! particle num:" + f.GetListAnalysisParticles().Count);
  933. SaveFieldData(f, m_pSampleRstFile.GetFieldFileSubFolderStr());
  934. }
  935. if (fieldQueue.Count() == 0)
  936. {
  937. bSaveThreadWorking = false; //must set this flag,so the main thread can know this thread has exited.
  938. log.Warn("finished batch saving");
  939. return;
  940. }
  941. }
  942. return;
  943. }
  944. private void SendFieldParticlesInfoToGUI(COTSFieldData curFld,CMsrSampleStatus pStatus)
  945. {
  946. double measuredArea = 0; // this area should be the field area
  947. var a_pBSEImg = curFld.GetBSEImage();
  948. double dPixelSize = curFld.GetPixelSize();
  949. measuredArea = a_pBSEImg.GetHeight() * a_pBSEImg.GetWidth() * dPixelSize * dPixelSize; //Get measured area
  950. CumulateFieldData(curFld.GetListAnalysisParticles(), measuredArea);
  951. log.Info("Send field data to screen!");
  952. pStatus.AddCompletedFieldCenter(curFld.OTSPos);
  953. //Field Data
  954. // record end time
  955. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  956. ST_MSTMsg MsgFieldEnd = new ST_MSTMsg(m_Sample,curFld);
  957. MsgFieldEnd.InitFieldDataMsg();
  958. m_pMsrThread.SendMessageToMeasureGUI(MsgFieldEnd);
  959. }
  960. protected void StartSaveFileThread(ref COTSFieldData a_pFieldMgr)
  961. {
  962. fieldQueue.Enqueue(a_pFieldMgr);
  963. if (fieldQueue.Count() > 0) //if there's data in the queue and the previous thread has finished then start a new thread.
  964. {
  965. if (bSaveThreadWorking == false)
  966. {
  967. bSaveThreadWorking = true;
  968. m_thread_ptr = new System.Threading.Thread(this.SaveFieldMgrData);//m_thread_ptr = shared_ptr<thread>(new thread(&CSmplMeasureInc::SaveFieldMgrData, this));
  969. m_thread_ptr.IsBackground = true;
  970. m_thread_ptr.Start();
  971. }
  972. }
  973. }
  974. public void ParticleSpecialTreatment(ref COTSFieldData fld)
  975. {
  976. //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.
  977. Dictionary<COTSParticleClr, int> mapPartXray = new Dictionary<COTSParticleClr, int>();
  978. double edsTime = 0; //to store the EDSTime returned from the engine.
  979. var bigparts = fld.GetListXrayParticles();
  980. var m_ClassifyEngine = new CClassifyEngine();
  981. for (int i = 0; i < bigparts.Count(); i++)
  982. {
  983. var engine = m_ClassifyEngine.GetExpressionClassifyEngine(m_Sample.GetMsrParams().GetSTDName());
  984. var p = bigparts[i];
  985. // 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.
  986. //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.
  987. edsTime = engine.IfNeedMaxEDS(p);
  988. if (edsTime > 0)
  989. {
  990. mapPartXray[p] = i;
  991. }
  992. }
  993. List<COTSParticleClr> partsMax = new List<COTSParticleClr>();
  994. if (mapPartXray.Count() > 0)
  995. {
  996. foreach (var p in mapPartXray)
  997. {
  998. partsMax.Add(p.Key);
  999. }
  1000. List<CPosXrayClr> maxEDSXrays = new List<CPosXrayClr>();
  1001. m_EDSController.GetXRayByFeatures(partsMax, edsTime, true);
  1002. }
  1003. }
  1004. public void CalculateParticleAbsolutPos(ref COTSFieldData curFldData)
  1005. {
  1006. double dPixelSize = m_Sample.CalculatePixelSize();
  1007. CSEMStageData pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  1008. foreach (var p in curFldData.GetListAnalysisParticles())
  1009. {
  1010. //Point fldOtsPos = new Point((curFldData.OTSPos.X, curFldData.OTSPos.Y);
  1011. PointF semP = new PointF(); ;
  1012. Point semPos = new Point();
  1013. pCSEMStageData.ConvertOTSToSEMCoord(curFldData.OTSPos, ref semP);
  1014. semPos.X = (int)semP.X;
  1015. semPos.Y = (int)semP.Y;
  1016. p.SetAbsolutPos(semPos);
  1017. }
  1018. }
  1019. }
  1020. }