Select to view content in your preferred language

Python cannot set extent correctly

1960
10
06-09-2011 04:51 AM
RichardKaufholz
Deactivated User
Hi there,

I am using the script from David Spriggs that prints a map to PDF. I am having a problem with the code "applying" the new extent to the map before printing.

The code is:
arcpy.AddMessage("Input extent pts: %s %s %s %s" % (newPntLL.X, newPntLL.Y, newPntUR.X, newPntUR.Y))

myExtent = dataFrame.extent
arcpy.AddMessage("Original extent pts: %s %s %s %s" % (myExtent.XMin, myExtent.YMin, myExtent.XMax, myExtent.YMax))
arcpy.AddMessage("... setting my extent xmin to: %s" % newPntLL.X)
myExtent.XMin = newPntLL.X
arcpy.AddMessage("... setting my extent ymin to: %s" % newPntLL.Y)
myExtent.YMin = newPntLL.Y
arcpy.AddMessage("... setting my extent xmax to: %s" % newPntUR.X)
myExtent.XMax = newPntUR.X
arcpy.AddMessage("... setting my extent ymax to: %s" % newPntUR.Y)
myExtent.YMax = newPntUR.Y
arcpy.AddMessage("New extent pts: %s %s %s %s" % (myExtent.XMin, myExtent.YMin, myExtent.XMax, myExtent.YMax))
dataFrame.extent = myExtent

arcpy.AddMessage("Final MAP extent pts: %s %s %s %s" % (dataFrame.extent.XMin, dataFrame.extent.YMin, dataFrame.extent.XMax, dataFrame.extent.YMax))


And the output is:
Input extent pts: 3448672.2243 -3490621.8401 3449675.4603 -3490056.9226
Original extent pts: 2055276.37123 -4018755.96024 2055518.36111 -4018583.26583
... setting my extent xmin to: 3448672.2243
... setting my extent ymin to: -3490621.8401
... setting my extent xmax to: 3449675.4603
... setting my extent ymax to: -3490056.9226
New extent pts: 2055518.36111 -4018583.26583 3449675.4603 -3490056.9226
Final MAP extent pts: 2055518.36111 -4251785.42335 3449675.4603 -3256854.76508


Baffling! Why does myExtent.XMin not show the 3448672 value? Its being explicitly defined there. Whats more confusing, is that it DOES show myExtent.XMax correctly...

Hopefully, its something obvious in there that I am missing,. Any help would be greatly appreicated.

Thanks in advance,
Richard.
Tags (2)
0 Kudos
10 Replies
DerivenC
Deactivated User
Well I found that my code works great for zooming to an extent but fails when displaying by scale and panning.  But yes I still have to set the numbers twice.

IEnvelope newEnvelope = mapDoc.ActiveView.Extent.Envelope;
newEnvelope.XMin = xMin;
newEnvelope.YMin = yMin;
newEnvelope.XMax = xMax;
newEnvelope.YMax = yMax;
newEnvelope.XMin = xMin;
newEnvelope.YMin = yMin;
newEnvelope.XMax = xMax;
newEnvelope.YMax = yMax;
mapFrame.MapBounds = newEnvelope;
mapFrame.ExtentType = esriExtentTypeEnum.esriExtentBounds;
activeView.Refresh();


To get scale to work, I have to do the same thing.  Then I have to save the document and THEN pan to a center point.  It's the only way I could get it to work.  I tried for hours.

IEnvelope newEnvelope = mapDoc.ActiveView.Extent.Envelope;
newEnvelope.XMin = xMin;
newEnvelope.YMin = yMin;
newEnvelope.XMax = xMax;
newEnvelope.YMax = yMax;
newEnvelope.XMin = xMin;
newEnvelope.YMin = yMin;
newEnvelope.XMax = xMax;
newEnvelope.YMax = yMax;
mapFrame.MapBounds = newEnvelope;
mapFrame.ExtentType = esriExtentTypeEnum.esriExtentBounds;
activeView.Refresh();

IPoint point = new Point();
point.X = ((xMin + xMax) / 2d);
point.Y = ((yMin + yMax) / 2d);
IEnvelope ab = mapFrame.MapBounds.Envelope;
ab.CenterAt(point);
mapFrame.MapBounds = ab;
activeView.Refresh();
Marshal.ReleaseComObject(activeView);
Marshal.ReleaseComObject(mapFrame);
Marshal.ReleaseComObject(frameElement);
Marshal.ReleaseComObject(graphicsContainer);
Marshal.ReleaseComObject(map);
SaveDocument();
OpenDocument();
map = mapDoc.ActiveView.FocusMap;
graphicsContainer = mapDoc.PageLayout as IGraphicsContainer;
frameElement = graphicsContainer.FindFrame(map);
mapFrame = (IMapFrame)frameElement;
activeView = (IActiveView)mapDoc.ActiveView;

mapFrame.ExtentType = esriExtentTypeEnum.esriExtentScale;
map.MapUnits = esriUnits.esriMeters;
mapFrame.MapScale = (double)request.scale;
activeView.Refresh();
Marshal.ReleaseComObject(point);


Works though.
0 Kudos