uf_mom_extend Examples

The following examples may help further explain the uf_mom_extend function.  Comments are called out in with a slash star (/*) and green.

 

/*retrieve_mom_component_part_attr.c

Description:

This example extends the TCL interpreter so that it can reteive component part attributes and write the values back into TCL.

The first arg is the primary search title - once this is found the secondary search titles ( the remaining args ) are sought.

Compile this program and put the dll in your UGII_CAM_POST_DIR.

Look in UGANSWER for the pb_cmd_for_comp_part_attr.tcl file which has the corrosponding procs that load this function into memory and calls it.

*/

 

/* Include files */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <uf.h>
#include <uf_ui.h>
#include <uf_param.h>
#include <uf_mom.h>
#include <uf_cam.h>
#include <uf_cam_errors.h>
#include <uf_obj.h>
#include <uf_object_types.h>
#include <uf_modl.h>
#include <uf_cfi.h>
#include <uf_assem.h>
#include <uf_part.h>
#include <uf_attr.h>

 

#define DEBUG_  FALSE
#define MAX_ARGC 10
#define ARG_VAL_STR_LENGTH 33

#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);

    /*  NOTE:  UF_print_syslog is new in V18 */

        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);

}

static void PrintErrorMessage( int errorCode );

#define WRITE(X)   UF_UI_open_listing_window(); UF_UI_write_listing_window(X)

 

void Debug_write(char *string)

{

    char

        msg[MAX_LINE_SIZE+1];

 

if (DEBUG_)

{

sprintf(msg,"%s\n",string);
WRITE(msg);

}

 

}

 

static int allocate_memory(unsigned int nbytes, void **where)

{

    int

        resp;

    *where = UF_allocate_memory(nbytes, &resp);

    return resp;

}

 

static int make_an_array(uf_list_p_t *object_list, tag_t **objects)

{

    int

        ii,
       n;
   uf_list_p_t
       temp;

    UF_CALL(UF_MODL_ask_list_count(*object_list, &n));

    UF_CALL(allocate_memory(n * sizeof(tag_t), (void **)objects));

    for (ii = 0, temp = *object_list; ii < n; temp = temp->next, ii++)
       (*objects)[ii] = temp->eid;

    UF_CALL(UF_MODL_delete_list(object_list));

    return n;

}

 

static logical add_proto_to_list(tag_t comp, void *user_data)

{

    tag_t
       proto = UF_ASSEM_ask_prototype_of_occ(comp);
   uf_list_p_t
       *list = (uf_list_p_t *)user_data;

    UF_CALL(UF_MODL_put_list_item(*list, proto));

    return TRUE;

}

 

static void report_load_status(UF_PART_load_status_p_t status)

{

    char

        msg[133];

    int

        ii;

    if (status->n_parts > 0)

    {

        UF_UI_open_listing_window();
       UF_UI_write_listing_window("Load notes:\n");

 

        for (ii = 0; ii < status->n_parts; ii++)

        {

            UF_UI_write_listing_window("  ");
           UF_UI_write_listing_window(status->file_names[ii]);
           UF_UI_write_listing_window(" - ");
           UF_get_fail_message(status->statuses[ii], msg);
           UF_UI_write_listing_window(msg);
           UF_UI_write_listing_window("\n");

        }

        UF_UI_write_listing_window("\n");

        UF_free(status->statuses);
       UF_free_string_array(status->n_parts, status->file_names);

    }

}

 

static int ask_all_assembly_parts(tag_t part, tag_t **parts)

{

    tag_t
       cset;
   uf_list_p_t
       part_list;
   UF_PART_load_status_t
       status;

    UF_CALL(UF_MODL_create_list(&part_list));
   UF_CALL(UF_MODL_put_list_item(part_list, part));

    UF_CALL(UF_ASSEM_ask_all_comp_cset(part, &cset));
   if (cset != NULL_TAG)

    {

        UF_CALL(UF_PART_open_cset(cset, &status));
       report_load_status(&status);
       UF_CALL(UF_ASSEM_apply_to_cset(cset, add_proto_to_list, &part_list));

    }

 

    return make_an_array(&part_list, parts);

}

 

extern int extn_rtv_mom_cp_part_attr(
void *client_dat,   /* I This is mom_object     */
void *interp,         /* I pointer to the TCL interpreter  */
int  argc,             /* I count of the number of arguments  */
char **argv          /* I array of arguments values    */
);

 

extern DllExport void entry_rtv_mom_cp_part_attr( char *parm,
                                  int *returnCode, int rlen )

