CSmplMeasure.cs 56 KB

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