Sie sind auf Seite 1von 19

Introduction to ArcObjects

Introduction to ArcObjects

http://www.gis.ethz.ch - Modified: 28.6.2006 1


Introduction to ArcObjects

Content
1. Introduction to ArcObjects ................................................................................................... 3
1.1. Using VBA objects ........................................................................................................ 4
1.1.1. Designing a form .................................................................................................... 5
1.1.2. Setting form and control properties ........................................................................ 8
1.1.3. Writing code ........................................................................................................... 9
1.1.4. Run the form ........................................................................................................... 11
1.1.5. Further development .............................................................................................. 13
1.2. Use of ArcObjects ......................................................................................................... 14
1.2.1. Import a shapefile ................................................................................................... 15
1.2.2. Buffer and Data Export ........................................................................................... 18
1.3. Glossary ........................................................................................................................ 19

http://www.gis.ethz.ch - Modified: 28.6.2006 2


Introduction to ArcObjects

1. Introduction to ArcObjects
This tutotial gives a general outlook on the development environment created by ESRI in ArcGIS
using Visual Basic for Application.
The following text is based on the ESRI-Course "ArcObjects for beginners".

Learning Objectives
• Visual Basic for Applications
• Using ArcGIS objects

http://www.gis.ethz.ch - Modified: 28.6.2006 3


Introduction to ArcObjects

1.1. Using VBA objects

1.1. Overview
Visual Basic is a high level programming language evolved from the earlier DOS version called
BASIC 1. It is a fairly easy programming language to learn. Visual Basic is a visual and events
driven Programming Language.
The VBA development environment consists of two primary tools:
• The Customize dialog box for interactively modifying the user interface.
The Customize dialog box is a powerful tool tha allows you to customize ArcMap and
ArcCatalog without writing a single line of code. You can create, add, move, and remove
toolbars and commands, and even add tools others have created.
• The Visual Basic Editor for creating user forms and for writing and debugging the code.

1
Beginners' All-purpose Symbolic Instruction Code

http://www.gis.ethz.ch - Modified: 28.6.2006 4


Introduction to ArcObjects

1.1.1. Designing a form


Objective
In this exercise, you will create a simple form that converts geographic coordinates WGS84
(degrees, minutes, and seconds) into CH1903 coordinates (meters).

After creating the form, you will test your application in an ArcMap document.
Settings
In the ArcMap and ArcCatalog Customize dialog box, you will find utilities for adding new toobars,
menus, and commands to the user interface. The Customize dialog box contains all ArcMap and
ArcCatalg commands. These commands are available on the Commands tab and are organized
into categories.

• Start ArcMap and create a new map (e.g. AO.mxd)


• From the menu item Tools choose Costumize...
• Click the Commands tab
• Select the Tools category
• Under Save in choose your new map
• Drag the Visual Basic Editor icon in the toolbar

Only pictures can be viewed in the PDF version! For flashes etc. see the online
version. Only screenshots of animations are displayed.

Costumizations can be stored in the current map document (*.mxd). Costumizations stored in the
map are available only in that particular document. If you want to load your costumizations every
time you use ArcMap, store the costumizations in the normal template (normal.mxt).

• Open the Visual Basic Editor by clicking on the correspondig icon

Create your form

http://www.gis.ethz.ch - Modified: 28.6.2006 5


Introduction to ArcObjects

The Visual Basic Project Editor window organizes all code written for a particular ArcMap
document or for the ArcCatalog application.
Use the Project Explorer to access all available Visual Basic programs.
Projects are like root directories that organize several subfolders of code documents within the
Project Explorer. In ArcMap, the projects define the level of customization for code you write
(Normal -vs- current document).
A Module is simply a document that contains code.

• Create a new UserForm Module

UserForm modules are code modules associated with a form (dialog). In the Visual Basic Editor,
you can design a form by adding and rearranging controls (CommandButtons, TextBoxes,
OptionButtons, etc.) and setting their properties. After the form is designed, you can write code in
the associated form code module to make the controls work.

