160 likes | 191 Views
Learn to work with ArcObjects framework in ArcGIS Add-Ins, accessing Arc version through gatekeeper objects & requesting objects. Utilize Java annotations, interfaces, and COM in coding. Discover document handling, API usage, and practical tips.
E N D
ProgrammingArcGIS AddIn Architecture ArcObjects Framework
Code The code that goes in the addIn is code to work with the ArcObjects framework. 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. Ask App for Document Ask Document for Maps Ask Maps for a Map Ask Map for Layers Ask Layers for Layer etc.
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://desktop.arcgis.com/en/arcobjects/latest/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 Four default annotations within java.lang (see docs).
@override Java ‘annotation’. Default annotation denoted an overridden method (i.e. one also in a superclass or interface). Compiler will check (e.g. incase you’ve misspelt it or not used the same parameter types). Also flagged in documentation. You can write your own annotations.
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.
Interfaces For 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.
Next Lecture Getting, sorting, and searching data. Practical Hello World! Utilising tools in Arc Addins.