#include <origin.h>
#include <GetNBox.h>
#include <xfutils.h>

/*------------------------------------------------------------------------------*
 * Modification Log:															*
 *	Folger 10/10/09 BETTER_ERROR_CHECKING_FOR_XYZAREA							*
 *------------------------------------------------------------------------------*/

///----- Folger 10/10/09 BETTER_ERROR_CHECKING_FOR_XYZAREA
static	BOOL	_check_xyz_data(TreeNode& trRng)
{
	XYZRange	xyz;
	return okxf_resolve_tree_construct_range(&trRng, &xyz) && xyz.IsValid();
}
///----- End BETTER_ERROR_CHECKING_FOR_XYZAREA


void xyzarea(const XYZRange& iz, double& area)
{
	
	if (!iz.IsValid())
	{
		XF_THROW(XFERR_INVALID_XYZ_RANGE);
	}
	
	vector vX, vY, vZ;
	if( !iz.GetData(vZ, vY, vX) )
	{
		XF_THROW(CER_NO_DATA);
	}
	
	int nSize = vX.GetSize();
	if( nSize != vZ.GetSize() || nSize != vY.GetSize() )
	{
		area = NANUM;
		XF_THROW(-1);
	}
	
	double dArea;
	int nRet = ocmath_3d_surface_area_mesh(vX, vY, vZ, nSize, &dArea);
	if(nRet != OE_NOERROR)
	{
		dArea = NANUM;
	}
	
	area = dArea;
	
}

//nEventID < 0 on event, like GETNE_ON_INIT, GETNE_ON_OK etc, >0 on change from control
//return 0 to do nothing, return PEVENT_GETN_RET_TO_CLOSE to close dialog
//dwEnables = bOKEnable, bBTN1Enable, bBTN2Enable, bBTN3Enable, , can set to false to disable OK/Apply buttons
//lpcszNodeName = the trGetN TreeNode tag name that generates the event, or is  if GETNE_ON_OK etc
//strAux = optional string for XF Wizard Bar, like DLG_NEED_CHANGE_GRID etc, it will be invalid if from internal node value change event
//strErrMsg = allowing messages to be shown
int xyzarea_event1(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	if( xf_is_extended_event(nEventID) )
		return xyzarea_event1_ex(trGetN, nRow, nEventID, dwEnables, lpcszNodeName, DynaCntrlContainer, strAux, strErrMsg);
	DECLARE_BUTTON_ENABLES
	
	///----- Folger 10/10/09 BETTER_ERROR_CHECKING_FOR_XYZAREA
	TreeNode	trRng = trGetN.iz;
	if ( !_check_xyz_data(trRng) )
	{
		strErrMsg = XFERR_INVALID_XYZ_RANGE;
		bOKEnable = false;
		return true;
	}
	///----- End BETTER_ERROR_CHECKING_FOR_XYZAREA
	
	string srtOutputInfo = _L("The area will be put in Results Log window");
	strErrMsg = srtOutputInfo;
	return false;
	

}

int xyzarea_make_tree(TreeNode& tr, LPCSTR lpcszVarName)
{
	return false;
}
//nGetNDialog = 0,1,2, corresponding to the XF GetN dropdown, 0 = dialog is not used
//you can change nRet to be one of XFEVT_PROCEED (defualt), XFEVT_ABORT, XFEVT_PROCEED_NO_DLG or XFEVT_PROCEED_WITH_DLG(when nGetNDialog = 0)
//nGetNDialog = -1 when Ok on GetN dialog to allow handler to preocess <auto> etc variables
//dwCntrl is combination of bits
//LTXF_FROM_GUI_MENU, LTXF_FROM_GUI_PROMPT, LTXF_FROM_AUTO_UPDATE, LTXF_FROM_FUNCTION, LTXF_THEME_USED, LTXF_CHANGE_PARAM
void xyzarea_before_execute(TreeNode& trGetN, int nGetNDialog, int& nRet, int dwCntrl)
{

}
//nEventID < 0 on event, like GETNE_ON_INIT, GETNE_ON_OK etc, >0 on change from control
//return 0 to do nothing, return PEVENT_GETN_RET_TO_CLOSE to close dialog
//dwEnables = bOKEnable, bBTN1Enable, bBTN2Enable, bBTN3Enable, , can set to false to disable OK/Apply buttons
//lpcszNodeName = the trGetN TreeNode tag name that generates the event, or is  if GETNE_ON_OK etc
//strAux = optional string for XF Wizard Bar, like DLG_NEED_CHANGE_GRID etc, it will be invalid if from internal node value change event
//strErrMsg = allowing messages to be shown
int xyzarea_event1_ex(TreeNode& trGetN, int nRow, int nEventID, DWORD& dwEnables, LPCSTR lpcszNodeName, WndContainer& DynaCntrlContainer, string& strAux, string& strErrMsg)
{
	return false;
}
