How to create an editing tool that draws freehand?

526
6
Jump to solution
07-13-2020 07:54 AM
BarbaraSchneider2
Occasional Contributor II

I would like to create an editing tool with which I can draw freehand.

In the constructor of the tool, I set the following:

IsSketchTool = true;
SketchType = SketchGeometryType.OpenLasso;

In the method HandleMouseDownAsync(MapViewMouseButtonEventArgs e), I get the current sketch because I want to  add the clicked point to it:

Polyline sketchPolyline = await base.GetCurrentSketchAsync() as Polyline;

In the method OnToolMouseMove(MapViewMouseEventArgs e), I do the same as above.

My question: what geometry type does the method  GetCurrentSketchAsync() return? It is not a polyline. But what else?

Thank you for your reply in advance.

0 Kudos
1 Solution

Accepted Solutions
NarelleChedzey
Esri Contributor

Barbara, 

I think if you add the following code which overrides the OnSketchModifiedAsync method you should be able to get what you need.   This method will be called on the first mouseClick.  You were on the right path with the GetCurrentSketchAsync method.  WIth the first mouseClick you should have a 1 point polyline.  You can use that point to check your intersection. 

protected override async Task<bool> OnSketchModifiedAsync()
{
  var geom = await this.GetCurrentSketchAsync();
  if (geom is Polyline polyline)
  {
    var ptCount = polyline.PointCount;
    var firstPt = polyline.Points[0];
  }
  var result = await base.OnSketchModifiedAsync();
  return result;
}

Narelle

View solution in original post

6 Replies
NarelleChedzey
Esri Contributor

The openLasso sketch type does produce a Polyline geometry via a streaming mechanism.  That is,  as the mouse moves points are added to the polyline, you do not need to do it via code.   The mouse down/mouse click with the openLasso sketch type will be the sketch Start and the sketch Complete.  There are no other mouse click's for this sketch type. 

 The code that the MapTool visual studio template gives you should be sufficient.  The polyline in the OnSketchCompleteAsync will have multiple points. 

internal class MapTool1 : MapTool
{
  public MapTool1()
  {
     IsSketchTool = true;
     SketchType = SketchGeometryType.OpenLasso;
     SketchOutputMode = SketchOutputMode.Map;
  }

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

  protected override Task<bool> OnSketchCompleteAsync(Geometry geometry)
  {
     var polyline = geometry as Polyline;
     return base.OnSketchCompleteAsync(geometry);
  }
}

BarbaraSchneider2
Occasional Contributor II

Hello Narelle,

thank you for your reply!

I have to catch the event when the user starts the sketch, i.e. the first OnMouseDown because I have to perform a test whether the first point of the sketch intersects another layer. In your answer, you mentioned something about sketch start. Is there a way to do this? I haven't found out how.

Thank you,

Barbara

0 Kudos
NarelleChedzey
Esri Contributor

Barbara, 

I think if you add the following code which overrides the OnSketchModifiedAsync method you should be able to get what you need.   This method will be called on the first mouseClick.  You were on the right path with the GetCurrentSketchAsync method.  WIth the first mouseClick you should have a 1 point polyline.  You can use that point to check your intersection. 

protected override async Task<bool> OnSketchModifiedAsync()
{
  var geom = await this.GetCurrentSketchAsync();
  if (geom is Polyline polyline)
  {
    var ptCount = polyline.PointCount;
    var firstPt = polyline.Points[0];
  }
  var result = await base.OnSketchModifiedAsync();
  return result;
}

Narelle

BarbaraSchneider2
Occasional Contributor II

Narelle,

thank you for your help!

I get the first point of my sketch polyline like you described above using GetCurrentSketchAsync(). This works fine.

Now I want to check whether the first point of my sketch intersects a point of a point feature layer. The problem is that the test always returns false because the coordinates of the first point of my sketch polyline and the point in the point feature layer are different, even if I snapped the sketch to the point. What does snapping actually do?

Barbara

0 Kudos
NarelleChedzey
Esri Contributor

Barbara, 

If you have snapping on, along with Point or Vertex snapping, then the first point of your sketch should be snapping to the point in the feature layer.  How are your checking to see whether they sketch intersects the feature layer? 

0 Kudos
BarbaraSchneider2
Occasional Contributor II

Hello Narelle,

this was my error, sorry about that. The two feature classes didn't have the same spatial reference.

Barbara

0 Kudos