using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Controls; using ESRI.ArcGIS.Geodatabase; using ESRI.ArcGIS.Geometry; using KGIS.Framework.EngineEditor; using KGIS.Framework.Maps; using KGIS.Framework.Platform; using KGIS.Framework.Utils; using KGIS.Framework.Utils.Helper; using KGIS.Framework.Utils.Interface; using Kingo.PluginServiceInterface; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Kingo.Plugin.EngineEditor.Commands.Tools { /// /// 提取任务图斑 /// public class CmdWithDraw : BaseToolCmd { private EngineEditorClass editor = new EngineEditorClass(); private IHookHelper m_hookHelper; private IFeatureLayer TargetLayer = null; public override void OnCreate(object hook) { try { base.OnCreate(hook); if (this.m_hookHelper == null) { this.m_hookHelper = base.m_pHookHelper; } editor.OnStopEditing += Editor_OnStopEditing; base.OnCreate(hook); } catch (Exception ex) { LogAPI.Debug("提取图斑初始化OnCreate异常:" + ex.Message); } } private void Editor_OnStopEditing(bool saveChanges) { try { (m_hookHelper.Hook as IMapControl2).CurrentTool = null; } catch (Exception ex) { LogAPI.Debug(ex); } } public override void OnClick() { base.OnClick(); #region 提取任务图斑 if (editor != null && (editor as EngineEditorClass).TargetLayer != null) TargetLayer = (editor as EngineEditorClass).TargetLayer as IFeatureLayer; if (TargetLayer == null || TargetLayer.Name != "外业任务图斑") return;//获取任务图斑图层 (editor as EngineEditorClass).StartOperation(); try { IFeature selectedFeature = null; if (m_hookHelper.FocusMap.FeatureSelection is IEnumFeature) { IEnumFeature enumFeature = m_hookHelper.FocusMap.FeatureSelection as IEnumFeature; //selectedFeature = enumFeature.Next(); while ((selectedFeature = enumFeature.Next()) != null) { if (MessageHelper.ShowYesNoAndTips($"是否从“{((IDataset)selectedFeature.Class).Name}”图层中,提取图斑?") == System.Windows.Forms.DialogResult.Yes) { break; } else selectedFeature = null; } //不从外业任务图斑图层中提取任务图斑(需求暂定) if (selectedFeature == null) return; if (selectedFeature != null && ((IDataset)selectedFeature.Class).Name == "WYRW") { MessageHelper.ShowTips("提取选中要素不能为任务图斑!"); return; } IFeatureBuffer targetFeatureBuffer = TargetLayer.FeatureClass.CreateFeatureBuffer(); // 复制源要素的几何形状和属性值到目标要素 targetFeatureBuffer.Shape = selectedFeature.ShapeCopy; for (int i = 0; i < targetFeatureBuffer.Fields.FieldCount; i++) { IField field = targetFeatureBuffer.Fields.get_Field(i); if (!"JCBH,TBBSM,TBMJ,JCMJ".Contains(field.Name)) continue; if (field.Name.Equals("JCBH")) targetFeatureBuffer.set_Value(i, (MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).CODE + "ZZBG" + Guid.NewGuid().ToString().Replace("-", "").Substring(0, 6)); else if (field.Name.Equals("TBBSM")) targetFeatureBuffer.set_Value(i, (MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).CODE + "ZZBG" + Guid.NewGuid().ToString().Replace("-", "").Substring(0, 6)); else if (field.Name.Equals("TBMJ")) { IArea area1 = selectedFeature.ShapeCopy as IArea; targetFeatureBuffer.set_Value(i, Math.Round(area1.Area, 2)); } else if (field.Name.Equals("JCMJ")) { IArea area1 = selectedFeature.ShapeCopy as IArea; double JCMJValue = area1.Area / 666.67;//转化为亩 targetFeatureBuffer.set_Value(i, Math.Round(JCMJValue, 2)); } //if (field.Editable) // targetFeatureBuffer.set_Value(i, selectedFeature.get_Value(i)); } // 插入目标要素到目标图层 IFeatureCursor targetFeatureCursor = TargetLayer.FeatureClass.Insert(true); object insertIndex = targetFeatureCursor.InsertFeature(targetFeatureBuffer); targetFeatureCursor.Flush(); // 结束事务(可选) (editor as EngineEditorClass).StopOperation("提取任务图斑"); (editor as EngineEditorClass).StopEditing(true); Platform.Instance.SendMsg(new NotifyMsgPackage() { MsgType = "WithDrawTBToTreeNode", Content = insertIndex }); (m_hookHelper.FocusMap as IActiveView).PartialRefresh(esriViewDrawPhase.esriViewGeography, TargetLayer, null); // 释放资源 if (selectedFeature != null) System.Runtime.InteropServices.Marshal.ReleaseComObject(selectedFeature); if (targetFeatureBuffer != null) System.Runtime.InteropServices.Marshal.ReleaseComObject(targetFeatureBuffer); if (targetFeatureCursor != null) System.Runtime.InteropServices.Marshal.ReleaseComObject(targetFeatureCursor); //重新开启编辑,不受保存编辑影响 (editor as EngineEditorClass).StartEditing((TargetLayer.FeatureClass as FeatureClass).Workspace, MapsManager.Instance.MapService.getAxMapControl().Map); IEngineEditLayers pEditLayer = editor as IEngineEditLayers; pEditLayer.SetTargetLayer(TargetLayer, 0); MessageHelper.ShowTips("提取任务图斑成功!"); } } catch (Exception ex) { (editor as EngineEditorClass).AbortOperation(); (editor as EngineEditorClass).StopOperation("提取任务图斑出现异常!"); LogAPI.Debug("提取任务图斑失败:" + ex); MessageHelper.ShowError("提取任务图斑失败" + ex.Message); } finally { if (TargetLayer != null) System.Runtime.InteropServices.Marshal.ReleaseComObject(TargetLayer); } #endregion } public override bool Enabled { get { ILayer layer = (editor as EngineEditorClass).TargetLayer; if (editor.EditState == esriEngineEditState.esriEngineStateEditing && layer != null && (layer as IFeatureLayer).FeatureClass.ShapeType == esriGeometryType.esriGeometryPolygon) { return Platform.Instance.SystemType == SystemTypeEnum.DTBJK; } else { return false; } } } } }