在Engine的工具(ITool)里:

OnClick事件处理函数中:

首先需要获取一个图层,作为Snapping的参照,

IFeatureLayer targetLayer

然后声明一个IMovePointFeedBack作为鼠标移动时捕捉点的显示:

IMovePointFeedback m_pMovePtFeed = new MovePointFeedback();
mFeedback = (IDisplayFeedback)m_pMovePtFeed;
ISimpleMarkerSymbol simpleMarkerSymbol = new SimpleMarkerSymbolClass();
IRgbColor pRGBColor = new RgbColorClass();
pRGBColor.Red = ;
pRGBColor.Green = ;
pRGBColor.Blue = ;
simpleMarkerSymbol.Color = pRGBColor;
simpleMarkerSymbol.Size = ;
simpleMarkerSymbol.Style = ESRI.ArcGIS.Display.esriSimpleMarkerStyle.esriSMSSquare;
ISymbol symbol = simpleMarkerSymbol as ISymbol;
symbol.ROP2 = esriRasterOpCode.esriROPNotXOrPen;
//symbol.ROP2 = esriRasterOpCode.;
m_pMovePtFeed.Symbol = (ISymbol)simpleMarkerSymbol;

然后, 开始Feedback的显示(tmpPoint是指开始的点,其实影响不大,如果不想要源点在屏幕上的话,可以取一个在屏幕外的点):

m_pMovePtFeed.Display = mMapControl.ActiveView.ScreenDisplay;
m_pMovePtFeed.Start(tmpPoint, tmpPoint);

在OnMouseMove事件中:

IPoint pPoint2 = null;
IPoint pPoint = pMap.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(x, y);
pPoint2 = Snapping(pPoint.X, pPoint.Y, targetLayer, pMap, );
if (pPoint2 == null)
pPoint2 = pPoint;
((IMovePointFeedback)mFeedback).MoveTo(pPoint2);

其中Snapping函数即为最主要的查找函数,其功能为根据输入的点坐标在目标图层上查找最为相近的点(也即需要捕捉的点),返回该点,若没有找到则返回NULL,最后一个参数的含义是,在地图控件上,以多少个像素为单位在周边查找捕捉点.

public IPoint Snapping(double x, double y, IFeatureLayer iFeatureLyr, IMapControl3 axMapControl1,double snappingDis)
{
IPoint iHitPoint = null;
IMap iMap = axMapControl1.Map;
IActiveView iView = axMapControl1.ActiveView;
IFeatureClass iFClss = iFeatureLyr.FeatureClass;
IPoint point = new PointClass();
point.PutCoords(x, y);
double length = ConvertPixelsToMapUnits(axMapControl1.ActiveView, snappingDis);
ITopologicalOperator pTopo = point as ITopologicalOperator;
IGeometry pGeometry = pTopo.Buffer(length).Envelope as IGeometry;
ISpatialFilter spatialFilter = new SpatialFilterClass();
spatialFilter.GeometryField = iFeatureLyr.FeatureClass.ShapeFieldName;
spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
spatialFilter.Geometry = pGeometry;
IFeatureCursor cursor = iFClss.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;
// Tolerance in pixels for line hits
double tol = ConvertPixelsToMapUnits(iView, snappingDis);
if (iHitTest.HitTest(point, tol, esriGeometryHitPartType.esriGeometryPartBoundary,
iHitPt, ref hitDist, ref partIndex, ref vertexIndex, ref bVertexHit))
{
iHitPoint = iHitPt;
}
//axMapControl1.ActiveView.Refresh();
return iHitPoint;
}
public double ConvertPixelsToMapUnits(IActiveView pActiveView, double pixelUnits)
{
double realWorldDisplayExtent;
int pixelExtent;
double sizeOfOnePixel;
pixelExtent = pActiveView.ScreenDisplay.DisplayTransformation.get_DeviceFrame().right - pActiveView.ScreenDisplay.DisplayTransformation.get_DeviceFrame().left;
realWorldDisplayExtent = pActiveView.ScreenDisplay.DisplayTransformation.VisibleBounds.Width;
sizeOfOnePixel = realWorldDisplayExtent / pixelExtent;
return pixelUnits * sizeOfOnePixel;
}

此时即可实现鼠标实时地捕捉目标图层上的对象,若需要获取当前位置的捕捉点时,则可以在相应事件(例如OnMouseDown或OnDbClick)中调用:

IPoint pPoint = ((IMovePointFeedback)mFeedback).Stop();

这时实时捕捉将会停止,若需要重新开始捕捉,则在之后调用这些语句即可:

//重新开始Snap
IPoint tmpPoint = new PointClass();
tmpPoint.PutCoords(pMap.Extent.XMin - , pMap.Extent.YMin - );
IMovePointFeedback m_pMovePtFeed = (IMovePointFeedback)mFeedback;
m_pMovePtFeed.Display = pMap.ActiveView.ScreenDisplay;
m_pMovePtFeed.Start(tmpPoint, tmpPoint);

