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



/*--------------------------------------------------------------------*\
Pro/TOOLKIT includes
\*--------------------------------------------------------------------*/
#include "ProToolkit.h"
#include "ProGeomitem.h"
#include "ProMenu.h"
#include "ProMdl.h"
#include "ProSelection.h"
#include "ProUtil.h"
/*--------------------------------------------------------------------*\
C System includes
\*--------------------------------------------------------------------*/

/*--------------------------------------------------------------------*\
Application includes
\*--------------------------------------------------------------------*/
#include "TestError.h"
#include "TestFiletypes.h"
#include "UtilFiles.h"
#include "UtilMessage.h"
#include <PTApplsUnicodeUtils.h>
#include <ProTKRunTime.h>
#include <ProSurface.h>
/*--------------------------------------------------------------------*\
Application macros
\*--------------------------------------------------------------------*/

#define  USER_ANGLE_EVAL     0
#define  USER_DISTANCE_EVAL  1
#define  USER_DIAMETER_EVAL  2

/*====================================================================*\
    FUNCTION :	ProTestMeasureMenu()
    PURPOSE  :	Commands for testing Geomitem...Eval in Pro/TOOLKIT
\*====================================================================*/
int ProTestMeasureMenu()
{
    ProMdl model;
    ProError status;
    int action, menu_id;
    int ProTestMeasure(ProAppData, int);

    status = ProMdlCurrentGet(&model);
    TEST_CALL_REPORT("ProMdlCurrentGet()", "ProTestMeasureMenu()", status,
						status != PRO_TK_NO_ERROR);

    status = ProMenuFileRegister((char*)"TkMeasure", (char*)"tkmeasure.mnu", &menu_id);
    TEST_CALL_REPORT("ProMenuFileRegister()", "ProTestMeasureMenu()",
				    status, status != PRO_TK_NO_ERROR);
    status = ProMenubuttonActionSet((char*)"TkMeasure", (char*)"Angle", (ProMenubuttonAction)ProTestMeasure,
                                     model, USER_ANGLE_EVAL);
    TEST_CALL_REPORT("ProMenubuttonActionSet()", "ProTestMeasureMenu()",
				    status, status != PRO_TK_NO_ERROR);
    status = ProMenubuttonActionSet((char*)"TkMeasure", (char*)"Distance", (ProMenubuttonAction)ProTestMeasure,
                                     model, USER_DISTANCE_EVAL);
    TEST_CALL_REPORT((char*)"ProMenubuttonActionSet()", (char*)"ProTestMeasureMenu()",
				    status, status != PRO_TK_NO_ERROR);
    status = ProMenubuttonActionSet((char*)"TkMeasure", (char*)"Diameter", (ProMenubuttonAction)ProTestMeasure,
                                     model, USER_DIAMETER_EVAL);
    TEST_CALL_REPORT("ProMenubuttonActionSet()", "ProTestMeasureMenu()",
				    status, status != PRO_TK_NO_ERROR);
    status = ProMenubuttonActionSet((char*)"TkMeasure", (char*)"TkMeasure", 
			(ProMenubuttonAction)ProMenuDelete, NULL, 0);
    TEST_CALL_REPORT("ProMenubuttonActionSet()", "ProTestMeasureMenu()",
				    status, status != PRO_TK_NO_ERROR);
    /* Display Menu and set it into action. */
    status = ProMenuCreate(PROMENUTYPE_MAIN, (char*)"TkMeasure", &menu_id);
    TEST_CALL_REPORT("ProMenuCreate()", "ProTestMeasureMenu()",
                      status, status != PRO_TK_NO_ERROR);
    status = ProMenuProcess((char*)"TkTkMeasure", &action);
    TEST_CALL_REPORT("ProMenuProcess()", "ProTestMeasureMenu()",
		status, status != PRO_TK_NO_ERROR && status != PRO_TK_E_FOUND);

    return(1);
}