{

int ret_code = 0;
void *interp  = NULL;
logical local_initialize = FALSE;

 

    UF_MOM_id_t  mom_id;

    /* Initialize the API environment */

    if( UF_is_initialized() == 0 )

{

       ret_code = UF_initialize();
  if( ret_code != 0 )

          /****UF_initialize failed so return **/

      return;
  local_initialize = TRUE;

}

/* get the TCL intepreter id from the ufusr param --*/

(void)UF_MOM_ask_interp_from_param ( parm, &interp );

 /****Get the mom_id by passing in the parm pointer ****/

UF_MOM_ask_mom( parm, &mom_id );

/* now extend the TCL interpreter with the following functions

 * the following TCL extensions will be added EXTN_rtv_mom_cp_part_attr */

     ret_code = UF_MOM_extend_xlator ( mom_id, "EXTN_rtv_mom_cp_part_attr",

                                             extn_rtv_mom_cp_part_attr );

 /*** If ret_code is non zero then an error has occured and

      so print the error ***/

     if( ret_code )
       PrintErrorMessage( ret_code );

     *returnCode = 0;

 if( local_initialize == TRUE )
       UF_terminate();

}

 extern int extn_rtv_mom_cp_part_attr(
void *client_data,   /* I This is mom_object                      */
void *interp,           /* I pointer to the TCL interpreter         */
int  argc,               /* I count of the number of arguments  */
char **argv            /* I array of arguments values              */ )

 

 {

    

    char

        part_spec[UF_CFI_MAX_PATH_NAME_SIZE],
msg[MAX_LINE_SIZE+1],
client_attr_titles[MAX_ARGC][ARG_VAL_STR_LENGTH],
str[33],
mom_var_str[33];

    int ret_code =0,

        i,
       ii,
       j,
jj,
k,
num,
       n_parts,
arg_length;

    tag_t
       *parts,
       part = UF_PART_ask_display_part();

 

logical
local_initialize = FALSE,
is_initialized = FALSE,
key_title_found = FALSE;


   UF_ATTR_part_attr_t
       *attrs;

UF_MOM_id_t  mom_id_tag;

sprintf(msg,"In extn_rtv_mom_cp_part_attr");
Debug_write(msg);

 

if( UF_is_initialized() == 0 )

{

    sprintf(msg,"UF_is_initialized() == 0");
   Debug_write(msg);

            ret_code = UF_initialize();

    sprintf(msg,"ret_code is %d",ret_code);
   Debug_write(msg);

   if( ret_code != 0 )

   {

          /****UF_initialize failed so return **/

      return 1;

   }

   local_initialize = TRUE;

}

        mom_id_tag = ( UF_MOM_id_t )client_data;

UF_CAM_is_session_initialized(&is_initialized);

if( is_initialized == TRUE )

{

   sprintf(msg,"is_initialized == TRUE");
  Debug_write(msg);

   for (i = 1; i < argc; i++)

   {

  sprintf(msg,"i is %d",i);
 Debug_write(msg);

  arg_length = (int)strlen(argv[i]);
         sprintf(msg,"argv[%d] is %d chars long",i, arg_length );
         Debug_write(msg);

  if(arg_length > ARG_VAL_STR_LENGTH )

  {

sprintf(msg,"ERROR - argv[%d] is greater than %d chars",
                             i,ARG_VAL_STR_LENGTH );
uc1601(msg,1);
Debug_write(msg);

  }

  else

  {

strcpy(client_attr_titles[i-1],argv[i]);
sprintf(msg,"client_attr_titles[%d] is %s",
        i-1, client_attr_titles[i-1]);
Debug_write(msg);

  }

   }

if ((n_parts = ask_all_assembly_parts(part, &parts)) > 0)

{
 for (ii = 0; ii < n_parts; ii++)
{
sprintf(msg,"ii = %d",ii);
Debug_write(msg);
UF_CALL(UF_PART_ask_part_name(parts[ii], part_spec));
sprintf(msg,"%s",part_spec);
Debug_write(part_spec);
                               UF_CALL(UF_ATTR_ask_part_attrs(parts[ii], &num, &attrs));

                        for (j = 0; j < num; j++)

                                 {
  sprintf(msg,"j = %d",j);
   Debug_write(msg);
    if(strcmp(attrs[j].title,client_attr_titles[0]) == 0 )

     {
sprintf(msg,"%s = %s",
      attrs[j].title,attrs[j].string_value);
Debug_write(msg);
sprintf(str,"%s",attrs[j].string_value);
sprintf(mom_var_str,"attr_comp_PART(1)");
Debug_write(mom_var_str);
UF_CALL(UF_MOM_set_string(mom_id_tag,mom_var_str,str));
key_title_found = TRUE;

/* start looking for secondary titles */

for (jj = 1; jj < argc-1 ; jj++)

{

  sprintf(msg,"jj = %d",jj);
  Debug_write(msg);
  for (k = 0; k < num; k++)

   {

     sprintf(msg,"k = %d",k);
    Debug_write(msg);
    if(strcmp(attrs[k].title,client_attr_titles[jj])
                                                        == 0 )

     {

       sprintf(msg,"%s = %s",
            attrs[k].title,attrs[k].string_value);

       Debug_write(msg);
      sprintf(str,"%s",attrs[k].string_value);
      sprintf(mom_var_str,"attr_comp_PART(%d)",jj+1);
      Debug_write(mom_var_str);
      UF_CALL(UF_MOM_set_string(mom_id_tag,mom_var_str,str));

      }

     }

   }

}

} /* end of for (j = 0; j < num; j++) */

                if (num > 0) UF_free( attrs);

if ( key_title_found == TRUE )

{

ii = n_parts;

}

} /* end of for (ii = 0; ii < n_parts; ii++) */

UF_free(parts);

}

} /* end of if( is_initialized == TRUE ) */

if( local_initialize == TRUE )

{

UF_terminate();

}

return ret_code;

 }

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

