/******************************************************************************
             Copyright (c) 1999 Unigraphics Solutions, Inc.
                       Unpublished - All Rights Reserved

*******************************************************************************/
/*

The following code creates two blocks, creates a distance expression
between the two solids, then cycles through all expressions in the part
and outputs the geometric expressions in the listing window.

*/

#include <unidefs.h>
#include <stdio.h>
#include <uf.h>
#include <uf_modl.h>
#include <uf_ui.h>
#include <uf_defs.h>
#include <uf_obj.h>
#include <uf_gexp.h>
#include <uf_object_types.h>
#define UF_CALL(X) (report( __FILE__, __LINE__, #X, (X)))
static int report( char *file, int line, char *call, int irc)
{
  if (irc)
  {
     char    messg[133];
     printf("%s, line %d:  %s\n", file, line, call);
     (UF_get_fail_message(irc, messg)) ?
       printf("    returned a %d\n", irc) :
       printf("    returned error %d:  %s\n", irc, messg);
  }
  return(irc);
}
static void do_ugopen_api(void)
{
    double block_orig[3] = {0.0,0.0,0.0};
    double block_orig1[3] = {5.0,5.0,5.0};
    char *block_len[3] = {"1","2","3"};
    tag_t blk_obj,blk_obj1;
    int ifail=0;
    char *string;
    tag_t *exps;
    int number_of_exps=0,i=0;
    char buf[UF_UI_MAX_STRING_LEN+1];
    char err_msg[MAX_LINE_SIZE+1];
    tag_t part, feat_tag, exp_tag, solid, solid_body[2];
    int type, subtype;

    UF_CALL(UF_MODL_create_block1(UF_NULLSIGN,block_orig,
                                  block_len,&blk_obj));
    UF_CALL(UF_MODL_create_block1(UF_NULLSIGN,block_orig1,
                                  block_len,&blk_obj1));
    UF_CALL(UF_UI_open_listing_window());
    if((part=UF_PART_ask_display_part()) == NULL_TAG)
    {
        UF_UI_write_listing_window("Failed to get part tag\n");
        return;
    }
    solid=NULL_TAG;
    while (i < 2)
    {
        type=UF_solid_type;
        UF_OBJ_cycle_objs_in_part( part, type, &solid );
        UF_OBJ_ask_type_and_subtype(solid, &type, &subtype);
        if (subtype == UF_solid_body_subtype)
            solid_body[i++]=solid;
    }
    UF_CALL(UF_GEXP_create_distance(solid_body[0], NULL_TAG,
             solid_body[1], NULL_TAG, &feat_tag, &exp_tag));
    ifail=UF_MODL_ask_exps_of_part(part,&number_of_exps,&exps);
    if(ifail)
    {
      UF_get_fail_message(ifail,err_msg);
      sprintf(buf,"Error: %s\n", err_msg);
      UF_UI_write_listing_window(buf);
    }
    sprintf(buf,"number_of_exps=%d\n",number_of_exps);
    UF_UI_write_listing_window(buf);
    UF_UI_write_listing_window("Asking for geometric expressions\n");
    for(i=0;i<number_of_exps;i++)
    {
        logical flag;
        ifail=UF_MODL_is_geometric_expression(exps[i],&flag);
        if (ifail)
        {
            UF_get_fail_message(ifail, err_msg);
            sprintf(buf, "Error: %s\n", err_msg);
            UF_UI_write_listing_window(buf);
        }
        if (flag == TRUE)
        {
            ifail=UF_MODL_ask_exp_tag_string(exps[i],&string);
            if(ifail)
            {
                UF_get_fail_message(ifail, err_msg);
                sprintf(buf, "Error: %s\n", err_msg);
                UF_UI_write_listing_window(buf);
            }
            sprintf(buf,"Geometric expression %s\n",string);
            UF_UI_write_listing_window(buf);
            UF_free(string);
        }
    }
    UF_free(exps);
}
/*ARGSUSED*/
void ufusr(char *param, int *retcode, int param_len)
{
  if (!UF_CALL(UF_initialize()))
  {
    do_ugopen_api();
    UF_CALL(UF_terminate());
  }
}
int ufusr_ask_unload(void)
{
  return (UF_UNLOAD_IMMEDIATELY);
}

