﻿'BlankByLevel blanks a grid above or below a given Z value,
'and saves the blanked grid to a new grid file.

Sub Main

Debug.Clear

	Dim SurferApp As Object
	Set SurferApp = CreateObject("Surfer.Application")
	SurferApp.Visible = True

	Set Plot = SurferApp.Documents.Add(srfDocPlot)
	Set Shapes = Plot.Shapes
	Set Grid = SurferApp.NewGrid

'Choose a grid file
	gridfilepath = GetFilePath( , "grd",SurferApp.Path+"\samples\","Open Grid File",0)

	Set Grid2 = Grid.LoadFile(gridfilepath,False)

    Debug.Print	"Grid Min =";Grid.zMin
	Debug.Print "Grid Max =";Grid.zMax

' Set level cutoff and flag above or below
	Dim level As Double
	Dim below As Boolean

	Begin Dialog UserDialog 270,112 ' %GRID:10,7,1,1
		Text 10,7,90,14,"Z Level value:",.Text0
		TextBox 10,28,180,21,.LevlVal
		OptionGroup .Group1
			OptionButton 20,77,130,14,"Blank Below",.below
			OptionButton 20,56,130,14,"Blank Above",.above
		OKButton 170,70,80,21
	End Dialog
	Dim entervalue As UserDialog

	Dialog entervalue
		level = Val(entervalue.LevlVal$)
		Debug.Print "Z Level Value = ";entervalue.LevlVal$
		If entervalue.Group1 = 0 Then below = True
	    If entervalue.Group1 = 1 Then below = False

	Debug.Print "Processing GridRow:"
	For row=1 To Grid.NumRows()
    For col=1 To Grid.NumCols()
		If below = True Then
	      If Grid.GetNode(row,col) < level And Grid.IsBlanked(row,col) = False Then Grid.BlankNode(row,col)
		ElseIf below = False Then
	      If Grid.GetNode(row,col) > level And Grid.IsBlanked(row,col) = False Then Grid.BlankNode(row,col)
	    End If

    Next col
    Debug.Print row;
    If row Mod 20 = 0 Then Debug.Print
	Next row

'Saves the grid to a new name
	gridfilepath = Left(gridfilepath,InStrRev(gridfilepath,".")-1) + "_Blanked.grd
	Grid.SaveFile(gridfilepath, srfGridFmtS7)
	Debug.Print	"" 'newline
	Debug.Print	"" 'newline
	Debug.Print "File saved as ";gridfilepath

'Creates a contour map of new grid
	Shapes.AddContourMap(gridfilepath)

End Sub
