Select and copy features in ArcMap using Python add-in tool
You will probably want to use onMouseDownMap
rather than onMouseDown
as this returns the location in map coordinates, not window coordinates.
Additionally, be sure to pass in a valid SpatialReference
object to the PointGeometry
constructor, otherwise it will most likely not work. In the example below I use the spatial reference of the active data frame.
Lastly you may want to specify a search_distance
on your SelectLayerByLocation
so that point and line features can be selected without clicking on them exactly. In ArcObjects you would normally use ArcMap's selection tolerance in pixels and expand your point's envelope by that amount in map coordinates. I couldn't find a way to access ArcMap's selection tolerance setting in arcpy, but if you want to go with the default of 3 pixels (or pass in your own), you can pass the output of the function in this answer as a search_distance
(in inches) to SelectLayerByLocation.
def onMouseDownMap(self, x, y, button, shift):
mxd = arcpy.mapping.MapDocument("CURRENT")
pointGeom = arcpy.PointGeometry(arcpy.Point(x, y), mxd.activeDataFrame.spatialReference)
searchdistance = getSearchDistanceInches(mxd.activeDataFrame.scale)
lyr = arcpy.mapping.ListLayers(mxd)[0] # assumes you want to select features from 1st layer in TOC
arcpy.SelectLayerByLocation_management(lyr, "INTERSECT", pointGeom, "%d INCHES" % searchdistance)
arcpy.RefreshActiveView()