/* Include files */
#include <stdio.h>
#include <string.h>
#include <uf.h>
#include <uf_ui.h>
#include <uf_attr.h>
#include <uf_assem.h>
#include <uf_attr.h>
#include <uf_weight.h>
#include <uf_modl.h>
#include <uf_part.h>
#include <uf_obj.h>
#include <uf_dirpath.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_s(msg,133, "*** 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);
}
void set_weight_attribute()
{
tag_t part = UF_ASSEM_ask_work_part();
UF_WEIGHT_exceptions_t exceptions;
UF_WEIGHT_properties_t properties;
char value_string[132];
char units_string[132];
char expr_string[133];
UF_ATTR_value_t value;
UF_ATTR_value_t expr;
logical expr_exists = FALSE;
int wp_units;
value.type = UF_ATTR_string;
UF_CALL(UF_WEIGHT_set_part_ref_set(part, "MODEL"));
UF_CALL(UF_WEIGHT_init_exceptions( &exceptions));
UF_CALL( UF_PART_ask_units(part,&wp_units));
if (wp_units == UF_PART_METRIC)
{
UF_CALL(UF_WEIGHT_estab_part_props(part, 0.9999, TRUE, UF_WEIGHT_units_km,&properties, &exceptions));
sprintf_s(units_string,132,"Kg");
}else{
UF_CALL(UF_WEIGHT_estab_part_props(part, 0.9999, TRUE, UF_WEIGHT_units_li,&properties, &exceptions));
sprintf_s(units_string,132,"LB");
}
sprintf_s(value_string,132,"%f",properties.mass);
value.value.string = value_string;
UF_CALL(UF_ATTR_assign(part, "DB_WEIGHT_VALUE", value));
value.value.string = units_string;
UF_CALL(UF_ATTR_assign(part, "DB_WEIGHT_UNIT", value));
// Set Expression for weight value on save.
sprintf_s(value_string,132,"%s=%f","DB_WEIGHT_VALUE",properties.mass);
UF_CALL(UF_MODL_is_exp_in_part(part,"DB_WEIGHT_VALUE",&expr_exists));
if(expr_exists)
{
UF_CALL(UF_MODL_edit_exp(value_string));
}else{
UF_CALL(UF_MODL_create_exp(value_string));
}
// Update the model
UF_CALL(UF_MODL_update());
} /* end of set_weight_Attribute*/
/*****************************************************************************
** Activation Methods
*****************************************************************************/
extern DllExport void ufsta( char *param, int *returnCode, int rlen )
{
/* Initialize the API environment */
if( UF_CALL(UF_initialize()) )
{
/* Failed to initialize */
return;
}
set_weight_attribute();
*returnCode=0;
/* 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_UG_TERMINATE );
}