Newer
Older
GHFX_REFACTOR / Frmpipediameterstatis.cs
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using DevComponents.DotNetBar;
using GeoScene.Data;
using GeoScene.Globe;

namespace Cyberpipe
{
    public partial class Frmpipediameterstatis :Office2007Form
    {
        GSOGlobeControl globeControl1;
        List<string> pipeLayerNames ;

        static Frmpipediameterstatis frm;
//         private MainFrm.PageControlStuct _pageControl;

         public static void ShowForm(GSOGlobeControl _globeControl1, List<string> pipeLayers, int visibleId)
        {
            if (frm == null)
            {
                frm = new Frmpipediameterstatis(_globeControl1, pipeLayers, visibleId);
                frm.Show(_globeControl1.Parent);
            }
            else if (frm.WindowState == FormWindowState.Minimized)
            {
                frm.WindowState = FormWindowState.Normal;
            }            
        }

         public Frmpipediameterstatis(GSOGlobeControl _globeControl1, List<string> pipeLayers, int visibleId)
        {
            InitializeComponent();
            globeControl1 = _globeControl1;
            pipeLayerNames = pipeLayers;
//            _pageControl = pageControl;
            if (visibleId == 0)
            {
                buttonX9.Visible = false;
                buttonX8.Visible = true;
                buttonX8.Location = buttonX9.Location;
            }
            if (visibleId == 1)
            {
                buttonX8.Visible = false;
                buttonX9.Visible = true;
            }
        }
        /// <summary>
        /// 窗体初始化事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Frmpipediameterstatis_Load(object sender, EventArgs e)
        {            
            for (int i = 0; i < pipeLayerNames.Count; i++)
            {
                clbPipeLine.Items.Add(pipeLayerNames[i]);
            }            
        }
      
        /// <summary>
        /// 上限按钮事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX4_Click(object sender, EventArgs e)//上限
        {
            if (dataGridViewX2.SelectedCells.Count > 0)
            {
                DataGridViewRow row = dataGridViewX2.Rows[dataGridViewX2.SelectedCells[0].RowIndex];
                if (lstpipeRadius.SelectedItem != null)
                {
                    if (row.Cells["下限"].Value != null&& row.Cells["下限"].Value.ToString() != "")
                    {
                        double downRadius = Convert.ToDouble(row.Cells["下限"].Value);
                        double upRadius = Convert.ToDouble(lstpipeRadius.SelectedItem.ToString());
                        if (upRadius >= downRadius)
                        {
                            row.Cells["上限"].Value = upRadius.ToString();
                        }
                    }
                    else
                    {
                        row.Cells["上限"].Value = lstpipeRadius.SelectedItem.ToString();
                    }
                }
            }
        }
        /// <summary>
        /// 下限按钮事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX5_Click(object sender, EventArgs e)//下限
        {
            if (dataGridViewX2.SelectedCells.Count > 0)
            {
                DataGridViewRow row = dataGridViewX2.Rows[dataGridViewX2.SelectedCells[0].RowIndex];
                if (lstpipeRadius.SelectedItem != null)
                {
                    if (row.Cells["上限"].Value == null)
                    {
                        row.Cells["下限"].Value = lstpipeRadius.SelectedItem.ToString();
                    }
                    else
                    {
                        double downRadius = Convert.ToDouble(lstpipeRadius.SelectedItem.ToString());
                        double upRadius = Convert.ToDouble(row.Cells["上限"].Value);
                        if (upRadius >= downRadius)
                        {
                            row.Cells["下限"].Value = downRadius.ToString();
                        }
                    }
                }
            }
        }
        /// <summary>
        /// 添加行按钮事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX6_Click(object sender, EventArgs e)
        {
            DataGridViewRow row = new DataGridViewRow();
            int rowIndex = dataGridViewX2.Rows.Add(row);
            dataGridViewX2.Rows[rowIndex].Selected = true;
        }
        /// <summary>
        /// 删除行按钮事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX7_Click(object sender, EventArgs e)
        {
            if (dataGridViewX2.SelectedCells.Count > 0)
            { 
                DataGridViewRow row=dataGridViewX2.Rows[dataGridViewX2.SelectedCells[0].RowIndex];
                dataGridViewX2.Rows.Remove(row);
            }
        }
        
