123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455 |
- #pragma once
- #include "stdafx.h"
- #include "IncAFileMgr.h"
- #include "DBConst.h"
- #include "OTSHelper.h"
- namespace OTSMODEL {
- using namespace OTSTools;
- // project file extension
- const CString INCA_FILE_EXT = _T("db");
- // project file filter
- const CString INCA_FILE_FILTER = _T("Inclusion Files (*.db)|*.db|All Files (*.*)|*.*||");
- CIncAFileMgr::CIncAFileMgr(CString fileName)
- {
- // initialization
-
- m_strPathName = fileName;
- Init();
- }
- CIncAFileMgr::~CIncAFileMgr()
- {
- // cleanup
- Cleanup();
- }
- //Create
- BOOL CIncAFileMgr::CreateIncAFile()
- {
- // check file name
- m_strPathName.Trim();
- if (m_strPathName.IsEmpty())
- {
- // error, wrong file name
- LogErrorTrace(__FILE__, __LINE__, _T("Empty file path name"));
- ASSERT(FALSE);
- return FALSE;
- }
- // get database name
- CString sDatabaseName = GetPathName();
- if (OTSTools::COTSFileSys::Exists(sDatabaseName))
- {
- if (!Open(m_strPathName, FALSE))
- {
- LogErrorTrace(__FILE__, __LINE__, _T("Open X-ray file failed."));
- ASSERT(FALSE);
- return FALSE;
- }
- }
- else
- {
- if (!Create(m_strPathName))
- {
- LogErrorTrace(__FILE__, __LINE__, _T("Create X-ray file failed."));
- ASSERT(FALSE);
- return FALSE;
- }
- }
- return TRUE;
- }
- BOOL CIncAFileMgr::Save(CString a_strPathName/* = _T("")*/)
- {
- AFX_MANAGE_STATE(AfxGetStaticModuleState());
- // check file pathname
- a_strPathName.Trim();
- if (a_strPathName.IsEmpty())
- {
- // file save as dialog
- CFileDialog dlg(FALSE, INCA_FILE_EXT, NULL, OFN_OVERWRITEPROMPT, INCA_FILE_FILTER);
- if (dlg.DoModal() != IDOK)
- {
- return FALSE;
- }
- // get file pathname
- a_strPathName = dlg.GetPathName();
- }
- // file pathname
- m_strPathName = a_strPathName;
- /*if (!CreateIncAFile())
- {
- LogErrorTrace(__FILE__, __LINE__, _T("Create or open X-ray file failed."));
- return FALSE;
- }*/
- if (!SaveIncAList())
- {
- LogErrorTrace(__FILE__, __LINE__, _T("Get X-ray list failed."));
- return FALSE;
- }
- // ok, return TRUE
- return TRUE;
- }
- BOOL CIncAFileMgr::Update(CString a_strPathName/* = _T("")*/)
- {
- AFX_MANAGE_STATE(AfxGetStaticModuleState());
- // check file pathname
- a_strPathName.Trim();
- if (a_strPathName.IsEmpty())
- {
- // file save as dialog
- CFileDialog dlg(FALSE, INCA_FILE_EXT, NULL, OFN_OVERWRITEPROMPT, INCA_FILE_FILTER);
- if (dlg.DoModal() != IDOK)
- {
- return FALSE;
- }
- // get file pathname
- a_strPathName = dlg.GetPathName();
- }
- // file pathname
- m_strPathName = a_strPathName;
- /* if (!CreateIncAFile())
- {
- LogErrorTrace(__FILE__, __LINE__, _T("Create or open X-ray file failed."));
- return FALSE;
- }*/
- if (!UpdateIncAList())
- {
- LogErrorTrace(__FILE__, __LINE__, _T("Update IncAList failed."));
- return FALSE;
- }
- // ok, return TRUE
- return TRUE;
- }
- void CIncAFileMgr::SetParticleList(COTSParticleList& a_listParticle, BOOL a_bClear)
- {
- // clear holes list if necessary
- if (a_bClear)
- {
- m_listParticle.clear();
- }
- // copy the list
- for (auto pParticle : a_listParticle)
- {
-
-
- m_listParticle.push_back(pParticle);
- }
- }
- void CIncAFileMgr::SetPosXrayList(CPosXraysList& a_listPosXray, BOOL a_bClear)
- {
- // clear holes list if necessary
- if (a_bClear)
- {
- m_listPosXray.clear();
- }
- // copy the list
- for (auto pPosXray : a_listPosXray)
- {
- m_listPosXray.push_back(pPosXray);
- }
- }
- bool CIncAFileMgr::GetAllFieldsFromDB(COTSFieldDataList & allFlds,CMsrSampleStatusPtr& status, CMsrResultsPtr& rst,double aFieldArea)
- {
- CString sDatabaseName = GetPathName();
- if (OTSTools::COTSFileSys::Exists(sDatabaseName))
- {
- if (!Open(m_strPathName, FALSE))
- {
- LogErrorTrace(__FILE__, __LINE__, _T("Open X-ray file failed."));
- ASSERT(FALSE);
- return false;
- }
- }
- std::vector <CPoint> completedflds;
- auto IncADataDB = GetIncADB();
- IncADataDB->GetAllFieldsRecord(allFlds);
- auto SegmentDB = GetSegmentDB();
- double msrFldsArea=0;
- std::map <int, COTSParticleList> mapTypeParticles;//record typeId relevants particlelist;
- std::map < std::vector <int>, COTSSegmentsList> AllSegments;
- if (SegmentDB->GetAllSegmentsRecord(AllSegments))
- {
- int nCol;
- for (auto fld : allFlds)
- {
- int fldId = fld->GetId();
- COTSParticleList& parts = fld->GetParticleList();
- for (auto part : parts)
- {
- std::vector<int> fldvec;
- fldvec.push_back(fldId);
- fldvec.push_back(part->GetTagId());
- auto itr = AllSegments.find(fldvec);
- if (itr != AllSegments.end())
- {
- COTSFeaturePtr f = COTSFeaturePtr(new COTSFeature());
- f->SetSegmentsList(itr->second);
- part->SetFeature(f);
- }
- mapTypeParticles[(int)part->GetType()].push_back(part);
- }
- completedflds.push_back(fld->GetPosition());
- msrFldsArea += aFieldArea;
- }
- }
- // get MsrStatus info from DB.
- auto GenDB = GetGeneralInfoDB();
- CString strTimeStart, strTimeEnd, strRstStatus;
- GenDB->GetStringValue(GenDB->GetTableItemNameTimeStart(), strTimeStart);
- GenDB->GetStringValue(GenDB->GetTableItemNameTimeEnd(), strTimeEnd);
- GenDB->GetStringValue(GenDB->GetTableItemNameResultStatus(), strRstStatus);
- status->SetCompletedFieldsCenter(completedflds);
- status->SetCompletedFields(completedflds.size());
- COleDateTime timeStart, timeEnd;
- COleDateTimeSpan timeSpan;
- timeStart = OTSTools::COTSHelper::GetDateTimeFromString(strTimeStart);
- timeEnd = OTSTools::COTSHelper::GetDateTimeFromString(strTimeEnd);
- status->SetStartTime(timeStart);
- status->SetEndTime(timeEnd);
- status->SetUsedTime(timeEnd - timeStart);
- status->SetStatus((OTS_MSR_SAMPLE_STATUS)std::stoi(strRstStatus.GetBuffer()));
- //get MsrResults data from map.
- CMsrResultItemsList rstItms;
- double allPartArea=0;
- for (auto typeParticles : mapTypeParticles)
- {
- CMsrResultItemPtr rstItm = CMsrResultItemPtr(new CMsrResultItem());
- int typeNum=0;
- double typeArea=0;
- for (auto p : typeParticles.second)
- {
- typeNum += 1;
- typeArea += p->GetActualArea();
- }
- rstItm->SetTypeId(typeParticles.first);
- rstItm->SetNumber(typeNum);
- rstItm->SetArea(typeArea);
- rstItms.push_back(rstItm);
- allPartArea += typeArea;
- }
- rst->SetResultItems(rstItms);
- rst->SetMeasuredArea(msrFldsArea*1000000);
- rst->SetRadio(allPartArea / (msrFldsArea * 1000000));
- return TRUE;
-
- }
-
- //protected:
- BOOL CIncAFileMgr::SaveIncAList()
- {
-
- auto IncADataDB = GetIncADB();
- IncADataDB->GetDatastore()->CloseSynchronous();
- IncADataDB->GetDatastore()->BeginTransaction();
- m_generalInfoTable = GetGeneralInfoDB();
- m_generalInfoTable->UpdateTimeStampRow(m_generalInfoTable->GetTableItemNameTimeEnd(), _T(""));
- int sta = (int)this->GetMsrStatus()->GetStatus();
- m_generalInfoTable->UpdateIntegerRow(m_generalInfoTable->GetTableItemNameResultStatus(), sta);
- for (auto pParticle : m_listParticle)
- {
- int nFieldId = pParticle->GetFieldId();
- int nXrayId = pParticle->GetAnalysisId();
- CPosXrayPtr pXrayPtr;
- auto pXray = std::find_if(m_listPosXray.begin(), m_listPosXray.end(), [nXrayId](CPosXrayPtr posxray) {return nXrayId == posxray->GetIndex(); });
- if (pXray != m_listPosXray.end())
- {
- pXrayPtr = *pXray;
- int nFieldIdInXray = pXrayPtr->GetScanFieldId();
- if (!(nFieldId == nFieldIdInXray))
- {
- LogErrorTrace(__FILE__, __LINE__, _T("SaveIncAList: field id is not same."));
- return FALSE;
- }
- }
- else
- {
- pXrayPtr = CPosXrayPtr(new CPosXray());
- }
-
- //save x-ray data
-
-
- if (!IncADataDB->SaveAIncA(pParticle, pXrayPtr, m_FieldPos))
- {
- LogErrorTrace(__FILE__, __LINE__, _T("Failed to save a inclusion data."));
- return FALSE;
- }
-
- // save segment
-
- auto SegmentDB = GetSegmentDB();
-
- if (!SegmentDB->SaveFeature(pParticle))
- {
- LogErrorTrace(__FILE__, __LINE__, _T("Failed to save element chemistry list data."));
- return FALSE;
- }
- }
- IncADataDB->GetDatastore()->CommitTransaction();
- return TRUE;
- }
- BOOL CIncAFileMgr::UpdateIncAList()
- {
- auto IncADataDB = GetIncADB();
- IncADataDB->GetDatastore()->CloseSynchronous();
- IncADataDB->GetDatastore()->BeginTransaction();
- for (auto pParticle : m_listParticle)
- {
- if (!IncADataDB->UpdataAIncA(pParticle))
- {
- LogErrorTrace(__FILE__, __LINE__, _T("Failed to update a inclusion data."));
- return FALSE;
- }
- }
- IncADataDB->GetDatastore()->CommitTransaction();
- return TRUE;
- }
-
- //Get DB
- CIncADataDBPtr CIncAFileMgr::GetIncADB()
- {
- if (!m_pIncADataDB)
- {
- auto datastorePtr = GetDatastore();
- if (datastorePtr)
- {
- m_pIncADataDB = std::make_shared<CIncADataDB>(datastorePtr);
- }
- }
- ASSERT(m_pIncADataDB);
- return m_pIncADataDB;
- }
- CElementChemistryDBPtr CIncAFileMgr::GetElementChemistryDB()
- {
- if (!m_pElementChemistryDB)
- {
- auto datastorePtr = GetDatastore();
- if (datastorePtr)
- {
- m_pElementChemistryDB = std::make_shared<CElementChemistryDB>(datastorePtr);
- }
- }
- ASSERT(m_pElementChemistryDB);
- return m_pElementChemistryDB;
- }
- CSegmentDBPtr CIncAFileMgr::GetSegmentDB()
- {
- if (!m_pSegmentDB)
- {
- auto datastorePtr = GetDatastore();
- if (datastorePtr)
- {
- m_pSegmentDB = std::make_shared<CSegmentDB>(datastorePtr);
- }
- }
- ASSERT(m_pSegmentDB);
- return m_pSegmentDB;
- }
- // cleanup
- void CIncAFileMgr::Cleanup()
- {
- m_listParticle.clear();
- // X-ray list
- m_listPosXray.clear();
- }
- // initialization
- void CIncAFileMgr::Init()
- {
-
-
- // particle list
- m_listParticle.clear();
- // X-ray list
- m_listPosXray.clear();
- if (!CreateIncAFile())
- {
- LogErrorTrace(__FILE__, __LINE__, _T("Create or open X-ray file failed."));
- ASSERT(false);
- }
- }
- // duplication
- void CIncAFileMgr::Duplicate(const CIncAFileMgr& a_oSource)
- {
- // initialization
- m_strPathName = _T("");
- // particle list
- m_listParticle.clear();
- // X-ray list
- m_listPosXray.clear();
- // copy data over
- for (auto pPosXray : a_oSource.m_listPosXray)
- {
- CPosXrayPtr pPosXrayNew = CPosXrayPtr(new CPosXray(*pPosXray.get()));
- m_listPosXray.push_back(pPosXrayNew);
- }
- for (auto pParticle : a_oSource.m_listParticle)
- {
- COTSParticlePtr pParticleNew = COTSParticlePtr(new COTSParticle(*pParticle.get()));
- m_listParticle.push_back(pParticleNew);
- }
- m_strPathName = a_oSource.m_strPathName;
- }
- }
|