I'm trying to write what *should be* a simple script to allow a user to input a nest number and zoom to that feature. I have been playing around with the script for two days now and alternately ArcMap either crashes or it tells me that the script has run successfully but it does not actually zoom in anywhere. Any suggestions? Script is below:
I've tried both the df.zoomToSelectedFeatures as well as the lyr.getSelectedExtent. ArcMap seems to crash more when using the zoomtoselectedfeatures.
Amy Meehan
import arcpy arcpy.AddMessage("Starting") val1 = arcpy.GetParameterAsText(0) arcpy.AddMessage(val1) mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd, "Main Map")[0] lyr = arcpy.mapping.ListLayers(mxd, <Your Feature Layer>, df)[0] arcpy.AddMessage(lyr.name) expression = "<Your Fieldname> = '"+val1+"'" arcpy.AddMessage(expression) arcpy.SelectLayerByAttribute_management(lyr,"NEW_SELECTION",expression) df.extent = lyr.getSelectedExtent() arcpy.RefreshActiveView()
mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd, "<Your Data Frame>")[0] lyr = arcpy.mapping.ListLayers(mxd, "<Your Layer>", df)[0] df.extent = lyr.getSelectedExtent()
I came across this thread after encountering the same problem as Amy and I too initially tried Mathew's code to no avail. I'm posting my experience with the hope that it saves someone frustration.
# So initially code wasn't working
df.extent = lyr.getSelectedExtent()
The solution in my case was that I had previously set the inset map's data frame extent properties to "Fixed Extent" (it's a MXD template I use a lot where prior I didn't want it to move from the state boundaries extent). Since the purpose of part of the script was to zoom to the selected features, I changed this property back to the default, "Automatic".
Subsequently, the code worked like it's supposed to.
df.extent = lyr.getSelectedExtent()