#pragma once #include "stdafx.h" #include "resource.h" #include "ReportMgr.h" #include "BSEImgFileMgr.h" #include "PosXrayFileMgr.h" #include "CGBCalculate.h" #include "GBFieldData.h" #include "OTSHelper.h" #include "OTSFileSys.h" #include "RptParamFileMgr.h" #include "GBImgPropCal.h" using namespace OTSGBCalculate; namespace OTSMODEL { using namespace std; // constructor CReportMgr::CReportMgr() { m_listPropParams.clear(); m_listSmplMgrs.clear(); m_nWorkingSampeIndex = -1; //load param file CRptParamFileMgr paramFleMgr; paramFleMgr.Load(OTSMODEL::OTS_SOFT_PACKAGE_ID::OTSIncA); m_rptparamfile = paramFleMgr.GetRptParamFile(); double dScale = m_rptparamfile->GetScale(); } // destructor CReportMgr::~CReportMgr() { } CRptParamFilePtr CReportMgr::GetRptParamFilePtr() { return m_rptparamfile; } // class methods // public // property parameters CPropParamPtr CReportMgr::GetPropertyParamImage() { return m_listPropParams[(int)DISPLAY_PICTURE_TYPE::IMAGE]; } CPropParamPtr CReportMgr::GetPropertyParamGrid() { return m_listPropParams[(int)DISPLAY_PICTURE_TYPE::TABLE]; } CPropParamPtr CReportMgr::GetPropertyParamChart() { return m_listPropParams[(int)DISPLAY_PICTURE_TYPE::CHART]; } // reset property parameters BOOL CReportMgr::ResetPropertyParams(BOOL a_bClear /*= FALSE*/) { // clear property parameters list if (a_bClear) { m_listPropParams.clear(); } // get data source names list std::vector listDataSourceNames = GetDataSourceNamesList(); // data source names list can't be empty if (listDataSourceNames.empty()) { LogErrorTrace(__FILE__, __LINE__, _T("ResetPropertyParams: empty data source name list.")); return FALSE; } // add property parameters into the list if (m_listPropParams.empty()) { //Image display picture's property CPropParamPtr pPropParam = CPropParamPtr(new CPropParamImage()); pPropParam->SetDataSourceList(listDataSourceNames); pPropParam->SetDataSourceId(m_nWorkingSampeIndex); pPropParam->SetType(DISPLAY_PICTURE_TYPE::IMAGE); m_listPropParams.push_back(pPropParam); //grid table display picture's property pPropParam = CPropParamPtr(new CPropParamGrid()); pPropParam->SetDataSourceList(listDataSourceNames); pPropParam->SetDataSourceId(m_nWorkingSampeIndex); pPropParam->SetType(DISPLAY_PICTURE_TYPE::TABLE); m_listPropParams.push_back(pPropParam); //chart display picture's property object pPropParam = CPropParamPtr(new CPropParamChart()); pPropParam->SetDataSourceList(listDataSourceNames); pPropParam->SetDataSourceId(m_nWorkingSampeIndex); pPropParam->SetType(DISPLAY_PICTURE_TYPE::CHART); m_listPropParams.push_back(pPropParam); } else { for (auto pPropParam : m_listPropParams) { pPropParam->SetDataSourceList(listDataSourceNames); pPropParam->SetDataSourceId(m_nWorkingSampeIndex); } } // ok, return TRUE return TRUE; } // sample measure result files BOOL CReportMgr::AddASmplMsrResultMgr(CString a_strPathName /*= _T("")*/) { /*int tmpFlag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); tmpFlag |= _CRTDBG_LEAK_CHECK_DF; _CrtSetDbgFlag(tmpFlag);*/ //_CrtSetBreakAlloc(351380); CSmplMsrResultFileMgrPtr pSmplMsrResultFileMgrPtr = CSmplMsrResultFileMgrPtr(new CSmplMsrResultFileMgr(a_strPathName)); // load sample result file if (!pSmplMsrResultFileMgrPtr->Load(a_strPathName)) { return FALSE; } //判断新打开的测量结果文件名,是否已经在打开的测量结果列表名中,是的话,不打开,防止同名 CString strSampleNameNew = pSmplMsrResultFileMgrPtr->GetSmplMsrResultFile()->GetSample()->GetName(); for (int i = 0; i < m_listSmplMgrs.size(); i++) { if (m_listSmplMgrs[i]->GetSmplMsrResultFile()->GetSample()->GetName() == strSampleNameNew) { LogErrorTrace(__FILE__, __LINE__, _T("AddASmplMsrResultMgr: The measurement result name is already included.")); return FALSE; } } // add the file path name into file path names list // add the sample result file into sample measure result files list m_listSmplMgrs.push_back(pSmplMsrResultFileMgrPtr); // set working sample m_nWorkingSampeIndex = (int)m_listSmplMgrs.size() - 1; // reset property parameters ResetPropertyParams(); // ok, return TRUE return TRUE; } void CReportMgr::SetSmplMsrResultMgrs(CSmplMsrResultFileMgrList a_listSmplMsrResultFileMgr) { for (auto pSmplMsrResultMgr : a_listSmplMsrResultFileMgr) { CString strWorkingFolder = pSmplMsrResultMgr->GetWorkingFolderStr(); CSmplMsrResultFileMgrPtr pSmplMsrResultMgrNew = CSmplMsrResultFileMgrPtr(new CSmplMsrResultFileMgr(strWorkingFolder)); CString strPathname = pSmplMsrResultMgr->GetPathName(); pSmplMsrResultMgrNew->SetPathName(strPathname); CSmplMsrResultFilePtr pSmplMsrResultFile = pSmplMsrResultMgr->GetSmplMsrResultFile(); CSmplMsrResultFilePtr pSmplMsrResultFileNew = CSmplMsrResultFilePtr(new CSmplMsrResultFile(pSmplMsrResultFile.get())); pSmplMsrResultMgrNew->SetSmplMsrResultFile(pSmplMsrResultFileNew); m_listSmplMgrs.push_back(pSmplMsrResultMgrNew); } } CSmplMsrResultFileMgrPtr CReportMgr::GetASmplMsrResultMgrById(int a_nIndex) { if (a_nIndex < 0 || a_nIndex >= (int)m_listSmplMgrs.size()) { return nullptr; } return m_listSmplMgrs[a_nIndex]; } CSmplMsrResultFileMgrPtr CReportMgr::GetASmplMsrResultMgrByPathName(CString a_strPathName) { a_strPathName.Trim(); if (a_strPathName.IsEmpty()) { return nullptr; } auto itr = std::find_if(m_listSmplMgrs.begin(), m_listSmplMgrs.end(), [a_strPathName](CSmplMsrResultFileMgrPtr p) { return p->GetPathName().CompareNoCase(a_strPathName) == 0; }); if (itr == m_listSmplMgrs.end()) { return nullptr; } return *itr; } CSmplMsrResultFileMgrPtr CReportMgr::GetASmplMsrResultMgrByFileName(CString a_strFileName) { a_strFileName.Trim(); if (a_strFileName.IsEmpty()) { return nullptr; } auto itr = std::find_if(m_listSmplMgrs.begin(), m_listSmplMgrs.end(), [a_strFileName](CSmplMsrResultFileMgrPtr p) { CString strDataSource = COTSHelper::GetFileName(p->GetPathName()); return strDataSource.CompareNoCase(a_strFileName) == 0; }); if (itr == m_listSmplMgrs.end()) { return nullptr; } return *itr; } BOOL CReportMgr::DeleteASmplMsrResultMgrById(int a_nIndex) { // check the index if (a_nIndex < 0 || a_nIndex >= (int)m_listSmplMgrs.size()) { // invalid input sample index LogErrorTrace(__FILE__, __LINE__, _T("DeleteSampleByIndex::input sample index")); return a_nIndex; } // calculate new working sample index int nNewWorkingSampeIndex; if (a_nIndex < m_nWorkingSampeIndex) { nNewWorkingSampeIndex = m_nWorkingSampeIndex - 1; } else if (a_nIndex > m_nWorkingSampeIndex) { nNewWorkingSampeIndex = m_nWorkingSampeIndex; } else { // deleting working sample. if (a_nIndex == (int)m_listSmplMgrs.size() - 1) { // this is the last sample, select the above sample as working sample // if this is only sample in the list working sample index will be -1; nNewWorkingSampeIndex = m_nWorkingSampeIndex - 1; } else { // select next sample as working sample nNewWorkingSampeIndex = m_nWorkingSampeIndex; } } // delete the sample m_listSmplMgrs.erase(m_listSmplMgrs.begin() + a_nIndex); // reset working sample index m_nWorkingSampeIndex = nNewWorkingSampeIndex; // reset property parameters ResetPropertyParams(); // ok, return TRUE return TRUE; } BOOL CReportMgr::DeleteASmplMsrResultMgrByPathName(CString a_strPathName) { // check report project file pointer a_strPathName.Trim(); if (a_strPathName.IsEmpty()) { return FALSE; } auto itrObj = std::find_if(m_listSmplMgrs.begin(), m_listSmplMgrs.end(), [a_strPathName](CSmplMsrResultFileMgrPtr p) { return p->GetPathName().CompareNoCase(a_strPathName) == 0; }); if (itrObj == m_listSmplMgrs.end()) { // failed to find sample measure result file LogErrorTrace(__FILE__, __LINE__, _T("DeleteASmplMsrResultMgrByPathName: failed to find sample measure result file.")); return FALSE; } m_listSmplMgrs.erase(itrObj); // reset property parameters ResetPropertyParams(); // ok, return TRUE return TRUE; } BOOL CReportMgr::EditASmplMsrResultMgrById(int a_nIndex, CSmplMsrResultFileMgrPtr a_pSmpFileMgr) { ASSERT(a_pSmpFileMgr); if (!a_pSmpFileMgr) { LogErrorTrace(__FILE__, __LINE__, _T("EditASmplMsrResultMgrById:invalid sample measure result manager pointer.")); return FALSE; } // check if the working sample index if (a_nIndex < 0 || a_nIndex >= (int)m_listSmplMgrs.size()) { LogErrorTrace(__FILE__, __LINE__, _T("EditASmplMsrResultMgrById: invalid sample measure result manager index.")); // invalid working sample index return FALSE; } m_listSmplMgrs.erase(m_listSmplMgrs.begin() + a_nIndex); m_listSmplMgrs.insert(m_listSmplMgrs.begin() + a_nIndex, a_pSmpFileMgr); return TRUE; } CSmplMsrResultFileMgrPtr CReportMgr::GetWorkingSmplMsrReslMgr() { // check if the working sample index if (m_nWorkingSampeIndex < 0 || m_nWorkingSampeIndex >= (int)m_listSmplMgrs.size()) { LogErrorTrace(__FILE__, __LINE__, _T("GetWorkingSmplMsrReslMgr: invalid working sample measure result manager index.")); // invalid working sample index return FALSE; } return GetASmplMsrResultMgrById(m_nWorkingSampeIndex); } CString CReportMgr::GetWorkingSampleName() { CString strRet = _T(""); CSmplMsrResultFileMgrPtr pSmplMsrResultFileMgr = GetWorkingSmplMsrReslMgr(); ASSERT(pSmplMsrResultFileMgr); if (!pSmplMsrResultFileMgr) { LogErrorTrace(__FILE__, __LINE__, _T("GetWorkingSampleName: invalid working sample measure result manager.")); return _T(""); } CSmplMsrResultFilePtr pSmplMsrResultFile = pSmplMsrResultFileMgr->GetSmplMsrResultFile(); ASSERT(pSmplMsrResultFile); if (!pSmplMsrResultFile) { LogErrorTrace(__FILE__, __LINE__, _T("GetWorkingSampleName: invalid working sample measure result file.")); return _T(""); } COTSSamplePtr pSample = pSmplMsrResultFile->GetSample(); ASSERT(pSample); if (!pSample) { LogErrorTrace(__FILE__, __LINE__, _T("GetWorkingSampleName: invalid working sample.")); return _T(""); } strRet = pSample->GetName(); return strRet; } BOOL CReportMgr::SetWorkingSmplMsrReslMgr(CSmplMsrResultFileMgrPtr a_pSmplMsrResultMgr) { ASSERT(a_pSmplMsrResultMgr); if (!a_pSmplMsrResultMgr) { LogErrorTrace(__FILE__, __LINE__, _T("SetWorkingSmplMsrReslMgr: invalid sample measure result manager pointer.")); return FALSE; } if (!EditASmplMsrResultMgrById(m_nWorkingSampeIndex, a_pSmplMsrResultMgr)) { LogErrorTrace(__FILE__, __LINE__, _T("SetWorkingSmplMsrReslMgr: can't edit working sample measure result file.")); return FALSE; } return TRUE; } // grid computing CGridDatasList CReportMgr::GridDataTransfer(CPropParamPtr thePropParam) { CGridDatasList listGridData; listGridData.clear(); CGBCalculate GBCal(this); CALCULATE_TABLE_TYPE nCalTableType = thePropParam->GetCalTableType(); switch (nCalTableType) { case CALCULATE_TABLE_TYPE::GB_Method1: listGridData = GBCal.GetGBInclusion(); break; case CALCULATE_TABLE_TYPE::GB_Method2: listGridData = GBCal.GetGBInclusion(); break; case CALCULATE_TABLE_TYPE::ASTM: listGridData = GBCal.GetGBInclusion(); break; case CALCULATE_TABLE_TYPE::DIN: listGridData = GBCal.GetGBInclusion(); break; default: break; } return listGridData; } // protected // get data source name list std::vector CReportMgr::GetDataSourceNamesList() { std::vector listDataSourceNames; CString strComSourceName = _T(""); int nComNum = 0; for (auto pSmplMsrResultFileMgr : m_listSmplMgrs) { // sample measure result file CString strFilePathName = pSmplMsrResultFileMgr->GetPathName(); // get data source name CString strDataSource = COTSHelper::GetFileName(strFilePathName); // get compound source name CSmplMsrResultFilePtr pSmplMsrResultFile = pSmplMsrResultFileMgr->GetSmplMsrResultFile(); ASSERT(pSmplMsrResultFile); if (!pSmplMsrResultFile) { LogErrorTrace(__FILE__, __LINE__, _T("GetDataSourceNamesList: Can't get sample result file pointer.")); break; } BOOL bSwitch = pSmplMsrResultFile->GetSwitch(); if (bSwitch) { strComSourceName += strDataSource + _T("+"); nComNum++; } // add data source name into the list listDataSourceNames.push_back(strDataSource); } if (nComNum > 1) { //listDataSourceNames.push_back(strComSourceName); strComSourceName.TrimRight(_T("+"));//去掉最后的+号 listDataSourceNames.insert(listDataSourceNames.begin(), strComSourceName); } return listDataSourceNames; } std::vector CReportMgr::GetDataSourcePosList() { std::vector listDataSourcePos; if (!IsHaveMultiDataSource()) { return listDataSourcePos; } int nPos = -1; for (auto pSmplMsrResultFileMgr : m_listSmplMgrs) { nPos++; // get compound source name CSmplMsrResultFilePtr pSmplMsrResultFile = pSmplMsrResultFileMgr->GetSmplMsrResultFile(); ASSERT(pSmplMsrResultFile); if (!pSmplMsrResultFile) { LogErrorTrace(__FILE__, __LINE__, _T("GetDataSourceNamesList: Can't get sample result file pointer.")); break; } BOOL bSwitch = pSmplMsrResultFile->GetSwitch(); if (bSwitch) { listDataSourcePos.push_back(nPos); } } return listDataSourcePos; } // get property parameter COTSParticleList CReportMgr::GetAnalysisParticleList(CString a_DataSourceName) { COTSParticleList listParticleAll; // compound sample measure result files CSmplMsrResultFileMgrPtr pSmplMsrResultFileMgr = this->GetASmplMsrResultMgrByFileName(a_DataSourceName); if (!pSmplMsrResultFileMgr->ComputeParticleList()) { LogErrorTrace(__FILE__, __LINE__, _T("GetAnalysisParticleList: can't get particle list.")); return listParticleAll; } listParticleAll = pSmplMsrResultFileMgr->GetParticleList(); return listParticleAll; } //CPosXraysList CReportMgr::GetAnalysisXrayList(CString a_DataSourceName) //{ // CPosXraysList listAnalysisXray; // CSmplMsrResultFileMgrPtr pSmplMsrResultFileMgr = this->GetASmplMsrResultMgrByFileName(a_DataSourceName); // pSmplMsrResultFileMgr->GetOTSFldMgrListAndAnalysisXrayList(); // // should first read x ray data from data base. // listAnalysisXray = pSmplMsrResultFileMgr->GetAnalysisXray(); // return listAnalysisXray; //} //判断该列是否要显示 BOOL CReportMgr::EstimateShowColumn(CString a_strColName) { CString str_Area_Enum = "Area"; CString str_MaxDiameter_Enum = "MaxDiameter"; CString str_MinDiameter_Enum = "MinDiameter"; CString str_DiameterRatio_Enum = "DiameterRatio"; CString str_EquivalentCircleDiameter_Enum = "EquivalentCircleDiameter"; CString str_FerretDiameter_Enum = "FerretDiameter"; CString str_Area_CN = "颗粒面积"; CString str_MaxDiameter_CN = "最长直径"; CString str_MinDiameter_CN = "最短直径"; CString str_DiameterRatio_CN = "长短直径比"; CString str_EquivalentCircleDiameter_CN = "等效圆直径"; CString str_FerretDiameter_CN = "费雷特直径"; //最后再根据设置默认显示列名,对不需要显示的列进行屏蔽 CString s_DefaultComputedColName = m_rptparamfile->GetDefaultComputedColName(); if (a_strColName.Find(str_Area_CN, 0) > -1 || a_strColName.Find(str_MaxDiameter_CN, 0) > -1 || a_strColName.Find(str_MinDiameter_CN, 0) > -1 || a_strColName.Find(str_DiameterRatio_CN, 0) > -1 || a_strColName.Find(str_EquivalentCircleDiameter_CN, 0) > -1 || a_strColName.Find(str_FerretDiameter_CN, 0) > -1) { //面积 if (s_DefaultComputedColName.Find(str_Area_Enum) > -1 && a_strColName.Find(str_Area_CN, 0) > -1) { return true; } //最长直径 if (s_DefaultComputedColName.Find(str_MaxDiameter_Enum) > -1 && a_strColName.Find(str_MaxDiameter_CN, 0) > -1) { return true; } //最短直径 if (s_DefaultComputedColName.Find(str_MinDiameter_Enum) > -1 && a_strColName.Find(str_MinDiameter_CN, 0) > -1) { return true; } //长短直径比 if (s_DefaultComputedColName.Find(str_DiameterRatio_Enum) > -1 && a_strColName.Find(str_DiameterRatio_CN, 0) > -1) { return true; } //等效圆直径 if (s_DefaultComputedColName.Find(str_EquivalentCircleDiameter_Enum) > -1 && a_strColName.Find(str_EquivalentCircleDiameter_CN, 0) > -1) { return true; } //费雷特直径 if (s_DefaultComputedColName.Find(str_FerretDiameter_Enum) > -1 && a_strColName.Find(str_FerretDiameter_CN, 0) > -1) { return true; } return false; } return true; } BOOL CReportMgr::IsHaveMultiDataSource() { int nMultiCount = 0; for (auto SmplMgr : m_listSmplMgrs) { CSmplMsrResultFilePtr pSmpl = SmplMgr->GetSmplMsrResultFile(); if (pSmpl->GetSwitch()) { nMultiCount++; } } if (nMultiCount > 1) { return TRUE; } return FALSE; } // get undefined element list from particle list CElementChemistriesList CReportMgr::GetUndefinedElementList(COTSParticleList a_listParticle, CPosXraysList a_listXray) { CElementChemistriesList listElementChemistry; // safety check to get xray data and element number int nXraySize = (int)a_listXray.size(); int nParticleSize = (int)a_listParticle.size(); if (nParticleSize > nXraySize) { LogErrorTrace(__FILE__, __LINE__, _T("GetUndefinedElementList: get xray number is not enough.")); return listElementChemistry; } CElementAreaList listElementArea; // get element area double dTotalArea = 0; for (auto pParticle : a_listParticle) { int nXrayId = pParticle->GetAnalysisId(); if (nXrayId < 0 || nXrayId >(int) a_listXray.size()) { LogErrorTrace(__FILE__, __LINE__, _T("GetUndefinedElementList: get wrong xray id.")); return listElementChemistry; } int nFieldId = pParticle->GetFieldId(); auto itr = std::find_if(a_listXray.begin(), a_listXray.end(), [nXrayId, nFieldId](CPosXrayPtr p) { return ((p->GetScanFieldId() == nFieldId) && (p->GetIndex() == nXrayId)); }); if (itr == a_listXray.end()) { LogErrorTrace(__FILE__, __LINE__, _T("GetUndefinedElementList: can't find x-ray.")); return listElementChemistry; } CPosXrayPtr pXray = *itr; // remove Fe CElementChemistriesList listElementChemistryCurrent = pXray->GetElementQuantifyData(); CElementChemistriesList listElementChemistryNew1 = CPosXray::RemoveFe(listElementChemistryCurrent); CElementChemistriesList listElementChemistryNew = CPosXray::RemoveC(listElementChemistryNew1); double t = 0; CElementChemistriesList listElementChemistryG1; for (auto pElementChemistry : listElementChemistryNew) { t += pElementChemistry->GetPercentage(); } if (t == 0) { continue; } for (auto pElementChemistry : listElementChemistryNew) { double dPecent = 100.0 *pElementChemistry->GetPercentage() / t; CElementChemistryPtr pElementNew = CElementChemistryPtr(new CElementChemistry(*pElementChemistry.get())); pElementNew->SetPercentage(dPecent); listElementChemistryG1.push_back(pElementNew); } CElementAreaPtr pElementArea = CElementAreaPtr(new CElementArea()); double dArea = pParticle->GetActualArea(); pElementArea->SetArea(dArea); pElementArea->SetElementList(listElementChemistryG1); listElementArea.push_back(pElementArea); dTotalArea += dArea; } for (auto pElementArea : listElementArea) { CElementChemistriesList listElementChemistryCurrent = pElementArea->GetElementList(); for (auto pElement : listElementChemistryCurrent) { CString strName = pElement->GetName(); double dPercentNorm = 1.0; auto itr = std::find_if(listElementChemistry.begin(), listElementChemistry.end(), [strName](CElementChemistryPtr p) { return p->GetName().CompareNoCase(strName) == 0; }); if (itr == listElementChemistry.end()) { double dArea = pElementArea->GetArea(); double dPercent = pElement->GetPercentage(); dPercentNorm = dArea * dPercent / dTotalArea; } else { CElementChemistryPtr pElementChemistry = *itr; double dAreaOld = pElementChemistry->GetPercentage() * dTotalArea * 0.01; double dArea = pElementArea->GetArea(); double dPercent = pElement->GetPercentage(); dPercentNorm = 100 * (0.01 * dArea * dPercent + dAreaOld) / dTotalArea; listElementChemistry.erase(itr); } CElementChemistryPtr pElement = CElementChemistryPtr(new CElementChemistry()); pElement->SetName(strName); pElement->SetPercentage(dPercentNorm); listElementChemistry.push_back(pElement); } } return listElementChemistry; } }