/*====================================================================*\
    FUNCTION :	ProTestMeasure
    PURPOSE  :	Commands for testing Geomitem...Eval in Pro/TOOLKIT
\*====================================================================*/
int ProTestMeasure(ProAppData app_data, int option)
{
    ProMdl model = (ProMdl) app_data;
    ProError status;
    int allow, p_count;
    ProCharLine string;
    char *ch;
    double dist, diam;
    ProSelection *p_sel_arr;
    ProModelitem mod_item;
    ProSurface surf;
    ProUvParam uv_par;
    ProCharLine fname;
    FILE *fp;

    ProTestQcrName(&model, (char*)".msr", fname);
    if ( (fp = PTApplsUnicodeFopen(fname,"a")) == NULL )
           return(-1);

    dist = 0.0;
    switch(option)
    {
	case  USER_ANGLE_EVAL:
	    ch = (char*)"edge";
	    allow =2;
	    break;
        case  USER_DISTANCE_EVAL:
	    ch =(char*)"surface,point,axis";
	    allow = 2;
	    break;
        case  USER_DIAMETER_EVAL:
	    ch = (char*)"surface";
	    allow = 1;
	    break;
	default:
	    return (-1);
    }

    /*  Get the user's selection  */
    ProUtilMsgPrint("gen", "TEST %0s", "Select a geometry");
    status = ProSelect(ch, allow, NULL, NULL, NULL, NULL,
		&p_sel_arr, &p_count);
    TEST_CALL_REPORT("ProSelect()", "ProTestMeasure()",
                      status, status != PRO_TK_NO_ERROR);
    if (status != PRO_TK_NO_ERROR || p_count < allow)
	return (0);

    switch (option)
    {
	case  USER_ANGLE_EVAL:
	    status = ProGeomitemAngleEval(p_sel_arr[0], p_sel_arr[1], &dist);
	    TEST_CALL_REPORT("ProGeomitemAngleEval()", "ProTestMeasure()",
                        status, status != PRO_TK_NO_ERROR);
	    ch = (char*)"Angle";
	    break;
	case  USER_DISTANCE_EVAL:
	    status = ProGeomitemDistanceEval(p_sel_arr[0], p_sel_arr[1], &dist);
	    TEST_CALL_REPORT("ProGeomitemDistanceEval()", "ProTestMeasure()",
                        status, status != PRO_TK_NO_ERROR);
	    ch = (char*)"Distance";
	    break;    
	case  USER_DIAMETER_EVAL:
          status =  ProSelectionModelitemGet(p_sel_arr[0], &mod_item);
          TEST_CALL_REPORT("ProSelectionModelitemGet()", "ProTestMeasure()",
                      status, status != PRO_TK_NO_ERROR);

          status =  ProSelectionUvParamGet(p_sel_arr[0], uv_par);
          TEST_CALL_REPORT("ProSelectionUvParamGet()", "ProTestMeasure()",
                      status, status != PRO_TK_NO_ERROR);

          status = ProGeomitemToSurface((ProGeomitem*)&mod_item, &surf);
          TEST_CALL_REPORT("ProGeomitemToSurface()", "ProTestMeasure()",
                      status, status != PRO_TK_NO_ERROR);

          status = ProSurfaceDiameterEval(surf, uv_par, &diam);
          TEST_CALL_REPORT("ProSurfaceDiameterEval()", "ProTestMeasure()",
                      status, status != PRO_TK_NO_ERROR);

          status = ProGeomitemDiameterEval(p_sel_arr[0], &dist);
          TEST_CALL_REPORT("ProGeomitemDiameterEval()", "ProTestMeasure()",
                      status, status != PRO_TK_NO_ERROR);	    
	  if (dist != diam)
	      ProTKFprintf(fp, "Error: ProSurfaceDiameterEval and "
		  "ProGeomitemDiameterEval return different value!\n");
	  ch = (char*)"Diameter";
	break;
    }
    ProTKSprintf(string, "%s = %7.3f", ch, dist);
    ProUtilMsgPrint("gen", "TEST %0s", string);

    ProTKFprintf(fp, "%s\n", string);
    fclose(fp);
    return (0);
}