MeasureFile.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. return 1;
  59. }
  60. else
  61. {
  62. return 0;
  63. }
  64. }
  65. else
  66. {
  67. XmlManager.CreateXmlFile(this.FileName);
  68. return 2;
  69. }
  70. }
  71. //XML文件保存
  72. //样品孔存储xml文档
  73. public override void Serialize(bool isStoring, XmlDocument xml, XmlNode rootNode)
  74. {
  75. Slo sFile = new Slo();
  76. Slo sParam = new Slo();
  77. Collection<CutHole> sCutHoles = new Collection<CutHole>();
  78. foreach (CutHole hole in ListCutHole)
  79. {
  80. sCutHoles.addItem(hole);
  81. }
  82. xString FileName = new xString();
  83. xString FilePath = new xString();
  84. FileName.AssignValue(this.FileName);
  85. FilePath.AssignValue(this.FilePath);
  86. sFile.Register("FileName", FileName);
  87. sFile.Register("FilePath", FilePath);
  88. sFile.Register("Param", this.MParam);
  89. sFile.Register("ListCutHole", sCutHoles);
  90. if (isStoring)
  91. {
  92. sFile.Serialize(true, xml, rootNode);
  93. }
  94. else
  95. {
  96. sFile.Serialize(false, xml, rootNode);
  97. this.FileName = FileName.value();
  98. this.FilePath = FilePath.value();
  99. for (int i = 0; i < sCutHoles.size(); i++)
  100. {
  101. ListCutHole.Add(sCutHoles.getItem(i));
  102. }
  103. }
  104. }
  105. //构造函数
  106. public MeasureFile()
  107. {
  108. Init();
  109. }
  110. public void Init()
  111. {
  112. this.ListCutHole = new List<CutHole>();
  113. this.FileName = @"";
  114. this.FilePath = @"";
  115. this.MParam = new MeasureParam();
  116. }
  117. #region 操作
  118. //新建
  119. public bool New()
  120. {
  121. int ret = CreateXml();
  122. if (ret > 0)
  123. {
  124. XmlDocument doc = new XmlDocument();
  125. doc.Load(this.FileName);//载入xml文件
  126. XmlNode root = doc.SelectSingleNode("XMLData");
  127. Serialize(true, doc, root);
  128. doc.Save(this.FileName);
  129. }
  130. // 设置路径为初始默认路径
  131. this.FileName = UNTITLED_FILE_NAME;
  132. // Ok, return TRUE
  133. return true;
  134. }
  135. //打开
  136. public void Open()
  137. {
  138. }
  139. //保存
  140. public bool Save()
  141. {
  142. //如果是新文件
  143. this.FileName.Trim();
  144. if (string.Compare(this.FileName,UNTITLED_FILE_NAME) == 0)
  145. {
  146. return SaveAs();
  147. }
  148. XmlDocument doc = new XmlDocument();
  149. if (File.Exists(FileName))
  150. {
  151. doc.Load(this.FileName);//载入xml文件
  152. }
  153. else
  154. {
  155. return SaveAs(); //当前路径不存在
  156. }
  157. XmlNode root = doc.SelectSingleNode("XMLData");
  158. Serialize(true, doc, root);
  159. doc.Save(this.FileName);
  160. return true;
  161. }
  162. //另存为
  163. public bool SaveAs()
  164. {
  165. //打开保存文件的对话框
  166. this.FileName.Trim();
  167. if (!string.IsNullOrEmpty(this.FileName))
  168. {
  169. this.FilePath = System.IO.Path.GetDirectoryName(this.FileName);
  170. }
  171. SaveFileDialog sfd = new SaveFileDialog();
  172. sfd.Title = "保存测量文件";
  173. sfd.Filter = "测量文件(*.msf)|*.msf";
  174. if (Directory.Exists(this.FilePath))
  175. {
  176. sfd.InitialDirectory = this.FilePath;
  177. }
  178. if (sfd.ShowDialog() == DialogResult.OK)
  179. {
  180. this.FileName = sfd.FileName.ToString(); //获得文件路径
  181. FilePath = System.IO.Path.GetDirectoryName(this.FileName);
  182. //创建一个新的文件
  183. XmlDocument doc = new XmlDocument();
  184. if (!File.Exists(this.FileName))
  185. {
  186. if (CreateXml() > 0)//创建该文件?
  187. {
  188. return false;
  189. }
  190. }
  191. doc.Load(this.FileName);
  192. XmlNode root = doc.SelectSingleNode("XMLData");
  193. Serialize(true, doc, root);
  194. doc.Save(this.FileName);
  195. }
  196. return true;
  197. }
  198. //从文件生成切割孔信息
  199. public bool GetCutHolesFromFile(string a_FilePathName)
  200. {
  201. //弹出打开txt文件的对话矿
  202. a_FilePathName.Trim();
  203. if (string.IsNullOrEmpty(a_FilePathName))
  204. {
  205. //新建一个文件对话框
  206. OpenFileDialog pOpenFileDialog = new OpenFileDialog();
  207. //设置对话框标题
  208. pOpenFileDialog.Title = "打开电镜位置列表文件";
  209. //设置打开文件类型
  210. pOpenFileDialog.Filter = "txt文件(*.txt)|*.txt";
  211. //监测文件是否存在
  212. pOpenFileDialog.CheckFileExists = true;
  213. //文件打开后执行以下程序
  214. if (pOpenFileDialog.ShowDialog() == DialogResult.OK)
  215. {
  216. a_FilePathName = System.IO.Path.GetFullPath(pOpenFileDialog.FileName); //绝对路径
  217. }
  218. }
  219. //按行取出txt文件
  220. string[] lines = File.ReadAllLines(a_FilePathName, System.Text.Encoding.Default);
  221. //按现有的文件格式生成
  222. string posMode = lines[1];
  223. if (posMode.CompareTo(@"Absolute") != 0)
  224. {
  225. return false;
  226. }
  227. //解析切孔生成带有位置的切孔
  228. //验证数量是否正确
  229. int nNum = Convert.ToInt32(lines[3]);
  230. int nLines = lines.Length;
  231. if (nNum != (lines.Length - 4))
  232. {
  233. return false;
  234. }
  235. //行内标识的个数及位置
  236. string[] titles = lines[2].Split(',');
  237. int numPos = titles.Length;
  238. int nLabelPos = Array.IndexOf(titles, "Label");
  239. int nXPos = Array.IndexOf(titles, "X");
  240. int nYPos = Array.IndexOf(titles, "Y");
  241. int nZPos = Array.IndexOf(titles, "Z");
  242. int nTPos = Array.IndexOf(titles, "T");
  243. int nRPos = Array.IndexOf(titles, "R");
  244. int nMPos = Array.IndexOf(titles, "M");
  245. for (int i = 0; i < nNum; i++)
  246. {
  247. int currentLine = i + 4;
  248. string currentString = lines[currentLine];
  249. string[] CurrentPos = currentString.Split(',');
  250. int nCurrentPosNum = CurrentPos.Length;
  251. //当前行内的标识及位置个数不够
  252. if (nCurrentPosNum != numPos)
  253. {
  254. return false;
  255. }
  256. //切孔标识
  257. CutHole CHole = new CutHole();
  258. CHole.HoleName = CurrentPos[nLabelPos];
  259. //切孔位置
  260. SemPosition holePos = new SemPosition();
  261. holePos.X = (float)Convert.ToDouble(CurrentPos[nXPos]);
  262. holePos.Y = (float)Convert.ToDouble(CurrentPos[nYPos]);
  263. holePos.Z = (float)Convert.ToDouble(CurrentPos[nZPos]);
  264. holePos.M = (float)Convert.ToDouble(CurrentPos[nMPos]);
  265. holePos.T = (float)Convert.ToDouble(CurrentPos[nTPos]);
  266. holePos.R = (float)Convert.ToDouble(CurrentPos[nRPos]);
  267. CHole.Position = holePos;
  268. //默认切割点均参与测试
  269. CHole.SWITCH = true;
  270. //更新切孔链表
  271. this.ListCutHole.Add(CHole);
  272. }
  273. return true;
  274. }
  275. #endregion
  276. }
  277. }