Arcgis Vba
Arcgis Vba
Introduction
Add custom controls to ArcGIS Overview of ArcObjects Use of VBA in ArcGIS Demonstrations
Commands, Tools, Editbox, Combobox Step 4 Drag new control onto interface
Menu: Step 2 Command Tab > New Menu > drag new menu on interface
A set of components of the ArcGIS platform which can be accessed through programming environment (VBA, VB, C++, .NET, VB Script) ArcObjects have:
characteristics which can be queried or set the ability to perform operations the ability to respond to changes in application framework in which they are deployed
Synoptic view of ArcObjects Included with ArcObjects Developer Kit ArcGIS installation option Diagrams (OMDs) in PDF format
Template (blueprint) for creating ArcGIS objects Defines interfaces, properties and methods for an object With VBA, code is stored in the document
Method of communication with an object Logical groups of methods and properties Can be one or many on a single object Can be used by more than one type object May be inbound or outbound
Properties of interface can be set and/or read Methods are actions object performed through interface Accessed as: <object interface>.<property or method> (par1, par2)
Egg
Chicken
Nest
Wings
Navigating OMD
Navigating OMD
Navigating OMD
Maps (Collection)
Map Map
Document Events
Code executes when user interacts with document
Defining Variables
Must define object variables before setting Use the Dim, Private, or Public statements Variables pointing to ArcObjects must reference one of the objects interfaces
Option Explicit Dim <variable> as <VBA instrinsic type> Dim strLayerName as String Public strTitle as String Dim <variable> as <interface name> Dim pMxDoc as IMxDocument Private pFL as IFeatureLayer
Starting points for writing code Application pointer to ArcMap or ArcCatalog ThisDocument pointer to current document
Objects
Created from class blueprints Stored in memory Created from interface property or method
Dim pPnt As IPoint Dim pMxDocument As IMxDocument Set pMxDocument = ThisDocument Set pPnt = _ pMxDocument.CurrentLocation Dim pTextElement As ITextElement Set pTextElement = _ New TextElement
Query Interface:
Method for getting to a different interface on an existing object using an established interface variable
Query Interface:
Coding example
When using query interface When instantiating an object with NEW keyword
When instantiating an object from a property of another object Setting a ByRef property to another object Not necessary for intrinsic data types
Create with VBA editor Run macros from Tools menu > Macros > Macros
Create with VBA editor Run macros from Tools menu > Macros > Macros
Export to class file or text file Copy/Paste code text to text editor
ArcObjects References
ArcObjects Online: http://arconline.esri.com/arcobjectsonline/ Exploring ArcObjects (on Digital Books CD) ArcObjects Developer Help ESRI Object Browser
ArcObjects Training
Instructor-Led Courses
Introduction to Programming ArcObjects with VBA Migrating from Avenue to VBA Advanced ArcObjects Component Development I Advanced ArcObjects Component Development II C++ Advanced ArcObjects Component Development II .NET
Exploring the VBA Environment (Free) Customizing ArcMap: Easy Ways to Extend the Interface Introduction to Visual Basic for ESRI Software Working with Variables and Functions in VBA Working with Forms in VBA Understanding Branching and Looping in VBA
ArcObjects References
ArcObjects Online: http://arconline.esri.com/arcobjectsonline/ Exploring ArcObjects (on Digital Books CD) ArcObjects Developer Help ESRI Object Browser
Demonstration toolbar
Sample code:
Sample code:
For the custom button click, get selected layer, then call a subroutine to draw the labels
Private Sub UIButtonControl1_Click() On Error GoTo Errorhandler Dim pMxDoc As IMxDocument Dim pGFL As IGeoFeatureLayer Set pMxDoc = ThisDocument If Not TypeOf pMxDoc.SelectedLayer Is IFeatureLayer Then GoTo Exitpnt Set pGFL = pMxDoc.SelectedLayer UpdateLabels pGFL pMxDoc.ActiveView.PartialRefresh esriViewGeography, Nothing, _ pMxDoc.ActiveView.Extent Exitpnt: Set pMxDoc = Nothing Set pGFL = Nothing Exit Sub Errorhandler: Resume Exitpnt End Sub
Sample code:
Filter selected TOC layer based on editbox value for chosen field
Private Sub UIEditBoxControl1_KeyDown(ByVal keyCode As Long, ByVal shift As Long) On Error GoTo Errorhandler Dim pMxDoc As IMxDocument Dim pFLD As IFeatureLayerDefinition If keyCode <> 13 Then GoTo Exitpnt Set pMxDoc = ThisDocument If Not TypeOf pMxDoc.SelectedLayer Is IFeatureLayer Then GoTo Exitpnt Set pFLD = pMxDoc.SelectedLayer If Len(UIComboBoxControl1.EditText) = 0 Then pFLD.DefinitionExpression = "" Else pFLD.DefinitionExpression = UIComboBoxControl1.EditText & " = '" & _ UIEditBoxControl1.Text & "'" End If If pMxDoc.SelectedLayer.Visible = False Then pMxDoc.SelectedLayer.Visible = True pMxDoc.UpdateContents End If pMxDoc.ActiveView.PartialRefresh esriViewGeography, Nothing, _ pMxDoc.ActiveView.Extent Exitpnt: Set pMxDoc = Nothing Set pFLD = Nothing Exit Sub Errorhandler: Resume Exitpnt End Sub
Sample code:
Sample code:
Sample code:
Set pMxDoc = ThisDocument Set pMxApp = Application Set pRubberPolygon = New RubberPolygon Set pFillSymbol = New SimpleFillSymbol pFillSymbol.Style = esriSFSHollow Set pPolygon = pRubberPolygon.TrackNew(pMxDoc.ActiveView.ScreenDisplay, pFillSymbol) If pPolygon Is Nothing Then GoTo Exitpnt Set pTOp = pPolygon If pTOp.IsSimple = False Then MsgBox "Selection polygon cannot self-intersect." GoTo Exitpnt End If pMxDoc.FocusMap.SelectByShape pPolygon, pMxApp.SelectionEnvironment, False pMxDoc.ActiveView.PartialRefresh esriViewGeoSelection, Nothing, pMxDoc.ActiveView.Extent Exitpnt: Set pMxApp = Nothing Set pRubberEnv = Nothing Set pFillSymbol = Nothing Set pRubberPolygon = Nothing Set pPolygon = Nothing Set pMxDoc = Nothing End Sub
When ArcMap document opened, collapse all TOC layer symbology and make all layers visible, regardless of how document was saved
Private Function MxDocument_OpenDocument() As Boolean Dim pDoc As IMxDocument Dim pGFL As IGeoFeatureLayer Dim pLInfo As ILegendInfo Dim pLGrp As ILegendGroup Dim i As Long Set pDoc = ThisDocument For i = 0 To pDoc.FocusMap.LayerCount - 1 If TypeOf pDoc.FocusMap.Layer(i) Is IFeatureLayer Then Set pGFL = pDoc.FocusMap.Layer(i) Set pLInfo = pGFL Set pLGrp = pLInfo.LegendGroup(0) pLGrp.Visible = False pGFL.Visible = True End If Next i pDoc.UpdateContents pDoc.ActiveView.Refresh Set pDoc = Nothing Set pGFL = Nothing Set pLInfo = Nothing Set pLGrp = Nothing End Function
Sample code:
Create ArcMap extension .dll so when ArcMap document opened, collapse all TOC layer symbology and make all layers visible, regardless of how document was saved
Sample code:
Create ArcMap extension .dll so when ArcMap document opened, collapse all TOC layer symbology and make all layers visible, regardless of how document was saved
Sample code:
Create ArcMap extension .dll so when ArcMap document opened, collapse all TOC layer symbology and make all layers visible, regardless of how document was saved
Sample code:
Step 3
Implement IExtension, establish MxDocument variable WithEvents and establish other necessary modulelevel variables
Create ArcMap extension .dll so when ArcMap document opened, collapse all TOC layer symbology and make all layers visible, regardless of how document was saved
Sample code:
Step 4
Add code to the OpenDocument event to control TOC configuration
Create ArcMap extension .dll so when ArcMap document opened, collapse all TOC layer symbology and make all layers visible, regardless of how document was saved
Sample code:
Step 5
Compile with no compatibility then with binary compatibility
Create ArcMap extension .dll so when ArcMap document opened, collapse all TOC layer symbology and make all layers visible, regardless of how document was saved
Sample code:
Step 6
Register with ESRI Mx Extensions Category using Categories.exe in the ArcGIS/arcexe83/bin directory
Create ArcMap extension .dll so when ArcMap document opened, collapse all TOC layer symbology and make all layers visible, regardless of how document was saved
Sample code:
Step 6
OR register with ESRI Mx Extensions Category using .reg file called from .bat, all in same directory as the extension .dll