MeasureListStatisticalDataDialog.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. using PaintDotNet;
  2. using PaintDotNet.Annotation.Enum;
  3. using PaintDotNet.Annotation.Measure;
  4. using PaintDotNet.Annotation.relationModel;
  5. using PaintDotNet.Measuring;
  6. using System;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using System.ComponentModel;
  10. using System.Data;
  11. using System.Drawing;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Text;
  15. using System.Text.RegularExpressions;
  16. using System.Threading.Tasks;
  17. using System.Windows.Forms;
  18. using static PaintDotNet.Annotation.relationModel.MeasureRelationModel;
  19. namespace Metis.Measuring
  20. {
  21. internal class MeasureListStatisticalDataDialog : Form
  22. {
  23. /// <summary>
  24. /// 关系数据
  25. /// </summary>
  26. private List<MeasureRelationModel> list;
  27. /// <summary>
  28. /// 数据
  29. /// </summary>
  30. private ListView listView = new ListView();
  31. private Dictionary<Enum, string> valuePairs;
  32. /// <summary>
  33. /// 主控件
  34. /// </summary>
  35. private AppWorkspace appWorkspace;
  36. #region 控件
  37. private GroupBox groupBox1;
  38. private Label label1;
  39. private ComboBox comboBox1;
  40. private Button button2;
  41. private ListView listView1;
  42. private Button button1;
  43. #endregion
  44. public MeasureListStatisticalDataDialog(AppWorkspace appWorkspace, ListView listView, int selectIndex)
  45. {
  46. this.list = InvariantData.GetMeasureRelations();
  47. this.appWorkspace = appWorkspace;
  48. //this.listView = listView;
  49. InitializeComponent();
  50. InitializeItemizedComboBox();
  51. this.comboBox1.SelectedIndex = selectIndex;
  52. RefreshData();
  53. }
  54. /// <summary>
  55. /// 初始化下拉
  56. /// </summary>
  57. private void InitializeItemizedComboBox()
  58. {
  59. valuePairs = new Dictionary<Enum, string>();
  60. if (this.appWorkspace.ActiveDocumentWorkspace != null
  61. && this.appWorkspace.ActiveDocumentWorkspace.GraphicsList != null
  62. && this.appWorkspace.ActiveDocumentWorkspace.GraphicsList.Count > 0)
  63. {
  64. int count = this.appWorkspace.ActiveDocumentWorkspace.GraphicsList.Count;
  65. for (int i = 0; i < count; i++)
  66. {
  67. if (this.appWorkspace.ActiveDocumentWorkspace.GraphicsList[i].objectType == DrawClass.Measure)
  68. {
  69. if (!valuePairs.ContainsKey(this.appWorkspace.ActiveDocumentWorkspace.GraphicsList[i].drawToolType))
  70. {
  71. valuePairs.Add(this.appWorkspace.ActiveDocumentWorkspace.GraphicsList[i].drawToolType,
  72. PdnResources.GetString("Menu.MeasureAction." + this.appWorkspace.ActiveDocumentWorkspace.GraphicsList[i].drawToolType.ToString() + ".Text"));
  73. }
  74. }
  75. }
  76. if (valuePairs.Count != 0)
  77. {
  78. this.comboBox1.DataSource = new BindingSource(valuePairs, null);
  79. this.comboBox1.DisplayMember = "Value";
  80. this.comboBox1.ValueMember = "Key";
  81. }
  82. }
  83. else
  84. {
  85. this.comboBox1.DataSource = null;
  86. }
  87. }
  88. private void InitializeComponent()
  89. {
  90. this.groupBox1 = new System.Windows.Forms.GroupBox();
  91. this.button2 = new System.Windows.Forms.Button();
  92. this.button1 = new System.Windows.Forms.Button();
  93. this.comboBox1 = new System.Windows.Forms.ComboBox();
  94. this.label1 = new System.Windows.Forms.Label();
  95. this.listView1 = new System.Windows.Forms.ListView();
  96. this.groupBox1.SuspendLayout();
  97. this.SuspendLayout();
  98. //
  99. // groupBox1
  100. //
  101. this.groupBox1.Controls.Add(this.button2);
  102. this.groupBox1.Controls.Add(this.button1);
  103. this.groupBox1.Controls.Add(this.comboBox1);
  104. this.groupBox1.Controls.Add(this.label1);
  105. this.groupBox1.Location = new System.Drawing.Point(12, 12);
  106. this.groupBox1.Name = "groupBox1";
  107. this.groupBox1.Size = new System.Drawing.Size(604, 49);
  108. this.groupBox1.TabIndex = 0;
  109. this.groupBox1.TabStop = false;
  110. this.groupBox1.Text = "操作";
  111. //
  112. // button2
  113. //
  114. this.button2.Location = new System.Drawing.Point(507, 16);
  115. this.button2.Name = "button2";
  116. this.button2.Size = new System.Drawing.Size(75, 23);
  117. this.button2.TabIndex = 3;
  118. this.button2.Text = "关闭";
  119. this.button2.UseVisualStyleBackColor = true;
  120. this.button2.Click += new System.EventHandler(this.button2_Click);
  121. //
  122. // button1
  123. //
  124. this.button1.Location = new System.Drawing.Point(415, 16);
  125. this.button1.Name = "button1";
  126. this.button1.Size = new System.Drawing.Size(75, 23);
  127. this.button1.TabIndex = 2;
  128. this.button1.Text = "导出";
  129. this.button1.UseVisualStyleBackColor = true;
  130. this.button1.Click += new System.EventHandler(this.button1_Click);
  131. //
  132. // comboBox1
  133. //
  134. this.comboBox1.FormattingEnabled = true;
  135. this.comboBox1.Location = new System.Drawing.Point(74, 19);
  136. this.comboBox1.Name = "comboBox1";
  137. this.comboBox1.Size = new System.Drawing.Size(156, 20);
  138. this.comboBox1.TabIndex = 1;
  139. this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
  140. //
  141. // label1
  142. //
  143. this.label1.AutoSize = true;
  144. this.label1.Location = new System.Drawing.Point(36, 23);
  145. this.label1.Name = "label1";
  146. this.label1.Size = new System.Drawing.Size(41, 12);
  147. this.label1.TabIndex = 0;
  148. this.label1.Text = "类型:";
  149. //
  150. // listView1
  151. //
  152. this.listView1.FullRowSelect = true;
  153. this.listView1.GridLines = true;
  154. this.listView1.HideSelection = false;
  155. this.listView1.Location = new System.Drawing.Point(12, 67);
  156. this.listView1.Name = "listView1";
  157. this.listView1.Size = new System.Drawing.Size(604, 310);
  158. this.listView1.TabIndex = 1;
  159. this.listView1.UseCompatibleStateImageBehavior = false;
  160. //
  161. // MeasureListStatisticalDataDialog
  162. //
  163. this.ClientSize = new System.Drawing.Size(628, 389);
  164. this.Controls.Add(this.listView1);
  165. this.Controls.Add(this.groupBox1);
  166. this.MaximizeBox = false;
  167. this.MaximumSize = new System.Drawing.Size(644, 428);
  168. this.MinimizeBox = false;
  169. this.MinimumSize = new System.Drawing.Size(644, 428);
  170. this.Name = "MeasureListStatisticalDataDialog";
  171. this.ShowIcon = false;
  172. this.ShowInTaskbar = false;
  173. this.groupBox1.ResumeLayout(false);
  174. this.groupBox1.PerformLayout();
  175. this.ResumeLayout(false);
  176. }
  177. /// <summary>
  178. /// 改变选择测量方式刷新统计数据
  179. /// </summary>
  180. /// <param name="sender"></param>
  181. /// <param name="e"></param>
  182. private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
  183. {
  184. RefreshData();
  185. }
  186. private void RefreshData()
  187. {
  188. InitializeItemizedHeader();
  189. InitializeItemizedData();
  190. //初始化头
  191. this.listView1.Clear();
  192. this.listView1.View = View.Details;
  193. for (int c = 0; c < this.listView.Columns.Count; c++)
  194. {
  195. ColumnHeader header0 = new ColumnHeader();
  196. if (c == 1)
  197. {
  198. header0.Text = "计算";
  199. header0.Width = 60;
  200. this.listView1.Columns.Add(header0);
  201. }
  202. if (c > 1)
  203. {
  204. header0.Text = listView.Columns[c].Text;
  205. header0.Width = 100;
  206. this.listView1.Columns.Add(header0);
  207. }
  208. }
  209. ArrayList arrayList1 = new ArrayList();
  210. ArrayList arrayList2 = new ArrayList();
  211. ArrayList arrayList3 = new ArrayList();
  212. ArrayList arrayList4 = new ArrayList();
  213. ArrayList arrayList5 = new ArrayList();
  214. Regex r1 = new Regex(@"^[0-9]+$");
  215. Regex r = new Regex(@"^([0-9]{1,}[.][0-9]*)$");
  216. for (int j = 2; j < this.listView.Columns.Count; j++)
  217. {
  218. string maxValue = "0";
  219. string minValue = "0";
  220. string sumValue = "0";
  221. string averageValue = "0";
  222. for (int i = 0; i < this.listView.Items.Count; i++)
  223. {
  224. if (j > 1)
  225. {
  226. if (this.listView.Items[i].SubItems.Count > j && (r.Match(this.listView.Items[i].SubItems[j].Text).Success
  227. || r1.Match(this.listView.Items[i].SubItems[j].Text).Success) && i != 0)
  228. {
  229. // 最大值
  230. if (this.listView.Items[i].SubItems.Count > j && Convert.ToDouble(this.listView.Items[i].SubItems[j].Text) > Convert.ToDouble(maxValue))
  231. maxValue = this.listView.Items[i].SubItems[j].Text;
  232. // 最小值
  233. if (this.listView.Items[i].SubItems.Count > j && Convert.ToDouble(this.listView.Items[i].SubItems[j].Text) < Convert.ToDouble(minValue))
  234. minValue = this.listView.Items[i].SubItems[j].Text;
  235. // 和
  236. if(this.listView.Items[i].SubItems.Count > j )
  237. sumValue = (Convert.ToDouble(sumValue) + Convert.ToDouble(this.listView.Items[i].SubItems[j].Text)).ToString();
  238. // 平均值
  239. if (this.listView.Items[i].SubItems.Count > j)
  240. averageValue = (Convert.ToDouble(sumValue) / this.listView.Items.Count).ToString();
  241. }
  242. else
  243. {
  244. if (this.listView.Items[i].SubItems.Count > j)
  245. maxValue = this.listView.Items[i].SubItems[j].Text;
  246. if (this.listView.Items[i].SubItems.Count > j)
  247. minValue = this.listView.Items[i].SubItems[j].Text;
  248. if (this.listView.Items[i].SubItems.Count > j)
  249. sumValue = this.listView.Items[i].SubItems[j].Text;
  250. if (this.listView.Items[i].SubItems.Count > j)
  251. averageValue = this.listView.Items[i].SubItems[j].Text;
  252. }
  253. }
  254. }
  255. arrayList1.Add(maxValue);
  256. arrayList2.Add(minValue);
  257. arrayList3.Add(averageValue);
  258. arrayList4.Add(sumValue);
  259. arrayList5.Add(this.listView.Items.Count);
  260. }
  261. for (int i = 0; i < 5; i++)
  262. {
  263. ListViewItem item = new ListViewItem();
  264. string rowHeader = string.Empty;
  265. ArrayList arrayList = new ArrayList();
  266. if (i == 0)
  267. {
  268. rowHeader = "最大值";
  269. arrayList = arrayList1;
  270. }
  271. else if (i == 1)
  272. {
  273. rowHeader = "最小值";
  274. arrayList = arrayList2;
  275. }
  276. else if (i == 2)
  277. {
  278. rowHeader = "平均值";
  279. arrayList = arrayList3;
  280. }
  281. else if (i == 3)
  282. {
  283. rowHeader = "和";
  284. arrayList = arrayList4;
  285. }
  286. else
  287. {
  288. rowHeader = "计数";
  289. arrayList = arrayList5;
  290. }
  291. if (arrayList.Count > 0)
  292. {
  293. item.SubItems[0].Text = rowHeader;
  294. foreach (var subItem in arrayList)
  295. {
  296. item.SubItems.Add(subItem.ToString());
  297. }
  298. int aaa = item.SubItems.Count;
  299. string aaaa = item.SubItems[0].Text;
  300. this.listView1.Items.Add(item);
  301. }
  302. }
  303. }
  304. /// <summary>
  305. /// 初始化分项数据的头
  306. /// </summary>
  307. private void InitializeItemizedHeader()
  308. {
  309. if (this.appWorkspace.ActiveDocumentWorkspace != null
  310. && this.appWorkspace.ActiveDocumentWorkspace.GraphicsList != null
  311. && this.appWorkspace.ActiveDocumentWorkspace.GraphicsList.Count > 0)
  312. {
  313. if (this.comboBox1.SelectedIndex >= 0 && !string.IsNullOrEmpty(this.comboBox1.Text))
  314. {
  315. if (valuePairs.Count == 0)
  316. return;
  317. DrawToolType type = (DrawToolType)valuePairs.ElementAt(this.comboBox1.SelectedIndex).Key;
  318. string text = PdnResources.GetString("Menu.MeasureAction." + type.ToString() + ".Text");
  319. this.comboBox1.Text = text;
  320. //初始化头
  321. this.listView.Clear();
  322. this.listView.View = View.Details;
  323. ColumnHeader header0 = new ColumnHeader();
  324. header0.Text = "";
  325. header0.Width = 0;
  326. this.listView.Columns.Add(header0);
  327. ColumnHeader header = new ColumnHeader();
  328. header.Text = "序号";
  329. header.Width = 40;
  330. this.listView.Columns.Add(header);
  331. if (MeasureListDialog.dataNodes != null)
  332. {
  333. int count3 = MeasureListDialog.dataNodes.Nodes.Count;
  334. for (int i = 0; i < count3; i++)
  335. {
  336. int count4 = MeasureListDialog.dataNodes.Nodes[i].Nodes.Count;
  337. for (int j = 0; j < count4; j++)
  338. {
  339. if (text.Equals(MeasureListDialog.dataNodes.Nodes[i].Nodes[j].Text))
  340. {
  341. foreach (TreeNode node2 in MeasureListDialog.dataNodes.Nodes[i].Nodes[j].Nodes)
  342. {
  343. ColumnHeader header1 = new ColumnHeader();
  344. header1.Text = node2.Text;
  345. header1.Width = 100;
  346. this.listView.Columns.Add(header1);
  347. }
  348. }
  349. }
  350. }
  351. }
  352. else
  353. {
  354. int[] arr;
  355. InvariantData.measureRelationAttributes.TryGetValue(type, out arr);
  356. foreach (int v in arr)
  357. {
  358. if (v > 0)
  359. {
  360. ColumnHeader header1 = new ColumnHeader();
  361. object value;
  362. InvariantData.measureAttributes.TryGetValue((MeasureAttributes)v, out value);
  363. header1.Text = value.ToString();
  364. header1.Width = 100;
  365. this.listView.Columns.Add(header1);
  366. }
  367. }
  368. }
  369. }
  370. }
  371. }
  372. /// <summary>
  373. /// 初始化分项数据
  374. /// </summary>
  375. private void InitializeItemizedData()
  376. {
  377. if (this.comboBox1.SelectedIndex >= 0)
  378. {
  379. if (this.appWorkspace.ActiveDocumentWorkspace != null)
  380. {
  381. if (this.appWorkspace.ActiveDocumentWorkspace.GraphicsList != null
  382. && this.appWorkspace.ActiveDocumentWorkspace.GraphicsList.Count > 0)
  383. {
  384. int count = this.appWorkspace.ActiveDocumentWorkspace.GraphicsList.Count;
  385. int no = 0;
  386. for (int i = 0; i < count; i++)
  387. {
  388. if (this.appWorkspace.ActiveDocumentWorkspace.GraphicsList[i].objectType == DrawClass.Measure)
  389. {
  390. if (this.appWorkspace.ActiveDocumentWorkspace.GraphicsList[i].drawToolType == (DrawToolType)valuePairs.ElementAt(this.comboBox1.SelectedIndex).Key)
  391. {
  392. Dictionary<System.Enum, object> data = ((MeasureDrawObject)this.appWorkspace.ActiveDocumentWorkspace.GraphicsList[i]).GetData();
  393. no++;
  394. ListViewItem item = new ListViewItem();
  395. item.SubItems.Add("" + (no));
  396. if (MeasureListDialog.dataNodes != null)
  397. {
  398. int count3 = MeasureListDialog.dataNodes.Nodes.Count;
  399. for (int a = 0; a < count3; a++)
  400. {
  401. int count4 = MeasureListDialog.dataNodes.Nodes[a].Nodes.Count;
  402. for (int j = 0; j < count4; j++)
  403. {
  404. if (this.comboBox1.Text == MeasureListDialog.dataNodes.Nodes[a].Nodes[j].Text)
  405. {
  406. foreach (TreeNode node2 in MeasureListDialog.dataNodes.Nodes[a].Nodes[j].Nodes)
  407. {
  408. foreach (var keys in data)
  409. {
  410. object v;
  411. InvariantData.measureAttributes.TryGetValue((MeasureAttributes)keys.Key, out v);
  412. if (v.ToString() == node2.Text)
  413. {
  414. item.SubItems.Add(keys.Value.ToString());
  415. }
  416. }
  417. }
  418. }
  419. }
  420. }
  421. }
  422. else
  423. {
  424. foreach (var keys in data)
  425. {
  426. item.SubItems.Add(keys.Value.ToString());
  427. }
  428. }
  429. this.listView.Items.Add(item);
  430. }
  431. }
  432. }
  433. }
  434. }
  435. }
  436. }
  437. /// <summary>
  438. /// 导出数据
  439. /// </summary>
  440. /// <param name="sender"></param>
  441. /// <param name="e"></param>
  442. private void button1_Click(object sender, EventArgs e)
  443. {
  444. if (this.listView1.Items.Count > 0)
  445. {
  446. SaveFileDialog exe = new SaveFileDialog();
  447. exe.Filter = "Execl files (*.xlsx)|*.xlsx";
  448. exe.FilterIndex = 0;
  449. exe.RestoreDirectory = true;
  450. exe.Title = "Export Excel File";
  451. exe.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
  452. exe.FileName = PdnResources.GetString("Menu.MeasurementListStatistics.Text") + DateTime.Now.ToString("yyyyMMddhhmmss");
  453. DialogResult dr = exe.ShowDialog();
  454. if (dr != DialogResult.OK)
  455. {
  456. return;
  457. }
  458. DataTable dtb = new DataTable();
  459. for (int c = 0; c < this.listView1.Columns.Count; c++)
  460. {
  461. dtb.Columns.Add(listView1.Columns[c].Text);
  462. }
  463. for (int i = 0; i < this.listView1.Items.Count; i++)
  464. {
  465. DataRow dataRow = dtb.NewRow();
  466. for (int j = 0; j < this.listView1.Columns.Count; j++)
  467. {
  468. dataRow[this.listView1.Columns[j].Text] = this.listView1.Items[i].SubItems[j].Text;
  469. }
  470. dtb.Rows.Add(dataRow);
  471. }
  472. List<DataTable> list = new List<DataTable>();
  473. list.Add(dtb);
  474. this.appWorkspace.ExportDataToExcelWithProgress(list, exe.FileName, true, false, true);
  475. }
  476. else
  477. {
  478. MessageBox.Show(PdnResources.GetString("Menu.nodata.text") + "!");
  479. }
  480. }
  481. private void button2_Click(object sender, EventArgs e)
  482. {
  483. this.Close();
  484. }
  485. }
  486. }