Introduction to VBA Programming with Arcobjects
Total Page:16
File Type:pdf, Size:1020Kb
z9/5/2007 Introduction to VBA Workshop Outline Programming with z ArcObjects/VBA overview (9:15-9:45) ArcObjects z Customizing ArcMap interface (9:45 – 10:30) z Visual Basic for Applications (VBA) environment (10:30-11:00) z Morning break (11:00-11:15) GeoTREE Center z VBA ppgrogramming concep ts ( 11:15-12:15) z Lunch (12:15-12:45) University of Northern Iowa z ArcObjects overview (12:45-1:30) Geography z Using ArcObjects z Using ArcObjects 1: Map Display (1:45 – 2:45) July 18, 2007 z Afternoon Break (2:45 – 3:00) z Using ArcObjects II: Selecting, Geoprocessing (3:00 – 4:00) Warning z Developing ArcGIS functionality and understanding ArcObjects is complicated z This workshop is a basic introduction to help you ArcObjects/VBA Overview develop ArcGIS customizations ArcObjects/VBA Overview ArcObjects z ArcGIS provides a large amount of z Set of components or building blocks on which the functionality scaleable ArcGIS framework is built z Developed by ESRI using C++ as classes z However users often want to harness that z Basically everything you see and interact with in any functionalityyyp in different ways than is possible ArcGIS application is an ArcObject out of the box z Maps z Layers z Develop customizations to carry out work-flow z Points tasks z Tables z Develop customized spatial modeling operations z Fields z Rasters z Combine multiple steps into a single customized tool z Buttons z1 z9/5/2007 Map Button ArcObjects Layer z There are a huge number of ArcObjects z Accessible through various Graphic Point programming/development enviroments z Focus today on VBA z Almost impossible to get to know all ArcObjects z A strong background using the applications (ArcMap, ArcCatalog, etc.) important z Learn how to navigate to get to proper ArcObject Polygon Table Field Visual Basic for Applications Other Development (VBA) Environments z Visual Basic z VBA is a development environment that is z C# provided with ArcGIS (also with Microsoft z C++ Word, Excel , Powerpoint , etc . ) with which you z Delphi can access ArcObjects z others z It is a simplified version of Visual Basic z For customizing applications Scripting vs. development environment (ArcObjects) Start VBA Interface z Scripting for geoprocessing in ArcGIS z Python, VBScript, etc. z Scripting calls on ArcObjects to do processing z Scrippgting calls up on one main ArcObj ect z Geoprocessing ArcObject z Development environments (VBA, VB, C# etc.) z Allow access to all ArcObjects z Developing customized interfaces z Distributable customizations z2 z9/5/2007 Scripting vs. Arcobjects (VBA,etc.) z Scripting with Python z ArcObjects with VBA z Clip feature class with z Clip feature class with another feature class another feature class z Add clipped layer to map z Symbolize the new layer Customizing ArcMap Interface z Create a map layout with the new layer z Print the map Customizing ArcMap Interface z You can control the look and feel of the ArcMap interface z Add/remove existing controls A full ArcMap Interface z Create new controls z Can associate VBA code to newly created tools, buttons and menu items Control Types in ArcMap z Toolbars z Buttons, tools Tool Button A minimalist ArcMap Interface z Buttons make something happen immediately z Tools work b y 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 Customization Demonstration and Exercise VBA Development Environment VBA Development Visual Basic Editor Projects Code Module Procedure Window 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 Forms VBA Environment • User can design custom interfaces using z You can store customized controls and codes the form designer in either an .mxd project, in the Normal.mxt or in one of your own template. z If you save these customizations in the Normal.mxt they will be available every time Check box you open ArcMap (only on your computer). z Today we are only going to work saving customizations into a specific .mxd. z4 z9/5/2007 VBA Development Environment Demonstration and Exercise VBA Programming Concepts 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 AlfibldAn example for a variable named 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 Variables Data types z Variables are used to store values z Numbers z It is encouraged practice to ‘declare’ z Integer – whole numbers from -32768-32767 variables z Long – large whole numbers z Dim intMyNumber as Integer z Double – all numbers ((ygvery large or with decimals) z This tells the program what kind of data type z Strings – text the variable is and provides clarity z Boolean – true or false z One programming convention has the z Dates – hold dates variable name prefaced by an abbreviation z Variant – a generic data type that can hold for what the data type is any data type z5 z9/5/2007 Basic data types and abbreviations Setting variables z Integer – int z You set the variables in an assignment z intTemp = 32 statement z Long – lng z lngLength = 45000 Ex. 1 z Double – dbl Dim lngggX as Long z dblArea = 1254.56 lngX = 120000 z String – str Ex. 2 z strStreet = “Clay Street” z Boolean – bln Dim dblAnnualTax as Double z blnCancel = True Dim dblParcelValue as Double z Date – dat dblParcelValue = 100000 z Variant - var dblAnnualTax = 0.05 * dblParcelValue Conditional logic Looping z It is common to have to account for different z A program often needs to loop through a collection conditions in programming of objects z First way to do it is with a For….Next z Use conditional logic For intNum = 1 to 10 z MtMost common iIfThis If Then msgBox “The number is “ & intNum If intTempF <= 32 then Next I msgBox “It might snow” ArcMap Example Else For i = 0 to pMap.LayerCount - 1 msgBox “The layer name is “ & pMap.Layer(i).Name msgBox “It might rain” Next i End if Looping Procedures z Second way to do it is with a Do….Until or z Procedures hold blocks of code that carry out Do…While specific functions Do While intCnt < 50 msgBox intCnt z We have seen event procedures intCnt = intCnt + 1 z E.g. My Bu tton_Cli c 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 Sub procedures Call sub example z Can be a control event procedure or a stand-alone Public Sub ShowName() procedure that is called from somewhere else Dim strName as string z Should be named logically Call GetName(strName) z E.ggp. ReturnMapRasters msgBox “The name is “ & strName z When you create a control in ArcMap or Form then End Sub each sub procedure linked with an event is automatically named Public Sub GetName(strName as string) z ‘ZoomOut_Click’ (ArcMap button) strName = InputBox(“Enter Name”, “Name”) z ‘cmdGetMapName_Click’ (form command button) End Sub Functions Function example z Functions take input, process the input, and Public Sub ReportTax() return output ……… dblPropVal = 80000 z Many built-in functions in VBA dblTotalTax = CalculateTax(dblPropVal) z It(26)tInt(2.6) returns 2 msgBox “The tax due is “ & dblTotalTax z Len(“Long”) – returns 4 (i.e. length of string) End Sub z Functions have a single output that can be string or numeric Public Function CalculateTax(dblPropVal) as Double z You can define your own functions CalculateTax = 0.075 * dblPropVal End Function 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 cmdLayer Name. Cap tion = “Firs t Layer Name ” z txtLayerName. Change z cmdLayerName.Enabled = False z frmLayerName.Initialize z txtLayerName.Text = “” z frmLayerName.Width = 200 Methods Other Tips z Things that an object can do z To get help put your cursor on a method or z frmLayerName.Hide property and click F1 z cmdLayerName.Move 25, 50 z cboLayerName. AddItem “Iowa Counties” z Other VBA objects have methods z E.g. Collection objects are lists to which can hold different variables z rasterColl.Add pRaster z intRasterCnt = rasterColl.Count Object Oriented Programming z OOP is centered around objects z OOP programs have objects that hold data, have properties, respond to methods, and raise events Overview of ArcObjects z E.g.