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


#include <ProToolkit.h>
#include <ProSizeConst.h>
#include <ProSimprep.h>
#include <ProSimprepdata.h>
#include <ProSelection.h>
#include <ProUtil.h>
#include <ProSolid.h>
#include <ProAsmcomppath.h>
#include <ProMessage.h>

#include <TestError.h>

ProLine msg_fil21;
#define MSG_FIL ProStringToWstring(msg_fil21,"msg_ugsrep.txt")


/*====================================================================*\
Function : UserSimpRepCreate
Purpose  : Create a simple simplified represenation
\*====================================================================*/
int UserSimpRepCreate ()
{
    ProMdl		 owner;
    ProMdlType		 mdl_type;
    ProSelection        *sels;
    ProSimprepAction     sr_action;
    ProSimprepitem       item;
    ProSimprep           simp_rep;
    ProSimprepdata      *data;
    ProAsmcomppath       comp_path;
    ProModelitem         model_item;
    ProError             status = PRO_TK_NO_ERROR;
    wchar_t              wname[PRO_NAME_SIZE];
    char                 selecting[20];
    wchar_t              wselecting[20];
    int                  num, i;

/*--------------------------------------------------------------------*\
    What is going to be selected?
\*--------------------------------------------------------------------*/
    selecting[0] = '\0';


    status = ProMdlCurrentGet(&owner);
    ERROR_CHECK("UserSimpRepCreate","ProMdlCurrentGet",status);
    if (status != 0) return (PRO_TK_GENERAL_ERROR);

    status = ProMdlTypeGet(owner, &mdl_type);
    ERROR_CHECK("UserSimpRepCreate","ProMdlTypeGet",status);

    if (status != PRO_TK_NO_ERROR) return (status);
    else if ( PRO_PART == mdl_type )
	strcpy(selecting, "feature");
    else if ( PRO_ASSEMBLY == mdl_type )
	strcpy(selecting, "part");
    else return (PRO_TK_GENERAL_ERROR);
	
    ProStringToWstring (wselecting, selecting );
    ERROR_CHECK("UserSimpRepCreate","ProStringToWstring",status);
    ProStringToWstring (wname, "SIMP_EXPLE");
    ERROR_CHECK("UserSimpRepCreate","ProStringToWstring",status);

/*--------------------------------------------------------------------*\
    Initialize the simplified rep to include by default and be permanent.
\*--------------------------------------------------------------------*/
    status = ProSimprepdataAlloc(wname,PRO_B_FALSE,PRO_SIMPREP_INCLUDE,&data);
    ERROR_CHECK("UserSimpRepCreate","ProSimprepdataAlloc",status);

    if (PRO_TK_NO_ERROR == status )
    {
	status = ProMessageDisplay(MSG_FIL, 
		"USER Select a %0w to exclude from the simplified rep",
		wselecting );
	ERROR_CHECK("UserSimpRepCreate","ProMessageDisplay",status);
	status = ProSelect (selecting, -1, NULL,NULL,NULL,NULL, &sels, &num);
    	ERROR_CHECK("UserSimpRepCreate","ProSelect",status);

        if (status == PRO_TK_NO_ERROR && num > 0)
        {
/*--------------------------------------------------------------------*\
            Reverse the default rule, so in this case the component or 
            feature will be excluded.
\*--------------------------------------------------------------------*/
            status = ProSimprepActionInit (PRO_SIMPREP_REVERSE,NULL,&sr_action);
	    ERROR_CHECK("UserSimpRepCreate","ProSimprepActionInit",status);

            if (status == PRO_TK_NO_ERROR)
            {
                for (i = 0; i < num; i++)
                {
		    status = ProSelectionAsmcomppathGet(sels[i], &comp_path); 
		    ERROR_CHECK("UserSimpRepCreate",
		    		"ProSelectionAsmcomppathGet",status);

		    status = ProSelectionModelitemGet(sels[i], &model_item);
		    ERROR_CHECK("UserSimpRepCreate",
		    			"ProSelectionModelitemGet",status);
		    
                    status = ProSimprepdataitemInit( comp_path.comp_id_table,
                                comp_path.table_num, model_item.id, &sr_action,
				&item);
		    ERROR_CHECK("UserSimpRepCreate","ProSimprepdataitemInit",
							    	    status);

                    if (status == PRO_TK_NO_ERROR)
		    {
                        status = ProSimprepdataitemAdd (data, &item);
			ERROR_CHECK("UserSimpRepCreate",
					"ProSimprepdataitemAdd",status);

		    }
                }

                status = ProSimprepCreate ((ProSolid)owner, data, &simp_rep);
		ERROR_CHECK("UserSimpRepCreate","ProSimprepCreate", status);
            }

            if (status == PRO_TK_NO_ERROR)
	    {
                status = ProSimprepActivate ((ProSolid)owner, &simp_rep);
		ERROR_CHECK("UserSimpRepCreate","ProSimprepActivate", status);
	    }

/*--------------------------------------------------------------------*\
            Once activated the a call to ProSolidDisplay() must be made
            to display the simplified rep.
\*--------------------------------------------------------------------*/
            status = ProSolidDisplay ((ProSolid)owner);
	    ERROR_CHECK("UserSimpRepCreate","ProSolidDisplay", status);
        }

        status = ProSimprepdataFree (&data);
	ERROR_CHECK("UserSimpRepCreate","ProSimprepdataFree", status);
    }

    return status;
}

#undef MSG_FIL