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

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

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

/******************************************************************************
 *                                                                            *
 * DESCRIPTION -                                                              *
 *   This program shows how to use the following UG/Open API routines:        *
 *                                                                            *
 *         UF_MODL_unite_bodies                                               *
 *         UF_MODL_subtract_bodies                                            *
 *         UF_MODL_intersect_bodies                                           *
 *                                                                            *
 * PROGRAM DESCRIPTION -                                                      *
 *   The following example requires an open, blank part. The code creates     *
 *   a block and cylinder.  It then performs unite, subtract, and intersect   *
 *   operations between the two bodies with an undo in between each operation.*
 *                                                                            *
 *                                                                            *
 ******************************************************************************/

#include <stdio.h>
#include <uf_modl.h>
#include <uf.h>
#include <uf_defs.h>
#include <uf_undo.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, feature, *resulting_bodies = NULL;

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

  int num_results;

  char *edge_lens[3]={"1","1","1"};
  char *diameter={"1.0"};
  char *height="2.0";
     
  int rollback_id = 0;  
  UF_UNDO_mark_name_t mark_name = NULL;

  /* Create a block 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_cyl1(UF_NULLSIGN,
                              origin,
                              height,
                              diameter,
                              direction,
                              &feature));
  UF_CALL(UF_MODL_ask_feat_body(feature,
                                &cylinder));

  /* Set undo mark */
  UF_CALL(UF_UNDO_set_mark( UF_UNDO_invisible, 
                            mark_name,
                            (UF_UNDO_mark_id_t *)&rollback_id));

  /* Perform unite */
  UF_CALL(UF_MODL_unite_bodies(block,
                               cylinder));

  /* Undo the unite */
  UF_CALL(UF_UNDO_undo_to_mark(rollback_id,
                               NULL));

  /* Set undo mark */
  UF_CALL(UF_UNDO_set_mark( UF_UNDO_invisible, 
                            mark_name,
                            (UF_UNDO_mark_id_t *)&rollback_id));

  /* Perform subtract */
  UF_CALL(UF_MODL_subtract_bodies(block,
                                  cylinder,
                                  &num_results,
                                  &resulting_bodies));

  if (num_results > 0)
      UF_free(resulting_bodies);

  /* Undo the subtract */
  UF_CALL(UF_UNDO_undo_to_mark(rollback_id,
                               NULL));

  /* Perform intersect */
  UF_CALL(UF_MODL_intersect_bodies(block,
                                   cylinder,
                                   &num_results,
                                   &resulting_bodies));
  if (num_results > 0)
      UF_free(resulting_bodies);

  
}
/*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);
}
