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

#include <ProToolkit.h>
#include <ProMfg.h>
#include <ProMdl.h>
#include <ProTool.h>
#include <ProToolinput.h>
#include <ProToolElem.h>
#include <ProParamval.h>

#include <TestError.h>

/*===========================================================*\
  Function : UserSolidToolCreate
  Purpose  : Creates a tool from a tool part.
\*===========================================================*/
ProErr UserSolidToolCreate (
    ProMfg         mfg_model,
    ProToolType    tool_type,
    wchar_t       *tool_id,
    wchar_t       *part_name,
    ProTool       *tool)
{
    ProToolElem     tool_elem;
    ProToolinputPtr tool_input;
    ProMdl          tool_part;
    ProMdlfileType  mdl_type = PRO_MDLFILE_PART;
    ProErr          status;
    ProErrorlist    errors;

    status = ProMdlnameRetrieve (part_name, mdl_type, &tool_part);
    ERROR_CHECK("UserSolidToolCreate", "ProMdlnameRetrieve", status);
    if (status != PRO_TK_NO_ERROR)
        return status;

    status = ProToolinputAlloc (&tool_input);
    ERROR_CHECK("UserSolidToolCreate", "ProToolinputAlloc", status);
    if (status == PRO_TK_NO_ERROR)
    {
/*--------------------------------------------------------------------*\
	Set the tool type.
\*--------------------------------------------------------------------*/
	status = ProToolinputTypeSet (tool_input, tool_type);
	ERROR_CHECK("UserSolidToolCreate", "ProToolinputTypeSet",status);
	if (status == PRO_TK_NO_ERROR)
	    return status;

/*--------------------------------------------------------------------*\
        Allocate the space for the tool model and to the tool_input.
\*--------------------------------------------------------------------*/
	status = ProElementAlloc(PRO_E_TOOL_MODEL, &tool_elem);
	ERROR_CHECK("UserSolidToolCreate", "ProElementAlloc",status);

	if (status == PRO_TK_NO_ERROR)
        {
	    status = ProToolElemModelSet (tool_elem, &tool_part);
	    ERROR_CHECK("UserSolidToolCreate", "ProToolElemModelSet",status);
	}

	if (status == PRO_TK_NO_ERROR)
        {
	    status = ProToolinputElemAdd (tool_input, tool_elem);
	    ERROR_CHECK("UserSolidToolCreate", "ProToolinputElemAdd",status);
        }

/*--------------------------------------------------------------------*\
        Create the tool.
\*--------------------------------------------------------------------*/
        if (status == PRO_TK_NO_ERROR)
        {
            status = ProToolInit (tool_id, mfg_model, tool);
	    ERROR_CHECK("UserSolidToolCreate", "ProToolInit",status);
            if (status == PRO_TK_NO_ERROR)
	    {
		status = ProToolCreate (tool, tool_input, &errors);
		ERROR_CHECK("UserSolidToolCreate","ProToolCreate",status);
	    }
    	}
/*--------------------------------------------------------------------*\
        Free the input structure
\*--------------------------------------------------------------------*/
        status = ProToolinputFree (&tool_input);
	ERROR_CHECK("UserSolidToolCreate","ProToolinputFree",status);
         
    }
    return status;
}