/**
Name: Surfer.cpp
Blame: John Gann, Chesapeake Technology, Inc. http://www.chesapeaketech.com
Start Date: 21 MAY 2002
Purpose: To handle the OLE automation of the Surfer application object to 
		produce a grid file from randomly spaced x,y,z data points.

**/
#include "stdafx.h"
#include "Surfer.h"

// Adjust this path to match where your Surfer8 is installed
#import "C:\Program Files\Golden Software\Surfer8\Surfer.exe" no_namespace raw_interfaces_only


/**
INPUT: 
	pXYZInputFile name of the input file containing the values to be gridded
	pOutputGridFile name of the output file where the grid should be written

My data looks like this (x, y, z)
592523, 3624300.9, 8.5
592523, 3624301.0, 8.5
592523, 3624301.1, 8.1
592523, 3624301.2, 8.1
**/
BOOL CreateSurferGridFile(const char * pXYZInputFile, const char * pOutputGridFile)
{
// Create an instance of the Application object
// TODO: Probably want to change this to look for an existing instance of the application
// object and just attach to it.
IApplicationPtr pApp(__uuidof(Application));

// Make the Application object visible
pApp->put_Visible(VARIANT_TRUE);

COleVariant vpXYZInputFile(pXYZInputFile, VT_BSTR);       // input filename as a variant
COleVariant vpOutputGridFile(pOutputGridFile, VT_BSTR);   // output filename as a variant
COleVariant vtXCol(1L);            // the column containing the X coordinate (1-based index)
COleVariant vtYCol(2L);            // the column containing the Y coordinate (1-based index)
COleVariant vtZCol(3L);            // the column containing the Z coordinate (1-based index)
COleVariant vtShowReport((long) FALSE, VT_I4); // i'm not interested in the report
/* In case you're wondering what the heck a VARIANT_BOOL is...
0 == FALSE, -1 == TRUE 
typedef short VARIANT_BOOL;
*/
VARIANT_BOOL vtStatus;             // I think this is the return value of the function
                                   // as a short.  The function returns an HRESULT via the wrapper class
								   // generated by the #import directive but the Scripter helpfile says
								   // it should return a boolean value.  According to the Microsoft
								   // docs -1 = TRUE and 0 = FALSE.  Odd.

/**
>From MSDN:
HOWTO: Pass Optional Arguments to MFC ActiveX Controls 

This seems to be the answer on how to default the bazillion unused parameters
like VB users do.
**/
VARIANT vDefaultArg;
vDefaultArg.vt = VT_ERROR;
vDefaultArg.scode = DISP_E_PARAMNOTFOUND;

HRESULT hResult = pApp->GridData(
		vpXYZInputFile.bstrVal,           // BSTR DataFile,
		vtXCol.lVal,
		vtYCol.lVal,
		vtZCol.lVal,
		vDefaultArg,
		vDefaultArg,
		vDefaultArg,
		vDefaultArg,
		vDefaultArg,
		vDefaultArg,                       // 10
		vDefaultArg,
		vDefaultArg,
		vDefaultArg,
		vDefaultArg,
		vDefaultArg,
		vtShowReport,                  // don't show the report
		vDefaultArg,
		vDefaultArg,
		vDefaultArg,
		vDefaultArg,					// 20
		vDefaultArg,
		vDefaultArg,
		vDefaultArg,
		vDefaultArg,
		vDefaultArg,
		vDefaultArg,
		vDefaultArg,
		vDefaultArg,
		vDefaultArg,						
		vDefaultArg,                   // 30
		vDefaultArg,
		vDefaultArg,
		vDefaultArg,
		vDefaultArg,
		vDefaultArg,
		vDefaultArg,
		vDefaultArg,
		vDefaultArg,
		vDefaultArg,			
		vDefaultArg,				 // 40
		vDefaultArg,
		vDefaultArg,
		vDefaultArg,
		vDefaultArg,
		vDefaultArg,
		vDefaultArg,
		vDefaultArg,
		vDefaultArg,
		vDefaultArg,					
		vpOutputGridFile.bstrVal,				// 50 Output data filename
		srfGridFmtAscii,                        // 51 Output data format
		vDefaultArg,
		vDefaultArg,
		vDefaultArg,
		vDefaultArg,
		vDefaultArg,
		vDefaultArg,
		&vtStatus);                              // return status?

TRACE("hResult: %lu, vtStatus: %d\n", hResult, vtStatus);

pApp->Quit();
return TRUE;
}
