Original User: wjacksonI'm trying to write python-addin code to create a tool that would enable a user to click on a feature and get an attribute value. The attribute value would be used as a parameter to open an MS Access form (much later). In 10.1 the arcpy.SelectLayerByLocation should be able to use a point geometry but I cannot get it to work and need help. I know I need to create a layer from the feature I'm trying to select on. So, in my code I try to add the layer to the TOC just so I can see that it gets created and the feature selected. I cannot get the layer to add so do not know if a selection is made or not. import arcpy
from arcpy import env
import pythonaddins
import os
import subprocess
class ToolClass2(object):
"""Implementation for ArcGISAddins_addin.tool (Tool)"""
def __init__(self):
self.enabled = True
self.shape = "NONE" # Can set to "Line", "Circle" or "Rectangle" for interactive shape drawing and to activate the onLine/Polygon/Circle event sinks.
def onMouseDownMap(self, x, y, button, shift):
# Local variables:
arcpy.env.workspace = "C:/temp/python/test.gdb"
Parcels = "C:/temp/python/test.gdb/Parcels"
#message = "Your mouse clicked:" + str(x) + ", " + str(y)
#pythonaddins.MessageBox(message, "My Coordinates")
mxd = arcpy.mapping.MapDocument("current")
pointGeom = arcpy.PointGeometry(arcpy.Point(x, y), mxd.activeDataFrame.spatialReference)
df = mxd.activeDataFrame
searchdistance = getSearchDistanceInches(df.scale, 3)
#lyr = arcpy.mapping.ListLayers(mxd)[0].name # assumes you want to select features from 1st layer in TOC
# Make a layer from the feature class
arcpy.MakeFeatureLayer_management(Parcels, "feature_layer")
arcpy.SaveToLayerFile_management("feature_layer", "C:/temp/python/test.gdg/Parcels.lyr")
addlayer = arcpy.mapping.Layer("C:/temp/python/Parcels.lyr")
arcpy.mapping.AddLayer(df, addlayer, "BOTTOM")
arcpy.SelectLayerByLocation_management("feature_layer", "INTERSECT", pointGeom, "%d INCHES" % searchdistance,"NEW_SELECTION")
arcpy.RefreshActiveView()
#arcpy.Delete_management("feature_layer")