MeasureFile1.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. #endregion
  56. /// <summary>
  57. /// 创建XML文件
  58. /// </summary>
  59. /// <returns>0:失败;1:成功;2:文件已存在</returns>
  60. public int CreateXml()
  61. {
  62. if (!File.Exists(this.FileName))
  63. {
  64. if( XmlManager.CreateXmlFile(this.FileName))
  65. {
  66. //创建一个新的
  67. return 1;
  68. }
  69. else
  70. {
  71. //创建失败
  72. return 0;
  73. }
  74. }
  75. else
  76. {
  77. //重新创建
  78. XmlManager.CreateXmlFile(this.FileName);
  79. return 2;
  80. }
  81. }
  82. //XML文件保存
  83. //样品孔存储xml文档
  84. public override void Serialize(bool isStoring, XmlDocument xml, XmlNode rootNode)
  85. {
  86. Slo sFile = new Slo();
  87. Slo sParam = new Slo();
  88. Collection<CutHole> sCutHoles = new Collection<CutHole>();
  89. foreach (CutHole hole in ListCutHole)
  90. {
  91. sCutHoles.addItem(hole);
  92. }
  93. xString FileName = new xString();
  94. xString FilePath = new xString();
  95. FileName.AssignValue(this.FileName);
  96. FilePath.AssignValue(this.FilePath);
  97. sFile.Register("FileName", FileName);
  98. sFile.Register("FilePath", FilePath);
  99. sFile.Register("Param", this.MParam);
  100. sFile.Register("ListCutHole", sCutHoles);
  101. if (isStoring)
  102. {
  103. sFile.Serialize(true, xml, rootNode);
  104. }
  105. else
  106. {
  107. sFile.Serialize(false, xml, rootNode);
  108. this.FileName = FileName.value();
  109. this.FilePath = FilePath.value();
  110. List<CutHole> tempCutHoleList = new List<CutHole>();
  111. for (int i = 0; i < sCutHoles.size(); i++)
  112. {
  113. tempCutHoleList.Add(sCutHoles.getItem(i));
  114. }
  115. if (tempCutHoleList.Count == sCutHoles.size())
  116. {
  117. ListCutHole.Clear();
  118. ListCutHole = tempCutHoleList;
  119. }
  120. }
  121. }
  122. //构造函数
  123. public MeasureFile()
  124. {
  125. Init();
  126. }
  127. public void Init()
  128. {
  129. this.ListCutHole = new List<CutHole>();
  130. this.FileName = @"";
  131. this.FilePath = @"";
  132. this.MParam = new MeasureParam();
  133. }
  134. #region 操作
  135. //新建
  136. public bool New()
  137. {
  138. int ret = CreateXml();
  139. if (ret > 0)
  140. {
  141. XmlDocument doc = new XmlDocument();
  142. doc.Load(this.FileName);//载入xml文件
  143. XmlNode root = doc.SelectSingleNode("XMLData");
  144. Serialize(true, doc, root);
  145. doc.Save(this.FileName);
  146. }
  147. // 设置路径为初始默认路径
  148. this.FileName = UNTITLED_FILE_NAME;
  149. // Ok, return TRUE
  150. return true;
  151. }
  152. //打开
  153. public void Open()
  154. {
  155. XmlDocument doc = new XmlDocument();
  156. doc.Load(this.FileName);//载入xml文件
  157. XmlNode root = doc.SelectSingleNode("XMLData");
  158. Serialize(false, doc, root);
  159. doc.Save(this.FileName);
  160. }
  161. //保存
  162. public bool Save()
  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. return true;
  219. }
  220. else
  221. {
  222. return false;
  223. }
  224. }
  225. //从文件生成切割孔信息
  226. public bool GetCutHolesFromFile(string a_FilePathName)
  227. {
  228. try
  229. {
  230. //清空原文件中的切孔
  231. this.ListCutHole.Clear();
  232. //弹出打开txt文件的对话矿
  233. a_FilePathName.Trim();
  234. if (string.IsNullOrEmpty(a_FilePathName))
  235. {
  236. //新建一个文件对话框
  237. OpenFileDialog pOpenFileDialog = new OpenFileDialog();
  238. //设置对话框标题
  239. pOpenFileDialog.Title = "打开电镜位置列表文件";
  240. //设置打开文件类型
  241. pOpenFileDialog.Filter = "txt文件(*.txt)|*.txt";
  242. //监测文件是否存在
  243. pOpenFileDialog.CheckFileExists = true;
  244. //文件打开后执行以下程序
  245. if (pOpenFileDialog.ShowDialog() == DialogResult.OK)
  246. {
  247. a_FilePathName = System.IO.Path.GetFullPath(pOpenFileDialog.FileName);
  248. //绝对路径
  249. CutHoleFilePath = a_FilePathName;
  250. }
  251. }
  252. //按行取出txt文件
  253. string[] lines = File.ReadAllLines(a_FilePathName, System.Text.Encoding.Default);
  254. LogManager.AddHardwareLog("read " + lines.Count().ToString() + "lines", true);
  255. //按现有的文件格式生成
  256. string posMode = lines[1];
  257. if (posMode.CompareTo(@"Absolute") != 0)
  258. {
  259. return false;
  260. }
  261. //解析切孔生成带有位置的切孔
  262. //验证数量是否正确
  263. int nNum = Convert.ToInt32(lines[3]);
  264. int nLines = lines.Length;
  265. if (nNum != (lines.Length - 4))
  266. {
  267. return false;
  268. }
  269. //行内标识的个数及位置
  270. string[] titles = lines[2].Split(',');
  271. int numPos = titles.Length;
  272. int nLabelPos = Array.IndexOf(titles, "Label");
  273. int nXPos = Array.IndexOf(titles, "X");
  274. int nYPos = Array.IndexOf(titles, "Y");
  275. int nZPos = Array.IndexOf(titles, "Z");
  276. int nTPos = Array.IndexOf(titles, "T");
  277. int nRPos = Array.IndexOf(titles, "R");
  278. int nMPos = Array.IndexOf(titles, "M");
  279. for (int i = 0; i < nNum; i++)
  280. {
  281. int currentLine = i + 4;
  282. string currentString = lines[currentLine];
  283. LogManager.AddHardwareLog(currentString, true);
  284. string[] CurrentPos = currentString.Split(',');
  285. int nCurrentPosNum = CurrentPos.Length;
  286. //当前行内的标识及位置个数不够
  287. if (nCurrentPosNum != numPos)
  288. {
  289. return false;
  290. }
  291. //切孔标识
  292. CutHole CHole = new CutHole();
  293. CHole.HoleName = CurrentPos[nLabelPos];
  294. //切孔位置
  295. SemPosition holePos = new SemPosition();
  296. holePos.X = Convert.ToSingle(CurrentPos[nXPos]);
  297. holePos.Y = Convert.ToSingle(CurrentPos[nYPos]);
  298. holePos.Z = Convert.ToSingle(CurrentPos[nZPos]);
  299. holePos.M = Convert.ToSingle(CurrentPos[nMPos]);
  300. holePos.T = Convert.ToSingle(CurrentPos[nTPos]);
  301. holePos.R = Convert.ToSingle(CurrentPos[nRPos]);
  302. CHole.Position = holePos;
  303. //默认切割点均参与测试
  304. CHole.SWITCH = true;
  305. //更新切孔链表
  306. this.ListCutHole.Add(CHole);
  307. LogManager.AddHardwareLog("X =" + CurrentPos[nXPos]
  308. + "Y =" + CurrentPos[nYPos]
  309. + "Z =" + CurrentPos[nZPos]
  310. + "M =" + CurrentPos[nTPos]
  311. + "T =" + CurrentPos[nRPos]
  312. + "R =" + CurrentPos[nMPos]
  313. , true);
  314. }
  315. return true;
  316. }
  317. catch (Exception ex)
  318. {
  319. LogManager.LogError(ex.Message);
  320. return false;
  321. }
  322. }
  323. //从点集合对象中生成切割孔信息
  324. public bool GetCutHolesFromAnalysisPosition(List<PointF> cutHolePointF)
  325. {
  326. //集合中的数量
  327. int nNum = Convert.ToInt32(cutHolePointF.Count);
  328. if (nNum == 0)
  329. {
  330. return false;
  331. }
  332. else
  333. {
  334. List<CutHole> tempCutHoleList = new List<CutHole>();
  335. for (int i = 0; i < nNum; i++)
  336. {
  337. //切孔标识
  338. CutHole CHole = new CutHole();
  339. CHole.HoleName = (i + 1).ToString();
  340. //切孔位置
  341. SemPosition holePos = new SemPosition();
  342. holePos.X = Convert.ToSingle(cutHolePointF[i].X);
  343. holePos.Y = Convert.ToSingle(cutHolePointF[i].Y);
  344. holePos.Z = Convert.ToSingle(0);
  345. holePos.M = Convert.ToSingle(0);
  346. holePos.T = Convert.ToSingle(0);
  347. holePos.R = Convert.ToSingle(0);
  348. CHole.Position = holePos;
  349. //默认切割点均参与测试
  350. CHole.SWITCH = true;
  351. //更新切孔链表
  352. tempCutHoleList.Add(CHole);
  353. LogManager.AddHardwareLog("X =" + cutHolePointF[i].X
  354. + "Y =" + cutHolePointF[i].Y
  355. + "Z =" + 0
  356. + "M =" + 0
  357. + "T =" + 0
  358. + "R =" + 0
  359. , true);
  360. }
  361. if (tempCutHoleList.Count > 0)
  362. {
  363. this.ListCutHole.Clear();
  364. this.ListCutHole = tempCutHoleList;
  365. }
  366. }
  367. return true;
  368. }
  369. #endregion
  370. }
  371. }