/*============================================================================*\
FILE    : UgAsmCompVisit.c
PURPOSE : Pro/TOOLKIT User Guide Example

HISTORY..
DATE      BUILD   AUTHOR    MODIFICATIONS
04-Dec-97 H-02-02 WU        $$1    Created
11-Dec-97 H-03-31 Philippe  $$2    (For Sun) Change asm into p_asm
\*============================================================================*/

/*------------------------ ProToolkit includes ---------------------------*/
#include <ProToolkit.h>
#include <ProMdl.h>
#include <ProFeature.h>
#include <ProObjects.h>
#include <ProFeatType.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_NAME_SIZE];
    char type[PRO_TYPE_SIZE];
    wchar_t wname[PRO_NAME_SIZE];
    UserAppdata *appd,appd1;
    ProMdldata mdldata;

    appd = (UserAppdata *)appdata;
    fp = appd->fp;
    level = appd->level;
    err = ProAsmcompMdlGet(feature , &mdl);
    ERROR_CHECK( "user_action", "ProAsmcompMdlGet", err );
    err = ProMdlDataGet(mdl, &mdldata);
    ERROR_CHECK( "user_action", "ProModelitemNameGet", err );
    err = ProWstringToString(name,mdldata.name);
    err = ProWstringToString(type,mdldata.type);
    for(l=0;l<level;l++)
            fprintf(fp,"    ");
    fprintf(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_NAME_SIZE];
    char type[PRO_TYPE_SIZE];
    wchar_t wname[PRO_NAME_SIZE];
    ProMdldata mdldata;
    ProError err;
    UserAppdata appdata;
    FILE *fp;
    ProMdl p_asm;

    err = ProMdlCurrentGet(&p_asm);
    ERROR_CHECK( "UserAsmCompVisit", "ProMdlCurrentGet", err );
/*---------------------------------------------------------------------*\
    Open the text file.
\*---------------------------------------------------------------------*/
    strcpy(name,FILENAME);
    fp = fopen(name,"w");
    err = ProMdlDataGet(p_asm, &mdldata);
    ERROR_CHECK( "UserAsmCompVisit", "ProMdlDataGet", err );
    ProWstringToString(name, mdldata.name);
    ProWstringToString(type, mdldata.type);
    fprintf(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);
}
