Center Snap Agent


The CenterSnap Agent is a sample that illustrates how to create a custom snap agent using Visual Basic. This snap agents snaps to the center of polygons using the editor's current snap tolerance. The snap agent has a special feature class property that enables you to set the polygon feature class you want to snap to.

How to use:
  1. Register the dll on your machine.
  2. Using Categories, register CenterSnap in the 'ESRI Snap Agents' component category.
  3. Start an edit session with at least one polygon layer loaded.
  4. Start VBA and paste in the VBA code from below. In References add 'CenterSnap'.
  5. Open the snapping dialog and the custom snap agent should appear in the bottom half of the window. Click it on.
  6. Set the editor's current layer equal to the polygon layer you want to snap to and then run the VBA script below. This script sets the feature class to snap to.
  7. Select the editor's sketch tool and notice its new snapping behavior.
Public Sub SetMySnapAgentFeatureClass()
  On Error Resume Next
  Dim pApp As IApplication
  Dim pEditor As IEditor
  Dim pSnapAgent As ISnapAgent
  Dim pSnapEnv As ISnapEnvironment
  Dim pEditLayers As IEditLayers
  Dim pMySnapAgent As CenterSnap.clsCenterSnap
  Dim pUID As New UID
  Dim Count As Integer

  pUID = "esriCore.Editor"
  Set pApp = Application
  Set pEditor = pApp.FindExtensionByCLSID(pUID)
  Set pEditLayers = pEditor
  Set pSnapEnv = pEditor
  'Find custom snap agent and set is featureclass property
  For Count = 0 To pSnapEnv.SnapAgentCount - 1
    Set pSnapAgent = pSnapEnv.SnapAgent(Count)
    If pSnapAgent.Name = "Center Snap" Then
      Set pMySnapAgent = pSnapAgent
      pMySnapAgent.FeatureClass = pEditLayers.CurrentLayer.FeatureClass
    End If
  Next Count

End Sub
Application: ArcMap

Requires: An edit session with a polygon layer.

Difficulty: Intermediate

Visual Basic
File Description
CenterSnap.cls Class file demonstrating how to create a snap agent.
CenterSnap.vbp The project file for the sample snap agent.
CenterSnap.dll The compiled project.

Key CoClasses: SnapAgent, FeatureCache
Key Interfaces: ISnapAgent, IFeatureCache
Key Members: ISnapAgent::Snap, IFeatureCache::Initialize, IFeatureCache::AddFeatures, IFeatureCache::Feature, IFeatureCache::Contains