SmplMeasureInclution.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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 OTSCLRINTERFACE;
  9. using OTSDataType;
  10. using OTSMeasureApp._0_OTSModel.Measure.ParamData;
  11. using OTSModelSharp.Measure.OTSInclution;
  12. using OTSModelSharp.ServiceCenter;
  13. using OTSModelSharp.ServiceInterface;
  14. using static OTSDataType.otsdataconst;
  15. namespace OTSModelSharp
  16. {
  17. class CSmplMeasureInclution : CSmplMeasure
  18. {
  19. public CSmplMeasureInclution(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_pBSEImg)
  27. {
  28. int nNewFieldId;
  29. nNewFieldId = m_pSampleRstFile.GetIdForANewField();
  30. // create a field
  31. curFldData = new CFieldDataIncA(a_pBSEImg, m_Sample.CalculatePixelSize());
  32. curFldData.SetId(nNewFieldId);
  33. curFldData.SetPosition(fldCenter);
  34. //first step:remove background of the bse image and compound all the finded particles.
  35. COTSXRayParam pXRayParam = m_Sample.GetMsrParams().GetXRayParam();
  36. log.Info("Begin to process image and get all particles!");
  37. GetOriginalParticles();
  38. // second step :filter the finded particles.
  39. log.Info("Begin to filter particles!");
  40. FilterParticles();
  41. if (pXRayParam.GetUsingXray() == true)
  42. {
  43. Thread.Sleep(100);
  44. CollectParticlesXrayData();
  45. Thread.Sleep(100);
  46. }
  47. log.Info("Begin to Calculate the image property of every particle!");
  48. var analysisparts = curFldData.GetListAnalysisParticles();
  49. curFldData.CalParticleImageProp(analysisparts);//calculate particle image property such as feret diameter, DMAX etc.
  50. log.Info("Begin to classify particles! particle num:"+ curFldData.GetListAnalysisParticles().Count);
  51. ClassifyFieldParticles();
  52. // save field files
  53. m_Sample.GetMsrStatus().SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
  54. StartSaveFileThread(curFldData);
  55. return true; ;
  56. }
  57. // save field data
  58. protected void SaveFieldMgrData()
  59. {
  60. while (bSaveThreadWorking)
  61. {
  62. while (fieldQueue.Count() > 0)
  63. {
  64. COTSFieldData f = fieldQueue.Dequeue();
  65. //save to disk first ,then pop . if the size is 0,then we know all the saving work is done.
  66. SaveFieldData(f,m_pSampleRstFile.GetFieldFileSubFolderStr());
  67. }
  68. if (fieldQueue.Count() == 0)
  69. {
  70. bSaveThreadWorking = false; //must set this flag,so the main thread can know this thread has exited.
  71. return;
  72. }
  73. }
  74. return;
  75. }
  76. protected void StartSaveFileThread(COTSFieldData a_pFieldMgr)
  77. {
  78. fieldQueue.Enqueue(a_pFieldMgr);
  79. if (fieldQueue.Count() > 0) //if there's data in the queue and the previous thread has finished then start a new thread.
  80. {
  81. if (bSaveThreadWorking == false)
  82. {
  83. bSaveThreadWorking = true;
  84. m_thread_ptr = new System.Threading.Thread(this.SaveFieldMgrData);//m_thread_ptr = shared_ptr<thread>(new thread(&CSmplMeasureInc::SaveFieldMgrData, this));
  85. m_thread_ptr.Start();//m_thread_ptr->detach();
  86. }
  87. }
  88. }
  89. public void FilterParticles()
  90. {
  91. // remove over sized particles, too small particles and get a analysis particles list
  92. // get pixel size
  93. double dPixelSize = m_Sample.CalculatePixelSize();
  94. CSampleParam pMsrParam = m_Sample.GetMsrParams();
  95. COTSImageProcParam pImgProcessParam = pMsrParam.GetImageProcessParam();
  96. curFldData.FilterParticles(pImgProcessParam, dPixelSize );
  97. // make sure the particles list is not empty
  98. if (curFldData.NoAnalysisParticle())
  99. {
  100. log.Info("There's no particles to be analysed!");
  101. //return false;
  102. }
  103. return ;
  104. }
  105. //calculate the real sem position of the particle
  106. public void CalculateParticlePos(List<COTSParticleClr> xrayParticles)
  107. {
  108. double dPixelSize = m_Sample.CalculatePixelSize();
  109. CSEMStageData pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  110. foreach (var p in xrayParticles)
  111. {
  112. Point fldOtsPos = new Point(curFldData.PoiPos.X, curFldData.PoiPos.Y);//take the field position as the particle's position instead
  113. Point semPos = new Point();
  114. pCSEMStageData.ConverOTSToSEMPoint(fldOtsPos, ref semPos);
  115. p.SetAbsolutPos(semPos);
  116. }
  117. }
  118. public void CollectParticlesXrayData()
  119. {
  120. // get x-ray parameters
  121. COTSXRayParam pXRayParam = m_Sample.GetMsrParams().GetXRayParam();
  122. var listXrayAnalysisparts = curFldData.GetListXrayParticles();
  123. var pStatus = m_Sample.GetMsrStatus();
  124. ////=============================================
  125. curFldData.CreateXrayList(listXrayAnalysisparts);
  126. CalculateParticlePos(listXrayAnalysisparts);
  127. var xraymode = m_Sample.GetMsrParams().GetXRayParam().GetScanMode();
  128. // get x-ray list (analysis) by particle features
  129. uint nXRayAQTime = (uint)pXRayParam.GetMidAnalyAQTime();
  130. if (xraymode == OTS_X_RAY_SCAN_MODE.FeatureMode)
  131. {
  132. log.Info("Begin feature mode xray collection!AQTime:"+nXRayAQTime+" xrayNum:"+ listXrayAnalysisparts.Count);
  133. m_EDSHardwareMgr.GetXRayByFeatures(listXrayAnalysisparts, nXRayAQTime, true);
  134. }
  135. else
  136. {
  137. log.Info("Begin point mode xray collection!AQTime:" + nXRayAQTime + " xrayNum:" + listXrayAnalysisparts.Count);
  138. // get x-ray list (analysis) by points
  139. m_EDSHardwareMgr.GetXRayByPoints(listXrayAnalysisparts, nXRayAQTime, true);
  140. }
  141. return ;
  142. }
  143. public override void ClassifyFieldParticles()
  144. {
  145. try
  146. {
  147. var curFld = (CFieldDataIncA)curFldData;
  148. var quantifyparts = curFld.GetListAnalysisParticles();
  149. int nSize = quantifyparts.Count();
  150. // go through all analysis particles
  151. for (int i = 0; i < nSize; ++i)
  152. {
  153. string libname = m_Sample.GetMsrParams().GetSTDName();
  154. ClassifyIncAParticle(quantifyparts[i], libname);
  155. }
  156. }
  157. catch (Exception e)
  158. {
  159. log.Info(" classify failed. " + e.Message);
  160. }
  161. }
  162. public override void ClassifyMergedParticles(List<COTSParticleClr> mergedParts)
  163. {
  164. try
  165. {
  166. var quantifyparts = mergedParts;
  167. int nSize = quantifyparts.Count();
  168. // go through all analysis particles
  169. for (int i = 0; i < nSize; ++i)
  170. {
  171. string libname = m_Sample.GetMsrParams().GetSTDName();
  172. ClassifyIncAParticle(quantifyparts[i], libname);
  173. }
  174. }
  175. catch (Exception e)
  176. {
  177. log.Info(" classify failed. " + e.Message);
  178. }
  179. }
  180. public bool ClassifyIncAParticle(COTSParticleClr particle, string libname)// classify particles
  181. {
  182. int steelTech = (int)m_Sample.GetMsrParams().GetSteelTechnology();
  183. particle.SetType((int)OTS_PARTCLE_TYPE.NOT_IDENTIFIED);
  184. if (m_Sample.IfUsingSysSTD())
  185. {
  186. if (libname != "NoSTDDB")
  187. {
  188. //var m_classifyEngine = new CClassifyEngine();
  189. IClassifyEngine engine = m_classifyEngine.GetParticleEngine(libname);
  190. engine.Classify(particle);
  191. }
  192. if (particle.GetType() ==(int) OTS_PARTCLE_TYPE.NOT_IDENTIFIED)
  193. {
  194. IClassifyEngine engine;
  195. engine = m_classifyEngine.GetIncClassifyEngine();
  196. engine.ClassifyIncA(particle, steelTech);
  197. }
  198. }
  199. else
  200. {
  201. if (libname != "NoSTDDB")
  202. {
  203. //var m_classifyEngine = new CClassifyEngine();
  204. IClassifyEngine engine = m_classifyEngine.GetParticleEngine(libname);
  205. engine.Classify(particle);
  206. }
  207. }
  208. return true;
  209. }
  210. public void GetOriginalParticles()
  211. {
  212. // measure status
  213. CMsrSampleStatus pStatus = m_Sample.GetMsrStatus();
  214. // get image process parameter
  215. CSampleParam pMsrParam = m_Sample.GetMsrParams();
  216. COTSImageProcParam pImgProcessParam = pMsrParam.GetImageProcessParam();
  217. var specialPartsparam = pMsrParam.SpecialGrayRangeParam;
  218. if (specialPartsparam.IsToRun)
  219. {
  220. List<CSpecialGrayRange> ranges = pMsrParam.SpecialGrayRangeParam.GetIntRanges();
  221. foreach (var grayRange in ranges)
  222. {
  223. CIntRangeClr range = new CIntRangeClr(grayRange.range.GetStart(), grayRange.range.GetEnd());
  224. curFldData.GetPartsBySpecialGray(range,grayRange.ifCollectXray);
  225. }
  226. }
  227. // remove BES image background
  228. curFldData.RemoveBSEImageBG(pImgProcessParam);
  229. // check if this is an empty image
  230. if (curFldData.NoParticle())
  231. { // empty fields
  232. log.Info("ImageProcess: empty field.");
  233. //return false;
  234. }
  235. return ;
  236. }
  237. public bool SaveFieldData(COTSFieldData fldData,string filedFileFoler)
  238. {
  239. //loger.Warn("begin bitmap saving...");
  240. string strFieldFileFolder = filedFileFoler;
  241. CBSEImageFileMgr pBSEImgFileMgr = new CBSEImageFileMgr();
  242. pBSEImgFileMgr.SetBSEImg(fldData.GetBSEImage());
  243. int nId = fldData.GetId();
  244. string sFieldId;
  245. sFieldId = nId.ToString();
  246. string strBSEFilePathname = strFieldFileFolder + "\\" + m_pSampleRstFile. SMPL_MSR_RESULT_FIELDS_BSE + sFieldId + pBSEImgFileMgr.BMP_IMG_FILE_EXT;
  247. if (!pBSEImgFileMgr.SaveIntoBitmap(strBSEFilePathname))
  248. {
  249. log.Info("SaveFieldFiles: save BSE file failed.");
  250. return false;
  251. }
  252. // IncA Data list
  253. CIncAFileMgr pDBFileMgr = m_pSampleRstFile.DBFileMgr;
  254. pDBFileMgr.SaveStatusDataToDB();
  255. var fldDB = pDBFileMgr.GetFieldDB();
  256. fldDB.SaveAField(fldData.GetId(),fldData.GetPosition());
  257. if (!pDBFileMgr.SaveIncADataToDB(curFldData.GetListAnalysisParticles(), fldData.GetPosition()))
  258. {
  259. log.Info("SaveFieldFiles: save inclusion file failed.");
  260. return false;
  261. }
  262. //save the xray data and element data.
  263. CPosXrayDBMgr PosXrayDBMgr = pDBFileMgr.GetPosXrayDBMgr();
  264. var listAnalysisPosXray = new List<CPosXrayClr>();
  265. foreach (var p in curFldData.GetListXrayParticles())
  266. {
  267. listAnalysisPosXray.Add(p.GetXray());
  268. }
  269. if (!PosXrayDBMgr.SaveXray(listAnalysisPosXray, true))
  270. {
  271. log.Info("SaveFieldFiles: save analysis x-ray failed.");
  272. return false;
  273. }
  274. return true;
  275. }
  276. }
  277. }