AnsweredAssumed Answered

OnToolMouseDown click point

Question asked by bulla_b on Aug 7, 2020
Latest reply on Sep 17, 2020 by krisculin

Hi,

 

I'm creating a MapTool to help with creating a fully digitized sewer connection.  It works by sketching a line on the map;  the first click creates the connection to the sewer, the last click the 'end' of the line, and everything in between is the actual sewer connection.

 

Currently I check for the first click to be intersecting the gravityMainLayer in the OnSketchCompleteAsync.  It works, and I've created some IntersectsLayer code to determine an intersection of the first point.

 

        protected override Task<bool> OnSketchCompleteAsync(Geometry geometry)
        {
            var pointCollection = ((Multipart)geometry).Points;
            MapPoint connectionPoint = pointCollection[0];
            MapPoint plugPoint = pointCollection[pointCollection.Count - 1];

           

            QueuedTask.Run(() =>
            {
                if (IntersectsLayer(connectionPoint, gravityMainLayer) == false)
                {
                    ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("The starting point must intersect the GravitySewer layer.", "Error");
                    return;
                }               

                .....
                   
                blah, blah, blah,

                .....
   

            return base.OnSketchCompleteAsync(geometry);
        }


        public bool IntersectsLayer(MapPoint point, FeatureLayer layer)
        {
            try
            {  
                SpatialQueryFilter spatialFilter = new SpatialQueryFilter();
                spatialFilter.FilterGeometry = point;
                spatialFilter.SpatialRelationship = SpatialRelationship.Intersects;
               
                RowCursor theCursor = layer.Search(spatialFilter);
                Feature feature;
                while (theCursor.MoveNext())
                {
                    using (feature = (Feature)theCursor.Current)
                    {
                        return true;
                    }
                }

                return false;               
            }
            catch (Exception ex)
            {
                ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show(ex.ToString());
            }

            return false;
        }

 

 

To make things work better for the user, I am trying to have it check for the intersection after the first mouse click, to avoid the user making more clicks when not necessary.  So I am using the OnToolMouseDown to capture the first click, and the look for the intersection.  The problem is that each time the IntersectLayer always returns false, even though I am for sure clicking a point that is intersecting the gravityMainLayer.  As soon as I comment out the OnToolMouseDown code, everything works fine again.

 

So my question is should the OnToolMouseDown event return the same MapPoint as the OnSketchCompleteAsync event??  From stepping through the code, it looks like a valid point, but I don't understand why it is never intersecting.

 

        protected override void OnToolMouseDown(MapViewMouseButtonEventArgs e)
        {
            if (e.ChangedButton == System.Windows.Input.MouseButton.Left)
            {
                clickCount++;
                e.Handled = true; //Handle the event args to get the call to the corresponding async method
            }
            //base.OnToolMouseDown(e);
        }

        protected override Task HandleMouseDownAsync(MapViewMouseButtonEventArgs e)
        {
            if (clickCount == 1)
            {
                QueuedTask.Run(() =>
                {
                    var mapPoint = MapView.Active.ClientToMap(e.ClientPoint);
                    if (IntersectsLayer(mapPoint, gravityMainLayer) == false)
                    {
                        ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("The starting point must intersect the GravitySewer layer.", "Error");
                        clickCount = 0;
                    }
                });
            }

            return base.HandleMouseDownAsync(e);
        }

Outcomes