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


/*--------------------------------------------------------------------*\
Pro/TOOLKIT includes
\*--------------------------------------------------------------------*/
#include <ProToolkit.h>
#include <ProObjects.h>
#include <ProAssembly.h>
#include <ProAsmcomp.h>
#include <ProElemId.h>
#include <ProElement.h>
#include <ProFeatType.h>
#include <ProMdl.h>
#include <ProMessage.h>
#include <ProModelitem.h>
#include <ProMenu.h>
#include <ProSelection.h>
#include <ProSolid.h>
#include <ProUtil.h>
#include <ProValue.h>
#include <ProSkeleton.h>


/*--------------------------------------------------------------------*\
Application includes
\*--------------------------------------------------------------------*/
#include "TestError.h"
#include "UtilMenu.h"
#include <UtilMessage.h>
/*====================================================================*\
  Function : ProTestMdlGet()
  Purpose  : 
\*====================================================================*/
ProError ProTestMdlGet(ProMdl * mdl)
{
    ProError err;
    wchar_t        wname[PRO_FAMILY_MDLNAME_SIZE];
    char           cname [PRO_FAMILY_MDLNAME_SIZE];
    char * c_pnt;
    ProMdlfileType type = PRO_MDLFILE_PART;
    
    ProUtilMsgPrint("gen", "TEST %0s", "Enter model name (<name>.prt or <name>.asm) : ");
    if(!ProUtilStringGet(wname, NULL, PRO_LINE_SIZE))
    {
	*mdl = NULL;
	return PRO_TK_NO_ERROR;
    }

    ProWstringToString(cname, wname);

    if ((c_pnt = strstr (cname , ".asm")) != NULL)
	type = PRO_MDLFILE_ASSEMBLY;
    else
	if ((c_pnt = strstr (cname , ".prt")) != NULL)
	    type = PRO_MDLFILE_PART;
    else
	return PRO_TK_BAD_INPUTS;
    *c_pnt = '\0';
    ProStringToWstring(wname, cname);

    err = ProMdlnameRetrieve ( wname, type, mdl);
    TEST_CALL_REPORT("ProMdlnameRetrieve()", "ProTestMdlGet()",
				  err, err != PRO_TK_NO_ERROR); 
    
    return err;
}
/*====================================================================*\
  Function : ProTestSkeletonCreate()
  Purpose  : 
\*====================================================================*/

ProError ProTestSkeletonCreate (ProAssembly curent_ass, int action)
{
    ProMdl skel_template;
    ProMdl skel;
    ProError err;
    wchar_t      wname[PRO_MDLNAME_SIZE];

	/*--------*/
    ProModelitem mi;
    ProMdlName nm;

    err = ProTestMdlGet (& skel_template);
    if (err != PRO_TK_NO_ERROR)
	return PRO_TK_NO_ERROR;
    ProUtilMsgPrint("gen", "TEST %0s", "Enter a skeleton name : ");
    if(!ProUtilStringGet(wname, NULL, PRO_LINE_SIZE))
    {
	return PRO_TK_NO_ERROR;
    }

    err = ProMdlToModelitem (skel_template, &mi);

    err = ProMdlMdlnameGet (skel_template, nm);
    
    err = ProMdlMdlnameGet (curent_ass, nm);
 
    err = ProAsmSkeletonMdlnameCreate (curent_ass, wname, skel_template, &skel);
    TEST_CALL_REPORT("ProAsmSkeletonMdlnameCreate()", "ProTestSkeletonCreate()",
				  err, err != PRO_TK_NO_ERROR);
      
    return err;
}

/*====================================================================*\
  Function : ProTestCopyCreate()
  Purpose  : 
\*====================================================================*/

