Introduction to Geographic Information Systems in Forest Resources
UW Home GIS@UW Search
Syllabus Schedule Class Meetings Assignments Course Data Internet Search

Current Grades

Contact Us CFR 590 Internet-only section Lab Locations  


Introduction to Avenue

Overview:

Read me first
What Avenue is
How Avenue works
Creating new scripts
Variables in scripts
Comments in scripts
Compiling scripts
Running scripts
Customizing the ArcView 3.x GUI
Creating ArcView 3.x extensions
Finding/stealing/begging scripts

 


Read me first

This lesson is intended to give the most brief and cursory overview to using Avenue. It IS NOT intended to be a substitute for digging your heels in and learning Avenue, which is a process that will take a large amount of time, specific objectives/projects, and a high degree of dedication. If you intend to learn to program with Avenue, your best source will be (as always) the ArcView 3.x on-line documentation. If you have not taken the documentation seriously up to this point, this would be a good time to start (better late than never). If you do not take the documentation seriously while attempting to learn to program in Avenue, you might as well throw in the towel and spare yourself a lot of time and difficulty, and take up some other hobby instead.

If, after those grave warnings, you still want to bite this off, there are a few resources available.

  1. The Avenue book that comes with ArcView 3.x is not extremely helpful.

  2. You may want to consider purchasing 2 books by Amir Razavi (ArcView 3.x Gis/Avenue Developer's Guide, ArcView 3.x GIS/Avenue Programmer's Reference). Both of these texts include descriptions as well as sample scripts.

  3. Definitely sign up for the ESRI listserv ArcView 3.x -L. The listserv is extremely active, and will give you an idea of what kinds of problems people have with ArcView 3.x and Avenue, and how many of those problems get solved.

  4. Learn to search the ESRI ArcScripts page. Make this your first stop when you are embarking on an Avenue programming project, because someone may have already written and distributed scripts that will solve your particular problem. Either that, or you can get some scripts that will be good starting points for your own customization.

  5. Snoop around in ArcView 3.x to see examples of scripts that work.

 


What Avenue is

ArcView 3.x 's own help pages describe Avenue most succinctly:

Avenue is the programming language and development environment that's part of ArcView 3.x. Avenue is fully integrated with ArcView 3.x and the work you do will run on any of the platforms for which ArcView 3.x is available. There are many uses for Avenue: you can use Avenue to customize the way you work with ArcView 3.x ; you can direct ArcView 3.x to perform a specific task that you need to do; or you can even develop a complete application that works along with ArcView 3.x 's graphical user interface.

ArcView 3.x provides the necessary customization and language environment tools in an easy-to-use framework so you can work with Avenue and see your results right away. You can create the graphical user interface for what you want to do, establish some initial properties for the graphical controls that a user will interact with, fine tune the behavior and appearance of those controls, and write Avenue code that responds to what goes on in the interface you've created. In addition, you can link scripts written in Avenue to other events such as starting up and shutting down a project.

 

Avenue is the scripting language that allows you to customize ArcView 3.x. The scripts allow you:

 

Nearly all of the out-of-the-box functionality of ArcView 3.x is accessed through a series of Avenue scripts. When you use menus, buttons, and tools, you are actually running Avenue scripts!

ArcView 3.x extensions are "packages" of scripts that are used for easy transfer and load/unload of custom functions. The scripts in the extensions are "hidden," but as long as the scripts are not encrypted, they can be accessed manually.

Most of the scripts that are part of ArcView 3.x and ArcView 3.x extensions can be loaded and customized. More about that later....

 


How Avenue works

From ArcView 3.x 's help:

Avenue is an object-oriented scripting language. The emphasis in Avenue, as in all object-oriented systems, is on identifying objects and then sending them requests. You can think of an object as a package that is comprised of tightly coupled data and functionality. You can contrast this with procedural programming practices in which the language places an emphasis on the function (procedure). In Avenue, instead of calling functions explicitly with arguments, you send a request to an object. When an object receives this request it performs some action. ArcView 3.x 's objects are members of a class hierarchy that are organized into functional categories related to all aspects of the application.

