123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387 |
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using OTSCLRINTERFACE;
- using OTSDataType;
- using OTSMeasureApp._0_OTSModel.Measure.ParamData;
- using OTSModelSharp.Measure.OTSInclution;
- using OTSModelSharp.ServiceCenter;
- using OTSModelSharp.ServiceInterface;
- using static OTSDataType.otsdataconst;
- namespace OTSModelSharp
- {
- class CSmplMeasureInclution : CSmplMeasure
- {
-
-
- public CSmplMeasureInclution(string a_strWorkingFolder, COTSSample a_pSample) : base(a_strWorkingFolder, a_pSample)
- {
- SetWorkingFolder(a_strWorkingFolder);
- SetSample(a_pSample);
- m_classifyEngine = new CClassifyEngine();
- }
- // field image process
- public override bool FieldImageProcess(Point fldCenter, CBSEImgClr a_pBSEImg)
- {
- int nNewFieldId;
- nNewFieldId = m_pSampleRstFile.GetIdForANewField();
- // create a field
- curFldData = new CFieldDataIncA(a_pBSEImg, m_Sample.CalculatePixelSize());
- curFldData.SetId(nNewFieldId);
- curFldData.SetOTSPosition(fldCenter);
- CSEMStageData a_pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
- Point semPos = new Point();
- a_pCSEMStageData.ConverOTSToSEMPoint(fldCenter,ref semPos);
- curFldData.SemPos = semPos;
-
- //first step:remove background of the bse image and compound all the finded particles.
-
- log.Info("Begin to process image and get all particles!");
- GetOriginalParticles();
-
- // second step :filter the finded particles.
- log.Info("Begin to filter particles!");
- FilterParticles();
- COTSXRayParam pXRayParam = m_Sample.GetMsrParams().GetXRayParam();
- if (pXRayParam.GetUsingXray() == true)
- {
- Thread.Sleep(100);
- CollectParticlesXrayData();
-
- Thread.Sleep(100);
- }
- log.Info("Begin to Calculate the image property of every particle!");
- var analysisparts = curFldData.GetListAnalysisParticles();
- curFldData.CalParticleImageProp(analysisparts);//calculate particle image property such as feret diameter, DMAX etc.
- log.Info("Begin to classify particles! particle num:"+ curFldData.GetListAnalysisParticles().Count);
- ClassifyFieldParticles();
-
- // save field files
- m_Sample.GetMsrStatus().SetStatus(OTS_MSR_SAMPLE_STATUS.SUCCESSED);
- StartSaveFileThread(curFldData);
- return true;
- }
- // save field data
- protected void SaveFieldMgrData()
- {
- while (bSaveThreadWorking)
- {
- while (fieldQueue.Count() > 0)
- {
- COTSFieldData f = fieldQueue.Dequeue();
-
- //save to disk first ,then pop . if the size is 0,then we know all the saving work is done.
- SaveFieldData(f,m_pSampleRstFile.GetFieldFileSubFolderStr());
- }
- if (fieldQueue.Count() == 0)
- {
- bSaveThreadWorking = false; //must set this flag,so the main thread can know this thread has exited.
- return;
- }
- }
- return;
- }
-
- protected void StartSaveFileThread(COTSFieldData a_pFieldMgr)
- {
-
-
- fieldQueue.Enqueue(a_pFieldMgr);
- if (fieldQueue.Count() > 0) //if there's data in the queue and the previous thread has finished then start a new thread.
- {
- if (bSaveThreadWorking == false)
- {
- bSaveThreadWorking = true;
- m_thread_ptr = new System.Threading.Thread(this.SaveFieldMgrData);//m_thread_ptr = shared_ptr<thread>(new thread(&CSmplMeasureInc::SaveFieldMgrData, this));
- m_thread_ptr.IsBackground = true;
- m_thread_ptr.Start();
- }
- }
- }
- public void FilterParticles()
- {
- // remove over sized particles, too small particles and get a analysis particles list
- // get pixel size
- double dPixelSize = m_Sample.CalculatePixelSize();
-
- CSampleParam pMsrParam = m_Sample.GetMsrParams();
- COTSImageProcParam pImgProcessParam = pMsrParam.GetImageProcessParam();
- curFldData.FilterParticles(pImgProcessParam, dPixelSize );
-
-
- // make sure the particles list is not empty
- if (curFldData.NoAnalysisParticle())
- {
- log.Info("There's no particles to be analysed!");
- //return false;
- }
- return ;
- }
- //calculate the real sem position of the particle
- public void CalculateParticlePos(List<COTSParticleClr> xrayParticles)
- {
- double dPixelSize = m_Sample.CalculatePixelSize();
- CSEMStageData pCSEMStageData = m_pMsrThread.GetProjResultData().GetSEMStageData();
- foreach (var p in xrayParticles)
- {
-
- Point fldOtsPos = new Point(curFldData.OTSPos.X, curFldData.OTSPos.Y);//take the field position as the particle's position instead
- Point semPos = new Point();
- pCSEMStageData.ConverOTSToSEMPoint(fldOtsPos, ref semPos);
- p.SetAbsolutPos(semPos);
- }
- }
- public void CollectParticlesXrayData()
- {
- // get x-ray parameters
- COTSXRayParam pXRayParam = m_Sample.GetMsrParams().GetXRayParam();
-
- var listXrayAnalysisparts = curFldData.GetListXrayParticles();
- var pStatus = m_Sample.GetMsrStatus();
-
- ////=============================================
- curFldData.CreateXrayList(listXrayAnalysisparts);
- CalculateParticlePos(listXrayAnalysisparts);
- var xraymode = m_Sample.GetMsrParams().GetXRayParam().GetScanMode();
- // get x-ray list (analysis) by particle features
- uint nXRayAQTime = (uint)pXRayParam.GetMidAnalyAQTime();
- if (xraymode == OTS_X_RAY_SCAN_MODE.FeatureMode)
- {
- log.Info("Begin feature mode xray collection!AQTime:"+nXRayAQTime+" xrayNum:"+ listXrayAnalysisparts.Count);
- m_EDSHardwareMgr.GetXRayByFeatures(listXrayAnalysisparts, nXRayAQTime, true);
-
- }
- else
- {
- log.Info("Begin point mode xray collection!AQTime:" + nXRayAQTime + " xrayNum:" + listXrayAnalysisparts.Count);
- // get x-ray list (analysis) by points
- m_EDSHardwareMgr.GetXRayByPoints(listXrayAnalysisparts, nXRayAQTime, true);
-
- }
-
- return ;
- }
-
- public override void ClassifyFieldParticles()
- {
- try
- {
- var curFld = (CFieldDataIncA)curFldData;
- var anylysisparts = curFld.GetListAnalysisParticles();
- int nSize = anylysisparts.Count();
- // go through all analysis particles
- for (int i = 0; i < nSize; ++i)
- {
- string libname = m_Sample.GetMsrParams().GetSTDName();
- ClassifyIncAParticle(anylysisparts[i], libname);
- }
-
-
- }
- catch (Exception e)
- {
- log.Info(" classify failed. " + e.Message);
- }
- }
- public override void ClassifyMergedParticles(List<COTSParticleClr> mergedParts)
- {
- try
- {
-
- var quantifyparts = mergedParts;
- int nSize = quantifyparts.Count();
- // go through all analysis particles
- for (int i = 0; i < nSize; ++i)
- {
- string libname = m_Sample.GetMsrParams().GetSTDName();
- ClassifyIncAParticle(quantifyparts[i], libname);
- }
- }
- catch (Exception e)
- {
- log.Info(" classify failed. " + e.Message);
- }
- }
- public bool ClassifyIncAParticle(COTSParticleClr particle, string libname)// classify particles
- {
- int steelTech = (int)m_Sample.GetMsrParams().GetSteelTechnology();
- particle.SetType((int)OTS_PARTCLE_TYPE.NOT_IDENTIFIED);
- if (m_Sample.IfUsingSysSTD())
- {
- if (libname != "NoSTDDB")
- {
- //var m_classifyEngine = new CClassifyEngine();
- IClassifyEngine engine = m_classifyEngine.GetParticleEngine(libname);
- engine.Classify(particle);
- }
- if (particle.GetType() ==(int) OTS_PARTCLE_TYPE.NOT_IDENTIFIED)
- {
- IClassifyEngine engine;
- engine = m_classifyEngine.GetIncClassifyEngine();
- engine.ClassifyIncA(particle, steelTech);
- }
- }
- else
- {
- if (libname != "NoSTDDB")
- {
- //var m_classifyEngine = new CClassifyEngine();
- IClassifyEngine engine = m_classifyEngine.GetParticleEngine(libname);
- engine.Classify(particle);
- }
- }
-
- return true;
- }
-
- public void GetOriginalParticles()
- {
- // measure status
- CMsrSampleStatus pStatus = m_Sample.GetMsrStatus();
- // get image process parameter
- CSampleParam pMsrParam = m_Sample.GetMsrParams();
- COTSImageProcParam pImgProcessParam = pMsrParam.GetImageProcessParam();
- var specialPartsparam = pMsrParam.GetSpecialGrayRangeParam();
- if (specialPartsparam.IsToRun)
- {
- List<CSpecialGrayRange> ranges = pMsrParam.GetSpecialGrayRangeParam().GetIntRanges();
- foreach (var grayRange in ranges)
- {
- CIntRangeClr range = new CIntRangeClr(grayRange.range.GetStart(), grayRange.range.GetEnd());
- curFldData.GetPartsBySpecialGray(range,grayRange.ifCollectXray);
- }
- }
-
- // remove BES image background
- curFldData.RemoveBSEImageBG(pImgProcessParam);
-
-
- // check if this is an empty image
- if (curFldData.NoParticle())
- { // empty fields
- log.Info("ImageProcess: empty field.");
- //return false;
- }
- return ;
- }
- public bool SaveFieldData(COTSFieldData fldData,string filedFileFoler)
- {
- //loger.Warn("begin bitmap saving...");
- string strFieldFileFolder = filedFileFoler;
- CBSEImageFileMgr pBSEImgFileMgr = new CBSEImageFileMgr();
- pBSEImgFileMgr.SetBSEImg(fldData.GetBSEImage());
- int nId = fldData.GetId();
- string sFieldId;
- sFieldId = nId.ToString();
-
- string strBSEFilePathname = strFieldFileFolder + "\\" + m_pSampleRstFile. SMPL_MSR_RESULT_FIELDS_BSE + sFieldId + pBSEImgFileMgr.BMP_IMG_FILE_EXT;
- if (!pBSEImgFileMgr.SaveIntoBitmap(strBSEFilePathname))
- {
- log.Error("SaveFieldFiles: save BSE file failed.");
- return false;
- }
- // IncA Data list
- CIncAFileMgr pDBFileMgr = m_pSampleRstFile.DBFileMgr;
-
- pDBFileMgr.SaveStatusDataToDB();
- var fldDB = pDBFileMgr.GetFieldDB();
- //fldDB.SaveAField(fldData.GetId(),fldData.GetPosition());
- var fldcmds = fldDB.GetSavingAFieldcmd(fldData.GetId(), fldData.GetOTSPosition(),fldData.SemPos);
- log.Warn("Start saving particle data.");
-
- var cmds = pDBFileMgr.GetSavingIncADataToDBCmds(curFldData.GetListAnalysisParticles(), fldData.GetOTSPosition());
- //save the xray data and element data.
- log.Warn("Start saving xray data.");
- CPosXrayDBMgr PosXrayDBMgr = pDBFileMgr.GetPosXrayDBMgr();
- var listAnalysisPosXray = new List<CPosXrayClr>();
- foreach (var p in curFldData.GetListXrayParticles())
- {
- listAnalysisPosXray.Add(p.GetXray());
- }
-
- var cmds1 = PosXrayDBMgr.GetSavingXrayCmds(listAnalysisPosXray, true);
- cmds.AddRange(cmds1);
- cmds.Add(new KeyValuePair<string, System.Data.SQLite.SQLiteParameter[]>(fldcmds, null));
- log.Warn("end saving xray data.");
- log.Warn("start batch saving!");
- pDBFileMgr.ExecuteNonQueryBatch(cmds);
- return true;
- }
- }
- }
|