• Arrange the following controls in the form:


8 TextBoxes
10 Labels
2 CommandButtons

http://www.gis.ethz.ch - Modified: 28.6.2006 6


Introduction to ArcObjects

After the desired controls have been added to the form, position them so they are intuitive for the
user to work with.

http://www.gis.ethz.ch - Modified: 28.6.2006 7


Introduction to ArcObjects

1.1.2. Setting form and control properties


Overview
The properties window displays design time properties for the selected object in the form designer.
Upon selecting an object, you can set any of its design time properties by clicking in the
Properties Window
One property that is common to all controls is the Name property. While the user will never care
how your controls are named, this property is very important for the programmer. In your code, you
will always refer to controls byn their name.
Properties of your form
In this step, you will set the initial properties for the form and its controls.
Control Properties
UserForm1 Name: frmWGS2CH
Captation: WGS84 - CH1903 CONVERTER
Further costumizations (e.g. font, color,
showmodal etc.)
TextBoxes Name: TXT1, TXT3, ..., TXT8
Dimensions (TXT1 to TXT6): width = 30; heigth
= 20
Dimensions (TXT7 and TXT8): width = 80;
heigth = 20
Labels Name: LBL1, LBL2, ..., LBL10
Captation: °, ', "", ... (depending on their
position)
CommandButtons Name: CMD1, CMD2
Captation and Dimension: as you want
Note: A list of suggested prefixes for controls is available.

http://www.gis.ethz.ch - Modified: 28.6.2006 8


Introduction to ArcObjects

1.1.3. Writing code


Overview
The next step is to write code to power the controls. Every control an a form will have a set of
events for which you may provide code, as the form itself.
A form can be visualised in two ways:
1. Code View: where you will arrange form controls
2. Object View: where you will write the form's code for the controls.
There are several ways to access a form's code module (code view). The easiest way is to simply
double-click a control in the form designer (object view), which opens the form's code module and
places your cursor in the default event control.
Write the code for the CommandButtons
You will add the code to the "Click" events of the two CommandButtons.
It is good practice to add a comment to every piece of code you write. Comment a line, just using
the symbol '.

The 'Convert' Button


This buttons will convert geographic WGS84 coordinates into CH1903 coordinates (formula).
Double click on the command and start to write your code as descibed below.

• Dim ension a variable dblBBE as double and assign it the value 169028.66 (Lat. Bern)
• Dim ension a variable dblLBE as double and assign it the value 26782.5 (Long. Bern)

To create a variable, you can declare it. Declaring a variable accomplishes two things:
1. It specifies the name for your variable
2. It defines the type of data to be stored in the variable
Although it is not technically required, it is good programming technique to declare all variables
before you use them.

• Dim ension a variable dblBreite as double (local Lat.)


• Calculate its value using the following code:
dblBreite = (((TXT1.Text * 60) + TXT2.Text) * 60 + TXT3.Text)

Remember that controls are Visual Basic objects, and the syntax for working with object properties
is Objects.Property
In the example above, the value entered by the users is referenced using the TextBox control's
Text property (e.g. TXT1.Value)

• Dim ension a variable dblLaenge as double (local Long.)


• Calculate its value using the following code:
dblLaenge = (((TXT4.Text * 60) + TXT5.Text) * 60 + TXT6.Text)

http://www.gis.ethz.ch - Modified: 28.6.2006 9


Introduction to ArcObjects

• Dim ension two variables dblB (coeff. B) and dblL (coeff. L)


• Calculate its value using the following code:
dblB = (dblBreite - dblBBE) / 10000
dblL = (dblLaenge - dblLBE) / 10000