        Dictionary<string, double> dic = new Dictionary<string, double>();
        /// <summary>
        /// 全区域分段统计
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX8_Click(object sender, EventArgs e)
        {
            if (clbPipeLine.CheckedItems.Count == 0)
            {
                MessageBox.Show("请选择图层!");
            }
            if (dataGridViewX2.Rows.Count == 0)
            {
                MessageBox.Show("请添加分段!");
            }
            pipeDiameterStatis(null);
        }
        /// <summary>
        /// 给表格绑定数据
        /// </summary>
        /// <param name="polygon"></param>
        private void pipeDiameterStatis(GSOGeoPolygon3D polygon)
        {
            try
            {
                dic.Clear(); ;
                dataGridViewX1.Rows.Clear();
                FeatureStatisticsService service = new FeatureStatisticsService();
                for (int i = 0; i < clbPipeLine.CheckedItems.Count; i++) {

                    GSOLayer layer = globeControl1.Globe.Layers.GetLayerByCaption(clbPipeLine.CheckedItems[i].ToString());
                    for (int j = 0; j < dataGridViewX2.Rows.Count; j++) {
                  
                        object tempUp = dataGridViewX2.Rows[j].Cells["上限"].Value;
                        double? upLimit = null;
                        if (tempUp != null) upLimit = Convert.ToDouble(tempUp);

                        object tempDown = dataGridViewX2.Rows[j].Cells["下限"].Value;
                        double? downLimit = null;
                        if (tempDown != null) downLimit = Convert.ToDouble(tempDown);
                      if (upLimit == null && downLimit == null) continue;//空行过滤
                        //获取符合条件的管线
                      FeaturesClassfyResult result = service.GetPipesInfoByValueSection(polygon, layer, downLimit, upLimit, "管径_毫米");
                      DataGridViewRow row = new DataGridViewRow();
                      int index = dataGridViewX1.Rows.Add(row);
                      row = dataGridViewX1.Rows[index];
                      row.Cells["图层名称"].Value = layer.Caption;
                      row.Cells["管径范围"].Value = result.groupFieldValue;
                      row.Cells["条数"].Value = result.ncount;
                      row.Cells["总长度"].Value = Math.Round(result.sum, 2);
                      dic.Add(layer.Caption + "," + row.Cells["管径范围"].Value + "," + row.Cells["总长度"].Value, result.ncount);
                      
                    }
                }


                showPipeWidget();
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message);
                LogHelper.Error(ex.Message);
                LogError.PublishError(ex);
            }
        }
        /// <summary>
        /// 给图表绑定数据
        /// </summary>
        private void showPipeWidget()
        {
       

            chart1.Series.Clear();
            chart1.Series.Add("管线");

            // Set axis title
            chart1.ChartAreas["ChartArea1"].AxisX.Title = "管线类型";
            chart1.ChartAreas["ChartArea1"].AxisY.Title = "管线数量";
            chart1.ChartAreas["ChartArea1"].AxisX.Interval = 1;
            LabelStyle labeStyleAxisX = new LabelStyle();
            labeStyleAxisX.Angle = -45;
            labeStyleAxisX.Enabled = true;
            chart1.ChartAreas["ChartArea1"].AxisX.LabelStyle = labeStyleAxisX;
            chart1.Series["管线"].ChartType = SeriesChartType.Column;

            chart1.Series["管线"]["DrawingStyle"] = "Cylinder";

            chart1.Series["管线"].Points.DataBindXY(dic.Keys, dic.Values);
            for (int m = 0; m < dic.Values.Count; m++)
            {
                if (chart1.Series["管线"].Points[m].YValues[0].ToString() != "0")
                {
                    chart1.Series["管线"].Points[m].Label = chart1.Series[0].Points[m].YValues[0].ToString();
                }
                string axisLabel = chart1.Series["管线"].Points[m].AxisLabel;
                chart1.Series["管线"].Points[m].AxisLabel = axisLabel.Substring(0, axisLabel.IndexOf("管线"));
            }

        }
        /// <summary>
        /// 全选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < clbPipeLine.Items.Count; i++)
            {
                clbPipeLine.SetItemChecked(i, true);
            }
        }
        /// <summary>
        /// 反选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX2_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < clbPipeLine.Items.Count; i++)
            {
                clbPipeLine.SetItemChecked(i, !clbPipeLine.GetItemChecked(i));
                
            }
        }
        /// <summary>
        /// 清空
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX3_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < clbPipeLine.Items.Count; i++)
            {
                clbPipeLine.SetItemChecked(i, false);
            }
        }
        /// <summary>
        /// 导出Excel按钮事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExport_Click(object sender, EventArgs e)
        {
            if (dataGridViewX1.Rows.Count <= 0)
            {
                MessageBox.Show("统计结果为空,请重新统计之后再导出Excel!", "提示");
                return;
            }

            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "Excel files (*.xls)|*.xls";
            dlg.FilterIndex = 0;
            dlg.RestoreDirectory = true;
            //dlg.CreatePrompt = true;
            dlg.Title = "保存为Excel文件";
            dlg.FileName = DateTime.Now.ToString("yyyyMMdd") + ".xls";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                Stream myStream;
                myStream = dlg.OpenFile();
                StreamWriter sw = new StreamWriter(myStream, Encoding.GetEncoding(-0));
                string columnTitle = "";
                try
                {
                    sw.WriteLine("日期:" + DateTime.Now.ToString("yyyy-MM-dd"));

                    //写入列标题   
                    for (int i = 0; i < dataGridViewX1.ColumnCount; i++)
                    {
                        if (i > 0)
                        {
                            columnTitle += "\t";
                        }
                        columnTitle += dataGridViewX1.Columns[i].HeaderText;
                    }
                    sw.WriteLine(columnTitle);

                    //写入列内容   
                    for (int j = 0; j < dataGridViewX1.Rows.Count; j++)
                    {
                        string columnValue = "";
                        for (int k = 0; k < dataGridViewX1.Columns.Count; k++)
                        {
                            if (k > 0)
                            {
                                columnValue += "\t";
                            }
                            if (dataGridViewX1.Rows[j].Cells[k].Value == null)
                                columnValue += "";
                            else
                                columnValue += dataGridViewX1.Rows[j].Cells[k].Value.ToString().Trim();
                        }

                        sw.WriteLine(columnValue);
                    }
                    sw.Close();
                    myStream.Close();
                    if (MessageBox.Show("导出Excel文件成功!是否打开?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        Process.Start(dlg.FileName);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                finally
                {
                    sw.Close();
                    myStream.Close();
                }
            }


        }
        /// <summary>
        /// 绘制区域分段统计
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX9_Click(object sender, EventArgs e)
        {
            if (clbPipeLine.CheckedItems.Count == 0) {
                MessageBox.Show("请选择图层!");
                return;
            }
            if (dataGridViewX2.Rows.Count == 0) {
                MessageBox.Show("请添加分段!");
                return;
            }
            FeatureTools.RemoveFeatureFromName(globeControl1, "trackPolygon");
            globeControl1.Globe.Action = EnumAction3D.TrackPolygon;
            globeControl1.Globe.TrackPolygonTool.TrackMode = EnumTrackMode.SpaceTrack;
            globeControl1.TrackPolygonEndEvent += globeControl1_TrackPolygonEndEvent;
        }

        
        /// <summary>
        /// 球的跟踪面结束事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void globeControl1_TrackPolygonEndEvent(object sender, TrackPolygonEndEventArgs e)
        {
            if (clbPipeLine.CheckedItems.Count == 0)
            {
                MessageBox.Show("请选择图层!");
                return;
            }
            if (dataGridViewX2.Rows.Count == 0)
            {
                MessageBox.Show("请添加分段!");
                return;
            }
            if (e.Polygon == null) return;

            GSOGeoPolygon3D polygon = e.Polygon.Clone() as GSOGeoPolygon3D;
            polygon.AltitudeMode = EnumAltitudeMode.RelativeToGround;
            GSOFeature feature = new GSOFeature();
            feature.Geometry = polygon;
            feature.Name = "trackPolygon";
            globeControl1.Globe.MemoryLayer.AddFeature(feature);
            globeControl1.Globe.ClearLastTrackPolygon();
            globeControl1.TrackPolygonEndEvent -= globeControl1_TrackPolygonEndEvent;
            globeControl1.Globe.Action = EnumAction3D.ActionNull;
            pipeDiameterStatis(e.Polygon);

        }
       
        /// <summary>
        /// 窗体关闭事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Frmpipediameterstatis_FormClosing(object sender, FormClosingEventArgs e)
        {
            frm = null;            
            
            globeControl1.Globe.Action = EnumAction3D.ActionNull;
            globeControl1.TrackPolygonEndEvent -= globeControl1_TrackPolygonEndEvent;
            FeatureTools.RemoveFeatureFromName(globeControl1, "trackPolygon");

            globeControl1.Globe.MemoryLayer.RemoveAllFeature(); 
        }
        TextBox control; 
        private void dataGridViewX2_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (e.Control.GetType().BaseType.Name == "TextBox")
            {
                control = new TextBox();
                control = (TextBox)e.Control;
                control.KeyPress += control_KeyPress;
            }
        }
        /// <summary>
        /// 键盘按键按下事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void control_KeyPress(object sender, KeyPressEventArgs e)
        {
            TextBox text = sender as TextBox;
            if ((e.KeyChar < 48 || e.KeyChar > 57) && (e.KeyChar != 8) && e.KeyChar != 46)
            {
                e.Handled = true;
            }
            if (e.KeyChar == 46)                           //小数点
            {
                if (text.Text.Length <= 0)
                    e.Handled = true;   //小数点不能在第一位
                else                                             //处理不规则的小数点
                {
                    float f;
                    float oldf;
                    bool b1 = false, b2 = false;
                    b1 = float.TryParse(text.Text, out oldf);
                    b2 = float.TryParse(text.Text + e.KeyChar, out f);
                    if (!b2)
                    {
                        e.Handled = b1;
                    }
                }
            }
        }
    }
}