Adv_GISArcObjects - 11 VBA and ArcObjects Programming Week 1 Spring 2008 Advanced GIS.

21
Adv_GIS ArcObjects - 1 1 VBA and ArcObjects Programming Week 1 Spring 2008 Advanced GIS

Transcript of Adv_GISArcObjects - 11 VBA and ArcObjects Programming Week 1 Spring 2008 Advanced GIS.

Page 1: Adv_GISArcObjects - 11 VBA and ArcObjects Programming Week 1 Spring 2008 Advanced GIS.

Adv_GIS ArcObjects - 1 1

VBA and ArcObjects Programming

Week 1

Spring 2008

Advanced GIS

Page 2: Adv_GISArcObjects - 11 VBA and ArcObjects Programming Week 1 Spring 2008 Advanced GIS.

Adv_GIS ArcObjects - 1 2

Why learn programming ?

• Because, there are three types of GIS jobs in the market....

• Logon to monster.com and type “GIS programmer”

• And you will find the pay scale….

Page 3: Adv_GISArcObjects - 11 VBA and ArcObjects Programming Week 1 Spring 2008 Advanced GIS.

Adv_GIS ArcObjects - 1 3

Object-Oriented Programming• Object has property and behaviors (methods). Lots

of objects are buttons, tools, windows, and dialog boxes.

• Object.Method– Table.AddRecord

– Tree.Grow(10) /argument, parameter

– Tree.Create /first create a tree, then

– Tree.GrowBranch /then, grow branch from the tree

– Branch.GrowApple /apple grown from branch.

– Boy.Eat(AnApple)

Page 4: Adv_GISArcObjects - 11 VBA and ArcObjects Programming Week 1 Spring 2008 Advanced GIS.

Adv_GIS ArcObjects - 1 4

Visual Basic for Application (VBA)• A simplified version of Visual Basic, loaded

with ArcGIS, embedded within applications.

• Use functions in ArcGIS

• Code organized in Procedures – a procedure includes instructions. Procedure can be linked to each other, by calling others. Events makes procedure run.

Page 5: Adv_GISArcObjects - 11 VBA and ArcObjects Programming Week 1 Spring 2008 Advanced GIS.

Adv_GIS ArcObjects - 1 5

ArcObjects and ArcGIS

• ArcMap and ArcCatalog are built with ArcObjects– Each GIS part is a programmable object– Users and programmers use same objects

• Data frames, layers, features, tables, symbols, points, lines, polygons, records, fields, colors --------are ArcObjects

Page 6: Adv_GISArcObjects - 11 VBA and ArcObjects Programming Week 1 Spring 2008 Advanced GIS.

Adv_GIS ArcObjects - 1 6

ArcObjects• There are 2,700+ classes and 2,000 interfaces

documented in several object model diagrams.• With such extensive collection of classes, you can

create many customized applications to extend ArcGIS’s functions.

• “Where to begin with?” is the major difficulties faced by GIS programmers.– Problem solving strategy help you glide through the

real-world ArcObjects programming tasks.– Three parts of problem-solving guides

Page 7: Adv_GISArcObjects - 11 VBA and ArcObjects Programming Week 1 Spring 2008 Advanced GIS.

Adv_GIS ArcObjects - 1 7

I: Define the ArcObjects Programming Tasks

• Describe the problem in ArcObjects terms

• Identify subtasks• Decide where to write the code• Search for a related sample of

recommended methodology

• Identify a subtask• Extract keywords• Search for the correct object

model diagrams• Review all related

documentation

II: Locate the correct object model

• Review the structure of the object model diagram

• Trace the flow between classes and assemble code

III: Navigate the object model diagram

Page 8: Adv_GISArcObjects - 11 VBA and ArcObjects Programming Week 1 Spring 2008 Advanced GIS.

Adv_GIS ArcObjects - 1 8

Describe the problem in ArcGIS terms

• For example: Add a dataset called States to ArcMap can be described as:– Access the States feature class from a personal

geodatabase and add it to ArcMap.

• A two-step approach illustrate the procedures.

Page 9: Adv_GISArcObjects - 11 VBA and ArcObjects Programming Week 1 Spring 2008 Advanced GIS.

Adv_GIS ArcObjects - 1 9

Customize Toolbars• Open a ArcMap screen (Start |Programs | ArcGIS |

ArcMap)• You want to start with “A new empty map”.• Add theme from c:\esri\esridata\usa\ (STATES,

Drainage, lakes, rivers and roads)• Once your themes are in the project, practice the

