' ExtractGridFile2.bas - ds 2010.01.22 ' ' ' Sometimes you have an [.SRF] file with maps, but you have lost the grids that made those maps. ' ' Select the map you want to extract the grid from and run this script. The grid file will ' be saved with the original grid's name, in the directory that contains the [.SRF] file. ' ' If you did not select a specific layer in the map, the script will just export from the ' first grid-based map in the selected mapframe. ' ' If nothing is selected the script will tell you to select something and try again. Sub Main Debug.Clear On Error GoTo NoSurfer Set surf = GetObject(,"Surfer.Application")'fetch the running instance of surfer On Error GoTo 0 Set plot = surf.ActiveWindow.Document'reference the active plot window if there is one 'Check for selected maps If plot.Type <> srfDocPlot Then MsgBox("No Plot document active. Please open a plot and try again." )', , "User error!") Exit Sub ElseIf (plot.Selection.Count = 0) Then MsgBox("Nothing selected. Select a map and try again." )', , "User error!") Exit Sub Else i = 0 For Each item In plot.Selection If item.Type = srfShapeMapFrame Then i = i +1 Next item If i <= 0 Then MsgBox("No maps selected. Select a map or map layer and try again." )', , "User error!") Exit Sub End If End If 'Create the object variables we will need to sift through the slection and extract the maps Dim export_map As Object Dim curr_sel As Object Dim curr_layer As Object first_grid_map = 0 'Sift through the selected items For i = 1 To plot.Selection.Count'this loop scans the entire Selection collection Set curr_sel = plot.Selection.Item(i)'refernce the current item in the collection If curr_sel.Type = srfShapeMapFrame Then'if it is a MapFrame For j = 1 To curr_sel.Overlays.Count'this scans the entire Overlays (layers) collection inside the mapframe Set curr_layer = curr_sel.Overlays.Item(j)'refernce the current layer in the collection 'is the layer a grid-based map? If (curr_layer.Type = srfShapeContourMap Or curr_layer.Type = srfShapeImageMap Or curr_layer.Type = srfShapeReliefMap Or curr_layer.Type = srfShapeVectorMap Or curr_layer.Type = srfShapeSurface Or curr_layer.Type = srfShapeWireframe) Then If (first_grid_map = 0) Then first_grid_map = j 'record the Overlays index of the first grid-based map If (curr_layer.Selected = True) Then'is the current layer selected? If so: Set export_map = curr_layer'assign it To the export_map variable j = curr_sel.Overlays.Count + 1 'increment the loop counters past thier limits so that the loops exit at the next iteration i = plot.Selection.Count + 1 ' End If ElseIf j = curr_sel.Overlays.Count Then'if we scanned the entire Overlays (layers) collection and no selected map caused the loops to end If (first_grid_map = 0) Then MsgBox("No grid-based maps selected. Select a map or map layer and try again." )', , "User error!") Exit Sub Else Set export_map = curr_sel.Overlays.Item(first_grid_map)'assign the first grid-based map to the export_map variable i = plot.Selection.Count + 1'increment the loop counter past its limit so that the loop exits at the next iteration End If End If Next j End If Next i If export_map.Type <> srfShapeVectorMap Then'if this is any kind of map other than a vector map grid_name = export_map.Grid.FileName grid_name = Right(grid_name , Len(grid_name) - InStrRev(grid_name , "\" ) ) grid_name = Left(grid_name , InStrRev(grid_name , "." ) -1 ) grid_name = plot.Path & grid_name & ".GRD" export_map.Grid.SaveFile(grid_name , srfGridFmtS7 )'save the grid file Debug.Print "Grid saved as " & grid_name'tell the user where it was saved in the Debug window Else'if it IS a vector map If export_map.GridFile <> "" Then 'If it is a 1-grid vector map 'export_name(,) grid_name = export_map.GridFile grid_name = Right(grid_name , Len(grid_name) - InStrRev(grid_name , "\" ) ) grid_name = Left(grid_name , InStrRev(grid_name , "." ) -1 ) grid_name = plot.Path & grid_name & ".GRD" export_map.GradientGrid.SaveFile(grid_name , srfGridFmtS7 )'save the grid file Debug.Print "Grid saved as " & grid_name'tell the user where it was saved in the Debug window End If If export_map.GradientGridFile <> "" Then'If it is a 2-grid vector map grid_name = export_map.GradientGridFile grid_name = Right(grid_name , Len(grid_name) - InStrRev(grid_name , "\" ) ) grid_name = Left(grid_name , InStrRev(grid_name , "." ) -1 ) grid_name = plot.Path & grid_name & ".GRD" export_map.GradientGrid.SaveFile(grid_name , srfGridFmtS7 )'save the grid file Debug.Print "Vector map Gradient Grid saved as " & grid_name'tell the user where it was saved in the Debug window End If If export_map.AspectGridFile <> "" Then'If it is a 2-grid vector map grid_name = export_map.AspectGridFile grid_name = Right(grid_name , Len(grid_name) - InStrRev(grid_name , "\" ) ) grid_name = Left(grid_name , InStrRev(grid_name , "." ) -1 ) grid_name = plot.Path & grid_name & ".GRD" export_map.AspectGrid.SaveFile(grid_name , srfGridFmtS7 )'save the grid file Debug.Print "Vector map Aspect Grid saved as " & grid_name'tell the user where it was saved in the Debug window End If If export_map.ColorGridFile <> "" Then'If it is a 2-grid vector map grid_name = export_map.ColorGridFile grid_name = Right(grid_name , Len(grid_name) - InStrRev(grid_name , "\" ) ) grid_name = Left(grid_name , InStrRev(grid_name , "." ) -1 ) grid_name = plot.Path & grid_name & ".GRD" export_map.ColorGrid.SaveFile(grid_name , srfGridFmtS7 )'save the grid file Debug.Print "Vector map Color Grid saved as " & grid_name'tell the user where it was saved in the Debug window End If End If NoSurfer:'this label is where the script jumps to if there was no running instance of Surfer. If Err.Number > 0 Then MsgBox("Surfer is not running! Please open a plot and try again." )', , "User error!") End Sub