OTSSTDLib.cpp 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559
  1. // describe elements
  2. #include "stdafx.h"
  3. #include "LanguageID.h"
  4. #include "MultiLang.h"
  5. #include "OTSSTDLib.h"
  6. #include <algorithm>
  7. namespace OTSClassifyEngine
  8. {
  9. // CSTDBase
  10. // constructor
  11. CSTDBase::CSTDBase()
  12. {
  13. // initialization
  14. Init();
  15. }
  16. CSTDBase::CSTDBase(const CSTDBase& a_oSource)
  17. {
  18. if (this == &a_oSource)
  19. {
  20. return;
  21. }
  22. Duplicate(a_oSource);
  23. }
  24. CSTDBase::CSTDBase(CSTDBase* a_poSource)
  25. {
  26. if (!a_poSource)
  27. {
  28. return;
  29. }
  30. if (this == a_poSource)
  31. {
  32. return;
  33. }
  34. Duplicate(*a_poSource);
  35. }
  36. CSTDBase::~CSTDBase()
  37. {
  38. Cleanup();
  39. }
  40. // =operator
  41. CSTDBase& CSTDBase::operator=(const CSTDBase& a_oSource)
  42. {
  43. Cleanup();
  44. Duplicate(a_oSource);
  45. return *this;
  46. }
  47. // ==operator
  48. BOOL CSTDBase::operator==(const CSTDBase& a_oSource)
  49. {
  50. return (m_iID == a_oSource.m_iID &&
  51. m_strName == a_oSource.m_strName &&
  52. m_strFormula == a_oSource.m_strFormula);
  53. }
  54. void CSTDBase::Serialize(bool isStoring, tinyxml2::XMLDocument * classDoc, tinyxml2::XMLElement * rootNode)
  55. {
  56. /*CPosXray::Serialize(isStoring, classDoc, rootNode);
  57. xmls::xInt xid;
  58. xmls::xString xstrname;
  59. xmls::xString xstrformula;
  60. xmls::xDouble xddensity;
  61. xmls::xDouble xdaverz;
  62. xmls::xDouble xdbulkmod;
  63. xmls::xDouble xdrigiditymod;
  64. xmls::xInt xibrpl;
  65. xmls::xInt xikind;
  66. xmls::Slo slo;
  67. slo.Register("id", &xid);
  68. slo.Register("name", &xstrname);
  69. slo.Register("formula", &xstrformula);
  70. slo.Register("density", &xddensity);
  71. slo.Register("averz", &xdaverz);
  72. if (isStoring)
  73. {
  74. xid = m_iID;
  75. xstrname = m_strName;
  76. xstrformula = m_strFormula;
  77. xddensity = m_dDensity;
  78. xdaverz = m_dAverz;
  79. slo.Serialize(true, classDoc, rootNode);
  80. }
  81. else
  82. {
  83. int iBRPL = 0;
  84. int iKind = 0;
  85. slo.Serialize(false, classDoc, rootNode);
  86. m_iID = xid.value();
  87. m_strName = xstrname.value().c_str();
  88. m_strFormula = xstrformula.value().c_str();
  89. m_dDensity = xddensity.value();
  90. m_dAverz = xdaverz.value();
  91. }*/
  92. }
  93. void CSTDBase::SetID(int iID)
  94. {
  95. m_iID = iID;
  96. }
  97. int CSTDBase::GetID()
  98. {
  99. return m_iID;
  100. }
  101. void CSTDBase::SetName(CString strName)
  102. {
  103. m_strName = strName;
  104. }
  105. CString CSTDBase::GetName()
  106. {
  107. return m_strName;
  108. }
  109. void CSTDBase::SetFormula(CString strFormula)
  110. {
  111. m_strFormula = strFormula;
  112. }
  113. CString CSTDBase::GetFormula()
  114. {
  115. return m_strFormula;
  116. }
  117. void CSTDBase::SetDensity(std::string dDensity)
  118. {
  119. m_dDensity = dDensity;
  120. }
  121. std::string CSTDBase::GetDensity()
  122. {
  123. return m_dDensity;
  124. }
  125. void CSTDBase::SetAvAltomicWeight(double dAvAltomicWeight)
  126. {
  127. m_dAverz = dAvAltomicWeight;
  128. }
  129. double CSTDBase::GetAvAltomicWeight()
  130. {
  131. return m_dAverz;
  132. }
  133. void CSTDBase::SetBulkmod(double dBulkmod)
  134. {
  135. m_dBulkmod = dBulkmod;
  136. }
  137. double CSTDBase::GetBulkmod()
  138. {
  139. return m_dBulkmod;
  140. }
  141. void CSTDBase::SetRigiditymod(double dRigiditymod)
  142. {
  143. m_dRigiditymod = dRigiditymod;
  144. }
  145. double CSTDBase::GetRigiditymod()
  146. {
  147. return m_dRigiditymod;
  148. }
  149. void CSTDBase::SetMineralBRPL(int iBRPL)
  150. {
  151. m_iBRPL = (MINERAL_BRPL)iBRPL;
  152. }
  153. void CSTDBase::SetMineralBRPL(CString strBRPL)
  154. {
  155. std::vector<CString> vecstrnames;
  156. GetBRPLNames(vecstrnames);
  157. m_iBRPL = MINERAL_BRPL::MINERAL_BR;
  158. for (int i = 0; i < (int)vecstrnames.size(); i++)
  159. {
  160. if (strBRPL == vecstrnames[i])
  161. {
  162. m_iBRPL = (MINERAL_BRPL)i;
  163. return;
  164. }
  165. }
  166. }
  167. int CSTDBase::GetMineralBRPL()
  168. {
  169. return (int)m_iBRPL;
  170. }
  171. CString CSTDBase::GetMineralBRPLName()
  172. {
  173. // AFX_MANAGE_STATE(AfxGetStaticModuleState());
  174. CString str = _T("");
  175. // str.LoadString(IDS_MINERAL_BRPL + (int)m_iBRPL);
  176. str = MultiLang::GetInstance().GetCStringByKey(10,IDS_MINERAL_BRPL + (int)m_iBRPL);
  177. return str;
  178. }
  179. void CSTDBase::GetBRPLNames(std::vector<CString>& vecstrnames)
  180. {
  181. // AFX_MANAGE_STATE(AfxGetStaticModuleState());
  182. CString str = _T("");
  183. vecstrnames.clear();
  184. for (int i = 0; i < (int)MINERAL_BRPL::MAX; i++)
  185. {
  186. // str.LoadString(IDS_MINERAL_BRPL + i);
  187. str = MultiLang::GetInstance().GetCStringByKey(10,IDS_MINERAL_BRPL + i);
  188. vecstrnames.push_back(str);
  189. }
  190. }
  191. void CSTDBase::SetMineralKind(int iKind)
  192. {
  193. m_iKind = (MINERAL_KINDS)iKind;
  194. }
  195. void CSTDBase::SetMineralKind(CString strKind)
  196. {
  197. std::vector<CString> vecstrnames;
  198. GetKindNames(vecstrnames);
  199. m_iKind = MINERAL_KINDS::MINERAL_SI;
  200. for (int i = 0; i < (int)vecstrnames.size(); i++)
  201. {
  202. if (strKind == vecstrnames[i])
  203. {
  204. m_iKind = (MINERAL_KINDS)i;
  205. return;
  206. }
  207. }
  208. }
  209. int CSTDBase::GetMineralKind()
  210. {
  211. return (int)m_iKind;
  212. }
  213. CString CSTDBase::GetMineralKindName()
  214. {
  215. // AFX_MANAGE_STATE(AfxGetStaticModuleState());
  216. CString str = _T("");
  217. // str.LoadString(IDS_MINERAL_KINDS + (int)m_iKind);
  218. str = MultiLang::GetInstance().GetCStringByKey(10,IDS_MINERAL_KINDS + (int)m_iKind);
  219. return str;
  220. }
  221. void CSTDBase::GetKindNames(std::vector<CString>& vecstrnames)
  222. {
  223. // AFX_MANAGE_STATE(AfxGetStaticModuleState());
  224. CString str = _T("");
  225. vecstrnames.clear();
  226. for (int i = 0; i < (int)MINERAL_KINDS::MAX; i++)
  227. {
  228. // str.LoadString(IDS_MINERAL_KINDS + i);
  229. str = MultiLang::GetInstance().GetCStringByKey(10,IDS_MINERAL_KINDS + i);
  230. vecstrnames.push_back(str);
  231. }
  232. }
  233. void CSTDBase::SetElementsList(const CElementsList& elementlist)
  234. {
  235. // m_listElements = elementlist;
  236. m_listElements.clear();
  237. for (int i = 0; i <(int) elementlist.size(); i++)
  238. {
  239. m_listElements.push_back(elementlist[i]);
  240. }
  241. }
  242. const CElementsList& CSTDBase::GetElementsList()
  243. {
  244. return m_listElements;
  245. }
  246. void CSTDBase::AddElement(const CElement& element)
  247. {
  248. // 判断是否存在
  249. int i = 0;
  250. for (i = 0; i < (int)m_listElements.size(); i++)
  251. {
  252. if ((*m_listElements[i]) == element)
  253. {
  254. break;
  255. }
  256. }
  257. // 不存在就添加
  258. if (i >= (int)m_listElements.size())
  259. {
  260. CElementPtr pMineral(new CElement(element));
  261. m_listElements.push_back(pMineral);
  262. }
  263. }
  264. void CSTDBase::RemoveElement(const CElement& element)
  265. {
  266. std::vector<CElementPtr>::iterator it;
  267. for (it = m_listElements.begin(); it < m_listElements.end(); it++)
  268. {
  269. if (*((*it).get()) == element)
  270. {
  271. m_listElements.erase(it);
  272. break;
  273. }
  274. }
  275. }
  276. void CSTDBase::RemoveAllElements()
  277. {
  278. m_listElements.clear();
  279. }
  280. CElementPtr CSTDBase::GetElement(int iIndex)
  281. {
  282. if (iIndex < 0 || iIndex >= GetElementCount())
  283. {
  284. return nullptr;
  285. }
  286. return m_listElements[iIndex];
  287. }
  288. int CSTDBase::GetElementCount()
  289. {
  290. return m_listElements.size();
  291. }
  292. void CSTDBase::Clear()
  293. {
  294. m_listElements.clear();
  295. }
  296. // cleanup
  297. void CSTDBase::Cleanup()
  298. {
  299. // nothing needs to be done at the moment
  300. }
  301. // initialization
  302. void CSTDBase::Init()
  303. {
  304. m_iID = -1;
  305. m_strName = _T("");
  306. m_strFormula = _T("-");
  307. m_dDensity = DENSITY_DEFAULT;
  308. m_dAverz = AVALTOMICWEIGHT_MIN;
  309. m_listElements.clear();
  310. }
  311. // duplication
  312. void CSTDBase::Duplicate(const CSTDBase& a_oSource)
  313. {
  314. Init();
  315. m_iID = a_oSource.m_iID;
  316. m_strName = a_oSource.m_strName;
  317. m_strFormula = a_oSource.m_strFormula;
  318. m_dDensity = a_oSource.m_dDensity;
  319. m_dAverz = a_oSource.m_dAverz;
  320. for (auto poElement : a_oSource.m_listElements)
  321. {
  322. m_listElements.push_back(poElement);
  323. }
  324. }
  325. CStandardItem::CStandardItem()
  326. {
  327. // initialization
  328. Init();
  329. }
  330. CStandardItem::CStandardItem(const CStandardItem& a_oSource)
  331. {
  332. if (this == &a_oSource)
  333. {
  334. return;
  335. }
  336. Duplicate(a_oSource);
  337. }
  338. CStandardItem::CStandardItem(CStandardItem* a_poSource)
  339. {
  340. if (!a_poSource)
  341. {
  342. return;
  343. }
  344. if (this == a_poSource)
  345. {
  346. return;
  347. }
  348. Duplicate(*a_poSource);
  349. }
  350. CStandardItem::~CStandardItem()
  351. {
  352. Cleanup();
  353. }
  354. CStandardItem& CStandardItem::operator=(const CStandardItem& a_oSource)
  355. {
  356. Cleanup();
  357. Duplicate(a_oSource);
  358. return *this;
  359. }
  360. BOOL CStandardItem::operator==(const CStandardItem& a_oSource)
  361. {
  362. return CSTDBase::operator==(a_oSource);
  363. }
  364. void CStandardItem::Serialize(bool isStoring, tinyxml2::XMLDocument * classDoc, tinyxml2::XMLElement * rootNode)
  365. {
  366. CSTDBase::Serialize(isStoring, classDoc, rootNode);
  367. int i = 0;
  368. xmls::xInt xicolor;
  369. xmls::xInt xibse;
  370. xmls::xInt xiclassify;
  371. xmls::Collection<CElement> xElementList;
  372. xmls::Slo slo;
  373. slo.Register("color", &xicolor);
  374. slo.Register("bse", &xibse);
  375. slo.Register("classify", &xiclassify);
  376. slo.Register("elements", &xElementList);
  377. if (isStoring)
  378. {
  379. xicolor = m_iColor;
  380. xibse = m_iBSEvalue;
  381. xiclassify = m_iClassify;
  382. xElementList.Clear();
  383. for (i = 0; i < (int)m_listElements.size(); i++)
  384. {
  385. xElementList.addItem(m_listElements[i].get());
  386. }
  387. slo.Serialize(true, classDoc, rootNode);
  388. }
  389. else
  390. {
  391. slo.Serialize(false, classDoc, rootNode);
  392. m_iColor = xicolor.value();
  393. m_iBSEvalue = xibse.value();
  394. m_iClassify = xiclassify.value();
  395. m_listElements.clear();
  396. for (i = 0; i < (int)xElementList.size(); i++)
  397. {
  398. m_listElements.push_back(CElementPtr(xElementList.getItem(i)));
  399. }
  400. }
  401. }
  402. void CStandardItem::SetColor(COLORREF iColor)
  403. {
  404. m_iColor = iColor;
  405. }
  406. COLORREF CStandardItem::GetColor()
  407. {
  408. return m_iColor;
  409. }
  410. void CStandardItem::SetBSEValue(int iBSEvalue)
  411. {
  412. m_iBSEvalue = iBSEvalue;
  413. }
  414. int CStandardItem::GetBSEValue()
  415. {
  416. return m_iBSEvalue;
  417. }
  418. void CStandardItem::SetClassify(int iClassify)
  419. {
  420. m_iClassify = iClassify;
  421. }
  422. int CStandardItem::GetClassify()
  423. {
  424. return m_iClassify;
  425. }
  426. BOOL CStandardItem::DoFormulaParser()
  427. {
  428. std::vector<CString> vecStrElementName;
  429. std::vector<double> vecDElementCount;
  430. if (DoFormulaParser(m_strFormula, vecStrElementName, vecDElementCount) < 0)
  431. {
  432. return false;
  433. }
  434. // vecStrElementName存放了m_strFormula里面的每个元素, vecDElementCount存放了每个元素的个数
  435. ASSERT(vecStrElementName.size() == vecDElementCount.size());
  436. RemoveAllElements();
  437. for (auto strName : vecStrElementName)
  438. {
  439. CElementPtr* pElementPtr = new CElementPtr(new CElement(strName));
  440. AddElement(*pElementPtr->get());
  441. }
  442. ASSERT(m_listElements.size() == vecDElementCount.size());
  443. double dAllAtomicNumber = 0;
  444. double dAllElementCount = 0;
  445. double dAllAutomicWeight = 0;
  446. double dwAvAltomicWeight = 0;
  447. for (int i = 0; i <(int) m_listElements.size(); i++)
  448. {
  449. dAllAtomicNumber += m_listElements[i]->GetAtomNum() * vecDElementCount[i];
  450. dAllElementCount += vecDElementCount[i];
  451. dAllAutomicWeight += m_listElements[i]->GetAtomWeight() * vecDElementCount[i];
  452. }
  453. dwAvAltomicWeight = (0 == dAllElementCount) ? dAllAtomicNumber : dAllAtomicNumber / dAllElementCount;
  454. // check the data is valid or not
  455. if (dAllElementCount <= 0 || dwAvAltomicWeight < AVALTOMICWEIGHT_MIN || dwAvAltomicWeight > AVALTOMICWEIGHT_MAX)
  456. {
  457. return FALSE;
  458. }
  459. SetAvAltomicWeight(dwAvAltomicWeight);
  460. // calc every element's percentage of mineralstd
  461. for (int i = 0; i < (int)m_listElements.size(); i++)
  462. {
  463. m_listElements[i]->SetPercentage(100 * m_listElements[i]->GetAtomWeight() * vecDElementCount[i] / dAllAutomicWeight);
  464. }
  465. return TRUE;
  466. }
  467. CString CStandardItem::GetSTDMineralPropName(int iPropid)
  468. {
  469. return GetPropName(IDS_MINERAL_PROP, iPropid);
  470. }
  471. CString CStandardItem::GetElementPropName(int iPropid)
  472. {
  473. return GetPropName(IDS_ELEMENT_PROP, iPropid);
  474. }
  475. void CStandardItem::Cleanup()
  476. {
  477. }
  478. // initialization
  479. void CStandardItem::Init()
  480. {
  481. CSTDBase::Init();
  482. CPosXray::Init();
  483. m_iColor = RGB(255, 255, 255);
  484. m_iBSEvalue = 0;
  485. m_iClassify = -1;
  486. }
  487. // duplication
  488. void CStandardItem::Duplicate(const CStandardItem& a_oSource)
  489. {
  490. CSTDBase::Duplicate(a_oSource);
  491. CPosXray::Duplicate(a_oSource);
  492. m_iColor = a_oSource.m_iColor;
  493. m_iBSEvalue = a_oSource.m_iBSEvalue;
  494. m_iClassify = a_oSource.m_iClassify;
  495. }
  496. int CStandardItem::DoFormulaParser(CString strFormula, std::vector<CString>& vecStrElementName, std::vector<double>& vecDElementCount)
  497. {
  498. int i = 0;
  499. CString strCryWater = _T("");
  500. strFormula.Trim();
  501. // first delete the formula's "+", for example change formula "Fe2+4Fe3+8Sb12O23S2" to "Fe4Fe8Sb12O23S2"
  502. strFormula.Replace(_T("2+"), _T(""));
  503. strFormula.Replace(_T("3+"), _T(""));
  504. strFormula.Replace(_T("4+"), _T(""));
  505. strFormula.Replace(_T("5+"), _T(""));
  506. strFormula.Replace(_T("?"), _T("·")); // when formula has "?",replace "·"
  507. strFormula.Replace(_T("¡¤"), _T("·"));// when formula's "¡¤" replace "·", it can be added "Â"
  508. strFormula.Replace(_T("Â"), _T("")); // so replace "Â" to ""
  509. // second delete crystal water, for example change formula "KAl(SO4)2·12H2O" to "KAl(SO4)2"
  510. if (strFormula.Find(_T("·")) > 0)
  511. {
  512. strCryWater = strFormula.Right(strFormula.GetLength() - strFormula.Find(_T("·")) - 1);
  513. strCryWater.Replace(_T("("), _T(""));
  514. strCryWater.Replace(_T(")"), _T(""));
  515. for (i = 0; i < strCryWater.GetLength(); i++)
  516. {
  517. if (('.' == strCryWater.GetAt(i)) || (strCryWater.GetAt(i) >= '0' && strCryWater.GetAt(i) <= '9'))
  518. {
  519. i++;
  520. }
  521. else
  522. {
  523. break;
  524. }
  525. }
  526. if (0 == i)
  527. {
  528. strCryWater = GetSubFormulaWithoutSymbol(_T("H2O"), 1);
  529. }
  530. else
  531. {
  532. strCryWater = GetSubFormulaWithoutSymbol(_T("H2O"), _tstof(strCryWater.Mid(0, i)));
  533. }
  534. strFormula = strFormula.Left(strFormula.Find(_T("·")));
  535. }
  536. struct STACK
  537. {
  538. int iSymbolIndex[10]; // Symbol at most 10
  539. int iTop;
  540. STACK()
  541. {
  542. memset(&iSymbolIndex, 0, 10 * sizeof(int));
  543. iTop = -1;
  544. }
  545. };
  546. STACK stack;
  547. TCHAR sz;
  548. CString strSubFormulaWithSymbol;
  549. CString strSubFormulaWithoutSymbol;
  550. int iLeftIndex = 0;
  551. int iDigitalCount = 0;
  552. i = 0;
  553. // delete "()", "[]", "{}" from strFormula
  554. while (1)
  555. {
  556. if (i >= strFormula.GetLength())
  557. {
  558. break;
  559. }
  560. sz = strFormula.GetAt(i);
  561. // sz is left symbol
  562. if ('(' == sz || '[' == sz || '{' == sz)
  563. {
  564. // save the symbol to stack
  565. stack.iTop = stack.iTop + 1;
  566. ASSERT(stack.iTop < 10);
  567. stack.iSymbolIndex[stack.iTop] = i;
  568. }
  569. // sz is right symbol
  570. else if (')' == sz || ']' == sz || '}' == sz)
  571. {
  572. double dRatio = GetRatioAfterRightSymbol(strFormula, i, iDigitalCount);
  573. if (dRatio <= 0 || stack.iTop < 0)
  574. {
  575. return -1;
  576. }
  577. strSubFormulaWithSymbol = strFormula.Mid(stack.iSymbolIndex[stack.iTop] + 1, i - stack.iSymbolIndex[stack.iTop] - 1);
  578. // strSubFormulaWithoutSymbol = GetSubFormulaWithoutSymbol(strSubFormulaWithSymbol, dRatio);
  579. strSubFormulaWithoutSymbol = (1.0 == dRatio) ? strSubFormulaWithSymbol : GetSubFormulaWithoutSymbol(strSubFormulaWithSymbol, dRatio);
  580. iLeftIndex = strFormula.GetLength() - i - 1;
  581. strFormula = strFormula.Left(stack.iSymbolIndex[stack.iTop]) + strSubFormulaWithoutSymbol + strFormula.Right(strFormula.GetLength() - i - iDigitalCount - 1);
  582. i = strFormula.GetLength() - iLeftIndex - 1;
  583. stack.iTop = stack.iTop - 1;
  584. }
  585. i++;
  586. }
  587. // the formula (have deleted symbol) add crywater
  588. strFormula = strFormula + strCryWater;
  589. return DoFormulaParser_ext(strFormula, vecStrElementName, vecDElementCount);
  590. }
  591. int CStandardItem::DoFormulaParser_ext(CString strFormula, std::vector<CString>& vecStrElementName, std::vector<double>& vecDElementCount)
  592. {
  593. CString strElementName;
  594. double dElementCount = 0;
  595. int iIndex = 0;
  596. int iDigitCount = 0;
  597. vecStrElementName.clear();
  598. vecDElementCount.clear();
  599. ///////////////////////////////////////////
  600. // strFormula may be CH, CH2, CH2.34, C2H, C2H3, C2H2.33, C2.34H, C2.34H2, C2.34H2.22,
  601. // CaH, CaH2, CaH2.34, Ca2H, Ca2H3, Ca2H2.33, Ca2.34H, Ca2.34H2, Ca2.34H2.22,
  602. //////////////////////////////////////////
  603. while (1)
  604. {
  605. if (strFormula.GetLength() <= 0)
  606. {
  607. break;
  608. }
  609. iIndex = 0;
  610. iDigitCount = 0;
  611. if (strFormula.GetAt(0) >= 'A' && strFormula.GetAt(0) <= 'Z')
  612. {
  613. for (iIndex = 1; iIndex < strFormula.GetLength(); iIndex++)
  614. {
  615. if (('.' == strFormula.GetAt(iIndex)) || (strFormula.GetAt(iIndex) >= '0' && strFormula.GetAt(iIndex) <= '9'))
  616. {
  617. iDigitCount++;
  618. }
  619. else
  620. {
  621. break;
  622. }
  623. }
  624. // strFormula's elementname is C or Ca or Ca2 or Ca0.33 or CH
  625. if (1 == iIndex)
  626. {
  627. // it is only C
  628. if (1 == strFormula.GetLength())
  629. {
  630. strElementName = strFormula.Mid(0, 1);
  631. AddElementAndCount(vecStrElementName, vecDElementCount, strElementName, 1);
  632. break;
  633. }
  634. // the length() >= 2, strFormula's elementname is Ca or Ca2 or Ca0.33 or CH
  635. else
  636. {
  637. // Get the second char
  638. // the second char is a-z, so strFormula's elementname is Ca or Ca2 or Ca0.33
  639. if (strFormula.GetAt(1) >= 'a' && strFormula.GetAt(1) <= 'z')
  640. {
  641. for (iIndex = 2; iIndex < strFormula.GetLength(); iIndex++)
  642. {
  643. if (('.' == strFormula.GetAt(iIndex)) || (strFormula.GetAt(iIndex) >= '0' && strFormula.GetAt(iIndex) <= '9'))
  644. {
  645. iDigitCount++;
  646. }
  647. else
  648. {
  649. break;
  650. }
  651. }
  652. // the second char is a-z, so strFormula's elementname is Ca
  653. if (2 == iIndex)
  654. {
  655. strElementName = strFormula.Mid(0, 2);
  656. AddElementAndCount(vecStrElementName, vecDElementCount, strElementName, 1);
  657. // delete the elementname from strFormula
  658. strFormula = strFormula.Right(strFormula.GetLength() - 2);
  659. }
  660. else
  661. {
  662. if (0 == iDigitCount)
  663. {
  664. return -1;
  665. }
  666. // the second char is a-z, so strFormula's elementname is Ca2 or Ca0.33
  667. else
  668. {
  669. if (strFormula.GetAt(2) >= '0' && strFormula.GetAt(2) <= '9')
  670. {
  671. strElementName = strFormula.Mid(0, 2);
  672. dElementCount = _tstof(strFormula.Mid(2, iDigitCount));
  673. if (dElementCount <= 0)
  674. {
  675. return -1;
  676. }
  677. AddElementAndCount(vecStrElementName, vecDElementCount, strElementName, dElementCount);
  678. strFormula = strFormula.Right(strFormula.GetLength() - 2 - iDigitCount);
  679. }
  680. else
  681. {
  682. return -1;
  683. }
  684. }
  685. }
  686. }
  687. // the second char is A-Z, so strFormula's elementname is CH, Get C
  688. else if (strFormula.GetAt(1) >= 'A' && strFormula.GetAt(1) <= 'Z')
  689. {
  690. strElementName = strFormula.Mid(0, 1);
  691. AddElementAndCount(vecStrElementName, vecDElementCount, strElementName, 1);
  692. // delete the elementname from strFormula
  693. strFormula = strFormula.Right(strFormula.GetLength() - 1);
  694. }
  695. else
  696. {
  697. return -1;
  698. }
  699. }
  700. }
  701. // iIndex > 1
  702. else
  703. {
  704. if (0 == iDigitCount)
  705. {
  706. return -1;
  707. }
  708. // the length() >= 2, strFormula's elementname is C2 or C2.33
  709. else
  710. {
  711. // strFormula's elementname is C2 or C2.33
  712. if (strFormula.GetAt(1) >= '0' && strFormula.GetAt(1) <= '9')
  713. {
  714. strElementName = strFormula.Mid(0, 1);
  715. dElementCount = _tstof(strFormula.Mid(1, iDigitCount));
  716. if (dElementCount <= 0)
  717. {
  718. return -1;
  719. }
  720. AddElementAndCount(vecStrElementName, vecDElementCount, strElementName, dElementCount);
  721. strFormula = strFormula.Right(strFormula.GetLength() - 1 - iDigitCount);
  722. }
  723. else
  724. {
  725. return -1;
  726. }
  727. }
  728. }
  729. }
  730. else
  731. {
  732. return -1;
  733. }
  734. }
  735. return 0;
  736. }
  737. void CStandardItem::AddElementAndCount(std::vector<CString>& vecStrElementName, std::vector<double>& vecDElementCount, CString strElementName, double dElementCount)
  738. {
  739. size_t i = 0;
  740. for (i = 0; i < vecStrElementName.size(); i++)
  741. {
  742. if (0 == vecStrElementName[i].Compare(strElementName))
  743. {
  744. break;
  745. }
  746. }
  747. // the elementname has existed, only update elementcount
  748. if (i < vecStrElementName.size())
  749. {
  750. vecDElementCount[i] = vecDElementCount[i] + dElementCount;
  751. }
  752. // the elementname has not exist, add it
  753. else
  754. {
  755. vecStrElementName.push_back(strElementName);
  756. vecDElementCount.push_back(dElementCount);
  757. }
  758. }
  759. double CStandardItem::GetRatioAfterRightSymbol(CString strFormula, int iRightSymbolIndex, int& iDigitalCount)
  760. {
  761. CString strSubFormula = strFormula.Right(strFormula.GetLength() - iRightSymbolIndex - 1);
  762. TCHAR sz;
  763. sz = strSubFormula.GetAt(0);
  764. iDigitalCount = 0;
  765. // after formula' right symbol is 'A'-'Z'(1), '(', '[', '{'(1), '0'-'9'(??), other(-1,error)
  766. // sz is 0-9
  767. if (sz >= '0' && sz <= '9')
  768. {
  769. int i = 0;
  770. for (i = 0; i < strSubFormula.GetLength(); i++)
  771. {
  772. if (('.' == strSubFormula.GetAt(i)) || (strSubFormula.GetAt(i) >= '0' && strSubFormula.GetAt(i) <= '9'))
  773. {
  774. iDigitalCount++;
  775. }
  776. else
  777. {
  778. break;
  779. }
  780. }
  781. CString str = strSubFormula.Mid(0, iDigitalCount);
  782. return _tstof(str);
  783. }
  784. // sz is A-Z
  785. else if (sz >= 'A' && sz <= 'Z')
  786. {
  787. return 1;
  788. }
  789. // sz is '(', '[', '{'
  790. else if ('(' == sz || '[' == sz || '{' == sz || '·' == sz || ')' == sz || ']' == sz || '}' == sz)
  791. {
  792. return 1;
  793. }
  794. // the last of formula is ')', so return 1
  795. else if ('\0' == sz)
  796. {
  797. return 1;
  798. }
  799. // sz is other char
  800. else
  801. {
  802. return -1;
  803. }
  804. }
  805. CString CStandardItem::GetSubFormulaWithoutSymbol(CString strSubFormulaWithSymbol, double dRatio)
  806. {
  807. std::vector<CString> vecStrElementName;
  808. std::vector<double> vecDElementCount;
  809. if (DoFormulaParser_ext(strSubFormulaWithSymbol, vecStrElementName, vecDElementCount) < 0)
  810. {
  811. return _T("");
  812. }
  813. ASSERT(vecStrElementName.size() == vecDElementCount.size());
  814. CString strSubFormula;
  815. CString strSubFormulaWithoutSymbol = _T("");
  816. for (size_t i = 0; i < vecStrElementName.size(); i++)
  817. {
  818. strSubFormula.Format(_T("%s%.2f"), vecStrElementName[i], vecDElementCount[i] * dRatio);
  819. strSubFormulaWithoutSymbol = strSubFormulaWithoutSymbol + strSubFormula;
  820. }
  821. return strSubFormulaWithoutSymbol;
  822. }
  823. CString CStandardItem::GetPropName(int iStartPropid, int iPropid)
  824. {
  825. return MultiLang::GetInstance().GetCStringByKey(10,iStartPropid + iPropid);
  826. }
  827. CClassify::CClassify()
  828. {
  829. Init();
  830. }
  831. CClassify::CClassify(const CClassify& a_oSource)
  832. {
  833. // 自赋值
  834. if (this == &a_oSource)
  835. {
  836. return;
  837. }
  838. Duplicate(a_oSource);
  839. }
  840. CClassify::CClassify(CClassify* a_poSource)
  841. {
  842. if (!a_poSource)
  843. {
  844. return;
  845. }
  846. if (this == a_poSource)
  847. {
  848. return;
  849. }
  850. Duplicate(*a_poSource);
  851. }
  852. CClassify::~CClassify()
  853. {
  854. Cleanup();
  855. }
  856. CClassify& CClassify::operator=(const CClassify& a_oSource)
  857. {
  858. Duplicate(a_oSource);
  859. return *this;
  860. }
  861. BOOL CClassify::operator==(const CClassify& a_oSource)
  862. {
  863. return (m_strName == a_oSource.m_strName);
  864. }
  865. void CClassify::Serialize(bool isStoring, tinyxml2::XMLDocument * classDoc, tinyxml2::XMLElement * rootNode)
  866. {
  867. /*xmls::xInt xid;
  868. xmls::xString xstrname;
  869. xmls::xInt xicolor;
  870. xmls::xInt xiorder;
  871. xmls::Slo slo;
  872. slo.Register("classify", &xid);
  873. slo.Register("name", &xstrname);
  874. slo.Register("color", &xicolor);
  875. slo.Register("order", &xiorder);
  876. if (isStoring)
  877. {
  878. xid = m_iID;
  879. xstrname = m_strName;
  880. xicolor = m_iColor;
  881. xiorder = m_iOrder;
  882. slo.Serialize(true, classDoc, rootNode);
  883. }
  884. else
  885. {
  886. slo.Serialize(false, classDoc, rootNode);
  887. m_iID = xid.value();
  888. m_strName = xstrname.value().c_str();
  889. m_iColor = xicolor.value();
  890. m_iOrder = xiorder.value();
  891. }*/
  892. }
  893. void CClassify::SetID(int iID)
  894. {
  895. m_iID = iID;
  896. }
  897. int CClassify::GetID()
  898. {
  899. return m_iID;
  900. }
  901. void CClassify::SetName(CString strName)
  902. {
  903. m_strName = strName;
  904. }
  905. CString CClassify::GetName()
  906. {
  907. return m_strName;
  908. }
  909. void CClassify::SetColor(COLORREF iColor)
  910. {
  911. m_iColor = iColor;
  912. }
  913. COLORREF CClassify::GetColor()
  914. {
  915. return m_iColor;
  916. }
  917. void CClassify::SetOrder(int iOrder)
  918. {
  919. m_iOrder = iOrder;
  920. }
  921. int CClassify::GetOrder()
  922. {
  923. return m_iOrder;
  924. }
  925. void CClassify::Init()
  926. {
  927. m_iID = -1;
  928. m_strName = _T("");
  929. m_iColor = RGB(255, 255, 255);
  930. m_iOrder = -1;
  931. }
  932. void CClassify::Cleanup()
  933. {
  934. }
  935. void CClassify::Duplicate(const CClassify& a_oSource)
  936. {
  937. m_iID = a_oSource.m_iID;
  938. m_strName = a_oSource.m_strName;
  939. m_iColor = a_oSource.m_iColor;
  940. m_iOrder = a_oSource.m_iOrder;
  941. }
  942. CSTDLib::CSTDLib()
  943. {
  944. // initialization
  945. Init();
  946. }
  947. CSTDLib::CSTDLib(const CSTDLib& a_oSource)
  948. {
  949. if (this == &a_oSource)
  950. {
  951. return;
  952. }
  953. Duplicate(a_oSource);
  954. }
  955. CSTDLib::CSTDLib(CSTDLib* a_poSource)
  956. {
  957. if (!a_poSource)
  958. {
  959. return;
  960. }
  961. if (this == a_poSource)
  962. {
  963. return;
  964. }
  965. Duplicate(*a_poSource);
  966. }
  967. CSTDLib::~CSTDLib()
  968. {
  969. Cleanup();
  970. }
  971. void CSTDLib::Serialize(bool isStoring, tinyxml2::XMLDocument * classDoc, tinyxml2::XMLElement * rootNode)
  972. {
  973. /*int i = 0;
  974. xmls::xString xstrname;
  975. xmls::Collection<CStandardItem> xSTDItemList;
  976. xmls::Collection<CClassify> xClassifyList;
  977. xmls::Slo slo;
  978. slo.Register("name", &xstrname);
  979. slo.Register("stdminerals", &xSTDItemList);
  980. slo.Register("classify", &xClassifyList);
  981. if (isStoring)
  982. {
  983. xstrname = m_strName;
  984. xSTDItemList.Clear();
  985. xClassifyList.Clear();
  986. for (i = 0; i < (int)m_listSTDItems.size(); i++)
  987. {
  988. xSTDItemList.addItem(m_listSTDItems[i].get());
  989. }
  990. for (i = 0; i < (int)m_listClassify.size(); i++)
  991. {
  992. xClassifyList.addItem(m_listClassify[i].get());
  993. }
  994. slo.Serialize(true, classDoc, rootNode);
  995. }
  996. else
  997. {
  998. slo.Serialize(false, classDoc, rootNode);
  999. m_strName = xstrname.value().c_str();
  1000. m_listSTDItems.clear();
  1001. m_listClassify.clear();
  1002. for (i = 0; i < (int)xSTDItemList.size(); i++)
  1003. {
  1004. m_listSTDItems.push_back(CStandardItemPtr(xSTDItemList.getItem(i)));
  1005. }
  1006. for (i = 0; i < (int)xClassifyList.size(); i++)
  1007. {
  1008. m_listClassify.push_back(CClassifyPtr(xClassifyList.getItem(i)));
  1009. }
  1010. }*/
  1011. }
  1012. CSTDLib& CSTDLib::operator= (const CSTDLib& a_oSource)
  1013. {
  1014. Cleanup();
  1015. Duplicate(a_oSource);
  1016. return *this;
  1017. }
  1018. BOOL CSTDLib::operator== (const CSTDLib& a_oSource)
  1019. {
  1020. return (m_strName == a_oSource.m_strName &&
  1021. m_listSTDItems.size() == a_oSource.m_listSTDItems.size() &&
  1022. m_listClassify.size() == a_oSource.m_listClassify.size());
  1023. }
  1024. void CSTDLib::SetName(CString strName)
  1025. {
  1026. m_strName = strName;
  1027. }
  1028. CString CSTDLib::GetName()
  1029. {
  1030. return m_strName;
  1031. }
  1032. bool CSTDLib::AddItem(CStandardItem stdMineral)
  1033. {
  1034. int i = 0;
  1035. for (i = 0; i < (int)m_listSTDItems.size(); i++)
  1036. {
  1037. if (*(m_listSTDItems[i]) == stdMineral)
  1038. {
  1039. break;
  1040. }
  1041. }
  1042. // 不存在就添加
  1043. if (i >=(int) m_listSTDItems.size())
  1044. {
  1045. CStandardItemPtr pSTDMineral(new CStandardItem(stdMineral));
  1046. m_listSTDItems.push_back(pSTDMineral);
  1047. return true;
  1048. }
  1049. return false;
  1050. }
  1051. // 从0开始数, 在第iIndex位置之前插入一个记录
  1052. bool CSTDLib::InsertItem(CStandardItem stdMineral, int iIndex)
  1053. {
  1054. if (iIndex >(int) m_listSTDItems.size() || iIndex < 0)
  1055. {
  1056. return false;
  1057. }
  1058. int i = 0;
  1059. for (i = 0; i <(int) m_listSTDItems.size(); i++)
  1060. {
  1061. if (*(m_listSTDItems[i]) == stdMineral)
  1062. {
  1063. break;
  1064. }
  1065. }
  1066. // 不存在就插入
  1067. if (i >= (int)m_listSTDItems.size())
  1068. {
  1069. CStandardItemPtr pSTDMineral(new CStandardItem(stdMineral));
  1070. m_listSTDItems.insert(m_listSTDItems.begin() + iIndex, pSTDMineral);
  1071. return true;
  1072. }
  1073. return false;
  1074. }
  1075. void CSTDLib::RemoveItem(const CStandardItem& stdMineral)
  1076. {
  1077. std::vector<CStandardItemPtr>::iterator it;
  1078. for (it = m_listSTDItems.begin(); it < m_listSTDItems.end(); it++)
  1079. {
  1080. if (*((*it).get()) == stdMineral)
  1081. {
  1082. m_listSTDItems.erase(it);
  1083. break;
  1084. }
  1085. }
  1086. }
  1087. CStandardItemPtr CSTDLib::GetSTDItem(int iIndex)
  1088. {
  1089. if (iIndex < 0 || iIndex >= GetSTDItemCount())
  1090. {
  1091. return nullptr;
  1092. }
  1093. return m_listSTDItems[iIndex];
  1094. }
  1095. void CSTDLib::SwapItem(CStandardItem* pstdMineral1, CStandardItem* pstdMineral2)
  1096. {
  1097. if (NULL == pstdMineral1 || NULL == pstdMineral2)
  1098. {
  1099. return;
  1100. }
  1101. // 交换pstdMineral1和pstdMineral2的ID即可
  1102. int id1 = pstdMineral1->GetID();
  1103. int id2 = pstdMineral2->GetID();
  1104. pstdMineral1->SetID(id2);
  1105. pstdMineral2->SetID(id1);
  1106. // 此时m_listSTDMinerals里面的数据顺序已经发生了改变(原来设计按照ID从小到大的顺序排列)
  1107. // 此时要重新排序
  1108. std::sort(m_listSTDItems.begin(), m_listSTDItems.end(), [&](const CStandardItemPtr& ptr1, const CStandardItemPtr& ptr2)-> int
  1109. {
  1110. return ptr1->GetID() < ptr2->GetID();
  1111. });
  1112. }
  1113. void CSTDLib::SortByItemID(BOOL bAsc/* = TRUE*/)
  1114. {
  1115. if (bAsc)
  1116. {
  1117. std::sort(m_listSTDItems.begin(), m_listSTDItems.end(), [&](const CStandardItemPtr& ptr1, const CStandardItemPtr& ptr2)-> int
  1118. {
  1119. return ptr1->GetID() < ptr2->GetID();
  1120. });
  1121. }
  1122. else
  1123. {
  1124. std::sort(m_listSTDItems.begin(), m_listSTDItems.end(), [&](const CStandardItemPtr& ptr1, const CStandardItemPtr& ptr2)-> int
  1125. {
  1126. return ptr1->GetID() > ptr2->GetID();
  1127. });
  1128. }
  1129. }
  1130. int CSTDLib::GetSTDItemCount()
  1131. {
  1132. return m_listSTDItems.size();
  1133. }
  1134. void CSTDLib::Clear()
  1135. {
  1136. for (int i = 0; i < (int)m_listSTDItems.size(); i++)
  1137. {
  1138. m_listSTDItems[i]->Clear();
  1139. }
  1140. // m_strName = _T("");
  1141. m_listSTDItems.clear();
  1142. m_listClassify.clear();
  1143. }
  1144. CString CSTDLib::GetValidItemName(CString strFormat)
  1145. {
  1146. int i = 1;
  1147. CString strName = _T("");
  1148. strName.Format(_T("%s%d"), strFormat, i);
  1149. for (int j = 0; j < (int)m_listSTDItems.size(); j++)
  1150. {
  1151. if (0 == strName.CompareNoCase(m_listSTDItems[j]->GetName()))
  1152. {
  1153. strName.Format(_T("%s%d"), strFormat, ++i);
  1154. j = -1; // 不可以为0因为会自动j++
  1155. }
  1156. }
  1157. return strName;
  1158. }
  1159. CString CSTDLib::GetDifferItemName(CString strMineralName)
  1160. {
  1161. int iCount = 1;
  1162. CString strNewName = strMineralName;
  1163. while (HasSameItemName(strNewName))
  1164. {
  1165. strNewName.Format(_T("%s_%d"), strMineralName, iCount++);
  1166. }
  1167. return strNewName;
  1168. }
  1169. bool CSTDLib::AddClassify(CClassify classify)
  1170. {
  1171. int i = 0;
  1172. for (i = 0; i < (int)m_listClassify.size(); i++)
  1173. {
  1174. if (*(m_listClassify[i]) == classify)
  1175. {
  1176. break;
  1177. }
  1178. }
  1179. // 不存在就添加
  1180. if (i >= (int)m_listClassify.size())
  1181. {
  1182. CClassifyPtr pClassify(new CClassify(classify));
  1183. m_listClassify.push_back(pClassify);
  1184. return true;
  1185. }
  1186. return false;
  1187. }
  1188. void CSTDLib::RemoveClassify(const CClassify& classify)
  1189. {
  1190. std::vector<CClassifyPtr>::iterator it;
  1191. for (it = m_listClassify.begin(); it < m_listClassify.end(); it++)
  1192. {
  1193. if (*((*it).get()) == classify)
  1194. {
  1195. m_listClassify.erase(it);
  1196. break;
  1197. }
  1198. }
  1199. }
  1200. int CSTDLib::GetClassifyCount()
  1201. {
  1202. return m_listClassify.size();
  1203. }
  1204. CString CSTDLib::GetClassifyName(int iClassify)
  1205. {
  1206. for (int i = 0; i < (int)m_listClassify.size(); i++)
  1207. {
  1208. if (iClassify == m_listClassify[i]->GetID())
  1209. {
  1210. return m_listClassify[i]->GetName();
  1211. }
  1212. }
  1213. return _T("");
  1214. }
  1215. void CSTDLib::GetClassifyNames(std::vector<CString>& vecstrnames)
  1216. {
  1217. vecstrnames.clear();
  1218. vecstrnames.push_back(_T(""));
  1219. for (int i = 0; i < (int)m_listClassify.size(); i++)
  1220. {
  1221. vecstrnames.push_back(m_listClassify[i]->GetName());
  1222. }
  1223. }
  1224. CString CSTDLib::GetDifferClassifyName(CString strClassifyName)
  1225. {
  1226. int iCount = 1;
  1227. CString strNewName = strClassifyName;
  1228. while (HasSameClassifyName(strNewName))
  1229. {
  1230. strNewName.Format(_T("%s_%d"), strClassifyName, iCount++);
  1231. }
  1232. return strNewName;
  1233. }
  1234. CClassifyPtr CSTDLib::GetClassifyPtr(int iIndex)
  1235. {
  1236. if (iIndex < 0 || iIndex >= GetClassifyCount())
  1237. {
  1238. return nullptr;
  1239. }
  1240. return m_listClassify[iIndex];
  1241. }
  1242. CClassifyPtr CSTDLib::GetClassifyPtr(CString strClassifyName)
  1243. {
  1244. for (int i = 0; i < GetClassifyCount(); i++)
  1245. {
  1246. if (strClassifyName == m_listClassify[i]->GetName())
  1247. {
  1248. return m_listClassify[i];
  1249. }
  1250. }
  1251. return nullptr;
  1252. }
  1253. void CSTDLib::SwapClassifyOrder(CClassify* pclassify1, CClassify* pclassify2)
  1254. {
  1255. if (NULL == pclassify1 || NULL == pclassify2)
  1256. {
  1257. return;
  1258. }
  1259. // 交换pstdMineral1和pstdMineral2的ID即可
  1260. int id1 = pclassify1->GetOrder();
  1261. int id2 = pclassify2->GetOrder();
  1262. pclassify1->SetOrder(id2);
  1263. pclassify2->SetOrder(id1);
  1264. // 此时m_listClassify里面的数据顺序已经发生了改变(原来设计按照ID从小到大的顺序排列)
  1265. // 此时要重新排序
  1266. std::sort(m_listClassify.begin(), m_listClassify.end(), [&](const CClassifyPtr& ptr1, const CClassifyPtr& ptr2)-> int
  1267. {
  1268. return ptr1->GetOrder() < ptr2->GetOrder();
  1269. });
  1270. }
  1271. void CSTDLib::LoadStrFromXml()
  1272. {
  1273. MultiLang::GetInstance().LoadStringFromXml();
  1274. }
  1275. CString CSTDLib::GetSTDLibPropName(int iPropid)
  1276. {
  1277. // AFX_MANAGE_STATE(AfxGetStaticModuleState());
  1278. CString str = _T("");
  1279. // str.LoadString(IDS_ORETYPE_PROP + iPropid);
  1280. str = MultiLang::GetInstance().GetCStringByKey(10,IDS_ORETYPE_PROP + iPropid);
  1281. return str;
  1282. }
  1283. bool CSTDLib::HasSameItemName(CString strMineralName)
  1284. {
  1285. for (int i = 0; i < (int)m_listSTDItems.size(); i++)
  1286. {
  1287. if (0 == strMineralName.CompareNoCase(m_listSTDItems[i]->GetName()))
  1288. {
  1289. return true;
  1290. }
  1291. }
  1292. return false;
  1293. }
  1294. bool CSTDLib::HasSameClassifyName(CString strClassifyName)
  1295. {
  1296. for (int i = 0; i < (int)m_listClassify.size(); i++)
  1297. {
  1298. if (0 == strClassifyName.CompareNoCase(m_listClassify[i]->GetName()))
  1299. {
  1300. return true;
  1301. }
  1302. }
  1303. return false;
  1304. }
  1305. // cleanup
  1306. void CSTDLib::Cleanup()
  1307. {
  1308. }
  1309. // initialization
  1310. void CSTDLib::Init()
  1311. {
  1312. m_strName = _T("");
  1313. m_listSTDItems.clear();
  1314. }
  1315. // duplication
  1316. void CSTDLib::Duplicate(const CSTDLib& a_oSource)
  1317. {
  1318. Init();
  1319. m_strName = a_oSource.m_strName;
  1320. for (int i = 0; i < (int)a_oSource.m_listSTDItems.size(); i++)
  1321. {
  1322. m_listSTDItems.push_back(a_oSource.m_listSTDItems[i]);
  1323. }
  1324. }
  1325. }