| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- #include "stdafx.h"
- #include "PartSizeFile.h"
- namespace OTSMODEL {
- // CPartSizeFile
- // constructor
- CPartSizeFile::CPartSizeFile()
- {
- // initialization
- Init();
- }
- // copy constructor
- CPartSizeFile::CPartSizeFile(const CPartSizeFile& a_oSource)
- {
- // can't copy itself
- if (&a_oSource == this)
- {
- return;
- }
- // copy data over
- Duplicate(a_oSource);
- }
- // copy constructor
- CPartSizeFile::CPartSizeFile(CPartSizeFile* a_poSource)
- {
- // can't copy itself
- if (a_poSource == this)
- {
- return;
- }
- // copy data over
- Duplicate(*a_poSource);
- }
- // =operator
- CPartSizeFile& CPartSizeFile::operator=(const CPartSizeFile& a_oSource)
- {
- // cleanup
- Cleanup();
- // copy the class data over
- Duplicate(a_oSource);
- // return class
- return *this;
- }
- // ==operator
- BOOL CPartSizeFile::operator==(const CPartSizeFile& a_oSource)
- {
- // double parts
- // return FASLE, if the two list are in different size
- int nSize = (int)m_listSizes.size();
- if (nSize != (int)a_oSource.m_listSizes.size())
- {
- return FALSE;
- }
- // return FALSE if any of the pare holes are different
- for (int i = 0; i < nSize; ++i)
- {
- if (!(m_listSizes[i] == a_oSource.m_listSizes[i]))
- {
- return FALSE;
- }
- }
- // return FASLE, if the two list are in different size
- nSize = (int)m_listPartSize.size();
- if (nSize != (int)a_oSource.m_listPartSize.size())
- {
- return FALSE;
- }
- // return FALSE if any of the pare holes are different
- for (int i = 0; i < nSize; ++i)
- {
- if (!(*(m_listPartSize[i].get()) == *(a_oSource.m_listPartSize[i].get())))
- {
- return FALSE;
- }
- }
- return (m_strName.Compare(a_oSource.m_strName) == 0 &&
- m_strVersion.Compare(a_oSource.m_strVersion) == 0);
- }
- // destructor
- CPartSizeFile::~CPartSizeFile()
- {
- // cleanup
- Cleanup();
- }
- // CPartSizeItem member functions
- // public
- // serialization
- void CPartSizeFile::Serialize(bool isStoring, tinyxml2::XMLDocument * classDoc, tinyxml2::XMLElement * rootNode)
- {
- xmls::xInt xnFileMark;
- xmls::xString xnVersion;
- xmls::xString xstrName;
- xmls::xString xSizeStr;
- xmls::Slo slo;
- slo.Register("FileMark", &xnFileMark);
- slo.Register("Version", &xnVersion);
- slo.Register("Name", &xstrName);
- slo.Register("Sizes", &xSizeStr);
-
- if (isStoring)
- {
- xnFileMark = PART_SIZE_FILE_MARK;
- xnVersion = PART_SIZE_FILE_VERSION;
- xstrName = m_strName;
- std::string ss="";
- std::string sizeStr = "";
- for (unsigned int i = 0; i < m_listSizes.size(); i++)
- {
- ss=std::to_string(m_listSizes[i]);
- if (sizeStr=="")
- {
- sizeStr = ss ;
- }
- else
- {
- sizeStr = sizeStr +","+ ss;
- }
- }
- xSizeStr = sizeStr;
- slo.Serialize(true, classDoc, rootNode);
- }
- else
- {
- slo.Serialize(false, classDoc, rootNode);
- m_strName = xstrName.value().c_str();
- m_strVersion = xnVersion.value().c_str();
- std::string sizeStr;
- sizeStr = xSizeStr.value();
- std::vector<std::string> sizes;
- xmls::SplitString(sizeStr, sizes, ",");
- m_listSizes.clear();
- for (unsigned int i = 0; i < sizes.size(); i++)
- {
- double ss;
- ss = std::stod(sizes[i]);
- m_listSizes.push_back(ss);
- }
- ChangDoubleToItem();
- }
- }
- void CPartSizeFile::SetSizes(CPartSizeList& a_listPartSize, BOOL a_bClear/* = TRUE*/)
- {
- // clear list if necessary
- if (a_bClear)
- {
- m_listSizes.clear();
- }
- // copy the list
- for (auto pPartSize : a_listPartSize)
- {
- m_listSizes.push_back(pPartSize);
- }
- }
- void CPartSizeFile::SetPartSizeList(CPartSizeItemList& a_listPartSize, BOOL a_bClear/* = TRUE*/)
- {
- // clear list if necessary
- if (a_bClear)
- {
- m_listPartSize.clear();
- }
- // copy the list
- for (auto pPartSize : a_listPartSize)
- {
- CPartSizeItemPtr pPartSizeNew = CPartSizeItemPtr(new CPartSizeItem(*pPartSize.get()));
- m_listPartSize.push_back(pPartSizeNew);
- }
- }
-
- CPartSizeItemPtr CPartSizeFile::GetPartSizeByIndex(int a_nIndex)
- {
- if (a_nIndex < 0 || a_nIndex >= (int)m_listPartSize.size())
- {
- // invalid index
- return nullptr;
- }
- return m_listPartSize[a_nIndex];
- }
- CPartSizeItemPtr CPartSizeFile::GetPartSizeByName(CString a_strPartSizeName)
- {
- a_strPartSizeName.Trim();
- if (a_strPartSizeName.IsEmpty())
- {
- // invalid hole name
- return nullptr;
- }
- auto itr = std::find_if(m_listPartSize.begin(), m_listPartSize.end(), [a_strPartSizeName](CPartSizeItemPtr p) { return p->GetName().CompareNoCase(a_strPartSizeName) == 0; });
- if (itr == m_listPartSize.end())
- {
- // didn't find the hole
- return nullptr;
- }
- // return the hole
- return *itr;
- }
- // protected
- // cleanup
- void CPartSizeFile::Cleanup()
- {
- m_listPartSize.clear();
- m_listSizes.clear();
- }
- // initialization
- void CPartSizeFile::Init()
- {
- // initialization
- m_strName = _T("");
- m_strVersion = _T("");
- m_listPartSize.clear();
- m_listSizes.clear();
- }
- // duplication
- void CPartSizeFile::Duplicate(const CPartSizeFile& a_oSource)
- {
- // initialization
- Init();
- // copy data over
- m_strName = a_oSource.m_strName;
- m_strVersion = a_oSource.m_strVersion;
- for (auto dSize : a_oSource.m_listSizes)
- {
- m_listSizes.push_back(dSize);
- }
- for (auto pPartSize : a_oSource.m_listPartSize)
- {
- CPartSizeItemPtr pPartSizeNew = CPartSizeItemPtr(new CPartSizeItem(pPartSize.get()));
- m_listPartSize.push_back(pPartSizeNew);
- }
- }
- //double to item
- BOOL CPartSizeFile::ChangDoubleToItem()
- {
- // generate
- CPartSizeList SizeList(m_listSizes);
- sort(SizeList.begin(), SizeList.end());
- m_listPartSize.clear();
- for (size_t i = 0; i < SizeList.size(); ++i)
- {
- CPartSizeItemPtr pParSize = CPartSizeItemPtr(new CPartSizeItem());
- if ((i + 1) < SizeList.size())
- {
- pParSize->BuildList(SizeList[i], SizeList[i + 1]);
- }
- else
- {//LASTPART
- pParSize->BuildList(SizeList[i], PART_LAST_NUMBER);
- }
- m_listPartSize.push_back(pParSize);
-
- }
- return true;
- }
- }
|