bgrund

Simple select and zoom code works in Python window but not as a script

Discussion created by bgrund on Jan 8, 2013
Latest reply on Jan 8, 2013 by bgrund
Hi all,

I'm working on a script tool that automatically selects and zooms based on user inputted Township, Range, and Section. The code runs and works perfectly in the Python window. It runs as a script tool with no errors, but does not zoom to the designated location... I suspect it's a problem with the MakeFeatureLayer command but I'm not sure. It's messy because i tried to use arcpy.MakeFeatureLayer(WySectns, "WySectnsLyr") but it was not working properly; this line was taken from a modelbuilder export. Here's the code:

import arcpy
from arcpy import env
env.workspace = 'C:\Users\...'
arcpy.env.overwriteOutput = True

# Local variables:
WySectns = "C:\\Users\\..."
WySectns_Lyr = "WySectns_Lyr"
mxd = arcpy.mapping.MapDocument("CURRENT") 

#User Defined Parameters
Township = arcpy.GetParameterAsText(0)
Range = arcpy.GetParameterAsText(1)
Section = arcpy.GetParameterAsText(2)

#Make Feature Layer
arcpy.MakeFeatureLayer_management(WySectns, WySectns_Lyr, "", "", "OBJECTID OBJECTID VISIBLE NONE;Shape Shape VISIBLE NONE;AREA AREA VISIBLE NONE;PERIMETER PERIMETER VISIBLE NONE;STATE_S_ STATE_S_ VISIBLE NONE;STATE_S_ID STATE_S_ID VISIBLE NONE;SECTION_ SECTION_ VISIBLE NONE;TR TR VISIBLE NONE;MERIDIAN MERIDIAN VISIBLE NONE;Shape_Length Shape_Length VISIBLE NONE;Shape_Area Shape_Area VISIBLE NONE")
#Select Layer By Attribute
quote = "\""
whereClause = "[TOWNSHIP] = " + quote + Township + quote +" AND [RANGE] = " + quote + Range + quote +" AND [SECTION_] = " + quote + Section + quote
arcpy.SelectLayerByAttribute_management(WySectns_Lyr, "NEW_SELECTION", whereClause)

#zoom to selected features
df = arcpy.mapping.ListDataFrames(mxd) [0]
df.zoomToSelectedFeatures()
#df.extent = WySectns_Lyr.getSelectedExtent()
arcpy.RefreshActiveView()

#Clean layer files
arcpy.Delete_management(WySectns_Lyr)



Thanks for any help!
Brigid

Outcomes