namespace 捕捉
{
public partial class Form1 : Form
{
private bool bCreateElement=true;
private int internalTime = ;
private int snapTime = ;
private IElement m_element = null;
IPoint currentPoint=new PointClass();
private IPoint snapPoint = null;
IMovePointFeedback movePointFeedback=new MovePointFeedbackClass();
private string snapLayer = ""; public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{ } private void axMapControl1_OnMouseMove(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseMoveEvent e)
{
currentPoint.PutCoords(e.mapX,e.mapY);
snapTime++;
snapTime = snapTime%internalTime;
ILayer layer=GetLayerByName(snapLayer,axMapControl1);
if (layer==null)
{
return;
}
IFeatureLayer featureLayer = layer as IFeatureLayer;
if (bCreateElement)
{
CreateMarkerElement(currentPoint);
bCreateElement = false;
}
if (snapPoint==null)
{
ElementMoveTo(currentPoint);
}
if (snapTime==)
{ #region 第一种捕捉方式 IPoint temPoint = new PointClass();
temPoint.X = e.mapX;
temPoint.Y = e.mapY;
snapPoint = GetNearestVertex(axMapControl1.ActiveView, temPoint, ); #endregion #region 第二种方式 //snapPoint = Snapping(e.mapX, e.mapY, featureLayer); #endregion }
if (snapPoint!=null&&snapTime==)
{
ElementMoveTo(snapPoint);
}
} public double ConvertPixelsToMapUnits(IActiveView activeView, double pixelUnits)
{
double realDisplayExtent;
int pixelExtent;
double sizeOfOnePixel;
pixelExtent = activeView.ScreenDisplay.DisplayTransformation.get_DeviceFrame().right -
activeView.ScreenDisplay.DisplayTransformation.get_DeviceFrame().left;
realDisplayExtent = activeView.ScreenDisplay.DisplayTransformation.VisibleBounds.Width;
sizeOfOnePixel = realDisplayExtent/pixelExtent;
return pixelUnits*sizeOfOnePixel;
}
public IPoint Snapping(double x,double y,IFeatureLayer featureLayer)
{
       #region 这种捕捉有问题,无法捕捉点和面图层
IPoint iHitPoint = null;
IMap iMap = axMapControl1.Map;
IActiveView iView = axMapControl1.ActiveView;
IFeatureClass iFClass = featureLayer.FeatureClass;
IPoint point=new PointClass();
point.PutCoords(x,y);
double length = ConvertPixelsToMapUnits(iView, );
ITopologicalOperator pTopo = point as ITopologicalOperator;
IGeometry pGeometry = pTopo.Buffer(length).Envelope as IGeometry;
ISpatialFilter spatialFilter=new SpatialFilterClass();
spatialFilter.GeometryField = featureLayer.FeatureClass.ShapeFieldName;
spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelCrosses;
spatialFilter.Geometry = pGeometry; IFeatureCursor cursor = iFClass.Search(spatialFilter, false);
IFeature iF = cursor.NextFeature();
if (iF==null)
{
return null;
}
IPoint iHitPt = new ESRI.ArcGIS.Geometry.Point();
IHitTest iHitTest = iF.Shape as IHitTest;
double hitDist = ;
int partIndex = ;
int vertexIndex = ;
bool bVertexHit = false; double tol = ConvertPixelsToMapUnits(iView, );
if (iHitTest.HitTest(point,tol,esriGeometryHitPartType.esriGeometryPartBoundary,iHitPt,ref hitDist,ref partIndex,ref vertexIndex,ref bVertexHit))
{
iHitPoint = iHitPt;
}
axMapControl1.ActiveView.Refresh();
return iHitPoint;
       #endregion
}
public void ElementMoveTo(IPoint point)
{
movePointFeedback.MoveTo(point);
IGeometry geometry1 = null;
IGeometry geometry2 = null;
if (m_element!=null)
{
geometry1 = m_element.Geometry;
geometry2 = movePointFeedback.Stop();
m_element.Geometry = geometry2;
this.axMapControl1.ActiveView.GraphicsContainer.UpdateElement(m_element);
movePointFeedback.Start(geometry1 as IPoint,point);
axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics,null,null); }
}
public IPoint GetNearestVertex(IActiveView actview, IPoint pnt, double mapSize)
{
IPoint vetex = null;
IPoint hitPnt = new PointClass();
IHitTest hitTest = null;
IPointCollection pntColl = new MultipointClass();
IProximityOperator prox = null;
double hitdis = ;
int hitpartindex = ;
int hitsegindex = ;
Boolean rside = false;
IFeatureCache2 featCache = new FeatureCacheClass();
double pixelSize = ConvertPixelsToMapUnits(actview, mapSize); //将地理范围转化为像素
featCache.Initialize(pnt, pixelSize); //初始化缓存
for (int i = ; i < actview.FocusMap.LayerCount; i++)
{
//只有点、线、面并且可视的图层才加入缓存
IFeatureLayer featLayer = (IFeatureLayer)actview.FocusMap.get_Layer(i);
if (featLayer != null && featLayer.Visible == true &&
(featLayer.FeatureClass.ShapeType == esriGeometryType.esriGeometryPolyline ||
featLayer.FeatureClass.ShapeType == esriGeometryType.esriGeometryPolygon ||
featLayer.FeatureClass.ShapeType == esriGeometryType.esriGeometryPoint))
{
featCache.AddFeatures(featLayer.FeatureClass, null);
for (int j = ; j < featCache.Count; j++)
{
IFeature feat = featCache.get_Feature(j);
hitTest = (IHitTest)feat.Shape;
//捕捉节点,另外可以设置esriGeometryHitPartType,捕捉边线点,中间点等。
if (hitTest.HitTest(pnt, mapSize, esriGeometryHitPartType.esriGeometryPartVertex, hitPnt, ref hitdis, ref hitpartindex, ref hitsegindex, ref rside))
{
object obj = Type.Missing;
pntColl.AddPoint(hitPnt, ref obj, ref obj);
break;
}
}
}
}
prox = (IProximityOperator)pnt;
double minDis = , dis = ;
for (int i = ; i < pntColl.PointCount; i++)
{
IPoint tmpPnt = pntColl.get_Point(i);
dis = prox.ReturnDistance(tmpPnt);
if (i == )
{
minDis = dis;
vetex = tmpPnt;
}
else
{
if (dis < minDis)
{
minDis = dis;
vetex = tmpPnt;
}
}
}
return vetex;
}
public ILayer GetLayerByName(string layerName, AxMapControl axMap)
{
for (int i = ; i < axMap.LayerCount; i++)
{
if (axMap.get_Layer(i).Name.EndsWith(layerName))
{
return axMap.get_Layer(i);
}
}
return null;
} public void CreateMarkerElement(IPoint point)
{
IActiveView activeView = this.axMapControl1.ActiveView;
IGraphicsContainer graphicsContainer = axMapControl1.Map as IGraphicsContainer;
IMarkerElement markerElement = new MarkerElement() as IMarkerElement;
ISimpleMarkerSymbol simpleMarkerSymbol=new SimpleMarkerSymbol(); IRgbColor rgbColor1=new RgbColor();
rgbColor1.Red = ;
rgbColor1.Blue = ;
rgbColor1.Green = ;
simpleMarkerSymbol.Color = rgbColor1; IRgbColor iRgbColor2=new RgbColor();
iRgbColor2.Red = ;
iRgbColor2.Blue = ;
iRgbColor2.Green = ; simpleMarkerSymbol.Outline = true;
simpleMarkerSymbol.OutlineColor = iRgbColor2 as IColor;
simpleMarkerSymbol.OutlineSize = ;
simpleMarkerSymbol.Size = ;
simpleMarkerSymbol.Style = esriSimpleMarkerStyle.esriSMSCircle;
ISymbol symbol = simpleMarkerSymbol as ISymbol;
symbol.ROP2 = esriRasterOpCode.esriROPNotXOrPen;
markerElement.Symbol = simpleMarkerSymbol;
m_element = markerElement as IElement;
m_element.Geometry = point as IGeometry;
graphicsContainer.AddElement(m_element,);
activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics,m_element,null);
IGeometry geometry = m_element.Geometry;
movePointFeedback.Display = activeView.ScreenDisplay;
movePointFeedback.Symbol = simpleMarkerSymbol as ISymbol;
movePointFeedback.Start(geometry as IPoint,point); } private void cbLayerName_SelectedIndexChanged(object sender, EventArgs e)
{
snapLayer = cbLayerName.Text;
} private void Form1_DoubleClick(object sender, EventArgs e)
{ } private void splitContainer1_Panel2_DoubleClick(object sender, EventArgs e)
{
for (int i = ; i < axMapControl1.Map.LayerCount; i++)
{
cbLayerName.Items.Add(axMapControl1.get_Layer(i).Name);
}
cbLayerName.Text = cbLayerName.Items[].ToString();
snapLayer = cbLayerName.Text;
} }
}

