using OTSCOMMONCLR; using OTSDataType; using OTSModelSharp.DTLBase; using OTSModelSharp.ServiceInterface; using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Xml; namespace OTSModelSharp { class CSmplMsrResult { //----------------定义---------------------- protected static NLog.Logger logger = null; // working folder string string m_strWorkingFolder; string m_strSampleWorkingFolder; string m_strFieldFileSubFolder; private string m_strBSEImageFileFolder; private string m_strDBFileFolder; // pathname string m_strRstFileName; string m_strdbPathName; CIncAFileMgr m_DBFileMgr ; public string SMPL_MSR_RESULT_FILE_EXT = ".rst"; // fields file sub-directory string public string SMPL_MSR_RESULT_FIELDS_FILE_SUBFOLDER = "FIELD_FILES"; public string DBFILE_NAME = "Inclusion.db"; public string SMPL_MSR_RESULT_FILE_FILTER = ("Sample Measure Result Files (*.rst)|*.rst||"); // BSE file name public string SMPL_MSR_RESULT_FIELDS_BSE = ("Field"); // search x-ray file name public string SMPL_MSR_RESULT_SEARCH_X_RAY_FILE = ("SearchXray.db"); //----------全局定义------------------ // sample measure result file mark public int SMPL_MSR_RESULT_FILE_MARK = 'S' + 'M' + 'P' + 'L' + 'M' + 'S' + 'R' + 'R' + 'E' + 'S' + 'U' + 'L' + 'T'; // sample measure result file version public string SMPL_MSR_RESULT_FILE_VERSION = ("2.0.0"); // sample measure result file extension //----------定义---------------------- // file version string string m_strFileVersion; // SEM sample stage CSEMStageData m_pSEMStageData; // sample stage CStage m_pStage; //CSEMStageDataPtr m_pSEMData; CSEMDataGnr m_pSEMData; // sample setting COTSSample m_pSample; // switch bool m_bSwitch; List allfieldcenters; public CIncAFileMgr DBFileMgr { get => m_DBFileMgr; set => m_DBFileMgr = value; } //----------------public----------------- public CSmplMsrResult() { Init(); m_pSample = new COTSSample(); allfieldcenters = new List(); } protected void Duplicate(CSmplMsrResult a_oSource) { // initialization Init(); m_pSample = new COTSSample(); // copy data over // file version string m_strFileVersion = a_oSource.m_strFileVersion; // SEM sample stage m_pSEMStageData = a_oSource.m_pSEMStageData; // sample stage m_pStage = a_oSource.m_pStage; // SEM condition?? //CSEMStageDataPtr m_pSEMData; m_pSEMData = a_oSource.m_pSEMData; // sample setting m_pSample = a_oSource.m_pSample; // switch m_bSwitch = a_oSource.m_bSwitch; // fields m_pSample.GetFieldsData().Clear(); foreach (COTSFieldData pFieldData in a_oSource.m_pSample.GetFieldsData()) { COTSFieldData pFieldDataNew = pFieldData; m_pSample.GetFieldsData().Add(pFieldDataNew); } } public List GetFieldData() { return m_pSample.GetFieldsData(); //m_pSample.GetFieldsData() } public CSmplMsrResult(string measureWorkingFolder,COTSSample a_pSample) { Init(); logger = NLog.LogManager.GetCurrentClassLogger(); m_pSample = a_pSample; m_strWorkingFolder = measureWorkingFolder; m_strSampleWorkingFolder = measureWorkingFolder + "\\" + a_pSample.GetName(); m_strFieldFileSubFolder = m_strSampleWorkingFolder + "\\" + SMPL_MSR_RESULT_FIELDS_FILE_SUBFOLDER; m_strBSEImageFileFolder = m_strFieldFileSubFolder; m_strDBFileFolder = m_strFieldFileSubFolder; m_strRstFileName = m_strSampleWorkingFolder + "\\" + a_pSample.GetName() + SMPL_MSR_RESULT_FILE_EXT; m_strdbPathName = m_strFieldFileSubFolder + "\\" + DBFILE_NAME; m_DBFileMgr = new CIncAFileMgr(m_strdbPathName); } protected void Init() { // file version string m_strFileVersion = (""); // SEM sample stage m_pSEMStageData = new CSEMStageData(); // sample stage m_pStage = new CStage(); // SEM condition?? //CSEMStageDataPtr m_pSEMData; m_pSEMData = new CSEMDataGnr(); // sample setting // switch m_bSwitch = false; // fields } protected void Cleanup() { m_pSample.GetFieldsData().Clear(); } public string GetFileVersion() { return m_strFileVersion; } public void SetFileVersion(string a_strFileVersion) { m_strFileVersion = a_strFileVersion; } public COTSSample GetSample() { return m_pSample; } public void SetSample(COTSSample a_pSample) { if (a_pSample != null) { return; } m_pSample = a_pSample; } public CSEMStageData GetSEMStageData() { return m_pSEMStageData; } public void SetSEMStageData(CSEMStageData a_pSEMStageData) { m_pSEMStageData = a_pSEMStageData; } // sample stage public CStage GetStage() { return m_pStage; } public void SetStage(CStage a_pStage) { if (a_pStage != null) { return; } m_pStage = a_pStage; } // SEM condition public CSEMDataGnr GetSEMStage() { return m_pSEMData; } public void SetSEMStage(CSEMDataGnr a_pSEMData) { if (a_pSEMData != null) { return; } m_pSEMData = a_pSEMData; } // sample setting // switch public bool GetSwitch() { return m_bSwitch; } public void SetSwitch(bool a_bSwitch) { m_bSwitch = a_bSwitch; } //fields //----------protected---------------- // cleanup // initialization public void Serialize(bool isStoring, XmlDocument classDoc, XmlNode rootNode) { xInt xnFileMark = new xInt(); xString xnVersion = new xString(); xBool xbSwitch = new xBool(); Collection xfields = new Collection(); Slo slo = new Slo(); slo.Register("FileMark", xnFileMark); slo.Register("Version", xnVersion); slo.Register("Switch", xbSwitch); slo.Register("SEMStageData", m_pSEMStageData); slo.Register("Stage", m_pStage); slo.Register("SEMData", m_pSEMData); slo.Register("Sample", m_pSample); slo.Register("Fields", xfields); if (isStoring) { xnFileMark.AssignValue(SMPL_MSR_RESULT_FILE_MARK); xnVersion.AssignValue(SMPL_MSR_RESULT_FILE_VERSION); xbSwitch.AssignValue(m_bSwitch); for(int i=0;i.., like 1.2.3 public uint GetVersionFromString(string a_sVersion) { int nMajorVersion = 0; int nMinorVersion = 0; int nBuildVersion = 0; uint nVersion = 0; if (GetMajorVersionFromString(a_sVersion, nMajorVersion) && GetMinorVersionFromString(a_sVersion, nMinorVersion) && GetBuildVersionFromString(a_sVersion, nBuildVersion)) { nVersion = (uint)nMajorVersion * 10000 + (uint)nMinorVersion * 100 + (uint)nBuildVersion; } return nVersion; } public bool GetMinorVersionFromString(string a_sVersion, int a_nVersion) { // version string String strVersion = a_sVersion; // get major file version string position first int nPosFirst = strVersion.IndexOf('.'); if (nPosFirst < 0) { // failed to find major file version string return false; } // get minor file version string position int nPosSecond = strVersion.IndexOf('.', nPosFirst + 1); int nStrLength = nPosSecond - nPosFirst - 1; if (nStrLength <= 0) { // failed to find minor file version string return false; } // get minor file version string String strMinorVersion = strVersion.Remove(nPosFirst + 1, nStrLength); // the minor file version string can't be empty strMinorVersion.Trim(); if (strMinorVersion == "") { // failed to find minor file version string return false; } // convert the minor version string to minor version number if (!StringToInt(strMinorVersion, a_nVersion)) { // failed to convert the major file version string to number return false; } // ok, return TRUE return true; } public bool GetMajorVersionFromString(string a_sVersion, int a_nVersion) { // version string String strVersion = a_sVersion; // get major file version string position int nPosFirst = strVersion.IndexOf('.'); if (nPosFirst <= 0) { // failed to find major file version return false; } // get major file version string String strMajorVersion = strVersion.PadLeft(nPosFirst); // the major file version string can't be empty strMajorVersion.Trim(); if (strMajorVersion == "") { // failed to find major file version return false; } // convert the major version string to major version number if (!StringToInt(strMajorVersion, a_nVersion)) { // failed to convert the major file version string to number return false; } // ok, return TRUE return true; } public bool GetBuildVersionFromString(string a_sVersion, int a_nBuild) { // version string String strVersion = a_sVersion; // get major file version string position first int nPosFirst = strVersion.IndexOf('.'); if (nPosFirst < 0) { // failed to find major file version string return false; } // get minor file version string position int nPosSecond = strVersion.IndexOf('.', nPosFirst + 1); if (nPosSecond < 0) { // failed to find minor file version string return false; } // get build string position int nStrLength = strVersion.Length - nPosSecond - 1; if (nStrLength <= 0) { // failed to find build string return false; } // get build string String strBuild = strVersion.PadRight(nStrLength); // build string can't be empty strBuild.Trim(); if (strBuild == "") { // failed to convert the build string to number return false; } // convert the build string to build number if (!StringToInt(strBuild, a_nBuild)) { // failed to convert the build string to number return false; } // ok, return TRUE return true; } // Strings to int. public bool StringToInt(string a_sValue, int a_nValue) { if (!IsDigitString(a_sValue)) { return false; } a_nValue = Convert.ToInt32(a_sValue); return true; } // Determines whether is digit string. public bool IsDigitString(string a_sValue) { String strInt = a_sValue; strInt.Trim(); if (strInt == "") { return false; } int nStart = 0; if (strInt[nStart] == '-') { ++nStart; } // cycle through string and check each character if it is a digit for (; nStart < strInt.Length; ++nStart) { if (strInt[nStart] != '-') { return false; } } return true; } // gets the name of the folder static String GetFolderName(String a_strPathName) { string str= FileHelper.GetFolderName(a_strPathName); return str.ToString(); } private bool LoadFieldDataFromDB(string a_strPathname) { // check the pathname string a_strPathname.Trim(); if (a_strPathname == null) { // open FolderBrowserDialog FileDialog = new FolderBrowserDialog();//创建一个文件对话框 if (FileDialog.ShowDialog() != DialogResult.OK) { // user canceled loading logger.Info("Load: user canceled loading."); return false; } a_strPathname = FileDialog.SelectedPath; } // get path of the pathname // COTSHelper cOTSHelper = new COTSHelper(); string strFilePath = GetFolderName(a_strPathname); if (strFilePath == null) { // file path string is an empty string logger.Info("Load: file path string is an empty string."); return false; } // field file sub folder string string strFieldFileSubFolder = strFilePath + "\\" + SMPL_MSR_RESULT_FIELDS_FILE_SUBFOLDER + "\\"; // check if the field file sub folder exists // COTSEDSBase cOTSFileSys = new COTSEDSBase(); if (!Exists(strFieldFileSubFolder)) {// field files folder doesn't exist logger.Info("Load: field files folder doesn't exist (%s)."); return false; } string strIncAFilename = strFieldFileSubFolder + "\\" +DBFILE_NAME; CIncAFileMgr pIncAFileMgr = new CIncAFileMgr(strIncAFilename); List allFlds; allFlds = null; CMsrSampleStatus poMsrStatus = m_pSample.GetMsrStatus(); CMsrDisplayResults poMsrResults = m_pSample.GetMsrResults(); double aFldArea = m_pSample.CalculateAFieldArea(); if (GetAllFieldsFromDB(allFlds, poMsrStatus, poMsrResults, aFldArea)) { SetFieldData(allFlds); return true; } else { return false; } } public bool GetAllFieldsFromDB(List allFlds, CMsrSampleStatus status, CMsrDisplayResults rst, double aFieldArea) { List completedfld = new List(); m_DBFileMgr.GetIncADB().GetAllFieldsRecord(ref allFlds); var SegmentDB = m_DBFileMgr.GetSegmentDB(); double msrFldsArea = 0; // std::map mapTypeParticles;//record typeId relevants particlelist; Dictionary> mapTypeParticles = new Dictionary>(); // std::map, COTSSegmentsList> AllSegments; Dictionary, List> AllSegments = new Dictionary, List>(); if (SegmentDB.GetAllSegmentsRecord(AllSegments)) { int nCol; foreach (var fld in allFlds) { int fldId = fld.GetId(); List parts = fld.ListAnalysisParticles; List cOTSSegment = new List(); foreach (var part in parts) { List fldvec = new List(); fldvec.Add(fldId); fldvec.Add(part.GetTagId()); var itr = AllSegments.Values; if (itr != AllSegments.Values) { COTSFeatureClr f = new COTSFeatureClr(); f.SetSegmentsList(cOTSSegment, true); part.SetFeature(f); } mapTypeParticles[part.GetClassifyId()].Add(part); } completedfld.Add(fld.GetPosition()); msrFldsArea += aFieldArea; } } // get MsrStatus info from DB. String strTimeStart = ""; String strTimeEnd = ""; String strRstStatus = ""; var m_GenInfoDB = m_DBFileMgr.GetGeneralInfoDB(); m_GenInfoDB.GetStringValue(m_GenInfoDB.GetTableItemNameTimeStart(), ref strTimeStart); m_GenInfoDB.GetStringValue(m_GenInfoDB.GetTableItemNameTimeEnd(),ref strTimeEnd); m_GenInfoDB.GetStringValue(m_GenInfoDB.GetTableItemNameResultStatus(),ref strRstStatus); status.SetCompletedFieldsCenter(completedfld); status.SetCompletedFields(completedfld.Count); DateTime timeStart, timeEnd; timeStart = Convert.ToDateTime(strTimeStart); timeEnd = Convert.ToDateTime(strTimeEnd); status.SetStartTime(timeStart); status.SetEndTime(timeEnd); status.SetUsedTime(timeEnd - timeStart); status.SetStatus((OTS_MSR_SAMPLE_STATUS)Convert.ToInt32(strRstStatus)); //get MsrResults data from map. List rstItms = new List(); double allPartArea = 0; foreach (var typeParticles in mapTypeParticles) { CMsrResultItem rstItm = new CMsrResultItem(); int typeNum = 0; double typeArea = 0; foreach (var p in mapTypeParticles) { typeNum += 1; typeArea += Convert.ToInt32(p); } rstItm.SetTypeId(Convert.ToInt32(typeParticles)); rstItm.SetNumber((uint)typeNum); rstItm.SetArea((uint)typeArea); rstItms.Add(rstItm); allPartArea += typeArea; } rst.SetResultItems(rstItms); rst.SetMeasuredArea((uint)msrFldsArea * 1000000); rst.SetRatio(allPartArea / (msrFldsArea * 1000000)); return true; } // Save public bool CreateResultFiles(List listallfldcenter) { InitFolders(); XmlDocument xmlDoc = new XmlDocument(); XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null); xmlDoc.AppendChild(xmlDeclaration); XmlElement rootNode = xmlDoc.CreateElement("XMLData"); xmlDoc.AppendChild(rootNode); //xmlDoc.LoadXml(a_strPathname); XmlNode root = xmlDoc.SelectSingleNode("XMLData"); allfieldcenters = listallfldcenter; Serialize(true, xmlDoc, root); xmlDoc.Save(m_strRstFileName); m_DBFileMgr.InitFile(); m_DBFileMgr.InitDataTable(); return true; } public string GetFieldFileSubFolderStr() { // return field file sub folder string return m_strFieldFileSubFolder; } // sample measure result file // save a BSE file for a field public bool SaveBSEFileForAField(COTSFieldData a_pField) { if (a_pField == null) { logger.Info("SaveBSEFileForAField: invalid field data pointer."); return false; } string strBSEFilePathname = m_strSampleWorkingFolder + "\\" +SMPL_MSR_RESULT_FIELDS_FILE_SUBFOLDER + "\\"; // save BSE file for the field CBSEImgClr pBSEImage = a_pField.GetBSEImage(); CBSEImageFileMgr pBSEImgFileMgr = new CBSEImageFileMgr(); pBSEImgFileMgr.SetBSEImg(pBSEImage); pBSEImgFileMgr.SaveIntoBitmap(strBSEFilePathname); // ok, return TRUE return true; } // save a x-ray file for a field public bool SaveXRayFileForAField(List a_pFieldXray) { string strIncAFilename = m_strDBFileFolder + "\\" + DBFILE_NAME; // save x-ray file for the field CPosXrayDBMgr pPosXrayDBMgr = new CIncAFileMgr(strIncAFilename).GetPosXrayDBMgr(); List listAnalysisXray = a_pFieldXray; if (!pPosXrayDBMgr.SaveXray(listAnalysisXray,true)) { logger.Info("SaveXRayFileForAField: save analysis x-ray failed."); return false; } // ok, return TRUE return true; } // fields public COTSFieldData GetFieldById(int a_nID) { List listFieldData = GetFieldData(); if (a_nID < 0 || a_nID > (int)listFieldData.Count) { return null; } COTSFieldData pFieldData = listFieldData[a_nID]; return pFieldData; } public void AddAField(COTSFieldData a_pFieldData) { m_pSample.AddFieldData(a_pFieldData); return ; } public void SetFieldData(List a_listFieldData) { m_pSample.SetFieldsData(a_listFieldData); } public bool DeleteAFieldById(int a_nID) { List listFieldData = GetFieldData(); if (a_nID < 0 || a_nID > (int)listFieldData.Count) { return false; } //listFieldData.erase(listFieldData.begin() + a_nID); for (int i = 0; i < a_nID; i++) { listFieldData.RemoveAt(0); } return true; } // pathname public string GetPathFileName() { return m_strRstFileName; } public string GetDBPathName() { return m_strdbPathName; } public void SetPathFileName(string PathName) { m_strRstFileName = PathName; } // set sample // set SEM general data public void SetSEMGnr(CSEMDataGnr a_pSEMGnr) { //ASSERT(a_pSEMGnr); if (a_pSEMGnr == null) { logger.Info("input a invalid SEM general data pointer."); return; } m_pSEMData= a_pSEMGnr; } // get id for a new field public int GetIdForANewField() { List listFieldData = GetFieldData(); // new field id int nNewFieldId= listFieldData.Count; bool bUnit = false; do { bool iffound = false; COTSFieldData cOTSFieldData; for (int i = 0; i < listFieldData.Count; i++) { cOTSFieldData = listFieldData[i]; if (cOTSFieldData.GetId() == nNewFieldId) { iffound = true; } } if (iffound == true) { ++nNewFieldId; } else { break; } } while (true); return nNewFieldId; } // set SEMStageData // set SEMStage public void SetSEMStage(CStage a_pStage) { //ASSERT(a_pStage); if (a_pStage == null) { logger.Info("input a invalid stage pointer."); return; } m_pStage= a_pStage; } // according to a sample result file get a list of OTSFieldMgr //public List GetOTSFldMgrListAndAnalysisXrayList() //{ // //Get FieldDataList // List listFieldData =GetFieldData(); // int nFieldNum = (int)listFieldData.Count; // // get field number // foreach (COTSFieldData pFieldData in listFieldData) // { // string strIncAFilename = m_strDBFileFolder + "\\" + DBFILE_NAME; // CIncAFileMgr DBFile = new CIncAFileMgr(strIncAFilename); // COTSFieldData pFieldMgr = new COTSFieldData(); // // get FieldData // //pFieldMgr.SetOTSFieldData(pFieldData); // List listParticle = pFieldData.ListAnalysisParticles; // if (listParticle == null) // { // logger.Info("GetOTSFieldMgrList: there is no particle in this field."); // continue; // } // // get BSE // string strFieldFileFolder = m_strFieldFileSubFolder; // int nId = pFieldData.GetId(); // string sFieldId; // sFieldId = nId.ToString(); // CBSEImageFileMgr cBSEImgFileMgr = new CBSEImageFileMgr(); // string strBSEFilePathname = strFieldFileFolder + SMPL_MSR_RESULT_FIELDS_BSE + sFieldId + cBSEImgFileMgr.BMP_IMG_FILE_EXT; // CBSEImageFileMgr pBSEImgFile = new CBSEImageFileMgr(); // if (!pBSEImgFile.LoadFromBitmap(strBSEFilePathname, true) == true) // { // logger.Error("GetOTSFieldMgrList: can't load BSE File."); // AddAField(pFieldMgr); // continue; // } // CBSEImgClr pBSEImg = pBSEImgFile.GetBSEImg(); // pFieldMgr.SetBSEImage(pBSEImg); // // get analysis X-ray list // CPosXrayDBMgr pPosAnalysisXrayFileMgr = DBFile.GetPosXrayDBMgr(); // pPosAnalysisXrayFileMgr.SetHasElement(true); // if (!pPosAnalysisXrayFileMgr.Load(pFieldData.GetId(), true)) // { // AddAField(pFieldMgr); // continue; // } // List listAnalysisXray = pPosAnalysisXrayFileMgr.GetPosXrayList(); // AddAField(pFieldMgr); // } // var flddata = GetFieldData(); // return flddata; //} //----------------protected----------------- //protected bool GetID(COTSFieldData cOTSFieldData, int a_TypeId) //{ // if (cOTSFieldData.GetId() == a_TypeId) // { // return true; // } // else // { // return false; // } //} } }