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

13
Arc: Accessing the Framework Dr Andy Evans

Upload: lora-kennedy

Post on 02-Jan-2016

219 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: 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 Document

Arc: Accessing the Framework

Dr Andy Evans

Page 2: 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 Document

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.

Page 3: 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 Document

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;

}

Page 4: 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 Document

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();

Page 5: 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 Document

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

Page 6: 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 Document

N.B.

The code utilises Java Annotations.@name

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

@Override

Page 7: 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 Document

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.

Page 8: 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 Document

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.

Page 9: 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 Document

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.

Page 10: 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 Document

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.

Page 11: 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 Document

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.

Page 12: 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 Document

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.

Page 13: 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 Document

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.