Saturday, 11 May 2019

c# - MapScale not being persisted properly in mxd when programmatically changed outside of ArcMap environment



I am trying to change the scale of a map in an mxd OUTSIDE of ArcMap in a standalone Windows application. The problem is the scale is never persisted - the best code I have come up with is the following:


private void UpdateMapScaleInMxd(double scale, string mxdFullPath)
{
IMapDocument mapDocument = new MapDocumentClass();
mapDocument.Open(mxdFullPath, "");
IPageLayout pageLayout = (IPageLayout)mapDocument.ActiveView;

IGraphicsContainer graphicsContainer = (IGraphicsContainer)pageLayout;
graphicsContainer.Reset();
IMapFrame mapFrame;


IActiveView tmpActiveView = null;

IElement element = graphicsContainer.Next();
while (element != null)
{
if (element is IMapFrame)
{
mapFrame = (IMapFrame)element;
tmpActiveView = (IActiveView)mapFrame.Map;


mapFrame.ExtentType = esriExtentTypeEnum.esriExtentScale;
mapFrame.MapScale = scale;
tmpActiveView.Refresh();
}

element = graphicsContainer.Next();
}

mapDocument.Save(false, false);

mapDocument.Close();
}

I check the mxd by opening it up in ArcMap and the map scale has changed i.e the data frame now has a fixed scale and the scale combobox is greyed out.


So now I try to export the map as a PDF in code - and the most frustrating thing is - it exports the map at the previous scale that was set - NOT the scale I just changed it to! So infuriating - if anyone can help me understand where I am going wrong that would be great.


UPDATE:


In the code above I am using the IMapDocument - and I think there are limitations (according to the help):



The IMapDocument interface provides properties and methods for reading map document files (*.mxd, *mxt, .pmf) and writing and saving changes to map document files (.mxd). However, since it is not tied to the ArcMap application, application-specific functionality in the MapDocument will not be persisted. Examples of application specific functionality are toolbar settings, UI customizations, VBA projects, and ArcMap graphs. For desktop developers who need to use this functionality, the MxDocument interface located in the ArcMapUI library is a better choice.




I am now thinking that changing the map scale can not be persisted properly using the IMapDocument interface.




No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...