Professional Documents
Culture Documents
VBAArcObjects Pres PDF
VBAArcObjects Pres PDF
Warning
z Developing ArcGIS functionality and
understanding ArcObjects is complicated
z This workshop is a basic introduction to help you
develop ArcGIS customizations
ArcObjects/VBA Overview
z1
z9/5/2007
Table
Field
z2
z9/5/2007
Tool Button
A minimalist ArcMap Interface z Buttons make something happen immediately
z Tools work
ork by
b clicking on the control and then clicking in the map
display
z Menus
z Menu items (really are just buttons)
z Combo boxes (e.g. Map scale)
z Provide user dropdown choice
z Editbox (rarely used)
z To enter and report information
z3
z9/5/2007
Environment
z Accessed through ArcMap or ArcCatalog
z Tools for developing code and user interfaces
z i.e. modules for code and forms for user
interfaces
Object Box
z A sophisticated program in itself that takes Procedure Box
time to learn
z Lots of functionality and tools to help you
more efficiently write code
z4
z9/5/2007
Comments Intellisense
z For your sake and others it is important to put z VBA has functionality to finish code for you
comments in your code z While typing a variable, method, procedure,
z Comment enough so when you return to the etc., clicking Ctrl+Spacebar will finish code
code later it will make it much easier for you z A example
An l ffor a variable
i bl namedd
to understand strStateName
z Comments begin with and are shown in red z Type strSt and click Cntrl+Spacebar and VBA will
Get the number of layers that are in the map finish strStateName for you
intLayerCnt = pMap.LayerCount z Very useful to guard against typos
z5
z9/5/2007
Looping Procedures
z Second way to do it is with a Do.Until or z Procedures hold blocks of code that carry out
DoWhile
Do While intCnt < 50
specific functions
msgBox intCnt z We have seen event procedures
intCnt = intCnt + 1 z E MyButton_Click
E.g. M B tt Cli k
Loop
ArcMap Example z Two types
Do Until pRow Is Nothing z Sub procedures
dblArea = pRow.Value(2)
z Functions
Set pRow = pCursor.NextRow
Loop
z6
z9/5/2007
Objects Properties
z Mentioned ArcObjects before z Properites are characteristics of an object
z There are other objects z Can set properties in form designer window
z Forms and all controls are objects
z Other objects such as Collections and Arrays
z Each of these objects has properties, events
and methods
z Property is a characteristic or attribute
z Events are user actions that happen to an object
z Methods are things an object can do
z7
z9/5/2007
Properties Events
z Can also set form and command properties z Forms and controls have a number of
through code potential events they react to
z Use the object.property syntax z cmdLayerName.Click
z cmdLayerName.Caption
dL N C ti = Fi
Firstt Layer
L N
Name z txtLayerName Change
txtLayerName.Change
z cmdLayerName.Enabled = False z frmLayerName.Initialize
z txtLayerName.Text =
z frmLayerName.Width = 200
z8
z9/5/2007
z9
z9/5/2007
z10
z9/5/2007
OMD Key
z There is a key on every OMD explaining
classes and relationships
z11
z9/5/2007
z12
z9/5/2007
z13
z9/5/2007
Code example
z14
z9/5/2007
IFeatureClass
ITable IRow
IEnvelope Composed
p
Inherits
IEnvelope XMin: Double
FeatureClass Feature
*
z15
z9/5/2007
'get the
intRowCnt = pOpenTable.RowCount(Nothing)
MsgBox intRowCnt
Selection Set
Tables and feature classes 'select the counties that have a population > 100000
'Declarations
Dim pCountyFLayer As IFeatureLayer
Dim pCountyQF As IQueryFilter
Row Dim pCountyFSel As IFeatureSelection
ICursor
..
Value 'get the layer and its feature class
NextRow IRow
ICursor Set pCountyFLayer = pMap.Layer(0)
Search
'set feature selection to the layer and refresh the map
ITable IQueryFilter Set pCountyFSel = pCountyFLayer QI
pCountyFSel.SelectFeatures pCountyQF, esriSelectionResultNew, False
pMxDoc.ActiveView.Refresh
z16
z9/5/2007
FeatureSelection FeatureLayer
z There is no central location for accessing
spatial processing objects
SelectFeatures FeatureClass
IFeatureSelection IFeatureLayer z IBasicGeoprocessor
z Cli Di
Clip, Dissolve,
l IIntersect,
t t MMerge, U
Union,
i etc.
t
z ITopologicalOperator
z Buffer, Clip, Cut, Simplify, etc.
QueryFilter z ITopologicalOperator2
z ConstructBuffers, ClipToDomain
IQueryFilter
Buffer Example
'This sub should buffer the first feature layer in map with graphics
'Declarations
..
Dim pTopoOperator As ITopologicalOperator
Dim pFeatureCursor As IFeatureCursor
Dim pFeature As IFeature
Dim pElement As IElement
Dim pGraphicsContainer As IGraphicsContainer
..
'set the graphics container
Set pGraphicsContainer = pMap
'get the layer and feature class
..
'set up feature cursor, loop through and buffer each feature, add graphic to map
Set pFeatureCursor = pBufferFC.Search(Nothing, True)
Set pFeature = pFeatureCursor.NextFeature
Do Until pFeature Is Nothing
Set pTopoOperator = pFeature.Shape
Set pElement = New PolygonElement
pElement.Geometry = pTopoOperator.Buffer(2500)
pGraphicsContainer.AddElement pElement, 0
Set pFeature = pFeatureCursor.NextFeature
Loop
'refresh the view
pMxDoc.ActiveView.Refresh
z17