How do I activate/deactivate a MapTool?

2689
3
07-12-2016 11:31 AM
Highlighted
Occasional Contributor II

While the help at ArcGIS Pro 1.3 API Reference Guide talks about a Tool or a Button, there is no topic on MapTool.

I have inherited the code from Community Samples, but there are no clue how to activate or mostly to deactivate the tool on OnSketchCompleteAsync or - better - when clicking the tool button again.

I can switch the tool appearance with IsChecked = false on OnSketchCompleteAsync, but - beside the fact that the tool button becomes not checked - the tool continue to be active on the page!

MapTool seems to be part of ArcGIS.Desktop.Mapping namespace, but neither this part of the SDK Help talks about it...

(ArcGis Pro 1.3)

Tags (1)
Reply
0 Kudos
3 Replies
Highlighted
by Esri Regular Contributor
Esri Regular Contributor

The current map view requires a 'current' active tool.  You can look at this snippet how to change the current tool:

FrameworkApplication.SetCurrentToolAsync("esri_mapping_selectByRectangleTool");

where "esri_mapping_selectByRectangleTool" is the DAML id of the tool.

See:

https://github.com/esri/arcgis-pro-sdk/wiki/ProSnippets-Framework#set-the-current-tool

Reply
0 Kudos
Highlighted
Occasional Contributor II

I knew that - in fact I was spending several hours: I have a lot of issues with that!

  1. Simple code (copied directly from some samples:)

protected override async Task<bool> OnSketchCompleteAsync(Geometry geometry)

{

    if (Module1.GfxViewModel == null) return false;

    var popupContent = await QueuedTask.Run(() =>

    {

        var mapView = MapView.Active;

        if (mapView == null) return null;

        var popups = new List<PopupContent>();

        var results = mapView.GetFeatures(geometry);

        if (results != null && results.Any())

        {

             foreach (var kvp in results)

             {

                   kvp.Value.ForEach(id => popups.Add(new GfxPopupContent(kvp.Key, id, kvp.Key == Module1.GfxViewModel.DeltaLayer)));

             }

            mpView.FlashFeature(results);

        }

                return popups;

            })

            ActiveMapView.ShowCustomPopup(popupContent, m_commands ?? (m_commands = CreateCommands()), true);

            return true;

        }

I can deactivate this Tool by going in the Ribbon and activate the Explore Map Tool. I can reactivate it again, but then I have to click twice (once to start the rectangle and second to end the rectangle. After reactivating the tool, even if I click and drag, I have to click again for the sketching to end!

This is (a simplified version of) the constructor:

public PickupTool()

{

    IsSketchTool = true;

    SketchType = SketchGeometryType.Rectangle;

    Cursor = GetCursor("RectangleCursor.cur");

    UseSnapping = true;

    SketchOutputMode = SketchOutputMode.Screen;

}

As I have checked again and again, nothing executes twice (except OnSketchCompleteAsync) when activating the other tool and reactivating this one!

2. Before line 23, I'm adding this code:

try

{

    //await SetCurrentToolAsync(DAML.Tool.esri_mapping_exploreTool);

    var cmd = GetPlugInWrapper(DAML.Tool.esri_mapping_exploreTool) as ICommand;

    if ((cmd != null) && cmd.CanExecute(null))

    {

        cmd.Execute(null);

    }

}

catch (NullReferenceException)

{

}

I have a choice of (line 03) SetCurrentToolAsync or the code from line 04 to 08.

SetCurrentToolAsync is slow, but also executes only one. On second attempt it gives a NullRefferenceExemption. It also comes with the same behavior of clicking twice to select a rectangle on the second attempt, before crashing the application without catching the same exception in the catch block!

The un-commented code (04-08) works better (does not crashes,) but cmd.CanExecute alternates between false and true! So it selects the Explore Tool every second selection.

3. Anyway, selecting that tool makes the popup to disappear, so using this way, in this place would be useless if it would have worked correctly.

4. A better way would have been to be able to deactivate the Pick Tool after the popup closes(, as when the user clicks X or  outside it...) However I cannot find any way to wait for ShowCustomPopup method to close the popup...

(Sorry for such long replay, but I've spent most of this day on it! I think it may be helpful...)

Reply
0 Kudos
Highlighted
Esri Contributor

I can reproduce the tool behavior. Looks like a bug. Ideally, the developer could choose how he/she wants their tool to behave - either complete the sketch on MouseUp or require a second click. In part, this behavior is being inherited from the base class which is more geared for editing (rather than selection) where individual clicks (eg for digitizing) are required. Unfortunately you are getting a bit of both - one click the first time, two clicks subsequently. We will look at this for 1.4.

I will speak with the developers about the possibility of a "popup window closed" event.