• Calculate the coordinates transformation (WGS -> CH) with the following formula:
TXT7.Text = 600072.37 + 211455.93 * dblL - 10938.51 * dblL *
dblB - 0.36 * dblL * dblB * dblB - 44.54 * dblL * dblL * dblL
TXT8.Text= 200147.07 + 308807.95 * dblB + 3745.25 * dblL * dblL
+ 76.63 * dblB * dblB - 194.56 * dblL * dblL * dblB + 119.79 *
dblB * dblB * dblB

The 'Exit' Button


Double click on the command and start to write your code as descibed below.

• To exit from the application write


frmWGStoCH.hide

http://www.gis.ethz.ch - Modified: 28.6.2006 10


Introduction to ArcObjects

1.1.4. Run the form


Test your form
Test your form by running the SubRoutine you have just written pressing the "PLAY" button on the
standard toolbar.

Once the your code is debbuged and runs properly, you can test the regularity of the conversion
using following data.
WGS coordinates CH03 coordinates
Lat: 46° 2' 38.87" Easting: 699 999.76 m
Long: 8° 43' 49.79" Nothing: 99 999.97 m

Customize the ArcMap toolbar to launch your form


Save your changes and switch to the ArcMap interface.
Add a new Button to one of your standard toolbar as described below.

• From the menu item Tools choose Costumize...


• Click the Commands tab
• Select the UIControls category
• Under Save in choose your new map
• Click New UIControl...
• Choose UIButtonControl
• Click Create
• Drag the Project.UIButtonControl1 in the toolbar

http://www.gis.ethz.ch - Modified: 28.6.2006 11


Introduction to ArcObjects

While the Customize dialog box is open, the user interface is in design mode, which allows you to
change the properties of your new Button

• Right-click on your Button to change the button image


• Close the Customize dialog box

In a similar way as you did to add code to your form, you can edit the code for your button, which
will open your form.

• Right-click on the new button icon


• From the menu choose View Source
• The Visual Basic Editor focus on the right place where you can enter the following
code:
frmWGS2CH.Show
• Save and switch to ArcMap
• Test your program

http://www.gis.ethz.ch - Modified: 28.6.2006 12


Introduction to ArcObjects

1.1.5. Further development


Based on the instruction provided so far create a new form, which calculates the distance between
two points given their (cartesian) coordinates.
If you need support go through following steps.

• Add following controls to the form:


5 TextBoxes (4 for the two pairs of coord. and 1 for the result)
1 CommandButton (to start the computation)
• Write the code to compute the distance (Pythagoras)
• Isolate the formula to compute the distance in a Visual Basic fuction (option)

http://www.gis.ethz.ch - Modified: 28.6.2006 13


Introduction to ArcObjects

1.2. Use of ArcObjects

1.2. Overview
ArcGIS objects are saved in libraries and usable through ArcObjects, which is the development
platform for the ArcGIS family for applications such as ArcMap, ArcCatalog etc. The ArcObjects
software components expose the full range of functionality available in ArcInfo and ArcView to
software developers. The ArcObjects components collaborate to serve every data management
and map presentation function common to most GIS applications.
Writing application programs for ArcGIS requires knowledge of both:
• VBA which provides the programming language
• ArcObjects which provide objects and their build-in properties and methodes.
In this Chapter you will learn how to work with VBA and ArcObjects. You will learn the basics of
working with maps and layers in ArcMap using ArcObjects. You will create 3 BUTTONS and code
different procedures associated to them.

Learning Objectives
• Program a tool used to import a shapefile as a new layer in an ArcMap document.
• Develope a tool used to create graphic buffers around polygons.
• Implement a tool to export the active view as an image.

http://www.gis.ethz.ch - Modified: 28.6.2006 14


Introduction to ArcObjects

1.2.1. Import a shapefile


Step 1: Create the button
A new button must be added to the toolbar and afterward associated with the corresponding code
so that when the button is clicked, the code can be executed to accomplish a specific task.

• Start ArcMap and create a new Map (e.g. AO2.mxd)


