SmplMeasureCleanliness.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.SQLite;
  4. using System.Drawing;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using OTSCLRINTERFACE;
  10. using OTSDataType;
  11. using OTSModelSharp.Measure.OTSCleanliness;
  12. using OTSModelSharp.ServiceCenter;
  13. using OTSModelSharp.ServiceInterface;
  14. using static OTSDataType.otsdataconst;
  15. namespace OTSModelSharp
  16. {
  17. class CSmplMeasureCleanliness : CSmplMeasure
  18. {
  19. public CSmplMeasureCleanliness(string a_strWorkingFolder, COTSSample a_pSample):base(a_strWorkingFolder,a_pSample)
  20. {
  21. SetWorkingFolder(a_strWorkingFolder);
  22. SetSample(a_pSample);
  23. m_classifyEngine = new CClassifyEngine();
  24. }
  25. // field image process
  26. public override bool FieldImageProcess(Point fldCenter, CBSEImgClr a_BSEImg)
  27. {
  28. int nNewFieldId;
  29. nNewFieldId = m_pSampleRstFile.GetIdForANewField();
  30. //first step:remove background of the bse image and compound all the finded particles.
  31. curFldData = new CFieldDataClean( a_BSEImg,m_Sample.CalculatePixelSize());
  32. CFieldDataClean curFldDataCln =( CFieldDataClean) curFldData;
  33. curFldData.SetId(nNewFieldId);
  34. curFldData.SetOTSPosition(fldCenter);
  35. Point semPos = new Point();
  36. CSEMStageData a_pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  37. a_pCSEMStageData.ConverOTSToSEMPoint(fldCenter, ref semPos);
  38. curFldData.SemPos = semPos;
  39. GetOriginalParticles();
  40. // second step :filter the finded particles.
  41. FilterParticles((CFieldDataClean)curFldData);
  42. CalculateParticleAbsolutPos();
  43. log.Info("Begin to Calculate the image property of every particle!");
  44. var analysisparts = curFldDataCln.ListBigParticles;
  45. curFldData.CalParticleImageProp(analysisparts);//calculate particle image property such as feret diameter, DMAX etc
  46. COTSXRayParam pXRayParam = m_Sample.GetMsrParams().GetXRayParam();
  47. //collect xray data.
  48. if (pXRayParam.GetUsingXray() == true)
  49. {
  50. Thread.Sleep(100);
  51. CollectParticlesXrayData((CFieldDataClean)curFldData);
  52. Thread.Sleep(100);
  53. }
  54. ClassifyFieldParticles();
  55. // save field files
  56. m_Sample.GetMsrStatus() .SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  57. StartSaveFileThread(curFldData);
  58. return true;
  59. }
  60. // save field data
  61. //protected void SaveFieldMgrData()
  62. //{
  63. // while (bSaveThreadWorking)
  64. // {
  65. // while (fieldQueue.Count() > 0)
  66. // {
  67. // CFieldDataClean f = (CFieldDataClean) fieldQueue.Dequeue();
  68. // //double pixelSize = m_Sample.CalculatePixelSize();
  69. // //save to disk first ,then pop . if the size is 0,then we know all the saving work is done.
  70. // SaveFieldData(f,m_pSampleRstFile.GetFieldFileSubFolderStr());
  71. // }
  72. // if (fieldQueue.Count() == 0)
  73. // {
  74. // bSaveThreadWorking = false; //must set this flag,so the main thread can know this thread has exited.
  75. // return;
  76. // }
  77. // }
  78. // return;
  79. //}
  80. //protected void StartSaveFileThread(COTSFieldData a_pFieldMgr)
  81. //{
  82. // fieldQueue.Enqueue(a_pFieldMgr);
  83. // if (fieldQueue.Count() > 0) //if there's data in the queue and the previous thread has finished then start a new thread.
  84. // {
  85. // if (bSaveThreadWorking == false)
  86. // {
  87. // bSaveThreadWorking = true;
  88. // m_thread_ptr = new System.Threading.Thread(this.SaveFieldMgrData);//m_thread_ptr = shared_ptr<thread>(new thread(&CSmplMeasureInc::SaveFieldMgrData, this));
  89. // m_thread_ptr.Start();//m_thread_ptr->detach();
  90. // }
  91. // }
  92. //}
  93. public void FilterParticles(CFieldDataClean fld)
  94. {
  95. // 1)remove over sized particles, too small particles and get a analysis particles list
  96. double dPixelSize = m_Sample.CalculatePixelSize();
  97. CSampleParam pMsrParam = m_Sample.GetMsrParams();
  98. COTSImageProcParam pImgProcessParam = pMsrParam.GetImageProcessParam();
  99. curFldData.InitParticles(pImgProcessParam, dPixelSize);
  100. if (curFldData.NoAnalysisParticle())
  101. {
  102. log.Warn("There's no analysis particles!");
  103. }
  104. var listXray = curFldData.GetListAnalysisParticles().OrderByDescending(x => x.GetActualArea()).ToList();
  105. var listXray1 = new List<COTSParticleClr>();
  106. var pXRayParam = pMsrParam.GetXRayParam();
  107. if (listXray.Count > pXRayParam.GetXrayLimit())
  108. {
  109. for (var i = 0; i < pXRayParam.GetXrayLimit(); i++)
  110. {
  111. listXray1.Add(listXray[i]);
  112. }
  113. }
  114. else
  115. {
  116. listXray1 = listXray;
  117. }
  118. //2) according to the quantify threshold size value saperate the analysis particles into two group :the bigparticles and the smallparticles.
  119. double quantifyThreshold = m_Sample.GetMsrParams().GetXRayParam().GetFeatureModeMinSize();
  120. var smallparts = fld.ListSmallParticles;
  121. var bigparts = fld.ListBigParticles;
  122. foreach (var part in listXray1)
  123. {
  124. double equalCircleDiameter = Math.Sqrt(part.GetActualArea() / 3.14159) * 2f;
  125. if (equalCircleDiameter < quantifyThreshold)
  126. {
  127. smallparts.Add(part);
  128. }
  129. else
  130. {
  131. bigparts.Add(part);
  132. }
  133. }
  134. fld.ListSmallParticles = smallparts;
  135. fld.ListBigParticles = bigparts;
  136. //fld.SmallParticlePercentage=percentage;
  137. log.Info("SmallQuantifyParts (<" + quantifyThreshold.ToString("f2") + "): " + smallparts.Count);
  138. log.Info("BigQuantifyParts (>=" + quantifyThreshold.ToString("f2")+ "): " + bigparts.Count);
  139. return ;
  140. }
  141. public void CollectParticlesXrayData(CFieldDataClean fld)
  142. {
  143. // get x-ray parameters
  144. COTSXRayParam pXRayParam = m_Sample.GetMsrParams ().GetXRayParam();
  145. // calculate search x-ray acquire time
  146. uint nXRayAQTime;
  147. var smallparts = fld.ListSmallParticles;
  148. var bigparts = fld.ListBigParticles;
  149. var pStatus = m_Sample.GetMsrStatus();
  150. curFldData.CreateXrayList(bigparts); // big particle using the full xray strategy.
  151. curFldData.CreateXrayList(smallparts); //small particle using the fast xray strategy
  152. if (bigparts.Count > 0)
  153. {
  154. var workmode = pXRayParam.GetScanMode();
  155. // get x-ray list (analysis) by particle features
  156. if (workmode == OTS_X_RAY_SCAN_MODE.FeatureMode)
  157. {
  158. nXRayAQTime = (uint)pXRayParam.GetMidAnalyAQTime();
  159. m_EDSHardwareMgr.GetXRayByFeatures(bigparts, nXRayAQTime, true);
  160. }
  161. else
  162. {
  163. nXRayAQTime = (uint)pXRayParam.GetMidAnalyAQTime();
  164. m_EDSHardwareMgr.GetXRayByPoints(bigparts, nXRayAQTime, true);
  165. }
  166. }
  167. if (smallparts.Count > 0)
  168. {
  169. nXRayAQTime = (uint)pXRayParam.GetFastXrayTime();
  170. // get x-ray list (analysis) by points
  171. m_EDSHardwareMgr.GetXRayByPoints(smallparts, nXRayAQTime, true);
  172. }
  173. return ;
  174. }
  175. public override void ClassifyFieldParticles()
  176. {
  177. try
  178. {
  179. CFieldDataClean curFldDataCln = (CFieldDataClean)curFldData;
  180. string libname = m_Sample.GetMsrParams().GetSTDName();
  181. if (libname != "NoSTDDB")
  182. {
  183. log.Info("Begin to classify big particles!Using " + libname);
  184. var bigparts = curFldDataCln.ListBigParticles;
  185. ClassifyQuantifyParticles(bigparts,libname);
  186. var smallParts = curFldDataCln.ListSmallParticles;
  187. ClassifyQuantifyParticles(smallParts, libname);
  188. }
  189. }
  190. catch (Exception e)
  191. {
  192. log.Info("calcu the particle image property or classify failed. "+e.Message);
  193. }
  194. }
  195. public override void ClassifyMergedParticles(List<COTSParticleClr> mergedParts)
  196. {
  197. try
  198. {
  199. string libname = m_Sample.GetMsrParams().GetSTDName();
  200. if (libname != "NoSTDDB")
  201. {
  202. log.Info("Begin to classify big particles!Using " + libname);
  203. var bigparts = mergedParts;
  204. ClassifyQuantifyParticles(bigparts, libname);
  205. }
  206. }
  207. catch (Exception e)
  208. {
  209. log.Info("calcu the particle image property or classify failed. " + e.Message);
  210. }
  211. }
  212. public void GetOriginalParticles()
  213. {
  214. // measure status
  215. CMsrSampleStatus pStatus = m_Sample.GetMsrStatus();
  216. // get image process parameter
  217. CSampleParam pMsrParam = m_Sample.GetMsrParams();
  218. COTSImageProcParam pImgProcessParam = pMsrParam.GetImageProcessParam();
  219. var dPixelsize = m_Sample.CalculatePixelSize();
  220. // remove BES image background
  221. curFldData.RemoveImgBGAndGetParticles(pImgProcessParam, dPixelsize);
  222. // check if this is an empty image
  223. if (curFldData.NoParticle())
  224. { // empty fields
  225. log.Info("ImageProcess: empty field.");
  226. //return false;
  227. }
  228. return ;
  229. }
  230. public bool ClassifyQuantifyParticles(List<COTSParticleClr> a_listAnalysisParticles, string libname)// classify particles
  231. {
  232. int nSize = (int)a_listAnalysisParticles.Count();
  233. // go through all analysis particles
  234. for (int i = 0; i < nSize; ++i)
  235. {
  236. COTSParticleClr pParticle = a_listAnalysisParticles[i];
  237. IClassifyEngine engine = m_classifyEngine.GetParticleEngine(libname);
  238. if (!engine.Classify(pParticle))
  239. {
  240. // invalid x-ray pointer.
  241. log.Info("ClassifyParticle: can't identify the particle as any classified id.");
  242. return false;
  243. }
  244. }
  245. return true;
  246. }
  247. public bool ClassifySmallParticles(List<COTSParticleClr> smallparts, string libname)
  248. {
  249. List<COTSParticleClr> a_listAnalysisParticles = smallparts;
  250. int nSize = (int)a_listAnalysisParticles.Count();
  251. // go through all analysis particles
  252. for (int i = 0; i < nSize; ++i)
  253. {
  254. COTSParticleClr pParticle = a_listAnalysisParticles[i];
  255. IClassifyEngine engine = m_classifyEngine.GetCurveCompareEngine(libname);
  256. if (!engine.Classify(pParticle))
  257. {
  258. log.Info("ClassifyParticle: can't identify the particle as any inclusion.");
  259. return false;
  260. }
  261. }
  262. return true;
  263. }
  264. //public bool SaveFieldFiles(CFieldDataClean fldData, string filedFileFoler)
  265. //{
  266. // string strFieldFileFolder = filedFileFoler;
  267. // CBSEImageFileMgr pBSEImgFileMgr = new CBSEImageFileMgr();
  268. // pBSEImgFileMgr.SetBSEImg(fldData.GetBSEImage());
  269. // int nId = fldData.GetId();
  270. // string sFieldId;
  271. // sFieldId = nId.ToString();
  272. // string strBSEFilePathname = strFieldFileFolder + "\\" + m_pSampleRstFile.SMPL_MSR_RESULT_FIELDS_BSE + sFieldId + pBSEImgFileMgr.BMP_IMG_FILE_EXT;
  273. // if (!pBSEImgFileMgr.SaveIntoBitmap(strBSEFilePathname))
  274. // {
  275. // log.Info("SaveFieldFiles: save BSE file failed.");
  276. // return false;
  277. // }
  278. // // IncA Data list
  279. // CIncAFileMgr pDBFileMgr = m_pSampleRstFile.DBFileMgr;
  280. // pDBFileMgr.SaveStatusDataToDB();
  281. // var fldDB = pDBFileMgr.GetFieldDB();
  282. // fldDB.SaveAField(fldData.GetId(), fldData.GetOTSPosition(),fldData.SemPos);
  283. // var analysisParts = fldData.GetListAnalysisParticles();
  284. // if (!pDBFileMgr.SaveIncADataToDB(analysisParts, fldData.GetOTSPosition()))
  285. // {
  286. // log.Info("SaveFieldFiles: save inclusion file failed.");
  287. // return false;
  288. // }
  289. // //save the xray data and element data.
  290. // CPosXrayDBMgr PosXrayDBMgr = pDBFileMgr.GetPosXrayDBMgr();
  291. // var m_listAnalysisPosXray = new List<CPosXrayClr>();
  292. // foreach (var p in analysisParts)
  293. // {
  294. // m_listAnalysisPosXray.Add(p.GetXray());
  295. // }
  296. // if (!PosXrayDBMgr.SaveXray(m_listAnalysisPosXray, true))
  297. // {
  298. // log.Info("SaveFieldFiles: save analysis x-ray failed.");
  299. // return false;
  300. // }
  301. // //save the small particle info
  302. // //CSmallParticleInfoDB smallPartDB = pDBFileMgr.GetSmallParticleInfoDB();
  303. // //Dictionary<int, List<COTSParticleClr>> mapSmallParts = new Dictionary<int, List<COTSParticleClr>>();
  304. // //foreach (COTSParticleClr smallP in fldData.ListSmallParticles)
  305. // //{
  306. // // if (mapSmallParts.ContainsKey((int)smallP.GetType()))
  307. // // {
  308. // // mapSmallParts[(int)smallP.GetType()].Add(smallP);
  309. // // }
  310. // // else
  311. // // {
  312. // // var plist = new List<COTSParticleClr>();
  313. // // plist.Add(smallP);
  314. // // mapSmallParts.Add((int)smallP.GetType(), plist);
  315. // // }
  316. // //}
  317. // //List<CSmallParticleInfo> smallparts = new List<CSmallParticleInfo>();
  318. // //foreach (KeyValuePair<int, List<COTSParticleClr>> particles in mapSmallParts)
  319. // //{
  320. // // CSmallParticleInfo partInfo = new CSmallParticleInfo();
  321. // // partInfo.FieldId = particles.Value[0].GetFieldId();
  322. // // partInfo.AveGray = particles.Value[0].GetAveGray();
  323. // // partInfo.Quantity = (int)(particles.Value.Count / fldData.SmallParticlePercentage);
  324. // // partInfo.TypeId = particles.Key;
  325. // // partInfo.TypeColor = particles.Value[0].GetTypeColor();
  326. // // partInfo.TypeName = particles.Value[0].GetTypeName();
  327. // // double area = 0;
  328. // // foreach (var p in particles.Value)
  329. // // {
  330. // // area += p.GetActualArea();
  331. // // }
  332. // // //partInfo.Area = Convert.ToInt32(area / f.SmallParticlePercentage);
  333. // // partInfo.Area =(int) area ;
  334. // // smallparts.Add(partInfo);
  335. // //}
  336. // //List<KeyValuePair<string, SQLiteParameter[]>> cmds = new List<KeyValuePair<string, SQLiteParameter[]>>();
  337. // //foreach (CSmallParticleInfo smallp in smallparts)
  338. // //{
  339. // // var cmd= smallPartDB.SaveAKindOfSmallParticle(smallp, new CPosXrayClr(), new System.Drawing.Point(0, 0));
  340. // // cmds.Add(cmd);
  341. // //}
  342. // //pDBFileMgr.ExecuteNonQueryBatch(ref cmds);
  343. // return true;
  344. //}
  345. }
  346. }