IncAFileMgr.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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(CPosXraysList& 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. IncADataDB->GetAllFieldsRecord(allFlds);
  160. auto SegmentDB = GetSegmentDB();
  161. double msrFldsArea=0;
  162. std::map <int, COTSParticleList> mapTypeParticles;//record typeId relevants particlelist;
  163. std::map < std::vector <int>, COTSSegmentsList> AllSegments;
  164. if (SegmentDB->GetAllSegmentsRecord(AllSegments))
  165. {
  166. int nCol;
  167. for (auto fld : allFlds)
  168. {
  169. int fldId = fld->GetId();
  170. COTSParticleList& parts = fld->GetParticleList();
  171. for (auto part : parts)
  172. {
  173. std::vector<int> fldvec;
  174. fldvec.push_back(fldId);
  175. fldvec.push_back(part->GetTagId());
  176. auto itr = AllSegments.find(fldvec);
  177. if (itr != AllSegments.end())
  178. {
  179. COTSFeaturePtr f = COTSFeaturePtr(new COTSFeature());
  180. f->SetSegmentsList(itr->second);
  181. part->SetFeature(f);
  182. }
  183. mapTypeParticles[(int)part->GetType()].push_back(part);
  184. }
  185. completedflds.push_back(fld->GetPosition());
  186. msrFldsArea += aFieldArea;
  187. }
  188. }
  189. // get MsrStatus info from DB.
  190. auto GenDB = GetGeneralInfoDB();
  191. CString strTimeStart, strTimeEnd, strRstStatus;
  192. GenDB->GetStringValue(GenDB->GetTableItemNameTimeStart(), strTimeStart);
  193. GenDB->GetStringValue(GenDB->GetTableItemNameTimeEnd(), strTimeEnd);
  194. GenDB->GetStringValue(GenDB->GetTableItemNameResultStatus(), strRstStatus);
  195. status->SetCompletedFieldsCenter(completedflds);
  196. status->SetCompletedFields(completedflds.size());
  197. COleDateTime timeStart, timeEnd;
  198. COleDateTimeSpan timeSpan;
  199. timeStart = OTSTools::COTSHelper::GetDateTimeFromString(strTimeStart);
  200. timeEnd = OTSTools::COTSHelper::GetDateTimeFromString(strTimeEnd);
  201. status->SetStartTime(timeStart);
  202. status->SetEndTime(timeEnd);
  203. status->SetUsedTime(timeEnd - timeStart);
  204. status->SetStatus((OTS_MSR_SAMPLE_STATUS)std::stoi(strRstStatus.GetBuffer()));
  205. //get MsrResults data from map.
  206. CMsrResultItemsList rstItms;
  207. double allPartArea=0;
  208. for (auto typeParticles : mapTypeParticles)
  209. {
  210. CMsrResultItemPtr rstItm = CMsrResultItemPtr(new CMsrResultItem());
  211. int typeNum=0;
  212. double typeArea=0;
  213. for (auto p : typeParticles.second)
  214. {
  215. typeNum += 1;
  216. typeArea += p->GetActualArea();
  217. }
  218. rstItm->SetTypeId(typeParticles.first);
  219. rstItm->SetNumber(typeNum);
  220. rstItm->SetArea(typeArea);
  221. rstItms.push_back(rstItm);
  222. allPartArea += typeArea;
  223. }
  224. rst->SetResultItems(rstItms);
  225. rst->SetMeasuredArea(msrFldsArea*1000000);
  226. rst->SetRadio(allPartArea / (msrFldsArea * 1000000));
  227. return TRUE;
  228. }
  229. //protected:
  230. BOOL CIncAFileMgr::SaveIncAList()
  231. {
  232. auto IncADataDB = GetIncADB();
  233. IncADataDB->GetDatastore()->CloseSynchronous();
  234. IncADataDB->GetDatastore()->BeginTransaction();
  235. m_generalInfoTable = GetGeneralInfoDB();
  236. m_generalInfoTable->UpdateTimeStampRow(m_generalInfoTable->GetTableItemNameTimeEnd(), _T(""));
  237. int sta = (int)this->GetMsrStatus()->GetStatus();
  238. m_generalInfoTable->UpdateIntegerRow(m_generalInfoTable->GetTableItemNameResultStatus(), sta);
  239. for (auto pParticle : m_listParticle)
  240. {
  241. int nFieldId = pParticle->GetFieldId();
  242. int nXrayId = pParticle->GetAnalysisId();
  243. CPosXrayPtr pXrayPtr;
  244. auto pXray = std::find_if(m_listPosXray.begin(), m_listPosXray.end(), [nXrayId](CPosXrayPtr posxray) {return nXrayId == posxray->GetIndex(); });
  245. if (pXray != m_listPosXray.end())
  246. {
  247. pXrayPtr = *pXray;
  248. int nFieldIdInXray = pXrayPtr->GetScanFieldId();
  249. if (!(nFieldId == nFieldIdInXray))
  250. {
  251. LogErrorTrace(__FILE__, __LINE__, _T("SaveIncAList: field id is not same."));
  252. return FALSE;
  253. }
  254. }
  255. else
  256. {
  257. pXrayPtr = CPosXrayPtr(new CPosXray());
  258. }
  259. //save x-ray data
  260. if (!IncADataDB->SaveAIncA(pParticle, pXrayPtr, m_FieldPos))
  261. {
  262. LogErrorTrace(__FILE__, __LINE__, _T("Failed to save a inclusion data."));
  263. return FALSE;
  264. }
  265. // save segment
  266. auto SegmentDB = GetSegmentDB();
  267. if (!SegmentDB->SaveFeature(pParticle))
  268. {
  269. LogErrorTrace(__FILE__, __LINE__, _T("Failed to save element chemistry list data."));
  270. return FALSE;
  271. }
  272. }
  273. IncADataDB->GetDatastore()->CommitTransaction();
  274. return TRUE;
  275. }
  276. BOOL CIncAFileMgr::UpdateIncAList()
  277. {
  278. auto IncADataDB = GetIncADB();
  279. IncADataDB->GetDatastore()->CloseSynchronous();
  280. IncADataDB->GetDatastore()->BeginTransaction();
  281. for (auto pParticle : m_listParticle)
  282. {
  283. if (!IncADataDB->UpdataAIncA(pParticle))
  284. {
  285. LogErrorTrace(__FILE__, __LINE__, _T("Failed to update a inclusion data."));
  286. return FALSE;
  287. }
  288. }
  289. IncADataDB->GetDatastore()->CommitTransaction();
  290. return TRUE;
  291. }
  292. //Get DB
  293. CIncADataDBPtr CIncAFileMgr::GetIncADB()
  294. {
  295. if (!m_pIncADataDB)
  296. {
  297. auto datastorePtr = GetDatastore();
  298. if (datastorePtr)
  299. {
  300. m_pIncADataDB = std::make_shared<CIncADataDB>(datastorePtr);
  301. }
  302. }
  303. ASSERT(m_pIncADataDB);
  304. return m_pIncADataDB;
  305. }
  306. CElementChemistryDBPtr CIncAFileMgr::GetElementChemistryDB()
  307. {
  308. if (!m_pElementChemistryDB)
  309. {
  310. auto datastorePtr = GetDatastore();
  311. if (datastorePtr)
  312. {
  313. m_pElementChemistryDB = std::make_shared<CElementChemistryDB>(datastorePtr);
  314. }
  315. }
  316. ASSERT(m_pElementChemistryDB);
  317. return m_pElementChemistryDB;
  318. }
  319. CSegmentDBPtr CIncAFileMgr::GetSegmentDB()
  320. {
  321. if (!m_pSegmentDB)
  322. {
  323. auto datastorePtr = GetDatastore();
  324. if (datastorePtr)
  325. {
  326. m_pSegmentDB = std::make_shared<CSegmentDB>(datastorePtr);
  327. }
  328. }
  329. ASSERT(m_pSegmentDB);
  330. return m_pSegmentDB;
  331. }
  332. // cleanup
  333. void CIncAFileMgr::Cleanup()
  334. {
  335. m_listParticle.clear();
  336. // X-ray list
  337. m_listPosXray.clear();
  338. }
  339. // initialization
  340. void CIncAFileMgr::Init()
  341. {
  342. // particle list
  343. m_listParticle.clear();
  344. // X-ray list
  345. m_listPosXray.clear();
  346. if (!CreateIncAFile())
  347. {
  348. LogErrorTrace(__FILE__, __LINE__, _T("Create or open X-ray file failed."));
  349. ASSERT(false);
  350. }
  351. }
  352. // duplication
  353. void CIncAFileMgr::Duplicate(const CIncAFileMgr& a_oSource)
  354. {
  355. // initialization
  356. m_strPathName = _T("");
  357. // particle list
  358. m_listParticle.clear();
  359. // X-ray list
  360. m_listPosXray.clear();
  361. // copy data over
  362. for (auto pPosXray : a_oSource.m_listPosXray)
  363. {
  364. CPosXrayPtr pPosXrayNew = CPosXrayPtr(new CPosXray(*pPosXray.get()));
  365. m_listPosXray.push_back(pPosXrayNew);
  366. }
  367. for (auto pParticle : a_oSource.m_listParticle)
  368. {
  369. COTSParticlePtr pParticleNew = COTSParticlePtr(new COTSParticle(*pParticle.get()));
  370. m_listParticle.push_back(pParticleNew);
  371. }
  372. m_strPathName = a_oSource.m_strPathName;
  373. }
  374. }