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

最新文章

  1. 根据 url请求数据
  2. Maven学习之 插件plugin
  3. PHP mcrypt加密扩展使用总结
  4. lamp环境编译错误
  5. jquery 更换皮肤
  6. Mysql 半同步复制配置
  7. C#后台执行bat
  8. C# 6 与 .NET Core 1.0 高级编程 - 41 ASP.NET MVC(下)
  9. Asp.net Authorization 学习
  10. python cookbook第三版学习笔记十三:类和对象(三)描述器
  11. [译] 所有你需要知道的关于完全理解 Node.js 事件循环及其度量
  12. Django框架中的model(操作数据库)
  13. public,private,protected,以及不写时的差别
  14. MongoDB入门系列(四):权限管理
  15. 【python学习笔记】9.魔法方法、属性和迭代器
  16. 6、Android Content Provider测试
  17. boostrap中模态框显示在阴影之下
  18. linux 系统调用之文件操作
  19. Newtonsoft.Json WindowPhone7.1
  20. Nginx 单个进程允许的最大连接数

热门文章

  1. ELK搭建(filebeat、elasticsearch、logstash、kibana)
  2. [SDOI2010] 古代猪文 (快速幂+中国剩余定理+欧拉定理+卢卡斯定理) 解题报告
  3. BZOJ 2424 DP OR 费用流
  4. PostgreSQL服务器存储参数的内部查看方法和实际表述之间的关系
  5. 《剑指offer》合并两个排序的链表
  6. mac本 maven项目还没发布成功,tomcat就报没有监听ContextLoaderListener 的解决方法
  7. php数据类型及运算
  8. 关于thinkphp 命令行
  9. C语言操作数截断
  10. ECNUOJ 2619 询问