English

How To: Define a snapping environment in ArcMap using VBA

Summary

ISnapEnvironment can reference the properties of SnapAgent FeatureSnap objects.

Procedure

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

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


    Note:
    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.

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

    Note:
    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.

    Code:
    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.

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

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