Select to view content in your preferred language

Move feature by the mouse

616
2
06-23-2022 11:01 PM
DavidMrázek
Frequent Contributor

Hi,

is there any way to move the selected feature by dragging the mouse? I know this is normal editing in ArcGIS Pro, but I also want to incorporate an element selection that I already have set up.

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

        protected override Task<bool> OnSketchCompleteAsync(Geometry geometry)
        {
            return QueuedTask.Run(() => Selection(geometry));
        }

        protected async Task<bool> Selection(Geometry geometry)
        {
            await QueuedTask.Run(() =>
            {
                MapView mapView = MapView.Active;
                const string bodyAnno = "BodyAnno";
                const string body = "Body";
                const string ramyPresahu25 = "ramyPresahu_25";
                var insp = new Inspector();
                AnnotationLayer alBodyAnno = mapView.Map.FindLayers(bodyAnno).FirstOrDefault() as AnnotationLayer;
                FeatureLayer flBody = mapView.Map.FindLayers(body).FirstOrDefault() as FeatureLayer;
                FeatureLayer flRamyPresahu25 = mapView.Map.FindLayers(ramyPresahu25).FirstOrDefault() as FeatureLayer;
                List<FeatureLayer> seznamVrstev = new List<FeatureLayer>()
                    {flBody, flRamyPresahu25};
                if (alBodyAnno == null)
                {
                    MessageBox.Show(@"Neexistuje.");
                    return;
                }

                foreach (var vrstva in seznamVrstev)
                {
                    if (vrstva == null)
                    {
                        MessageBox.Show(@"Neexistuje.");
                        return;
                    }
                }

                var editace = new EditOperation
                {
                    Name = "Posun anotace",
                    SelectNewFeatures = false
                };
                var features = mapView.GetFeatures(geometry);
                if (features.ContainsKey(alBodyAnno))
                {
                    var thisTag = features.FirstOrDefault(t => t.Key == alBodyAnno);
                    var oidSeznam = thisTag.Value;
                    long oid = 0;
                    string strana = null;
                    foreach (var longOid in oidSeznam)
                    {
                        oid = longOid;
                    }
                    var filter = new QueryFilter
                    {
                        WhereClause = " OBJECTID = " + oid
                    };
                    var anotaceHledana = alBodyAnno.Search(filter);
                    while (anotaceHledana.MoveNext())
                    {
                        var aktual = anotaceHledana.Current;
                        strana = aktual["STRANA"].ToString();
                    }
                    var anotaceZvolena = alBodyAnno.Select(filter);
                    insp.Load(alBodyAnno, oid);
                    AnnotationProperties annoProperties = insp.GetAnnotationProperties();
                    var shape = annoProperties.Shape;
                    var envelope = shape.Extent;
                    var mapPointCentroid = GeometryEngine.Instance.Centroid(envelope);
                    double souradniceStrduX = mapPointCentroid.X;
                    double souradniceStreduY = mapPointCentroid.Y;
                    if (strana == "horni" || strana == "dolni")
                    {
                        if (shape.GeometryType != GeometryType.GeometryBag)
                        {
                            HandleMouseUp(e);
                            var dictionary = GetMousePositionWindowsForms();
                            var souradniceY = dictionary.FirstOrDefault(x => x.Key == "Y").Value;
                            var posun = GeometryEngine.Instance.Move(shape, souradniceStrduX, souradniceY);
                            annoProperties.Shape = posun;
                        }
                    }
                    else
                    {
                        if (shape.GeometryType != GeometryType.GeometryBag)
                        { 
                            HandleMouseUp(e);
                            var dictionary = GetMousePositionWindowsForms();
                            var souradniceX = dictionary.FirstOrDefault(x => x.Key == "X").Value;
                            var posun = GeometryEngine.Instance.Move(shape, souradniceX, souradniceStreduY);
                            annoProperties.Shape = posun;
                        }
                    }
                    insp.SetAnnotationProperties(annoProperties);
                    editace.Modify(insp);
                }
                else
                {
                    MessageBox.Show("Nebyla zvolena anotace!");
                    return;
                }
                MouseButtonEventArgs e;//I don't know how to set it up
                OnMouseDoubleClick(editace, e);
            });
            return true;
        }
        void OnMouseDoubleClick(EditOperation editace, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left)
            {
                editace.Execute();
            }
        }
        void HandleMouseUp(MapViewMouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left)
            {
                GetMousePositionWindowsForms();
            }
        }
        public static Dictionary<string,double> GetMousePositionWindowsForms()
        {
            Dictionary<string,double> list = new Dictionary<string, double>();
            var point = Control.MousePosition;
            list.Add("X",point.X);
            list.Add("Y",point.Y);
            return list;
        }
    }

Finally, I would like to end the method by double-clicking the mouse. Is it possible to do that?

 

Thank you

David

0 Kudos
2 Replies
NarelleChedzey
Esri Contributor

Hi David, 

I'd recommend taking a look at this section of the map exploration ProConcepts with regards to handling mouse down in a MapTool  https://github.com/ArcGIS/arcgis-pro-sdk/wiki/ProConcepts-Map-Exploration#mouse-and-keyboard-events

 

and this sample which offers as example (In particular look at the way the BasicMapTool uses the OnToolMouseDown and HandleMouseDownAsync methods)

https://github.com/Esri/arcgis-pro-sdk-community-samples/tree/6078006a3943364f2a7931b19d1eb03c8a99e1...

 

There are similar options for MouseUp and MouseMove on the MapTool.   You could incorporate these methods into your solution. 

One option would be to note when a left mouse down occurs  (ie set a variable - mouseDown = true), trap the mouse move (and perform some action when mousedown == true), and finally note when the mouse up occurs (and clear the mouseDown variable).   You could execute the editOperation in the MouseUp if enough distance has been travelled by the mouse. 

 

I hope this gives you some ideas. 

Narelle

 

 

 

DavidMrázek
Frequent Contributor

Thank you very much, this will definitely help me to make some progress. Thanks again.

0 Kudos