Define a snapping environment in ArcMap using VBA

Last Published: April 25, 2020


ISnapEnvironment can reference the properties of SnapAgent FeatureSnap objects.


  1. Get a reference to the ESRI Object Editor and start editing in the desired workspace.

    Dim pUID As UID
    Dim pEditor As IEditor
    Set pUID = New UID
    pUID = "esricore.Editor"
    Set pEditor = Application.FindExtensionByCLSID(pUID)
    pEditor.StartEditing pWorkspace

    This code example assumes the pWorkspace variable has been defined as an IWorkspace and has been set to a valid workspace.

  2. Define the snap environment. The following is an example of snapping to a feature class edge (such as a polygon edge) using the esriGeometryHitPartType of esriGeometryPartBoundary. This will snap to edges in the given feature class.

    Dim pSnapEnvironment As ISnapEnvironment
    Dim pFeatureSnapagent As IFeatureSnapAgent
    Set pSnapEnvironment = pEditor
    Set pFeatureSnapAgent = New FeatureSnap
    Set pFeatureSnapAgent.FeatureClass = pFeatureClass
    pFeatureSnapAgent.HitType = esriGeometryPartBoundary
    pSnapEnvironment.AddSnapAgent pFeatureSnapagent
    pSnapEnvironment.SnapToleranceUnits = esriSnapTolerancePixels
    pSnapEnvironment.SnapTolerance = 5

    This code example assumes the pFeatureClass variable has been defined as an IFeatureClass and has been set to a feature class with edges (such as polygons).

  3. Snap some sketch points to the feature class edges.

    Dim pPoint1 As IPoint
    Dim pPoint2 As IPoint
    Set pPoint1 = New Point
    Set pPoint2 = New Point
    pPoint1.X = 413188.31
    pPoint1.Y = 1124061.78
    pPoint2.X = 438944.57
    pPoint2.Y = 1123086.17
    pSnapEnvironment.SnapPoint pPoint1
    pSnapEnvironment.SnapPoint pPoint2

  4. Start a new feature sketch using the snapped points.

    Dim pSketchOperation As ISketchOperation
    Dim pEditSketch As IEditSketch
    Set pSketchOperation = New SketchOperation
    pSketchOperation.Start pEditor
    Set pEditSketch = pEditor
    pEditSketch.AddPoint pPoint1, True
    pEditSketch.AddPoint pPoint2, True
    pEnvelope.Expand 1.2, 1.2, True
    pSketchOperation.Finish pEnvelope

    This code assumes the pEnvelope was defined as IEnvelope and has the extent of the feature currently selected and being edited.

Article ID:000002754

  • ArcMap 8 x

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options