engine的工具中实现Snapping(捕捉)的更多相关文章

  1. 使用chrome开发者工具中的network面板测量网站网络性能

    前面的话 Chrome 开发者工具是一套内置于Google Chrome中的Web开发和调试工具,可用来对网站进行迭代.调试和分析.使用 Network 面板测量网站网络性能.本文将详细介绍chrom ...

  2. F12 开发人员工具中的控制台错误消息

    使用此参考解释显示在 Internet Explorer 11 的控制台 和调试程序中的错误消息. 简介 使用 F12 开发人员工具进行调试时,错误消息(例如 EC7111 或 HTML1114)将显 ...

  3. Linux Netcat 命令——网络工具中的瑞士军刀

    原文:http://www.oschina.net/translate/linux-netcat-command netcat是网络工具中的瑞士军刀,它能通过TCP和UDP在网络中读写数据.通过与其他 ...

  4. 【译】在 Chrome 开发者工具中调试 node.js

    原文链接 : Debugging Node.js in Chrome DevTools 原文作者 : MATT DESLAURIERS 译文出自 : 掘金翻译计划 译文链接 : https://git ...

  5. 如何在视频处理控件TVideoGrabber中设置音频捕捉设备

    TVideoGrabber不仅可以捕捉视频,还可以作为多媒体播放器,并支持包括C#..NET.VB.NET.C++.Delphi.C++Builder和ActiveX平台,本文将剖析TVideoGra ...

  6. 在listener或者工具中使用spring容器中的bean实例

    在项目中经常遇见需要在Listener中或者工具中使用Spring容器中的bean实例,由于bean不能在stataic的类中使用. 介绍一种方式: public class SpringTool { ...

  7. 详解WPF Blend工具中的复合路径功能 ( 含路径标记语法 )

    写此文章的目的是为了简单分析一下 Blend工具中提供的"复合路径"功能.有人在我的博文中留言问我复合路径的问题.  稍微琢磨一下,觉得应该是对的.因此贴出来和大家分享.有不对的说 ...

  8. Mysql编辑工具中使用(Navicat查询结果显示行号)

    Mysql编辑工具中使用(Navicat查询结果显示行号) as rownum,a.roleId ) t where a.roleId='admin';

  9. Analyze 静态分析工具中显示 大量的CF类型指针 内存leak 问题, Core Foundation 类型指针内存泄漏

    Analyze 静态分析工具中显示 大量的CF类型指针 内存leak 问题   今天使用Analyze 看了下项目,   解决办法,项目中使用了ARC,OC的指针类型我们完全不考虑release的问题 ...

随机推荐

  1. cli下的php(并传递参数)

    传递参数有两种方式: 第一种使用文件操作,STDOUT作为标准输出,STDIN作为标准输入 使用fwrite($file,$string)作输出,使用fgets($file)作输入.这种应该算是继承自 ...

  2. 网站seo优化--jsoup 批量分析相关网站 标签,描述,关键词.

    网站seo优化--jsoup 批量分析相关网站 标签,描述,关键词. 因为自己写了一个磁力搜索网站Btgoogle,准备进行优化一下,需要分析其他的网站的优化情况. Java的Jsoup类库和PHP的 ...

  3. win8 开启wifi

    1,以管理员身份运行命令提示符,输入命令 netsh wlan set hostednetwork mode=allow ssid=networkname key=password后回车 其中ssid ...

  4. CSS3 学习

    border-radius: 半径,不用学了,用得很熟了,但要记得它的某一个角的写法是border-top/bottom-left/right-radius: ,参数中的两个值为先左右后上下,支持百分 ...

  5. POJ 2002 Squares 哈希

    题目链接: http://poj.org/problem?id=2002 #include <stdio.h> #include <string.h> ; struct Has ...

  6. Java NIO回炉

    重回东软了,据说可能要做一个跟文件相关的项目,于是决定把Java NIO的内容再捡起来,看看. 为什么要使用NIO,其实在低连接数的情况下,NIO的性能是要低于IO的:但是在高并发的情况下,确实NIO ...

  7. 【转载】TCP保活(TCP keepalive)

    下图是我遇到tcp keepalive的例子: 以下为转载: TCP保活的缘起 双方建立交互的连接,但是并不是一直存在数据交互,有些连接会在数据交互完毕后,主动释放连接,而有些不会,那么在长时间无数据 ...

  8. JavaScript学习代码整理(一)

    /** * Created by wyl on 15-1-23. */ function displayDate() { document.getElementById("demo" ...

  9. hdu 1281

    二分图,简单的模板题,不过题目比较难懂: 其中important chess就是删掉它不能够完美匹配,所以就枚举每一个可能删的棋子: 代码: #include <cstdio> #incl ...

  10. Linux 下编译、安装、配置 QT

    转自Linux 下编译.安装.配置 QT 注意:编译安装耗时费力,且很容易出错,要不断调整编译参数,不推荐使用,否则这将会是一个纠结痛苦的过程. 打算做嵌入式图像处理,计划方案嵌入式Linux+Ope ...