MeasureFile.cs 12 KB

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