adv_gisarcobjects - 11 vba and arcobjects fall 2003 advanced gis

26
Adv_GIS ArcObjects - 1 1 VBA and ArcObjects Fall 2003 Advanced GIS

Upload: donald-joseph

Post on 27-Dec-2015

236 views

Category:

Documents


7 download

TRANSCRIPT

Adv_GIS ArcObjects - 1 1

VBA and ArcObjects

Fall 2003

Advanced GIS

Adv_GIS ArcObjects - 1 2

VBA• Available in ArcMap, ArcCatalog, and

ArcScene applications

• Many other programming languages can work with ArcObjects, such as VB,C++

• VBA is the easiest one for interacting with ArcMap.

Adv_GIS ArcObjects - 1 3

ArcObjects• There are 1,000 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

Adv_GIS ArcObjects - 1 4

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

Adv_GIS ArcObjects - 1 5

II: Locate the correct object model

• Identify a subtask

• Extract keywords

• Search for the correct object model diagrams

• Review all related documentation

Adv_GIS ArcObjects - 1 6

III: Navigate the object model diagram

• Review the structure of the object model diagram

• Trace the flow between classes and assemble code

Adv_GIS ArcObjects - 1 7

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.

Adv_GIS ArcObjects - 1 8

Customize Toolbars

• Open a ArcMap screen (Start>Programs>ArcGIS>ArcMap)

• Bring in your county themes by click on Add layers from toolbar and navigate to your folder and then add them to current project.

• You may also convert ArcView project to ArcMap (File > Import from ArcView project…..)

• Once your themes are in the project, practice the functions of ArcMap (such as right-click on layer to open command panel….)

Adv_GIS ArcObjects - 1 9

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 Toola Name area type in “My personal tools” and Save in “Normal.mxt”

• Click OK

Adv_GIS ArcObjects - 1 10

Adding more tools to your personal tool box

• Adding buttons and menus to your personal tool by selecting commands dragging to “My personal tools” toolbar. With category of Menu open (Command>Menu), drag commands on right window to “My personal tools”

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

Adv_GIS ArcObjects - 1 11

Saving change to a template

• Go to File > Save As and navigate to the installed folder (most probably e:\arcgis81 d:arcgis81 or c:\arcgis81, or \arcexec80\)

• Move in template folder and create a new folder called newtemplate and save your file as test.mxt (ArcMap Template, *.mxt)

Adv_GIS ArcObjects - 1 12

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 “Creat” (this will take you to the VB screen and you are ready to create a customized tool)

Adv_GIS ArcObjects - 1 13

Code window

Sub MyZoomIn()'' macro; MyRoomIn'Dim pDoc As IMxDocumentDim pEnv As IEnvelopeSet pDoc = ThisDocumentSet pEnv = pDoc.ActiveView.ExtentpEnv.Expand 0.5, 0.5, TruepDoc.ActiveView.Extent = pEnvpDoc.ActiveView.RefreshEnd 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

Adv_GIS ArcObjects - 1 14

Sub MyZoomIn()'' macro; MyRoomIn'Dim pDoc As IMxDocumentDim pEnv As IEnvelopeSet pDoc = ThisDocumentSet pEnv = pDoc.ActiveView.ExtentpEnv.Expand 0.5, 0.5, TruepDoc.ActiveView.Extent = pEnvpDoc.ActiveView.RefreshEnd 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 DocThisDocument - predefined 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

Adv_GIS ArcObjects - 1 15

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)

• The display zoomed in 50% smaller.

Adv_GIS ArcObjects - 1 16

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 #5)

• 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

Adv_GIS ArcObjects - 1 17

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

Adv_GIS ArcObjects - 1 18

Calling built-in commands

• Calling existing commands is working with the ArcID module. Using Find methods, the code locates the unique identifier (UID) of the command in the ArcID module (in normal template)

• Go to Tools>Macros>Visual Basic Editor• In the Module 1 module, create a Sub procedure

with the code such as the following page:• Add this macro to your tool bar and run it.

Adv_GIS ArcObjects - 1 19

Sub FullExtentPlus()Sub FullExtentPlus()'' mcaro; FullExtentPlus'Dim intAns As IntegerDim pItem As ICommandItem With ThisDocument.CommandBars Set pItem = .Find(ArcID.PanZoom_FullExtent) pItem.Execute intAns = MsgBox("Zoom to previous extent?", vbYesNo) If intAns = vbYes Then Set pItem = .Find(ArcID.PanZoom_ZoomToLastExtentBack) pItem.Execute End If End WithEnd Su

Adv_GIS ArcObjects - 1 20

Commands in VBA

• Command, similar to macro but, allow more customization in the way that it interacts with the user and provides ToolTips, description, and so on.

• A command is a type of UIControls(Application->Document->Command Bars->CommandBar->Command Item->Command->UIControl->UIButton Control,UIComboBoxControl,UIEditBoxControl,UIToolControl,

Adv_GIS ArcObjects - 1 21

Create Command

• Go to Tools>Customize and in “ Save in”(at the bottom of the dialog) change to “Untitled”

• In the Categories list, select UIControls and click on “New UIControl”

• In the dialog box, choose UIButtonControl as UIControl type, then click “Create and Edit”

Adv_GIS ArcObjects - 1 22

Adding code for UIToolControl• Now you have an Object “UIButtonControl” and a

subprocedure for UIButtonControl_Click() event. You need to add code to this event to zoom the display to the extents of the dataset. Add the following code to the Click event. So far you have done exactly same procedures as macro. Next, you will add a ToolTip and message for the command.

Adv_GIS ArcObjects - 1 23

Message in command

• Scroll event from Click to Message, this create a stub function for this command.

• Add code to this function

• Scroll to ToolTip and type in code as follows:

• UIButtonControl1_ToolTip = “Full Extent”

• Close ->Return to ArcMap

Adv_GIS ArcObjects - 1 24

Test this command

• Back to ArcMap• Go to Tools>Customize> and click “Command” tab and change

“Save in” to “Untitled”• Drag “Project.UIButtonControl” to toolbar to create your own tool.• Test this button and move cursor over the button. The ToolTip will

appear and at the bottom of the window the status bar will display the description of this button

Adv_GIS ArcObjects - 1 25

Exercise – Create a tool through UIToolControl• Same procedures as creating UIButtonControl except selecting

“UIToolControl” and click “Create and Edit”• This tool will deal with “Select” event. Go to “MouseDown”

event in the Procedures comboBox on the right-side of the Code Window and add the following code to this event:

Dim pDoc As IMxDocumentDim pScreenDisp As IScreenDisplayDim pRubber As IRubberBandDim pEnv As IEnvelopeSet pDoc = ThisDocumentSet pScreenDisp = pDoc.ActiveView.ScreenDisplaySet pRubber = New RubberEnvelopeSet pEnv = pRubber.TrackNew(pScreenDisp, Nothing)pDoc.ActiveView.Extent = pEnv

pDoc.ActiveView.Refresh

Adv_GIS ArcObjects - 1 26

Exercise - continued• Add the following code to the UIToolControl1_Enabled()

event procedure Dim pDoc As IMxDocument Set pDoc = ThisDocument UIToolControl_Enabled = (pDoc.FocusMap.LayerCount <> 0)• Add the following code to the UIToolControl1_CursorID()

event procedure UIToolControl1_CursorID = 3 'Crosshair• Add ToolTip as described before for this tool• Create this tool and test out the tool by selecting it and

dragging a rectangle on the display