using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.SystemUI;
using KGIS.Framework.Utils.Helper;
using System;
namespace Kingo.Plugin.NYYP.Tool
{
public class AddFeatureCommand : BaseTool
{
private IHookHelper m_hookHelper;
private IEngineEditor edit;
private ControlsEditingSketchToolClass controlsEditingSketchToolClass = null;
private IToolbarMenu m_toolbarMenuVertex;//绘图时右键弹出的鼠标点在编辑要素的顶点或边缘上,弹出的右键菜单
private ControlsEditingSketchDeleteCommandChinese pControlsEditingSketchDeleteCommandChinese;
public override void OnCreate(object hook)
{
if (m_hookHelper == null)
{
m_hookHelper = new HookHelperClass();
m_hookHelper.Hook = hook;
}
edit = new EngineEditorClass();
if (controlsEditingSketchToolClass == null)
{
controlsEditingSketchToolClass = new ControlsEditingSketchToolClass();
}
(m_hookHelper.Hook as IMapControl2).CurrentTool = controlsEditingSketchToolClass;
//base.CurrentTool = controlsEditingSketchToolClass;
//base.OnCreate(hook);
//初始化右键菜单 郑英杰 2018-09-10
if (m_toolbarMenuVertex == null)
{
m_toolbarMenuVertex = CreateVertexContextMenu();
m_toolbarMenuVertex.SetHook(m_hookHelper.Hook);
}
}
public override void OnClick()
{
if (edit == null)
{
edit = new EngineEditorClass();
}
if ((edit as EngineEditorClass).TargetLayer != null)
{
(edit as EngineEditorClass).GeometryType = ((edit as EngineEditorClass).TargetLayer as IFeatureLayer).FeatureClass.ShapeType;
}
this.OnCreate(this.m_hookHelper.Hook);
base.OnClick();
controlsEditingSketchToolClass.OnClick();
}
public override void OnDblClick()
{
try
{
IFeatureLayer featureLayer = (edit as EngineEditorClass).TargetLayer as IFeatureLayer;
if (featureLayer == null || featureLayer.FeatureClass == null)
{
return;
}
IEngineEditSketch editSketch = edit as IEngineEditSketch;
IGeometry geometry = editSketch.Geometry;
if (geometry == null || geometry.GeometryType == esriGeometryType.esriGeometryPoint)
{
return;
}
//BUG12271 郑英杰 2018-09-12
if (featureLayer.FeatureClass.ShapeType == esriGeometryType.esriGeometryPolygon)
{
//判断面图形面积是否大于0
IPolygon pPolygon = geometry as IPolygon;
IArea pArea = (IArea)pPolygon;
//BUG12300 郑英杰 2018-09-13 修改逆时针方向划线无法结束问题
//if (pArea.Area <= 0)
if (pArea.Area == 0)
{
return;
}
}
else if (featureLayer.FeatureClass.ShapeType == esriGeometryType.esriGeometryPolyline || featureLayer.FeatureClass.ShapeType == esriGeometryType.esriGeometryLine)
{
//判断线图形长度是否大于0
IPolyline pPolyline = geometry as IPolyline;
if (pPolyline.Length <= 0)
{
return;
}
}
double xmin = 0;
double xmax = 0;
double ymin = 0;
double ymax = 0;
//判断绘制的图形是否在目标图层数据范围内
if ((featureLayer.FeatureClass as IGeoDataset) != null && (featureLayer.FeatureClass as IGeoDataset).SpatialReference != null)
{
(featureLayer.FeatureClass as IGeoDataset).SpatialReference.GetDomain(out xmin, out xmax, out ymin, out ymax);
IEnvelope enveloe = (featureLayer.FeatureClass as IGeoDataset).Extent;
enveloe.XMin = xmin;
enveloe.XMax = xmax;
enveloe.YMin = ymin;
enveloe.YMax = ymax;
geometry.SpatialReference = enveloe.SpatialReference;
if (!KGIS.Framework.AE.FeatureAPI.IsContains(enveloe, geometry))
{
MessageHelper.ShowError("绘制图形超出目标图层范围!");
editSketch.Geometry = null;
editSketch.RefreshSketch();
this.m_hookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewBackground, null, this.m_hookHelper.ActiveView.Extent);
return;
}
}
//VerificationOfFeatureNodeDensity density = new VerificationOfFeatureNodeDensity();
//geometry = density.Verification(geometry);
editSketch.FinishSketch();
}
catch (Exception ex)
{
MessageHelper.ShowError("新增要素异常:" + ex.Message);
}
}
public override void OnKeyDown(int keyCode, int shift)
{
if (keyCode == (int)ConsoleKey.F9 || keyCode == (int)ConsoleKey.Escape)
{
#region 撤销点
ESRI.ArcGIS.Geometry.IPointCollection points = (edit as IEngineEditSketch).Geometry as ESRI.ArcGIS.Geometry.IPointCollection;
if (points.PointCount == 0)
{
return;
}
if ((edit as IEngineEditSketch).Geometry.GeometryType == esriGeometryType.esriGeometryPolygon)
{
if (points.PointCount - 2 > 0)
{
points.RemovePoints(points.PointCount - 2, 1);
}
else if (points.PointCount - 2 == 0)
{
points.RemovePoints(points.PointCount - 2, 2);
}
}
else
{
points.RemovePoints(points.PointCount - 1, 1);
}
(edit as IEngineEditSketch).Geometry = points as IGeometry;
(edit as IEngineEditSketch).RefreshSketch();
//this.m_hookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewBackground, null, this.m_hookHelper.ActiveView.Extent);
#endregion
}
//修改者:周旺华 修改时间:20180911 增加删除功能
else if (keyCode == (int)ConsoleKey.Backspace)
{
#region 清除绘制的图形
ESRI.ArcGIS.Geometry.IPointCollection points = (edit as IEngineEditSketch).Geometry as ESRI.ArcGIS.Geometry.IPointCollection;
if (points.PointCount == 0)
{
return;
}
points.RemovePoints(0, points.PointCount);
(edit as IEngineEditSketch).Geometry = points as IGeometry;
(edit as IEngineEditSketch).RefreshSketch();
this.m_hookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewBackground, null, this.m_hookHelper.ActiveView.Extent);
#endregion
}
controlsEditingSketchToolClass.OnKeyDown(keyCode, shift);
}
public override void OnKeyUp(int keyCode, int shift)
{
controlsEditingSketchToolClass.OnKeyUp(keyCode, shift);
}
public override void OnMouseDown(int button, int shift, int x, int y)
{
base.OnMouseDown(button, shift, x, y);
//显示右键菜单 郑英杰 2018-09-10
if (button == 2)
{
IEngineEditSketch editSketch = edit as IEngineEditSketch;
pControlsEditingSketchDeleteCommandChinese.Geometry = editSketch.Geometry;
((IEngineEditSketch)edit).SetEditLocation(x, y);
m_toolbarMenuVertex.PopupMenu(x, y, m_hookHelper.ActiveView.ScreenDisplay.hWnd);
}
}
public override bool OnContextMenu(int x, int y)
{
return base.OnContextMenu(x, y);
}
public override bool Enabled
{
get
{
if (edit == null)
{
edit = new EngineEditorClass();
}
if (edit.EditState == esriEngineEditState.esriEngineStateEditing)
{
ILayer layer = (edit as EngineEditorClass).TargetLayer;
if (layer != null && (layer as IFeatureLayer) != null)
{
return true;
}
}
return false;
}
}
///
/// 创建右键菜单
///
///
public IToolbarMenu CreateVertexContextMenu()
{
IToolbarMenu pToolbarMenuVertex = new ToolbarMenuClass();
pControlsEditingSketchDeleteCommandChinese = new ControlsEditingSketchDeleteCommandChinese();
pToolbarMenuVertex.AddItem(pControlsEditingSketchDeleteCommandChinese, -1, 0, false, esriCommandStyles.esriCommandStyleTextOnly);
pToolbarMenuVertex.AddItem(new ControlsEditingSketchFinishCommandChinese(), -1, 1, false, esriCommandStyles.esriCommandStyleTextOnly);
return pToolbarMenuVertex;
}
}
}