• Personalise the ArcMap Toolbar creating a new button
• Rename the button as ImportShapeFile (Project.ImportShapeFile )
• Right click on the button to edit the source code

Only pictures can be viewed in the PDF version! For flashes etc. see the online
version. Only screenshots of animations are displayed.

Step 2: Add the source code

Classes and Objects


ArcObjects consist of objects and classes. A class can be described as a template that defines
how objects created from the class will look and behave. In this respect, a class is abstract, while
an object is a concrete thing that has been created from the class. The characteristics and
behaviors of an object can be manipulated in your code. To manipulate an object this can be
accessed through an interface which allows the programmer to communicate with the object.
An example
There is an ArcObjects class called Map. The Map class defines the properties and behaviors that
all maps have. Obviously, this does not mean that all maps you work with in ArcMap will be
identical - simply that they will all have the same familiar properties such as Scale, Spatial
Reference, and Layers.

• All code powering your UIControls will be written with the Visual Basic Editor in the
ThisDocument code module
• Using the given printed document, write your code in the "CLICK EVENT" of your new
button

Object model diagrams


To describe how the ArcObjects library (structured collection of all classes for ArcGIS) several
object model diagrams are provided. As a programmer, these diagrams are invaluable tools that
indicate you how to work with specific classes and how each class is related to others.

http://www.gis.ethz.ch - Modified: 28.6.2006 15


Introduction to ArcObjects

Example of hierarchical structure

The objects discussed in this exercise can be found on two separate Object Model Diagrams:
• Geodatabase and DataSourcesFile which provide the programming API for the geodatabase
and for file-based data sources
• Carto which supports the creation and display of maps
Always use the Object Browser (F2) to check the unknown commands and/or viste the ESRI
Developer Network (EDN).
Step 3: Test the button

• Go back in ArcMap (ALT + F11) and CLICK your button

As you can notice the program adds a shapefile to the Data Frame "Layers" but without assigning

http://www.gis.ethz.ch - Modified: 28.6.2006 16


Introduction to ArcObjects

it a name. In Step 4 you will find out what is still missing in your code.
Step 4: Explore object models
You will now explore through EDN the two Object Model Diagrams used for your button coding.

• Navigate to Library Reference link


• Look for the Carto library
• Click on the Carto Library Object Model Diagram

The Carto library contains the objects for displaying data. The PageLayout and Map objects are in
this library along map layers, renders for all the supported data types.

• Using the search tool look for the IMap and the IFeatureLayer
• Give a name to the Data Frame (pMap) of your map
• Give a name to the shapefile (pFeatureLayer) that you added to the project

• At the end of your code write pMxDocument.UpdateContents


This comand will refresh your project

http://www.gis.ethz.ch - Modified: 28.6.2006 17


Introduction to ArcObjects

1.2.2. Buffer and Data Export


EDN Code Exchange Service
EDN offers you also a platform where to find and share samples and scripts written by ESRI staff
or contributed by users for ArcGIS products.

Don't reinvent the wheel!

The next paragraphs show the implementation of two typical GIS-fuctions getting support from the
script collections available on the EDN site.
Buffer
Now you will search for a sample that creates buffers around polygons.

• Always on EDN, search in the category Code Exchange for Buffer Features and
Create Graphic Elements
• Add a new button to the ArcMap toolbar named BufferPoly
• Copy the code from the EDN site in the "CLICK EVENT" of the button
• Test the button

Data export
Now you will search for a sample that exports the active view to an image.

• On EDN, search for a script to export active view to jpeg


• Create the corresponding button in ArcMap
• Copy the code in the "CLICK EVENT" of the button
• Test the button

http://www.gis.ethz.ch - Modified: 28.6.2006 18


Introduction to ArcObjects

1.3. Glossary
BASIC:
B eginners' A ll-purpose S ymbolic I nstruction C ode

http://www.gis.ethz.ch - Modified: 28.6.2006 19

Das könnte Ihnen auch gefallen