using PaintDotNet; using PaintDotNet.Annotation.Enum; using PaintDotNet.Annotation.Measure; using PaintDotNet.Annotation.relationModel; using PaintDotNet.Measuring; using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Windows.Forms; using static PaintDotNet.Annotation.relationModel.MeasureRelationModel; namespace Metis.Measuring { internal class MeasureListStatisticalDataDialog : Form { /// /// 关系数据 /// private List list; /// /// 数据 /// private ListView listView = new ListView(); private Dictionary valuePairs; /// /// 主控件 /// private AppWorkspace appWorkspace; #region 控件 private GroupBox groupBox1; private Label label1; private ComboBox comboBox1; private Button button2; private ListView listView1; private Button button1; #endregion public MeasureListStatisticalDataDialog(AppWorkspace appWorkspace, ListView listView, int selectIndex) { this.list = InvariantData.GetMeasureRelations(); this.appWorkspace = appWorkspace; //this.listView = listView; InitializeComponent(); InitializeItemizedComboBox(); this.comboBox1.SelectedIndex = selectIndex; RefreshData(); } /// /// 初始化下拉 /// private void InitializeItemizedComboBox() { valuePairs = new Dictionary(); if (this.appWorkspace.ActiveDocumentWorkspace != null && this.appWorkspace.ActiveDocumentWorkspace.GraphicsList != null && this.appWorkspace.ActiveDocumentWorkspace.GraphicsList.Count > 0) { int count = this.appWorkspace.ActiveDocumentWorkspace.GraphicsList.Count; for (int i = 0; i < count; i++) { if (this.appWorkspace.ActiveDocumentWorkspace.GraphicsList[i].objectType == DrawClass.Measure) { if (!valuePairs.ContainsKey(this.appWorkspace.ActiveDocumentWorkspace.GraphicsList[i].drawToolType)) { valuePairs.Add(this.appWorkspace.ActiveDocumentWorkspace.GraphicsList[i].drawToolType, PdnResources.GetString("Menu.MeasureAction." + this.appWorkspace.ActiveDocumentWorkspace.GraphicsList[i].drawToolType.ToString() + ".Text")); } } } if (valuePairs.Count != 0) { this.comboBox1.DataSource = new BindingSource(valuePairs, null); this.comboBox1.DisplayMember = "Value"; this.comboBox1.ValueMember = "Key"; } } else { this.comboBox1.DataSource = null; } } private void InitializeComponent() { this.groupBox1 = new System.Windows.Forms.GroupBox(); this.button2 = new System.Windows.Forms.Button(); this.button1 = new System.Windows.Forms.Button(); this.comboBox1 = new System.Windows.Forms.ComboBox(); this.label1 = new System.Windows.Forms.Label(); this.listView1 = new System.Windows.Forms.ListView(); this.groupBox1.SuspendLayout(); this.SuspendLayout(); // // groupBox1 // this.groupBox1.Controls.Add(this.button2); this.groupBox1.Controls.Add(this.button1); this.groupBox1.Controls.Add(this.comboBox1); this.groupBox1.Controls.Add(this.label1); this.groupBox1.Location = new System.Drawing.Point(12, 12); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(604, 49); this.groupBox1.TabIndex = 0; this.groupBox1.TabStop = false; this.groupBox1.Text = "操作"; // // button2 // this.button2.Location = new System.Drawing.Point(507, 16); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(75, 23); this.button2.TabIndex = 3; this.button2.Text = "关闭"; this.button2.UseVisualStyleBackColor = true; this.button2.Click += new System.EventHandler(this.button2_Click); // // button1 // this.button1.Location = new System.Drawing.Point(415, 16); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 2; this.button1.Text = "导出"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // comboBox1 // this.comboBox1.FormattingEnabled = true; this.comboBox1.Location = new System.Drawing.Point(74, 19); this.comboBox1.Name = "comboBox1"; this.comboBox1.Size = new System.Drawing.Size(156, 20); this.comboBox1.TabIndex = 1; this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged); // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(36, 23); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(41, 12); this.label1.TabIndex = 0; this.label1.Text = "类型:"; // // listView1 // this.listView1.FullRowSelect = true; this.listView1.GridLines = true; this.listView1.HideSelection = false; this.listView1.Location = new System.Drawing.Point(12, 67); this.listView1.Name = "listView1"; this.listView1.Size = new System.Drawing.Size(604, 310); this.listView1.TabIndex = 1; this.listView1.UseCompatibleStateImageBehavior = false; // // MeasureListStatisticalDataDialog // this.ClientSize = new System.Drawing.Size(628, 389); this.Controls.Add(this.listView1); this.Controls.Add(this.groupBox1); this.MaximizeBox = false; this.MaximumSize = new System.Drawing.Size(644, 428); this.MinimizeBox = false; this.MinimumSize = new System.Drawing.Size(644, 428); this.Name = "MeasureListStatisticalDataDialog"; this.ShowIcon = false; this.ShowInTaskbar = false; this.groupBox1.ResumeLayout(false); this.groupBox1.PerformLayout(); this.ResumeLayout(false); } /// /// 改变选择测量方式刷新统计数据 /// /// /// private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { RefreshData(); } private void RefreshData() { InitializeItemizedHeader(); InitializeItemizedData(); //初始化头 this.listView1.Clear(); this.listView1.View = View.Details; for (int c = 0; c < this.listView.Columns.Count; c++) { ColumnHeader header0 = new ColumnHeader(); if (c == 1) { header0.Text = "计算"; header0.Width = 60; this.listView1.Columns.Add(header0); } if (c > 1) { header0.Text = listView.Columns[c].Text; header0.Width = 100; this.listView1.Columns.Add(header0); } } ArrayList arrayList1 = new ArrayList(); ArrayList arrayList2 = new ArrayList(); ArrayList arrayList3 = new ArrayList(); ArrayList arrayList4 = new ArrayList(); ArrayList arrayList5 = new ArrayList(); Regex r1 = new Regex(@"^[0-9]+$"); Regex r = new Regex(@"^([0-9]{1,}[.][0-9]*)$"); for (int j = 2; j < this.listView.Columns.Count; j++) { string maxValue = "0"; string minValue = "0"; string sumValue = "0"; string averageValue = "0"; for (int i = 0; i < this.listView.Items.Count; i++) { if (j > 1) { if (this.listView.Items[i].SubItems.Count > j && (r.Match(this.listView.Items[i].SubItems[j].Text).Success || r1.Match(this.listView.Items[i].SubItems[j].Text).Success) && i != 0) { // 最大值 if (this.listView.Items[i].SubItems.Count > j && Convert.ToDouble(this.listView.Items[i].SubItems[j].Text) > Convert.ToDouble(maxValue)) maxValue = this.listView.Items[i].SubItems[j].Text; // 最小值 if (this.listView.Items[i].SubItems.Count > j && Convert.ToDouble(this.listView.Items[i].SubItems[j].Text) < Convert.ToDouble(minValue)) minValue = this.listView.Items[i].SubItems[j].Text; // 和 if(this.listView.Items[i].SubItems.Count > j ) sumValue = (Convert.ToDouble(sumValue) + Convert.ToDouble(this.listView.Items[i].SubItems[j].Text)).ToString(); // 平均值 if (this.listView.Items[i].SubItems.Count > j) averageValue = (Convert.ToDouble(sumValue) / this.listView.Items.Count).ToString(); } else { if (this.listView.Items[i].SubItems.Count > j) maxValue = this.listView.Items[i].SubItems[j].Text; if (this.listView.Items[i].SubItems.Count > j) minValue = this.listView.Items[i].SubItems[j].Text; if (this.listView.Items[i].SubItems.Count > j) sumValue = this.listView.Items[i].SubItems[j].Text; if (this.listView.Items[i].SubItems.Count > j) averageValue = this.listView.Items[i].SubItems[j].Text; } } } arrayList1.Add(maxValue); arrayList2.Add(minValue); arrayList3.Add(averageValue); arrayList4.Add(sumValue); arrayList5.Add(this.listView.Items.Count); } for (int i = 0; i < 5; i++) { ListViewItem item = new ListViewItem(); string rowHeader = string.Empty; ArrayList arrayList = new ArrayList(); if (i == 0) { rowHeader = "最大值"; arrayList = arrayList1; } else if (i == 1) { rowHeader = "最小值"; arrayList = arrayList2; } else if (i == 2) { rowHeader = "平均值"; arrayList = arrayList3; } else if (i == 3) { rowHeader = "和"; arrayList = arrayList4; } else { rowHeader = "计数"; arrayList = arrayList5; } if (arrayList.Count > 0) { item.SubItems[0].Text = rowHeader; foreach (var subItem in arrayList) { item.SubItems.Add(subItem.ToString()); } int aaa = item.SubItems.Count; string aaaa = item.SubItems[0].Text; this.listView1.Items.Add(item); } } } /// /// 初始化分项数据的头 /// private void InitializeItemizedHeader() { if (this.appWorkspace.ActiveDocumentWorkspace != null && this.appWorkspace.ActiveDocumentWorkspace.GraphicsList != null && this.appWorkspace.ActiveDocumentWorkspace.GraphicsList.Count > 0) { if (this.comboBox1.SelectedIndex >= 0 && !string.IsNullOrEmpty(this.comboBox1.Text)) { if (valuePairs.Count == 0) return; DrawToolType type = (DrawToolType)valuePairs.ElementAt(this.comboBox1.SelectedIndex).Key; string text = PdnResources.GetString("Menu.MeasureAction." + type.ToString() + ".Text"); this.comboBox1.Text = text; //初始化头 this.listView.Clear(); this.listView.View = View.Details; ColumnHeader header0 = new ColumnHeader(); header0.Text = ""; header0.Width = 0; this.listView.Columns.Add(header0); ColumnHeader header = new ColumnHeader(); header.Text = "序号"; header.Width = 40; this.listView.Columns.Add(header); if (MeasureListDialog.dataNodes != null) { int count3 = MeasureListDialog.dataNodes.Nodes.Count; for (int i = 0; i < count3; i++) { int count4 = MeasureListDialog.dataNodes.Nodes[i].Nodes.Count; for (int j = 0; j < count4; j++) { if (text.Equals(MeasureListDialog.dataNodes.Nodes[i].Nodes[j].Text)) { foreach (TreeNode node2 in MeasureListDialog.dataNodes.Nodes[i].Nodes[j].Nodes) { ColumnHeader header1 = new ColumnHeader(); header1.Text = node2.Text; header1.Width = 100; this.listView.Columns.Add(header1); } } } } } else { int[] arr; InvariantData.measureRelationAttributes.TryGetValue(type, out arr); foreach (int v in arr) { if (v > 0) { ColumnHeader header1 = new ColumnHeader(); object value; InvariantData.measureAttributes.TryGetValue((MeasureAttributes)v, out value); header1.Text = value.ToString(); header1.Width = 100; this.listView.Columns.Add(header1); } } } } } } /// /// 初始化分项数据 /// private void InitializeItemizedData() { if (this.comboBox1.SelectedIndex >= 0) { if (this.appWorkspace.ActiveDocumentWorkspace != null) { if (this.appWorkspace.ActiveDocumentWorkspace.GraphicsList != null && this.appWorkspace.ActiveDocumentWorkspace.GraphicsList.Count > 0) { int count = this.appWorkspace.ActiveDocumentWorkspace.GraphicsList.Count; int no = 0; for (int i = 0; i < count; i++) { if (this.appWorkspace.ActiveDocumentWorkspace.GraphicsList[i].objectType == DrawClass.Measure) { if (this.appWorkspace.ActiveDocumentWorkspace.GraphicsList[i].drawToolType == (DrawToolType)valuePairs.ElementAt(this.comboBox1.SelectedIndex).Key) { Dictionary data = ((MeasureDrawObject)this.appWorkspace.ActiveDocumentWorkspace.GraphicsList[i]).GetData(); no++; ListViewItem item = new ListViewItem(); item.SubItems.Add("" + (no)); if (MeasureListDialog.dataNodes != null) { int count3 = MeasureListDialog.dataNodes.Nodes.Count; for (int a = 0; a < count3; a++) { int count4 = MeasureListDialog.dataNodes.Nodes[a].Nodes.Count; for (int j = 0; j < count4; j++) { if (this.comboBox1.Text == MeasureListDialog.dataNodes.Nodes[a].Nodes[j].Text) { foreach (TreeNode node2 in MeasureListDialog.dataNodes.Nodes[a].Nodes[j].Nodes) { foreach (var keys in data) { object v; InvariantData.measureAttributes.TryGetValue((MeasureAttributes)keys.Key, out v); if (v.ToString() == node2.Text) { item.SubItems.Add(keys.Value.ToString()); } } } } } } } else { foreach (var keys in data) { item.SubItems.Add(keys.Value.ToString()); } } this.listView.Items.Add(item); } } } } } } } /// /// 导出数据 /// /// /// private void button1_Click(object sender, EventArgs e) { if (this.listView1.Items.Count > 0) { SaveFileDialog exe = new SaveFileDialog(); exe.Filter = "Execl files (*.xlsx)|*.xlsx"; exe.FilterIndex = 0; exe.RestoreDirectory = true; exe.Title = "Export Excel File"; exe.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); exe.FileName = PdnResources.GetString("Menu.MeasurementListStatistics.Text") + DateTime.Now.ToString("yyyyMMddhhmmss"); DialogResult dr = exe.ShowDialog(); if (dr != DialogResult.OK) { return; } DataTable dtb = new DataTable(); for (int c = 0; c < this.listView1.Columns.Count; c++) { dtb.Columns.Add(listView1.Columns[c].Text); } for (int i = 0; i < this.listView1.Items.Count; i++) { DataRow dataRow = dtb.NewRow(); for (int j = 0; j < this.listView1.Columns.Count; j++) { dataRow[this.listView1.Columns[j].Text] = this.listView1.Items[i].SubItems[j].Text; } dtb.Rows.Add(dataRow); } List list = new List(); list.Add(dtb); this.appWorkspace.ExportDataToExcelWithProgress(list, exe.FileName, true, false, true); } else { MessageBox.Show(PdnResources.GetString("Menu.nodata.text") + "!"); } } private void button2_Click(object sender, EventArgs e) { this.Close(); } } }