/*====================================================================*\
FILE    : UgMfgParamToolCreate.c
PURPOSE : Pro/TOOLKIT User Guide Example

HISTORY..
DATE      BUILD   AUTHOR   MODIFICATIONS
04-dec-97 H-02-02 mgs      $$1  Created
\*====================================================================*/
#include <ProToolkit.h>
#include <ProMfg.h>
#include <ProTool.h>
#include <ProParameter.h>
#include <ProParamval.h>
#include <ProElemId.h>
#include <ProUtil.h>

#include <TestError.h>

/*====================================================================*\
Function : UserParamToolCreate
Purpose  : Creates a simple drilling tool from parameters
\*====================================================================*/
ProError UserParamToolCreate (
    ProMfg           mfg_model,
    ProToolType      tool_type,
    wchar_t         *tool_id,
    double           tool_diam,
    double           tool_length,
    double           point_angle,
    ProTool         *tool)
{
    #define NUM_PARAMS   3
    ProElement       tool_elem;
    ProToolinputPtr  tool_input;
    ProError         status;
    ProErrorlist     errors;
    ProParamvalue    param_val;
    
    char par_name[NUM_PARAMS][PRO_NAME_SIZE]
			= { "CUTTER_DIAM","LENGTH","POINT_ANGLE"};
    double par_vals[NUM_PARAMS];
    int i;

    par_vals[0] = tool_diam; 
    par_vals[1] = tool_length; 
    par_vals[2] = point_angle;

/*--------------------------------------------------------------------*\
    Allocate the input structure
\*--------------------------------------------------------------------*/
    status = ProToolinputAlloc(&tool_input);
    ERROR_CHECK("UserParamToolCreate", "ProToolinputAlloc", status);
    
    if (status == PRO_TK_NO_ERROR)
    {
/*--------------------------------------------------------------------*\
	Set the type
\*--------------------------------------------------------------------*/
        status = ProToolinputTypeSet(tool_input, tool_type);
	ERROR_CHECK("UserParamToolCreate", "ProToolinputTypeSet", status);
	
	if (status != PRO_TK_NO_ERROR)
	    return status;
	
/*--------------------------------------------------------------------*\
	Add tool parameters
\*--------------------------------------------------------------------*/
        status = ProElementAlloc(PRO_E_PARAMS, &tool_elem);
	ERROR_CHECK("UserParamToolCreate", "ProElementAlloc", status);
	
        param_val.type = PRO_PARAM_DOUBLE;
	
	for ( i = 0; i < NUM_PARAMS; i++)
	{
            param_val.value.d_val = par_vals[i];
	    status = ProToolElemParamAdd(tool_elem, &param_val, par_name[i]); 
	    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);
	
    	if (status == PRO_TK_NO_ERROR)
	{
	    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;
}

