using System; using System.Collections.Generic; using System.Text; using System.IO; using ESRI.ArcGIS.Geometry; //For geometry IPoint using ESRI.ArcGIS.Carto; //For ActiveView using ESRI.ArcGIS.Display; //For IScreenDisplay namespace myProject { public class GetCoord: ESRI.ArcGIS.Desktop.AddIns.Tool { public GetCoord() { } protected override void OnUpdate() { } protected override void OnMouseDown(ESRI.ArcGIS.Desktop.AddIns.Tool.MouseEventArgs arg) { try { //Get the active view IActiveView activeView = ArcMap.Document.ActiveView; IScreenDisplay screenDisplay = activeView.ScreenDisplay; //Get the point when the user clicks on the map IPoint myPoint = screenDisplay.DisplayTransformation.ToMapPoint(arg.X, arg.Y); System.Windows.Forms.MessageBox.Show("X coordinate from the IPoint: " + myPoint.X); System.Windows.Forms.MessageBox.Show("Y coordinate fom the IPoint: " + myPoint.Y); } catch (Exception e) { System.Windows.Forms.MessageBox.Show("Problem to get the coordinates: " + e.Message); } } } }
public override IPoint OnMouseDown(ESRI.ArcGIS.Desktop.AddIns.Tool.MouseEventArgs arg) { //Get the active view IActiveView activeView = ArcMap.Document.ActiveView; IScreenDisplay screenDisplay = activeView.ScreenDisplay; //Get the point when the user clicks on the map IPoint myPoint = screenDisplay.DisplayTransformation.ToMapPoint(arg.X, arg.Y); return myPoint; }
Ipoint myNewPoint = GetCoord.OnMouseDown(ESRI.ArcGIS.Desktop.AddIns.Tool.MouseEventArgs arg);
using System; using System.Collections.Generic; using System.Text; using System.IO; using ESRI.ArcGIS.Geometry; //For geometry IPoint using ESRI.ArcGIS.Carto; //For ActiveView using ESRI.ArcGIS.Display; //For IScreenDisplay namespace myProject { public class GetCoord: ESRI.ArcGIS.Desktop.AddIns.Tool { private static IPoint newpoint = null; public static IPoint NewPoint { get { return newpoint; } } public GetCoord() { } protected override void OnUpdate() { } protected override void OnMouseDown(ESRI.ArcGIS.Desktop.AddIns.Tool.MouseEventArgs arg) { try { if(newpoint == null) { newpoint = new Point(); } newpoint.PutCoords(arg.X,arg.Y); //Get the active view IActiveView activeView = ArcMap.Document.ActiveView; IScreenDisplay screenDisplay = activeView.ScreenDisplay; //Get the point when the user clicks on the map IPoint myPoint = screenDisplay.DisplayTransformation.ToMapPoint(arg.X, arg.Y); System.Windows.Forms.MessageBox.Show("X coordinate from the IPoint: " + myPoint.X); System.Windows.Forms.MessageBox.Show("Y coordinate fom the IPoint: " + myPoint.Y); } catch (Exception e) { System.Windows.Forms.MessageBox.Show("Problem to get the coordinates: " + e.Message); } } } }
using System; using System.Collections.Generic; using System.Text; using System.IO; using ESRI.ArcGIS.Geometry; //For the IPoint namespace myProject { public class GetCoord : ESRI.ArcGIS.Desktop.AddIns.Tool { private static IPoint newpoint = null; public static IPoint NewPoint { get { return newpoint; } } public GetCoord() { } protected override void OnUpdate() { } protected override void OnMouseDown(ESRI.ArcGIS.Desktop.AddIns.Tool.MouseEventArgs arg) { try { if (newpoint == null) { newpoint = new Point(); } newpoint.PutCoords(arg.X, arg.Y); } catch (Exception e) { System.Windows.Forms.MessageBox.Show("Problem to get coordinates : " + e.Message); } } } }
IPoint newLocation = GetCoord.NewPoint; System.Windows.Forms.MessageBox.Show("X Coordinate: " + newLocation.X); System.Windows.Forms.MessageBox.Show("Y Coordinate: " + newLocation.Y);
sshetty said :
Prior to invoking the point in another cs file did you select the GetCoord tool and clicked the mouse on the map.
ICommandItem cmd = ArcMap.Application.Document.CommandBars.Find("ID", false, false);
ArcMap.Application.CurrentTool = cmd;
Your new location tool would have its' own onMouseDown implementation where you would put that same code in it. The method is designed to be invoked by ArcMap when you mouse down on the map with your tool. You could write a static method that took in the mouseenventargs and the screendisplay as arguments and returns a point, but I don't think it is worth it. It is only a few lines of code and making a new function for it seems like a hassle. In my opinion code reuse is rarely worth the trouble and modularity in code is much more handy even if it means writing a few lines of code over and over. If you really want to, you can make an abstract class that implements mouseenventargs and the tool interface and then inherit the class, but to me that seems like using a cannon to go after flies.
If you are using Addin button then add below code in onclick method where "ID" is GetCoord id in Config.esriaddinx. Current tool will automatically invoke mouse down event.ICommandItem cmd = ArcMap.Application.Document.CommandBars.Find("ID", false, false); ArcMap.Application.CurrentTool = cmd;