How to work with the snap environment


Summary
The snap environment manages snap agents and the snap tolerance. The snap environment 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 engine editor's snap environment, IEngineSnapEnvironment, controls each snap agent (IEngineSnapAgent), hit type settings, and the snapping tolerance. The hit type of each feature snap agent is managed through the IEngineFeatureSnapAgent interface. All of these settings can be manually modified or verified on the Snapping Settings dialog box. See the following screen shot:
The engine editor's snap environment also controls the snap tolerance. If required, the developer 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 snap tips on.
[Java]
public void snapEnvirSettings(IEngineEditor editor)throws Exception{

    //Get the snap environment from the editor.
    IEngineSnapEnvironment snapEnvironment = (IEngineSnapEnvironment)editor;

    //Make sure there is a snap agent turned on in the snap environment. 
    if (snapEnvironment.getSnapAgentCount() == 0){
        System.out.println("You need to turnon at least one snapping agent!!!");
        return ;
    }

    //Code to display the snap tolerance in a message box.
    double tolerance = snapEnvironment.getSnapTolerance();
    System.out.println("Tolerance: " + tolerance);

    //To set the snap tolerance.
    snapEnvironment.setSnapToleranceUnits
        (esriEngineSnapToleranceUnits.esriEngineSnapToleranceMapUnits);
    snapEnvironment.setSnapTolerance(15);

    //To turn snap tips on.
    ((IEngineEditProperties2)editor).setSnapTips(true);
}
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, so if the number of snap agents is more than the number of feature snap agents, a snap agent (sketch snap agent) is checked.
See the following code example:
[Java]
private boolean checkIsAnySnapAgentSelected(IEngineSnapEnvironment snapEnvironment){
    int snapAgentCount = snapEnvironment.getSnapAgentCount();
    int checkedFeatureSnapAgentCount = 0;
    int featureSnapAgentCount = 0;

    //Loop through all registered snap agents in the snap environment. 
    //Count feature snap agents, 
    //check feature snap agents and nonfeature snap agents. 

    for (int i = 0; i < snapAgentCount; i++){
        IEngineSnapAgent currentSnapAgent = snapEnvironment.getSnapAgent(i);
        if (currentSnapAgent instanceof IEngineFeatureSnapAgent){
            IEngineFeatureSnapAgent featureSnapAgent = (IEngineFeatureSnapAgent)
                currentSnapAgent;
            featureSnapAgentCount++;

            //Determine if the feature snap agent is checked. 
            if (featureSnapAgent.getHitType() !=
                esriGeometryHitPartType.esriGeometryPartNone){
                checkedFeatureSnapAgentCount++;
            }
        }
    }
    if ((checkedFeatureSnapAgentCount > 0) || (snapAgentCount >
        featureSnapAgentCount)){
        return true;
    }
    else{
        return false;
    }
}

Snap agents

Snap agents implement the IEngineSnapAgent interface. 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 has not already been created programmatically.
See the following code example to create a feature snap agent programmatically:
[Java]
public void addNewSnapAgent(){
    IEngineEditor editor = new EngineEditor();
    IEngineEditLayers editLayers = (IEngineEditLayers)editor;
    IEngineSnapEnvironment snapEnvironment = (IEngineSnapEnvironment)editor;

    //Check the user is editing; otherwise, there will be no
    //snap agent loaded that needs to be cleared. 

    if (editLayers.IEngineEditLayers_getTargetLayer() == null){
        System.out.println("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.IEngineEditLayers_getTargetLayer()
        .getFeatureClass();
    featureSnapAgent.setFeatureClassByRef(layerFeatureClass);
    featureSnapAgent.setHitType(esriGeometryHitPartType.esriGeometryPartBoundary);

    //Activate only the snap agent for the target layer.
    snapEnvironment.addSnapAgent(featureSnapAgent);
}
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 call each snap agent in priority order until it finds one that returns true. This results in new coordinates that are 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
Sample: Manage snap agents




Development licensingDeployment licensing
Engine Developer KitEngine
ArcGIS for Desktop Basic
ArcGIS for Desktop Standard
ArcGIS for Desktop Advanced