SmplMeasureInclution.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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.IsBackground = true;
  86. m_thread_ptr.Start();
  87. }
  88. }
  89. }
  90. public void FilterParticles()
  91. {
  92. // remove over sized particles, too small particles and get a analysis particles list
  93. // get pixel size
  94. double dPixelSize = m_Sample.CalculatePixelSize();
  95. CSampleParam pMsrParam = m_Sample.GetMsrParams();
  96. COTSImageProcParam pImgProcessParam = pMsrParam.GetImageProcessParam();
  97. curFldData.FilterParticles(pImgProcessParam, dPixelSize );
  98. // make sure the particles list is not empty
  99. if (curFldData.NoAnalysisParticle())
  100. {
  101. log.Info("There's no particles to be analysed!");
  102. //return false;
  103. }
  104. return ;
  105. }
  106. //calculate the real sem position of the particle
  107. public void CalculateParticlePos(List<COTSParticleClr> xrayParticles)
  108. {
  109. double dPixelSize = m_Sample.CalculatePixelSize();
  110. CSEMStageData pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
  111. foreach (var p in xrayParticles)
  112. {
  113. Point fldOtsPos = new Point(curFldData.PoiPos.X, curFldData.PoiPos.Y);//take the field position as the particle's position instead
  114. Point semPos = new Point();
  115. pCSEMStageData.ConverOTSToSEMPoint(fldOtsPos, ref semPos);
  116. p.SetAbsolutPos(semPos);
  117. }
  118. }
  119. public void CollectParticlesXrayData()
  120. {
  121. // get x-ray parameters
  122. COTSXRayParam pXRayParam = m_Sample.GetMsrParams().GetXRayParam();
  123. var listXrayAnalysisparts = curFldData.GetListXrayParticles();
  124. var pStatus = m_Sample.GetMsrStatus();
  125. ////=============================================
  126. curFldData.CreateXrayList(listXrayAnalysisparts);
  127. CalculateParticlePos(listXrayAnalysisparts);
  128. var xraymode = m_Sample.GetMsrParams().GetXRayParam().GetScanMode();
  129. // get x-ray list (analysis) by particle features
  130. uint nXRayAQTime = (uint)pXRayParam.GetMidAnalyAQTime();
  131. if (xraymode == OTS_X_RAY_SCAN_MODE.FeatureMode)
  132. {
  133. log.Info("Begin feature mode xray collection!AQTime:"+nXRayAQTime+" xrayNum:"+ listXrayAnalysisparts.Count);
  134. m_EDSHardwareMgr.GetXRayByFeatures(listXrayAnalysisparts, nXRayAQTime, true);
  135. }
  136. else
  137. {
  138. log.Info("Begin point mode xray collection!AQTime:" + nXRayAQTime + " xrayNum:" + listXrayAnalysisparts.Count);
  139. // get x-ray list (analysis) by points
  140. m_EDSHardwareMgr.GetXRayByPoints(listXrayAnalysisparts, nXRayAQTime, true);
  141. }
  142. return ;
  143. }
  144. public override void ClassifyFieldParticles()
  145. {
  146. try
  147. {
  148. var curFld = (CFieldDataIncA)curFldData;
  149. var anylysisparts = curFld.GetListAnalysisParticles();
  150. int nSize = anylysisparts.Count();
  151. // go through all analysis particles
  152. for (int i = 0; i < nSize; ++i)
  153. {
  154. string libname = m_Sample.GetMsrParams().GetSTDName();
  155. ClassifyIncAParticle(anylysisparts[i], libname);
  156. }
  157. }
  158. catch (Exception e)
  159. {
  160. log.Info(" classify failed. " + e.Message);
  161. }
  162. }
  163. public override void ClassifyMergedParticles(List<COTSParticleClr> mergedParts)
  164. {
  165. try
  166. {
  167. var quantifyparts = mergedParts;
  168. int nSize = quantifyparts.Count();
  169. // go through all analysis particles
  170. for (int i = 0; i < nSize; ++i)
  171. {
  172. string libname = m_Sample.GetMsrParams().GetSTDName();
  173. ClassifyIncAParticle(quantifyparts[i], libname);
  174. }
  175. }
  176. catch (Exception e)
  177. {
  178. log.Info(" classify failed. " + e.Message);
  179. }
  180. }
  181. public bool ClassifyIncAParticle(COTSParticleClr particle, string libname)// classify particles
  182. {
  183. int steelTech = (int)m_Sample.GetMsrParams().GetSteelTechnology();
  184. particle.SetType((int)OTS_PARTCLE_TYPE.NOT_IDENTIFIED);
  185. if (m_Sample.IfUsingSysSTD())
  186. {
  187. if (libname != "NoSTDDB")
  188. {
  189. //var m_classifyEngine = new CClassifyEngine();
  190. IClassifyEngine engine = m_classifyEngine.GetParticleEngine(libname);
  191. engine.Classify(particle);
  192. }
  193. if (particle.GetType() ==(int) OTS_PARTCLE_TYPE.NOT_IDENTIFIED)
  194. {
  195. IClassifyEngine engine;
  196. engine = m_classifyEngine.GetIncClassifyEngine();
  197. engine.ClassifyIncA(particle, steelTech);
  198. }
  199. }
  200. else
  201. {
  202. if (libname != "NoSTDDB")
  203. {
  204. //var m_classifyEngine = new CClassifyEngine();
  205. IClassifyEngine engine = m_classifyEngine.GetParticleEngine(libname);
  206. engine.Classify(particle);
  207. }
  208. }
  209. return true;
  210. }
  211. public void GetOriginalParticles()
  212. {
  213. // measure status
  214. CMsrSampleStatus pStatus = m_Sample.GetMsrStatus();
  215. // get image process parameter
  216. CSampleParam pMsrParam = m_Sample.GetMsrParams();
  217. COTSImageProcParam pImgProcessParam = pMsrParam.GetImageProcessParam();
  218. var specialPartsparam = pMsrParam.GetSpecialGrayRangeParam();
  219. if (specialPartsparam.IsToRun)
  220. {
  221. List<CSpecialGrayRange> ranges = pMsrParam.GetSpecialGrayRangeParam().GetIntRanges();
  222. foreach (var grayRange in ranges)
  223. {
  224. CIntRangeClr range = new CIntRangeClr(grayRange.range.GetStart(), grayRange.range.GetEnd());
  225. curFldData.GetPartsBySpecialGray(range,grayRange.ifCollectXray);
  226. }
  227. }
  228. // remove BES image background
  229. curFldData.RemoveBSEImageBG(pImgProcessParam);
  230. // check if this is an empty image
  231. if (curFldData.NoParticle())
  232. { // empty fields
  233. log.Info("ImageProcess: empty field.");
  234. //return false;
  235. }
  236. return ;
  237. }
  238. public bool SaveFieldData(COTSFieldData fldData,string filedFileFoler)
  239. {
  240. //loger.Warn("begin bitmap saving...");
  241. string strFieldFileFolder = filedFileFoler;
  242. CBSEImageFileMgr pBSEImgFileMgr = new CBSEImageFileMgr();
  243. pBSEImgFileMgr.SetBSEImg(fldData.GetBSEImage());
  244. int nId = fldData.GetId();
  245. string sFieldId;
  246. sFieldId = nId.ToString();
  247. string strBSEFilePathname = strFieldFileFolder + "\\" + m_pSampleRstFile. SMPL_MSR_RESULT_FIELDS_BSE + sFieldId + pBSEImgFileMgr.BMP_IMG_FILE_EXT;
  248. if (!pBSEImgFileMgr.SaveIntoBitmap(strBSEFilePathname))
  249. {
  250. log.Error("SaveFieldFiles: save BSE file failed.");
  251. return false;
  252. }
  253. // IncA Data list
  254. CIncAFileMgr pDBFileMgr = m_pSampleRstFile.DBFileMgr;
  255. pDBFileMgr.SaveStatusDataToDB();
  256. var fldDB = pDBFileMgr.GetFieldDB();
  257. //fldDB.SaveAField(fldData.GetId(),fldData.GetPosition());
  258. var fldcmds = fldDB.GetSavingAFieldcmd(fldData.GetId(), fldData.GetPosition());
  259. log.Warn("Start saving particle data.");
  260. var cmds = pDBFileMgr.GetSavingIncADataToDBCmds(curFldData.GetListAnalysisParticles(), fldData.GetPosition());
  261. //save the xray data and element data.
  262. log.Warn("Start saving xray 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. var cmds1 = PosXrayDBMgr.GetSavingXrayCmds(listAnalysisPosXray, true);
  270. cmds.AddRange(cmds1);
  271. cmds.Add(new KeyValuePair<string, System.Data.SQLite.SQLiteParameter[]>(fldcmds, null));
  272. log.Warn("end saving xray data.");
  273. log.Warn("start batch saving!");
  274. pDBFileMgr.ExecuteNonQueryBatch(cmds);
  275. return true;
  276. }
  277. }
  278. }