CSmplMeasure.cs 57 KB

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