functions of ArcMap (– Zoom In/Out, – Pan, – Select Feature, – Identify, – Measure ? (you may get degrees as distance) change

DataFrame Properties’ setting of Display Unit to “Miles”

Page 10: Adv_GISArcObjects - 11 VBA and ArcObjects Programming Week 1 Spring 2008 Advanced GIS.

Adv_GIS ArcObjects - 1 10

Showing/Hiding toolbars• Double-click any unoccupied area of any

toolbars to display the Customize dialog box (or from Tools|Customize..)

• The presence of a check mark next to the toolbar name indicates it is present.

• In the Toolbars tab of the Customize dialog , click New. In the Tools Name area type in “My personal tools” and Save in “Untitled” (since you haven’t saved your project yet). If you save the personal tool into “Normal.mxt”, then it will become a default tool for everyone who use this machine. We don’t want to do so. Simply save it to “Untitled” to avoid confusion to other users.

• Click OK

Page 11: Adv_GISArcObjects - 11 VBA and ArcObjects Programming Week 1 Spring 2008 Advanced GIS.

Adv_GIS ArcObjects - 1 11

Adding more tools to your personal tool box

• Adding buttons and menus to your personal tool by selecting commands (click Command tab to switch to Command window) dragging to “My personal tools” toolbar. With category of Selection open (Command | Selection), drag commands to “My personal tools” (drag Buffer Selection and Hyperlink to the window, if Category is “Selection”)

• Practice putting different menu and commands on your personal toolbar. You can also remove them by dragging out of the box.

• Click Close to close the customize window.• Save file in your own folder (if you don’t have one, create

one in G drive)

Page 12: Adv_GISArcObjects - 11 VBA and ArcObjects Programming Week 1 Spring 2008 Advanced GIS.

Adv_GIS ArcObjects - 1 12

Writing Macros in VBA• You can use VBA integrated

development environment (IDE) to create macros to help you automate tasks you perform repeatedly

• With the VB Editor you can edit macros, copy macros from one module to next, rename modules that store macros.

• Click the Tool menu, point to Macros, then click Macro. In the Macro dialog, type MyZoomIn as name and click “Create” (this will take you to the VB screen and you are ready to create a customized tool)

Page 13: Adv_GISArcObjects - 11 VBA and ArcObjects Programming Week 1 Spring 2008 Advanced GIS.

Adv_GIS ArcObjects - 1 13

Sub MyZoomIn()'' macro; MyRoomIn'Dim pDoc As IMxDocumentDim pEnv As IEnvelopeSet pDoc = ThisDocumentSet pEnv = pDoc.ActiveView.ExtentpEnv.Expand 0.5, 0.5, TruepDoc.ActiveView.Extent = pEnvpDoc.ActiveView.RefreshEnd Sub

• Envelopes are the rectangular window that contain a specific element.  All Geometry objects have an envelope defined by the XMin, XMax, YMin, and YMax of the object.  Envelopes can also serve as the viewing area for a particular view screen or data frame.

Code window

ArcMap Doc

ThisPredefined variable-is the Idocument interface to the MxDocument object

ActiveView property provides an IActiveView interface that links the document data to the current screen display of that data

Page 14: Adv_GISArcObjects - 11 VBA and ArcObjects Programming Week 1 Spring 2008 Advanced GIS.

Adv_GIS ArcObjects - 1 14

Run Macro• Go to File | Close Return to

ArcMap• In ArcMap, go to Tools

|Macro and select Module1.MyZoomIn macro and click Run (make sure your macro settings is in Normal) (Alt + F8)

• The display zoomed in 50% smaller.

Page 15: Adv_GISArcObjects - 11 VBA and ArcObjects Programming Week 1 Spring 2008 Advanced GIS.

Adv_GIS ArcObjects - 1 15

Add Macro to a toolbar• Go to Tools | Customize.• In the Toolbar tab, make sure “My personal

Tool” is still there • Click Command tab and select Macros

category. Select your macro (MyRoomIn) and drag to ”My Personal Tool” bar

• A default icon appears. To change image, right-click on this icon and a context menu shows, go to Change Button Image and select one from the panel (smiling face,ok?)

• Close the Customize dialog box• Click the smiling face to run the macro Right-click on smiling

face, select View Source to modify your code to 0.75 from 0.5 for zoomin ratio

Page 16: Adv_GISArcObjects - 11 VBA and ArcObjects Programming Week 1 Spring 2008 Advanced GIS.

Adv_GIS ArcObjects - 1 16

Exercise - 1• Create a new Macro named: MyRoomOut in

Module1• Hint: copy code from codewindow (from the

beginning of the Sub to the End Sub, and paste below the existing code. Rename the copied Sub to “MyZoomOut” and change line:– pEnv.Expand 0.5, 0.5, True to– pEnv.Expand 2.0,2.0,True

• Add this macro to My Personal Tool and run it

Page 17: Adv_GISArcObjects - 11 VBA and ArcObjects Programming Week 1 Spring 2008 Advanced GIS.

Adv_GIS ArcObjects - 1 17

UIControls – User Interface Controls

• UIButton - Save

• UITool - Pan

• UIComboBox

• UIEditBox

• Code is required for UI to work.

Page 18: Adv_GISArcObjects - 11 VBA and ArcObjects Programming Week 1 Spring 2008 Advanced GIS.

Adv_GIS ArcObjects - 1 18

Project, Code module and Procedure

Page 19: Adv_GISArcObjects - 11 VBA and ArcObjects Programming Week 1 Spring 2008 Advanced GIS.

Adv_GIS ArcObjects - 1 19

Project/Code Module/Procedure3 types of projects:

– Map documents (.mxd)

– Base templates (.mxt)• Normal template (Normal.mxt)

• Code modules – – ThisDocument, you may write code for any UIControls

that you make in a particular project

• Procedure –– Code is organized into procedures

Page 20: Adv_GISArcObjects - 11 VBA and ArcObjects Programming Week 1 Spring 2008 Advanced GIS.

Adv_GIS ArcObjects - 1 20

Procedures• 4 types of procedures:

– Event, subroutine, function and property

– Event : correspond to user actions, such as Click, which will execute the codes once it is clicked and three other types of codes will be called to work

• Public/Private: “Private” procedure can only be called within same code module where “Public” procedures can be called by other code modules.

Page 21: Adv_GISArcObjects - 11 VBA and ArcObjects Programming Week 1 Spring 2008 Advanced GIS.

Adv_GIS ArcObjects - 1 21

InputBox/MsgBox

• InputBox and MsgBox are two fundamental message receiving and conveying methods used in the beginning of this class.

• Inputbox (“Message here”, “Title Here”.)

• MsgBox (“Message Here”, “Title here”)

• You may not use () for these boxes