Programming with Avenue - PowerPoint PPT Presentation

1 / 77
About This Presentation
Title:

Programming with Avenue

Description:

Custom ArcView applications are created with AvenueTM scripts. Scripts are written, compiled, tested and ... An apostrophe indicates a comment in Avenue. ... – PowerPoint PPT presentation

Number of Views:64
Avg rating:3.0/5.0
Slides: 78
Provided by: ELM
Category:

less

Transcript and Presenter's Notes

Title: Programming with Avenue


1
Programming with Avenue
  • CEEN 514
  • Civil Engineering Application of GIS
  • BYU

2
Object model diagram
3
Object model diagram
4
Avenue scripts
  • An object-oriented programming language
  • Custom ArcView applications are created with
    AvenueTM scripts.
  • Scripts are written, compiled, tested and
    debugged from the ArcView Script Editor document.

5
Script Editor
  • ArcView document used to write, compile, run,
    debug, and edit scripts
  • Like other documents, the Script Editor has a
    unique set of menus, and buttons

6
Creating your first script
  • Step 1 Enter the code into the script window
  • Step 2 Compile the script
  • Step 3 Test and debug as necessary
  • Step 4 Save the script

7
Typing and Loading
  • Start writing Avenue code by typing directly into
    a Script window.
  • Its a good habit to add comments in your code.
    An apostrophe indicates a comment in Avenue.
  • You can also use a text editor to type your
    Avenue code into an ASCII text file, then load
    the text file into a Script window.

8
Compiling, Testing, Debugging, and Saving
  • Use Compile button to compile the script
  • Use Run button to execute and test the script
  • The Examine Variables button displays a
    window showing the variable type, name, class and
    value
  • Scripts can be saved with your current project.
    You can also save scripts as text files to store
    them separately

9
Customizing the user interface
  • The customize dialog box is the visual interface
    you use to customize the documents GUIs.
  • The individual menu choices, buttons, and tools
    are called controls.
  • With the Customize dialog box you can move, add,
    delete or change properties of the controls.

10
The Customize dialog box
  • To open the Customize dialog box, choose
    Customize in the Project menu, OR
  • Double-click on any blank space on the button or
    tool bar

11
Assigning a script to a control
  • In the Customize dialog box, use the Properties
    List to assign a script to a control.
  • Double-click an event property to display the
    Script Manager, and choose the script that will
    execute when the event occurs.
  • The Script Manager is a dialog box listing all
    scripts that are currently available in the
    project.
  • Menu choices, buttons, and tools all have a Click
    property. Tools also have an Apply property.

12
Objects, classes, and requests
  • An object is a package of properties and requests
  • Objects are elements you work with in ArcView,
    such as views, themes, GUIs controls, and
    graphics.
  • Objects can also be things like number, strings,
    and dates.

13
Classes how objects are defined
  • A class is a template for building objects
  • Each class defines a unique set of requests and
    properties
  • Instances are objects created from the same class
    all share the same request and properties
  • View class View1, MyView are instances

14
Requests
  • Create objects, retrieve objects, and check or
    change properties
  • Create new objects (class requests)
  • myView View.Make
  • Retrieve Objects (instance requests)
  • myView aProject.FindDoc(View1)
  • Check and change property values
  • myView.SetName(Atlanta)

15
Avenue online help
  • The Avenue online Help system gives detailed
    explanations of classes and requests.

16
Constructing Avenue Statements
  • Send a request to an object
  • General statement Object.RequestRequest
  • av.GetProject.FindDoc(View1).FindTheme(City)
  • Requests can require parameters
  • myView.SetName(Atlanta)
  • Statement with numbers Object Request Object
  • 15 20
  • theValue 100

17
Assignment statements
  • A request sent to an object returns another
    object
  • Assign a variable to the returned object
  • aTheme av.GetProject.FindDoc(World).FindTheme
    (GNP)
  • Assign variables directly
  • CompanyTitle Environmental Modeling Research
    Laboratory
  • aConstant 1.5
  • Assign local or global variables
  • myLocalVar 25 23
  • _myGlobalVar myValue 37

18
Variable rules
  • Not declared or typed
  • Must begin with a letter or underscore
  • Not case-sensitive
  • Avenue compiler finds conflicts with reserved
    names
  • View av.GetProject.FindDoc(Redlands)
  • A local variable is available only in the script
    in which it is assigned
  • Global variable names are preceded by an
    underscore (_) and are available to all scripts

