set the extent of a dataframe

495
2
12-07-2011 06:44 AM
VeraDiaz-Köhli
New Contributor
In my script I select a feature (layer with definition query) and set the extent of the dataframe to the extent of this feature. This works fine, as long as just one feature is selected.
Sometimes though, two features need to be selected. In this case, I do get the extent of the features and I can set the extent of the dataframe, but nothing gets drawn.

Here's my code:

    Layer_xy.definitionQuery = DefinitionQuery

    newExtent = Layer_xy.getExtent(True)
#  newExtent = Layer_xy.getSelectedExtent(True)
    df.extent = newExtent
    df.panToExtent(df.extent)
    
    print "Extent of feature:\nXMin: %f, YMin: %f, \nXMax: %f, YMax: %f" % \
                      (df.extent.XMin,df.extent.YMin,df.extent.XMax,df.extent.YMax)


I tried it both with Layer_xy.getExtent and Layer_xy.getSelectedExtent and with or without panToExtent, with Layer_xy.getSelectedExtent(True) or (False), but nothing was ever drawn...

Any suggestions? Thanks for your help.
Vera
Tags (2)
0 Kudos
2 Replies
DaveJordan1
New Contributor III
Have you tried RefreshActiveView?  Here is a sample of I use of something similar to what you are trying to do:

  df.extent = ParcelLayer.getSelectedExtent()
  df.scale = df.scale * 1.1  #  Add 10 percent to the extent
  arcpy.RefreshActiveView()  #  Force a redraw
0 Kudos
VeraDiaz-Köhli
New Contributor
It's working! My script is quite long by now and setting the extent of this dataframe on my layout is just one small part.
I tried exporting the map to a pdf right after I set the extent and it was drawn correctly. My problem was, that later in the script I set the size of the dataframe explicitly in a loop through all my layout-elements.  For some reason, whenever two features are selected the size of the dataframe is set to a size that doesn't fit on the layout anymore and consequently nothing is drawn.
Now I don't set the size anymore, and it works ok.

Thanks,
Vera
0 Kudos