using KGIS.Framework.Utils; using KGIS.Framework.Utils.Helper; using System; using System.Collections.Generic; using System.Linq; using System.Windows; using KUI.Windows; using KGIS.Framework.Platform; using KGIS.Framework.Maps; using Kingo.PluginServiceInterface; using System.IO; using KGIS.Framework.DBOperator; using Aspose.Cells; using System.Data; using KGIS.Framework.Utils.ExtensionMethod; using Kingo.Plugin.BGResultManager.Model; using ESRI.ArcGIS.Geodatabase; using ESRI.ArcGIS.DataSourcesGDB; using ESRI.ArcGIS.Carto; using System.Runtime.InteropServices; using KGIS.Framework.AE.ExtensionMethod; using KGIS.Framework.AE; using Path = System.IO.Path; using KGIS.Framework.AE.Enum; using KGIS.Framework.OpenData.Control; using KGIS.Framework.OpenData.InterFace; using KGIS.Framework.OpenData.Filter; using Kingo.OpenData.Filter; using IRDBHelper = KGIS.Framework.DBOperator.IRDBHelper; using System.Windows.Controls; using Kingo.PluginServiceInterface.Model; using System.Windows.Forms; using System.Text; using System.Diagnostics; using IDEParameter; using GPHelper; namespace Kingo.Plugin.BGResultManager.View { /// /// 合库成果导出 的交互逻辑 /// public partial class FrmHKExport : BaseWindow { private ProjectInfo projectInfo = null; private string s_SelPath = string.Empty; private IWorkspaceAPI s_WsAPI = null; List hKPathModels = new List(); Dictionary tcNameDic = new Dictionary(); public FrmHKExport() { InitializeComponent(); GetInitTCDictionary(); InitData(); } private void InitData() { try { projectInfo = MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo; string taskPath = projectInfo.BGResultPath; if (string.IsNullOrEmpty(projectInfo.BGResultPath) || !Directory.Exists(projectInfo.BGResultPath)) return; foreach (var item in Directory.GetDirectories(taskPath)) { if (!Directory.Exists(item)) continue; string gxcgPath = Directory.GetDirectories(item)[0]; if (!Directory.Exists(gxcgPath)) continue; gxcgPath = Path.Combine(gxcgPath, "更新数据包\\原格式数据"); if (!Directory.Exists(gxcgPath)) continue; foreach (string gdbPath in Directory.GetDirectories(gxcgPath)) { if (gdbPath.Contains("PDTGX.gdb")) continue; PathNCM.Text = gdbPath; s_WsAPI = new WorkspaceAPI(gdbPath, WorkspaceTypeEnum.GDBFile, true); if (s_WsAPI == null) continue; List s_TbList = s_WsAPI.GetFeatureClassName(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTFeatureDataset).Where(x => !x.Value.StartsWith("TDQSQ")).Select(x => new KeyAndValue() { S_Key = x.Key, S_Value = x.Value }).ToList(); Dictionary s_TbList2 = s_WsAPI.GetFeatureClassName(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTFeatureClass); foreach (string item1 in s_TbList2.Keys) { if (!s_TbList.Select(x => x.S_Value).Contains(item1)) s_TbList.Add(new KeyAndValue() { S_Key = item1, S_Value = s_TbList2[item1] }); } foreach (KeyAndValue item1 in s_TbList) { if (!tcNameDic.ContainsKey(item1.S_Key)) continue; tcNameDic.TryGetValue(item1.S_Key, out string aliasName); hKPathModels.Add(new HKPathModel() { Ischeck = true, LayerName = item1.S_Key, LayerAliasName = aliasName, LayerPath = gdbPath }); } if (hKPathModels != null) hKPathModels = hKPathModels.OrderBy(a => a.LayerName).ToList(); } } dgTableMapping.ItemsSource = hKPathModels; } catch (Exception ex) { LogAPI.Debug("初始化合库列表数据失败!" + ex); } } //合并库导出 private void BtnOK_Click(object sender, RoutedEventArgs e) { if (string.IsNullOrEmpty(PathNCM.Text) && dgTableMapping.ItemsSource == null) { MessageHelper.ShowTips("请选择需合库数据来源!"); return; } try { projectInfo = MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo; if (projectInfo != null && projectInfo.CODE == null) return; if (string.IsNullOrEmpty(projectInfo.ZLDatabase)) return; if (this.dgTableMapping == null || (dgTableMapping.ItemsSource as List) == null) return; List hkPathModels = dgTableMapping.ItemsSource as List; if (hkPathModels == null || hkPathModels.Count <= 0) { MessageHelper.ShowTips("请选择需合库数据来源!"); return; } if (hkPathModels.FindAll(a => a.Ischeck).Count <= 0) { MessageHelper.ShowTips("请勾选合库数据,使需要合库的数据处于勾选状态!"); return; } List isCheckTCName = hkPathModels.Where(a => a.Ischeck).Select(a => a.LayerName).ToList(); this.ShowLoading($"正在进行合库操作...", 0, 0); string gdbFileName = Guid.NewGuid().ToString(); string tempExportPath = Path.Combine(Directory.GetCurrentDirectory(), gdbFileName); List copyPath = new List(); foreach (HKPathModel item in hkPathModels) { if (!copyPath.Contains(item.LayerPath)) { copyPath.Add(item.LayerPath); CopyGDB(item.LayerPath, tempExportPath); } item.LayerPath = Path.Combine(tempExportPath, Path.GetFileName(item.LayerPath)); } if (isCheckTCName.Contains("DLTBGX")) MergeGX(hkPathModels, "DLTBGX", "DLTBGXGC", "BGHTBBSM"); if (isCheckTCName.Contains("DLTBGXGC")) MergeGXGC(hkPathModels, "DLTBGXGC"); if (isCheckTCName.Contains("CZCDYDGX")) MergeGX(hkPathModels, "CZCDYDGX", "CZCDYDGXGC", "BGHBSM"); if (isCheckTCName.Contains("CZCDYDGXGC")) MergeGXGC(hkPathModels, "CZCDYDGXGC"); if (isCheckTCName.Contains("XZQGX")) MergeGX(hkPathModels, "XZQGX", "XZQGXGC", "BGHBSM"); if (isCheckTCName.Contains("XZQGXGC")) MergeGXGC(hkPathModels, "XZQGXGC"); if (isCheckTCName.Contains("CJDCQGX")) MergeGX(hkPathModels, "CJDCQGX", "CJDCQGXGC", "BGHBSM"); if (isCheckTCName.Contains("CJDCQGXGC")) MergeGXGC(hkPathModels, "CJDCQGXGC"); //删除临时文件 DelectDirect(tempExportPath); this.CloseLoading(); } catch (Exception ex) { this.CloseLoading(); MessageHelper.ShowError("合库导出成果失败。" + ex.Message); LogAPI.Debug("合库导出成果失败!" + ex.Message); LogAPI.Debug("合库导出成果失败!" + ex.StackTrace); } } private bool CopyGDB(string sourceDir, string targetDirPath) { try { if (!Directory.Exists(sourceDir)) return false; string targetDir = targetDirPath + "\\" + System.IO.Path.GetFileName(sourceDir); if (!Directory.Exists(targetDir)) Directory.CreateDirectory(targetDir); // 文件及文件夹名称数组 string[] dirColl = Directory.GetDirectories(sourceDir); string[] fileColl = Directory.GetFiles(sourceDir); // 便利所有文件 if (fileColl.Length > 0) { string fileName; foreach (string fileDir in fileColl) { fileName = System.IO.Path.GetFileName(fileDir); File.Copy(sourceDir + "\\" + fileName, targetDir + "\\" + fileName, true); } } // 遍历所有文件夹 if (dirColl.Length > 0) { string folderName; foreach (string dir in dirColl) { folderName = System.IO.Path.GetFileName(dir); // 递归调用 Directory.CreateDirectory(targetDir + "\\" + folderName); CopyGDB(dir, targetDir + "\\" + folderName); } } return true; } catch (Exception ex) { LogAPI.Debug("CopyGDB失败!" + ex.Message); LogAPI.Debug("CopyGDB失败!" + ex.StackTrace); } return false; } private void MergeGX(List hkPathModels, string layerName, string gxgcLayerName, string gxgcGLBsmName) { IWorkspaceAPI wsFirstAPI = null; IFeatureClassAPI fcFirstAPI = null; IWorkspaceAPI wsSecondAPI = null; IFeatureClassAPI fcSecondGXAPI = null; IFeatureLayer SecondFeatureLayer = null; IFeatureClassAPI fcSecondGXGCAPI = null; ICursor secondGXLayerCur = null; IRow secondGXLayerRow = null; IFeatureClassAPI fcOutExportAPI = null; IWorkspaceAPI newOutExportAPI = null; IFeatureClassAPI fcAPI = null; try { List dltbgxHk = hkPathModels.FindAll(a => a.LayerName == layerName).ToList(); if (dltbgxHk != null) { wsFirstAPI = new WorkspaceAPI(projectInfo.ZLDatabase, WorkspaceTypeEnum.GDBFile, true); fcFirstAPI = wsFirstAPI.OpenFeatureClass(layerName); int xh = 0; string bsmLeftStr = string.Empty; string MaxBSM = GetMaxBSM(fcFirstAPI.FeatureClass); if (MaxBSM.Length != 18) { MessageHelper.ShowError($"列表中{dltbgxHk[0].LayerName}中的标识码字长不是18位!"); LogAPI.Debug($"列表中{dltbgxHk[0].LayerName}中的标识码字长不是18位!"); return; } int xh2 = Convert.ToInt32(MaxBSM.Substring(10)); bsmLeftStr = MaxBSM.Substring(0, 10); if (xh < xh2) xh = xh2; //更新层重新编码 for (int i = 0; i < dltbgxHk.Count; i++) { wsSecondAPI = new WorkspaceAPI(dltbgxHk[i].LayerPath, WorkspaceTypeEnum.GDBFile, true); fcSecondGXAPI = wsSecondAPI.OpenFeatureClass(dltbgxHk[0].LayerName); SecondFeatureLayer = new FeatureLayerClass() { FeatureClass = fcSecondGXAPI.FeatureClass }; ITable secondGXLayerTable = fcSecondGXAPI.FeatureClass as ITable; secondGXLayerCur = secondGXLayerTable.Update(null, true); int bsmIdx = secondGXLayerTable.FindField("BSM"); fcSecondGXGCAPI = wsSecondAPI.OpenFeatureClass(gxgcLayerName); while ((secondGXLayerRow = secondGXLayerCur.NextRow()) != null) { string oldbsm = secondGXLayerRow.Value[bsmIdx].ToTrim(); xh++; string newbsm = bsmLeftStr + xh.ToString().PadLeft(8, '0'); secondGXLayerRow.Value[bsmIdx] = newbsm; secondGXLayerCur.UpdateRow(secondGXLayerRow); //重新赋值gxgc (fcSecondGXGCAPI.FeatureClass as FeatureClass).Workspace.ExecuteSQL($"update {gxgcLayerName} set {gxgcGLBsmName}='{newbsm}' where {gxgcGLBsmName}='{oldbsm}' "); } secondGXLayerCur.Flush(); fcAPI = new FeatureClassAPI(SecondFeatureLayer.FeatureClass); fcAPI.FcToFc(fcFirstAPI.FeatureClass, null, false); } } } catch (Exception ex) { LogAPI.Debug("合库地类图斑更新失败!" + ex.Message); LogAPI.Debug("合库地类图斑更新失败!" + ex.StackTrace); throw; } finally { if (wsFirstAPI != null) wsFirstAPI.CloseWorkspace(); if (fcFirstAPI != null) fcFirstAPI.CloseFeatureClass(); if (wsSecondAPI != null) wsSecondAPI.CloseWorkspace(); if (fcSecondGXAPI != null) fcSecondGXAPI.CloseFeatureClass(); if (SecondFeatureLayer != null) Marshal.ReleaseComObject(SecondFeatureLayer); if (fcSecondGXGCAPI != null) fcSecondGXGCAPI.CloseFeatureClass(); if (secondGXLayerCur != null) Marshal.ReleaseComObject(secondGXLayerCur); if (secondGXLayerRow != null) Marshal.ReleaseComObject(secondGXLayerRow); if (fcOutExportAPI != null) fcOutExportAPI.CloseFeatureClass(); if (newOutExportAPI != null) newOutExportAPI.CloseWorkspace(); if (fcAPI != null) fcAPI.CloseFeatureClass(); } } private void MergeGXGC(List hkPathModels, string gxgcLayerName) { IWorkspaceAPI wsFirstAPI = null; IFeatureClassAPI fcFirstAPI = null; IWorkspaceAPI wsSecondAPI = null; IFeatureClassAPI fcSecondGXAPI = null; IFeatureLayer SecondFeatureLayer = null; IFeatureClassAPI fcSecondGXGCAPI = null; ICursor secondGXLayerCur = null; IRow secondGXLayerRow = null; IFeatureClassAPI fcOutExportAPI = null; IWorkspaceAPI newOutExportAPI = null; IFeatureClassAPI fcAPI = null; try { List dltbgxHk = hkPathModels.FindAll(a => a.LayerName == gxgcLayerName).ToList(); if (dltbgxHk != null) { wsFirstAPI = new WorkspaceAPI(projectInfo.ZLDatabase, WorkspaceTypeEnum.GDBFile, true); fcFirstAPI = wsFirstAPI.OpenFeatureClass(gxgcLayerName); int xh = 0; string bsmLeftStr = string.Empty; string MaxBSM = GetMaxBSM(fcFirstAPI.FeatureClass); if (MaxBSM.Length != 18) { MessageHelper.ShowError($"列表中{dltbgxHk[0].LayerName}中的标识码字长不是18位!"); LogAPI.Debug($"列表中{dltbgxHk[0].LayerName}中的标识码字长不是18位!"); return; } int xh2 = Convert.ToInt32(MaxBSM.Substring(10)); bsmLeftStr = MaxBSM.Substring(0, 10); if (xh < xh2) xh = xh2; //更新层重新编码 for (int i = 0; i < dltbgxHk.Count; i++) { wsSecondAPI = new WorkspaceAPI(dltbgxHk[i].LayerPath, WorkspaceTypeEnum.GDBFile, true); fcSecondGXAPI = wsSecondAPI.OpenFeatureClass(dltbgxHk[0].LayerName); SecondFeatureLayer = new FeatureLayerClass() { FeatureClass = fcSecondGXAPI.FeatureClass }; ITable secondGXLayerTable = fcSecondGXAPI.FeatureClass as ITable; secondGXLayerCur = secondGXLayerTable.Update(null, true); int bsmIdx = secondGXLayerTable.FindField("BSM"); fcSecondGXGCAPI = wsSecondAPI.OpenFeatureClass(gxgcLayerName); while ((secondGXLayerRow = secondGXLayerCur.NextRow()) != null) { string oldbsm = secondGXLayerRow.Value[bsmIdx].ToTrim(); xh++; string newbsm = bsmLeftStr + xh.ToString().PadLeft(8, '0'); secondGXLayerRow.Value[bsmIdx] = newbsm; secondGXLayerCur.UpdateRow(secondGXLayerRow); } secondGXLayerCur.Flush(); fcAPI = new FeatureClassAPI(SecondFeatureLayer.FeatureClass); fcAPI.FcToFc(fcFirstAPI.FeatureClass, null, false); } } } catch (Exception ex) { LogAPI.Debug("合库地类图斑更新失败!" + ex.Message); LogAPI.Debug("合库地类图斑更新失败!" + ex.StackTrace); throw; } finally { if (wsFirstAPI != null) wsFirstAPI.CloseWorkspace(); if (fcFirstAPI != null) fcFirstAPI.CloseFeatureClass(); if (wsSecondAPI != null) wsSecondAPI.CloseWorkspace(); if (fcSecondGXAPI != null) fcSecondGXAPI.CloseFeatureClass(); if (SecondFeatureLayer != null) Marshal.ReleaseComObject(SecondFeatureLayer); if (secondGXLayerCur != null) Marshal.ReleaseComObject(secondGXLayerCur); if (secondGXLayerRow != null) Marshal.ReleaseComObject(secondGXLayerRow); if (fcOutExportAPI != null) fcOutExportAPI.CloseFeatureClass(); if (newOutExportAPI != null) newOutExportAPI.CloseWorkspace(); if (fcAPI != null) fcAPI.CloseFeatureClass(); } } private string GetMaxBSM(IFeatureClass fc) { //System.Windows.Forms.Application.DoEvents(); string BSM = string.Empty; int BSMIndex = fc.FindField("BSM"); if (BSMIndex == -1) return BSM; ITable table = (ITable)fc; ITableSort tableSort = new TableSortClass(); tableSort.Table = table; tableSort.Fields = "BSM"; tableSort.set_Ascending("BSM", false); tableSort.Sort(null); ICursor cursor = tableSort.Rows; IRow row = cursor.NextRow(); if (row != null) { int maxBSM = 0; int currBSM = 0; string BSMStr = row.Value[BSMIndex].ToString(); if (BSMStr.Length != 18) return BSM; string subBSMStr = BSMStr.Substring(9); try { currBSM = Convert.ToInt32(subBSMStr); } catch (Exception) { return BSM; } if (currBSM > maxBSM) maxBSM = currBSM; if (BSMStr.Length != 18) return BSM; string maxStr = maxBSM.ToString(); int zeroNum = 9 - maxStr.Length; for (int i = 0; i < zeroNum; i++) { maxStr = 0 + maxStr; } BSM = BSMStr.Substring(0, 9) + maxStr; } return BSM; } private void BtnCanel_Click(object sender, RoutedEventArgs e) { this.Close(); } //删除勾选路径 private void btnDeletePath_Click(object sender, RoutedEventArgs e) { try { if (this.dgTableMapping == null || (dgTableMapping.ItemsSource as List) == null) return; List listTableMapping = dgTableMapping.ItemsSource as List; listTableMapping.RemoveAll(a => a.Ischeck == true); hKPathModels = listTableMapping; dgTableMapping.ItemsSource = null; dgTableMapping.ItemsSource = hKPathModels; } catch (Exception ex) { MessageHelper.ShowTips("全选发生异常:" + ex.Message); } } //选择需添加图层的路径 private void btnSelectedHKPath_Click(object sender, RoutedEventArgs e) { try { PathNCM.Text = null; hKPathModels.Clear(); //dgTableMapping.ItemsSource = null; FolderBrowserDialog dialog = new FolderBrowserDialog(); if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { s_SelPath = dialog.SelectedPath; string fileName = string.Empty; if (!string.IsNullOrWhiteSpace(s_SelPath)) PathNCM.Text = s_SelPath; if (!string.IsNullOrEmpty(PathNCM.Text)) { foreach (string gdbPath in Directory.GetDirectories(PathNCM.Text)) { if (gdbPath.EndsWith("PDTGX.gdb")) continue; if (Path.GetExtension(gdbPath).ToUpper().Replace('.', ' ').TrimStart() != "GDB") continue; s_WsAPI = new WorkspaceAPI(gdbPath, WorkspaceTypeEnum.GDBFile, true); if (s_WsAPI == null) continue; List s_TbList = s_WsAPI.GetFeatureClassName(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTFeatureDataset).Where(x => !x.Value.StartsWith("TDQSQ")).Select(x => new KeyAndValue() { S_Key = x.Key, S_Value = x.Value }).ToList(); Dictionary s_TbList2 = s_WsAPI.GetFeatureClassName(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTFeatureClass); foreach (string item1 in s_TbList2.Keys) { if (!s_TbList.Select(x => x.S_Value).Contains(item1)) s_TbList.Add(new KeyAndValue() { S_Key = item1, S_Value = s_TbList2[item1] }); } foreach (KeyAndValue item1 in s_TbList) { if (!tcNameDic.ContainsKey(item1.S_Key)) continue; tcNameDic.TryGetValue(item1.S_Key, out string aliasName); hKPathModels.Add(new HKPathModel() { Ischeck = true, LayerName = item1.S_Key, LayerAliasName = aliasName, LayerPath = gdbPath }); } } if (hKPathModels != null) hKPathModels = hKPathModels.OrderBy(a => a.LayerName).ToList(); dgTableMapping.ItemsSource = null; dgTableMapping.ItemsSource = hKPathModels; } } } catch (Exception ex) { LogAPI.Debug("选择合库数据库失败:" + ex); MessageHelper.Show("选择合库数据库失败:" + ex); } finally { if (s_WsAPI != null) s_WsAPI.CloseWorkspace(); } } private void AllCheckEidt_EditValueChanged(object sender, DevExpress.Xpf.Editors.EditValueChangedEventArgs e) { try { if (this.dgTableMapping == null || (dgTableMapping.ItemsSource as List) == null) { return; } List listTableMapping = dgTableMapping.ItemsSource as List; bool check = (sender as DevExpress.Xpf.Editors.CheckEdit).IsChecked == null ? false : bool.Parse((sender as DevExpress.Xpf.Editors.CheckEdit).IsChecked.Value.ToString()); if (check) { listTableMapping.ForEach(a => a.Ischeck = true); } else { listTableMapping.ForEach(a => a.Ischeck = false); } dgTableMapping.RefreshData(); dgTableMapping.ItemsSource = null; dgTableMapping.ItemsSource = listTableMapping; } catch (Exception ex) { MessageHelper.ShowTips("全选发生异常:" + ex.Message); } } private void checkBox_Click(object sender, RoutedEventArgs e) { HKPathModel hkPath = dgTableMapping.SelectedItem as HKPathModel; if (hkPath != null) { hkPath.Ischeck = (bool)(sender as System.Windows.Controls.CheckBox).IsChecked; } } private void GetInitTCDictionary() { if (tcNameDic == null) tcNameDic = new Dictionary(); if (tcNameDic.Count > 0) tcNameDic.Clear(); tcNameDic.Add("DLTBGX", "地类图斑更新层"); tcNameDic.Add("DLTBGXGC", "地类图斑更新过程层"); tcNameDic.Add("CZCDYDGX", "城镇村更新层"); tcNameDic.Add("CZCDYDGXGC", "城镇村更新过程层"); tcNameDic.Add("XZQGX", "行政区更新层"); tcNameDic.Add("XZQGXGC", "行政区更新过程层"); tcNameDic.Add("CJDCQGX", "村级调查区更新层"); tcNameDic.Add("CJDCQGXGC", "村级调查区更新过程层"); } #region DelectDirect private void DelectDirect(string srcPath) { try { DirectoryInfo dir = new DirectoryInfo(srcPath); FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //返回目录中所有文件和子目录 foreach (FileSystemInfo i in fileinfo) { if (i is DirectoryInfo) //判断是否文件夹 { DirectoryInfo subdir = new DirectoryInfo(i.FullName); subdir.Delete(true); //删除子目录和文件 } else { File.Delete(i.FullName); //删除指定文件 } } dir.Delete(true); } catch (Exception ex) { //MessageHelper.ShowWarning("工程目录文件有占用,请退出占用后重试!"); LogAPI.Debug("工程目录文件有占用,请退出占用后重试!" + ex); } } #endregion } }