CSmplMeasure.cs 56 KB

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