MeasureFile.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. return true;
  186. }
  187. //另存为
  188. public bool SaveAs()
  189. {
  190. //打开保存文件的对话框
  191. this.FileName.Trim();
  192. if (!string.IsNullOrEmpty(this.FileName))
  193. {
  194. this.FilePath = System.IO.Path.GetDirectoryName(this.FileName);
  195. }
  196. SaveFileDialog sfd = new SaveFileDialog();
  197. sfd.Title = "保存测量文件";
  198. sfd.Filter = "测量文件(*.msf)|*.msf";
  199. if (Directory.Exists(this.FilePath))
  200. {
  201. sfd.InitialDirectory = this.FilePath;
  202. }
  203. if (sfd.ShowDialog() == DialogResult.OK)
  204. {
  205. this.FileName = sfd.FileName.ToString(); //获得文件路径
  206. FilePath = System.IO.Path.GetDirectoryName(this.FileName);
  207. //创建一个新的文件
  208. XmlDocument doc = new XmlDocument();
  209. if (CreateXml() == 0)//创建该文件?
  210. {
  211. return false;
  212. }
  213. doc.Load(this.FileName);
  214. XmlNode root = doc.SelectSingleNode("XMLData");
  215. Serialize(true, doc, root);
  216. doc.Save(this.FileName);
  217. return true;
  218. }
  219. else
  220. {
  221. return false;
  222. }
  223. }
  224. //从文件生成切割孔信息
  225. public bool GetCutHolesFromFile(string a_FilePathName)
  226. {
  227. try
  228. {
  229. //清空原文件中的切孔
  230. this.ListCutHole.Clear();
  231. //弹出打开txt文件的对话矿
  232. a_FilePathName.Trim();
  233. if (string.IsNullOrEmpty(a_FilePathName))
  234. {
  235. //新建一个文件对话框
  236. OpenFileDialog pOpenFileDialog = new OpenFileDialog();
  237. //设置对话框标题
  238. pOpenFileDialog.Title = "打开电镜位置列表文件";
  239. //设置打开文件类型
  240. pOpenFileDialog.Filter = "txt文件(*.txt)|*.txt";
  241. //监测文件是否存在
  242. pOpenFileDialog.CheckFileExists = true;
  243. //文件打开后执行以下程序
  244. if (pOpenFileDialog.ShowDialog() == DialogResult.OK)
  245. {
  246. a_FilePathName = System.IO.Path.GetFullPath(pOpenFileDialog.FileName);
  247. //绝对路径
  248. CutHoleFilePath = a_FilePathName;
  249. }
  250. }
  251. //按行取出txt文件
  252. string[] lines = File.ReadAllLines(a_FilePathName, System.Text.Encoding.Default);
  253. LogManager.AddHardwareLog("read " + lines.Count().ToString() + "lines", true);
  254. //按现有的文件格式生成
  255. string posMode = lines[1];
  256. if (posMode.CompareTo(@"Absolute") != 0)
  257. {
  258. return false;
  259. }
  260. //解析切孔生成带有位置的切孔
  261. //验证数量是否正确
  262. int nNum = Convert.ToInt32(lines[3]);
  263. int nLines = lines.Length;
  264. if (nNum != (lines.Length - 4))
  265. {
  266. return false;
  267. }
  268. //行内标识的个数及位置
  269. string[] titles = lines[2].Split(',');
  270. int numPos = titles.Length;
  271. int nLabelPos = Array.IndexOf(titles, "Label");
  272. int nXPos = Array.IndexOf(titles, "X");
  273. int nYPos = Array.IndexOf(titles, "Y");
  274. int nZPos = Array.IndexOf(titles, "Z");
  275. int nTPos = Array.IndexOf(titles, "T");
  276. int nRPos = Array.IndexOf(titles, "R");
  277. int nMPos = Array.IndexOf(titles, "M");
  278. for (int i = 0; i < nNum; i++)
  279. {
  280. int currentLine = i + 4;
  281. string currentString = lines[currentLine];
  282. LogManager.AddHardwareLog(currentString, true);
  283. string[] CurrentPos = currentString.Split(',');
  284. int nCurrentPosNum = CurrentPos.Length;
  285. //当前行内的标识及位置个数不够
  286. if (nCurrentPosNum != numPos)
  287. {
  288. return false;
  289. }
  290. //切孔标识
  291. CutHole CHole = new CutHole();
  292. CHole.HoleName = CurrentPos[nLabelPos];
  293. //切孔位置
  294. SemPosition holePos = new SemPosition();
  295. holePos.X = Convert.ToSingle(CurrentPos[nXPos]);
  296. holePos.Y = Convert.ToSingle(CurrentPos[nYPos]);
  297. holePos.Z = Convert.ToSingle(CurrentPos[nZPos]);
  298. holePos.M = Convert.ToSingle(CurrentPos[nMPos]);
  299. holePos.T = Convert.ToSingle(CurrentPos[nTPos]);
  300. holePos.R = Convert.ToSingle(CurrentPos[nRPos]);
  301. CHole.Position = holePos;
  302. //默认切割点均参与测试
  303. CHole.SWITCH = true;
  304. //更新切孔链表
  305. this.ListCutHole.Add(CHole);
  306. LogManager.AddHardwareLog("X =" + CurrentPos[nXPos]
  307. + "Y =" + CurrentPos[nYPos]
  308. + "Z =" + CurrentPos[nZPos]
  309. + "M =" + CurrentPos[nTPos]
  310. + "T =" + CurrentPos[nRPos]
  311. + "R =" + CurrentPos[nMPos]
  312. , true);
  313. }
  314. return true;
  315. }
  316. catch (Exception ex)
  317. {
  318. LogManager.LogError(ex.Message);
  319. return false;
  320. }
  321. }
  322. //从点集合对象中生成切割孔信息
  323. public bool GetCutHolesFromAnalysisPosition(List<PointF> cutHolePointF)
  324. {
  325. //集合中的数量
  326. int nNum = Convert.ToInt32(cutHolePointF.Count);
  327. if (nNum == 0)
  328. {
  329. return false;
  330. }
  331. else
  332. {
  333. List<CutHole> tempCutHoleList = new List<CutHole>();
  334. for (int i = 0; i < nNum; i++)
  335. {
  336. //切孔标识
  337. CutHole CHole = new CutHole();
  338. CHole.HoleName = (i + 1).ToString();
  339. //切孔位置
  340. SemPosition holePos = new SemPosition();
  341. holePos.X = Convert.ToSingle(cutHolePointF[i].X);
  342. holePos.Y = Convert.ToSingle(cutHolePointF[i].Y);
  343. holePos.Z = Convert.ToSingle(0);
  344. holePos.M = Convert.ToSingle(0);
  345. holePos.T = Convert.ToSingle(0);
  346. holePos.R = Convert.ToSingle(0);
  347. CHole.Position = holePos;
  348. //默认切割点均参与测试
  349. CHole.SWITCH = true;
  350. //更新切孔链表
  351. tempCutHoleList.Add(CHole);
  352. LogManager.AddHardwareLog("X =" + cutHolePointF[i].X
  353. + "Y =" + cutHolePointF[i].Y
  354. + "Z =" + 0
  355. + "M =" + 0
  356. + "T =" + 0
  357. + "R =" + 0
  358. , true);
  359. }
  360. if (tempCutHoleList.Count > 0)
  361. {
  362. this.ListCutHole.Clear();
  363. this.ListCutHole = tempCutHoleList;
  364. }
  365. }
  366. return true;
  367. }
  368. #endregion
  369. }
  370. }