CSmplMeasure.cs 50 KB

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