Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.

share|improve this question
add comment

migrated from stackoverflow.com May 19 '11 at 10:09

This question came from our site for professional and enthusiast programmers.

2 Answers

Solved - and it took Redlands to come back with a slightly unusual fix, it utilises the InteropServices library - hopefully it will be fixed properly in a future release or patch by ESRI.

[DllImport("User32.dll")]
public static extern int GetDesktopWindow(); 

private IMapDocument mapDocument = null;

private void ChangeMapScale()
{
    mapDocument.Open(@"C:\Temp\foo.mxd", null);

    IPageLayout pageLayout = mapDocument.PageLayout;
    IActiveView activeView = (IActiveView)pageLayout;
    IMap map = activeView.FocusMap;

    activeView = (IActiveView)mapDocument.PageLayout;
    activeView.Activate(GetDesktopWindow());

    map.MapScale = value;
    activeView.Refresh();

    mapDocument.Save(true, true);
}
share|improve this answer
    
Upvoting because it seems to have solved a problem I was having with the map scale as well. example here: forums.esri.com/Thread.asp?c=93&f=993&t=258229 –  Eddie Jesinsky Apr 23 '12 at 16:49
add comment

If you manually set the mapframe to fixed-scale and run your code that exports to pdf, do you get the correct results?

If not, perhaps the problem is with the code that does the export. Can you post that code too?

share|improve this answer
    
Check out my update - do you think this is a right assumption? I doubt there is a difinitive list of the items you can't persist via the IMapDocument interface. –  Vidar Jun 2 '10 at 15:06
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.