OTSSpectrumLibFileMgr.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #include "stdafx.h"
  2. #include "OTSSpectrumLibFileMgr.h"
  3. #include "OTSFileSys.h"
  4. using namespace OTSTools;
  5. namespace OTSClassifyEngine
  6. {
  7. // project file extension
  8. const CString ORETYPE_FILE_EXT = _T("db");
  9. const CString ORETYPE_FILE_FILTER = _T("Particle Type Files (*.db)|*.db|All Files (*.*)|*.*||");
  10. const CString SysSTDLibFilePath = ".\\Config\\SysData\\";
  11. // constructor
  12. CSpectrumLibFileMgr::CSpectrumLibFileMgr(std::string fileName)
  13. {
  14. m_strPathName = SysSTDLibFilePath+fileName.c_str();
  15. Init();
  16. }
  17. // destructor
  18. CSpectrumLibFileMgr::~CSpectrumLibFileMgr()
  19. {
  20. // cleanup
  21. Cleanup();
  22. }
  23. BOOL CSpectrumLibFileMgr::CreateSTDLibFile()
  24. {
  25. // check file name
  26. m_strPathName.Trim();
  27. if (m_strPathName.IsEmpty())
  28. {
  29. // error, wrong file name
  30. LogErrorTrace(__FILE__, __LINE__, _T("Empty file path name"));
  31. ASSERT(FALSE);
  32. return FALSE;
  33. }
  34. if (COTSFileSys::Exists(m_strPathName))
  35. {
  36. if (!Open(m_strPathName, FALSE))
  37. {
  38. LogErrorTrace(__FILE__, __LINE__, _T("Open STD file failed."));
  39. ASSERT(FALSE);
  40. return FALSE;
  41. }
  42. }
  43. else
  44. {
  45. if (!Create(m_strPathName))
  46. {
  47. LogErrorTrace(__FILE__, __LINE__, _T("Create STD file failed."));
  48. ASSERT(FALSE);
  49. return FALSE;
  50. }
  51. }
  52. return TRUE;
  53. }
  54. // Load/Save
  55. BOOL CSpectrumLibFileMgr::LoadSTDSpectrumItems(CSpectrumSTDItemList& itms, BOOL bClear /*= TRUE*/)
  56. {
  57. ;
  58. // program manager param file exists?
  59. if (!COTSFileSys::Exists(m_strPathName))
  60. {
  61. LogErrorTrace(__FILE__, __LINE__, _T("GenerateSTDLib: there is no STD lib file."+ m_strPathName));
  62. return FALSE;
  63. }
  64. if (!CreateSTDLibFile())
  65. {
  66. LogErrorTrace(__FILE__, __LINE__, _T("Create or open STD file failed."));
  67. return FALSE;
  68. }
  69. m_pSTDLibDB=GetSTDLibDB();
  70. auto specItms= m_pSTDLibDB->GetSTDItems(bClear);
  71. for (auto itm : specItms)
  72. {
  73. itms.push_back(itm);
  74. }
  75. return TRUE;
  76. }
  77. BOOL CSpectrumLibFileMgr::InsertSpectrumSTDItemIntoDB(CSpectrumSTDItemPtr itm)
  78. {
  79. if (!CreateSTDLibFile())
  80. {
  81. LogErrorTrace(__FILE__, __LINE__, _T("Create or open STD file failed."));
  82. return FALSE;
  83. }
  84. auto specDB = GetSTDLibDB();
  85. auto specTableInfoPtr = specDB->GetTableInfo();
  86. auto datastorePtr = specDB->GetDatastore();
  87. auto sInsertFormat = specTableInfoPtr->GetInsertCommandFormatString(TRUE);
  88. CString sSQLCommand;
  89. DWORD* xrayData =itm->GetXraySpectrum()->GetXrayData();
  90. sSQLCommand.Format(sInsertFormat,
  91. itm->GetID(),
  92. itm->GetName(),
  93. "",
  94. 0.0,
  95. 0,
  96. 0,
  97. 0,
  98. itm->GetColor()
  99. );
  100. auto sql = sSQLCommand.GetBuffer();
  101. if (!datastorePtr->InsertBlobData(sql, xrayData, GENERALXRAYCHANNELS * 4))
  102. {
  103. ASSERT(FALSE);
  104. return FALSE;
  105. }
  106. return TRUE;
  107. }
  108. CSTDLibDBPtr CSpectrumLibFileMgr::GetSTDLibDB()
  109. {
  110. if (!m_pSTDLibDB)
  111. {
  112. auto datastorePtr = GetDatastore();
  113. if (datastorePtr)
  114. {
  115. m_pSTDLibDB = std::make_shared<CSTDLibDB>(datastorePtr);
  116. }
  117. }
  118. ASSERT(m_pSTDLibDB);
  119. return m_pSTDLibDB;
  120. }
  121. void CSpectrumLibFileMgr::Init()
  122. {
  123. }
  124. void CSpectrumLibFileMgr::Cleanup()
  125. {
  126. }
  127. }