源码:http://pan.baidu.com/s/1gdzP8QJ

提取密码:dlpn

visual studio 2008

AE9.3

C# Arcgis Engine 捕捉功能实现的更多相关文章

  1. ArcGIS Engine 捕捉

    原文 ArcGIS Engine 捕捉 bool bCreateElement = true; ;//时间间隔 ;//初始值 IElement m_element = null; //界面绘制点元素 ...

  2. ArcGIS Engine开发鹰眼图的功能(基础篇)

    鹰眼是用于调节全视域范围内主地图显示范围情况的副地图.它体现了地图整体与详细局部的关系. 用户可以通过鼠标单击或者画框等动作实现鹰眼与主地图的交互情况. 鹰眼功能的原理是通过主地图窗口的地图控件和鹰眼 ...

  3. ArcGIS Engine开发的ArcGIS 版本管理的功能

    原文:ArcGIS Engine开发的ArcGIS 版本管理的功能 转自:http://blog.csdn.net/linghe301/article/details/7965901 这是以前的Arc ...

  4. ArcGIS Engine开发鹰眼图的功能(代码优化篇)

    在上一篇,ArcGIS Engine开发鹰眼图的功能(基础篇) 上一篇的实现效果图如下, 如果仔细观察,会发现一个问题,即在“鹰眼”的区域,只要在红色线框内的注记会被覆盖. 如果红色框包括整张图的话, ...

  5. ArcGIS AddIn 图斑比例分割工具,调用捕捉功能

    最近做一个图斑按比例分割的工具,需要绘制一条用以切割的方向线,通过Tool的方式实现 绘制时希望能够使用捕捉功能,查阅相关资料如下: 使用该文章,第Implementing snapping in a ...

  6. 利用ArcGIS Engine、VS .NET和Windows控件开发GIS应用

    Dixon 原文  用ArcGIS Engine.VS .NET和Windows控件开发GIS应用     此过程说明适合那些使用.NET建立和部署应用的开发者,它描述了使用ArcGIS控件建立和部署 ...

  7. C#,ArcGIS Engine开发入门教程

    C#,ArcGIS Engine开发入门教程 转自:http://blog.csdn.net/yanleigis/article/details/2233674  目录(?)[+] 五实现 一 加载A ...

  8. ArcGIS Engine开发之属性查询

    属性查询即基于空间数据的属性数据的查询,通过用户提交SQL语言中的where语句定义的查询条件,对属性数据进行搜索,从而得到查询结果的操作. 相关的类与接口 与属性查询功能相关的类主要有QureyFi ...

  9. ArcGIS Engine开发之地图基本操作(4)

    ArcGIS Engine开发中数据库的加载 1.加载个人地理数据库数据 个人地理数据库(Personal Geodatabase)使用Miscrosoft Access文件(*.mdb)进行空间数据 ...

