Title: VBA and ArcObjects
1VBA and ArcObjects
2VBA
- Available in ArcMap, ArcCatalog, and ArcScene
applications - Many other programming languages can work with
ArcObjects, such as VB,C - VBA is the easiest one for interacting with
ArcMap.
3ArcObjects
- There are 1,000 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
4I 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
5II Locate the correct object model
- Identify a subtask
- Extract keywords
- Search for the correct object model diagrams
- Review all related documentation
6III Navigate the object model diagram
- Review the structure of the object model diagram
- Trace the flow between classes and assemble code
7Describe 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.
8Customize Toolbars
- Open a ArcMap screen (StartgtProgramsgtArcGISgtArcMap
) - Bring in your county themes by click on Add
layers from toolbar and navigate to your folder
and then add them to current project. - You may also convert ArcView project to ArcMap
(File gt Import from ArcView project..) - Once your themes are in the project, practice the
functions of ArcMap (such as right-click on layer
to open command panel.)
9Showing/Hiding toolbars
- Double-click any unoccupied area of any toolbars
to display the Customize dialog box (or from
ToolsgtCustomize..) - 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 Toola Name area type in My
personal tools and Save in Normal.mxt - Click OK
10Adding more tools to your personal tool box
- Adding buttons and menus to your personal tool by
selecting commands dragging to My personal
tools toolbar. With category of Menu open
(CommandgtMenu), drag commands on right window to
My personal tools - 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.
11Saving change to a template
- Go to File gt Save As and navigate to the
installed folder (most probably e\arcgis81
darcgis81 or c\arcgis81, or \arcexec80\) - Move in template folder and create a new folder
called newtemplate and save your file as test.mxt
(ArcMap Template, .mxt)
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 Creat (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
14ThisDocument - predefined variable, is the
Idocument interface to the MxDocument object
- 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
ActiveView property provides an IActiveView
interface that links the document data to the
current screen display of that data
15Run Macro
- Go to FilegtClose Return to ArcMap
- In ArcMap, go to ToolsgtMacrogt and select
Module1.MyZoomIn macro and click Run (make sure
your macro settings is in Normal) - The display zoomed in 50 smaller.
16Add Macro to a toolbar
- Go to Tools gt Customize.
- In the Toolbar tab, make sure My personal Tool
is still there (created previously on slide 5) - 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
17Exercise
- 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
18Calling built-in commands
- Calling existing commands is working with the
ArcID module. Using Find methods, the code
locates the unique identifier (UID) of the
command in the ArcID module (in normal template) - Go to ToolsgtMacrosgtVisual Basic Editor
- In the Module 1 module, create a Sub procedure
with the code such as the following page - Add this macro to your tool bar and run it.
19Sub FullExtentPlus()
- Sub FullExtentPlus()
- '
- ' mcaro FullExtentPlus
- '
- Dim intAns As Integer
- Dim pItem As ICommandItem
- With ThisDocument.CommandBars
- Set pItem .Find(ArcID.PanZoom_FullExtent)
- pItem.Execute
- intAns MsgBox("Zoom to previous extent?",
vbYesNo) - If intAns vbYes Then
- Set pItem .Find(ArcID.PanZoom_ZoomToLas
tExtentBack) - pItem.Execute
- End If
- End With
- End Su
20Commands in VBA
- Command, similar to macro but, allow more
customization in the way that it interacts with
the user and provides ToolTips, description, and
so on. - A command is a type of UIControls(Application-gtDoc
ument-gtCommand Bars-gtCommandBar-gtCommand
Item-gtCommand-gtUIControl-gtUIButton
Control,UIComboBoxControl,UIEditBoxControl,UIToolC
ontrol,
21Create Command
- Go to ToolsgtCustomize and in Save in(at the
bottom of the dialog) change to Untitled - In the Categories list, select UIControls and
click on New UIControl - In the dialog box, choose UIButtonControl as
UIControl type, then click Create and Edit
22Adding code for UIToolControl
- Now you have an Object UIButtonControl and a
subprocedure for UIButtonControl_Click() event.
You need to add code to this event to zoom the
display to the extents of the dataset. Add the
following code to the Click event. So far you
have done exactly same procedures as macro. Next,
you will add a ToolTip and message for the
command.
23Message in command
- Scroll event from Click to Message, this create a
stub function for this command. - Add code to this function
- Scroll to ToolTip and type in code as follows
- UIButtonControl1_ToolTip Full Extent
- Close -gtReturn to ArcMap
24Test this command
- Back to ArcMap
- Go to ToolsgtCustomizegt and click Command tab
and change Save in to Untitled - Drag Project.UIButtonControl to toolbar to
create your own tool. - Test this button and move cursor over the button.
The ToolTip will appear and at the bottom of the
window the status bar will display the
description of this button
25Exercise Create a tool through UIToolControl
- Same procedures as creating UIButtonControl
except selecting UIToolControl and click
Create and Edit - This tool will deal with Select event. Go to
MouseDown event in the Procedures comboBox on
the right-side of the Code Window and add the
following code to this event - Dim pDoc As IMxDocument
- Dim pScreenDisp As IScreenDisplay
- Dim pRubber As IRubberBand
- Dim pEnv As IEnvelope
- Set pDoc ThisDocument
- Set pScreenDisp pDoc.ActiveView.ScreenDisplay
- Set pRubber New RubberEnvelope
- Set pEnv pRubber.TrackNew(pScreenDisp, Nothing)
- pDoc.ActiveView.Extent pEnv
- pDoc.ActiveView.Refresh
26Exercise - continued
- Add the following code to the UIToolControl1_Enabl
ed() event procedure - Dim pDoc As IMxDocument
- Set pDoc ThisDocument
- UIToolControl_Enabled (pDoc.FocusMap.LayerCou
nt ltgt 0) - Add the following code to the UIToolControl1_Curso
rID() event procedure - UIToolControl1_CursorID 3 'Crosshair
- Add ToolTip as described before for this tool
- Create this tool and test out the tool by
selecting it and dragging a rectangle on the
display