import arcpy MXD=arcpy.mapping.mapdocument('current') #get access to the current mxd dataframe = arcpy.mapping.ListDataFrames(MXD, "Layers")[0] #grab the dataframe object you want (probably named Layers) dataframe.zoomToSelectedFeatures() #zoom to the feature in memory arcpy.RefreshActiveView() #refresh the map so that the extent change shows up
Thanks for the response. I caught that after posting, thats not it. I tried changing that line a lot of ways and the error always changes to whatever I change the "variable" to as I tried substituing in different expressions for what df. extent = AttributeError: 'str' object has no attribute 'zoomToSelectedFeatures'AttributeError: 'str' object has no attribute 'Layer'AttributeError: 'str' object has no attribute 'section'AttributeError: 'str' object has no attribute 'selected'AttributeError: 'str' object has no attribute 'getSelectedExtent'
I had that originally...tried changing it back just now, so script is currently import arcpymxd = ("G:\search_zoom\search_zoom.mxd")df=arcpy.mapping.ListDataFrames(MXD, "Layers")[0]Layer = (mxd, "section", df)[0]df.zoomToSelectedFeatures(layer)df.scale = df.scale * 1.1arcpy.RefreshActiveView()with error of:File "G:\kpw\arc_server\auto_assessment\search_zoom\zoom2selected2.py", line 3, in <module> df=arcpy.mapping.ListDataFrames(MXD, "Layers")[0]NameError: name 'MXD' is not definedI originally had the arcpy.mapping.... starting line 2 as well, but that gave me the same error, so I removed it and just went straight to a direct reference and then it read fine until the next arcpy.mapping....What am I missing?
import arcpy mxd = ("G:\search_zoom\search_zoom.mxd") df=arcpy.mapping.ListDataFrames(mxd, "Layers")[0] Layer = (mxd, "section", df)[0] df.zoomToSelectedFeatures(Layer) df.scale = df.scale * 1.1 arcpy.RefreshActiveView()
Mike,I got this to work for me, fill in your dataframe name and shapefile name where indicated. Make sure map is open and a feature is selected. This zooms to selected feature.import arcpymxd = arcpy.mapping.MapDocument('CURRENT')df = arcpy.mapping.ListDataFrames(mxd,"put name of top dataframe here")[0]Layer = (mxd, "put name of first shapefile in top dataframe here", df)[0]df.zoomToSelectedFeatures()arcpy.RefreshActiveView
THANKS SO MUCH. That fixed it and your annotated script helped to.
I am trying to execute a "Zoom to Selected" command at the end of a tool I am building in ModelBuilder. I have followed the advice on this thread and am almost all the way there. The model will work as I intend and it zooms to the selected features; however, when I save the model to a tool and try to run it from the toolbox, the zoom does not work and just zooms to the full extent of the map (as if there were no features selected). If I manually select "Zoom to Selected Features" from the "Selection" menu after the tool has run, it zooms as it should. I am including a screenshot of the model as well as the script I am using...If anyone has any tips they would be greatly appreciated.Cheers!
What needs to change in this code for it to be able to be published as a geoprocessing service? This code works for me in Desktop, but Server does not like the "current" mxd......
Try changing the 'Current' keyword to a path to an mxd
Many failed attempts until i saw this answer. You saved me. Thanks.
Přihlášení členové mohou přispívat, sledovat aktualizace a další. Jste tu noví? Zaregistrujte si bezplatný účet.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.