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


/*---------------------- Pro/Toolkit Includes ------------------------*/
#include "ProToolkit.h"
#include "ProMdl.h"
#include "ProFeature.h"
#include "ProMenu.h"
#include "ProSolid.h"
#include "ProArray.h"
#include "ProFeatType.h"
#include "ProUtil.h"


/*---------------------- Application Includes ------------------------*/
#include "TestError.h"

/*---------------------- Function Prototypes -------------------------*/
int UserFeatCollectExample();
int UserFeatureCollect(ProFeature **fArray);
ProError FeatFilterAction(ProFeature* pFeature, ProAppData aData);
ProError FeatVisitAction(ProFeature* pFeature, ProError eStatus, ProAppData aData); 

/* ========================================================================== *\
	Function: 	UserButtonAction() 
	Purpose:	Write feature type and id to file on disk and diplays in Info
				Window
\* ========================================================================== */

int UserFeatCollectExample()
{
	ProFeature *fList;
	ProFeattype fType;
	int status, eStatus, aSize, i;
	FILE *fp;
	ProPath	w_filename;
	
	status = ProArrayAlloc(0, sizeof(ProFeature), 1, (ProArray *)&fList);
	ERROR_CHECK("UserButtonAction", "ProArrayAlloc", status);
	eStatus = UserFeatureCollect(&fList);

	fp = PTApplsUnicodeFopen("fCollect.txt", "w");

	status = ProArraySizeGet(fList, &aSize);
	ERROR_CHECK("UserButtonAction", "ProArraySizeGet", status);
	
	for ( i = 0; i < aSize; i++)
	{
		status = ProFeatureTypeGet(&fList[i], &fType);
		ProTKFprintf(fp, "\nFeature type:  %d\tFeature id:  %d\n", fType, fList[i].id);
	}
	eStatus = fclose(fp);

	ProStringToWstring(w_filename, "fCollect.txt");
	status = ProInfoWindowDisplay(w_filename, NULL, NULL);

	status = ProArrayFree((ProArray*)&fList);
	ERROR_CHECK("UserButtonAction", "ProArrayFree", status);

	return(eStatus);
}


/* ========================================================================== *\
	Function: 	UserFeatureCollect() 
	Purpose:	Visit all features in part
\* ========================================================================== */

int UserFeatureCollect(ProFeature **fArray)
{
	ProMdl	model;
	int status;

	status = ProMdlCurrentGet(&model);
	ERROR_CHECK("UserFeatureCollect", "ProMdlCurrentGet", status);


	status = ProSolidFeatVisit((ProSolid)model, FeatVisitAction, FeatFilterAction, (ProAppData)fArray);
	ERROR_CHECK("UserFeatureCollect", "ProSolidFeatureVisit", status);

	return(status);
}	

/* ========================================================================== *\
	Function: 	FeatFilterAction() 
	Purpose:	Eliminates parts that are not visible from visit action
\* ========================================================================== */
ProError FeatFilterAction(ProFeature* pFeature, ProAppData aData)
{

	int status;
	ProBoolean iVisible;

	status = ProFeatureVisibilityGet(pFeature, &iVisible);
	ERROR_CHECK("FeatFilterAction", "ProFeatureVisibilityGet", status);

	if (iVisible == PRO_B_TRUE)
		return(PRO_TK_NO_ERROR);
	else
		return(PRO_TK_CONTINUE);

}


/* ========================================================================== *\
	Function: 	FeatVisitAction() 
	Purpose:	Collects feature handles in expandable array
\* ========================================================================== */
ProError FeatVisitAction(ProFeature *pFeature, ProError eStatus, ProAppData aData)
{
	int	status;

	status = ProArrayObjectAdd((ProArray *)aData, PRO_VALUE_UNUSED, 1, pFeature);	
	ERROR_CHECK("FeatVisitAction", "ProArrayObjectAdd", status);

	return(status); 
}