Title: VBA and ArcObjects Programming
1VBA and ArcObjects Programming
- Week 1
- Spring 2008
- Advanced GIS
2Why 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.
3Object-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)
4Visual 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.
5ArcObjects 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
6ArcObjects
- 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
ArcGISs 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
7I Define the ArcObjects Programming Tasks
II Locate the correct object model
- Identify a subtask
- Extract keywords
- Search for the correct object model diagrams
- Review all related documentation
- Describe the problem in ArcObjects terms
- Identify subtasks
- Decide where to write the code
- Search for a related sample of recommended
methodology
III Navigate the object model diagram
- Review the structure of the object model diagram
- Trace the flow between classes and assemble code
8Describe 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.
9Customize 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
10Showing/Hiding toolbars
- Double-click any unoccupied area of any toolbars
to display the Customize dialog box (or from
ToolsCustomize..) - 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
havent 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 dont want to do so. Simply save it
to Untitled to avoid confusion to other users. - Click OK
11Adding 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 dont have
one, create one in G drive)
12Writing 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)
13Code window
- Sub MyZoomIn()
- '
- ' macro MyRoomIn
- '
- Dim pDoc As IMxDocument
- Dim pEnv As IEnvelope
- Set pDoc ThisDocument
- Set pEnv pDoc.ActiveView.Extent
- pEnv.Expand 0.5, 0.5, True
- pDoc.ActiveView.Extent pEnv
- pDoc.ActiveView.Refresh
- End 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.
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
14Run Macro
- Go to File Close Return to ArcMap
- In ArcMap, go to ToolsMacro and select
Module1.MyZoomIn macro and click Run (make sure
your macro settings is in Normal) (Alt F8) - The display zoomed in 50 smaller.
15Add 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
16Exercise - 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
17UIControls User Interface Controls
- UIButton - Save
- UITool - Pan
- UIComboBox
- UIEditBox
- Code is required for UI to work.
18Project, Code module and Procedure
19Project/Code Module/Procedure
- 3 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
20Procedures
- 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.
21InputBox/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