2

I am developing an ArcGIS Engine program. I am using the code below to activate snapping to a layer programmatically. I want to get the coordinates of the snapped point (clicked point). I know I must use ESRI.ArcGIS.Controls.ISnappingResult interface. I do not know how. Can anyone help me?

 public void AddNewSnapAgent(IEngineEditor editor)
    {
        //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.esriGeometryPartVertex;
        //Activate only the snap agent for the target layer.
        snapEnvironment.AddSnapAgent(featureSnapAgent); 
    }
1

I've not programmed in Engine but looking at the help file for Interface IEngineFeatureSnapAgent the method Snap states:

Snapping the passed in point means changing its x,y location properties. Because an IPoint interface pointer is passed into this function ByValue, direct edits of the point's parameters are possible. Resetting the IPoint interface to a different Point object will have no effect after leaving the scope of the function.

Without testing this it suggests to me that after the snap you can query the XY coordinates of the point for its new location.

  • Thank you. but that's the problem. I don't know how to do that. should I use FeatureCache or...? – M a Feb 14 '16 at 4:52
0

The code you have shared, shows how to add a new snap agent to the SnapEnvironment.

Once you have the SnapEnvironment set, all you need to do is use the ISnapEnvironment.SnapPoint method and pass the current mouse location. Then the SnapEnvironment will update the coordinates of the point to a new location, if it is able to snap it to the feature.

Below is a code showing how to get the snap point from snap agent on ITool MouseMove event.

public override void OnMouseMove(int Button, int Shift, int X, int Y)
{
     var activeView = m_hookHelper.ActiveView;
     var mapPoint = activeView.ScreenDisplay.DisplayTransformation.ToMapPoint(x, y);
     _snapPoint = mapPoint;
     var changed = m_engineSnapEnv.SnapPoint(_snapPoint);

     //use the new _snapPoint 
}

_snapPoint is a private field, so that you will be able to use it in other methods like MouseDown or MouseUp.

I know this is an old post, but hopefully it will help others who are looking for solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.