Download presentation
Presentation is loading. Please wait.
Published byNicholas Lloyd Modified over 9 years ago
1
Adv_GISArcObjects - 11 VBA and ArcObjects Programming Week 1 Spring 2008 Advanced GIS
2
Adv_GISArcObjects - 12 Why 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….
3
Adv_GISArcObjects - 13 Object-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)
4
Adv_GISArcObjects - 14 Visual 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.
5
Adv_GISArcObjects - 15 ArcObjects 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
6
Adv_GISArcObjects - 16 ArcObjects 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 ArcGIS’s 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
7
Adv_GISArcObjects - 17 I: 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 Identify a subtask Extract keywords Search for the correct object model diagrams Review all related documentation II: Locate the correct object model Review the structure of the object model diagram Trace the flow between classes and assemble code III: Navigate the object model diagram
8
Adv_GISArcObjects - 18 Describe 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.
9
Adv_GISArcObjects - 19 Customize 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”
10
Adv_GISArcObjects - 110 Showing/Hiding toolbars Double-click any unoccupied area of any toolbars to display the Customize dialog box (or from Tools|Customize..) 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 haven’t 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 don’t want to do so. Simply save it to “Untitled” to avoid confusion to other users. Click OK
11
Adv_GISArcObjects - 111 Adding 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 don’t have one, create one in G drive)
12
Adv_GISArcObjects - 112 Writing 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)
13
Adv_GISArcObjects - 113 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.XMinXMaxYMinYMax Code window 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
14
Adv_GISArcObjects - 114 Run Macro Go to File | Close Return to ArcMap In ArcMap, go to Tools |Macro and select Module1.MyZoomIn macro and click Run (make sure your macro settings is in Normal) (Alt + F8) The display zoomed in 50% smaller.
15
Adv_GISArcObjects - 115 Add 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
16
Adv_GISArcObjects - 116 Exercise - 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
17
Adv_GISArcObjects - 117 UIControls – User Interface Controls UIButton - Save UITool - Pan UIComboBox UIEditBox Code is required for UI to work.
18
Adv_GISArcObjects - 118 Project, Code module and Procedure
19
Adv_GISArcObjects - 119 Project/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
20
Adv_GISArcObjects - 120 Procedures 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.
21
Adv_GISArcObjects - 121 InputBox/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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.