Select to view content in your preferred language

programmatically adding a dataframe in arcmap 10 using java add-ins

3030
2
11-04-2010 02:22 PM
udaykirankatikaneni
Emerging Contributor
Hi,
I have an mxd on disk with one map(dataframe). I have a different mxd open in arcmap and I wrote a simple add-in button to read the dataframe in the mxd on disk and add to the currently open document.

I was able to do this to some extent. The new map got displayed in the data view but the entry in content view(TOC) never got updated(nothing shows up) no matter I do. I am posting the code below. Any help is appreciated.


public class SampleButton extends Button {

IApplication app;
MxDocument mxDoc;
IMapDocument mapDocument;
String documentPath = "c:\\temp1\\tempDocument.mxd";

// Returns whether this button is checked
public boolean isChecked() {
return false;
}

// Returns whether this button is enabled
public boolean isEnabled() {
return true;
}

// This is called when the button is clicked
public void onClick() {
try {
addDataFrame();
}
catch (Exception e) {
e.printStackTrace();
}
}

// This initializes the button and gets a reference to the hosting ArcGIS application
public void init(IApplication app) {
this.app = app;
}

public void addDataFrame() throws Exception{

//This code saves the dataframe in the current application document to a new mxd.

mxDoc = (MxDocument)app.getDocument();

//open the mxd file on disk and get the map dataframe from it.
mapDocument = new MapDocument();
mapDocument.open(documentPath, null);
Map map = (Map)mapDocument.getMap(0);

//add the map dataframe on disk to the current open mxd
mxDoc.setActiveViewByRef(map);
mxDoc.updateContents();
mxDoc.getActiveView().refresh();

//refresh all the content views.
for (int i = 0; i < mxDoc.getContentsViewCount(); i++){
System.out.println(mxDoc.getContentsView(i).getNam e());
mxDoc.getContentsView(i).refresh(null);
}

//close the map document on disk.
mapDocument.close();
}
}
0 Kudos
2 Replies
StevenRozic
Emerging Contributor
Hello,

I have had a chance to look at your code and implement on my development machine. It appears that you have discovered a bug. When I created essentially the same add-in on my machine it only updated the source and selection views in the table of contents, but fails to update the default view.

You can contact support: http://support.esri.com to submit the bug.

Thanks for finding and reporting this issue. I'm currently attempting to research a work around and will post again if I discover anything.

Thanks,
-=Steve
0 Kudos
StevenRozic
Emerging Contributor
Hello again:

I have actually found a solution to your code issue in this thread.

Now, if you are trying to add the DataFrame from the map that resides on the local hard drive and add it to the currently running session of ArcMap, then the following code will work for you:

//Instance variables of the add-in button's class definition.
IApplication application;
IMxDocument mxDocument;
IMapDocument mapDocument;
String documentPath = "C:\\temp\\StatesMap.mxd";

//Save the application variable.
@Override
public void init(IApplication app) throws IOException, AutomationException {
application = app;
}

//This code is in a private method inside my Add-In button that is called from the button's onClick()
//method
private void addDataFrame() throws Exception{
   mxDocument = (MxDocument) application.getDocument();
  
   mapDocument = new MapDocument();
   mapDocument.open(documentPath, null);
   Map map = (Map) mapDocument.getMap(0);

   mxDocument.getMaps().add(map);
   mxDocument.updateContents();
}


When I deployed the add-in button that had this source code and tested in ArcMap, it added the DataFrame to the existing map document and the table of contents was updated accordingly. I simply had to activate the new DataFrame in ArcMap in order to see the layer that was added to the .mxd on disk.

Now, if you wanted to add the layer to the active DataFrame in ArcMap instead of adding a brand new DataFrame, it's only a couple additional lines of code (see next snippet):

//This code is in a private method inside my Add-In button that is called from the button's onClick()
//method
private void addDataFrame() throws Exception{
   mxDocument = (MxDocument) application.getDocument();
  
   mapDocument = new MapDocument();
   mapDocument.open(documentPath, null);
   Map map = (Map) mapDocument.getMap(0);

   ILayer layer = map.get_layer(0);
   mxDocument.getActiveView().getFocusMap().addLayer(layer);

   mxDocument.updateContents();
}


Please try the above modifications to your source code and retest. Let me know if you run into any additional issues.

Thanks and I hope this updated source code works for you.

-=Steve
0 Kudos