' Name: SetConstants
'
' Title: Set compliance ranges and colour codes for project
'
' Topics: Views
'
' Description: Sets compliance ranges and colour codes based on
'              water quality guidelines
'
' Author: Michael Silberbauer 2001-03-12, 2001-04-23
' Institute for Water Quality Studies, Department of Water and Sanitation
' Private Bag X313 PRETORIA South Africa 0001
' SilberbauerM@dwaf.gov.za
'
' Requires: _UseType, e.g. "Domestic_Health", _ValType, e.g. "Val_info"
' av.Run( SetConstants {} )
' Self:
'
' Results: A set of special colours,
'          Dictionaries of colours (_theColourDict) and
'          water quality class cutpoints (_UseVariables)
' -----------------------------------------------------------------------------

nCuts = 8 ' maximum possible number of cutpoints

theProject = av.GetProject
ClassCodes = theProject.FindDoc("classcodes.txt")
DocumentList = theProject.GetDocs
theVTab = ClassCodes.getVTab
theVTab.GetSelection.ClearAll
theVTab.UpdateSelection
theBitMap = theVTab.GetSelection
_Dark_Blue = Color.Make
_Dark_Blue.SetRGBList({0,0,128})
_Light_Green = Color.Make
_Light_Green.SetRGBList({30,255,30})
_Dark_Green = Color.Make
_Dark_Green.SetRGBList({0,128,0})
_Salmon_Pink = Color.Make
_Salmon_Pink.SetRGBList({255,128,128})
_Orange = Color.Make
_Orange.SetRGBList({255,200,0})
_Dark_Red = Color.Make
_Dark_Red.SetRGBList({128,0,0})
FieldList = theVTab.GetFields
F_Group = theVTab.FindField("Group")
if (_ValType="Val_info_med") then
  F_Val = theVTab.FindField("Val_info_med")
end
if (_ValType="Val_info_dbf") then
  F_Val = theVTab.FindField("Val_info_dbf")
end
if (_ValType="Val_info") then
  F_Val = theVTab.FindField("Val_info")
end
if (_ValType="Val_info_txt") then
  F_Val = theVTab.FindField("Val_info_txt")
end
if (_ValType="Val_info_mn") then
  F_Val = theVTab.FindField("Val_info_mn")
end
F_Posn = theVTab.FindField("Posn")
F_Type = theVTab.FindField("Type")
F_Number = theVTab.FindField("Number")
F_Symbol = theVTab.FindField("Symbol")
F_Units = theVTab.FindField("Units")
F_Cut = List.Make
for each nCut in 1..nCuts
  F_Cut.Add(theVTab.FindField("Cut"+nCut.AsString))
end
F_R = List.Make
F_G = List.Make
F_B = List.Make
F_Txt = List.Make
for each nCut in 0..nCuts
  F_R.Add(theVTab.FindField("R"+nCut.AsString))
  F_G.Add(theVTab.FindField("G"+nCut.AsString))
  F_B.Add(theVTab.FindField("B"+nCut.AsString))
  F_Txt.Add(theVTab.FindField("Text"+nCut.AsString))
end
_theColourDict = Dictionary.Make(256)
_UseVariables = Dictionary.Make(256)
_theChemSym = Dictionary.Make(256)
_theNarrative = Dictionary.Make(512)
expr = "( [Group] = "+_UseType.AsString.Quote++")"
theVTab.Query(expr,theBitMap,#VTAB_SELTYPE_NEW)
selBits = theBitMap.Count
if (selBits<=0) then
  MsgBox.Info("Group"++_UseType.AsString++"does not exist: set guideline manually","MANUAL GUIDELINE")
  return {}
else
  for each rec in theBitmap
    valmd = theVTab.ReturnValue(F_Val,rec)
    postn = theVTab.ReturnValue(F_Posn,rec)
    vtype = theVTab.ReturnValue(F_Type,rec)
    numbr = theVTab.ReturnValue(F_Number,rec)
    'MsgBox.Info( postn.AsString++valmd++numbr.AsString, "CLASS CODES" )

    CutList = List.Make
    CutList.Add(postn)
    CutList.Add(vtype)
    CutList.Add(numbr)
    for each nCut in 1..numbr
      CutList.Add(theVTab.ReturnValue(F_Cut.Get(nCut-1),rec))
    end
    _UseVariables.Add(valmd.AsString,CutList)
    RGBList = List.Make
    TxtList = List.Make
    RGBList.Add(postn)
    RGBList.Add(vtype)
    RGBList.Add(numbr)
    TxtList.Add(postn)
    TxtList.Add(vtype)
    TxtList.Add(numbr)
    for each nCut in 0..numbr
      RGBList.Add(theVTab.ReturnValue(F_R.Get(nCut),rec))
      RGBList.Add(theVTab.ReturnValue(F_G.Get(nCut),rec))
      RGBList.Add(theVTab.ReturnValue(F_B.Get(nCut),rec))
      TxtList.Add(theVTab.ReturnValue(F_Txt.Get(nCut),rec))
    end
    _theColourDict.Add(valmd.AsString,RGBList)
    _theNarrative.Add(valmd.AsString,TxtList)
    ChemSym = List.Make
    ChemSym.Add(postn)
    ChemSym.Add(vtype)
    ChemSym.Add(numbr)
    ChemSym.Add(theVTab.ReturnValue(F_Symbol,rec))
    ChemSym.Add(theVTab.ReturnValue(F_Units,rec))
    _theChemSym.Add(valmd.AsString,ChemSym)
    'MsgBox.ListAsString( RGBList, "RGB values", "CLASS CODES" )

  end
end
return {}