IncAFileMgr.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. #pragma once
  2. #include "stdafx.h"
  3. #include "IncAFileMgr.h"
  4. #include "DBConst.h"
  5. #include "OTSHelper.h"
  6. namespace OTSMODEL {
  7. using namespace OTSTools;
  8. // project file extension
  9. const CString INCA_FILE_EXT = _T("db");
  10. // project file filter
  11. const CString INCA_FILE_FILTER = _T("Inclusion Files (*.db)|*.db|All Files (*.*)|*.*||");
  12. CIncAFileMgr::CIncAFileMgr(CString fileName)
  13. {
  14. // initialization
  15. m_strPathName = fileName;
  16. Init();
  17. }
  18. CIncAFileMgr::~CIncAFileMgr()
  19. {
  20. // cleanup
  21. Cleanup();
  22. }
  23. //Create
  24. BOOL CIncAFileMgr::CreateIncAFile()
  25. {
  26. // check file name
  27. m_strPathName.Trim();
  28. if (m_strPathName.IsEmpty())
  29. {
  30. // error, wrong file name
  31. LogErrorTrace(__FILE__, __LINE__, _T("Empty file path name"));
  32. ASSERT(FALSE);
  33. return FALSE;
  34. }
  35. // get database name
  36. CString sDatabaseName = GetPathName();
  37. if (OTSTools::COTSFileSys::Exists(sDatabaseName))
  38. {
  39. if (!Open(m_strPathName, FALSE))
  40. {
  41. LogErrorTrace(__FILE__, __LINE__, _T("Open X-ray file failed."));
  42. ASSERT(FALSE);
  43. return FALSE;
  44. }
  45. }
  46. else
  47. {
  48. if (!Create(m_strPathName))
  49. {
  50. LogErrorTrace(__FILE__, __LINE__, _T("Create X-ray file failed."));
  51. ASSERT(FALSE);
  52. return FALSE;
  53. }
  54. }
  55. return TRUE;
  56. }
  57. BOOL CIncAFileMgr::Save(CString a_strPathName/* = _T("")*/)
  58. {
  59. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  60. // check file pathname
  61. a_strPathName.Trim();
  62. if (a_strPathName.IsEmpty())
  63. {
  64. // file save as dialog
  65. CFileDialog dlg(FALSE, INCA_FILE_EXT, NULL, OFN_OVERWRITEPROMPT, INCA_FILE_FILTER);
  66. if (dlg.DoModal() != IDOK)
  67. {
  68. return FALSE;
  69. }
  70. // get file pathname
  71. a_strPathName = dlg.GetPathName();
  72. }
  73. // file pathname
  74. m_strPathName = a_strPathName;
  75. /*if (!CreateIncAFile())
  76. {
  77. LogErrorTrace(__FILE__, __LINE__, _T("Create or open X-ray file failed."));
  78. return FALSE;
  79. }*/
  80. if (!SaveIncAList())
  81. {
  82. LogErrorTrace(__FILE__, __LINE__, _T("Get X-ray list failed."));
  83. return FALSE;
  84. }
  85. // ok, return TRUE
  86. return TRUE;
  87. }
  88. BOOL CIncAFileMgr::Update(CString a_strPathName/* = _T("")*/)
  89. {
  90. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  91. // check file pathname
  92. a_strPathName.Trim();
  93. if (a_strPathName.IsEmpty())
  94. {
  95. // file save as dialog
  96. CFileDialog dlg(FALSE, INCA_FILE_EXT, NULL, OFN_OVERWRITEPROMPT, INCA_FILE_FILTER);
  97. if (dlg.DoModal() != IDOK)
  98. {
  99. return FALSE;
  100. }
  101. // get file pathname
  102. a_strPathName = dlg.GetPathName();
  103. }
  104. // file pathname
  105. m_strPathName = a_strPathName;
  106. /* if (!CreateIncAFile())
  107. {
  108. LogErrorTrace(__FILE__, __LINE__, _T("Create or open X-ray file failed."));
  109. return FALSE;
  110. }*/
  111. if (!UpdateIncAList())
  112. {
  113. LogErrorTrace(__FILE__, __LINE__, _T("Update IncAList failed."));
  114. return FALSE;
  115. }
  116. // ok, return TRUE
  117. return TRUE;
  118. }
  119. void CIncAFileMgr::SetParticleList(COTSParticleList& a_listParticle, BOOL a_bClear)
  120. {
  121. // clear holes list if necessary
  122. if (a_bClear)
  123. {
  124. m_listParticle.clear();
  125. }
  126. // copy the list
  127. for (auto pParticle : a_listParticle)
  128. {
  129. m_listParticle.push_back(pParticle);
  130. }
  131. }
  132. void CIncAFileMgr::SetPosXrayList(CPosXrayList& a_listPosXray, BOOL a_bClear)
  133. {
  134. // clear holes list if necessary
  135. if (a_bClear)
  136. {
  137. m_listPosXray.clear();
  138. }
  139. // copy the list
  140. for (auto pPosXray : a_listPosXray)
  141. {
  142. m_listPosXray.push_back(pPosXray);
  143. }
  144. }
  145. bool CIncAFileMgr::GetAllFieldsFromDB(COTSFieldDataList & allFlds,CMsrSampleStatusPtr& status, CMsrResultsPtr& rst,double aFieldArea)
  146. {
  147. CString sDatabaseName = GetPathName();
  148. if (OTSTools::COTSFileSys::Exists(sDatabaseName))
  149. {
  150. if (!Open(m_strPathName, FALSE))
  151. {
  152. LogErrorTrace(__FILE__, __LINE__, _T("Open X-ray file failed."));
  153. ASSERT(FALSE);
  154. return false;
  155. }
  156. }
  157. std::vector <CPoint> completedflds;
  158. auto IncADataDB = GetIncADB();
  159. auto EleInfoDB = GetElementChemistryDB();
  160. IncADataDB->GetAllFieldsRecord(allFlds);
  161. auto SegmentDB = GetSegmentDB();
  162. double msrFldsArea=0;
  163. std::map <int, COTSParticleList> mapTypeParticles;//record typeId relevants particlelist;
  164. std::map < std::vector <int>, COTSSegmentsList> AllSegments;
  165. if (SegmentDB->GetAllSegmentsRecord(AllSegments))
  166. {
  167. int nCol;
  168. for (auto fld : allFlds)
  169. {
  170. int fldId = fld->GetId();
  171. COTSParticleList& parts = fld->GetParticleList();
  172. for (auto part : parts)
  173. {
  174. std::vector<int> fldvec;
  175. fldvec.push_back(fldId);
  176. fldvec.push_back(part->GetParticleId());
  177. auto itr = AllSegments.find(fldvec);
  178. if (itr != AllSegments.end())
  179. {
  180. COTSFeaturePtr f = COTSFeaturePtr(new COTSFeature());
  181. f->SetSegmentsList(itr->second);
  182. part->SetFeature(f);
  183. }
  184. mapTypeParticles[(int)part->GetType()].push_back(part);
  185. }
  186. completedflds.push_back(fld->GetPosition());
  187. msrFldsArea += aFieldArea;
  188. }
  189. }
  190. auto xrayInfoMap = EleInfoDB->ReadPosXrayInfo();
  191. if (xrayInfoMap.size()>0)
  192. {
  193. int nCol;
  194. for (auto fld : allFlds)
  195. {
  196. int fldId = fld->GetId();
  197. COTSParticleList& parts = fld->GetParticleList();
  198. for (auto part : parts)
  199. {
  200. std::vector<int> fldvec;
  201. fldvec.push_back(fldId);
  202. fldvec.push_back(part->GetParticleId());
  203. auto itr = xrayInfoMap.find(fldvec);
  204. if (itr != xrayInfoMap.end())
  205. {
  206. part->SetXrayInfo(itr->second);
  207. }
  208. }
  209. }
  210. }
  211. // get MsrStatus info from DB.
  212. auto GenDB = GetGeneralInfoDB();
  213. CString strTimeStart, strTimeEnd, strRstStatus;
  214. GenDB->GetStringValue(GenDB->GetTableItemNameTimeStart(), strTimeStart);
  215. GenDB->GetStringValue(GenDB->GetTableItemNameTimeEnd(), strTimeEnd);
  216. GenDB->GetStringValue(GenDB->GetTableItemNameResultStatus(), strRstStatus);
  217. status->SetCompletedFieldsCenter(completedflds);
  218. status->SetCompletedFields(completedflds.size());
  219. COleDateTime timeStart, timeEnd;
  220. COleDateTimeSpan timeSpan;
  221. timeStart = OTSTools::COTSHelper::GetDateTimeFromString(strTimeStart);
  222. timeEnd = OTSTools::COTSHelper::GetDateTimeFromString(strTimeEnd);
  223. status->SetStartTime(timeStart);
  224. status->SetEndTime(timeEnd);
  225. status->SetUsedTime(timeEnd - timeStart);
  226. status->SetStatus((OTS_MSR_SAMPLE_STATUS)std::stoi(strRstStatus.GetBuffer()));
  227. //get MsrResults data from map.
  228. CMsrResultItemsList rstItms;
  229. double allPartArea=0;
  230. for (auto typeParticles : mapTypeParticles)
  231. {
  232. CMsrResultItemPtr rstItm = CMsrResultItemPtr(new CMsrResultItem());
  233. int typeNum=0;
  234. double typeArea=0;
  235. for (auto p : typeParticles.second)
  236. {
  237. typeNum += 1;
  238. typeArea += p->GetActualArea();
  239. }
  240. rstItm->SetTypeId(typeParticles.first);
  241. rstItm->SetNumber(typeNum);
  242. rstItm->SetArea(typeArea);
  243. rstItms.push_back(rstItm);
  244. allPartArea += typeArea;
  245. }
  246. rst->SetResultItems(rstItms);
  247. rst->SetMeasuredArea(msrFldsArea*1000000);
  248. rst->SetRadio(allPartArea / (msrFldsArea * 1000000));
  249. return TRUE;
  250. }
  251. //protected:
  252. BOOL CIncAFileMgr::SaveIncAList()
  253. {
  254. auto IncADataDB = GetIncADB();
  255. IncADataDB->GetDatastore()->CloseSynchronous();
  256. IncADataDB->GetDatastore()->BeginTransaction();
  257. m_generalInfoTable = GetGeneralInfoDB();
  258. m_generalInfoTable->UpdateTimeStampRow(m_generalInfoTable->GetTableItemNameTimeEnd(), _T(""));
  259. int sta = (int)this->GetMsrStatus()->GetStatus();
  260. m_generalInfoTable->UpdateIntegerRow(m_generalInfoTable->GetTableItemNameResultStatus(), sta);
  261. for (auto pParticle : m_listParticle)
  262. {
  263. int nFieldId = pParticle->GetFieldId();
  264. int nXrayId = pParticle->GetAnalysisId();
  265. CPosXrayPtr pXrayPtr;
  266. auto pXray = std::find_if(m_listPosXray.begin(), m_listPosXray.end(), [nXrayId](CPosXrayPtr posxray) {return nXrayId == posxray->GetIndex(); });
  267. if (pXray != m_listPosXray.end())
  268. {
  269. pXrayPtr = *pXray;
  270. int nFieldIdInXray = pXrayPtr->GetScanFieldId();
  271. if (!(nFieldId == nFieldIdInXray))
  272. {
  273. LogErrorTrace(__FILE__, __LINE__, _T("SaveIncAList: field id is not same."));
  274. return FALSE;
  275. }
  276. }
  277. else
  278. {
  279. pXrayPtr = CPosXrayPtr(new CPosXray());
  280. }
  281. //save x-ray data
  282. if (!IncADataDB->SaveAIncA(pParticle, pXrayPtr, m_FieldPos))
  283. {
  284. LogErrorTrace(__FILE__, __LINE__, _T("Failed to save a inclusion data."));
  285. return FALSE;
  286. }
  287. // save segment
  288. auto SegmentDB = GetSegmentDB();
  289. if (!SegmentDB->SaveFeature(pParticle))
  290. {
  291. LogErrorTrace(__FILE__, __LINE__, _T("Failed to save element chemistry list data."));
  292. return FALSE;
  293. }
  294. }
  295. IncADataDB->GetDatastore()->CommitTransaction();
  296. return TRUE;
  297. }
  298. BOOL CIncAFileMgr::UpdateIncAList()
  299. {
  300. auto IncADataDB = GetIncADB();
  301. IncADataDB->GetDatastore()->CloseSynchronous();
  302. IncADataDB->GetDatastore()->BeginTransaction();
  303. for (auto pParticle : m_listParticle)
  304. {
  305. if (!IncADataDB->UpdataAIncA(pParticle))
  306. {
  307. LogErrorTrace(__FILE__, __LINE__, _T("Failed to update a inclusion data."));
  308. return FALSE;
  309. }
  310. }
  311. IncADataDB->GetDatastore()->CommitTransaction();
  312. return TRUE;
  313. }
  314. //Get DB
  315. CIncADataDBPtr CIncAFileMgr::GetIncADB()
  316. {
  317. if (!m_pIncADataDB)
  318. {
  319. auto datastorePtr = GetDatastore();
  320. if (datastorePtr)
  321. {
  322. m_pIncADataDB = std::make_shared<CIncADataDB>(datastorePtr);
  323. }
  324. }
  325. ASSERT(m_pIncADataDB);
  326. return m_pIncADataDB;
  327. }
  328. CElementChemistryDBPtr CIncAFileMgr::GetElementChemistryDB()
  329. {
  330. if (!m_pElementChemistryDB)
  331. {
  332. auto datastorePtr = GetDatastore();
  333. if (datastorePtr)
  334. {
  335. m_pElementChemistryDB = std::make_shared<CElementChemistryDB>(datastorePtr);
  336. }
  337. }
  338. ASSERT(m_pElementChemistryDB);
  339. return m_pElementChemistryDB;
  340. }
  341. CSegmentDBPtr CIncAFileMgr::GetSegmentDB()
  342. {
  343. if (!m_pSegmentDB)
  344. {
  345. auto datastorePtr = GetDatastore();
  346. if (datastorePtr)
  347. {
  348. m_pSegmentDB = std::make_shared<CSegmentDB>(datastorePtr);
  349. }
  350. }
  351. ASSERT(m_pSegmentDB);
  352. return m_pSegmentDB;
  353. }
  354. // cleanup
  355. void CIncAFileMgr::Cleanup()
  356. {
  357. m_listParticle.clear();
  358. // X-ray list
  359. m_listPosXray.clear();
  360. }
  361. // initialization
  362. void CIncAFileMgr::Init()
  363. {
  364. // particle list
  365. m_listParticle.clear();
  366. // X-ray list
  367. m_listPosXray.clear();
  368. if (!CreateIncAFile())
  369. {
  370. LogErrorTrace(__FILE__, __LINE__, _T("Create or open X-ray file failed."));
  371. ASSERT(false);
  372. }
  373. }
  374. // duplication
  375. void CIncAFileMgr::Duplicate(const CIncAFileMgr& a_oSource)
  376. {
  377. // initialization
  378. m_strPathName = _T("");
  379. // particle list
  380. m_listParticle.clear();
  381. // X-ray list
  382. m_listPosXray.clear();
  383. // copy data over
  384. for (auto pPosXray : a_oSource.m_listPosXray)
  385. {
  386. CPosXrayPtr pPosXrayNew = CPosXrayPtr(new CPosXray(*pPosXray.get()));
  387. m_listPosXray.push_back(pPosXrayNew);
  388. }
  389. for (auto pParticle : a_oSource.m_listParticle)
  390. {
  391. COTSParticlePtr pParticleNew = COTSParticlePtr(new COTSParticle(*pParticle.get()));
  392. m_listParticle.push_back(pParticleNew);
  393. }
  394. m_strPathName = a_oSource.m_strPathName;
  395. }
  396. }