Map Tool as a Select Tool

588
1
01-19-2018 12:43 PM
annegrimes2
New Contributor II

Maptool

I created a map-tool which splits a line. If the user has not selected anything and the tool is active, I want the tool to act as a selection tool and after the selection has been made, the tool should revert to a split line tool. This is what I have so far. The await ActivateSelectAsync does not return a value. I tried to set it to a result but I kept getting compile errors.

I thought about adding a timer before calling  StartSketchAsync? 

Do you have any ideas on how I might get this to work.

Thanks,

protected override async Task OnToolActivateAsync(bool hasMapViewChanged)
{
if (_attributeVM == null)

{

_attributeVM = this.EmbeddableControl as SplitParcelViewModel;

}

if (MapView.Active.Map.SelectionCount == 0)

{
IsSketchTool = false;
_attributeVM.Norecords = System.Windows.Visibility.Visible;
_attributeVM.typeofmessage = ArcGIS.Desktop.Internal.Framework.Controls.MessageType.Error;
_attributeVM.Changetext = "Nothing selected: Please select a segment to split";
await ActivateSelectAsync(true);
//need to wait for user to select something.
//MessageBox.Show("Please Select a line");


//This happens immediately. I need to ensure that the user has selected something before proceeding.
if (UseSelection == true)

{
IsSketchTool = true;
//MessageBox.Show("Something selected");
await StartSketchAsync();


//return Task.FromResult(true);
}
else
{
MessageBox.Show("Nothing selected");
//return Task.FromResult(false);
}
}
else
{
IsSketchTool = true;
_attributeVM.Norecords = System.Windows.Visibility.Collapsed;
//return Task.FromResult(true);
}
}

Tags (1)
0 Kudos
1 Reply
by Anonymous User
Not applicable

Anne,

Normally UseSelection = true in the tools constructor is all you need to make this happen. If there's no selection, the framework would hijack your tool and turn it into a rectangle selection tool until something is selected but unfortunately this is bugged.

The manual way would be something like:

  internal class tss : MapTool
  {
    public tss()
    {
      IsSketchTool = true;
      SketchType = SketchGeometryType.Line;
      SketchOutputMode = SketchOutputMode.Map;
      //UseSelection = true; This doesnt work
    }

    protected override Task OnToolActivateAsync(bool active)
    {
      SetSketchType();
      return base.OnToolActivateAsync(active);
    }

    protected override Task<bool> OnSketchCompleteAsync(Geometry geometry)
    {
      //if there is no selection then select features with a rectangle
      //else do something with the sketch
      if (MapView.Active.Map.SelectionCount == 0)
      {
        QueuedTask.Run(() =>
        {
          //select features with geom
          MapView.Active.SelectFeatures(geometry);
        });
      }
      //else
      //split with geometry
      return base.OnSketchCompleteAsync(geometry);
    }

    protected override Task OnSelectionChangedAsync(MapSelectionChangedEventArgs arg)
    {
      //note this is a hidden override method on MapTool
      SetSketchType();
      return base.OnSelectionChangedAsync(arg);
    }

    private void SetSketchType()
    {
      //set the sketch type based on selection count
      if (MapView.Active.Map.SelectionCount == 0)
        SketchType = SketchGeometryType.Rectangle;
      else
        SketchType = SketchGeometryType.Line;
    }
  }
0 Kudos