You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							254 lines
						
					
					
						
							9.6 KiB
						
					
					
				
			
		
		
	
	
							254 lines
						
					
					
						
							9.6 KiB
						
					
					
				using DevExpress.Utils.Win.Hook; | 
						|
using DevExpress.XtraExport; | 
						|
using ESRI.ArcGIS.Controls; | 
						|
using ESRI.ArcGIS.Geodatabase; | 
						|
using KGIS.Framework.AE; | 
						|
using KGIS.Framework.Commands; | 
						|
using KGIS.Framework.Maps; | 
						|
using KGIS.Framework.Platform; | 
						|
using KGIS.Framework.Utils; | 
						|
using KGIS.Framework.Utils.Helper; | 
						|
using KGIS.Framework.Utils.Interface; | 
						|
using Kingo.Plugin.EngineEditor.helper; | 
						|
using Kingo.Plugin.EngineEditor.Model; | 
						|
using Kingo.Plugin.EngineEditor.View; | 
						|
using Kingo.Plugin.EngineEditor.Views.BoundaryFitting; | 
						|
using Newtonsoft.Json; | 
						|
using OSGeo.OGR; | 
						|
using System; | 
						|
using System.Collections.Generic; | 
						|
using System.Linq; | 
						|
using System.Text; | 
						|
using System.Threading.Tasks; | 
						|
using System.Web.UI.WebControls; | 
						|
using System.Windows; | 
						|
using System.Xml.Linq; | 
						|
 | 
						|
