MeasureFile.cs 13 KB

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