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


/*----------------------------------------------------------------------------*\
Pro/TOOLKIT includes
\*----------------------------------------------------------------------------*/
#include <ProToolkit.h>
#include <ProSelection.h>
#include <ProWeld.h>
#include <ProMenu.h>

/*----------------------------------------------------------------------------*\
Application includes
\*----------------------------------------------------------------------------*/
#include "UtilMenu.h"
#include "UtilFiles.h"
#include "UtilMessage.h"
#include "UtilString.h"
#include <PTApplsUnicodeUtils.h>
#include <ProTKRunTime.h>
#include <ProUtil.h>

/*----------------------------------------------------------------------------*\
Application macros
\*----------------------------------------------------------------------------*/
#define  WELD_INFO      0
#define  PARAM_INFO     1

#define  WELD_QCR       ".wld"
#define  PARAM_QCR      "_param.wld"

/*----------------------------------------------------------------------------*\
Application prototypes
\*----------------------------------------------------------------------------*/
char* ProTestWeldTypeGet (ProWeldtype );
char* ProTestWeldSubtypeGet (ProWeldsubtype );
ProError    ProTestWeldIntermittencePrint (ProFeature *, ProStandard, FILE *);

/*============================================================================*\
    FUNCTION :	ProTestWeldMenu()
    PURPOSE  :	Create a weld Menu.
\*============================================================================*/
int ProTestWeldMenu ()
{
    ProError err;
    int menu_id, action;
    int ProTestWeldAct();
    
    err= ProMenuFileRegister( (char *)"TkWelding", (char *)"tkwelding.mnu", 
	&menu_id );
    TEST_CALL_REPORT( "ProMenuFileRegister()", "ProTestWeldMenu()",
    	err, err!= PRO_TK_NO_ERROR );
    ProMenubuttonActionSet( (char *)"TkWelding", (char *)"WeldInfo",
        (ProMenubuttonAction)ProTestWeldAct, NULL,  0 );
    ProMenubuttonActionSet( (char *)"TkWelding", (char *)"TkWelding",
    	(ProMenubuttonAction)ProMenuDelete, NULL,  0 );
    err= ProMenuCreate( PROMENUTYPE_MAIN,(char *)"TkWelding", &menu_id );
    TEST_CALL_REPORT( "ProMenuCreate()", "ProTestWeldMenu()",
        err, err!= PRO_TK_NO_ERROR );
   ProMenuProcess( (char *)"TkWelding", &action );
   
   return (0);  

}

/*============================================================================*\
    FUNCTION :	ProTestWeldMenu()
    PURPOSE  :	Get options for testing
\*============================================================================*/
int ProTestWeldAct ()
{
    int     	    	id, num_sel;
    ProError	    	err;
    ProUtilMenuButtons  buttons[] = {
        {"WeldInfo", 0, TEST_CALL_PRO_MENU_DELETE},
        {"Weld", WELD_INFO, 0},
        {"Parameters", PARAM_INFO, 0},
        {"", 0, 0}
        };
    ProSelection    	*sel = NULL;
    ProModelitem    	model_item;
    int ProTestWeldParam (ProFeature *weld);
    int ProTestWeldInfo (ProFeature *weld);

    if (ProUtilMenuIntValueSelect( buttons, &id) != PRO_TK_NO_ERROR)
        return -1;

    while (1)
    {
    	ProUtilMsgPrint ("gen", "TEST %0s", "Select weld.");
    
    	if (ProSelect((char *)"feature", 1, NULL, NULL, NULL, NULL,
    	    &sel, &num_sel) != PRO_TK_NO_ERROR || sel == NULL)
            return(0);
	
    	ProSelectionModelitemGet(sel[0], &model_item);
	
	err = ProWeldSequenceIdGet ((ProFeature*)&model_item,
	    &num_sel);
	TEST_CALL_REPORT ("ProWeldSequenceIdGet", "ProTestWeldAct",
	    err, err!=PRO_TK_NO_ERROR);

	if (err != PRO_TK_NO_ERROR )
	{
	    ProUtilMsgPrint ("gen", "TEST %0s", "The selected feature is not"
	    	    	    	    	    	" weld. Try again.");
    	    continue;
	}
	break;
    }

    switch (id)
    {
    case WELD_INFO:
	    ProTestWeldInfo ((ProFeature*)&model_item);
            
            break;
    case PARAM_INFO:        
            ProTestWeldParam((ProFeature*)&model_item);
            
            break;
    default:;
    }
    return (0);
}

