arc: accessing the framework dr andy evans. code the code that goes in the addin is then code to...

Post on 02-Jan-2016

223 Views

Category:

Documents

5 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Arc: Accessing the Framework

Dr Andy Evans

Code

The code that goes in the addIn is then code to work with the ArcObjects framework.

Ask App for DocumentAsk Document for MapsAsk Maps for a MapAsk Map for LayersAsk Layers for Layer etc.

The running version of Arc is accessed via gatekeeper objects.

You then request objects from these, and subsequent objects from the objects you get back.

init(IApplication app)

Most addIns come with a init method that can be overridden. This is sent the application as an entry point for the running framework.

import com.esri.arcgis.framework.*;

private IApplication app;

@Override

public void init(IApplication app){

this.app = app;

}

Document

You can use this to get hold of the current document. This is the current data and GUI.

import com.esri.arcgis.arcmapui.*;

IMxDocument mxDoc = (IMxDocument)app.getDocument();

API

It is then just a matter of finding how to do stuff in the API docs:http://resources.arcgis.com/en/help/arcobjects-java/api/arcobjects/index.html

N.B.

The code utilises Java Annotations.@name

Used as a marker for software intending to process the code.

@Override

N.B.II

Arc returns objects that implement multiple interfaces. You generally cast the object to a specific interface when using it.

IMxDocument mxDoc = (IMxDocument)app.getDocument();

There is nothing to stop you recasting it to another interface to use different methods in it:IDocument iDoc = (IDocument) mxDoc;

Infact, it is very common.

The COM

In Microsoft languages, this reassignment of interface labels is key.

It allows chunks of code to use other chunks of code without knowing anything other than the interface names.

It is used to hold together a great deal of the Component Object Model (COM) at the heart of Microsoft software.

ArcGIS is built around the COM, so this is a common thing to see.

Interfaces

This requires some care.You might think that redefining everything as the right object would be sensible:public void init(IApplication app){

Application a = (Application)app;

You could then use all the methods.

However, while objects Arc gives you will match the interfaces, they may not do anything sensible.

InterfacesFor example, this works in ArcMap:

public void init(IApplication app){

Application a = (Application)app;

IGxSelection selec = a.getSelection();

JOptionPane.showMessageDialog

(null, selec.toString());

}

But doesn’t give you anything sensible, as IGxSelection is an ArcCatalog class, and here the Application object is the ArcMap Application object, not the ArcCatalog one.

Interfaces

In general, better to get the interface right and keep the object defined by its interfaces.As well as explicitly casting, just attaching the right label works if you are going to a less specific class:IMxDocument mxDoc = app.getDocument();

But generally in the examples you’ll see an explicit cast because it saves looking up the relationship.In the API Docs, in general go for the interface list at the top of the class.

Interfaces generally start “I”Then “Mx” for ArcMap, “Gx” for ArcCatalog.

ArcMap IApplication methods

newDocument(boolean userPickTemplate?, String path)

openDocument(String optionalPath)

refreshWindow()

saveDocument(String optionalPath)

If the paths are null, the usual “new”, “open”, and “save” processes occur.

ArcMap IMXDocument methods

Used for getting data:getActiveView() i.e. layout view or data view data.getFocusMap() i.e. currently selected/shown map.getMaps() i.e. all maps.getSelectedItem() i.e. that the user has picked.getSelectedLayer() i.e. that the user has picked.

Documents also implement IDocument, the main use of which is programmatically controlling toolbars.

top related