' Example Surfer scripting file for plot automation using Surfer v. 7.0 or higher Option Explicit Dim SurferApp As Object Dim SurferWks As Object Dim SurferDoc As Object Dim SurferPlot As Object Dim SurferPageSetup As Object Dim SurferShapes As Object Dim SurferMapFrame As Object Dim SurferAxes As Object Dim SurferAxis As Object Dim SurferSelection As Object Dim SurferText As Object Dim SurferFontFormat As Object Dim SurferImageMap As Object Dim SurferColorMap As Object Sub Main ' This routine is created by Probe for EPMA Dim Directory As String Dim File As String Dim Sample As String Dim MaxCol As Integer Dim XLabel As String Dim YLabel As String Directory$ = "C:\UserData\SurferData\" ' this line points to the data folder containing all files Directory$ = CurDir$() & "\" ' comment out this line if running on Win7 64 bit File$ = "xyscan2" Sample$ = "azl-c-02 d PIS 22" MaxCol% = 14 XLabel$ = "X .1mm" YLabel$ = "Y .1mm" ReDim ZLabel(1 To MaxCol%) As String ZLabel$( 1) = "Si Elemental Percents" ZLabel$( 2) = "Fe Elemental Percents" 'ZLabel$( 3) = "Ca Elemental Percents" 'ZLabel$( 4) = "Al Elemental Percents" 'ZLabel$( 5) = "O Elemental Percents" 'ZLabel$( 6) = "Mn Elemental Percents" 'ZLabel$( 7) = "Na Elemental Percents" ZLabel$( 8) = "K Elemental Percents" 'ZLabel$( 9) = "Ba Elemental Percents" 'ZLabel$(10) = "Mg Elemental Percents" 'ZLabel$(11) = "P Elemental Percents" 'ZLabel$(12) = "Co Elemental Percents" 'ZLabel$(13) = "Ti Elemental Percents" 'ZLabel$(14) = "Total Elemental Percents" ' Call output routine Call GridAll(Directory$, File$, Sample$, MaxCol%, XLabel$, YLabel$, ZLabel$()) End Sub Sub GridAll(Directory As String, File As String, Sample As String, MaxCol As Integer, XLabel As String, YLabel As String, ZLabel() As String) ' This is the routine that actually creates the output. It is appended to text output from PFW to ' create a complete surfer automation module. See XYSCAN2.BAS (Surfer 7) for a complete example. Dim OutputType As String Dim GridMethodType As Integer ' ATTENTION! ATTENTION! ATTENTION ATTENTION! ATTENTION! ATTENTION! ' The last UNcommented "OutputType$" variable will determine the output (edit as desired) OutputType$ = "TEST" 'OutputType$ = "PRINT" ' The last UNcommented "GridMethodType%" variable will determine the grid method (edit as desired) 'GridMethodType% = srfInverseDistance GridMethodType% = srfKriging 'GridMethodType% = srfMinCurvature 'GridMethodType% = srfShepards 'GridMethodType% = srfNaturalNeighbor 'GridMethodType% = srfNearestNeighbor 'GridMethodType% = srfRegression 'GridMethodType% = srfRadialBias 'GridMethodType% = srfTriangulation ' ATTENTION! ATTENTION! ATTENTION ATTENTION! ATTENTION! ATTENTION! Dim FileData As String Dim FileBlank As String Dim FileBlank2 As String Dim FileOut As String Dim FileGrid As String Dim FileSurfContour As String Dim FileSurfImage As String Dim FileSurfSurface As String Dim FileSurfShaded As String Dim FileFont As String Dim NoBLNFile As Integer Dim iMap As Integer ' Create the Surfer object Set SurferApp = CreateObject("Surfer.Application") ' Check version number Dim astring As String If Val(Left$(SurferApp.Version, 2)) < Val("7.") Then astring$ = "This script requires v. 7 or higher of Surfer Scripter (from Golden Software). Please upgrade your Surfer version." MsgBox astring$, vbOkOnly + vbCritical, "GridAll" End If ' First make sure that the file directory names are upper case Directory$ = UCase$(Directory$) File$ = UCase$(File$) If OutputType$ = "" Then OutputType$ = "TEST" OutputType$ = UCase$(OutputType$) ' Open the .DAT data file and then close the worksheet FileData$ = Trim$(Directory$) & Trim$(File$) & ".DAT" FileBlank$ = Trim$(Directory$) & Trim$(File$) & ".BLN" Debug.Print "Data File: " & FileData$ Debug.Print "Blanking (boundary) File: " & FileBlank$ If Dir$(FileData$) = "" Then MsgBox ("File " & FileData$ & " was not found. If this is Win 7 64 bit, comment out the second Directory$ = statement above and try again.", vbOkOnly + vbExclamation, "GridAll") End End If ' Open data file If OutputType$ = "TEST" Then Debug.Print "Opening Data File : " & FileData$ SurferApp.Visible = True Set SurferWks = SurferApp.Documents.Open(FileData$) ' Open plot document Set SurferDoc = SurferApp.Documents.Add() ' Open plot window Set SurferPlot = SurferDoc.Windows(1) ' If in test mode enable auto-redraw If OutputType$ = "TEST" Then SurferPlot.AutoRedraw = True Else SurferPlot.AutoRedraw = False End If ' Specify Page Orientation (landscape) Set SurferPageSetup = SurferDoc.PageSetup SurferPageSetup.Orientation = srfLandscape If OutputType$ = "TEST" Then Debug.Print "Setting Landscape Page Mode" ' Loop through columns and create the grids For iMap% = 1 To MaxCol% ' Check for blank elements If Asc(ZLabel$(iMap%)) = 0 Then GoTo NextColumn: ' Make the Map Title and Output File Name (use the whole ZLabel string for the grid file name) FileOut$ = Trim$(Directory$) & "OUT.GRD" FileGrid$ = Trim$(Directory$) & Trim$(File$) & "_" & Trim$(ZLabel$(iMap%)) & ".GRD" FileSurfContour$ = Trim$(Directory$) & Trim$(File$) & "_Contour_" & Trim$(ZLabel$(iMap%)) & ".SRF" FileSurfImage$ = Trim$(Directory$) & Trim$(File$) & "_Image_" & Trim$(ZLabel$(iMap%)) & ".SRF" FileSurfSurface$ = Trim$(Directory$) & Trim$(File$) & "_Surface_" & Trim$(ZLabel$(iMap%)) & ".SRF" FileSurfShaded$ = Trim$(Directory$) & Trim$(File$) & "_Shaded_" & Trim$(ZLabel$(iMap%)) & ".SRF" Debug.Print "Grid File (column " & Str$(iMap%) & "): " & FileGrid$ FileFont$ = "Times New Roman" ' Grid the current X,Y,Z data If OutputType$ = "TEST" Then Debug.Print "Gridding Column " & Str$(iMap%) & " for " & ZLabel$(iMap%) If SurferApp.GridData(FileData$, xCol:=1, yCol:=2, zCol:=iMap% + 2, NumRows:=40, NumCols:=40, Algorithm:=GridMethodType%, ShowReport:=False, OutGrid:=FileOut$) = 0 Then End ' Spline smooth the grid and blank it If OutputType$ = "TEST" Then Debug.Print "Smoothing Column " & Str$(iMap%) & " for " & ZLabel$(iMap%) If SurferApp.GridSplineSmooth(InGrid:=FileOut$, OutGrid:=FileOut$, nRow:=2, nCol:=2) = 0 Then End ' See if the .BLN file is present NoBLNFile% = 1 If Dir$(FileBlank$) <> "" Then NoBLNFile% = 0 ' If BLN file was found blank the grid If NoBLNFile% = 0 Then If OutputType$ = "TEST" Then Debug.Print "Blanking Column " & Str$(iMap%) & " for " & ZLabel$(iMap%) If SurferApp.GridBlank(InGrid:=FileOut$, OutGrid:=FileGrid$, BlankFile:=FileBlank$) = 0 Then End ' If the BLN file was not found, spline smooth the grid (with no added nodes) to change the name Else If NoBLNFile% = 1 Then SurferApp.GridSplineSmooth(InGrid:=FileOut$, OutGrid:=FileGrid$, nRow:=0, nCol:=0) End If ' Set the Shapes collection Set SurferShapes = SurferDoc.Shapes ' Load base map if .BLN file is available If OutputType$ = "TEST" Then Debug.Print "Loading Base Map : " & FileBlank$ If NoBLNFile% = 0 Then SurferShapes.AddBaseMap(FileBlank$) ' Create a contour map If OutputType$ = "TEST" Then Debug.Print "Contouring Column" & Str$(iMap%) & " for " & ZLabel$(iMap%) Set SurferMapFrame = SurferShapes.AddContourMap(FileGrid$) ' Add post points from data file If OutputType$ = "TEST" Then Debug.Print "Posting Column " & Str$(iMap%) & " for " & ZLabel$(iMap%) Set SurferMapFrame = SurferShapes.AddPostMap(FileData$) ' Select Base and grid and overlay maps if base map was loaded SurferShapes.SelectAll If OutputType$ = "TEST" Then Debug.Print "Overlaying Maps" Set SurferMapFrame = SurferDoc.Selection.OverlayMaps ' Create x, y axes and titles Call GridText(OutputType$, XLabel$, YLabel$, srfATLeft, srfATTop, Sample$, ZLabel$(iMap%), FileFont$) ' Output the plot Call GridOutput(OutputType$, SurferPlot, SurferDoc) ' Save to SRF file If OutputType$ = "TEST" Then Debug.Print "Saving Contour Plot" SurferDoc.SaveAs(FileName:=FileSurfContour$) ' Select all objects and delete If OutputType$ = "TEST" Then Debug.Print "Selecting and deleting contour plot" SurferShapes.SelectAll SurferDoc.Selection.Delete ' Now plot a color flood (image) plot If OutputType$ = "TEST" Then Debug.Print "Color Image Mapping Column " & Str$(iMap%) & " for " & ZLabel$(iMap%) Set SurferMapFrame = SurferShapes.AddImageMap(FileGrid$) ' Set color palette Set SurferImageMap = SurferMapFrame.Overlays(1) SurferImageMap.ShowColorScale = True ' Assigns the color spectrum properties to the variable named Set SurferColorMap = SurferImageMap.ColorMap ' Load color spectrum depending on version number If Val(Left$(SurferApp.Version, 2)) <= Val("8.") Then SurferColorMap.LoadFile(SurferApp.Path+"\Samples\Rainbow2.clr") Else SurferColorMap.LoadFile(SurferApp.Path+"\ColorScales\Rainbow2.clr") End If ' Create x, y axes and titles (axis label positions are flipped because of landscape mode) Call GridText(OutputType$, XLabel$, YLabel$, srfATLeft, srfATTop, Sample$, ZLabel$(iMap%), FileFont$) ' Output the plot Call GridOutput(OutputType$, SurferPlot, SurferDoc) ' Save to SRF file If OutputType$ = "TEST" Then Debug.Print "Saving Image Map Plot" SurferDoc.SaveAs(FileName:=FileSurfImage$) 'Export the plots as a JPG file Dim FileJPG As String FileJPG$ = Trim$(Directory$) & Trim$(File$) & "_Image_" & Trim$(ZLabel$(iMap%)) & ".jpg" SurferDoc.Export(FileName:=FileJPG$, SelectionOnly:=False,Options:="Defaults=1, ForgetOptions=1, KeepAspect=1, Width=2048") ' Select all objects and delete for next plot If OutputType$ = "TEST" Then Debug.Print "Selecting and deleting color image plot" SurferShapes.SelectAll SurferDoc.Selection.Delete ' Now plot a 3d map If OutputType$ = "TEST" Then Debug.Print "Wire Framing Column " & Str$(iMap%) & " for " & ZLabel$(iMap%) Set SurferMapFrame = SurferShapes.AddWireframe(FileGrid$) ' Create x, y axes and titles (axis label positions are flipped because of landscape mode) Call GridText(OutputType$, XLabel$, YLabel$, srfATLeft, srfATTop, Sample$, ZLabel$(iMap%), FileFont$) ' Output the plot Call GridOutput(OutputType$, SurferPlot, SurferDoc) ' Save to SRF file If OutputType$ = "TEST" Then Debug.Print "Saving Surface Plot" SurferDoc.SaveAs(FileName:=FileSurfSurface$) ' Select all objects and delete for next plot If OutputType$ = "TEST" Then Debug.Print "Selecting and deleting surface plot" SurferShapes.SelectAll SurferDoc.Selection.Delete ' Now plot a shaded relief map If OutputType$ = "TEST" Then Debug.Print "Shaded Reliefing Column " & Str$(iMap%) & " for " & ZLabel$(iMap%) Set SurferMapFrame = SurferShapes.AddReliefMap(FileGrid$) ' Create x, y axes and titles Call GridText(OutputType$, XLabel$, YLabel$, srfATLeft, srfATTop, Sample$, ZLabel$(iMap%), FileFont$) ' Output the plot Call GridOutput(OutputType$, SurferPlot, SurferDoc) ' Save to SRF file If OutputType$ = "TEST" Then Debug.Print "Saving Sahded Relief Plot" SurferDoc.SaveAs(FileName:=FileSurfShaded$) ' Select all objects and delete for next plot If OutputType$ = "TEST" Then Debug.Print "Selecting and deleting surface plot" SurferShapes.SelectAll SurferDoc.Selection.Delete NextColumn: Next iMap% ' Close the Surfer App SurferApp.Documents.CloseAll(SaveChanges:=srfSaveChangesNo) SurferApp.Quit astring$ = "All (.SRF, .JPG, etc) output was automatically saved to the " & Directory$ & " folder." MsgBox astring$, vbOkOnly + vbInformation, "GridAll" End End Sub Sub GridText(OutputType$, XLabel$, YLabel$, XLabelPosition%, YLabelPosition%, Sample$, Title$, FileFont$) If OutputType$ = "TEST" Then Debug.Print "Adding text for axes" Set SurferAxes = SurferMapFrame.Axes Set SurferAxis = SurferAxes(XLabelPosition%) SurferAxis.Title = XLabel$ ' Create y axis Set SurferAxes = SurferMapFrame.Axes Set SurferAxis = SurferAxes(YLabelPosition%) SurferAxis.Title = YLabel$ ' Add the sample name and title Set SurferText = SurferShapes.AddText(5.5, 1, Sample$) SurferText.Font.Face = FileFont$ SurferText.Font.Size = 14 Set SurferFontFormat = SurferText.Font SurferFontFormat.HAlign = srfTACenter Set SurferText = SurferShapes.AddText(5.5, .75, Title$) SurferText.Font.Face = FileFont$ SurferText.Font.Size = 18 Set SurferFontFormat = SurferText.Font SurferFontFormat.HAlign = srfTACenter End Sub Sub GridOutput(OutputType$, SurferPlot, SurferDoc) If OutputType$ = "PRINT" Then Debug.Print "Sending output to printer" SurferPlot.AutoRedraw = True SurferDoc.PrintOut(Method:=srfTruncate) SurferPlot.AutoRedraw = False End If If OutputType$ = "TEST" Then Debug.Print "Waiting for 8 seconds" Wait(8) End If End Sub