/*===================================================================

        Copyright (c) 1998  Unigraphics Solutions Corporation
                     Unpublished - All rights reserved

===================================================================*/

/******************************************************************************
 *                                                                            *
 * DESCRIPTION -                                                              *
 *   This program shows how to use the following UG/Open API routines:        *
 *                                                                            *
 *         UF_MODL_replace_boolean_body                                       *
 *                                                                            *
 * PROGRAM DESCRIPTION -                                                      *
 *   The following example requires an open, blank part. The code creates     *
 *   a block, cone, and cylinder.  It then unites the block and the cone.     *
 *   Next it changes the tool body of the unite to the cylinder and updates.  *
 *                                                                            *
              
 *                                                                            *
 ******************************************************************************/

#include <stdio.h>
#include <string.h>
#include <uf_modl.h>
#include <uf.h>
#include <uf_csys.h>
#include <uf_defs.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)
{


  tag_t block, cylinder, cone, feature;

  double origin[3]={0.0,0.0,0.0};
  double direction[3]={0.0,0.0,1.0};

  int num_of_feats;
  int i;

  char *edge_lens[3]={"1","1","1"};
  char *diameters[2]={"1.0","2.0"};
  char *height="2.0";
  char *name;
  
  uf_list_p_t uf_list;

  /* Create a block, cone, and cylinder. */
  UF_CALL(UF_MODL_create_block1(UF_NULLSIGN,
                                origin,
                                edge_lens,
                                &feature));
  UF_CALL(UF_MODL_ask_feat_body(feature,
                                &block));

  UF_CALL(UF_MODL_create_cone1(UF_NULLSIGN,
                               origin,
                               height,
                               diameters,
                               direction,
                               &feature));
  UF_CALL(UF_MODL_ask_feat_body(feature,
                                &cone));

  UF_CALL(UF_MODL_create_cyl1(UF_NULLSIGN,
                              origin,
                              height,
                              diameters[0],
                              direction,
                              &feature));
  UF_CALL(UF_MODL_ask_feat_body(feature,
                                &cylinder));


  /* Unite the block and cone */
  UF_CALL(UF_MODL_operations(block,
                             cone,
                             UF_UNITE));

  /* Find the boolean feature (META) */
  UF_CALL(UF_MODL_ask_body_feats(block,
                                 &uf_list));

  UF_MODL_ask_list_count(uf_list,
                         &num_of_feats);
  for (i = 0; i < num_of_feats; i++)
  {
      UF_MODL_ask_list_item ( uf_list,i,&feature);
      UF_MODL_ask_feat_name(feature,&name);

      if (strncmp(name, "META", 4) == 0)
      {
          UF_free(name);          
          break;
      }
      UF_free(name);
  }

  UF_MODL_delete_list(&uf_list);
  
  /* Change the tool body from the cone to the cylinder. */
  if(UF_CALL(UF_MODL_replace_boolean_body(feature, 
                                          UF_MODL_TOOL_BODY,
                                          cylinder)))
  {
     /* error check */
  }

  /* Trigger update */  
  if(UF_CALL(UF_MODL_update()))
  {
     /* error check */
  }

}
/*ARGSUSED*/
void ufusr(char *param, int *retcode, int paramLen)
{
    if (!UF_CALL(UF_initialize()))
    {
        do_ugopen_api();
        UF_CALL(UF_terminate());
    }
}

int ufusr_ask_unload(void)
{
    return (UF_UNLOAD_IMMEDIATELY);
}