You use Avenue's statements to organize and structure when and how requests are made. Requests are Avenue's counterpart to the traditional function call. A function call and its implementation are in one-to-one correspondence, whereas a request can trigger one of any number of methods. A request specifies what an instance of a given class will do and a method specifies how it is done. Therefore, programming in Avenue consists of writing object requests rather than calling functions. By sending a request to an object, you activate a method appropriate to the class of which the object is an instance. An object in Avenue always responds to a request by returning an object; in some cases, the request creates a new object and in other cases, the original request returns an existing object.

In procedural languages, writing code consists of establishing routines that call other routines. You maintain the state of the program in variables (basic types or structures, with global or local scopes) and these routines operate on the state of these variables. In Avenue, you maintain and control the state of the system in the objects that have been instantiated.

 

Objects

ArcView 3.x objects are any entity that ArcView 3.x can work with.

The list of objects includes (but is not limited to):

 

Classes

Each of these objects are members of classes. Classes are hierarchical groups of objects that share common characteristics. For example, the shape class contains 6 different classes (circle, line, multipoint, oval, point, and rect). A particular shape object, such as a rectangle on a view, is an instance of the rect class.

The object/class model and the relationships among different classes is documented in the ArcView 3.x help system. Here is the help topic displaying the object model and the following discussion for the Shape class:

 

Requests

Again, from ArcView 3.x 's help:

Avenue requests let you create, control, or get information about objects. Each Avenue class provides requests that operate on the class or instances of that class. The requests trigger methods that are class-specific.

For example, you can move a graphic object like a line by sending that line a request to move itself a certain distance in the x- and y-direction. Likewise, you can display a theme object by sending the theme a request to draw itself, or you can remove a view object by telling that view to close. You can find the product of two numbers by using a multiply request. You can inquire about the visibility of a control by sending it the IsVisible request or learn which document is active by sending the project a GetActiveDoc request.

When you send a request to an object in Avenue, the request results in the return of another object. In some cases these objects represent information about other objects. For example, the statement:

theName = theView.GetName

returns a string object which is the name of the object "theView". The variable theName receives that object.

(The request is GetName.)

Likewise, the expression:

myNumber = 2 + 5

returns a number object which is the result of the addition request to two number objects. ArcView 3.x assigns that number object to the variable myNumber. Some requests result in the return of the NIL object.

Thus, mastering Avenue is a matter of mastering objects, classes, and requests. There is more to it than simply memorizing a series of facts, however. Programming with Avenue is similar to programming other languages, in that the same types of conceptual structures are used (e.g., logical testing, flow control, input/output). In order to really master Avenue, you will need to have a very strong handle on programming in general, object-oriented programming in particular, and the specifics of Avenue objects, classes, and requests.

 


Creating new scripts

Scripts, as you should know by now (simply by having looked at the Project window), are another type of ArcView 3.x document.

 

Any scripts that exist in the current project (with the exception of system scripts and extension scripts) will be listed in the Project window.

A new script is created by clicking the New button on the Project window while the Scripts icon is active (or double-clicking on the Scripts icon). This opens a new blank script in which you can begin to write an Avenue program.

 

The script document is both the script itself and a text editor similar in function to Notepad.

 

Like other documents, scripts have properties, including names, that can be altered.

 


Variables in scripts

In order to build flexibility into scripts, variables are used. From ArcView 3.x 's help:

Avenue variables reference objects. A variable is created when it appears on the left hand side of an assignment statement. The variable references the object on the right-hand side of the equals sign. A variable name preceded by an underscore (_) is a global variable. The scope of global variables extends to all scripts in the project.

aDoc = av.GetActiveDoc

In the example above, the variable name is aDoc, and its value is what is returned by the GetActiveDoc request on the av object (the av object is a reserved object that refers to the current project).

