AnsweredAssumed Answered

Setting Data frame extent

Question asked by roryhall on Mar 7, 2013
Latest reply on Mar 10, 2013 by roryhall
Hello,
I have a script which updates a layer in a mxd on disc and want to set the extent of the Data Frame to the new layer, so when the mxd is opened the new layer is centered in the frame. I have poured of the web and found to get the extent of the layer to be zoomed to and set the data frame layer to that. ie "df.extent = lyr.getSelectedExtent(). When I run this script I get a RuntimeError DataFrameObject: Error in setting extent.

A simplified extract of my code as follows:
import arcpy

mxd = arcpy.mapping.MapDocument("C:\SRWBA\Braemar1959\Braemar1959.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "")[0]

for lyr in arcpy.mapping.ListLayers(mxd):
        if lyr.name == "BenefitArea_Symbology":
            print "found it"
            arcpy.SelectLayerByAttribute_management(lyr)
            print "selected"
            lyr_extent = lyr.getSelectedExtent()           

print df.extent
print lyr_extent

newExtent = df.extent
newExtent.XMin = lyr_extent.XMin
newExtent.YMin = lyr_extent.YMin
newExtent.XMax = lyr_extent.XMax
newExtent.YMax = lyr_extent.YMax

print newExtent
df.extent = newExtent
mxd.save()
del mxd, lyr
print "finished"

I have expanded on the code to try and make sense of what is going on. I get the extents I need, but cant seem to set the Data Frame extent. You help will be very appreciated, this is driving me insane as it must be something very simple I am missing. Thanks in advance.
Cheers ~ Rory

Outcomes