ProError ProTestCopyCreate (ProAssembly curent_ass, int action)
{
    ProMdl pt_template;
    ProMdlType comp_type;
    ProBoolean leave_unpl;
    ProAsmcomp new_comp;
    ProError err;
    wchar_t      wname[PRO_MDLNAME_SIZE];


    wchar_t * wnames [3];
    int i, dummy; 

    err = ProTestMdlGet (& pt_template);
    if (err != PRO_TK_NO_ERROR)
	return PRO_TK_NO_ERROR;
    
    ProUtilMsgPrint("gen", "TEST %0s", "Enter a component name : ");
    if(!ProUtilStringGet(wname, NULL, PRO_LINE_SIZE))
    {
	return PRO_TK_NO_ERROR;
    }

    for (i=0; i<3; i++)
    {
	wnames[i] = (wchar_t*)calloc(PRO_NAME_SIZE, sizeof(wchar_t));
    }

    ProStringToWstring(wnames[0], (char *)"Part");
    ProStringToWstring(wnames[1], (char *)"Assembly");
    ProStringToWstring(wnames[2], (char *)"");
    
    err = ProMenuFromStringsRegister((char *)"Model Type", NULL, wnames, NULL, 
	    NULL, &dummy);

    ProMenubuttonActionSet( (char *)"Model Type", (char *)"Part", 
	    (ProMenubuttonAction)ProUtilAssign, (ProAppData)&comp_type ,
	PRO_PART);
    ProMenubuttonActionSet( (char *)"Model Type", (char *)"Assembly", 
	    (ProMenubuttonAction)ProUtilAssign,(ProAppData)&comp_type  ,
	PRO_ASSEMBLY);
    ProMenubuttonActionSet( (char *)"Model Type", (char *)"Model Type", 
       	    (ProMenubuttonAction)ProMenuDelete, NULL, 0);

    ProMenuCreate(PROMENUTYPE_MAIN, (char *)"Model Type", NULL);
    ProMenuProcess((char *)"Model Type", &i);

    ProStringToWstring(wnames[0], (char *)"Unplaced");
    ProStringToWstring(wnames[1], (char *)"Default");
    
    err = ProMenuFromStringsRegister((char *)"Placment Type", NULL, wnames, NULL, 
	    NULL, &dummy);

    ProMenubuttonActionSet( (char *)"Placment Type", (char *)"Unplaced", 
	    (ProMenubuttonAction)ProUtilAssign, (ProAppData)&leave_unpl , PRO_B_TRUE);
    ProMenubuttonActionSet( (char *)"Placment Type", (char *)"Default", 
	    (ProMenubuttonAction)ProUtilAssign,(ProAppData)&leave_unpl  , PRO_B_FALSE);
     ProMenubuttonActionSet( (char *)"Placment Type", (char *)"Placment Type", 
       	    (ProMenubuttonAction)ProMenuDelete, NULL, 0);

    ProMenuCreate(PROMENUTYPE_MAIN, (char *)"Placment Type", NULL);
    ProMenuProcess((char *)"Placment Type", &i);

	
    err = ProAsmcompMdlnameCreateCopy (curent_ass, wname, comp_type, pt_template,
	leave_unpl, &new_comp);
    TEST_CALL_REPORT("ProAsmcompMdlnameCreateCopy()","ProTestCopyCreate()",
				  err, err != PRO_TK_NO_ERROR);
	

    for (i=0; i<3; i++)
    {
	free (wnames [i]);
    }

    
    return err;
}
/*====================================================================*\
  Function : ProTestSkelCopyCreate()
  Purpose  : 
\*====================================================================*/
int ProTestSkelCopyCreate(ProAssembly curent_ass)
{
    char * cnames [] =
    {
	"Create skeleton",
	"Create Copy",
	""
    };
    wchar_t * wnames [3];
    int i, dummy;
    ProError err;

    for (i=0; i<3; i++)
    {
	wnames[i] = (wchar_t*)calloc(PRO_NAME_SIZE, sizeof(wchar_t));
	ProStringToWstring(wnames[i], cnames[i]);
    }
    err = ProMenuFromStringsRegister((char *)"TkAsmcompCreate", NULL, wnames, NULL, 
	    NULL, &dummy);
    TEST_CALL_REPORT("ProMenuFromStringsRegister()", "ProTestSkelCopyCreate()",
	    err, err != PRO_TK_NO_ERROR);
    for (i=0; i<3; i++)
    {
	free (wnames [i]);
    }

    ProMenubuttonActionSet( (char *)"TkAsmcompCreate", (char *)"Create skeleton", 
	    (ProMenubuttonAction)ProTestSkeletonCreate, curent_ass, 0);
    ProMenubuttonActionSet( (char *)"TkAsmcompCreate", (char *)"Create Copy", 
	    (ProMenubuttonAction)ProTestCopyCreate, curent_ass, 0);
    ProMenubuttonActionSet( (char *)"TkAsmcompCreate", (char *)"TkAsmcompCreate", 
       	    (ProMenubuttonAction)ProMenuDelete, NULL, 0);

    ProMenuCreate(PROMENUTYPE_MAIN, (char *)"TkAsmcompCreate", NULL);
    ProMenuProcess((char *)"TkAsmcompCreate", &i); 
    
    return (0);
}
/*====================================================================*\
  Function : ProTestAsmSkeletonAdd()
  Purpose  : 
\*====================================================================*/
ProError ProTestAsmSkeletonAdd (ProAssembly curent_ass, int action)
{
    ProMdl asm_template;
    ProError err;

    err = ProTestMdlGet (& asm_template);
    if (err != PRO_TK_NO_ERROR ||asm_template == NULL )
	return PRO_TK_NO_ERROR;

	 
    err = ProAsmSkeletonAdd (curent_ass, asm_template);
    TEST_CALL_REPORT("ProAssmSkeletonAdd()", "ProTestAsmSkeletonAdd()",
	    err, err != PRO_TK_NO_ERROR);
  
    return err;
}