Most objects are stored in variables for use later in the script. Hard-coding values, rather than using variables, makes scripts inflexible. Take this example, in which the extent of a view is being retrieved:

myView = av.GetActiveDoc
myViewExtent = myView.ReturnExtent

As long as the currently active document is a view, the myViewExtent variable will have its value set. Compare this to:

myViewExtent = av.FindDoc("View 1").ReturnExtent

In the second example, if a view called View 1 does not exist, the script will bail out.

 

You can also examine values of variables in scripts as they run, to make sure that your variables are capturing exactly what you expect. More on this in the section on running scripts.

 

 


Comments in scripts

In order to make your scripts readable, you should always include copious comments. Comments should describe the script name, the overall function of the script, who wrote it and when, and what the individual lines or groups of lines do. Even if your code is tight, it will be considered "amateur" if it is not commented.

Comments always begin with a single quote (') and terminate at the end of the line.

' this is a comment

Here is an example of the Hello World! script properly commented.

 

 


Compiling scripts

After a script has been written, it must be compiled. Compiling converts the Avenue script to machine-code. A script cannot be run if it has not been compiled.

Use the Compile button to compile the script.

Certain syntactical errors in the script, will prevent compilation. For example, I will leave out a necessary argument from the Hello World! script:

 

The script does not compile, and gives a syntax error.

 

If I make another blunder, such as not including the entire 2nd argument (including the comma), a runtime error results:

 

After the script compiles, the Compile button will be grayed out. As soon as an edit is made to the script, however, the script must be re-compiled before it can be run.

 

 


Running scripts

Running

Scripts that have been compiled are run in one of two ways. If a script has been associated with a GUI control (i.e, a menu choice, button, or tool), the script is run by using that control. If a script has not been associated with a GUI control, it is run simply by clicking the Run button .

When the correct and compiled Hello World! script is run by clicking the Run button, a message box pops up:

 

 

Stepping through

Scripts can also be run object-and-request, step-by-step. Instead of clicking the Run button, click the Step button . This will run only a single bit of code at a time. As you step through the script, you will see the requests and objects highlighted sequentially in black.

 

If there is a runtime error in the script, you can usually find it by stepping through the script. When ArcView 3.x reports the error, you will be at the line containing the error, which you can then fix.

 

Toggling breakpoint

If you have a long script, and your error is more than a few lines into the script, you can create breakpoints. Breakpoints interrupt the normal operation of the script so that you can step through the script or examine variables. When a breakpoint is no longer needed, you can clear it. Breakpoints are added by moving the cursor to the breakpoint position and then clicking the Breakpoint button .

When a breakpoint has been set, the object or request will appear in a yellow highlight. When the script is run, it will interrupt at the breakpoint.

 

You can either step through the rest of the script or click the Run button to continue normal operation of the script.

 

Examining variables

One of the most frequent causes of problems in scripts is variables that have been assigned incorrect or null values. It is possible to examine the values of variables as the script progresses. In order to do this, however, the script must be run in a stepwise fashion or with a breakpoint.

Consider this script:

' hello_world.ave
' Tells the user "Hello World!"
' by Phil Hurvitz
' <mailto:phurvitz@u.washington.edu>
' 2000.04.18

' Tell the user "Hello World!"
MsgBox.Info( "Hello World!", "" )

' Ask the user: is Avenue easy?
aveEasy = MsgBox.YesNo("Is Avenue Easy?","Well?",true)

' Parse the answer
if (aveEasy = true) then
howEasy = " "
else
howEasy = " not "
end

'report back
MsgBox.Info("You say Avenue is"+howEasy+"easy.", "Really!")

 

There are 2 variables, aveEasy and howEasy. If a breakpoint is set at the Info request on line 17, and the script is run, it will interrupt before terminating. At that time, if the Examine Variables button () is clicked, a popup will show the variables as well as their values:

 

The values will be dependent on user input while the script was running. If the values are not what you expect, you can edit your code to fix errors.

 

 


Customizing the ArcView 3.x GUI

When you use ArcView 3.x, the scripts you run are generally accessed through the GUI. You can easily alter the GUI to add new menus, buttons, and tools. Double-clicking on the GUI opens it for editing.

Each GUI control, for each document type, has properties that can be set in the GUI customization dialog. Here is the Customize GUI dialog for menus in the Script GUI.

 

And the Buttons for the Script GUI:

 

As you can see, there are several properties for each control. Look at help for Customize dialog box to see what each property does.

In this example, I have added the Hello World! script (renamed in the project to View.Hello_World) to the View GUI. The Click property specifies that the View.Hello_World script will run when the button is clicked. The Help property sets both the little yellow popup tip as well as the status bar when the pointer is placed over the button. The Icon property sets the design of the button.

 

Scripts are generally run to act upon certain objects. For example, returning the Display Extent is performed on a View and not on a Table, therefore a script to return Display Extent would be associated with a View GUI button or menu choice rather than a Table GUI button or menu choice.

 


Creating ArcView 3.x extensions

If you create a series of scripts that you will run again and again in a variety of projects, or if you create an application that uses several scripts in tandem, you can create an extension.

Several variations on the Extension Builder extension exist. You can try them all and decide which you like the best. They are all available on the ArcScripts web page.

Once your extension is created, it can be loaded like any other extension. Extensions that are not "officially" supported by ESRI are generally placed in the Samples Extension directory (usually C:\esri\av_gis30\ArcView 3.x \samples\ext). Extensions in this directory can be accessed using the Samples Browser extension.

 


Finding/stealing/begging scripts

Probably the best way of learning Avenue is to read code that has already been written by people who know what they are doing.

 

System scripts

All the scripts that "run" ArcView 3.x are available for your perusal. System scripts can be loaded into the currently open and active script by clicking the Load System Script button . Before loading a system script, however, you will need to know what script to load. You find this out by customizing the GUI and looking at the click property for the GUI control of interest.

When an extension is loaded, as long as the scripts are not encrypted, the scripts are available as system scripts, along with the core ArcView 3.x scripts.

For example, I am interested in how Xtools calculates area, perimeter, and length for themes. Opening the View GUI, and examining the Xtools menu shows that the script for Updating feature geometry metrics is called aanView.CalculateFeatureSize.

 

This can be read into a new script document by clicking the Load System Script button, and then selecting aanView.CalculateFeatureSize.

 

 

The script can then be viewed or altered.

 

ArcScripts

Even if you are not interested in creating Avenue programs, you will probably find the need to load and run scripts or extensions that are available from other sources. The single best place to find scripts is the ArcScripts page (URL: http://gis.esri.com/arcscripts/scripts.cfm) on the ESRI web site. ArcScripts has a very large collection of scripts for all ESRI software applications, and the greatest number of entries is Avenue scripts. Most of the scripts have been submitted by users from around the world, who are glad to have their work appreciated by others. The ArcScripts are searchable by a number of different methods.

 

Here is what was returned on 2000.04.18 by the above query (Search by keywords "extension builder"):

 

You can download any of the scripts, projects, applications, or extensions on the ArcScripts page.

 

ESRI listserv ArcView 3.x -L

If you have problems writing scripts and need help and advice, the single best place to go is the ArcView 3.x -L listserv, managed by ESRI. Sign up by visiting the ESRI E-mail Discussion Lists page (URL: http://www.esri.com/usersupport/support/selfhelp/lists-sub.html). The list has dozens of messages a day either asking for help, providing advice, or summarizing solutions to problems. A helpful and supportive user community is one of the strongest features about the GIS profession.


Syllabus Schedule Class Meetings Assignments Course Data Internet Search

Current Grades

Contact Us CFR 590 Internet-only section Lab Locations  

 

The University of Washington Spatial Technology, GIS, and Remote Sensing Page is provided by the College of Forest Resources and the College of Ocean and Fisheries Sciences through Unit-Specific UIF. Site administrator: Phil Hurvitz.
Copyright © Phil Hurvitz, 1998-2003