PartSizeFile.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. #include "stdafx.h"
  2. #include "PartSizeFile.h"
  3. namespace OTSMODEL {
  4. // CPartSizeFile
  5. // constructor
  6. CPartSizeFile::CPartSizeFile()
  7. {
  8. // initialization
  9. Init();
  10. }
  11. // copy constructor
  12. CPartSizeFile::CPartSizeFile(const CPartSizeFile& a_oSource)
  13. {
  14. // can't copy itself
  15. if (&a_oSource == this)
  16. {
  17. return;
  18. }
  19. // copy data over
  20. Duplicate(a_oSource);
  21. }
  22. // copy constructor
  23. CPartSizeFile::CPartSizeFile(CPartSizeFile* a_poSource)
  24. {
  25. // can't copy itself
  26. if (a_poSource == this)
  27. {
  28. return;
  29. }
  30. // copy data over
  31. Duplicate(*a_poSource);
  32. }
  33. // =operator
  34. CPartSizeFile& CPartSizeFile::operator=(const CPartSizeFile& a_oSource)
  35. {
  36. // cleanup
  37. Cleanup();
  38. // copy the class data over
  39. Duplicate(a_oSource);
  40. // return class
  41. return *this;
  42. }
  43. // ==operator
  44. BOOL CPartSizeFile::operator==(const CPartSizeFile& a_oSource)
  45. {
  46. // double parts
  47. // return FASLE, if the two list are in different size
  48. int nSize = (int)m_listSizes.size();
  49. if (nSize != (int)a_oSource.m_listSizes.size())
  50. {
  51. return FALSE;
  52. }
  53. // return FALSE if any of the pare holes are different
  54. for (int i = 0; i < nSize; ++i)
  55. {
  56. if (!(m_listSizes[i] == a_oSource.m_listSizes[i]))
  57. {
  58. return FALSE;
  59. }
  60. }
  61. // return FASLE, if the two list are in different size
  62. nSize = (int)m_listPartSize.size();
  63. if (nSize != (int)a_oSource.m_listPartSize.size())
  64. {
  65. return FALSE;
  66. }
  67. // return FALSE if any of the pare holes are different
  68. for (int i = 0; i < nSize; ++i)
  69. {
  70. if (!(*(m_listPartSize[i].get()) == *(a_oSource.m_listPartSize[i].get())))
  71. {
  72. return FALSE;
  73. }
  74. }
  75. return (m_strName.Compare(a_oSource.m_strName) == 0 &&
  76. m_strVersion.Compare(a_oSource.m_strVersion) == 0);
  77. }
  78. // destructor
  79. CPartSizeFile::~CPartSizeFile()
  80. {
  81. // cleanup
  82. Cleanup();
  83. }
  84. // CPartSizeItem member functions
  85. // public
  86. // serialization
  87. void CPartSizeFile::Serialize(bool isStoring, tinyxml2::XMLDocument * classDoc, tinyxml2::XMLElement * rootNode)
  88. {
  89. xmls::xInt xnFileMark;
  90. xmls::xString xnVersion;
  91. xmls::xString xstrName;
  92. xmls::xString xSizeStr;
  93. xmls::Slo slo;
  94. slo.Register("FileMark", &xnFileMark);
  95. slo.Register("Version", &xnVersion);
  96. slo.Register("Name", &xstrName);
  97. slo.Register("Sizes", &xSizeStr);
  98. if (isStoring)
  99. {
  100. xnFileMark = PART_SIZE_FILE_MARK;
  101. xnVersion = PART_SIZE_FILE_VERSION;
  102. xstrName = m_strName;
  103. std::string ss="";
  104. std::string sizeStr = "";
  105. for (unsigned int i = 0; i < m_listSizes.size(); i++)
  106. {
  107. ss=std::to_string(m_listSizes[i]);
  108. if (sizeStr=="")
  109. {
  110. sizeStr = ss ;
  111. }
  112. else
  113. {
  114. sizeStr = sizeStr +","+ ss;
  115. }
  116. }
  117. xSizeStr = sizeStr;
  118. slo.Serialize(true, classDoc, rootNode);
  119. }
  120. else
  121. {
  122. slo.Serialize(false, classDoc, rootNode);
  123. m_strName = xstrName.value().c_str();
  124. m_strVersion = xnVersion.value().c_str();
  125. std::string sizeStr;
  126. sizeStr = xSizeStr.value();
  127. std::vector<std::string> sizes;
  128. xmls::SplitString(sizeStr, sizes, ",");
  129. m_listSizes.clear();
  130. for (unsigned int i = 0; i < sizes.size(); i++)
  131. {
  132. double ss;
  133. ss = std::stod(sizes[i]);
  134. m_listSizes.push_back(ss);
  135. }
  136. ChangDoubleToItem();
  137. }
  138. }
  139. void CPartSizeFile::SetSizes(CPartSizeList& a_listPartSize, BOOL a_bClear/* = TRUE*/)
  140. {
  141. // clear list if necessary
  142. if (a_bClear)
  143. {
  144. m_listSizes.clear();
  145. }
  146. // copy the list
  147. for (auto pPartSize : a_listPartSize)
  148. {
  149. m_listSizes.push_back(pPartSize);
  150. }
  151. }
  152. void CPartSizeFile::SetPartSizeList(CPartSizeItemList& a_listPartSize, BOOL a_bClear/* = TRUE*/)
  153. {
  154. // clear list if necessary
  155. if (a_bClear)
  156. {
  157. m_listPartSize.clear();
  158. }
  159. // copy the list
  160. for (auto pPartSize : a_listPartSize)
  161. {
  162. CPartSizeItemPtr pPartSizeNew = CPartSizeItemPtr(new CPartSizeItem(*pPartSize.get()));
  163. m_listPartSize.push_back(pPartSizeNew);
  164. }
  165. }
  166. CPartSizeItemPtr CPartSizeFile::GetPartSizeByIndex(int a_nIndex)
  167. {
  168. if (a_nIndex < 0 || a_nIndex >= (int)m_listPartSize.size())
  169. {
  170. // invalid index
  171. return nullptr;
  172. }
  173. return m_listPartSize[a_nIndex];
  174. }
  175. CPartSizeItemPtr CPartSizeFile::GetPartSizeByName(CString a_strPartSizeName)
  176. {
  177. a_strPartSizeName.Trim();
  178. if (a_strPartSizeName.IsEmpty())
  179. {
  180. // invalid hole name
  181. return nullptr;
  182. }
  183. auto itr = std::find_if(m_listPartSize.begin(), m_listPartSize.end(), [a_strPartSizeName](CPartSizeItemPtr p) { return p->GetName().CompareNoCase(a_strPartSizeName) == 0; });
  184. if (itr == m_listPartSize.end())
  185. {
  186. // didn't find the hole
  187. return nullptr;
  188. }
  189. // return the hole
  190. return *itr;
  191. }
  192. // protected
  193. // cleanup
  194. void CPartSizeFile::Cleanup()
  195. {
  196. m_listPartSize.clear();
  197. m_listSizes.clear();
  198. }
  199. // initialization
  200. void CPartSizeFile::Init()
  201. {
  202. // initialization
  203. m_strName = _T("");
  204. m_strVersion = _T("");
  205. m_listPartSize.clear();
  206. m_listSizes.clear();
  207. }
  208. // duplication
  209. void CPartSizeFile::Duplicate(const CPartSizeFile& a_oSource)
  210. {
  211. // initialization
  212. Init();
  213. // copy data over
  214. m_strName = a_oSource.m_strName;
  215. m_strVersion = a_oSource.m_strVersion;
  216. for (auto dSize : a_oSource.m_listSizes)
  217. {
  218. m_listSizes.push_back(dSize);
  219. }
  220. for (auto pPartSize : a_oSource.m_listPartSize)
  221. {
  222. CPartSizeItemPtr pPartSizeNew = CPartSizeItemPtr(new CPartSizeItem(pPartSize.get()));
  223. m_listPartSize.push_back(pPartSizeNew);
  224. }
  225. }
  226. //double to item
  227. BOOL CPartSizeFile::ChangDoubleToItem()
  228. {
  229. // generate
  230. CPartSizeList SizeList(m_listSizes);
  231. sort(SizeList.begin(), SizeList.end());
  232. m_listPartSize.clear();
  233. for (size_t i = 0; i < SizeList.size(); ++i)
  234. {
  235. CPartSizeItemPtr pParSize = CPartSizeItemPtr(new CPartSizeItem());
  236. if ((i + 1) < SizeList.size())
  237. {
  238. pParSize->BuildList(SizeList[i], SizeList[i + 1]);
  239. }
  240. else
  241. {//LASTPART
  242. pParSize->BuildList(SizeList[i], PART_LAST_NUMBER);
  243. }
  244. m_listPartSize.push_back(pParSize);
  245. }
  246. return true;
  247. }
  248. }