' Name: WMAstep
'
' Title: Steps through each WMA and draws a map
'
' Topics: Views, Layouts
'
' Description: Steps through each Water Management Area and
'              asks the user whether to draw a map using the current
'              layout.
'
' Requires:
' -Locality map (a script that draws a locality
'  map of South Africa)
' -View with the required themes
' -Layout with the required graphics objects already created
'  and tagged with SetObjectTag
'  (to avoid creating new layouts or using the confusing command .FillFrames)
'
' Author: Michael Silberbauer 2000-03-23
' - modified 2001-01-17
' - pies 2001-02-05
' - national assessment 2001-12-11
'-----------------------------------------------------------------------------

av.GetProject.SetModified(true)
av.GetProject.SetSelColor(color.GetGray)
symbol_type = MsgBox.ListAsString({"Maucha","Pie","None"},"Please select the symbol type","Symbol selection")
DocumentList = av.GetProject.GetDocs
TableList = List.Make
for each anItem in DocumentList
  if (anItem.is(table)) then
    TableList.Add(anItem)
  end
end
_InorganicData = MsgBox.ListAsString(TableList,"Select a file with water quality data","DATA TABLES")
if (_InorganicData=nil) then
  MsgBox.Info("No tables available in this project","ERROR: NO TABLE")
  exit
end
_IDSymbol = MsgBox.YesNo("Label symbols with station names ?","WMAstep: ID Symbols",FALSE)
if (symbol_type="None") then
  theView = av.GetProject.FindDoc("View_Landcover_WMA")
  map = "Layout_landcover250_WMA"
  description = MsgBox.ListAsString({"Landcover classes: agriculture, urban and mining","Landcover classes: agriculture (irrigation)","-"},"Please choose a subtitle","WMAstep: subtitle")
end
if (symbol_type="Maucha") then
  theView = av.GetProject.FindDoc("View_PieMaucha_WMA")
  theViewKey = av.GetProject.FindDoc("Key_Maucha")
  theViewList = av.GetProject.FindDoc("Key_Maucha_Scale")
  map = "Layout_Maucha_WMA"
  description = "Salinity (major cations and anions)"+nl+"represented by Maucha symbols"
end
if (symbol_type="Pie") then
  theView = av.GetProject.FindDoc("View_PieMaucha_WMA")
  _UseMissing = MsgBox.YesNo("Use incomplete records (Recommended) ?","JuanitaPie: Incomplete Records",TRUE)
  _LblSymbol = MsgBox.YesNo("Label symbols with variable names (usually for key) ?","JuanitaPie: Label Symbols",FALSE)
  UseTypeList = ({"Domestic_Health","Domestic_Salts","Irrigation"})
  _UseType = MsgBox.ListAsString(UseTypeList,"Please choose a use type","Juanita Pie: Choose use type")
  _ValType = "Val_info_mn" ' Code for looking up median cutpoint data in classcodes.txt
  theViewKey = av.GetProject.FindDoc("Key_"+_UseType)
  theViewList = av.GetProject.FindDoc("Key_for_"+_UseType)
  map = "Layout_Pie_WMA"
  if (_UseType="Domestic_Health") then
    description = "Water quality suitability for domestic use (health)"+nl+"represented by pie symbols"
  end
  if (_UseType="Domestic_Salts") then
    description = "Water quality suitability for domestic use (salinity)"+nl+"represented by pie symbols"
  end
  if (_UseType="Irrigation") then
    description = "Water quality suitability for agriculture (irrigation)"+nl+"represented by pie symbols"
  end
end
_IDtype = "station"
if (_IDSymbol) then
  _IDtype = MsgBox.ListAsString({"num_nat","number","station"},"National number, full number or station?","Juanita Pie: Choose labelling")