19
Using strings
  • Concatenate strings
  • theNewString Good MorningMichael
  • Good Morning Michael
  • theNewString Good MorningMichael
  • Good MorningMichael
  • Newline, tab, and carriage return NL, TAB, and
    CR
  • theNewString Good MorningNLMichael
  • Good Morning
  • Michael
  • String conversions
  • myNumber string1.AsNumber
  • newDate myString.AsDate

20
Using numbers
  • Numeric evaluation is left to right, unless
    parentheses are used
  • x 2 3 3 x 15
  • x 2 (3 3) x 11
  • Mathematical operations
  • wholeNumber 35.689.Round returns 36
  • Number formatting
  • myNumber.SetFormat(d.dd)
  • Number conversions
  • numRadians 90.AsRadians
  • numDegrees numRadians.AsDegrees

21
Example Avenue script
  • Start ArcView and open example project
  • Open script Example01
  • Compile and run the script
  • Examine variables during script execution
  • Save the script to a text file
  • Modify the View menu and button bar
  • Assign the script to a control
  • Save and close the project

22
Working with a View
  • Creating a new view
  • aView View.Make
  • aView.SetName(aName)
  • aView.GetWin.Open
  • Getting a view
  • aView av.GetActiveDoc
  • aView av.GetProject.FindDoc(aViewName)

23
Adding a theme to a view
  • Create a data source name
  • from a shapefile
  • dataSrc SrcName.Make(c\avdata\streets.shp)
  • Create the theme
  • theTheme Theme.Make(dataSrc)
  • Add the theme to a view
  • theView av.GetProject.FindDoc(Redlands)
  • theView.AddTheme(theTheme)

24
Working with a theme
  • Getting a theme from a view
  • aView av.GetProject.FindDoc(aViewName)
  • aTheme aView.FindTheme(aThemeName)
  • Making a theme visible
  • aTheme.SetVisible(TRUE)
  • Changing the name of a theme
  • aTheme.SetName(aThemeName)

25
Example VwThm01
  • creates a view
  • add themes to a view

26
Example VwThm02
  • changes the name of a view
  • changes the name of a theme

27
Working with tables
  • The role of virtual tables (VTabs)
  • How a table and a Vtab are associated
  • Getting a Vtab or an Ftab
  • Modifying fields and field values
  • Using a bitmap for managing selections

28
Table and VTab classes
  • A Table is a type of document for viewing tabular
    data
  • Each table has an associated virtual table (VTab)
  • A VTab manages the data sources
  • supports the viewing of multiple data sources
    as one
  • Each VTab is composed of zero or many fields
  • The VTab has a selection bitmap
  • FTab supports tables that are associated with
    themes. FTab is a type of VTab, and therefore
    performs the same operations

29
Creating a table from an existing data source
  • Convert the pathname to a FileName
  • theFN c\data\offices.txt.AsFileName
  • Create a VTab and a Table
  • Vtab.Make(aFile, isWritable,skipRecord)
  • theVTab Vtab.Make(theFN, fasle,false)
  • myTable Table.Make(theVTab)
  • Get the table window and open it
  • myTable.GetWin.Open

30
Getting a Vtab or an Ftab
  • Getting a Vtab from a table
  • aTable av.GetProject.FindDoc(aTableName)
  • aVtab aTable.GetVtab
  • Getting an Ftab from a theme
  • aView av.GetProject.FindDoc(aViewName)
  • aTheme aView.FindTheme(aThemeName)
  • anFtab aTheme.GetFtab

31
Working with fields
  • Returning fields
  • fieldList theVTab.GetFields
  • Getting a field
  • firstField fieldList.Get(0)
  • myField theVTab.GetActiveField
  • myField theVTab. FindField(City)
  • Getting the field type
  • theFieldType myField.GetType
  • Setting an alias name or visibility
  • myField.SetAlias(Zoning Code)
  • myField.SetVisible(True)

32
Updating record values
  • Get the Vtab and set it editable
  • theVTab av.getProjectFindDoc(Table1).GetVtab
  • theVTab.SetEditable(True)
  • Get the field and update its value for a record
  • nameField theVTab.FindField(Name)
  • idField theVTab.FindField(ID)
  • theValue theVTab.ReturnValue(idField, 3)
  • theVTab.SetValue(nameField, 3, newName)
  • Update the document
  • theVTab.Refresh

