SmplMeasureCleanliness.cs 17 KB

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