Newer
Older
GHFX_REFACTOR / FrmAccessoriesgather.cs
wxn on 29 Nov 2016 14 KB FeatureTools方法整理
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
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 FrmAccessoriesgather : Office2007Form
    {
        GSOGlobeControl globeControl1;
        List<string> instrumenLayerNames;
        //string[] accessStrs = null;
        Dictionary<string, double> dic = new Dictionary<string, double>();

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

        public FrmAccessoriesgather(GSOGlobeControl globeControl, List<string> instrumentlist, int visibleId)
        {
            InitializeComponent();
            globeControl1 = globeControl;
            instrumenLayerNames = instrumentlist;
            if (visibleId == 0)
            {
                buttonX9.Visible = false;
                buttonX8.Visible = true;
                buttonX8.Location = buttonX9.Location;
            }else{
                buttonX8.Visible = false;
                buttonX9.Visible = true;
            }
        }
        /// <summary>
        /// 窗体初始化事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmAccessoriesgather_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < instrumenLayerNames.Count; i++)
            {
                if (instrumenLayerNames[i].EndsWith("附属物"))
                {
                    clbPipeLine2.Items.Add(instrumenLayerNames[i]);
                }
            }
           
        }
       
       
        /// <summary>
        /// 绘制区域完成事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void globeControl1_TrackPolygonEndEvent(object sender, TrackPolygonEndEventArgs e)
        {
            if (e.Polygon != null)
            {
                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);
                pipeDiameterStatisNew(e.Polygon, clbPipeLine2.Text);
            }
        }

        /// <summary>
        /// 全区域分类汇总按钮事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX8_Click(object sender, EventArgs e)
        {
           if (clbPipeLine2.Text!="")
           {
                globeControl1.Globe.Action = EnumAction3D.ActionNull;
                pipeDiameterStatisNew(null, clbPipeLine2.Text); //pipeDiameterStatis(null);
            }
            else
            {
               MessageBox.Show("请选择图层!");
            }
        }
        /// <summary>
        /// 汇总区域分类汇总事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX9_Click(object sender, EventArgs e)
        {
            if (clbPipeLine2.Text!="")
            {
                FeatureTools.RemoveFeatureFromName(globeControl1, "trackPolygon");
                globeControl1.Globe.Action = EnumAction3D.TrackPolygon;
                globeControl1.Globe.TrackPolygonTool.TrackMode = EnumTrackMode.SpaceTrack;  
                globeControl1.TrackPolygonEndEvent += globeControl1_TrackPolygonEndEvent;
            }
            else
            {
                MessageBox.Show("请选择图层!");
            }
        }               
        
        /// <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);
            }
        }
       
        //private void pipeDiameterStatis(GSOGeoPolygon3D polygon)
        //{
        //    try
        //    {
        //        dic = new Dictionary<string, double>();
        //        dataGridViewX1.Rows.Clear();
        //        if (clbPipeLine.CheckedItems.Count > 0)
        //        {
        //            for (int i = 0; i < clbPipeLine.CheckedItems.Count; i++)
        //            {
        //                GSOLayer layer = globeControl1.Globe.Layers.GetLayerByCaption(clbPipeLine.CheckedItems[i].ToString());
        //                GSOFeatureLayer flayer = layer as GSOFeatureLayer;
        //                GSOFeatures feats = new GSOFeatures();
        //                if (polygon == null)
        //                    feats = flayer.GetAllFeatures();
        //                else
        //                    feats = flayer.FindFeaturesInPolygon(polygon, false);
        //                int ncount = 0;
        //                ncount = feats.Length;
        //                DataGridViewRow row = new DataGridViewRow();
        //                int index = dataGridViewX1.Rows.Add(row);
        //                row = dataGridViewX1.Rows[index];
        //                row.Cells["图层名称"].Value = layer.Caption;
        //                row.Cells["数量"].Value = ncount;
        //                dic.Add(layer.Caption, ncount);
        //            }
        //        }
        //        else
        //        {
        //            MessageBox.Show("请选择图层!");
        //        }
                
        //        showPipeWidget();
        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox.Show(ex.Message);
        //        LogError.PublishError(ex);
        //    }
        //}
        private void pipeDiameterStatisNew(GSOGeoPolygon3D polygon, string layertype)
        {
            try
            {
                FeatureStatisticsService featureService = new FeatureStatisticsService();
                GSOLayer layer = globeControl1.Globe.Layers.GetLayerByCaption(layertype);
                List<FeaturesClassfyResult> accessoriesResult = featureService.GroupAccessories(polygon, layer);

                if (accessoriesResult == null || accessoriesResult.Count == 0)
                {
                    MessageBox.Show("该图层没有附属物");
                }
                dataGridViewX1.Rows.Clear();
                dic.Clear();
                foreach (FeaturesClassfyResult accessoryResult in accessoriesResult)
                {
                    DataGridViewRow row = new DataGridViewRow();
                    int index = dataGridViewX1.Rows.Add(row);
                    string layerName = accessoryResult.layerName;
                    row = dataGridViewX1.Rows[index];
                    row.Cells["图层名称"].Value = layerName;// layer.Caption;
                    row.Cells["数量"].Value = accessoryResult.ncount;
                    dic.Add(layerName, accessoryResult.ncount);

                }
                showPipeWidget();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                LogError.PublishError(ex);
            }
        }
        private void showPipeWidget()
        {
            if (chart1.Series.Count > 0)
            {
                chart1.Series.Clear();
            }
            if (chart1.Legends.Count > 0)
            {
                chart1.Legends.Clear();
            }
            
            chart1.Series.Add("附属物");
            chart1.Legends.Add("附属物");
            chart1.Legends[0].Docking = Docking.Top;
            chart1.Legends["附属物"].IsTextAutoFit = true;
            chart1.Legends["附属物"].LegendStyle = LegendStyle.Table;
            chart1.Legends["附属物"].DockedToChartArea = "ChartArea1";
            chart1.Legends["附属物"].BackColor = Color.Transparent;
            chart1.ChartAreas["ChartArea1"].AxisY.IsLabelAutoFit = false;

            // 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[0].ChartType = SeriesChartType.Column;

            chart1.Series[0]["DrawingStyle"] = "Cylinder";

            //chart1.Series[j].IsValueShownAsLabel = true;

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

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

            globeControl1.Globe.MemoryLayer.RemoveAllFeature();
        }
        /// <summary>
        /// 导出Excel按钮事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExport_Click(object sender, EventArgs e)
        {
            if (dataGridViewX1.Rows.Count > 0)
            {
                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();
                    }
                }
            }
            else
            {
                MessageBox.Show("统计结果为空,请重新统计之后再导出Excel!", "提示");
            }
        }

    }
}