33
Calculating field values
  • Get the Vtab and the field to calculate
  • theVTab av.GetActiveDoc.GetVTab
  • theField theVTab.FiendField(Total_Tax)
  • Create an expression
  • theExpression FieldName1 FieldName2
  • Calculate the new field value
  • theVTab.Calculate(theExpression, theField)
  • theVTab.Refresh

34
Creating a selection bitmap
  • A selection bitmap flags each record as selected
    or not selected
  • theBitmap aVtab.GetSelection
  • theBitmap anFtab.GetSelection
  • Clearing selection
  • theBitmap.ClearAll
  • Getting counts on the bitmap
  • totalBits theBitMap.GetSize
  • selBits theBitMap.Count

35
Processing records
  • Process all records in the Vtab
  • for each rec in aVTab
  • do something
  • end
  • Process all selected records in the bitmap
  • for each rec in aVTab.GetSelection
  • do something
  • end

36
Summing selected records
  • Get the VTab, the field, and the bitmap
  • theVTab theTable.GetVTab
  • theField theVTab.FindField(Area)
  • theBitMap theVTab.GetSelection
  • Sum the area for the selected records
  • sum 0
  • for each rec in theBitMap
  • val theVTab.ReturnValue(theField, rec)
  • sum sum val
  • end
  • Report the results
  • MsgBox.Info(Total area issum.AsString,Stat
    istics)

37
Example TblVtab01
  • Find a table
  • Get the Vtab
  • Find a field
  • Calculate field values

38
More work with tables
  • Summarizing a table (Vtab or Ftab)
  • Joining two tables (Vtab or Ftab)
  • Creating a new database file and a table
  • Querying a table (Vtab or Ftab)

39
Summarizing a table (Vtab or Ftab)
  • Get the Vtab/Ftab and a field
  • aFtab aTheme.GetFtab
  • stateFld aFtab.FindField(StateName)
  • Summarize area and create a new database file
  • aFTab.Summarize (aFileName, aClass, aField,
    Fields, Summarys)
  • aFTab.Summarize( States".AsFileName, dbase,
    stateFld,
  • areaFld,VTAB_SUMMARY_SUM )
  • To count the number of occurrence only
  • aFTab.Summarize( States".AsFileName, dbase,
    stateFld, ,)

40
Joining two tables (Vtab/Ftab)
  • Get both tables
  • aFTab aTheme.GetFtab
  • aVtab aTable.GetVtab
  • Find fields
  • fldFtab aFtab.FindField(fldFtabName)
  • fldVtab aVtab.FindField(fldVtabName)
  • Joining two tables
  • aFTab.Join (aToField, aFromVTab, aFromField)
  • aFTab.Join (fldFtab, aVTab, fldVtab)
  • Removing join
  • aFTab.UnjoinAll

41
Creating a new database and table
  • Create source file and table
  • theVTab VTab.makeNew(emplyees.dbf.AsFileName
    ,dbase)
  • myTable Table.make(theVTab)
  • Add two fields
  • field1 Field.Make(ID,FIELD_SHORT,5,0)
  • field2 Field.Make(Name,FIELD_CHAR,10,0)
  • theVTab.AddFields(field1, field2)
  • Add ten records and set initial values
  • for each i in 1..10
  • rec theVTab.AddRecord
  • theVTab.SetValue(field1, rec, i)
  • end