随机推荐

  1. struts2 异常页面乱码问题

    在 struts.xml 或者 struts.properties 文件里添加 <constant name="struts.locale" value="zh_C ...

  2. 用java实现螺旋数组

    接收数组的行数和列数,返回正序和倒序的螺旋数组 package cn.baokx; public class Test { public static void main(String[] args) ...

  3. node07---post请求、表单提交、文件上传

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. struts2标签#、%、$取值

    转自:https://blog.csdn.net/kosum/article/details/21375635 首先了解下OGNL的概念: OGNL是Object-Graph Navigation L ...

  5. 18.查询效率最高的unordered_map

    #include <string> #include <iostream> //查询性能最高 //增删查改与map是一样的,但是本质区别就是unordered_map底层是ha ...

  6. Unix版权史

    原文出处: 阮一峰    这几天,我在读<Unix编程艺术>. 书中介绍了Unix的发展历史.我发现,这是一个很好的例子,说明现行版权制度具有阻碍社会发展的负面作用. 2. Unix诞生于 ...

  7. JVM监控工具介绍jstack, jconsole, jinfo, jmap, jdb, jstat(复制)

    jstack -- 如果java程序崩溃生成core文件,jstack工具可以用来获得core文件的java stack和native stack的信息,从而可以轻松地知道java程序是如何崩溃和在程 ...

  8. python-生成器即send()用法

    参考链接: http://www.mamicode.com/info-detail-2399245.html 作者首先介绍了生成器的作用:是为了让程序员可以更简单的编写用来产生值的序列的代码,然后又介 ...

  9. CMSIS-RTOS 时间管理之虚拟定时器Virtual Timers

    虚拟定时器Virtual Timers CMSIS-RTOS API里有几个向下计数的虚拟定时器,它们实现计数完成时用户的回调功能.每个定时器都可以配置成单次计数或重复计数模式,它们可以在定义定时器结 ...

  10. 题解 CF915D 【Almost Acyclic Graph】

    这道题我第一次的想法是直接判环的数量,然而事实证明实在是太naive了. 随便画个图都可以卡掉我的解法.(不知道在想什么) 这道题的正解是拓扑排序. 朴素的想法是对所有边都跑一次拓扑,但这样$O(m( ...