Basic ArcPad Programming

advertisement
Basic Programming for ArcPad Application Builder
By
Kenneth Smith
Table of Contents
Page
Introduction…………………………………………………………………………………..1
Object Hierarchy……………………………………………………………………………..1
Subroutines…………………………………………………………………………………..1
Variables……………………………………………………………………………………..2
Determining the Filepath of a Shapefile……………………………………………………..2
Recording Military Time…………………………………………………………………….4
Recording GPS Position to a Field…………………………………………………………..4
Writing to a Separate File……………………………………………………………………4
References…………………………………………………………………………………...5
Basic ArcPad Programming
Introduction
November 4, 2004
Before you begin programming in ArcPad, it is recommended that you first be familiar with the
program itself. This is especially true if you are not very familiar with ArcView/ArcGis. The book Using
ArcPad (Clarke et al 2002) is a good start. Follow the tutorials in the book and use the sample data that
comes with ArcPad. Once you are familiar with the various features of ArcPad (look through the book and
try everything out), start with the tutorials for ArcPad Application Builder (located in the help menu). This
is the best way to get acquainted with how the customization process is structured. After you go through
this, the information presented here will make more sense. The Customizing ArcPad Help is useful --- if
you know what you are looking for. The best way to get what you want is to type in a key word and search
for it. The more you use the help, the more familiar you will be with it and you will be able to find what
you want quicker. Another great resource is the ArcPad users forum located at ESRI’s website
(http://forums.esri.com/Forums.asp?c=34) . Get an account here and you can post any questions you might
have. Again, you can enter a key term you are looking for and browse through old postings as well.
Object Hierarchy
The object hierarchy in ArcPad is structured as the following:
November 1, 2004
Layers(“lyrLayer”).Forms(“frmForm”).Pages(“pgPage”).Controls(“txtEditbox”)
Or
Applets(“aplApplet”).Forms(“frmForm”).Pages(“pgPage”).Controls(“txtEditbox”)
Taking the object out this far would be to reference a control such as a text box (as shown), a combo box, a
label, or a check box. Sometimes only the page or form need to be referenced. Variables can be used to
store the value of an object, and thus make them easier to work with.
Dim MyPage, theEditBox ‘declares the variable
Set MyPage = Layers(“lyrMyLayer”).Forms(“EDITFORM”).Pages(“pgFirstPage”) ‘page is referenced
Set theEditBox = MyPage.Controls(“txtTextBox”)
‘text box is referenced
theEditBox.Value = “DIRS” ‘sets the value of the text box to a string
Using the following would have the same result:
Layers(“lyrMyLayer”).Forms(“EDITFORM”).Pages(“pgFirstPage”).Controls(“txtTextBox”).Value = “DIRS”
When referencing an object from an event handler, the ThisEvent.Object property can be used to reference the
object that is calling the action.
ThisEvent.Object.Value = 5
This would be the same as
Layers(“lyrLayer”).Forms(“frmForm”).Pages(“pgPage”).Controls(“txtEditbox”).Value = 5
To reference a level above the current one, you can use the Parent property of the object.
Set MyPage = ThisEvent.Object.Parent
This would reference the active page if being called from a control on the page.
Set MyForm = MyPage.Parent
This would reference the form.
Subroutines
October 26, 2004
Subroutines are pieces of code in the VBScript file that can be called to do a specific operation.
Generally they are called from either an event handler on one of the Forms or from another subroutine. Each
subroutine must begin with
Sub MyName (where MyName is the name of the subroutine) and then close
with End Sub. All of the code is placed between these two. The following shows where you would call a
subroutine from an event handler of a control on a form.
2
Variables
October 26, 2004
Variables are used to store information such as a string, integer, double, or boolean. They make it
easer to manipulate and use these values without typing them out each time. First each variable must be
declared. To do this you type:
Dim MyVar ‘comments can be added here to describe what the variable will be used for
Dim MyVar, VarTwo, VarThree
‘multiple variables can be declared by placing a comma between them
MyVar = 1
VarTwo = MyVar + 34
‘VarTwo is now equal to 35
Dim strMyString, strString2, String3
strMyString = “peanut butter”
‘gives the string a value
strString2 = strMyString & “ tastes good” ‘ says: peanut butter tastes good
String3 = strString2 & “with even more “ & strMyString ‘says: peanut butter tastes good with even more
peanut butter
Global variables are those that can be used throughout the entire VBScript page. Normally variables
will only work within the subroutine or function that they were created in. At the very top of the VBScript
file, just after the ‘Option Explicit’, all of the global variables can be placed. They are written using the User
Properties property as follows:
Application.UserProperties (“strGlobalVar1”) = 4
‘add comments to describe the variable
Application.UserProperties (“strGlobalVar2”) = “RIT”
Inside of a function, you can set another variable equal to it.
MyVar = Application.UserProperties (“strGlobalVar1”)
‘MyVar is now equal to 4
Or another value can be set equal to a global variable.
MyVar = 89
Application.UserProperties (“strGlobalVar1”) = MyVar
‘global variable is now equal to 89
Determining the Filepath of the Current Shapefile
October 27, 2004
In order to determine the currently editable layer on the map, use the EditLayer property of the Map
object.
Dim Layer
3
Set Layer = Application.map.EditLayer(1)
This references the first editable point layer. The number 1 is for point layers, 2 is for line layers, and 3 is for
polygon layers. To get the filepath of the layer, use the FilePath property of the layer object.
Dim LayerPath
LayerPath = Layer.FilePath
This will reference the filepath of the currently editable layer.
Recording Military Time
October 29, 2004
In order to record the current time in a metadata field, put the following in the onsetfocus event
handler of the chosen textbox.
ThisEvent.Object.Value = FormatDateTime(Time,4)
ThisEvent.Object references the textbox. The FormatDateTime(Time, 4) sets the value to the current time in
military format.
Recording GPS Position to a Field
November 1, 2004
To collect the GPS position into latitude, longitude, and elevation into separate fields, use the
following. In the onsetfocus event handler of the latitude textbox place the following:
ThisEvent.Object.Value = GPS.Latitude
This will record the latitude in the textbox when the user taps it. This is assuming that there is a connection
with the GPS unit. The same can be done for longitude and elevation.
ThisEvent.Object.Value = GPS.Longitude
ThisEvent.Object.Value = GPS.Altitude
Normally ArcPad will store the point location information in XYZ map coordinates on the last tab of the
EDITFORM. This information will not be accessible from the *.dbf file however. By storing the latitude,
longitude, and elevation data in a field, you will be able to access it easily.
Writing to a Separate File
November 3, 2004
For data that needs to be stored, but not in the shapefile, ArcPad will let you write to a separate file,
such as a *.dbf or a *.html. If the file already exists, you can write to it directly. If it doesn’t, then you will
have to create it programmatically and then write to it. To create a file you will use the Application’s
CreateAppObject method. Application is ArcPad’s highest level of object.
First you must declare a variable and then define it as a file object.
Dim myFile
Set myFile = Application.CreateAppObject (“file”)
This creates a file object. Next you must open the file using the complete file path to name it.
myFile.Open (“\StorageCard\Collect\PracticeFile.dbf”, 2)
The number after the file name represents the mode. A mode of 1 is for read only files, 2 is for read/write
files, and 8 is to begin writing at the end of a file. If the file already exists, mode 2 would write over the
existing file. In order to copy information off of one file (a template for instance) to another one, you would
use the ReadByte and WriteByte methods. In this example, objASDtable is a variable name for a file that has
the column header information of a *.dbf file. It is used as a template and copied to SourceDBF, which is a
variable for a blank *.dbf file. Notice how the SourceDBF is opened with a mode of 1 so that it is read-only
and does not get over written. The variables FileName and LayerPath are used to store the filepaths of both
files.
4
objASDtable.Open (FileName + ("_ASD.dbf")),2
objSourceDBF.Open (LayerPath + ("ASD_template.dbf")),1
While Not objSourceDBF.EOF
objASDtable.WriteByte (objSourceDBF.ReadByte)
from template to newly created table
Wend
objSourceDBF.Close
objASDtable.Close
'copies fields
'closes file
'closes file
For more information on using these methods, see the ArcPad Application Builder Help and search for File
Object.
When adding data to an existing *.dbf file, you can do this using the Recordset object type of the Applications
CreateAppObject method. First you must declare a variable and set it as a recordset.
Dim myRS
Set myRS = Application.CreateAppObject (“recordset”)
To open the recordset you must include the complete file path of the *.dbf file and the mode in which it is to be
opened. A mode of 1 is for read-only and 2 is for read/write capabilities.
myRS.Open (“\StorageCard\Collect\ASDinfo.dbf”, 2)
In a recordset, picture a spreadsheet. Each column is known as a field, and each row is a record. To access a
value stored in a record, move to the specific record and use the Fields property.
myRS.Fields(“field1”).Value = “popcorn”
This would change the value in field1 to the word popcorn. To navigate around the record set you can use the
following methods: MoveNext, MovePrevious, MoveFirst, and MoveLast. To add a new record, you can use
the AddNew method. If you want the new record to be added at the end of the file, which you usually would,
remember to use the MoveLast method first.
myRS.MoveLast
myRS.AddNew
myRS.Fields(“field1”).Value = “calibrated”
myRS.Fields(“field2”).Value = “code blue”
After making changes to a record, you must call the Update method to save any changes made to it. If you
don’t, and then move to another record, the changes will not be saved.
myRS.Update
When you are done with the recordset, you can close it by using the Close method.
myRS.Close
References:
Clarke, Shane, Craig Greenwald, and Valerie Spalding. Using ArcPad. ESRI Press: Redlands, CA, 2002.
5
Download