42
Querying a table (Vtab/Ftab)
  • Getting the Ftab and the Bitmap
  • aFTab aTheme.GetFtab
  • aBitMap aFtab.GetSelection
  • Writing an query expression
  • theQuery StateNameaStateName.Quote
  • Querying and updating selection
  • aFtab.Query(theQuery, aBitMap,
    VTAB_SELTYPE_NEW
  • aFtab.UpdateSelection

43
Example Summarize01
  • Summarize a shapefile based on a field and thus
    create a new dbase file
  • Create a table from the new dbase file

44
Example GetInfo01
  • Join a Vtab with an Ftab
  • Calculate a field in Ftab using joined fields

45
Example CrtDbfQuery01
  • Create a new dbase file and add fields
  • Get information from a Ftab
  • Query another Ftab and find totals
  • Add records in the new dbase file and set values
    in the fields.

46
Dialog Designer
  • CEEN 514
  • Civil Engineering Application of GIS
  • BYU

47
What is a dialog
  • A dialog is another user interface tool you can
    use to build applications in ArcView
  • Create your own input forms
  • Organize related tools in separate windows
  • Link whats displayed in a dialog directly to
    your data
  • Add buttons directly to a View or Layout

48
How does the Dialog Designer work?
  • Load Dialog Designer extension into ArcView. You
    will notice a new document in the project window.
  • Double click the icon and youll create a new
    dialog editor document.
  • All of the interface components, controls, you
    can add to your dialogs are located on the tool
    bar.
  • Once you have added controls to the dialog,
    youll write scripts that make it work. Then,
    youll attach your dialog to ArcViews interface
    so people can use it

49
Dialog Designer document
50
Creating a dialog and adding a control to it
  • Load the Dialog Designer extension
  • Start ArcView and bring up the Extension Manager
    dialog. Check Dialog Designer and press OK.
  • Create a dialog document
  • Double-click on the Dialogicon in the
    project window
  • Add controls to a dialog
  • Select a control button from the tool bar, then
    click and drag out a rectangle on the dialog
    inside the dialog frame

51
Creating a dialog and adding a control to it
(contd)
  • Set the control properties
  • Display the label buttons properties by
    double-clicking it on the dialog. In the Name
    field, type in the name. To change the label,
    double-click on the Label property and type in.

52
Creating a dialog and adding a control to it
(contd)
  • Attach a script to the control
  • Locate the Click property and double-click to
    display the script manager dialog. Select the
    script and press OK.

53
Creating a dialog and adding a control to it
(contd)
  • Compile and run the dialog
  • Make the dialog the active document
  • Press the Compile button
  • Press the Run button

54
Attach the dialog to the interface
  • At this point, you can run the dialog by
    pressing the Run button. But, youll likely want
    to attach the dialogs you create to ArcViews
    interface.
  • Create the following script and attach to a
    button on the ArcViews button bar.
  • aDialogName.Run
  • Runs the dialog.
  • Av.GetProject.FindDialog(aDialogName).Open
  • If your dialogs are loaded from an extension, use
    following request.
  • Av. FindDialog(aDialogName).Open

55
Whats next?
  • Depending on your application, there are several
    ways to distribute your custom dialogs.
  • You can save those in a project.
  • If youll be using your dialogs with different
    projects or you plan to distribute them to
    others, you can create an ArcView extension.

56
Example Dialog Designer
  • Load the Dialog Designer extension
  • Create a dialog
  • Add controls
  • Change properties of the control
  • Write script and attach to the control
  • Compile and run
  • Attach dialog to the interface

57
Advance Avenue
  • CEEN 514
  • Civil Engineering Application of GIS
  • BYU

58
The Application Framework
  • When ArcView starts, the application is created
  • Reference the application with the keyword av
  • A one-to-one association exists between
    application and project
  • av.GetProject
  • Multiple associations between Project and Doc,
    Project and DocGUI
  • av.GetProject.FindDoc(World)
  • A one-to-one association exists between Doc and
    DocWin, Doc and DocGUI
  • av.GetProject.FindDoc(World).GetWin

59
Working with the application
  • Window properties
  • av.MoveTo(20,20)
  • av.Resize(800,600)
  • av.TileWindows
  • Active document and window
  • theActiveDoc av.GetActiveDoc
  • theActiveWin av.GetActiveWin

60
Controlling the status bar
  • Display a message in the status bar
  • av.ShowMsg(Creating themes)
  • av.ClearMsg
  • Move the progress bar or stop progress
  • for each i in 0..100
  • av.SetStatus(i)
  • end
  • av.ClearStatus

61
Executing a script from another script
  • Use the application with the Run request
  • Pass any object into the called script
  • In the called script, use SELF to refer to the
    passed object
  • Use the Return statement to send an object back

62
Working with the project
  • Create, name, and save a project
  • theProject Project.Make
  • theProject.SetFileName(world.apr.AsFileName)
  • theProject.Save
  • Open and close a project
  • theProject Project.Open(world.apr.AsFileName)
  • theProject.Close
  • Manage documents in the project
  • theView theProject.FindDoc(Asia)
  • theProject.SetActive(theView)
  • theProject.RemoveDoc(theView)

63
Example Application and Project
  • Start ArcView and open a project
  • Modify the application window
  • Customize the status bar
  • Change the name of a project
  • Write a script that runs another script
  • Manage documents in a project

64
Branching and looping
  • Branching statements
  • IfThenElse
  • Boolean expressions
  • Check a condition for true or false
  • Looping statements
  • For Each
  • While
  • Collections
  • Lists
  • Dictionaries

65
IfThenElse statement
  • Conditional branching
  • if (Boolean expression) then
  • do something
  • else
  • do something else
  • end
  • Example verify a password
  • thePWord MsgBox.Password
  • if (thePWord cheddar) then
  • av.GetProject.FindDoc(LA Banks).GetWin.Open
  • else
  • MsgBox.Info(Sorry, incorrect
    password,Password)
  • end

66
Boolean objects and expressions
  • There are two Boolean objects true and false
  • Assign directly or return from a request
  • done false
  • status theTheme.IsVisible
  • Place Boolean expressions in parentheses
  • (yourNumber gt 36)
  • Create compound expressions with and,or, or not
  • ((yourResponse yes) and (yourNumber gt 36))

67
While statement
  • Performs a set of requests while a condition is
    true, the loop stops when a condition is false
  • While (condition)
  • some request here
  • condition must change
  • end
  • Example sum a set of numbers
  • While (start)
  • if (thevalue ltgt nil) then
  • sum sum theValue.AsNumber
  • else
  • start false
  • end
  • end

68
For Each statement
  • Applies a set of requests to each object in a
    collection
  • for each anObject in aCollection
  • request block
  • end
  • Turn on each theme in the view
  • theThemeList theView.GetThemes
  • for each thm in theThemeList
  • thm.SetVisible(true)
  • end
  • Intervals and increments
  • for each num in 0..10 by 2
  • sum sum num
  • end

69
Lists
  • A group or collection of objects
  • Create a list
  • myList theCompany, Phone, 157.50
  • theThemeList theView.GetThemes
  • newList List.Make
  • Add objects to a list
  • theNames.Insert(City)
  • theNames.Add(Streets)
  • Reference objects in a list
  • myList theCompany, Phone, 157.50
  • theName myList.Get(0)
  • myList.Remove(2)

70
Dictionaries
  • A dictionary is a collection of unique key and
    value pairs
  • KEY VALUE "Utah" "Provo", "Orem", "Salt lake
    City
  • Create and add elements
  • usStateCity Dictionary.make(52)
  • usStateCity.Add(Utah, Provo,Orem,Salt
    Lake City)
  • Retrieve dictionary entries
  • theStateList usStateCity.ReturnKeys
  • UtahCities usStateCity.Get(Utah)

71
Continue, Break, and Exit
  • Alters the normal flow control
  • Continue skips the current iteration of the loop
  • Break causes immediate exit from the loop
  • Exit immediately terminates the script

for each num in 1..4 if (num 3) then
continue end sum sum num end sum 7
for each num in 1..4 if (num 3) then
break end sum sum num end sum 3
72
Example Branching and looping
  • Creating and displaying list of themes
  • Toggle theme visibility
  • Design a customer data input form
  • Use a dictionary to store customer data

73
Themes and legends
  • Setting theme symbology
  • Theme selection
  • Theme analysis

74
Setting theme symbology
  • Get a list of legend symbols
  • theLegend theTheme.GetLegend
  • theLegendSyms theLegend.getSymbols
  • Get the Symbol window and the Color Palette
  • thePalette av.GetSymbolWin.GetPalette
  • theColors thePalette.GetList(PALETTE_LIST_COL
    OR)
  • Set symbol color
  • firstSym theLegendSyms.Get(0)
  • firstSym.SetColor(theColors.Get(4))
  • Redraw legend
  • theTheme.InvalidateLegend
  • Turn the theme on
  • theTheme.SetVisible(true)

75
Shuffling themes in the TOC
  • Get the view and theme list
  • theView av.GetActiveDoc
  • theThemeList theView.GetThemes
  • Shuffle the first theme to the bottom
  • theTheme theThemeList.Get(0)
  • theThemeList.Shuffle(theTheme,theThemeList.Count)
  • Redraw all theme
  • theView.InvalidateTOC(nil)

76
Selecting theme features
  • Get the feature table and the feature selection
  • theView av.getActiveDoc
  • theTheme theView.FindTheme(nwtracts)
  • theFTab theTheme.GetFtab
  • theSelection theFTab.GetSelection
  • Create a logical expression
  • theExpression (med_hh_in gt 60000)
  • theFTab.Query(theExpression, theSelection,
  • VTAB_SELTYPE_NEW)
  • Highlight the new selection in the view and table
  • theFTab.UpdateSelection

77
Example Themes and Legends
  • Create a theme from a shapefile
  • Change the symbols
  • Shuffle the themes
  • Build an expression to select features
Write a Comment
User Comments (0)
About PowerShow.com