/*============================================================================*\
    FUNCTION :	ProTestWeldInfo ()
    PURPOSE  :  Store weld information.
\*============================================================================*/
int ProTestWeldInfo (ProFeature *weld)
{
    int     	    sequence_id;
    FILE            *fp = NULL;
    char            c_name[PRO_MDLNAME_SIZE];
    ProMdlName 	    w_name;
    ProError	    err;
    ProWeldtype     type;
    ProWeldsubtype  subtype;
    ProWeldInfo     info;
    ProFeature	    rod; 
    ProUtilCname    fname;
    
    ProTestQcrName(&(weld->owner), (char *)WELD_QCR, fname);
    if ((fp = PTApplsUnicodeFopen (fname, "w")) != NULL)
    {
        err = ProMdlMdlnameGet(weld->owner, w_name);
        ProWstringToString (c_name, w_name);
        ProTKFprintf (fp, "\n\t Weld information \n");
        ProTKFprintf (fp, "\nAssembly Name: %s\n", c_name);
        
        err = ProWeldRodGet (weld, &rod);
	TEST_CALL_REPORT ("ProWeldRodGet", "ProTestWeldInfo",
	    err, err!=PRO_TK_NO_ERROR);
	err = ProWeldRodNameGet (&rod, w_name);
	TEST_CALL_REPORT ("ProWeldRodNameGet", "ProTestWeldInfo",
	    err, err!=PRO_TK_NO_ERROR);
	ProWstringToString (c_name, w_name);
	ProTKFprintf (fp, "Rod : %s\n", c_name);

        
	err = ProWeldTypeGet (weld, &type, &subtype);
	TEST_CALL_REPORT ("ProWeldTypeGet", "ProTestWeldInfo",
	    err, err!=PRO_TK_NO_ERROR);
	ProTKFprintf (fp, "Type %s, Subtype %s \n",
	    ProTestWeldTypeGet (type), ProTestWeldSubtypeGet (subtype));
        
        err = ProWeldSequenceIdGet (weld, &sequence_id);
	TEST_CALL_REPORT ("ProWeldSequenceIdGet", "ProTestWeldInfo",
	    err, err!=PRO_TK_NO_ERROR);
	ProTKFprintf (fp, "Sequ id = %d\n", sequence_id);

    	err = ProWeldInfoGet (weld, &info);
	TEST_CALL_REPORT ("ProWeldInfoGet", "ProTestWeldInfo",
	    err, err!=PRO_TK_NO_ERROR);
	ProTKFprintf (fp, "Size = (%f, %f)\n x_sec_area = %f\n length = %f\n",
	    info.size[0],info.size[1], info.x_sec_area, info.length);
	ProTKFprintf (fp, " volume = %f\n mass = %f\n time = %f\n rod_length = %f",
	    info.volume, info.mass, info.time, info.rod_length);
        
    	fclose (fp);
        ProStringToWstring(w_name, fname);
        ProInfoWindowDisplay(w_name, NULL, NULL); 
    }
    return (0);
}

/*============================================================================*\
    FUNCTION :	ProTestWeldInfo ()
    PURPOSE  :  Store weld specific information.
\*============================================================================*/
int ProTestWeldParam (ProFeature *weld)
{
    ProError err;
    ProWeldFilletdata  fillet ;
    ProWeldGroovedata  groove ;
    ProWeldPlugdata    plug ;
    ProWeldSlotdata    slot ;
    ProWeldSpotdata    spot ;
    ProUtilCname    fname;
    char            c_name[PRO_MDLNAME_SIZE];
    ProMdlName 	    w_name;
    FILE            *fp;
    ProWeldtype     type;
    ProWeldsubtype  subtype;
    int num_belongs = 0;
    ProFeature *belongs = NULL;
    
    ProTestQcrName(&(weld->owner), (char *)PARAM_QCR, fname);
    if ((fp = PTApplsUnicodeFopen (fname, "w")) != NULL)
    {
        err = ProMdlMdlnameGet(weld->owner, w_name);
        ProWstringToString (c_name, w_name);
        ProTKFprintf (fp, "\n\t Weld parameters \n");
        ProTKFprintf (fp, "\nAssembly Name: %s\n", c_name);
	err = ProWeldTypeGet (weld, &type, &subtype);
	TEST_CALL_REPORT ("ProWeldTypeGet", "ProTestWeldParam",
	    err, err!=PRO_TK_NO_ERROR);
	ProTKFprintf (fp, "Type %s, Subtype %s \n",
	    ProTestWeldTypeGet (type), ProTestWeldSubtypeGet (subtype));
        switch(type)
        {
        case  PRO_WELD_FILLET :
            err = ProWeldFilletdataGet(weld, &fillet);
            TEST_CALL_REPORT ("ProWeldFilletdataGet", "ProTestWeldParam",
                err, err!=PRO_TK_NO_ERROR);
            ProTKFprintf (fp, "\n all_around is %s\n trajectory_length = %f", 
                (fillet.all_around == PRO_B_TRUE) ? "true" : "false", 
                fillet.trajectory_length);
            
            break;
        case  PRO_WELD_GROOVE :
            err = ProWeldGroovedataGet(weld, &groove);
            TEST_CALL_REPORT ("ProWeldGroovedataGet", "ProTestWeldParam",
                err, err!=PRO_TK_NO_ERROR);
            ProTKFprintf (fp, "\n butt_width = %f\n face_offset = %f\n"
                "trajectory_length = %f\n penetration = %f", groove.butt_width,
            groove.face_offset, groove.trajectory_length, groove.penetration);
            
            break;
        case  PRO_WELD_PLUG :
            err = ProWeldPlugdataGet(weld, &plug);
            TEST_CALL_REPORT ("ProWeldPlugdataGet", "ProTestWeldParam",
                err, err!=PRO_TK_NO_ERROR);
            ProTKFprintf (fp, "\n depth = %f", plug.depth);    
            
            break;    
        case  PRO_WELD_SLOT :
            err = ProWeldSlotdataGet(weld, &slot);
            TEST_CALL_REPORT ("ProWeldSlotdataGet", "ProTestWeldParam",
                err, err!=PRO_TK_NO_ERROR);
            ProTKFprintf (fp, "\n depth = %f", slot.depth); 
            
            break;             
        case  PRO_WELD_SPOT :
            err = ProWeldSpotdataGet(weld, &spot);
            TEST_CALL_REPORT ("ProWeldSpotdataGet", "ProTestWeldParam",
                err, err!=PRO_TK_NO_ERROR);
            ProTKFprintf (fp, "\n number_of_points = %d\n diameter = %f\n"
                "penetration = %f", spot.number_of_points, spot.diameter, 
                spot.penetration);
            
            break; 
        default:;
        }

	err = ProTestWeldIntermittencePrint (weld, PROSTANDARD_ANSI, fp);
	if (err == PRO_TK_E_NOT_FOUND)
            ProTKFprintf(fp,"\n The weld is not intermittent");
	
	err = ProWeldCompoundGet (weld, &belongs, &num_belongs);
	TEST_CALL_REPORT ("ProWeldCompoundGet", "ProTestWeldParam",
	    err, err!=PRO_TK_NO_ERROR);
	
	if (num_belongs>0)
	{
	    ProTKFprintf (fp, "\n Weld is a compound. Belong features %s",
                num_belongs);
	    err = ProArrayFree ((ProArray*)&belongs);
	}
        else if (err == PRO_TK_E_NOT_FOUND)
            ProTKFprintf(fp,"\n The weld is not compound weld");
    	fclose (fp);
        ProStringToWstring(w_name, fname);
        ProInfoWindowDisplay(w_name, NULL, NULL); 
    }
    return (0);
}

