3

I've built an Arcobjects add-in tool that creates point features OnMouseDown. I set up the tool's snapping environment with ISnappingEnvironment using code from the Arcobjects Help page Working with the ArcGIS snapping environment and it works great.

When using tools in ArcMap (e.g. Measurement tool, Create features), users have the option to temporarily disable snapping by holding down the spacebar. However, when using the add-in tool I created, holding down the space does not temporarily disable snapping.

How can I plug that spacebar functionality into my programmatic snapping environment?

I've tried checking if snapping is enabled via ISnappingEnvironment.Enabled and then UnInitializing the snapping feedback, but it seems that holding the spacebar does not change ISnappingEnvironment.Enabled to false.

The snapping code I am using is from "Working with the ArcGIS snapping environment", but here it is:

ISnappingEnvironment pSnappingEnv;
IPointSnapper pSnapper;
ISnappingFeedback pSnappingFeedback;
IPoint pPosition;

protected override void OnActivate()
    {
        InitializeSnapping();
    }

protected override bool OnDeactivate()
    {
        if (pSnappingFeedback != null)
        {
             pSnappingFeedback.UnInitialize();
        }
    }
private void InitializeSnapping()
    {
        // Get the snap environment and initialize the feedback
        UID snapUID = new UID();
        snapUID.Value = "{E07B4C52-C894-4558-B8D4-D4050018D1DA}";

        pSnappingEnv = ArcMap.Application.FindExtensionByCLSID(snapUID) as 
        ISnappingEnvironment;
        pSnapper = pSnappingEnv.PointSnapper;

        pSnappingFeedback = new SnappingFeedbackClass();
        pSnappingFeedback.Initialize(ArcMap.Application, pSnappingEnv, true);
    }

protected override void OnRefresh(int hDC)
    {
        if (pSnappingFeedback != null)
        {
            pSnappingFeedback.Refresh(hDC);
        }
    }

protected override void OnMouseMove(MouseEventArgs arg)
    {
        //Get the current position in map units... This is the cursor location before snapping
        pPosition = ArcMap.Document.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(arg.X, arg.Y);

        if (pPosition != null)
        {
            ISnappingResult snapResult = null;
            //Try to snap the current position
            snapResult = pSnapper.Snap(pPosition);
            pSnappingFeedback.Update(snapResult, 0);

            if (snapResult != null)
            {
                pPosition = snapResult.Location; //Snapping occurred. Set the current position to the snapped location
            }
            else
            {
                IPoint point = ArcMap.Document.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(arg.X, arg.Y);
            }
        }
    }
3

You can override OnKeyDown and OnKeyUp. Check if it was the space bar that is up and down. You can then set pSnappingEnv.Enabled to True or False which should toggle snapping on and off.

    protected override void OnKeyDown(KeyEventArgs arg)
    {
        if (arg.KeyCode == System.Windows.Forms.Keys.Space)
            pSnappingEnv.Enabled = false;
    }

    protected override void OnKeyUp(KeyEventArgs arg)
    {
        if (arg.KeyCode == System.Windows.Forms.Keys.Space)
            pSnappingEnv.Enabled = true;
    }

I'm testing this on 10.2, but I doubt this has changed in 10.4.

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.