CSmplMeasure.cs 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410
  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. if (hardWareDelay > 0)
  334. {
  335. Thread.Sleep(hardWareDelay);
  336. }
  337. return true;
  338. }
  339. // Acquire a BSE image
  340. CBSEImgClr AcquireABSEImage()
  341. {
  342. // BSE image
  343. CBSEImgClr pBSEImage = null;
  344. // get scan controller
  345. var pScanController = m_ScanHardwareMgr;
  346. pBSEImage = pScanController.AcquireBSEImage();
  347. return pBSEImage;
  348. }
  349. public bool IsLowCounts(COTSParticleClr particle)
  350. {
  351. COTSXRayParam pXRayParam = m_Sample.GetMsrParams().GetXRayParam();
  352. if (pXRayParam.GetUsingXray() == true)
  353. {
  354. var thecount = particle.GetXray().GetTotalCount();
  355. var expect = pXRayParam.GetAnalyExpCount();
  356. if (thecount < expect)
  357. {
  358. particle.SetType((int)OTS_PARTICLE_TYPE.LOW_COUNT);
  359. particle.SetTypeColor("#000000");
  360. particle.SetTypeName("LowCounts");
  361. return true;
  362. }
  363. }
  364. return false;
  365. }
  366. public virtual void ClassifyFieldParticles(COTSFieldData curFldData)
  367. {
  368. return;
  369. }
  370. private class SEMStateObject
  371. {
  372. private PointF pos;
  373. private double workingDistance;
  374. private CSEMDataGnr semdata;
  375. private COTSSample originalSample;
  376. private double magnification;
  377. public PointF Pos { get => pos; set => pos = value; }
  378. public double WorkingDistance { get => workingDistance; set => workingDistance = value; }
  379. public CSEMDataGnr Semdata { get => semdata; set => semdata = value; }
  380. public double Magnification { get => magnification; set => magnification = value; }
  381. public COTSSample OriginalSample { get => originalSample; set => originalSample = value; }
  382. }
  383. private class AutoResetSEMControl:IDisposable
  384. {
  385. CSmplMeasure sm;
  386. private SEMStateObject semState=null;
  387. public AutoResetSEMControl(CSmplMeasure s)
  388. {
  389. sm = s;
  390. }
  391. public SEMStateObject SemState
  392. {
  393. get => semState;
  394. set => semState = value;
  395. }
  396. public void Dispose()
  397. {
  398. if (semState != null)
  399. {
  400. sm.SetBSEParam();
  401. sm.m_SemHardwareMgr.SetMagnification(semState.Magnification);
  402. Thread.Sleep(100);
  403. sm.MoveSEMToPoint(semState.Pos);
  404. Thread.Sleep(100);
  405. sm.m_SemHardwareMgr.SetWorkingDistance(semState.WorkingDistance);
  406. }
  407. sm.SetSEMExteralOff();
  408. }
  409. }
  410. private bool GetSEMDataGnrFromHw(ref CSEMDataGnr SemDataGnr)
  411. {
  412. double kv = 0, brightness = 0, contrast = 0;
  413. var hw = SemController.GetSEMController();
  414. hw.GetSemHighTension(ref kv);
  415. hw.GetSemBrightness(ref brightness);
  416. hw.GetSemContrast(ref contrast);
  417. SemDataGnr.SetValue(kv, brightness, contrast);
  418. return true;
  419. }
  420. public void DoMeasureForOneSample()
  421. {
  422. using (AutoResetSEMControl autoReset = new AutoResetSEMControl(this)) //when this method exit ,the SetSEMExternalOff and ResetScan will be called automatically.
  423. {
  424. // let the main thread to know that this sample measurement starts
  425. var pStatus = m_Sample.GetMsrStatus();
  426. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.INPROCESS);
  427. // set current time to current time
  428. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.START);
  429. // let main App know that the sample begin to measure
  430. ST_MSTMsg MsgSmpStart = new ST_MSTMsg(m_Sample);
  431. MsgSmpStart.InitSampleStartMsg();
  432. m_pMsrThread.SendMessageToMeasureGUI(MsgSmpStart);
  433. log.Info(m_Sample.GetName() + " Measurement started!");
  434. // get SEM controller to set magnification and working distance
  435. if (!SetSEMDataMrs(m_Sample))
  436. {
  437. log.Error("DoMeasure: fail to set SEM data.");
  438. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  439. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  440. return;
  441. }
  442. // set the BSE scan param
  443. if (!SetBSEParam())
  444. {
  445. log.Error("DoMeasure: fail to set BSE param.");
  446. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  447. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  448. return;
  449. }
  450. var pSEMDataGnr = new CSEMDataGnr();
  451. log.Info("Get Kv, Brightness and Contrast!");
  452. GetSEMDataGnrFromHw(ref pSEMDataGnr);
  453. m_pSampleRstFile.SetSEMGnr(pSEMDataGnr);
  454. // record SEM data
  455. COTSMsrPrjResultData pProjMgrFile = m_pMsrThread.GetProjResultData();
  456. CSEMStageData pSEMStageData = pProjMgrFile.GetSEMStageData();
  457. m_pSampleRstFile.SetSEMStageData(pSEMStageData);
  458. // record stage
  459. CStage pStage = pProjMgrFile.GetStage();
  460. m_pSampleRstFile.SetSEMStage(pStage);
  461. //-----save the static measure result file data into xml file and the dynamic data of every field will be saved into sqlite database
  462. log.Info("Create result file!");
  463. if (!m_pSampleRstFile.CreateResultFiles())
  464. {// failed to call measure result file Save method
  465. log.Error("DoMeasure: failed to call measure result file Save method.");
  466. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  467. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  468. return;
  469. }
  470. //------
  471. var FldDatas = m_Sample.GetFieldsData();
  472. if (m_EDSController.GetEDSType()==EDSTYPE.BRUKER && m_Sample.GetMsrParams().GetXRayParam().GetUseFilter())
  473. {
  474. List<string> KeyNameList = new List<string>();
  475. string DBAddress = Application.StartupPath + "\\Config\\SysData\\" + m_Sample.GetMsrParams().GetSTDName() + ".db";
  476. SQLiteHelper sQLiteHelper = new SQLiteHelper(DBAddress);//
  477. KeyNameList = sQLiteHelper.GetDBKeyElementList("ClassifySTD", "KeyElementList");
  478. m_EDSController.SetFilterKeyEleNames( KeyNameList);//will decide if do the quantification according to this keyelelist.
  479. }
  480. for (int i = 0; i < FldDatas.Count; ++i)
  481. {// check and break if stop button is clicked
  482. var curFld = FldDatas[i];
  483. if (curFld.GetIsMeasureComplete())
  484. {
  485. continue;
  486. }
  487. if (IsPaused())
  488. {// measure stopped
  489. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.PAUSED);
  490. // record end time
  491. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  492. //must wait for the saving data thread finished,or we'll get null pointer exception when we stop the measure process.
  493. while (fieldQueue.Count() > 0)
  494. {
  495. Thread.Sleep(100);
  496. }
  497. SetSEMExteralOff();
  498. // update thread measure status class, let the main thread know that this sample measurement stopped
  499. ST_MSTMsg MsgSmpStop = new ST_MSTMsg(m_Sample);
  500. MsgSmpStop.InitSamplePausedMsg();
  501. m_pMsrThread.SendMessageToMeasureGUI(MsgSmpStop);
  502. while (IsPaused())
  503. {
  504. Thread.Sleep(300);
  505. }
  506. }
  507. if (IsAborted())
  508. {// measure stopped
  509. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.STOPPED);
  510. // record end time
  511. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  512. //must wait for the saving data thread finished,or we'll get null pointer exception when we stop the measure process.
  513. while (fieldQueue.Count() > 0)
  514. {
  515. Thread.Sleep(100);
  516. }
  517. break;
  518. }
  519. // check if sample measurement completes
  520. COTSImgScanPrm pScanParam = m_Sample.GetMsrParams().GetImageScanParam();
  521. if (IsSampleOver(pScanParam))
  522. {
  523. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  524. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.COMPLT);
  525. break;
  526. }
  527. // get a field center
  528. System.Drawing.PointF poiFieldCentre = curFld.GetOTSPosition();
  529. // update thread measure status class, let the main thread know that starts a new field
  530. ST_MSTMsg MsgFieldStart = new ST_MSTMsg(m_Sample,curFld);
  531. MsgFieldStart.InitFieldStartMsg();
  532. m_pMsrThread.SendMessageToMeasureGUI(MsgFieldStart);
  533. int fldNo = curFld.GetId();
  534. log.Warn("Current field:" + fldNo.ToString());
  535. // move SEM to the field center
  536. if (!MoveSEMToPoint(poiFieldCentre))
  537. {// failed to move SEM to the position
  538. log.Error("DoMeasure: failed to move SEM to the field centre point.");
  539. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  540. // record end time
  541. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.STOPPED);
  542. return;
  543. }
  544. log.Info("Begin to Acquire BSE image!");
  545. // take BSE image for the fields
  546. CBSEImgClr pBSEImg = AcquireABSEImage();
  547. // let the main thread to know that image process is completed
  548. if (pBSEImg == null)
  549. {
  550. log.Error("ImageProcess: can't get no background image.");
  551. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.FAILED);
  552. return;
  553. }
  554. curFld.SetBSEImage(pBSEImg);
  555. //BSEData
  556. ST_MSTMsg MsgFieldBSE = new ST_MSTMsg(m_Sample,curFld);
  557. MsgFieldBSE.InitFieldBSEImageMsg();
  558. m_pMsrThread.SendMessageToMeasureGUI(MsgFieldBSE);
  559. log.Info("Acquire BSE image success! Processing image...");
  560. // image process
  561. FieldImageProcess(curFld);
  562. COTSXRayParam pXRayParam = m_Sample.GetMsrParams().GetXRayParam();
  563. if (pXRayParam.GetUsingXray() == true)
  564. {
  565. CollectParticlesXrayData(curFld);
  566. }
  567. m_Sample.GetMsrStatus().SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  568. curFld.SetIsMeasureComplete(true);
  569. log.Info("Begin to classify particles! particle num:" + curFld.GetListAnalysisParticles().Count);
  570. ClassifyFieldParticles(curFld);
  571. //start db save
  572. StartSaveFileThread(ref curFld);
  573. SendFieldParticlesInfoToGUI(curFld, m_Sample.GetMsrStatus());
  574. }
  575. while (bSaveThreadWorking)//wait untill all the field data has been saved.
  576. {
  577. Thread.Sleep(1000);
  578. log.Warn("db saving!");
  579. }
  580. TheLastWorkOfSampleMeasure();
  581. }
  582. }
  583. public void TheLastWorkOfSampleMeasure()
  584. {
  585. COTSSample theSample = m_Sample;
  586. var pStatus = theSample.GetMsrStatus();
  587. pStatus.ComputeTime(OTS_MSR_TIME_TYPE.COMPLT);
  588. pStatus.SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  589. // let main thread to know that this sample measurement completes
  590. ST_MSTMsg MsgSmplEnd = new ST_MSTMsg(m_Sample);
  591. MsgSmplEnd.InitSampleCompleteMsg();
  592. m_pMsrThread.SendMessageToMeasureGUI(MsgSmplEnd);
  593. string userdB = m_Sample.GetMsrParams().GetSTDName();
  594. if(userdB.ToLower() != "nostddb" && userdB.ToLower() != "nostddb.db")
  595. {
  596. try
  597. {
  598. string userdBpath= Application.StartupPath + "\\Config\\SysData\\" + m_Sample.GetMsrParams().GetSTDName() + ".db";
  599. File.Copy(userdBpath, m_pSampleRstFile.GetFieldFileSubFolderStr().Remove(m_pSampleRstFile.GetFieldFileSubFolderStr().IndexOf("FIELD_FILES")) + m_Sample.GetMsrParams().GetSTDName() + ".db", true);
  600. }
  601. catch(Exception ex)
  602. {
  603. MessageBox.Show("userdB copy failed:" + ex.ToString());
  604. }
  605. }
  606. }
  607. public bool DoMEasureForReMeasure()
  608. {
  609. return true;
  610. }
  611. public virtual void FieldImageProcess(COTSFieldData curFldData)
  612. {
  613. PointF fldCenter = curFldData.OTSPos;
  614. CSEMStageData a_pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  615. PointF semPos = new Point();
  616. a_pCSEMStageData.ConvertOTSToSEMCoord(fldCenter, ref semPos);
  617. curFldData.SetSemPos(semPos);
  618. //first step:remove background of the bse image and compound all the finded particles.
  619. log.Info("Begin to process image and get all particles!");
  620. GetOriginalParticles(ref curFldData);
  621. // second step :filter the finded particles.
  622. log.Info("Begin to filter particles!");
  623. FilterParticles(ref curFldData);
  624. CalculateParticleAbsolutPos(ref curFldData);
  625. log.Info("Begin to Calculate the image property of every particle!");
  626. var analysisparts = curFldData.GetListAnalysisParticles();
  627. curFldData.CalParticleImageProp(analysisparts);//calculate particle image property such as feret diameter, DMAX etc.
  628. return ;
  629. }
  630. private void FilterParticles(ref COTSFieldData curFldData)
  631. {
  632. var listXray = curFldData.GetAllParticles().OrderByDescending(x => x.GetActualArea()).ToList();
  633. var listXray1 = new List<COTSParticleClr>();
  634. var pXRayParam =m_Sample.GetMsrParams().GetXRayParam();
  635. if (listXray.Count > pXRayParam.GetXrayLimit())
  636. {
  637. for (var i = 0; i < pXRayParam.GetXrayLimit(); i++)
  638. {
  639. listXray1.Add(listXray[i]);
  640. }
  641. }
  642. else
  643. {
  644. listXray1 = listXray;
  645. }
  646. curFldData.SetListAnalysisParticles(listXray1);
  647. log.Info("Analysis particles:" + listXray1.Count);
  648. }
  649. public void GetOriginalParticles(ref COTSFieldData curFldData)
  650. {
  651. // get image process parameter
  652. CSampleParam pMsrParam = m_Sample.GetMsrParams();
  653. COTSImageProcParam pImgProcessParam = pMsrParam.GetImageProcessParam();
  654. var specialPartsparam = pMsrParam.GetSpecialGrayRangeParam();
  655. var pixelsize = m_Sample.CalculatePixelSize();
  656. if (specialPartsparam != null && specialPartsparam.GetIsToRun())
  657. {
  658. List<CSpecialGrayRange> ranges = pMsrParam.GetSpecialGrayRangeParam().GetSpecialGreyRanges();
  659. foreach (var grayRange in ranges)
  660. {
  661. CIntRangeClr range = new CIntRangeClr(grayRange.range.GetStart(), grayRange.range.GetEnd());
  662. CDoubleRangeClr diaRange = new CDoubleRangeClr(grayRange.diameterRange.GetStart(), grayRange.diameterRange.GetEnd());
  663. curFldData.GetPartsBySpecialGray(range, diaRange, pixelsize, grayRange.ifCollectXray);
  664. }
  665. }
  666. var ifCollectxray = pMsrParam.GetXRayParam().GetUsingXray();
  667. // remove BES image background
  668. curFldData.RemoveImgBGAndGetParticles(pImgProcessParam, pixelsize, ifCollectxray);
  669. // check if this is an empty image
  670. if (curFldData.NoParticle())
  671. { // empty fields
  672. log.Info("ImageProcess: empty field.");
  673. return;
  674. }
  675. double dPixelSize = m_Sample.CalculatePixelSize();
  676. curFldData.InitParticles(pImgProcessParam, dPixelSize);
  677. return;
  678. }
  679. public virtual void CollectParticlesXrayData(COTSFieldData curFldData)
  680. {
  681. var allParts = curFldData.GetListXrayParticles();
  682. var smallparts = new List<COTSParticleClr>();
  683. var bigparts = new List<COTSParticleClr>();
  684. double quantifyThreshold = m_Sample.GetMsrParams().GetXRayParam().GetFeatureModeMinSize();
  685. foreach (var part in allParts)
  686. {
  687. double equalCircleDiameter = Math.Sqrt(part.GetActualArea() / 3.14159) * 2f;
  688. if (equalCircleDiameter < quantifyThreshold)
  689. {
  690. smallparts.Add(part);
  691. }
  692. else
  693. {
  694. bigparts.Add(part);
  695. }
  696. }
  697. log.Info("SmallQuantifyParts (<" + quantifyThreshold.ToString("f2") + "): " + smallparts.Count);
  698. log.Info("BigQuantifyParts (>=" + quantifyThreshold.ToString("f2") + "): " + bigparts.Count);
  699. curFldData.CreateXrayList(smallparts);
  700. curFldData.CreateXrayList(bigparts);
  701. // get x-ray parameters
  702. COTSXRayParam pXRayParam = m_Sample.GetMsrParams().GetXRayParam();
  703. var workmode = pXRayParam.GetScanMode();
  704. if (workmode == OTS_X_RAY_SCAN_MODE.PointMode)
  705. {
  706. uint nXRayAQTime;
  707. if (bigparts.Count > 0)
  708. {
  709. nXRayAQTime = (uint)pXRayParam.GetMidAnalyAQTime();
  710. log.Info("Begin to collect xraydata:" + bigparts.Count +"(" +nXRayAQTime.ToString()+") on " + workmode.ToString());
  711. m_EDSController.GetXRayByParts(bigparts, nXRayAQTime, true);
  712. }
  713. if (smallparts.Count > 0)
  714. {
  715. Thread.Sleep(500);//add delay here,or else the eds system will halt.
  716. nXRayAQTime = (uint)pXRayParam.GetSmallPartXrayTime();
  717. log.Info("Begin to collect xraydata:" + smallparts.Count + "(" + nXRayAQTime.ToString() + ") on " + workmode.ToString());
  718. m_EDSController.GetXRayByParts(smallparts, nXRayAQTime, true);
  719. }
  720. }
  721. else if (workmode == OTS_X_RAY_SCAN_MODE.FeatureMode)
  722. {
  723. uint nXRayAQTime;
  724. if (bigparts.Count > 0)
  725. {
  726. log.Info("Begin to collect xraydata:" + bigparts.Count + " on " + OTS_X_RAY_SCAN_MODE.FeatureMode.ToString());
  727. nXRayAQTime = (uint)pXRayParam.GetMidAnalyAQTime();
  728. m_EDSController.GetXRayByFeatures(bigparts, nXRayAQTime, true);
  729. }
  730. if (smallparts.Count > 0)
  731. {
  732. Thread.Sleep(500);
  733. log.Info("Begin to collect xraydata:" + smallparts.Count + " on " + OTS_X_RAY_SCAN_MODE.PointMode.ToString());
  734. nXRayAQTime = (uint)pXRayParam.GetSmallPartXrayTime();
  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. }