/*============================================================================*\
    FUNCTION :	ProTestWeldTypeGet()
    PURPOSE  :	Convert ProWeldtype to string.
\*============================================================================*/
char* ProTestWeldTypeGet (ProWeldtype     type)
{
    switch (type)
    {
    case PRO_WELD_FILLET:   return (char *)"FILLET";
    case PRO_WELD_GROOVE:   return (char *)"GROOVE";
    case PRO_WELD_PLUG:     return (char *)"PLUG";
    case PRO_WELD_SLOT:     return (char *)"SLOT";
    case PRO_WELD_SPOT:     return (char *)"SPOT";
    }
    return (char *)"Unknown";
}

/*============================================================================*\
    FUNCTION :	ProTestWeldSubtypeGet ()
    PURPOSE  :  Convert ProWeldsubtype to string.
\*============================================================================*/
char* ProTestWeldSubtypeGet (ProWeldsubtype     subtype)
{
    switch (subtype)
    {
    case PRO_WELD_GROOVE_NONE:      	return (char *)"NONE";
    case PRO_WELD_GROOVE_SQUARE:    	return (char *)"SQUARE";
    case PRO_WELD_GROOVE_V:   	    	return (char *)"V";
    case PRO_WELD_GROOVE_BEVEL:     	return (char *)"BEVEL";
    case PRO_WELD_GROOVE_U:   	    	return (char *)"U";
    case PRO_WELD_GROOVE_J:   	    	return (char *)"J";
    case PRO_WELD_GROOVE_FLARED_V:  	return (char *)"FLARED V";
    case PRO_WELD_GROOVE_FLARED_BEVEL:	return (char *)"FLARED BEVEL";
    }
    return (char *)"Unknown";
}

/*============================================================================*\
    FUNCTION :	ProTestWeldIntermittencePrint()
    PURPOSE  :	Print weld intermittence information.
\*============================================================================*/
ProError   ProTestWeldIntermittencePrint (
    ProFeature *weld,
    ProStandard standard,
    FILE* fp)
{
    ProError	err;
    ProWeldIntermittence ince;
    err = ProWeldIntermittenceGet (weld, standard,
	    &ince);
	TEST_CALL_REPORT ("ProWeldIntermittenceGet",
	    "ProTestWeldIntermittencePrint",
	    err, err!=PRO_TK_NO_ERROR);
    if(err != PRO_TK_NO_ERROR)
        return(err);
    ProTKFprintf (fp, "\n Intermittence in %s standard:\n",
    	(standard == PROSTANDARD_ANSI)?"ANSI":"ISO");

    ProTKFprintf (fp, " angular is %s\n",
    	(ince.angular == PRO_B_TRUE) ? "true" : "false");
    ProTKFprintf (fp, " at_center is %s\n",
    	(ince.at_center == PRO_B_TRUE) ? "true" : "false");
    ProTKFprintf (fp, " length = %f\n pitch = %f\n number_of_welds = %d\n",
    	ince.length, ince.pitch, ince.number_of_welds);
    return(err); 
}