This document is archived and information here might be outdated.  Recommended version.


How to work with the snap environment (ArcObjects .NET 10.6 SDK)
ArcObjects Help for .NET developers > Developing with ArcGIS > Learning ArcObjects > Editing data > ArcGIS Engine editing > How to work with the snap environment (ArcObjects .NET 10.6 SDK)

How to work with the snap environment


Summary
The snap environment manages snap agents and snap tolerance and is responsible for attempting to snap vertices and points using the snap environment settings. This topic includes code examples and methods to best manage snapping programmatically.

In this topic


Working with the snap environment

The ArcGIS Engine editor's snap environment (IEngineSnapEnvironment) controls each snap agent (IEngineSnapAgent), hit type settings, and snapping tolerance. The hit type of each feature snap agent is managed through the IEngineFeatureSnapAgent interface. All settings can be manually modified or verified on the Snapping Settings dialog box. See the following screen shot:
The ArcGIS Engine editor's snap environment also controls the snap tolerance. If required, you can provide an interface to allow the end user to manage the snap tolerance.
See the following code example to set up basic snap environment settings and turn on snap tips:
[C#]
public void SnapEnvirSettings(IEngineEditor editor)
{
    //Get the snap environment from the editor.
    IEngineSnapEnvironment snapEnvironment=editor as IEngineSnapEnvironment;

    //Ensure there is a snap agent turned on in the snap environment.
    if (snapEnvironment.SnapAgentCount == 0)
    {
        System.Windows.Forms.MessageBox.Show(
            "You need to turn on at least one snapping agent!!!");
        return ;
    }

    //Code to display the snap tolerance in a message box. 
    double tolerance=snapEnvironment.SnapTolerance;
    System.Windows.Forms.MessageBox.Show(Convert.ToString(tolerance));

    //Set the snap tolerance.
    snapEnvironment.SnapToleranceUnits =
        esriEngineSnapToleranceUnits.esriEngineSnapToleranceMapUnits;
    snapEnvironment.SnapTolerance=15;

    //Turn on snap tips.
    ((IEngineEditProperties2)editor).SnapTips=true;
}
[VB.NET]
Public Sub SnapEnvirSettings(ByVal editor As IEngineEditor)
    'Get the snap environment from the editor.
    Dim snapEnvironment As IEngineSnapEnvironment=editor As IEngineSnapEnvironment
    
    'Ensure there is a snap agent turned on in the snap environment.
    If snapEnvironment.SnapAgentCount=0 Then
        System.Windows.Forms.MessageBox.Show("You need to turn on at least one snapping agent!!!")
        Return
    End If
    
    'Code to display the snap tolerance in a message box.
    Dim tolerance As Double=snapEnvironment.SnapTolerance
    System.Windows.Forms.MessageBox.Show(Convert.ToString(tolerance))
    
    'Set the snap tolerance.
    snapEnvironment.SnapToleranceUnits=esriEngineSnapToleranceUnits.esriEngineSnapToleranceMapUnits
    snapEnvironment.SnapTolerance=15
    
    'Turn on snap tips.
    ((IEngineEditProperties2)editor).SnapTips=True
End Sub
This method checks the snap environment for selected snap agents. Feature snap agents are checked if the HitPartType is not equal to esriGeometryPartNone. The other snap agents are only registered if they are checked; therefore, if the number of snap agents is greater than the number of feature snap agents, a snap agent (sketch snap agent) is checked.
See the following code example:
[C#]
private bool CheckIsAnySnapAgentSelected(IEngineSnapEnvironment snapEnvironment)
{
    int snapAgentCount=snapEnvironment.SnapAgentCount;
    int checkedFeatureSnapAgentCount=0;
    int featureSnapAgentCount=0;

    //Loop through all registered snap agents in the snap environment. Count feature snap agents,
    //checked feature snap agents, and nonfeature snap agents.
    for (int i=0; i < snapAgentCount; i++)
    {
        IEngineSnapAgent currentSnapAgent=snapEnvironment.get_SnapAgent(i);
        if (currentSnapAgent is IEngineFeatureSnapAgent)
        {
            IEngineFeatureSnapAgent featureSnapAgent=currentSnapAgent as
                IEngineFeatureSnapAgent;
            featureSnapAgentCount++;

            //Determine if the feature snap agent is checked.
            if (featureSnapAgent.HitType !=
                esriGeometryHitPartType.esriGeometryPartNone)
            {
                checkedFeatureSnapAgentCount++;
            }
        }
    }
    if (checkedFeatureSnapAgentCount > 0 || snapAgentCount > featureSnapAgentCount)
    {
        return true;
    }
    else
    {
        return false;
    }
}
[VB.NET]
Private Function CheckIsAnySnapAgentSelected(ByVal snapEnvironment As IEngineSnapEnvironment) As Boolean
    Dim snapAgentCount As Integer=snapEnvironment.SnapAgentCount
    Dim checkedFeatureSnapAgentCount As Integer=0
    Dim featureSnapAgentCount As Integer=0
    
    'Loop through all registered snap agents in the snap environment. Count feature snap agents,
    'checked feature snap agents, and nonfeature snap agents.
    Dim i As Integer
    For i=0 To snapAgentCount - 1 Step i + 1
        Dim currentSnapAgent As IEngineSnapAgent=snapEnvironment.get_SnapAgent(i)
        If TypeOf currentSnapAgent Is IEngineFeatureSnapAgent Then
            Dim featureSnapAgent As IEngineFeatureSnapAgent=currentSnapAgent As IEngineFeatureSnapAgent
            featureSnapAgentCount=featureSnapAgentCount + 1
            
            'Determine if the feature snap agent is checked.
            If featureSnapAgent.HitType <> esriGeomeTryHitPartType.esriGeomeTryPartNone Then
                checkedFeatureSnapAgentCount=checkedFeatureSnapAgentCount + 1
            End If
        End If
    Next
    If checkedFeatureSnapAgentCount > 0 Or snapAgentCount > featureSnapAgentCount Then
        Return True
    Else
        Return False
    End If
End Function

Snap agents

Snap agents implement the IEngineSnapAgent interface and, when registered as a component category, are inserted into the ESRI snap agents component category. However, the feature snap agent (IEngineFeatureSnapAgent) is a more detailed class of snap agent, and each feature class has a feature snap agent instantiated when the snap environment window is first opened, if it hasn’t already been created programmatically.
See the following code example showing how to create a feature snap agent programmatically:
[C#]
public void AddNewSnapAgent()
{
    IEngineEditor editor=new EngineEditorClass();
    IEngineEditLayers editLayers=editor as IEngineEditLayers;
    IEngineSnapEnvironment snapEnvironment=editor as IEngineSnapEnvironment;

    //Check that the user is editing; otherwise, there will be no snap agent loaded.
    if (editLayers.TargetLayer == null)
    {
        System.Windows.Forms.MessageBox.Show("Please start an edit session");
        return ;
    }

    //Clear all existing snap agents.
    snapEnvironment.ClearSnapAgents();

    //Create a feature snap agent.
    IEngineFeatureSnapAgent featureSnapAgent=new EngineFeatureSnap();
    IFeatureClass layerFeatureClass=editLayers.TargetLayer.FeatureClass;
    featureSnapAgent.FeatureClass=layerFeatureClass;
    featureSnapAgent.HitType=esriGeometryHitPartType.esriGeometryPartBoundary;

    //Activate only the snap agent for the target layer.
    snapEnvironment.AddSnapAgent(featureSnapAgent);
}
[VB.NET]
Public Sub AddNewSnapAgent()
    Dim editor As IEngineEditor=New EngineEditorClass()
    Dim editLayers As IEngineEditLayers=editor As IEngineEditLayers
    Dim snapEnvironment As IEngineSnapEnvironment=editor As IEngineSnapEnvironment
    
    'Check that the user is editing; otherwise, there will be no snap agent loaded.
    If editLayers.TargetLayer Is Nothing Then
        System.Windows.Forms.MessageBox.Show("Please start an edit session")
        Return
    End If
    
    'Clear all existing snap agents.
    snapEnvironment.ClearSnapAgents()
    
    'Create a feature snap agent.
    Dim featureSnapAgent As IEngineFeatureSnapAgent=New EngineFeatureSnap()
    Dim layerFeatureClass As IFeatureClass=editLayers.TargetLayer.FeatureClass
    featureSnapAgent.FeatureClass=layerFeatureClass
    featureSnapAgent.HitType=esriGeomeTryHitPartType.esriGeomeTryPartBoundary
    
    'Activate only the snap agent for the target layer.
    snapEnvironment.AddSnapAgent(featureSnapAgent)
End Sub
Programmatically changing the snap environment parameters does not require opening the snap window to change the settings. In ArcGIS Engine, the snap window reflects the snap environment settings while it is open. All IEngineSnapAgents, such as the edit sketch vertices snap agent, are visible on the snap window even if they have been programmatically removed; this allows the end user to turn them on and off as needed.

Snapping in z-dimension

ArcGIS Engine does not currently support snapping in z-dimension.

Snap point method

To use the SnapPoint method from the IEngineSnapEnvironment interface, an IPoint is passed to the method and used to find the closest feature to snap to. Using SnapPoint calls the IEngineSnapAgent.Snap method to then call each snap agent in priority order until it finds one that returns true. This results in new coordinates that are then assigned to the original point.
The order in which snap agents are added to the snap environment determines the priority of snap agents. This priority order is reflected in the snap environment window and can be changed using the window.


See Also:

IEngineEditProperties2.SnapTips Property




Development licensing Deployment licensing
Engine Developer Kit Engine
ArcGIS for Desktop Basic
ArcGIS for Desktop Standard
ArcGIS for Desktop Advanced