**  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".
**     In this example the dll should not be unloaded IMMEDIATELY because we are
**     going to call the extension function we have registered */

extern int ufusr_ask_unload( void )

{

    return( UF_UNLOAD_UG_TERMINATE );

}

/* PrintErrorMessage

**
**     Prints error messages to standard error and the Unigraphics status
**     line. */

static void PrintErrorMessage( int errorCode )

{

    if ( 0 != errorCode )

    {

        /* Retrieve the associated error message */

        char message[133];
       UF_get_fail_message( errorCode, message );

        /* Print out the message */
       UF_UI_set_status( message );

        fprintf( stderr, "%s\n", message );

    }

}

 

 

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

proc PB_CMD_init_for_extended_libraries { } {

#=============================================================

###############################################################################
#                                                                                                          
#     This proc is to be placed in the Start of Program as the first               
#     custom command. It's purpose is to load the extended function into   
#     memory so that it can be used at a later time.( see docs on              
#     MOM_run_user_function ). This proc also initializes the mom_vars     
#     for the extended function to write to. Reserving and initializing            
#     these mom_vars MUST be done for successful execution. In this case
#     the mom_var is a string so it is initialized as "" ( an empty string).      
#     We set the names of the attribute titles to be searched for in the        
#     component part. mom_attr_comp_PART_title(1) is the primary search title
#     once this is found, the current component part file is searched and the  
#     secondary attributes are searched for and written back to the array     
#     mom_attr_comp_PART - the extended function will terminate and return   
#     to TCL. The extended function needs to be unloaded at                   
#     Program End Sequence - see PB_CMD_unload_extended_libraries             
#     Look in UGANSWER NXOPEN for Sample CAM API program to                   
#     retreive_mom_component_part_attr. Compile this sample CAM API program   
#     and put the dll in your UGII_CAM_POST_DIR                               
#                                                                             #
#                                                                             ############################################################################## */

global external_lib_rtv_mom_component_part_attr
global mom_attr_comp_PART_title
global mom_attr_comp_PART

   set external_lib_rtv_mom_component_part_attr [MOM_ask_env_var UGII_CAM_POST_DIR]retrieve_mom_component_part_attr.dll
  MOM_run_user_function $external_lib_rtv_mom_component_part_attr "entry_rtv_mom_cp_part_attr"

*/ initialize the mom variable array that will be written to by the extended function  */

   for { set i 1 } { $i <= 4 } { incr i } {
       set mom_attr_comp_PART($i) ""

   }

  set mom_attr_comp_PART_title(1) "ITEM_NUMBER"
 set mom_attr_comp_PART_title(2) "DESCRIPTION"
 set mom_attr_comp_PART_title(3) "MATERIAL"
 set mom_attr_comp_PART_title(4) "REVISION"

*/ call the extended function  */

    EXTN_rtv_mom_cp_part_attr $mom_attr_comp_PART_title(1) $mom_attr_comp_PART_title(2) \
                                             $mom_attr_comp_PART_title(3) $mom_attr_comp_PART_title(4)

}

#=============================================================

proc PB_CMD_output_comp_part_attributes { } {

#=============================================================

###############################################################################
#                                                                              
#     This proc writes the title and value of the component part attributes    
#     This custom command is usually placed in Start of Program                
#     - just after the %                                                       
#                                                                               ##############################################################################

global mom_attr_comp_PART_title
global mom_attr_comp_PART

   for { set i 1 } { $i <= 4 } { incr i } {
       MOM_output_literal "($mom_attr_comp_PART_title($i) $mom_attr_comp_PART($i))"

   }

}

#=============================================================

proc PB_CMD_unload_extended_libraries { } {

#=============================================================

################################################################################
#                                                                              #
#        This proc unloads the extended function(s)         #
#                                                                              #
################################################################################

global external_lib_rtv_mom_component_part_attr

/* unload extended function */

  MOM_unload_library $external_lib_rtv_mom_component_part_attr

}