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


/*------------------------ ProToolkit includes ---------------------------*/
#include <ProToolkit.h>
#include <ProMdl.h>
#include <ProFeature.h>
#include <ProObjects.h>
#include <ProFeatType.h>
#include <ProUtil.h>
#include <PTApplsUnicodeUtils.h>
#include <ProAsmcomp.h>
#include <ProSolid.h>
/*----------------------- Application includes --------------------------*/
#include <TestError.h>

/*------------------------- Global definition ---------------------------*/
#define FILENAME "assembly.lst"

typedef struct user_appdata
{
    FILE *fp;
    int level;
} UserAppdata;


/*------------------------ Function prototypes --------------------------*/
int  UserAsmCompVisit(void *dummy, int dummy2);
ProError UserAsmCompFilter(ProFeature *feature,ProAppData app_data);
ProError user_action(ProFeature *feature,ProError status,ProAppData appdata);

 
/*---------------------------------------------------------------------*\
    Write the information to a file
\*---------------------------------------------------------------------*/

ProError user_action(
    ProFeature *feature,
    ProError status,
    ProAppData appdata)
{
    FILE *fp;
    int l,level;
    ProError err;
    ProMdl mdl;
    char name[PRO_MDLNAME_SIZE];
    char type[PRO_MDLEXTENSION_SIZE];
    wchar_t wname[PRO_NAME_SIZE];
    UserAppdata *appd,appd1;
    ProMdldata mdldata;
	ProMdlName modelName;
	ProMdlExtension modelExtension;

    appd = (UserAppdata *)appdata;
    fp = appd->fp;
    level = appd->level;
    err = ProAsmcompMdlGet(feature , &mdl);
    ERROR_CHECK( "user_action", "ProAsmcompMdlGet", err );

	
	err = ProMdlMdlnameGet(mdl, modelName);
    ERROR_CHECK( "user_action", "ProMdlMdlnameGet", err );
 
	err = ProMdlExtensionGet(mdl, modelExtension);
    ERROR_CHECK( "user_action", "ProMdlExtensionGet", err );


    ProWstringToString(name,modelName);
    ProWstringToString(type,modelExtension);
    for(l=0;l<level;l++)
            ProTKFprintf(fp,"    ");
    ProTKFprintf(fp,"%s	%s\n",name,type);
    if (strncmp(type,"ASM",3) == 0)
    {
	appd1.fp = appd->fp;
	appd1.level = appd->level+1;
        err = ProSolidFeatVisit(mdl, user_action, UserAsmCompFilter, &appd1);
    	ERROR_CHECK( "user_action", "ProSolidFeatVisit", err );
    }
    if (feature != NULL) return(PRO_TK_NO_ERROR);
    return(PRO_TK_CONTINUE);
}

/*---------------------------------------------------------------------*\
Function : to write out the members of the current assembly, and display
 		the result in an information window.
\*---------------------------------------------------------------------*/
int UserAsmCompVisit(void *dummy, int dummy2)
{
    char name[PRO_MDLNAME_SIZE];
    char type[PRO_MDLEXTENSION_SIZE];
    wchar_t wname[PRO_NAME_SIZE];    
    ProError err;
    UserAppdata appdata;
    FILE *fp;
    ProMdl p_asm;
	ProMdlName modelName;
	ProMdlExtension modelExtension;

    err = ProMdlCurrentGet(&p_asm);
    ERROR_CHECK( "UserAsmCompVisit", "ProMdlCurrentGet", err );
/*---------------------------------------------------------------------*\
    Open the text file.
\*---------------------------------------------------------------------*/
    strcpy(name,FILENAME);
    fp = PTApplsUnicodeFopen(name,"w");

	err = ProMdlMdlnameGet(p_asm, modelName);
    ERROR_CHECK( "user_action", "ProMdlMdlnameGet", err );
 
	err = ProMdlExtensionGet(p_asm, modelExtension);
    ERROR_CHECK( "user_action", "ProMdlExtensionGet", err );

    ProWstringToString(name,modelName);
    ProWstringToString(type,modelExtension);
    ProTKFprintf(fp, "%s	%s\n",name,type);
    appdata.fp = fp;
    appdata.level = 1;
/*---------------------------------------------------------------------*\
    List the assembly members
\*---------------------------------------------------------------------*/
    err = ProSolidFeatVisit(p_asm, user_action, UserAsmCompFilter, &appdata);
    ERROR_CHECK( "UserAsmCompVisit", "ProSolidFeatVisit", err );

/*-------- ------------------------------------------------------------*\
    Close the file, and display it.
\*---------------------------------------------------------------------*/
    fclose(fp);
    ProStringToWstring(wname, FILENAME);
    err = ProInfoWindowDisplay(wname, NULL, NULL);
    ERROR_CHECK( "UserAsmCompVisit", "ProInfoWindowDisplay", err ); 
    return(PRO_TK_NO_ERROR);
}

/*====================================================================*\
    FUNCTION :  UserAsmCompFilter()
    PURPOSE  :  A filter used by ProSolidFeatVisit() to visit
                features which are assembly components
\*====================================================================*/
ProError UserAsmCompFilter(
    ProFeature *feature,
    ProAppData app_data)
{
    ProError status;
    ProFeattype ftype;

/*--------------------------------------------------------------------*\
    Get the feature type
\*--------------------------------------------------------------------*/
    status = ProFeatureTypeGet(feature, &ftype);

/*--------------------------------------------------------------------*\
    If the feature is an assembly component,
        return NO ERROR,
    else
        return CONTINUE
\*--------------------------------------------------------------------*/
    if(ftype == PRO_FEAT_COMPONENT)
        return(PRO_TK_NO_ERROR);
    return(PRO_TK_CONTINUE);
}