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 FrmpipeDiametergather :Office2007Form { GSOGlobeControl globeControl1; List<string> pipeLayerNames; GSOGeoPolygon3D polygon; static FrmpipeDiametergather frm; Dictionary<string, double> dic = new Dictionary<string, double>(); public static void ShowForm(GSOGlobeControl globeControl, List<string> pipeLayers, GSOGeoPolygon3D _polygon) { if (frm == null) { frm = new FrmpipeDiametergather(globeControl, pipeLayers, _polygon); frm.Show(globeControl.Parent); } else if (frm.WindowState == FormWindowState.Minimized) { frm.WindowState = FormWindowState.Normal; } } public FrmpipeDiametergather(GSOGlobeControl _globeControl1, List<string> pipeLayers, GSOGeoPolygon3D _polygon) { InitializeComponent(); globeControl1 = _globeControl1; pipeLayerNames = pipeLayers; } /// <summary> /// 窗体初始化事件处理 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void FrmpipeDiametergather_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 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> /// 全区域分类汇总 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void buttonX8_Click(object sender, EventArgs e) { if (clbPipeLine.CheckedItems.Count == 0) { MessageBox.Show("请选择图层!"); return; } globeControl1.Globe.Action = EnumAction3D.ActionNull; pipeDiameterStatis(null); } /// <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; } pipeDiameterStatis(polygon); } /// <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()); List<FeaturesClassfyResult> results = service.GroupPipeByDiameter(polygon,layer); foreach(FeaturesClassfyResult result in results){ 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, result.ncount); } } showPipeWidget(); } catch (Exception ex) { MessageBox.Show(ex.Message); LogError.PublishError(ex); } } /// <summary> /// 给图表绑定数据 /// </summary> private void showPipeWidget() { if (chart1.Series.Count > 0) { chart1.Series.Clear(); } if (chart1.Legends.Count > 0) { chart1.Legends.Clear(); } for (int i = 0; i < clbPipeLine.CheckedItems.Count; i++) { chart1.Series.Add(clbPipeLine.CheckedItems[i].ToString()); GSOLayer layer = globeControl1.Globe.Layers.GetLayerByCaption(clbPipeLine.CheckedItems[i].ToString()); if (layer != null && layer is GSOFeatureLayer) { GSOFeatureLayer flayer = layer as GSOFeatureLayer; GSOFeatures features = flayer.GetAllFeatures(); if (features.Length > 0) { if (features[0] != null && features[0].Geometry != null && features[0].Geometry.Type == EnumGeometryType.GeoPolyline3D) { GSOGeoPolyline3D p3d = features[0].Geometry as GSOGeoPolyline3D; if (p3d.Style != null && p3d.Style is GSOPipeLineStyle3D) { GSOPipeLineStyle3D ps3d = p3d.Style as GSOPipeLineStyle3D; chart1.Series[i].Color = ps3d.LineColor; } } } } chart1.Legends.Add(clbPipeLine.CheckedItems[i].ToString()); chart1.Legends[i].Docking = Docking.Top; chart1.Legends[clbPipeLine.CheckedItems[i].ToString()].IsTextAutoFit = true; chart1.Legends[clbPipeLine.CheckedItems[i].ToString()].LegendStyle = LegendStyle.Table; chart1.Legends[clbPipeLine.CheckedItems[i].ToString()].DockedToChartArea = "ChartArea1"; chart1.Legends[clbPipeLine.CheckedItems[i].ToString()].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; List<double> listKeys = new List<double>(); foreach (string key in dic.Keys) { if (key.Split(',').Length > 1) { string dic1TempKey = key.Split(',')[1]; double listItem = 0.0; if (double.TryParse(dic1TempKey, out listItem)) { if (!listKeys.Contains(listItem)) { listKeys.Add(listItem); } } } } listKeys.Sort(); for (int j = 0; j < chart1.Series.Count; j++) { Dictionary<string, double> dic1 = new Dictionary<string, double>(); for (int i = 0; i < listKeys.Count; i++) { dic1.Add(listKeys[i].ToString(), 0.0); } foreach (string key in dic.Keys) { if (key.Contains(chart1.Series[j].Name)) { if (dic1.ContainsKey(key.Split(',')[1])) { dic1[key.Split(',')[1]] = dic[key]; } } } chart1.Series[j].ChartType = SeriesChartType.Column; chart1.Series[j]["DrawingStyle"] = "Cylinder"; chart1.Series[j].Points.DataBindXY(dic1.Keys, dic1.Values); for (int m = 0; m < dic1.Values.Count; m++) { if (chart1.Series[j].Points[m].YValues[0].ToString() != "0") { chart1.Series[j].Points[m].Label = chart1.Series[j].Points[m].YValues[0].ToString(); } } } } /// <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!", "提示"); } } /// <summary> /// 窗体关闭事件处理 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void FrmpipeDiametergather_FormClosing(object sender, FormClosingEventArgs e) { frm = null; globeControl1.Globe.MemoryLayer.RemoveAllFeature(); } //限制只能选择一个 private void clbPipeLine_ItemCheck(object sender, ItemCheckEventArgs e) { if (clbPipeLine.CheckedItems.Count == 0) return; for (int i = 0; i < clbPipeLine.Items.Count; i++) { if (i != e.Index) { clbPipeLine.SetItemCheckState(i, CheckState.Unchecked); } } } } }