/*
	Copyright (c) 2024 PTC Inc. and/or Its Subsidiary Companies. All Rights Reserved.
*/

#include <ProToolkit.h>
#include <ProMfg.h>
#include <ProTool.h>
#include <ProParameter.h>
#include <ProParamval.h>
#include <ProElemId.h>
#include <ProUtil.h>
#include <ProMfgOptions.h>

#include <TestError.h>
#include <UgMfg.h>

/*====================================================================*\
Function : UserParamToolCreate
Purpose  : Creates a simple drilling tool from parameters
\*====================================================================*/
ProError UserParamToolCreate (
/*--------------------------------------------------------------------*/
ProMfg		mfg_model,
ProToolType	tool_type,
wchar_t		*tool_id,
MfgPrmDblValue	params[],
int		num_params,
ProUnitLength   units, /* PRO_UNITLENGTH_MM or PRO_UNITLENGTH_IN */
ProTool		*tool

)
/*--------------------------------------------------------------------*/
{
 ProElement		tool_elem;
 ProToolinputPtr	tool_input;
 ProError		status;
 ProErrorlist		errors;
 ProParamvalue		param_val;
 int			ii;

 /*------------------------------------------------*\
    Allocate the input structure
 \*------------------------------------------------*/
 status = ProToolinputAlloc( &tool_input );
 ERROR_CHECK( "UserParamToolCreate", "ProToolinputAlloc", status );
    
 /*------------------------------------------------*\
	Set the type
 \*------------------------------------------------*/
 status = ProToolinputTypeSet( tool_input, tool_type );
 ERROR_CHECK( "UserParamToolCreate", "ProToolinputTypeSet", status );
	
 /*------------------------------------------------*\
	Add tool parameters
 \*------------------------------------------------*/
 status = ProElementAlloc( PRO_E_PARAMS, &tool_elem );
 ERROR_CHECK( "UserParamToolCreate", "ProElementAlloc", status );
	
 for ( ii = 0; ii < num_params; ii++ )
 {
   param_val.type = PRO_PARAM_DOUBLE;
   param_val.value.d_val = params[ii].dbl_val;

   status = ProToolElemParamAdd( tool_elem, &param_val, 
                                 params[ii].param_name ); 
   ERROR_CHECK( "UserParamToolCreate", "ProToolElemParamAdd",status );
 }

 param_val.type = PRO_PARAM_STRING;
 if ( units == PRO_UNITLENGTH_IN )
   ProStringToWstring( param_val.value.s_val, "INCH" );
 else
   ProStringToWstring( param_val.value.s_val, "MILLIMETER" );

 status = ProToolElemParamAdd( tool_elem, &param_val, "LENGTH_UNITS" );
 ERROR_CHECK( "UserParamToolCreate", "ProToolElemParamAdd",status );


 status = ProToolinputElemAdd( tool_input, tool_elem );
 ERROR_CHECK( "UserParamToolCreate", "ProToolinputElemAdd",status );
         
 /*------------------------------------------------*\
        Create the tool.
 \*------------------------------------------------*/
 status = ProToolInit( tool_id, mfg_model, tool );
 ERROR_CHECK( "UserParamToolCreate", "ProToolInit", status );
	
 status = ProToolCreate( tool, tool_input, &errors );
 ERROR_CHECK( "UserParamToolCreate", "ProToolCreate", status );

 /*------------------------------------------------*\
         Free the input structure
 \*------------------------------------------------*/
 status = ProToolinputFree( &tool_input );
 ERROR_CHECK( "UserParamToolCreate", "ProToolinputFree", status );

 return ( status );
}