直接上代码:
IFeatureCache m_FeatureCache; bool m_CacheFilled; public bool Snap(IFeatureClass featureClass, IPoint point, double tolerance) // 参数 featureClass 是要捕捉的图层的 featureClass // 参数 point 是鼠标所在点,把它传进来,最后要返回捕捉到的点 // 参数 tolerance 用来设置捕捉容差,比如屏幕上捕捉7个像素范围内的对象 { int Index = 0; double Dist, minDist; IFeature feature; IPoint pCachePt, pSnapPt; IProximityOperator pProximity = (IProximityOperator)point; minDist = tolerance * 10; if (m_FeatureCache == null) m_FeatureCache = new FeatureCacheClass(); if (featureClass == null) return false; if (!m_CacheFilled) { FillCache(featureClass, point, 10); m_CacheFilled = true; } if (!m_FeatureCache.Contains(point)) FillCache(featureClass, point, tolerance * 10); pCachePt = new PointClass (); for (int Count = 0; Count < m_FeatureCache.Count; Count++) { feature = m_FeatureCache.get_Feature(Count); if (feature.Shape.GeometryType == esriGeometryType.esriGeometryPoint) { pCachePt = (IPoint)feature.Shape; // 取到一点 } else return false; Dist = pProximity.ReturnDistance(pCachePt); // 判断点到鼠标当前点的距离 if (Dist < minDist) { Index = Count; minDist = Dist; } } if (minDist >= tolerance) // 如果最小距离大于设置的容差,则不捕捉 return false; feature = m_FeatureCache.get_Feature(Index); pSnapPt = pCachePt; // 取距离最近的点为最后捕捉到的点 point.PutCoords(pSnapPt.X, pSnapPt.Y); // 把捕捉到的点的坐标传给鼠标当前点 return true; } private void FillCache(IFeatureClass featureClass, IPoint pPoint, double Distance) { m_FeatureCache.Initialize(pPoint, Distance); // 设置缓存 m_FeatureCache.AddFeatures(FClass); // 把featureclass中的feature提取到缓存中 }
更多GIS开发相关问题请加入 GIS开发学习QQ交流群 192251607 共同交流学习!