CSampleParamMgr.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using OTSDataType;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows.Forms;
  9. using System.Xml;
  10. using NLog;
  11. namespace OTSModelSharp
  12. {
  13. //used for export and import the parameter
  14. public class CSampleParamMgr
  15. {
  16. //-------------------全局定义---------
  17. protected static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
  18. // measurement parameters file mark
  19. public int MRS_PARAM_FILE_MARK = 'M' + 'E' + 'A' + 'S' + 'U' + 'R' + 'E' + 'P' + 'A' + 'R' + 'A' + 'M';
  20. // measurement parameters file version string
  21. public string MRS_PARAM_FILE_VERSION = "1.1.1";
  22. // measurement parameters file
  23. public string MESUREMENT_PARAM_FILE_EXT = (".mrp");
  24. public string MESUREMENT_PARAM_FILE_FILTER = ("Measurement Parmeters Files (*.mrp)|*.mrp||");
  25. //------------------定义----------------
  26. // file pathname
  27. string m_strPathName;
  28. // measurement parameters
  29. CSampleParam m_poMsrParams;
  30. //----------------public------------------
  31. public CSampleParamMgr()
  32. {
  33. // initialization
  34. Init();
  35. }
  36. public void Serialize(bool isStoring, XmlDocument classDoc, XmlNode rootNode)
  37. {
  38. xInt xMrsParamFileMark = new xInt();
  39. xString xMrsOaramFileVersion = new xString();
  40. Slo slo = new Slo();
  41. slo.Register("MrsParamFileMark", xMrsParamFileMark);
  42. slo.Register("MrsParamFileVersion", xMrsOaramFileVersion);
  43. slo.Register("MsrParams", m_poMsrParams);
  44. if (isStoring)
  45. {
  46. xMrsParamFileMark.AssignValue(MRS_PARAM_FILE_MARK);
  47. xMrsOaramFileVersion.AssignValue(MRS_PARAM_FILE_VERSION);
  48. slo.Serialize(true, classDoc, rootNode);
  49. }
  50. else
  51. {
  52. slo.Serialize(false, classDoc, rootNode);
  53. }
  54. }
  55. //load/Save
  56. public bool Load(string a_strPathName, bool a_bClear)
  57. {
  58. if (a_bClear)
  59. {
  60. Init();
  61. }
  62. //获取属性配置文件
  63. XmlDocument doc = new XmlDocument();
  64. //载入xml文件
  65. doc.Load(a_strPathName);
  66. XmlNode rootNode = doc.SelectSingleNode("XMLData");
  67. Serialize(false, doc, rootNode);
  68. // file pathname
  69. m_strPathName = a_strPathName;
  70. // ok, return TRUE
  71. return true;
  72. }
  73. public bool Load(bool a_bClear)
  74. {
  75. if (a_bClear)
  76. {
  77. Init();
  78. }
  79. SaveFileDialog folder = new SaveFileDialog();
  80. folder.InitialDirectory = Application.StartupPath + "\\Config\\ProData";
  81. if (folder.ShowDialog() != DialogResult.OK)
  82. {
  83. return false;
  84. }
  85. //a_strPathName = folder.InitialDirectory;
  86. XmlDocument doc = new XmlDocument();
  87. doc.Load(folder.InitialDirectory);
  88. doc.RemoveAll();
  89. XmlDeclaration xmldecl = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
  90. doc.AppendChild(xmldecl);
  91. XmlElement rootNode = doc.CreateElement("XMLData");
  92. doc.AppendChild(rootNode);
  93. Serialize(false, doc, rootNode);
  94. try
  95. {
  96. doc.Save(folder.InitialDirectory);
  97. }
  98. catch
  99. {
  100. return false;
  101. }
  102. //doc.LoadXml(folder.InitialDirectory);
  103. //XmlDocument xmlDoc = new XmlDocument();
  104. //xmlDoc.LoadXml(folder.InitialDirectory);
  105. //XmlNode Root = xmlDoc.SelectSingleNode("XMLData");
  106. //Serialize(false, xmlDoc, Root);
  107. // file pathname
  108. m_strPathName = folder.InitialDirectory;
  109. return true;
  110. }
  111. public bool Save(string a_strPathName)
  112. {
  113. //保存测量项目文件 .prj
  114. XmlDocument doc = new XmlDocument();
  115. //添加xml文件头申明
  116. XmlDeclaration xmldecl = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
  117. doc.AppendChild(xmldecl);
  118. XmlElement rootNode = doc.CreateElement("XMLData");
  119. doc.AppendChild(rootNode);
  120. Serialize(true, doc, rootNode);
  121. m_strPathName = a_strPathName;
  122. try
  123. {
  124. doc.Save(a_strPathName);
  125. }
  126. catch
  127. {
  128. return false;
  129. }
  130. m_strPathName = a_strPathName;
  131. return true;
  132. }
  133. public bool Save()
  134. {
  135. SaveFileDialog folder = new SaveFileDialog();
  136. folder.InitialDirectory = Application.StartupPath + "\\Config\\ProData";
  137. if (folder.ShowDialog() != DialogResult.OK)
  138. {
  139. return false;
  140. }
  141. XmlDocument doc = new XmlDocument();
  142. doc.Load(folder.InitialDirectory);
  143. doc.RemoveAll();
  144. XmlDeclaration xmldecl = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
  145. doc.AppendChild(xmldecl);
  146. XmlElement rootNode = doc.CreateElement("XMLData");
  147. doc.AppendChild(rootNode);
  148. Serialize(true, doc, rootNode);
  149. // file pathname
  150. m_strPathName = folder.InitialDirectory;
  151. try
  152. {
  153. doc.Save(folder.InitialDirectory);
  154. }
  155. catch
  156. {
  157. return false;
  158. }
  159. return true;
  160. }
  161. // file pathname
  162. public string GetPathName()
  163. {
  164. return m_strPathName;
  165. }
  166. public void SetPathName(string a_strPathName)
  167. {
  168. m_strPathName = a_strPathName;
  169. }
  170. // measurement parameters file
  171. public CSampleParam GetMsrParams()
  172. {
  173. return m_poMsrParams;
  174. }
  175. public bool SetMsrParamFile(CSampleParam a_poMsrParams)
  176. {
  177. m_poMsrParams = a_poMsrParams;
  178. return true;
  179. }
  180. //--------------protected----------------
  181. // initialization
  182. protected void Init()
  183. {
  184. m_poMsrParams = new CSampleParam();
  185. }
  186. // duplication
  187. public void Duplicate( CSampleParamMgr a_oSource)
  188. {
  189. // initialization
  190. Init();
  191. // copy data over
  192. }
  193. }
  194. }