SmplMeasureCleanliness.cs 18 KB

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