MeasureFile.cs 14 KB

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