CSmplMeasure.cs 58 KB

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