/*****************************************************************************
**
** feature_name_test.c
**
** Description:
**     Opens a dialog for feature selection.  Then outputs the feature
**     types of the selected features to the listing window in the
**     following format:
**
**     Feature tag 123 is of type <feat_type>
**
**     123 will be replaced by the tag of the selected feature 
**     and feat_type is the string returned by UF_MODL_ask_feat_type()
**
**     NOTE: the <>'s are not in the string returned by UF_MODL_ask_feat_type.
**     They were added to the output string to show any leading or 
**     trailing white spaces in the feature type name.
*****************************************************************************/

/* Include files */
#include <stdio.h>
#include <uf.h>
#include <uf_ui.h>
#include <uf_modl.h>
#define UF_CALL(X) (report_error( __FILE__, __LINE__, #X, (X)))

static int report_error( char *file, int line, char *call, int irc)
{
    if (irc)
    {
        char err[133],
             msg[133];

        sprintf(msg, "*** ERROR code %d at line %d in %s:\n+++ ",
            irc, line, file);
        UF_get_fail_message(irc, err);

        UF_print_syslog(msg, FALSE);
        UF_print_syslog(err, FALSE);
        UF_print_syslog("\n", FALSE);
        UF_print_syslog(call, FALSE);
        UF_print_syslog(";\n", FALSE);

        if (!UF_UI_open_listing_window())
        {
            UF_UI_write_listing_window(msg);
            UF_UI_write_listing_window(err);
            UF_UI_write_listing_window("\n");
            UF_UI_write_listing_window(call);
            UF_UI_write_listing_window(";\n");
        }
    }

    return(irc);
}

/*****************************************************************************
**  Activation Methods
*****************************************************************************/
/*  Explicit Activation
**      This entry point is used to activate the application explicitly, as in
**      "File->Execute UG/Open->User Function..." */
extern DllExport void ufusr( char *parm, int *returnCode, int rlen )
{
    int     response = 0;
    tag_p_t objects  = NULL;
    int     count    = 0;
    int     i        = 0;
    UF_UI_feat_sel_type_t filter = UF_UI_FEAT_SEL_TYPE_BROWSEABLE;

    /* Initialize the API environment */
    if( UF_CALL(UF_initialize()) ) 
    {
        /* Failed to initialize */
        return;
    }
    
    /* TODO: Add your application code here */
    UF_UI_select_feature( "Select Features",
                          &filter,
                          &count,
                          &objects,
                          &response );

    for( i = 0; i < count; i++ )
    {
        char *feature_type = NULL;

        UF_CALL( UF_MODL_ask_feat_type( objects[i], &feature_type ) );
        if( feature_type != NULL )
        {
            char msg[133];
            sprintf( msg, "Feature tag %u is of type <%s>\n", objects[i], feature_type );
            if (!UF_UI_open_listing_window())
            {
                UF_UI_write_listing_window(msg);
            }
            UF_free( feature_type );
        }
    }
    UF_free( objects );


    /* Terminate the API environment */
    UF_CALL(UF_terminate());
}

/*****************************************************************************
**  Utilities
*****************************************************************************/

/* Unload Handler
**     This function specifies when to unload your application from Unigraphics.
**     If your application registers a callback (from a MenuScript item or a
**     User Defined Object for example), this function MUST return
**     "UF_UNLOAD_UG_TERMINATE". */
extern int ufusr_ask_unload( void )
{
    return( UF_UNLOAD_IMMEDIATELY );
}