namespace Kingo.Plugin.EngineEditor.Commands.Commands | 
						|
{ | 
						|
    public class BoundaryFittingCommandNew : BaseMenuCommand | 
						|
    { | 
						|
        private IHookHelper m_hookHelper { get; set; } | 
						|
        private EngineEditorClass m_engineEdit = new EngineEditorClass(); | 
						|
        private UCBoundaryFittingNew uCBoundaryFitting; | 
						|
        public override void OnClick() | 
						|
        { | 
						|
            try | 
						|
            { | 
						|
                if (uCBoundaryFitting == null) | 
						|
                { | 
						|
                    uCBoundaryFitting = new UCBoundaryFittingNew(m_hookHelper, m_engineEdit) | 
						|
                    { | 
						|
                        WindowStartupLocation = WindowStartupLocation.CenterScreen | 
						|
                    }; | 
						|
                    uCBoundaryFitting.ShowInMainForm(); | 
						|
                } | 
						|
                uCBoundaryFitting.Closed += (s, e) => | 
						|
                { | 
						|
                    uCBoundaryFitting = null; | 
						|
                }; | 
						|
            } | 
						|
            catch (Exception ex) | 
						|
            { | 
						|
                m_engineEdit.AbortOperation(); | 
						|
                MessageHelper.ShowError(ex.Message); | 
						|
                LogAPI.Debug("边界修复异常:" + ex.Message); | 
						|
            } | 
						|
        } | 
						|
 | 
						|
        public override void OnCreate(object hook) | 
						|
        { | 
						|
            try | 
						|
            { | 
						|
                Platform.Instance.NotifyMsgEven2 += Instance_NotifyMsgEven2; | 
						|
                if (hook == null) | 
						|
                { | 
						|
                    return; | 
						|
                } | 
						|
                if (m_hookHelper == null) | 
						|
                { | 
						|
                    m_hookHelper = new HookHelperClass() { Hook = hook }; | 
						|
                } | 
						|
                if (m_hookHelper.ActiveView == null) | 
						|
                { | 
						|
                    m_hookHelper = null; | 
						|
                    return; | 
						|
                } | 
						|
            } | 
						|
            catch (Exception ex) | 
						|
            { | 
						|
                m_hookHelper = null; | 
						|
                LogAPI.Debug("边界修复工具初始化异常:" + ex.Message); | 
						|
            } | 
						|
        } | 
						|
        public override bool Enabled | 
						|
        { | 
						|
            get | 
						|
            { | 
						|
                return m_engineEdit.EditState == esriEngineEditState.esriEngineStateEditing; | 
						|
            } | 
						|
        } | 
						|
 | 
						|
        private void Instance_NotifyMsgEven2(NotifyMsgPackage msg) | 
						|
        { | 
						|
            switch (msg.MsgType) | 
						|
            { | 
						|
                case "BoundaryRepair": | 
						|
                    string[] data = msg.Content.ToString().Split('|'); | 
						|
                    string tbbsm = data[0]; | 
						|
                    string bid = data[1]; | 
						|
                    List<int> UnionList = new List<int>(); | 
						|
                    try | 
						|
                    { | 
						|
                        List<RepairEntity> repairs = GetData(tbbsm, bid); | 
						|
                        m_engineEdit.StartOperation(); | 
						|
                        BoundaryRepairHelper.Execute(repairs, UnionList); | 
						|
                        ChangeFeature(repairs, UnionList); | 
						|
                        m_engineEdit.StopOperation("边界修复"); | 
						|
                    } | 
						|
                    catch (Exception ex) | 
						|
                    { | 
						|
                        m_engineEdit.AbortOperation(); | 
						|
                        LogAPI.Debug($"通知调用边界修复失败:\r\n"); | 
						|
                        LogAPI.Debug(ex); | 
						|
                        throw; | 
						|
                    } | 
						|
                    break; | 
						|
            } | 
						|
        } | 
						|
 | 
						|
        private List<RepairEntity> GetData(string tbbsm, string bid) | 
						|
        { | 
						|
            List<RepairEntity> repairs = new List<RepairEntity>(); | 
						|
            ESRI.ArcGIS.Geodatabase.IFeatureCursor cursor = null; | 
						|
            ESRI.ArcGIS.Geodatabase.ISpatialFilter spatialFilter = null; | 
						|
            try | 
						|
            { | 
						|
                ESRI.ArcGIS.Carto.IFeatureLayer DLTBFeatureLayer = MapsManager.Instance.MapService.GetFeatureLayerByName("DLTB"); | 
						|
                ESRI.ArcGIS.Carto.IFeatureLayer DtbFeatureLayer = MapsManager.Instance.MapService.GetFeatureLayerByName("DLTBBG"); | 
						|
                int indexBSM = DLTBFeatureLayer.FeatureClass.Fields.FindField("BSM"); | 
						|
                int indexTBBSM = DLTBFeatureLayer.FeatureClass.Fields.FindField("TBBSM"); | 
						|
 | 
						|
                Dictionary<string, List<string>> xfDic = new Dictionary<string, List<string>>(); | 
						|
                spatialFilter = new SpatialFilterClass() | 
						|
                { | 
						|
                    WhereClause = $"tbbsm = '{tbbsm}' and packageid = '{bid}'" | 
						|
                }; | 
						|
                IFeature feature; | 
						|
                cursor = DtbFeatureLayer.FeatureClass.Search(spatialFilter, true); | 
						|
                List<IFeature> _features = null; | 
						|
                #region 分组统计选中子地块所在任务包及该TBBSM下其余子地块 | 
						|
                while ((feature = cursor.NextFeature()) != null) | 
						|
                { | 
						|
                    int tbbsmIndex = feature.Fields.FindField("bsm"); | 
						|
                    int bidIndex = feature.Fields.FindField("packageid"); | 
						|
                    if (tbbsmIndex < 0 || bidIndex < 0) | 
						|
                    { | 
						|
                        continue; | 
						|
                    } | 
						|
                    string bsm = feature.get_Value(tbbsmIndex).ToString(); | 
						|
 | 
						|
                    RepairEntity repairEntity = new RepairEntity() | 
						|
                    { | 
						|
                        Geometry = feature.ShapeCopy, | 
						|
                        ListReference = new List<ReferenceEntity>(), | 
						|
                        Distance = 1.0, | 
						|
                        JCBH = bsm, | 
						|
                        PackageId = bid, | 
						|
                        OID = feature.OID | 
						|
                    }; | 
						|
                    _features = FeatureAPI.Identify(feature.ShapeCopy, DLTBFeatureLayer); | 
						|
                    if (_features == null || _features.Count < 1) | 
						|
                    { | 
						|
                        System.Threading.Thread.Sleep(1000); | 
						|
                        MessageHelper.ShowTips($"边界修复提示:标识码为【{bsm}】地块未获取到参考图层要素!跳过当前图斑。"); | 
						|
                        continue; | 
						|
                    } | 
						|
 | 
						|
                    foreach (IFeature jcfeature in _features) | 
						|
                    { | 
						|
                        if (indexTBBSM > -1 && jcfeature.get_Value(indexTBBSM).ToString().Equals(tbbsm)) | 
						|
                        { | 
						|
                            continue; | 
						|
                        } | 
						|
                        repairEntity.ListReference.Add(new ReferenceEntity() | 
						|
                        { | 
						|
                            BSM = indexBSM > -1 ? jcfeature.Value[indexBSM].ToString().Trim() : "", | 
						|
                            RefGeometry = jcfeature.ShapeCopy, | 
						|
                            oid = jcfeature.OID | 
						|
                        }); | 
						|
                    } | 
						|
                    repairs.Add(repairEntity); | 
						|
                } | 
						|
                #endregion | 
						|
 | 
						|
                return repairs; | 
						|
            } | 
						|
            catch (Exception ex) | 
						|
            { | 
						|
                LogAPI.Debug($"构造边界修复数据失败:\r\n"); | 
						|
                LogAPI.Debug(ex); | 
						|
                throw; | 
						|
            } | 
						|
        } | 
						|
 | 
						|
        private void ChangeFeature(List<RepairEntity> repairs, List<int> UnionList) | 
						|
        { | 
						|
            IFeature feature; | 
						|
            ESRI.ArcGIS.Carto.IFeatureLayer dltbbgLayer = MapsManager.Instance.MapService.GetFeatureLayerByName("DLTBBG"); | 
						|
            List<RepairEntity> updRes = repairs.Where(x => !x.JCBH.Contains("NMK") && !x.JCBH.Contains("_DELETE")).ToList(); | 
						|
            List<RepairEntity> delRes = repairs.Where(x => x.JCBH.Contains("_DELETE")).ToList(); | 
						|
            #region 删除要素 | 
						|
            List<int> oidList = new List<int>(); | 
						|
            foreach (RepairEntity item in delRes) | 
						|
            { | 
						|
                oidList.Add(item.OID); | 
						|
            } | 
						|
            if (UnionList.Count > 0) | 
						|
            { | 
						|
                oidList.AddRange(UnionList); | 
						|
            } | 
						|
            if (oidList.Count > 0) | 
						|
            { | 
						|
                foreach (int oid in oidList) | 
						|
                { | 
						|
                    feature = dltbbgLayer.FeatureClass.GetFeature(oid); | 
						|
                    if (feature != null) | 
						|
                    { | 
						|
                        feature.Delete(); | 
						|
                    } | 
						|
                } | 
						|
            } | 
						|
            #endregion | 
						|
 | 
						|
            #region 修改要素 | 
						|
            Dictionary<int, string> oidDic = new Dictionary<int, string>(); | 
						|
            foreach (RepairEntity item in updRes) | 
						|
            { | 
						|
                oidDic.Add(item.OID, item.JCBH); | 
						|
            } | 
						|
            foreach (int oid in oidDic.Keys) | 
						|
            { | 
						|
                feature = dltbbgLayer.FeatureClass.GetFeature(oid); | 
						|
                if (feature == null) | 
						|
                { | 
						|
                    continue; | 
						|
                } | 
						|
                string bsm = oidDic[oid]; | 
						|
                RepairEntity repair = repairs.FirstOrDefault(x => x.JCBH == bsm); | 
						|
                if (repair == null) | 
						|
                { | 
						|
                    continue; | 
						|
                } | 
						|
                bool isConvert = repair.Geometry == null || repair.Geometry.IsEmpty; | 
						|
                bool isContinue = string.IsNullOrWhiteSpace(repair.WKT); | 
						|
                if (isConvert && isContinue) | 
						|
                { | 
						|
                    continue; | 
						|
                } | 
						|
                feature.Shape = isConvert ? PluginServiceInterface.GeometryConvertHelper.ConvertWKTToIGeometry(repair.WKT) : repair.Geometry; | 
						|
            } | 
						|
            #endregion | 
						|
        } | 
						|
    } | 
						|
}
 | 
						|
 |