Presentation is loading. Please wait.

Presentation is loading. Please wait.

Adv_GISArcObjects - 11 VBA and ArcObjects Fall 2003 Advanced GIS.

Similar presentations


Presentation on theme: "Adv_GISArcObjects - 11 VBA and ArcObjects Fall 2003 Advanced GIS."— Presentation transcript:

1 Adv_GISArcObjects - 11 VBA and ArcObjects Fall 2003 Advanced GIS

2 Adv_GISArcObjects - 12 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.

3 Adv_GISArcObjects - 13 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

4 Adv_GISArcObjects - 14 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

5 Adv_GISArcObjects - 15 II: Locate the correct object model Identify a subtask Extract keywords Search for the correct object model diagrams Review all related documentation

6 Adv_GISArcObjects - 16 III: Navigate the object model diagram Review the structure of the object model diagram Trace the flow between classes and assemble code

7 Adv_GISArcObjects - 17 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.

8 Adv_GISArcObjects - 18 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….)

9 Adv_GISArcObjects - 19 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

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

11 Adv_GISArcObjects - 111 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)

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

13 Adv_GISArcObjects - 113 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.XMinXMaxYMinYMax ArcMap Doc ThisPredefined variable-is the Idocument interface to the MxDocument object

14 Adv_GISArcObjects - 114 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 ArcMap Doc ThisDocument - 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

15 Adv_GISArcObjects - 115 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.

16 Adv_GISArcObjects - 116 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

17 Adv_GISArcObjects - 117 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

18 Adv_GISArcObjects - 118 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.

19 Adv_GISArcObjects - 119 Sub FullExtentPlus() ' ' mcaro; FullExtentPlus ' Dim intAns As Integer Dim 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 With End Su

20 Adv_GISArcObjects - 120 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,

21 Adv_GISArcObjects - 121 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”

22 Adv_GISArcObjects - 122 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.

23 Adv_GISArcObjects - 123 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

24 Adv_GISArcObjects - 124 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

25 Adv_GISArcObjects - 125 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 IMxDocument Dim pScreenDisp As IScreenDisplay Dim pRubber As IRubberBand Dim pEnv As IEnvelope Set pDoc = ThisDocument Set pScreenDisp = pDoc.ActiveView.ScreenDisplay Set pRubber = New RubberEnvelope Set pEnv = pRubber.TrackNew(pScreenDisp, Nothing) pDoc.ActiveView.Extent = pEnv pDoc.ActiveView.Refresh

26 Adv_GISArcObjects - 126 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


Download ppt "Adv_GISArcObjects - 11 VBA and ArcObjects Fall 2003 Advanced GIS."

Similar presentations


Ads by Google