MeasureFile.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. //时间:20200610
  2. //作者:郝爽
  3. //功能:测量文件
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using FileManager;
  11. using System.Xml;
  12. using System.IO;
  13. namespace MeasureData
  14. {
  15. public class MeasureFile:ISlo
  16. {
  17. public const string UNTITLED_FILE_NAME = "Untitled";
  18. #region 内容
  19. //文件名
  20. private string m_fileName;
  21. public string FileName
  22. {
  23. get { return this.m_fileName; }
  24. set { this.m_fileName = value; }
  25. }
  26. //文件路径
  27. private string m_filepath;
  28. public string FilePath
  29. {
  30. get { return this.m_filepath; }
  31. set { this.m_filepath = value; }
  32. }
  33. //切孔链表
  34. private List<CutHole> m_listCutHole;
  35. public List<CutHole> ListCutHole
  36. {
  37. get { return this.m_listCutHole; }
  38. set { this.m_listCutHole = value; }
  39. }
  40. //测量参数
  41. private MeasureParam m_measureParam;
  42. public MeasureParam MParam
  43. {
  44. get { return this.m_measureParam; }
  45. set { this.m_measureParam = value; }
  46. }
  47. #endregion
  48. /// <summary>
  49. /// 创建XML文件
  50. /// </summary>
  51. /// <returns>0:失败;1:成功;2:文件已存在</returns>
  52. public int CreateXml()
  53. {
  54. if (!File.Exists(this.FileName))
  55. {
  56. if( XmlManager.CreateXmlFile(this.FileName))
  57. {
  58. //创建一个新的
  59. return 1;
  60. }
  61. else
  62. {
  63. //创建失败
  64. return 0;
  65. }
  66. }
  67. else
  68. {
  69. //重新创建
  70. XmlManager.CreateXmlFile(this.FileName);
  71. return 2;
  72. }
  73. }
  74. //XML文件保存
  75. //样品孔存储xml文档
  76. public override void Serialize(bool isStoring, XmlDocument xml, XmlNode rootNode)
  77. {
  78. Slo sFile = new Slo();
  79. Slo sParam = new Slo();
  80. Collection<CutHole> sCutHoles = new Collection<CutHole>();
  81. foreach (CutHole hole in ListCutHole)
  82. {
  83. sCutHoles.addItem(hole);
  84. }
  85. xString FileName = new xString();
  86. xString FilePath = new xString();
  87. FileName.AssignValue(this.FileName);
  88. FilePath.AssignValue(this.FilePath);
  89. sFile.Register("FileName", FileName);
  90. sFile.Register("FilePath", FilePath);
  91. sFile.Register("Param", this.MParam);
  92. sFile.Register("ListCutHole", sCutHoles);
  93. if (isStoring)
  94. {
  95. sFile.Serialize(true, xml, rootNode);
  96. }
  97. else
  98. {
  99. sFile.Serialize(false, xml, rootNode);
  100. this.FileName = FileName.value();
  101. this.FilePath = FilePath.value();
  102. for (int i = 0; i < sCutHoles.size(); i++)
  103. {
  104. ListCutHole.Add(sCutHoles.getItem(i));
  105. }
  106. }
  107. }
  108. //构造函数
  109. public MeasureFile()
  110. {
  111. Init();
  112. }
  113. public void Init()
  114. {
  115. this.ListCutHole = new List<CutHole>();
  116. this.FileName = @"";
  117. this.FilePath = @"";
  118. this.MParam = new MeasureParam();
  119. }
  120. #region 操作
  121. //新建
  122. public bool New()
  123. {
  124. int ret = CreateXml();
  125. if (ret > 0)
  126. {
  127. XmlDocument doc = new XmlDocument();
  128. doc.Load(this.FileName);//载入xml文件
  129. XmlNode root = doc.SelectSingleNode("XMLData");
  130. Serialize(true, doc, root);
  131. doc.Save(this.FileName);
  132. }
  133. // 设置路径为初始默认路径
  134. this.FileName = UNTITLED_FILE_NAME;
  135. // Ok, return TRUE
  136. return true;
  137. }
  138. //打开
  139. public void Open()
  140. {
  141. }
  142. //保存
  143. public bool Save()
  144. {
  145. //如果是新文件
  146. this.FileName.Trim();
  147. if (string.Compare(this.FileName,UNTITLED_FILE_NAME) == 0)
  148. {
  149. return SaveAs();
  150. }
  151. XmlDocument doc = new XmlDocument();
  152. if (CreateXml() == 0)//创建该文件?
  153. {
  154. return false;
  155. }
  156. if (File.Exists(FileName))
  157. {
  158. doc.Load(this.FileName);//载入xml文件
  159. }
  160. else
  161. {
  162. return SaveAs(); //当前路径不存在
  163. }
  164. XmlNode root = doc.SelectSingleNode("XMLData");
  165. Serialize(true, doc, root);
  166. doc.Save(this.FileName);
  167. return true;
  168. }
  169. //另存为
  170. public bool SaveAs()
  171. {
  172. //打开保存文件的对话框
  173. this.FileName.Trim();
  174. if (!string.IsNullOrEmpty(this.FileName))
  175. {
  176. this.FilePath = System.IO.Path.GetDirectoryName(this.FileName);
  177. }
  178. SaveFileDialog sfd = new SaveFileDialog();
  179. sfd.Title = "保存测量文件";
  180. sfd.Filter = "测量文件(*.msf)|*.msf";
  181. if (Directory.Exists(this.FilePath))
  182. {
  183. sfd.InitialDirectory = this.FilePath;
  184. }
  185. if (sfd.ShowDialog() == DialogResult.OK)
  186. {
  187. this.FileName = sfd.FileName.ToString(); //获得文件路径
  188. FilePath = System.IO.Path.GetDirectoryName(this.FileName);
  189. //创建一个新的文件
  190. XmlDocument doc = new XmlDocument();
  191. if (CreateXml() == 0)//创建该文件?
  192. {
  193. return false;
  194. }
  195. doc.Load(this.FileName);
  196. XmlNode root = doc.SelectSingleNode("XMLData");
  197. Serialize(true, doc, root);
  198. doc.Save(this.FileName);
  199. }
  200. return true;
  201. }
  202. //从文件生成切割孔信息
  203. public bool GetCutHolesFromFile(string a_FilePathName)
  204. {
  205. try
  206. {
  207. //清空原文件中的切孔
  208. this.ListCutHole.Clear();
  209. //弹出打开txt文件的对话矿
  210. a_FilePathName.Trim();
  211. if (string.IsNullOrEmpty(a_FilePathName))
  212. {
  213. //新建一个文件对话框
  214. OpenFileDialog pOpenFileDialog = new OpenFileDialog();
  215. //设置对话框标题
  216. pOpenFileDialog.Title = "打开电镜位置列表文件";
  217. //设置打开文件类型
  218. pOpenFileDialog.Filter = "txt文件(*.txt)|*.txt";
  219. //监测文件是否存在
  220. pOpenFileDialog.CheckFileExists = true;
  221. //文件打开后执行以下程序
  222. if (pOpenFileDialog.ShowDialog() == DialogResult.OK)
  223. {
  224. a_FilePathName = System.IO.Path.GetFullPath(pOpenFileDialog.FileName); //绝对路径
  225. }
  226. }
  227. //按行取出txt文件
  228. string[] lines = File.ReadAllLines(a_FilePathName, System.Text.Encoding.Default);
  229. LogManager.AddHardwareLog("read " + lines.Count().ToString() + "lines", true);
  230. //按现有的文件格式生成
  231. string posMode = lines[1];
  232. if (posMode.CompareTo(@"Absolute") != 0)
  233. {
  234. return false;
  235. }
  236. //解析切孔生成带有位置的切孔
  237. //验证数量是否正确
  238. int nNum = Convert.ToInt32(lines[3]);
  239. int nLines = lines.Length;
  240. if (nNum != (lines.Length - 4))
  241. {
  242. return false;
  243. }
  244. //行内标识的个数及位置
  245. string[] titles = lines[2].Split(',');
  246. int numPos = titles.Length;
  247. int nLabelPos = Array.IndexOf(titles, "Label");
  248. int nXPos = Array.IndexOf(titles, "X");
  249. int nYPos = Array.IndexOf(titles, "Y");
  250. int nZPos = Array.IndexOf(titles, "Z");
  251. int nTPos = Array.IndexOf(titles, "T");
  252. int nRPos = Array.IndexOf(titles, "R");
  253. int nMPos = Array.IndexOf(titles, "M");
  254. for (int i = 0; i < nNum; i++)
  255. {
  256. int currentLine = i + 4;
  257. string currentString = lines[currentLine];
  258. LogManager.AddHardwareLog(currentString, true);
  259. string[] CurrentPos = currentString.Split(',');
  260. int nCurrentPosNum = CurrentPos.Length;
  261. //当前行内的标识及位置个数不够
  262. if (nCurrentPosNum != numPos)
  263. {
  264. return false;
  265. }
  266. //切孔标识
  267. CutHole CHole = new CutHole();
  268. CHole.HoleName = CurrentPos[nLabelPos];
  269. //切孔位置
  270. SemPosition holePos = new SemPosition();
  271. holePos.X = Convert.ToSingle(CurrentPos[nXPos]);
  272. holePos.Y = Convert.ToSingle(CurrentPos[nYPos]);
  273. holePos.Z = Convert.ToSingle(CurrentPos[nZPos]);
  274. holePos.M = Convert.ToSingle(CurrentPos[nMPos]);
  275. holePos.T = Convert.ToSingle(CurrentPos[nTPos]);
  276. holePos.R = Convert.ToSingle(CurrentPos[nRPos]);
  277. CHole.Position = holePos;
  278. //默认切割点均参与测试
  279. CHole.SWITCH = true;
  280. //更新切孔链表
  281. this.ListCutHole.Add(CHole);
  282. LogManager.AddHardwareLog("X =" + CurrentPos[nXPos]
  283. + "Y =" + CurrentPos[nYPos]
  284. + "Z =" + CurrentPos[nZPos]
  285. + "M =" + CurrentPos[nTPos]
  286. + "T =" + CurrentPos[nRPos]
  287. + "R =" + CurrentPos[nMPos]
  288. , true);
  289. }
  290. return true;
  291. }
  292. catch (Exception ex)
  293. {
  294. LogManager.LogError(ex.Message);
  295. return false;
  296. }
  297. }
  298. #endregion
  299. }
  300. }