CSmplMeasure.cs 56 KB

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