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

#include <ProToolkit.h>
#include <ProMfg.h>
#include <ProSolid.h>
#include <ProAsmcomppath.h>
#include <ProFeatType.h>
#include <ProUtil.h>

#include <TestError.h>

ProLine  msg_fil17;
#define MSG_FIL ProStringToWstring(msg_fil17,"msg_ugmfg.txt")

/*+++++++++++++++++UG_ENTRY_STARTS_HERE++++++++++++++++++++++*/

/*====================================================================*\
Function : UserMfgWcellCollect
Purpose  : Add workcell feature pointer to array of workcells
\*====================================================================*/
ProError UserMfgWcellCollect (
    ProFeature *wcell,
    ProError    err,
    ProAppData  wcell_array )
{
    err = ProArrayObjectAdd((ProArray*)wcell_array, PRO_VALUE_UNUSED,
						    1, (void*)wcell);
    ERROR_CHECK("UserMfgWcellCollect","ProArrayObjectAdd",err);
    return err;
}

/*====================================================================*\
Function : UserFeatNonWcellFilter
Purpose  : Filter out non workcell features
\*====================================================================*/
ProError  UserFeatNonWcellFilter (
    ProFeature *feature,
    ProAppData  unused )
{
    ProError    status = PRO_TK_NO_ERROR;
    ProFeattype feat_type;
    
    status = ProFeatureTypeGet(feature, &feat_type);
    ERROR_CHECK("UserFeatNonWcellFilter","ProFeatureTypeGet",status);

    if ( status != PRO_TK_NO_ERROR || feat_type != PRO_FEAT_WORKCELL)
	return PRO_TK_CONTINUE;   /* Don't visit this item */
    else
	return PRO_TK_NO_ERROR;   /* Do visit this one!    */
}


/*====================================================================*\
Function : UserMfgWcellList
Purpose  : Collects an array of Workcells that belong to a manufacturing
           model.
\*====================================================================*/
int UserMfgWcellList ()
{
    ProError      err  = PRO_TK_NO_ERROR;
    ProFeature   *wcell_array = (ProFeature*)NULL;
    ProMdl	  mfg_model;
    ProSolid      mfg_solid;
    int           num_wcells = -1;

    err = ProMdlCurrentGet(&mfg_model);
    ERROR_CHECK("UserMfgWcellList","ProMdlCurrentGet",err);

    err = ProArrayAlloc(0, sizeof(ProFeature*), 1, (ProArray*)&wcell_array); 
    ERROR_CHECK("UserMfgWcellList","ProArrayAlloc",err);
    
    if (err == PRO_TK_NO_ERROR)
    {
	err = ProMfgSolidGet ((ProMfg)mfg_model, &mfg_solid);
	ERROR_CHECK("UserMfgWcellList","ProMfgSolidGet",err);

        err = ProSolidFeatVisit(mfg_solid, UserMfgWcellCollect, 
			UserFeatNonWcellFilter, (ProAppData)&wcell_array ); 
	ERROR_CHECK("UserMfgWcellList","ProSolidFeatVisit",err);

	if (err == PRO_TK_NO_ERROR)
	{
	    err = ProArraySizeGet((ProArray)wcell_array, &num_wcells);
	    ERROR_CHECK("UserMfgWcellList","ProSolidFeatVisit",err);

	    if (err == PRO_TK_NO_ERROR )
	    {
		err = ProMessageDisplay(MSG_FIL, 
    		"USER There are %0d workcells in this model", &num_wcells );
		ERROR_CHECK("UserMfgWcellList","ProMessageDisplay",err);
	    }
	}

        err = ProArrayFree((ProArray*)&wcell_array);
	ERROR_CHECK("UserMfgWcellList","ProArrayFree",err);
    }

    if ( num_wcells == -1 )
    {
	err = ProMessageDisplay(MSG_FIL,
		"USER Error determining the number of workcells");
	ERROR_CHECK("UserMfgWcellList","ProMessageDisplay",err);

    }

    return ((int)err);
}

/*++++++++++++++++++++++++++++++++UG_ENTRY_ENDS_HERE++++++++++*/

#undef MSG_FIL