VBA and ArcObjects Programming - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

VBA and ArcObjects Programming

Description:

... money and not a technician.. Logon to monster.com and type 'GIS programmer' ... http://jobsearch.monster.com/getjob.asp?JobID=25991128&AVSDM=2004-12-30 10 ... – PowerPoint PPT presentation

Number of Views:69
Avg rating:3.0/5.0
Slides: 21
Provided by: pet978
Category:

less

Transcript and Presenter's Notes

Title: VBA and ArcObjects Programming


1
VBA and ArcObjects Programming
  • Week 1
  • Spring 2005
  • Advanced GIS

2
Why learn programming ?
  • Because you make more money and not a
    technician..
  • Logon to monster.com and type GIS programmer
  • http//jobsearch.monster.com/getjob.asp?JobID2599
    1128AVSDM20042D122D30103A473A49Logo1col
    dltcicyUSbrd12C18622C1863lidfnqGISpro
    grammer

3
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
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
ArcObjects and ArcGIS
  • ArcMap and ArcCatalog are built with ArcObjects
  • Each GIS part is a programmable object
  • Users and programmers use same objects

6
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
    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

7
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

8
II Locate the correct object model
  • Identify a subtask
  • Extract keywords
  • Search for the correct object model diagrams
  • Review all related documentation

9
III Navigate the object model diagram
  • Review the structure of the object model diagram
  • Trace the flow between classes and assemble code

10
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.

11
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 (such as right-click on layer
    to open command panel.)

12
Showing/Hiding toolbars
  • Double-click any unoccupied area of any toolbars
    to display the Customize dialog box (or from
    ToolsCustomize..)
  • 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
    havent 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 dont intend to do so. Simply save it
    to Untitled to avoid confusion to other users.
  • Click OK

13
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)
  • 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.

14
Saving change to a template
  • Go to File Save As and navigate to the
    installed folder (most probably e\arcgis81
    darcgis81 or c\arcgis81, or \arcexec80\)
  • You may have problem doing this due to the
    security setting of the computer. Save it to your
    own folder instead.
  • Move in template folder and create a new folder
    called newtemplate and save your file as test.mxt
    (ArcMap Template, .mxt)

15
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)

16
Code 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
17
ThisDocument - 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
18
Run Macro
  • Go to File Close Return to ArcMap
  • In ArcMap, go to ToolsMacro and select
    Module1.MyZoomIn macro and click Run (make sure
    your macro settings is in Normal)
  • The display zoomed in 50 smaller.

19
Add Macro to a toolbar
  • Go to Tools Customize.
  • In the Toolbar tab, make sure My personal Tool
    is still there (created previously on slide 10)
  • 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
20
Exercise
  • 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
Write a Comment
User Comments (0)
About PowerShow.com