using System; 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 System.Xml; using GeoSigmaDrawLib; using UCDraw; using UCDraw.Print; namespace PcgDraw { //public delegate void LayerTreeLoad(string layerName); public partial class ExporFilesDialog : Form { private GeoSigmaXY geo; private List layers; private LayerTree layer; //public LayerTreeLoad GetLayerDataEvent; public GeoSigmaXY Geo { get { return this.geo; } set { this.geo = value; } } public ExporFilesDialog(LayerTree layer) { InitializeComponent(); this.layer = layer; layers = new List(); this.radioBtnPoints.Checked = true; this.combIntervalSymbol.SelectedIndex = 1; } private void ExporFilesDialog_Load(object sender, EventArgs e) { string strLayers = this.layer.DrawViewer.GetLayers(); this.ResetLayers(strLayers); this.combIntervalSymbol.SelectedIndex = 0; this.combOutput.SelectedIndex = 0; } public void LoadLayerTree() { if (this.geo == null) return; string strLayers = this.geo.GetLayers(); string CurrentLayer = this.geo.GetCurrentLayer(); ResetLayers(strLayers); } public void ResetLayers(string layers) { string[] saFullLayers = Regex.Split(layers, "\r\n"); Array.Reverse(saFullLayers); tvLayer.Nodes.Clear(); TreeNode tnRoot = new TreeNode("图层"); tnRoot.Name = tnRoot.Text; tvLayer.Nodes.Add(tnRoot); foreach (string strLine in saFullLayers) { if (strLine.Length == 0) continue; string[] saLayers = Regex.Split(strLine, @"\\"); FillLayerTree(tnRoot, saLayers, 0); } tvLayer.UpdateView(); tvLayer.UnselectAll(); tnRoot.Expand(); } private void FillLayerTree(TreeNode tnRoot, string[] saLayers, int startIndex) { for (int i = startIndex; i < saLayers.Length; i++) { string strLayer = saLayers[i]; if (strLayer.Equals("Layer:")) { continue; } int nFind = tnRoot.Nodes.IndexOfKey(strLayer); if (nFind < 0) { MakeFullNode(tnRoot, saLayers, i); break; } else { TreeNode tnFind = tnRoot.Nodes[nFind]; FillLayerTree(tnFind, saLayers, i + 1); break; } } } private void MakeFullNode(TreeNode curNode, string[] nodeNames, int startIndex) { if (startIndex < nodeNames.Length) { TreeNode tnChild = new TreeNode(nodeNames[startIndex]); tnChild.Name = tnChild.Text; tnChild.ImageIndex = 0; curNode.Nodes.Add(tnChild); MakeFullNode(tnChild, nodeNames, startIndex + 1); } } private void btnExport_Click(object sender, EventArgs e) { var saveFile = new SaveFileDialog(); saveFile.Filter = "文本文件(*.txt)|*.txt|BMP files(*.bmp)|*.bmp|GIF files(*.gif)|*.gif|所有文件(*.*;*.emf;*.wmf)|*.*"; saveFile.RestoreDirectory = true; if (saveFile.ShowDialog() != DialogResult.OK) return; string filePath = saveFile.FileName.ToString(); //获得要保存文件的绝对路径 this.txtExportPath.Text = filePath; } private void btnCancel_Click(object sender, EventArgs e) { this.Close(); } private void radioBtnPoints_CheckedChanged(object sender, EventArgs e) { var listItem = new List(); var item0 = new ListItem("name", "NAME,X,Y,Z,Layer"); listItem.Add(item0); if (this.combOutput.DataSource != listItem) { this.combOutput.DataSource = listItem; } } private void radioBtnCurve_CheckedChanged(object sender, EventArgs e) { var listItem = new List(); var item0 = new ListItem("xyz", "X,Y,Z"); var item1 = new ListItem("name", "NAME,X,Y,Z"); var item2 = new ListItem("id", "X,Y,Z,ID,LAYER,NAME"); listItem.Add(item0); listItem.Add(item1); listItem.Add(item2); this.combOutput.DataSource = listItem; } private void btnFinish_Click(object sender, EventArgs e) { this.ExportTxt(); this.Close(); } public void ExportTxt() { var fileName = this.txtExportPath.Text.Trim(); string id = string.Empty; string txtContent = string.Empty; string fileTitle = string.Empty; StringBuilder fileExport = new StringBuilder(); if (fileName == string.Empty) { MessageBox.Show("请选择文件!"); return; } StreamWriter f = new StreamWriter(fileName, false, Encoding.Default); int mode = 0; int split = 0; int format = 0; if (this.radioBtnPoints.Checked) { mode = 1; } else if (this.radioBtnCurve.Checked) { mode = 2; } if (this.combIntervalSymbol.SelectedIndex == 1) { split = 1; } if (this.combOutput.SelectedIndex == 1) { format = 1; } else if (this.combOutput.SelectedIndex == 2) { format = 2; } this.GetSelectNode(tvLayer.Nodes); foreach (var itemLayer in layers) { if (this.geo == null) { return; } string xml = this.geo.ExportFiles(itemLayer, mode); if (xml == string.Empty) { continue; //return; } XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(xml); XmlElement root = xmlDoc.DocumentElement; XmlNode rootNode = xmlDoc.SelectSingleNode("ROOT"); if (mode == 1) { if (split == 0) { //f.WriteLine("{0},{1},{2},{3},{4}", "名称", "X", "Y", "Z", "图层"); fileTitle = string.Format("{0},{1},{2},{3},{4}", "名称", "X", "Y", "Z", "图层"); } else { fileTitle = string.Format("{0,12}\t\t{1,-10}\t\t{2,-10}\t\t{3,-10}\t\t{4,-10}", "名称", "X", "Y", "Z", "图层"); } foreach (XmlNode xxNode in rootNode.ChildNodes) { if (split == 0) { fileExport.AppendFormat("{0},{1},{2},{3},{4}", xxNode.ChildNodes.Item(0).InnerText, xxNode.ChildNodes.Item(2).InnerText, xxNode.ChildNodes.Item(3).InnerText, xxNode.ChildNodes.Item(4).InnerText, xxNode.ChildNodes.Item(1).InnerText.Substring(7)).AppendLine(); } else { fileExport.AppendFormat("{0,12}\t\t{1,-10}\t\t{2,-10}\t\t{3,-10}\t\t{4,-10}", xxNode.ChildNodes.Item(0).InnerText, xxNode.ChildNodes.Item(2).InnerText, xxNode.ChildNodes.Item(3).InnerText, xxNode.ChildNodes.Item(4).InnerText, xxNode.ChildNodes.Item(1).InnerText.Substring(7)).AppendLine(); } } } else { if (split == 0) { if (format == 0) { fileTitle = string.Format("{0},{1},{2}", "X", "Y", "Z"); } else if (format == 1) { fileTitle = string.Format("{0},{1},{2},{3}", "X", "Y", "Z", "名称"); } else if (format == 2) { fileTitle = string.Format("{0},{1},{2},{3},{4},{5}", "X", "Y", "Z", "名称", "编号", "图层"); } } else { if (format == 0) { fileTitle = string.Format("{0,12}\t\t{1,-10}\t\t{2,-15}", "X", "Y", "Z"); } else if (format == 1) { fileTitle = string.Format("{0,12}\t\t{1,-10}\t\t{2,-15}\t\t{3,-20}", "X", "Y", "Z", "名称"); } else if (format == 2) { fileTitle = string.Format("{0,12}\t\t{1,-10}\t\t{2,-15}\t\t{3,-20}\t\t{4,-10}\t\t{5,-10}", "X", "Y", "Z", "名称", "编号", "图层"); } } if (split == 0) { foreach (XmlNode xxNode in rootNode.ChildNodes) { foreach (XmlNode item in xxNode) { if (format == 0) { fileExport.AppendFormat("{0},{1},{2}", item.ChildNodes.Item(2).InnerText, item.ChildNodes.Item(3).InnerText, item.ChildNodes.Item(4).InnerText).AppendLine(); } else if (format == 1) { fileExport.AppendFormat("{0},{1},{2},{3}", item.ChildNodes.Item(2).InnerText, item.ChildNodes.Item(3).InnerText, item.ChildNodes.Item(4).InnerText, item.ChildNodes.Item(5).InnerText).AppendLine(); } else if (format == 2) { fileExport.AppendFormat("{0},{1},{2},{3},{4},{5}", item.ChildNodes.Item(2).InnerText, item.ChildNodes.Item(3).InnerText, item.ChildNodes.Item(4).InnerText, item.ChildNodes.Item(5).InnerText, item.ChildNodes.Item(0).InnerText, item.ChildNodes.Item(1).InnerText.Substring(7)).AppendLine(); } } fileExport.AppendLine(); } } else { foreach (XmlNode xxNode in rootNode.ChildNodes) { foreach (XmlNode item in xxNode) { if (format == 0) { fileExport.AppendFormat("{0,12}\t\t{1,-10}\t\t{2,-20}", item.ChildNodes.Item(2).InnerText, item.ChildNodes.Item(3).InnerText, item.ChildNodes.Item(4).InnerText).AppendLine(); } else if (format == 1) { fileExport.AppendFormat("{0,12}\t\t{1,-10}\t\t{2,-20}\t\t{3,-20}", item.ChildNodes.Item(2).InnerText, item.ChildNodes.Item(3).InnerText, item.ChildNodes.Item(4).InnerText, item.ChildNodes.Item(5).InnerText).AppendLine(); } else if (format == 2) { fileExport.AppendFormat("{0,12}\t\t{1,-10}\t\t{2,-20}\t\t{3,-20}\t\t{4,-10}\t\t{5,-10}", item.ChildNodes.Item(2).InnerText, item.ChildNodes.Item(3).InnerText, item.ChildNodes.Item(4).InnerText, item.ChildNodes.Item(5).InnerText, item.ChildNodes.Item(0).InnerText, item.ChildNodes.Item(1).InnerText.Substring(7)).AppendLine(); } } fileExport.AppendLine(); } } } } f.WriteLine(fileTitle); f.WriteLine(fileExport); f.Close(); MessageBox.Show("导出成功!"); } private string GetLayerName(TreeNode node) { if (node == null) { return null; } string strLayerName = node.FullPath; if (strLayerName.Length < 3) return null; strLayerName = "Layer:\\" + strLayerName.Remove(0, 3); return strLayerName; } private void combOutput_SelectedIndexChanged(object sender, EventArgs e) { this.ExportPreview(); } private void combIntervalSymbol_SelectedIndexChanged(object sender, EventArgs e) { this.ExportPreview(); } public void GetSelectNode(TreeNodeCollection tree) { foreach (var itemNode in tree) { TreeNode node = (TreeNode)itemNode; if (node.GetCheckStatus() == 1 && node.GetNodeCount(false) == 0) { if (!layers.Contains(GetLayerName(node))) { layers.Add(GetLayerName(node)); } } else { layers.Remove(GetLayerName(node)); } GetSelectNode(node.Nodes); } } public void ExportPreview() { var fileName = this.txtExportPath.Text.Trim(); StringBuilder txtContent = new StringBuilder(); string fileTitle = string.Empty; string fileExport = string.Empty; int mode = 0; int split = 0; int format = 0; if (this.radioBtnPoints.Checked) { mode = 1; } else if (this.radioBtnCurve.Checked) { mode = 2; } if (this.combIntervalSymbol.SelectedIndex == 1) { split = 1; } if (this.combOutput.SelectedIndex == 1) { format = 1; } else if (this.combOutput.SelectedIndex == 2) { format = 2; } this.GetSelectNode(tvLayer.Nodes); foreach (var itemLayer in layers) { if (this.geo == null) { return; } string xml = this.geo.ExportFiles(itemLayer, mode); if (xml == string.Empty) { this.scintillaExportPreview.Text = ""; continue; //return; } XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(xml); XmlElement root = xmlDoc.DocumentElement; XmlNode rootNode = xmlDoc.SelectSingleNode("ROOT"); if (mode == 1) { if (split == 0) { fileTitle = string.Format("{0},{1},{2},{3},{4}", "名称", "X", "Y", "Z", "图层") + "\r\n"; } else { fileTitle = string.Format("{0,12}\t\t{1,-10}\t\t{2,-10}\t\t{3,-10}\t\t{4,-10}", "名称", "X", "Y", "Z", "图层") + "\r\n"; } foreach (XmlNode xxNode in rootNode.ChildNodes) { if (split == 0) { txtContent.AppendFormat("{0},{1},{2},{3},{4}", xxNode.ChildNodes.Item(0).InnerText, xxNode.ChildNodes.Item(2).InnerText, xxNode.ChildNodes.Item(3).InnerText, xxNode.ChildNodes.Item(4).InnerText, xxNode.ChildNodes.Item(1).InnerText.Substring(7)).AppendLine(); } else { txtContent.AppendFormat("{0,12}\t\t{1,-10}\t\t{2,-10}\t\t{3,-10}\t\t{4,-10}", xxNode.ChildNodes.Item(0).InnerText, xxNode.ChildNodes.Item(2).InnerText, xxNode.ChildNodes.Item(3).InnerText, xxNode.ChildNodes.Item(4).InnerText, xxNode.ChildNodes.Item(1).InnerText.Substring(7)).AppendLine(); } } } else { if (split == 0) { if (format == 0) { fileTitle = string.Format("{0},{1},{2}", "X", "Y", "Z") + "\r\n"; } else if (format == 1) { fileTitle = string.Format("{0},{1},{2},{3}", "X", "Y", "Z", "名称") + "\r\n"; } else if (format == 2) { fileTitle = string.Format("{0},{1},{2},{3},{4},{5}", "X", "Y", "Z", "名称", "编号", "图层") + "\r\n"; } } else { if (format == 0) { fileTitle = string.Format("{0,12}\t\t{1,-10}\t\t{2,-15}", "X", "Y", "Z") + "\r\n"; } else if (format == 1) { fileTitle = string.Format("{0,12}\t\t{1,-10}\t\t{2,-15}\t\t{3,-20}", "X", "Y", "Z", "名称") + "\r\n"; } else if (format == 2) { fileTitle = string.Format("{0,12}\t\t{1,-10}\t\t{2,-15}\t\t{3,-20}\t\t{4,-10}\t\t{5,-10}", "X", "Y", "Z", "名称", "编号", "图层") + "\r\n"; } } if (split == 0) { foreach (XmlNode xxNode in rootNode.ChildNodes) { foreach (XmlNode item in xxNode) { if (format == 0) { txtContent.AppendFormat("{0},{1},{2}", item.ChildNodes.Item(2).InnerText, item.ChildNodes.Item(3).InnerText, item.ChildNodes.Item(4).InnerText).AppendLine(); } else if (format == 1) { txtContent.AppendFormat("{0},{1},{2},{3}", item.ChildNodes.Item(2).InnerText, item.ChildNodes.Item(3).InnerText, item.ChildNodes.Item(4).InnerText, item.ChildNodes.Item(5).InnerText).AppendLine(); } else if (format == 2) { txtContent.AppendFormat("{0},{1},{2},{3},{4},{5}", item.ChildNodes.Item(2).InnerText, item.ChildNodes.Item(3).InnerText, item.ChildNodes.Item(4).InnerText, item.ChildNodes.Item(5).InnerText, item.ChildNodes.Item(0).InnerText, item.ChildNodes.Item(1).InnerText.Substring(7)).AppendLine(); } } } } else { foreach (XmlNode xxNode in rootNode.ChildNodes) { foreach (XmlNode item in xxNode) { if (format == 0) { txtContent.AppendFormat("{0,12}\t\t{1,-10}\t\t{2,-20}", item.ChildNodes.Item(2).InnerText, item.ChildNodes.Item(3).InnerText, item.ChildNodes.Item(4).InnerText).AppendLine(); } else if (format == 1) { txtContent.AppendFormat("{0,12}\t\t{1,-10}\t\t{2,-20}\t\t{3,-20}", item.ChildNodes.Item(2).InnerText, item.ChildNodes.Item(3).InnerText, item.ChildNodes.Item(4).InnerText, item.ChildNodes.Item(5).InnerText).AppendLine(); } else if (format == 2) { txtContent.AppendFormat("{0,12}\t\t{1,-10}\t\t{2,-20}\t\t{3,-20}\t\t{4,-10}\t\t{5,-10}", item.ChildNodes.Item(2).InnerText, item.ChildNodes.Item(3).InnerText, item.ChildNodes.Item(4).InnerText, item.ChildNodes.Item(5).InnerText, item.ChildNodes.Item(0).InnerText, item.ChildNodes.Item(1).InnerText.Substring(7)).AppendLine(); } } } } } } if (!txtContent.Equals(txtContent.ToString())) { this.scintillaExportPreview.Text = fileTitle + txtContent; } else { this.scintillaExportPreview.Text = ""; } fileTitle = ""; txtContent.Clear(); } private void tvLayer_AfterSelect(object sender, TreeViewEventArgs e) { this.tvLayer.SelectedNode.SetCheckStatus(1); this.GetSelectNode(tvLayer.Nodes); this.ExportPreview(); } private void tvLayer_Click(object sender, EventArgs e) { this.GetSelectNode(tvLayer.Nodes); this.ExportPreview(); } } }