// describe elements #include "stdafx.h" #include "LanguageID.h" #include "MultiLang.h" #include "OTSSTDLib.h" #include namespace OTSClassifyEngine { // CSTDBase // constructor CSTDBase::CSTDBase() { // initialization Init(); } CSTDBase::CSTDBase(const CSTDBase& a_oSource) { if (this == &a_oSource) { return; } Duplicate(a_oSource); } CSTDBase::CSTDBase(CSTDBase* a_poSource) { if (!a_poSource) { return; } if (this == a_poSource) { return; } Duplicate(*a_poSource); } CSTDBase::~CSTDBase() { Cleanup(); } // =operator CSTDBase& CSTDBase::operator=(const CSTDBase& a_oSource) { Cleanup(); Duplicate(a_oSource); return *this; } // ==operator BOOL CSTDBase::operator==(const CSTDBase& a_oSource) { return (m_iID == a_oSource.m_iID && m_strName == a_oSource.m_strName && m_strFormula == a_oSource.m_strFormula); } void CSTDBase::Serialize(bool isStoring, tinyxml2::XMLDocument * classDoc, tinyxml2::XMLElement * rootNode) { /*CPosXray::Serialize(isStoring, classDoc, rootNode); xmls::xInt xid; xmls::xString xstrname; xmls::xString xstrformula; xmls::xDouble xddensity; xmls::xDouble xdaverz; xmls::xDouble xdbulkmod; xmls::xDouble xdrigiditymod; xmls::xInt xibrpl; xmls::xInt xikind; xmls::Slo slo; slo.Register("id", &xid); slo.Register("name", &xstrname); slo.Register("formula", &xstrformula); slo.Register("density", &xddensity); slo.Register("averz", &xdaverz); if (isStoring) { xid = m_iID; xstrname = m_strName; xstrformula = m_strFormula; xddensity = m_dDensity; xdaverz = m_dAverz; slo.Serialize(true, classDoc, rootNode); } else { int iBRPL = 0; int iKind = 0; slo.Serialize(false, classDoc, rootNode); m_iID = xid.value(); m_strName = xstrname.value().c_str(); m_strFormula = xstrformula.value().c_str(); m_dDensity = xddensity.value(); m_dAverz = xdaverz.value(); }*/ } void CSTDBase::SetID(int iID) { m_iID = iID; } int CSTDBase::GetID() { return m_iID; } void CSTDBase::SetName(CString strName) { m_strName = strName; } CString CSTDBase::GetName() { return m_strName; } void CSTDBase::SetFormula(CString strFormula) { m_strFormula = strFormula; } CString CSTDBase::GetFormula() { return m_strFormula; } void CSTDBase::SetDensity(std::string dDensity) { m_dDensity = dDensity; } std::string CSTDBase::GetDensity() { return m_dDensity; } void CSTDBase::SetAvAltomicWeight(double dAvAltomicWeight) { m_dAverz = dAvAltomicWeight; } double CSTDBase::GetAvAltomicWeight() { return m_dAverz; } void CSTDBase::SetBulkmod(double dBulkmod) { m_dBulkmod = dBulkmod; } double CSTDBase::GetBulkmod() { return m_dBulkmod; } void CSTDBase::SetRigiditymod(double dRigiditymod) { m_dRigiditymod = dRigiditymod; } double CSTDBase::GetRigiditymod() { return m_dRigiditymod; } void CSTDBase::SetMineralBRPL(int iBRPL) { m_iBRPL = (MINERAL_BRPL)iBRPL; } void CSTDBase::SetMineralBRPL(CString strBRPL) { std::vector vecstrnames; GetBRPLNames(vecstrnames); m_iBRPL = MINERAL_BRPL::MINERAL_BR; for (int i = 0; i < (int)vecstrnames.size(); i++) { if (strBRPL == vecstrnames[i]) { m_iBRPL = (MINERAL_BRPL)i; return; } } } int CSTDBase::GetMineralBRPL() { return (int)m_iBRPL; } CString CSTDBase::GetMineralBRPLName() { // AFX_MANAGE_STATE(AfxGetStaticModuleState()); CString str = _T(""); // str.LoadString(IDS_MINERAL_BRPL + (int)m_iBRPL); str = MultiLang::GetInstance().GetCStringByKey(10,IDS_MINERAL_BRPL + (int)m_iBRPL); return str; } void CSTDBase::GetBRPLNames(std::vector& vecstrnames) { // AFX_MANAGE_STATE(AfxGetStaticModuleState()); CString str = _T(""); vecstrnames.clear(); for (int i = 0; i < (int)MINERAL_BRPL::MAX; i++) { // str.LoadString(IDS_MINERAL_BRPL + i); str = MultiLang::GetInstance().GetCStringByKey(10,IDS_MINERAL_BRPL + i); vecstrnames.push_back(str); } } void CSTDBase::SetMineralKind(int iKind) { m_iKind = (MINERAL_KINDS)iKind; } void CSTDBase::SetMineralKind(CString strKind) { std::vector vecstrnames; GetKindNames(vecstrnames); m_iKind = MINERAL_KINDS::MINERAL_SI; for (int i = 0; i < (int)vecstrnames.size(); i++) { if (strKind == vecstrnames[i]) { m_iKind = (MINERAL_KINDS)i; return; } } } int CSTDBase::GetMineralKind() { return (int)m_iKind; } CString CSTDBase::GetMineralKindName() { // AFX_MANAGE_STATE(AfxGetStaticModuleState()); CString str = _T(""); // str.LoadString(IDS_MINERAL_KINDS + (int)m_iKind); str = MultiLang::GetInstance().GetCStringByKey(10,IDS_MINERAL_KINDS + (int)m_iKind); return str; } void CSTDBase::GetKindNames(std::vector& vecstrnames) { // AFX_MANAGE_STATE(AfxGetStaticModuleState()); CString str = _T(""); vecstrnames.clear(); for (int i = 0; i < (int)MINERAL_KINDS::MAX; i++) { // str.LoadString(IDS_MINERAL_KINDS + i); str = MultiLang::GetInstance().GetCStringByKey(10,IDS_MINERAL_KINDS + i); vecstrnames.push_back(str); } } void CSTDBase::SetElementsList(const CElementsList& elementlist) { // m_listElements = elementlist; m_listElements.clear(); for (int i = 0; i <(int) elementlist.size(); i++) { m_listElements.push_back(elementlist[i]); } } const CElementsList& CSTDBase::GetElementsList() { return m_listElements; } void CSTDBase::AddElement(const CElement& element) { // 判断是否存在 int i = 0; for (i = 0; i < (int)m_listElements.size(); i++) { if ((*m_listElements[i]) == element) { break; } } // 不存在就添加 if (i >= (int)m_listElements.size()) { CElementPtr pMineral(new CElement(element)); m_listElements.push_back(pMineral); } } void CSTDBase::RemoveElement(const CElement& element) { std::vector::iterator it; for (it = m_listElements.begin(); it < m_listElements.end(); it++) { if (*((*it).get()) == element) { m_listElements.erase(it); break; } } } void CSTDBase::RemoveAllElements() { m_listElements.clear(); } CElementPtr CSTDBase::GetElement(int iIndex) { if (iIndex < 0 || iIndex >= GetElementCount()) { return nullptr; } return m_listElements[iIndex]; } int CSTDBase::GetElementCount() { return m_listElements.size(); } void CSTDBase::Clear() { m_listElements.clear(); } // cleanup void CSTDBase::Cleanup() { // nothing needs to be done at the moment } // initialization void CSTDBase::Init() { m_iID = -1; m_strName = _T(""); m_strFormula = _T("-"); m_dDensity = DENSITY_DEFAULT; m_dAverz = AVALTOMICWEIGHT_MIN; m_listElements.clear(); } // duplication void CSTDBase::Duplicate(const CSTDBase& a_oSource) { Init(); m_iID = a_oSource.m_iID; m_strName = a_oSource.m_strName; m_strFormula = a_oSource.m_strFormula; m_dDensity = a_oSource.m_dDensity; m_dAverz = a_oSource.m_dAverz; for (auto poElement : a_oSource.m_listElements) { m_listElements.push_back(poElement); } } CStandardItem::CStandardItem() { // initialization Init(); } CStandardItem::CStandardItem(const CStandardItem& a_oSource) { if (this == &a_oSource) { return; } Duplicate(a_oSource); } CStandardItem::CStandardItem(CStandardItem* a_poSource) { if (!a_poSource) { return; } if (this == a_poSource) { return; } Duplicate(*a_poSource); } CStandardItem::~CStandardItem() { Cleanup(); } CStandardItem& CStandardItem::operator=(const CStandardItem& a_oSource) { Cleanup(); Duplicate(a_oSource); return *this; } BOOL CStandardItem::operator==(const CStandardItem& a_oSource) { return CSTDBase::operator==(a_oSource); } void CStandardItem::Serialize(bool isStoring, tinyxml2::XMLDocument * classDoc, tinyxml2::XMLElement * rootNode) { CSTDBase::Serialize(isStoring, classDoc, rootNode); int i = 0; xmls::xInt xicolor; xmls::xInt xibse; xmls::xInt xiclassify; xmls::Collection xElementList; xmls::Slo slo; slo.Register("color", &xicolor); slo.Register("bse", &xibse); slo.Register("classify", &xiclassify); slo.Register("elements", &xElementList); if (isStoring) { xicolor = m_iColor; xibse = m_iBSEvalue; xiclassify = m_iClassify; xElementList.Clear(); for (i = 0; i < (int)m_listElements.size(); i++) { xElementList.addItem(m_listElements[i].get()); } slo.Serialize(true, classDoc, rootNode); } else { slo.Serialize(false, classDoc, rootNode); m_iColor = xicolor.value(); m_iBSEvalue = xibse.value(); m_iClassify = xiclassify.value(); m_listElements.clear(); for (i = 0; i < (int)xElementList.size(); i++) { m_listElements.push_back(CElementPtr(xElementList.getItem(i))); } } } void CStandardItem::SetColor(COLORREF iColor) { m_iColor = iColor; } COLORREF CStandardItem::GetColor() { return m_iColor; } void CStandardItem::SetBSEValue(int iBSEvalue) { m_iBSEvalue = iBSEvalue; } int CStandardItem::GetBSEValue() { return m_iBSEvalue; } void CStandardItem::SetClassify(int iClassify) { m_iClassify = iClassify; } int CStandardItem::GetClassify() { return m_iClassify; } BOOL CStandardItem::DoFormulaParser() { std::vector vecStrElementName; std::vector vecDElementCount; if (DoFormulaParser(m_strFormula, vecStrElementName, vecDElementCount) < 0) { return false; } // vecStrElementName存放了m_strFormula里面的每个元素, vecDElementCount存放了每个元素的个数 ASSERT(vecStrElementName.size() == vecDElementCount.size()); RemoveAllElements(); for (auto strName : vecStrElementName) { CElementPtr* pElementPtr = new CElementPtr(new CElement(strName)); AddElement(*pElementPtr->get()); } ASSERT(m_listElements.size() == vecDElementCount.size()); double dAllAtomicNumber = 0; double dAllElementCount = 0; double dAllAutomicWeight = 0; double dwAvAltomicWeight = 0; for (int i = 0; i <(int) m_listElements.size(); i++) { dAllAtomicNumber += m_listElements[i]->GetAtomNum() * vecDElementCount[i]; dAllElementCount += vecDElementCount[i]; dAllAutomicWeight += m_listElements[i]->GetAtomWeight() * vecDElementCount[i]; } dwAvAltomicWeight = (0 == dAllElementCount) ? dAllAtomicNumber : dAllAtomicNumber / dAllElementCount; // check the data is valid or not if (dAllElementCount <= 0 || dwAvAltomicWeight < AVALTOMICWEIGHT_MIN || dwAvAltomicWeight > AVALTOMICWEIGHT_MAX) { return FALSE; } SetAvAltomicWeight(dwAvAltomicWeight); // calc every element's percentage of mineralstd for (int i = 0; i < (int)m_listElements.size(); i++) { m_listElements[i]->SetPercentage(100 * m_listElements[i]->GetAtomWeight() * vecDElementCount[i] / dAllAutomicWeight); } return TRUE; } CString CStandardItem::GetSTDMineralPropName(int iPropid) { return GetPropName(IDS_MINERAL_PROP, iPropid); } CString CStandardItem::GetElementPropName(int iPropid) { return GetPropName(IDS_ELEMENT_PROP, iPropid); } void CStandardItem::Cleanup() { } // initialization void CStandardItem::Init() { CSTDBase::Init(); CPosXray::Init(); m_iColor = RGB(255, 255, 255); m_iBSEvalue = 0; m_iClassify = -1; } // duplication void CStandardItem::Duplicate(const CStandardItem& a_oSource) { CSTDBase::Duplicate(a_oSource); CPosXray::Duplicate(a_oSource); m_iColor = a_oSource.m_iColor; m_iBSEvalue = a_oSource.m_iBSEvalue; m_iClassify = a_oSource.m_iClassify; } int CStandardItem::DoFormulaParser(CString strFormula, std::vector& vecStrElementName, std::vector& vecDElementCount) { int i = 0; CString strCryWater = _T(""); strFormula.Trim(); // first delete the formula's "+", for example change formula "Fe2+4Fe3+8Sb12O23S2" to "Fe4Fe8Sb12O23S2" strFormula.Replace(_T("2+"), _T("")); strFormula.Replace(_T("3+"), _T("")); strFormula.Replace(_T("4+"), _T("")); strFormula.Replace(_T("5+"), _T("")); strFormula.Replace(_T("?"), _T("·")); // when formula has "?",replace "·" strFormula.Replace(_T("¡¤"), _T("·"));// when formula's "¡¤" replace "·", it can be added "Â" strFormula.Replace(_T("Â"), _T("")); // so replace "Â" to "" // second delete crystal water, for example change formula "KAl(SO4)2·12H2O" to "KAl(SO4)2" if (strFormula.Find(_T("·")) > 0) { strCryWater = strFormula.Right(strFormula.GetLength() - strFormula.Find(_T("·")) - 1); strCryWater.Replace(_T("("), _T("")); strCryWater.Replace(_T(")"), _T("")); for (i = 0; i < strCryWater.GetLength(); i++) { if (('.' == strCryWater.GetAt(i)) || (strCryWater.GetAt(i) >= '0' && strCryWater.GetAt(i) <= '9')) { i++; } else { break; } } if (0 == i) { strCryWater = GetSubFormulaWithoutSymbol(_T("H2O"), 1); } else { strCryWater = GetSubFormulaWithoutSymbol(_T("H2O"), _tstof(strCryWater.Mid(0, i))); } strFormula = strFormula.Left(strFormula.Find(_T("·"))); } struct STACK { int iSymbolIndex[10]; // Symbol at most 10 int iTop; STACK() { memset(&iSymbolIndex, 0, 10 * sizeof(int)); iTop = -1; } }; STACK stack; TCHAR sz; CString strSubFormulaWithSymbol; CString strSubFormulaWithoutSymbol; int iLeftIndex = 0; int iDigitalCount = 0; i = 0; // delete "()", "[]", "{}" from strFormula while (1) { if (i >= strFormula.GetLength()) { break; } sz = strFormula.GetAt(i); // sz is left symbol if ('(' == sz || '[' == sz || '{' == sz) { // save the symbol to stack stack.iTop = stack.iTop + 1; ASSERT(stack.iTop < 10); stack.iSymbolIndex[stack.iTop] = i; } // sz is right symbol else if (')' == sz || ']' == sz || '}' == sz) { double dRatio = GetRatioAfterRightSymbol(strFormula, i, iDigitalCount); if (dRatio <= 0 || stack.iTop < 0) { return -1; } strSubFormulaWithSymbol = strFormula.Mid(stack.iSymbolIndex[stack.iTop] + 1, i - stack.iSymbolIndex[stack.iTop] - 1); // strSubFormulaWithoutSymbol = GetSubFormulaWithoutSymbol(strSubFormulaWithSymbol, dRatio); strSubFormulaWithoutSymbol = (1.0 == dRatio) ? strSubFormulaWithSymbol : GetSubFormulaWithoutSymbol(strSubFormulaWithSymbol, dRatio); iLeftIndex = strFormula.GetLength() - i - 1; strFormula = strFormula.Left(stack.iSymbolIndex[stack.iTop]) + strSubFormulaWithoutSymbol + strFormula.Right(strFormula.GetLength() - i - iDigitalCount - 1); i = strFormula.GetLength() - iLeftIndex - 1; stack.iTop = stack.iTop - 1; } i++; } // the formula (have deleted symbol) add crywater strFormula = strFormula + strCryWater; return DoFormulaParser_ext(strFormula, vecStrElementName, vecDElementCount); } int CStandardItem::DoFormulaParser_ext(CString strFormula, std::vector& vecStrElementName, std::vector& vecDElementCount) { CString strElementName; double dElementCount = 0; int iIndex = 0; int iDigitCount = 0; vecStrElementName.clear(); vecDElementCount.clear(); /////////////////////////////////////////// // strFormula may be CH, CH2, CH2.34, C2H, C2H3, C2H2.33, C2.34H, C2.34H2, C2.34H2.22, // CaH, CaH2, CaH2.34, Ca2H, Ca2H3, Ca2H2.33, Ca2.34H, Ca2.34H2, Ca2.34H2.22, ////////////////////////////////////////// while (1) { if (strFormula.GetLength() <= 0) { break; } iIndex = 0; iDigitCount = 0; if (strFormula.GetAt(0) >= 'A' && strFormula.GetAt(0) <= 'Z') { for (iIndex = 1; iIndex < strFormula.GetLength(); iIndex++) { if (('.' == strFormula.GetAt(iIndex)) || (strFormula.GetAt(iIndex) >= '0' && strFormula.GetAt(iIndex) <= '9')) { iDigitCount++; } else { break; } } // strFormula's elementname is C or Ca or Ca2 or Ca0.33 or CH if (1 == iIndex) { // it is only C if (1 == strFormula.GetLength()) { strElementName = strFormula.Mid(0, 1); AddElementAndCount(vecStrElementName, vecDElementCount, strElementName, 1); break; } // the length() >= 2, strFormula's elementname is Ca or Ca2 or Ca0.33 or CH else { // Get the second char // the second char is a-z, so strFormula's elementname is Ca or Ca2 or Ca0.33 if (strFormula.GetAt(1) >= 'a' && strFormula.GetAt(1) <= 'z') { for (iIndex = 2; iIndex < strFormula.GetLength(); iIndex++) { if (('.' == strFormula.GetAt(iIndex)) || (strFormula.GetAt(iIndex) >= '0' && strFormula.GetAt(iIndex) <= '9')) { iDigitCount++; } else { break; } } // the second char is a-z, so strFormula's elementname is Ca if (2 == iIndex) { strElementName = strFormula.Mid(0, 2); AddElementAndCount(vecStrElementName, vecDElementCount, strElementName, 1); // delete the elementname from strFormula strFormula = strFormula.Right(strFormula.GetLength() - 2); } else { if (0 == iDigitCount) { return -1; } // the second char is a-z, so strFormula's elementname is Ca2 or Ca0.33 else { if (strFormula.GetAt(2) >= '0' && strFormula.GetAt(2) <= '9') { strElementName = strFormula.Mid(0, 2); dElementCount = _tstof(strFormula.Mid(2, iDigitCount)); if (dElementCount <= 0) { return -1; } AddElementAndCount(vecStrElementName, vecDElementCount, strElementName, dElementCount); strFormula = strFormula.Right(strFormula.GetLength() - 2 - iDigitCount); } else { return -1; } } } } // the second char is A-Z, so strFormula's elementname is CH, Get C else if (strFormula.GetAt(1) >= 'A' && strFormula.GetAt(1) <= 'Z') { strElementName = strFormula.Mid(0, 1); AddElementAndCount(vecStrElementName, vecDElementCount, strElementName, 1); // delete the elementname from strFormula strFormula = strFormula.Right(strFormula.GetLength() - 1); } else { return -1; } } } // iIndex > 1 else { if (0 == iDigitCount) { return -1; } // the length() >= 2, strFormula's elementname is C2 or C2.33 else { // strFormula's elementname is C2 or C2.33 if (strFormula.GetAt(1) >= '0' && strFormula.GetAt(1) <= '9') { strElementName = strFormula.Mid(0, 1); dElementCount = _tstof(strFormula.Mid(1, iDigitCount)); if (dElementCount <= 0) { return -1; } AddElementAndCount(vecStrElementName, vecDElementCount, strElementName, dElementCount); strFormula = strFormula.Right(strFormula.GetLength() - 1 - iDigitCount); } else { return -1; } } } } else { return -1; } } return 0; } void CStandardItem::AddElementAndCount(std::vector& vecStrElementName, std::vector& vecDElementCount, CString strElementName, double dElementCount) { size_t i = 0; for (i = 0; i < vecStrElementName.size(); i++) { if (0 == vecStrElementName[i].Compare(strElementName)) { break; } } // the elementname has existed, only update elementcount if (i < vecStrElementName.size()) { vecDElementCount[i] = vecDElementCount[i] + dElementCount; } // the elementname has not exist, add it else { vecStrElementName.push_back(strElementName); vecDElementCount.push_back(dElementCount); } } double CStandardItem::GetRatioAfterRightSymbol(CString strFormula, int iRightSymbolIndex, int& iDigitalCount) { CString strSubFormula = strFormula.Right(strFormula.GetLength() - iRightSymbolIndex - 1); TCHAR sz; sz = strSubFormula.GetAt(0); iDigitalCount = 0; // after formula' right symbol is 'A'-'Z'(1), '(', '[', '{'(1), '0'-'9'(??), other(-1,error) // sz is 0-9 if (sz >= '0' && sz <= '9') { int i = 0; for (i = 0; i < strSubFormula.GetLength(); i++) { if (('.' == strSubFormula.GetAt(i)) || (strSubFormula.GetAt(i) >= '0' && strSubFormula.GetAt(i) <= '9')) { iDigitalCount++; } else { break; } } CString str = strSubFormula.Mid(0, iDigitalCount); return _tstof(str); } // sz is A-Z else if (sz >= 'A' && sz <= 'Z') { return 1; } // sz is '(', '[', '{' else if ('(' == sz || '[' == sz || '{' == sz || '·' == sz || ')' == sz || ']' == sz || '}' == sz) { return 1; } // the last of formula is ')', so return 1 else if ('\0' == sz) { return 1; } // sz is other char else { return -1; } } CString CStandardItem::GetSubFormulaWithoutSymbol(CString strSubFormulaWithSymbol, double dRatio) { std::vector vecStrElementName; std::vector vecDElementCount; if (DoFormulaParser_ext(strSubFormulaWithSymbol, vecStrElementName, vecDElementCount) < 0) { return _T(""); } ASSERT(vecStrElementName.size() == vecDElementCount.size()); CString strSubFormula; CString strSubFormulaWithoutSymbol = _T(""); for (size_t i = 0; i < vecStrElementName.size(); i++) { strSubFormula.Format(_T("%s%.2f"), vecStrElementName[i], vecDElementCount[i] * dRatio); strSubFormulaWithoutSymbol = strSubFormulaWithoutSymbol + strSubFormula; } return strSubFormulaWithoutSymbol; } CString CStandardItem::GetPropName(int iStartPropid, int iPropid) { return MultiLang::GetInstance().GetCStringByKey(10,iStartPropid + iPropid); } CClassify::CClassify() { Init(); } CClassify::CClassify(const CClassify& a_oSource) { // 自赋值 if (this == &a_oSource) { return; } Duplicate(a_oSource); } CClassify::CClassify(CClassify* a_poSource) { if (!a_poSource) { return; } if (this == a_poSource) { return; } Duplicate(*a_poSource); } CClassify::~CClassify() { Cleanup(); } CClassify& CClassify::operator=(const CClassify& a_oSource) { Duplicate(a_oSource); return *this; } BOOL CClassify::operator==(const CClassify& a_oSource) { return (m_strName == a_oSource.m_strName); } void CClassify::Serialize(bool isStoring, tinyxml2::XMLDocument * classDoc, tinyxml2::XMLElement * rootNode) { /*xmls::xInt xid; xmls::xString xstrname; xmls::xInt xicolor; xmls::xInt xiorder; xmls::Slo slo; slo.Register("classify", &xid); slo.Register("name", &xstrname); slo.Register("color", &xicolor); slo.Register("order", &xiorder); if (isStoring) { xid = m_iID; xstrname = m_strName; xicolor = m_iColor; xiorder = m_iOrder; slo.Serialize(true, classDoc, rootNode); } else { slo.Serialize(false, classDoc, rootNode); m_iID = xid.value(); m_strName = xstrname.value().c_str(); m_iColor = xicolor.value(); m_iOrder = xiorder.value(); }*/ } void CClassify::SetID(int iID) { m_iID = iID; } int CClassify::GetID() { return m_iID; } void CClassify::SetName(CString strName) { m_strName = strName; } CString CClassify::GetName() { return m_strName; } void CClassify::SetColor(COLORREF iColor) { m_iColor = iColor; } COLORREF CClassify::GetColor() { return m_iColor; } void CClassify::SetOrder(int iOrder) { m_iOrder = iOrder; } int CClassify::GetOrder() { return m_iOrder; } void CClassify::Init() { m_iID = -1; m_strName = _T(""); m_iColor = RGB(255, 255, 255); m_iOrder = -1; } void CClassify::Cleanup() { } void CClassify::Duplicate(const CClassify& a_oSource) { m_iID = a_oSource.m_iID; m_strName = a_oSource.m_strName; m_iColor = a_oSource.m_iColor; m_iOrder = a_oSource.m_iOrder; } CSTDLib::CSTDLib() { // initialization Init(); } CSTDLib::CSTDLib(const CSTDLib& a_oSource) { if (this == &a_oSource) { return; } Duplicate(a_oSource); } CSTDLib::CSTDLib(CSTDLib* a_poSource) { if (!a_poSource) { return; } if (this == a_poSource) { return; } Duplicate(*a_poSource); } CSTDLib::~CSTDLib() { Cleanup(); } void CSTDLib::Serialize(bool isStoring, tinyxml2::XMLDocument * classDoc, tinyxml2::XMLElement * rootNode) { /*int i = 0; xmls::xString xstrname; xmls::Collection xSTDItemList; xmls::Collection xClassifyList; xmls::Slo slo; slo.Register("name", &xstrname); slo.Register("stdminerals", &xSTDItemList); slo.Register("classify", &xClassifyList); if (isStoring) { xstrname = m_strName; xSTDItemList.Clear(); xClassifyList.Clear(); for (i = 0; i < (int)m_listSTDItems.size(); i++) { xSTDItemList.addItem(m_listSTDItems[i].get()); } for (i = 0; i < (int)m_listClassify.size(); i++) { xClassifyList.addItem(m_listClassify[i].get()); } slo.Serialize(true, classDoc, rootNode); } else { slo.Serialize(false, classDoc, rootNode); m_strName = xstrname.value().c_str(); m_listSTDItems.clear(); m_listClassify.clear(); for (i = 0; i < (int)xSTDItemList.size(); i++) { m_listSTDItems.push_back(CStandardItemPtr(xSTDItemList.getItem(i))); } for (i = 0; i < (int)xClassifyList.size(); i++) { m_listClassify.push_back(CClassifyPtr(xClassifyList.getItem(i))); } }*/ } CSTDLib& CSTDLib::operator= (const CSTDLib& a_oSource) { Cleanup(); Duplicate(a_oSource); return *this; } BOOL CSTDLib::operator== (const CSTDLib& a_oSource) { return (m_strName == a_oSource.m_strName && m_listSTDItems.size() == a_oSource.m_listSTDItems.size() && m_listClassify.size() == a_oSource.m_listClassify.size()); } void CSTDLib::SetName(CString strName) { m_strName = strName; } CString CSTDLib::GetName() { return m_strName; } bool CSTDLib::AddItem(CStandardItem stdMineral) { int i = 0; for (i = 0; i < (int)m_listSTDItems.size(); i++) { if (*(m_listSTDItems[i]) == stdMineral) { break; } } // 不存在就添加 if (i >=(int) m_listSTDItems.size()) { CStandardItemPtr pSTDMineral(new CStandardItem(stdMineral)); m_listSTDItems.push_back(pSTDMineral); return true; } return false; } // 从0开始数, 在第iIndex位置之前插入一个记录 bool CSTDLib::InsertItem(CStandardItem stdMineral, int iIndex) { if (iIndex >(int) m_listSTDItems.size() || iIndex < 0) { return false; } int i = 0; for (i = 0; i <(int) m_listSTDItems.size(); i++) { if (*(m_listSTDItems[i]) == stdMineral) { break; } } // 不存在就插入 if (i >= (int)m_listSTDItems.size()) { CStandardItemPtr pSTDMineral(new CStandardItem(stdMineral)); m_listSTDItems.insert(m_listSTDItems.begin() + iIndex, pSTDMineral); return true; } return false; } void CSTDLib::RemoveItem(const CStandardItem& stdMineral) { std::vector::iterator it; for (it = m_listSTDItems.begin(); it < m_listSTDItems.end(); it++) { if (*((*it).get()) == stdMineral) { m_listSTDItems.erase(it); break; } } } CStandardItemPtr CSTDLib::GetSTDItem(int iIndex) { if (iIndex < 0 || iIndex >= GetSTDItemCount()) { return nullptr; } return m_listSTDItems[iIndex]; } void CSTDLib::SwapItem(CStandardItem* pstdMineral1, CStandardItem* pstdMineral2) { if (NULL == pstdMineral1 || NULL == pstdMineral2) { return; } // 交换pstdMineral1和pstdMineral2的ID即可 int id1 = pstdMineral1->GetID(); int id2 = pstdMineral2->GetID(); pstdMineral1->SetID(id2); pstdMineral2->SetID(id1); // 此时m_listSTDMinerals里面的数据顺序已经发生了改变(原来设计按照ID从小到大的顺序排列) // 此时要重新排序 std::sort(m_listSTDItems.begin(), m_listSTDItems.end(), [&](const CStandardItemPtr& ptr1, const CStandardItemPtr& ptr2)-> int { return ptr1->GetID() < ptr2->GetID(); }); } void CSTDLib::SortByItemID(BOOL bAsc/* = TRUE*/) { if (bAsc) { std::sort(m_listSTDItems.begin(), m_listSTDItems.end(), [&](const CStandardItemPtr& ptr1, const CStandardItemPtr& ptr2)-> int { return ptr1->GetID() < ptr2->GetID(); }); } else { std::sort(m_listSTDItems.begin(), m_listSTDItems.end(), [&](const CStandardItemPtr& ptr1, const CStandardItemPtr& ptr2)-> int { return ptr1->GetID() > ptr2->GetID(); }); } } int CSTDLib::GetSTDItemCount() { return m_listSTDItems.size(); } void CSTDLib::Clear() { for (int i = 0; i < (int)m_listSTDItems.size(); i++) { m_listSTDItems[i]->Clear(); } // m_strName = _T(""); m_listSTDItems.clear(); m_listClassify.clear(); } CString CSTDLib::GetValidItemName(CString strFormat) { int i = 1; CString strName = _T(""); strName.Format(_T("%s%d"), strFormat, i); for (int j = 0; j < (int)m_listSTDItems.size(); j++) { if (0 == strName.CompareNoCase(m_listSTDItems[j]->GetName())) { strName.Format(_T("%s%d"), strFormat, ++i); j = -1; // 不可以为0因为会自动j++ } } return strName; } CString CSTDLib::GetDifferItemName(CString strMineralName) { int iCount = 1; CString strNewName = strMineralName; while (HasSameItemName(strNewName)) { strNewName.Format(_T("%s_%d"), strMineralName, iCount++); } return strNewName; } bool CSTDLib::AddClassify(CClassify classify) { int i = 0; for (i = 0; i < (int)m_listClassify.size(); i++) { if (*(m_listClassify[i]) == classify) { break; } } // 不存在就添加 if (i >= (int)m_listClassify.size()) { CClassifyPtr pClassify(new CClassify(classify)); m_listClassify.push_back(pClassify); return true; } return false; } void CSTDLib::RemoveClassify(const CClassify& classify) { std::vector::iterator it; for (it = m_listClassify.begin(); it < m_listClassify.end(); it++) { if (*((*it).get()) == classify) { m_listClassify.erase(it); break; } } } int CSTDLib::GetClassifyCount() { return m_listClassify.size(); } CString CSTDLib::GetClassifyName(int iClassify) { for (int i = 0; i < (int)m_listClassify.size(); i++) { if (iClassify == m_listClassify[i]->GetID()) { return m_listClassify[i]->GetName(); } } return _T(""); } void CSTDLib::GetClassifyNames(std::vector& vecstrnames) { vecstrnames.clear(); vecstrnames.push_back(_T("")); for (int i = 0; i < (int)m_listClassify.size(); i++) { vecstrnames.push_back(m_listClassify[i]->GetName()); } } CString CSTDLib::GetDifferClassifyName(CString strClassifyName) { int iCount = 1; CString strNewName = strClassifyName; while (HasSameClassifyName(strNewName)) { strNewName.Format(_T("%s_%d"), strClassifyName, iCount++); } return strNewName; } CClassifyPtr CSTDLib::GetClassifyPtr(int iIndex) { if (iIndex < 0 || iIndex >= GetClassifyCount()) { return nullptr; } return m_listClassify[iIndex]; } CClassifyPtr CSTDLib::GetClassifyPtr(CString strClassifyName) { for (int i = 0; i < GetClassifyCount(); i++) { if (strClassifyName == m_listClassify[i]->GetName()) { return m_listClassify[i]; } } return nullptr; } void CSTDLib::SwapClassifyOrder(CClassify* pclassify1, CClassify* pclassify2) { if (NULL == pclassify1 || NULL == pclassify2) { return; } // 交换pstdMineral1和pstdMineral2的ID即可 int id1 = pclassify1->GetOrder(); int id2 = pclassify2->GetOrder(); pclassify1->SetOrder(id2); pclassify2->SetOrder(id1); // 此时m_listClassify里面的数据顺序已经发生了改变(原来设计按照ID从小到大的顺序排列) // 此时要重新排序 std::sort(m_listClassify.begin(), m_listClassify.end(), [&](const CClassifyPtr& ptr1, const CClassifyPtr& ptr2)-> int { return ptr1->GetOrder() < ptr2->GetOrder(); }); } void CSTDLib::LoadStrFromXml() { MultiLang::GetInstance().LoadStringFromXml(); } CString CSTDLib::GetSTDLibPropName(int iPropid) { // AFX_MANAGE_STATE(AfxGetStaticModuleState()); CString str = _T(""); // str.LoadString(IDS_ORETYPE_PROP + iPropid); str = MultiLang::GetInstance().GetCStringByKey(10,IDS_ORETYPE_PROP + iPropid); return str; } bool CSTDLib::HasSameItemName(CString strMineralName) { for (int i = 0; i < (int)m_listSTDItems.size(); i++) { if (0 == strMineralName.CompareNoCase(m_listSTDItems[i]->GetName())) { return true; } } return false; } bool CSTDLib::HasSameClassifyName(CString strClassifyName) { for (int i = 0; i < (int)m_listClassify.size(); i++) { if (0 == strClassifyName.CompareNoCase(m_listClassify[i]->GetName())) { return true; } } return false; } // cleanup void CSTDLib::Cleanup() { } // initialization void CSTDLib::Init() { m_strName = _T(""); m_listSTDItems.clear(); } // duplication void CSTDLib::Duplicate(const CSTDLib& a_oSource) { Init(); m_strName = a_oSource.m_strName; for (int i = 0; i < (int)a_oSource.m_listSTDItems.size(); i++) { m_listSTDItems.push_back(a_oSource.m_listSTDItems[i]); } } }