using DevExpress.Xpf.Editors; using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Controls; using ESRI.ArcGIS.esriSystem; using ESRI.ArcGIS.Geodatabase; using KGIS.Framework.AE; using KGIS.Framework.AE.Enum; using KGIS.Framework.AE.GPHelper; using KGIS.Framework.Maps; using KGIS.Framework.OpenData.Control; using KGIS.Framework.OpenData.Filter; using KGIS.Framework.OpenData.InterFace; using KGIS.Framework.Utils; using KGIS.Framework.Utils.ExtensionMethod; using System; using System.Collections.Generic; using System.IO; using System.Runtime.InteropServices; using System.Windows; using KUI.Windows; using KGIS.Framework.Utils.Helper; namespace Kingo.Plugin.EngineEditor.View { public partial class UCSetAttribute : BaseWindow { private IHookHelper m_hookHelper;//获取地图控件和主窗体 private Dictionary keys { get; set; } private Dictionary keyValuePairs { get; set; } public IFeatureClass ExternalFeatureClass { get; set; } public UCSetAttribute(object Helpers) { InitializeComponent(); try { if (this.m_hookHelper == null) this.m_hookHelper = Helpers as IHookHelper; //获取当前工程中的全部图层 List featureLayers = MapsManager.Instance.MapService.GetAllVisibleLayerInMap(); if (featureLayers != null && featureLayers.Count > 0) { keys = new Dictionary(); foreach (IFeatureLayer iFeatureLayer in featureLayers) { if (!keys.ContainsKey(iFeatureLayer.Name)) keys.Add(iFeatureLayer.Name, iFeatureLayer); } targetName.ItemsSource = keys.Keys; } } catch (Exception ex) { LogAPI.Debug("UCSetAttribute异常:" + ex.Message); LogAPI.Debug("UCSetAttribute异常:" + ex.StackTrace); } } #region 目标图层切换事件 private void targetName_SelectionChanged(object sender, System.Windows.RoutedEventArgs e) { try { if (keys != null) { IFeatureLayer featureLayer = keys[targetName.SelectedItem.ToTrim()]; if (featureLayer == null) return; IFields fields = featureLayer.FeatureClass.Fields; if (fields == null) return; keyValuePairs = new Dictionary(); for (int i = 0; i < fields.FieldCount; i++) { IField field = fields.Field[i]; if (field.Name.StartsWith("SHAPE") || field.Name == "OBJECTID" || field.Name.ToUpper().Equals("BSM")) continue; if (keyValuePairs.ContainsKey(field.Name)) continue; keyValuePairs.Add(field.Name, field.Type); } targetField.ItemsSource = keyValuePairs.Keys; } } catch (Exception ex) { LogAPI.Debug("targetName_SelectionChanged异常:" + ex.Message); LogAPI.Debug("targetName_SelectionChanged异常:" + ex.StackTrace); } } #endregion #region 目标字段切换事件 private void targetField_SelectionChanged(object sender, System.Windows.RoutedEventArgs e) { try { //根据选择的属性,判断数据类型 if (keyValuePairs == null) return; esriFieldType esriFieldType = keyValuePairs[targetField.SelectedItem.ToTrim()]; string strMask = string.Empty; switch (targetField.SelectedItem.ToTrim()) { case "KCXS": strMask = @"0|0\.\d{3}"; break; } if (!string.IsNullOrEmpty(strMask)) { AttributeValue.Mask = strMask; AttributeValue.MaskType = MaskType.RegEx; } else { AttributeValue.MaskType = MaskType.None; } } catch (Exception ex) { LogAPI.Debug("targetField_SelectionChanged异常:" + ex.Message); LogAPI.Debug("targetField_SelectionChanged异常:" + ex.StackTrace); } } #endregion #region 关闭窗体事件 private void BaseWindow_Closed(object sender, EventArgs e) { try { this.Close(); } catch (Exception ex) { LogAPI.Debug("BaseWindow_Closed异常:" + ex.Message); LogAPI.Debug("BaseWindow_Closed异常:" + ex.StackTrace); } } #endregion #region 选择外部数据 private void btnExternalData_Click(object sender, System.Windows.RoutedEventArgs e) { try { OpenDataDialog pDialog = new OpenDataDialog(); ISpatialDataObjectFilter pOFilter; pOFilter = new FilterDatasetsAndLayers(); pDialog.AddFilter(pOFilter, true); pDialog.Title = "选择导入的数据"; pDialog.AllowMultiSelect = false; pDialog.RestoreLocation = true; pDialog.StartLocation = pDialog.FinalLocation; System.Windows.Forms.DialogResult dialogResult = pDialog.ShowDialog(); if (dialogResult == System.Windows.Forms.DialogResult.OK && pDialog.Selection.Count != 0) { foreach (ISpatialDataObject distObj in pDialog.Selection) { if (distObj.DatasetType == esriDatasetType.esriDTFeatureClass) { ExternalFeatureClass = (distObj.DatasetName as IName).Open() as IFeatureClass; externalDataPath.Text = distObj.FullName; break; } } } } catch (Exception ex) { LogAPI.Debug("btnExternalData_Click异常:" + ex.Message); LogAPI.Debug("btnExternalData_Click异常:" + ex.StackTrace); } } #endregion #region 确定 private void btnOK_Click(object sender, System.Windows.RoutedEventArgs e) { IFeatureCursor pCursor = null; IFeature feature = null; try { if (string.IsNullOrEmpty(targetName.SelectedItem.ToTrim())) { MessageBox.Show("请选择目标图层.", "提示信息", MessageBoxButton.OK); return; } if (string.IsNullOrEmpty(targetField.SelectedItem.ToTrim())) { MessageBox.Show("请选择目标字段.", "提示信息", MessageBoxButton.OK); return; } if (string.IsNullOrEmpty(AttributeValue.Text.ToTrim())) { MessageBoxResult result = MessageBox.Show($"【{targetField.SelectedItem.ToTrim()}】字段设置为空?", "提示信息", MessageBoxButton.YesNoCancel); if (result != MessageBoxResult.Yes) return; } if (string.IsNullOrEmpty(externalDataPath.Text.ToTrim())) { MessageBox.Show("请选择要赋值的源图层数据.", "提示信息", MessageBoxButton.OK); return; } if (ExternalFeatureClass == null) { MessageBox.Show($"未能打开【{externalDataPath.Text.ToTrim()}】,请检查数据是否正确.", "提示信息", MessageBoxButton.OK); return; } this.ShowLoading("正在进行批量赋值中........", 0, 0); System.Threading.Thread.Sleep(1000); IFeatureLayer UpdatefeatureLayer = keys[targetName.SelectedItem.ToTrim()]; var Externalcount = ExternalFeatureClass.FeatureCount(null); List oids = new List(); IField field = UpdatefeatureLayer.FeatureClass.Fields.get_Field(UpdatefeatureLayer.FeatureClass.FindField(targetField.SelectedItem.ToTrim())); if (field.Length < AttributeValue.Text.ToTrim().Length) { this.CloseLoading(); MessageHelper.ShowError("当前值长度大于字段长度!"); return; } if (Externalcount <= 100) { #region 用外部数据源的图形与要赋值的目标图层 数据量小的情况 pCursor = ExternalFeatureClass.Search(null, true); while ((feature = pCursor.NextFeature()) != null) { var Identify2List = FeatureAPI.Identify2(feature.ShapeCopy, keys[targetName.SelectedItem.ToTrim()]); foreach (var item in Identify2List) { oids.Add(item.OID.ToTrim()); } } #endregion } else { #region 根据外部选择的数据源范围大小,数据量大小,可以使用GP工具 string gdbFileName = Guid.NewGuid().ToString().Replace("-", "") + ".gdb"; string gdbFolder = Directory.GetCurrentDirectory() + "\\Temp\\External"; if (!Directory.Exists(gdbFolder)) Directory.CreateDirectory(gdbFolder); PluginServiceInterface.CommonHelper.DelectDir(gdbFolder); string path = System.IO.Path.Combine(gdbFolder, gdbFileName); IWorkspaceFactory pFtWsFct = new ESRI.ArcGIS.DataSourcesGDB.FileGDBWorkspaceFactory(); IWorkspaceName workspaceName = pFtWsFct.Create(gdbFolder, gdbFileName, null, 0); #endregion IFeatureLayer FeatureLayer = null; GPParamClass gPParamClass = new GPParamClass { FirstFeatureLayer = UpdatefeatureLayer, SecondFeatureLayer = new FeatureLayer() { FeatureClass = ExternalFeatureClass }, OutFeatureClassPath = path + "\\" + "External", IsGetOutPutFeature = true }; GeoprocessorHelper.IntersectAnalysis(gPParamClass, ref FeatureLayer); pCursor = FeatureLayer.Search(null, true); var fid = FeatureLayer.FeatureClass.FindField($"FID_{(UpdatefeatureLayer.FeatureClass as FeatureClass).Name}"); while ((feature = pCursor.NextFeature()) != null) { if (fid != -1) oids.Add(feature.Value[fid].ToTrim()); } } string stroid = string.Join(",", oids); if (string.IsNullOrWhiteSpace(stroid)) { MessageBox.Show($"赋值失败,当前图层数据间无相交关系,请核对数据是否正确", "提示信息", MessageBoxButton.OK); return; } string sql = string.Empty; try { string strsetsql = string.IsNullOrEmpty(AttributeValue.Text.ToTrim()) || AttributeValue.Text.ToTrim().ToUpper() == "NULL" ? $"{targetField.SelectedItem.ToTrim()} =null " : $"{targetField.SelectedItem.ToTrim()} ='{AttributeValue.Text.ToTrim()}' "; sql = $"Update {(UpdatefeatureLayer.FeatureClass as FeatureClass).Name} set {strsetsql} where OBJECTID in ({stroid}) "; (keys[targetName.SelectedItem.ToTrim()].FeatureClass as FeatureClass).Workspace.ExecuteSQL(sql); } catch (Exception) { string strsetsql = string.IsNullOrEmpty(AttributeValue.Text.ToTrim()) || AttributeValue.Text.ToTrim().ToUpper() == "NULL" ? $"{targetField.SelectedItem.ToTrim()} =null " : $"{targetField.SelectedItem.ToTrim()} ={AttributeValue.Text.ToTrim()}"; sql = $"Update {(UpdatefeatureLayer.FeatureClass as FeatureClass).Name} set {strsetsql} where OBJECTID in ({stroid}) "; (keys[targetName.SelectedItem.ToTrim()].FeatureClass as FeatureClass).Workspace.ExecuteSQL(sql); } this.CloseLoading(); MessageBox.Show($"属性赋值完成.", "提示信息", MessageBoxButton.OK); } catch (Exception ex) { this.CloseLoading(); LogAPI.Debug("btnOK_Click异常:" + ex.Message); LogAPI.Debug("btnOK_Click异常:" + ex.StackTrace); } finally { this.CloseLoading(); if (feature != null) Marshal.ReleaseComObject(feature); if (pCursor != null) Marshal.ReleaseComObject(pCursor); } } #endregion #region 取消 private void btnCancel1_Click(object sender, System.Windows.RoutedEventArgs e) { try { this.Close(); } catch (Exception ex) { LogAPI.Debug("btnCancel1_Click异常:" + ex.Message); LogAPI.Debug("btnCancel1_Click异常:" + ex.StackTrace); } } #endregion } }