/*====================================================================*\
  Function : ProTestAsmSkeletonDelete()
  Purpose  : 
\*====================================================================*/
ProError ProTestAsmSkeletonDelete (ProAssembly curent_ass, int action)
{

    ProError err;
 
    err = ProAsmSkeletonDelete (curent_ass);
    TEST_CALL_REPORT("ProAsmSkeletonDelete()", "ProTestAsmSkeletonDelete()",
	    err, err != PRO_TK_NO_ERROR);
	
    return err;
}
/*====================================================================*\
  Function : ProTestAsmSkeletonSave()
  Purpose  : 
\*====================================================================*/
ProError ProTestAsmSkeletonSave (ProAssembly curent_ass, int action)
{
    ProMdl skeleton;
    ProError err;
 
    err = ProAsmSkeletonGet (curent_ass, &skeleton);
    TEST_CALL_REPORT("ProAsmSkeletonGet()", "ProTestAsmSkeletonSave()",
	    err, err != PRO_TK_NO_ERROR);
    if (err != PRO_TK_NO_ERROR)
         return err;

    err = ProMdlSave (skeleton);
    TEST_CALL_REPORT("ProMdlSave()", "ProTestAsmSkeletonSave()",
	    err, err != PRO_TK_NO_ERROR);
	
    return err;
    
}


/*====================================================================*\
  Function : ProTestAsmcompFill()
  Purpose  : 
\*====================================================================*/
ProError ProTestAsmcompFill  (ProAssembly curent_ass, int action)
{
    ProError ProUtilAsmcompSelect(ProAsmcomp *p_asmcomp);
    
    ProMdl asm_template;
    ProError err;
    ProAsmcomp asmcomp;

    err = ProTestMdlGet (& asm_template);
    if (err != PRO_TK_NO_ERROR )
	return PRO_TK_NO_ERROR;
    err = ProUtilAsmcompSelect (&asmcomp);
    if (err != PRO_TK_NO_ERROR)
	return PRO_TK_NO_ERROR;
    
 
    err = ProAsmcompFillFromMdl (&asmcomp, asm_template);
     TEST_CALL_REPORT("ProAsmcompFillFromMdl()", "ProTestAsmSkeletonSave()",
	    err, err != PRO_TK_NO_ERROR);
	 
    
    return err;
    
}


/*====================================================================*\
  Function : ProTestCompSkelReplace()
  Purpose  : 
\*====================================================================*/
int ProTestCompSkelReplace (ProAssembly curent_ass)
{
      char * cnames [] =
    {
	"Skeleton Add",
	"Skeleton Delete",
	"Skeleton Save",
	"Fill from Model",
	""
    };
    wchar_t * wnames [sizeof (cnames)/sizeof (char*)];
    int i, dummy;
    ProError err;

    for (i=0; i<sizeof (cnames)/sizeof (char*); i++)
    {
	wnames[i] = (wchar_t*)calloc(PRO_NAME_SIZE, sizeof(wchar_t));
	ProStringToWstring(wnames[i], cnames[i]);
    }
    err = ProMenuFromStringsRegister((char *)"TkAsmcompReplace", NULL, wnames, NULL, 
	    NULL, &dummy);
    TEST_CALL_REPORT("ProMenuFromStringsRegister()", "ProTestSkelCopyCreate()",
	    err, err != PRO_TK_NO_ERROR);
    for (i=0; i<sizeof (cnames)/sizeof (char*); i++)
    {
	free (wnames [i]);
    }

    ProMenubuttonActionSet( (char *)"TkAsmcompReplace", (char *)"Skeleton Add", 
	    (ProMenubuttonAction)ProTestAsmSkeletonAdd, curent_ass, 0);
    ProMenubuttonActionSet( (char *)"TkAsmcompReplace", (char *)"Skeleton Delete", 
	    (ProMenubuttonAction)ProTestAsmSkeletonDelete, curent_ass, 0);
    ProMenubuttonActionSet( (char *)"TkAsmcompReplace", (char *)"Skeleton Save", 
	    (ProMenubuttonAction)ProTestAsmSkeletonSave, curent_ass, 0);
    ProMenubuttonActionSet( (char *)"TkAsmcompReplace", (char *)"Fill from Model", 
	    (ProMenubuttonAction)ProTestAsmcompFill, curent_ass, 0); 
    ProMenubuttonActionSet( (char *)"TkAsmcompReplace", (char *)"TkAsmcompReplace", 
       	    (ProMenubuttonAction)ProMenuDelete, NULL, 0);

    ProMenuCreate(PROMENUTYPE_MAIN, (char *)"TkAsmcompReplace", NULL);
    ProMenuProcess((char *)"TkAsmcompReplace", &i); 
    

    return (0);
}