| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501 | 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    {        /// <summary>        /// 关系数据        /// </summary>        private List<MeasureRelationModel> list;        /// <summary>        /// 数据        /// </summary>        private ListView listView = new ListView();        private Dictionary<Enum, string> valuePairs;        /// <summary>        /// 主控件        /// </summary>        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();        }        /// <summary>        /// 初始化下拉        /// </summary>        private void InitializeItemizedComboBox()        {            valuePairs = new Dictionary<Enum, string>();            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);        }        /// <summary>        /// 改变选择测量方式刷新统计数据        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        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);                }            }        }        /// <summary>        /// 初始化分项数据的头        /// </summary>        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);                            }                        }                    }                }            }        }        /// <summary>        /// 初始化分项数据        /// </summary>        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<System.Enum, object> 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);                                }                            }                        }                    }                }            }        }        /// <summary>        /// 导出数据        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        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<DataTable> list = new List<DataTable>();                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();        }    }}
 |