public bool Snap(ESRI.ArcGIS.Geometry.IGeometry geom,
ESRI.ArcGIS.Geometry.IPoint point, double tolerance)
{
GetFeatureClass();
bool b_setNewFeatureCache = false;
if (m_featureClass == null || m_engineeditor == null)
return false;
if (m_featureClass.ShapeType != esriGeometryType.esriGeometryPoint)
return false;
//Check if a feature cache has been created.
if (!b_setNewFeatureCache)
{
m_featureCache = new FeatureCache();
b_setNewFeatureCache = true;
}
//Fill the cache with the geometries.
//It is up to the developer to choose an appropriate value
//given the map units and the scale at which editing will be undertaken.
FillCache(m_featureClass, point, 10000);
IProximityOperator proximityOp = point as IProximityOperator;
double minDist = tolerance;
IPoint cachePt = new PointClass();
IPoint snapPt = new PointClass();
IPolygon outPoly = new PolygonClass();
ITopologicalOperator topoOp;
IFeature feature;
int Index = 0;
for (int Count = 0; Count < m_featureCache.Count; Count++)
{
feature = m_featureCache.get_Feature(Count);
cachePt = feature.Shape as IPoint;
topoOp = cachePt as ITopologicalOperator;
//Set the buffer distance to an appropriate value
//given the map units and data being edited
outPoly = topoOp.Buffer(1000) as IPolygon;
double Dist = proximityOp.ReturnDistance(outPoly);
if (Dist < minDist)
{
Index = Count;
minDist = Dist;
}
}
//Make sure minDist is within the search tolerance.
if (minDist >= tolerance)
return false;
//Retrieve the feature and its part again.
feature = m_featureCache.get_Feature(Index);
cachePt = feature.Shape as IPoint;
topoOp = cachePt as ITopologicalOperator;
//Set the buffer distance to an appropriate value
//given the map scale and data being edited
outPoly = topoOp.Buffer(1000) as IPolygon;
proximityOp = outPoly as IProximityOperator;
snapPt = proximityOp.ReturnNearestPoint(point,esriSegmentExtension.esriNoExtension);
//Since point was passed in ByValue, we have to modify its values instead.
//of giving it a new address.
point.PutCoords(snapPt.X, snapPt.Y);
return true;
}