' Title: Sums the AvgGPM of the selected point within a thiessen polygon
' Topic: View, Tables
' Description: This program does a theme on theme selection,
' sum the avg_gpm of the select group then writes
' the total to a field in the junction shapefile table, for the selected record.
' Requires: Must be associated with button click event.
' Self:
' Results
' **********************************************************

'*** Read list of Junction points.
theTable =av.GetProject.FindDoc("mastertable.dbf")
theVTab = theTable.GetVTab
labelfld = theVTab.FindField("label")
theTable.GetWin.Open

'*** Loops through each record in the list of Junction points

for each record in theVTab
theLabelNo = theVTab.ReturnValueString (labelfld, record)
'MsgBox.Info(theLabelNo,"Is Data Present")
'*********************************************************************

theIdxNo = theLabelNo
theView = av.GetProject.FindDoc("View1") '**** The view name goes here
thePolyTheme = theView.FindTheme("Thiesn1.shp") '**** The Thiessen poly name goes here

theDpy = theView.GetDisplay

if (thePolyTheme.IsActive.not) then
thePolyTheme.SetActive(true)
end
theVTabPoly = thePolyTheme.GetFTab

theBitMapPoly= theVTabPoly.GetSelection
selPoly = theBitMapPoly.GetNextSet (-1)
theField = theVTabPoly.FindField ("Label")
thePoint = theVTabPoly.ReturnValueString (theField, selPoly)

'For testing return variable values
'MsgBox.Info (theIdxNo, "Run Time Message 1")
'MsgBox.Info (selPoly.AsString, "Run Time Message 2")
'MsgBox.Info (theField.AsString, "Run Time Message 3")
'MsgBox.Info (thePoint, "Run Time Message 4")

aQstr = "[Label] ="++theIdxNO.Quote

'MsgBox.Info (aQstr, "Run Time query result Message 5") 'For testing return variable values

theVTabPoly.Query (aQstr, theBitMapPoly, #VTAB_SELTYPE_NEW)
theVTabPoly.SetSelection (theBitMapPoly)
'exit ' Remove comment when testing to prevent endless loop
'******************************************************************

if (thePolyTheme.IsActive) then
thePolyTheme.SetActive(false)
end

theNodeTheme = theView.FindTheme("Address.shp") 'The geo-code address shapefile goes here

theDpy = theView.GetDisplay

if (theNodeTheme.IsActive.not) then
theNodetheme.SetActive(true)
end

theVTabNode = theNodeTheme.GetFTab
theBitMapNode = theVTabNode.GetSelection
'*******************************************************************

distance = Units.Convert(1, #UNITS_LINEAR_FEET, #UNITS_LINEAR_FEET)

' Select all points within the selected thiessen polygon

theNodeTheme.SelectByTheme(thePolyTheme, #FTAB_RELTYPE_HASCENTERWITHIN, distance, #VTAB_SELTYPE_NEW)
theNodeFtab = theNodeTheme.GetFtab
theVTabNodeSel = theNodeFTab.GetSelection
if ( theVTabNodeSel.IsEmpty) then
end

if ( theVTabNodeSel.IsEmpty.Not) then
theTable = av.GetProject.FindDoc("Attributes of Address.shp")
theVTab = theTable.GetVTab
theGpmField = theVTab.FindField ("Avg_gpm")

'*** Sums up all the selected AvgGPM within the Thiessen polygon

sum = 0
for each rec in theVTab.GetSelection
val = theVTab.ReturnValue(theGpmField, rec)
'MsgBox.Info(val.AsString, "Rpt-1 ") ' For testing the variable result
sum = sum + val
'MsgBox.Info(sum.AsString, "Rpt-2") ' For testing the variable result
end

'*** Populate the Demand field in the Junction shapefile

theJunctionTable = av.GetProject.FindDoc("Attributes of Junction.shp")
theJunctionVTab = theJunctionTable.GetVTab
theLabelFld = theJunctionVTab.FindField("Label")
theBitmap = thejunctionVTab.GetSelection

aQstr = "[Label] ="++theIdxNO.Quote

'MsgBox.Info(aQstr, "Rpt5") ' For testing the variable result
theJunctionVTab.Query (aQstr, theBitMap, #VTAB_SELTYPE_NEW)
theJunctionTable.PromoteSelection
theJunctionVTab.Calculate(sum.AsString, theJunctionVTab.FindField("Demand2"))
end

theTable =av.GetProject.FindDoc("mastertable.dbf")
theVTab = theTable.GetVTab
labelfld = theVTab.FindField("label")
end