IncAFileMgr.cpp 10 KB

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