0

I'm working with C#, Visual Studio 2010 and ArcMap. I have a fairly simple question that I am not sure of the answer because of my Noob status with ArcObjects and ArcMap.

I am familiar with many of the ArcObjects Interfaces such as IMxDocument, IMap, IActiveView and IPageLayout.

I was wondering how to check if an actual map document is loaded into ArcMap or opened up in ArcMap. I am working on a boolean function on a button click. I am just not sure what interfaces or ArcObjects (if any) would be needed to return True if there is a map document loaded or not.

balexandre
  • 73,608
  • 45
  • 233
  • 342
user1898629
  • 329
  • 1
  • 4
  • 22

3 Answers3

1

In C# just look at:

(IDocumentInfo2)ArcMap.Application.Document).Path

If the is no map loaded it will be an empty string, if a map is loaded it will be the path to the MXD.

Allan
  • 11
  • 1
0

You can check against the currently loaded document with arcpy:

mxd = arcpy.mapping.MapDocument("CURRENT")

access the filepath like this:

filename = mxd.filePath

and wrap that in an if clause to compare it with your documents name to get a boolean result. Hope that helps.

Danfro
  • 196
  • 4
  • Thanks for your response. With using Arcpy, is there a specific library or reference I'd need to include in my C# program? – user1898629 Oct 01 '14 at 14:41
  • Sorry, comming from Python I assumed you could somehow just load the Arcpy library into C# and use that. Thinking about it arcPY is a Python library, not C#. I will post another answer that might still help you. Because I do not have C# running I can not try it. – Danfro Oct 02 '14 at 18:54
0

I came across those two links:

http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#/How_to_run_a_geoprocessing_tool/0001000003rr000000/

http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#/Walkthrough_Consuming_a_geoprocessing_model_tool_in_NET/0001000001sw000000/

So you may be able to use this information to run a custum geoprocessing tool. That tool can execute the arcpy code give back the currently loaded document.

  1. Using an texteditor create the following python script, say "getfile.py":

    #importing the arcpy module into python
    import arcpy
    #setting the current document
    mxd = arcpy.mapping.MapDocument("CURRENT")
    #retrieving the filename and adding this to an message
    arcpy.AddMessage(mxd.filePath)
    
  2. Create a custum toolbox, say "MyTools".

  3. Import that script into your MyTools toolbox.
  4. Try to run that geoprocessing tool and catch the message to get the filename. Based upon the samples that code may look a bit like that, but I can not try it myself. You will have to play around with that by yourself.

    using System;
    using ESRI.ArcGIS.EsriSystem;
    using ESRI.ArcGIS.Geoprocessing;
    
    public void getfilenameTool()
    {
    object sev = null;
    // Initialize the geoprocessor.
    IGeoProcessor2 gp = new GeoProcessorClass();
    
    // Add your toolbox, you will have to alter the path.
    gp.AddToolbox(@"C:\temp\MyTools.tbx");
    
    // Execute the tool by name.
    gp.Execute("getfile", null, null);
    string messages = gp.GetMessages(ref sev);
    }
    

If you can not get the workaround with the geoprocessing tool to work you have two other workarounds.

A) Because you can run that python script as a stand alone process from the command prompt you could call that script from C# and redirect the comand prompt output to your C# code.

In the python script replace the line

   arcpy.AddMessage(mxd.filePath)

with

   print mxd.filePath

B) Print the output of that python script into a file and read your filename from there.

In the python script replace the line

    arcpy.AddMessage(mxd.filePath)

with

    tempfile = open(r"C:\temp\tempfile.txt", "w")
    tempfile.write(mxd.filePath)
    tempfile.close()

You may then read that file with C# and get the filename of your ArcMap file from there.

Danfro
  • 196
  • 4