end
wmaTheme = theView.FindTheme("Water Management Area")
wmaFTab = wmaTheme.GetFTab
wmaField = wmaFTab.FindField("Name")
myLayout = av.GetProject.FindDoc(map)
myLayout.GetWin.Open
theViewLoc = av.GetProject.FindDoc("Locality_WMAmap")
myTheme = theViewLoc.FindTheme("Water Management Area")
myLegend = myTheme.GetLegend
if (myTheme.HasTable) then
  myFTab = myTheme.GetFTab
  theField = myFTab.FindField("Name")
else
  MsgBox.Info(myTheme.asString,"No table - exit")
  exit
end
output_type = MsgBox.ListAsString({"image on disk","print to default printer","no idea"}, "Please select the output type","Output Selection")
_redraw_sym = MsgBox.YesNo("Redraw all SA symbols for every map (slow!)?","WMA SYMBOLS",FALSE)
auto_mode = MsgBox.YesNo("Run in automatic mode?","AUTO MODE",FALSE)
num = 0
for each rec in myFTab
  num = num+1
end
myTheme.ClearSelection
av.ShowStopButton
for each rec in myFTab
  wma = myFTab.ReturnValue(theField,rec)
  if (wma.asString.Count>0) then
    if (auto_mode) then
      Please_print = true
    else
      Please_print = MsgBox.YesNoCancel("Process"++wma.asString++"?","Water Management Area Map",TRUE)
    end
    _WMAname = wma.UCase
    if (Please_print=nil) then
      exit
    end
    if (Please_print) then
      myTheme.ClearSelection
      myFTab.SetDefinition("([Name] = wma)")
      myFTab.UpdateDefBitmap
      myFTab.GetSelection.Set(rec)
      myTheme.Invalidate(true)
      theNumField = myFTab.FindField("Number")
      wmanumber = myFTab.ReturnValue(theNumField,rec)
      av.Run("Locality_map",{rec,wma,map})
      r = Rect.MakeEmpty
      r = myTheme.GetSelectedExtent
      if (r.ReturnSize=(0@0)) then
        theView.GetDisplay.PanTo(r.ReturnOrigin)
      else
        theView.GetDisplay.SetExtent(r.Scale(0.97))
      end
      theTable = myFTab
      theTable.GetSelection.Not
      theTable.UpdateSelection
      myTheme.Invalidate(true)
      
      ' Try blacking out the area outside the WMA:      

      wmaTheme.ClearSelection
      wmaFTab.SetDefinition("([Name] = wma)")
      wmaFTab.UpdateDefBitmap
      wmaFTab.GetSelection.Set(rec)
      wmaTheme.Invalidate(true)
      wmaTable = wmaFTab
      wmaTable.GetSelection.Not
      wmaTable.UpdateSelection
      wmaTheme.Invalidate(true)
      theView.GetWin.Activate
      av.Run("MauchaDelete",{})
      av.Run("JuanitaPiesDelete",{})
      if (symbol_type="Maucha") then
        av.Run("MauchaGraphicsShape",{})
        av.Run("MauchaBack",{})
      end
      if (symbol_type="Pie") then
        av.Run("PiePlotAuto",{})
        av.Run("JuanitaPieBack",{})
      end
      theView.GetDisplay.Flush
      
      'MainMap is the main map View:
      

      theGraphics = myLayout.GetGraphics
      theGraphicsList = theGraphics.FindAllByObjectTag("MainMap")
      theGraphics.UnSelectAll
      g = 0
      for each g in theGraphicsList
        g.SetView(theView,true)
        ViewScale = g.ReturnScale
      end
      
      'MsgBox.Info( "1:"++ ViewScale.asString, "View Scale" )
      'LocalityMap is the locality map View:
      

      theGraphics = myLayout.GetGraphics
      theGraphicsList = theGraphics.FindAllByObjectTag("LocalityMap")
      theGraphics.UnSelectAll
      g = 0
      for each g in theGraphicsList
        g.SetView(theViewLoc,true)
      end
      if ((symbol_type="Maucha") or (symbol_type="Pie")) then
        'KeySymbol is the symbol shape key View:
        

        theGraphics = myLayout.GetGraphics
        theGraphicsList = theGraphics.FindAllByObjectTag("KeySymbol")
        theGraphics.UnSelectAll
        g = 0
        for each g in theGraphicsList
          g.SetView(theViewKey,true)
        end
        
        'KeyList is the symbol colour key View:
        

        if (symbol_type<>"Maucha") then
          theGraphics = myLayout.GetGraphics
          theGraphicsList = theGraphics.FindAllByObjectTag("KeyList")
          theGraphics.UnSelectAll
          g = 0
          for each g in theGraphicsList
            if (symbol_type="Maucha") then
              g.SetUserScale(ViewScale)
              'g.SetUserScale( 2933333 )

            end
            g.SetView(theViewList,true)
          end
        end
      end
      'ISOdate is the datestamp and scale text string:
      

      nowDate = Date.Now
      nowDate.SetFormat("yyyy-MM-dd")
      nowTime = Date.Now
      nowTime.SetFormat("hhhh:m")
      dtStamp = nowDate.asString++" "++nowTime.AsString
      av.ShowMsg(wma.asString++"record"++rec.AsString++dtStamp)
      theGraphics = myLayout.GetGraphics
      theGraphicsList = theGraphics.FindAllByObjectTag("ISOdate")
      theGraphics.UnSelectAll
      g = 0
      for each g in theGraphicsList
        g.SetText(dtStamp++" Approximate scale 1:"++ViewScale.SetFormat("d").AsString)
      end
      
      'Title is the Water Management Area text:
      

      theGraphicsList = theGraphics.FindAllByObjectTag("Title")
      theGraphics.UnSelectAll
      g = 0
      for each g in theGraphicsList
        g.SetText(wma++"Water Management Area")
      end
      
      'WMAnr is the Water Management Area number on the 1:2 000 000 map (Year 2000):
      

      theGraphics.ClearSelected
      theGraphicsList = theGraphics.FindAllByObjectTag("WMAnr")
      theGraphics.UnSelectAll
      g = 0
      if (theGraphicsList<>Nil) then
        for each g in theGraphicsList
          g.SetText("WMA"++wmanumber.SetFormat("d").AsString)
        end
      end
      theGraphics.ClearSelected
      
      'Description is the sub-heading describing the map type:
      

      theGraphicsList = theGraphics.FindAllByObjectTag("Description")
      theGraphics.UnSelectAll
      g = 0
      for each g in theGraphicsList
        g.SetText(description)
      end
      av.ShowMsg(wmanumber.SetFormat("d").AsString++wma.asString++dtStamp++r.asString++"record"++rec.AsString)
      myLayout.Invalidate
      myLayout.GetWin.Activate
      myLayout.GetDisplay.ZoomToPage
      myLayout.GetDisplay.Flush
      
      'theView.Invalidate
      

      if (Please_print) then
        if (output_type="print to default printer") then
          'printer.The.SetFileName("c:\temp\wma.prt")

          myLayout.Print
        elseif (output_type="image on disk") then
          anImageFile = wma.Substitute("(","_")
          anImageFile = anImageFile.Substitute(".","_")
          anImageFile = anImageFile.Substitute("\","_")
          anImageFile = anImageFile.Substitute("/","_")
          anImageFile = anImageFile.Substitute(")","_")
          anImageFile = anImageFile.Substitute(" ","_")
          anImageFile = anImageFile.Substitute("__","_")
          anImageFile = "c:\temp\avtemp\"+anImageFile+".jpg"
          anImageFile = anImageFile.LCase
          if (File.Exists(anImageFile.AsFileName)) then
            File.Delete(anImageFile.AsFileName)
          end
          theExportFile = myLayout.ExportToFile(anImageFile.AsFileName,"JPEG",{120,100})
          'myLayout.Export

        else
          'nothing

        end
      end
      end
    end
  
  'myTheme.ClearSelection

end