Friday 30 December 2016

Teamcenter ITK : BOM EXPORT WITH PLM XML

Problem Statement :Use ITK and PLM XML export to export BOM

The following example shows how to use ITK to export a BOM view revision
PLM XML: Code Taken from server customization guide PDF

#include <stdlib.h>
#include <tc/tc.h>
#include <tc/emh.h>
#include <tccore/item.h>
#include <itk/mem.h>
#include <pie/pie.h>
#include <bom/bom.h>
#define SampleError 1
#define EXIT_FAILURE 1
#define ERROR_CHECK(x) { \

int stat; \
char *err_string; \
if( (stat = (x)) != ITK_ok) \
{ \
EMH_get_error_string (NULLTAG, stat, &err_string); \
printf ("ERROR: %d ERROR MSG: %s.\n", stat, err_string); \
printf ("FUNCTION: %s\nFILE: %s LINE: %d\n",#x, __FILE__, __LINE__); \
if(err_string) MEM_free(err_string); \
exit (EXIT_FAILURE); \
} \
}

int ITK_user_main(int argc, char* argv[]);
static void initialize ( char *usr , char *upw , char *ugp);
static void display_help_message(void);
static int find_topline_of_itemid(char *itemid,char *rev, int *n_tags, tag_t **tags);
static int export_bvr();
/* Main */

int ITK_user_main(int argc, char* argv[])
{

int status = 0;
if ( argc <= 6 )
{

display_help_message();
return( SampleError);
}
char *usr = ITK_ask_cli_argument( "-u=");
char *upw = ITK_ask_cli_argument( "-p=");
char *ugp = ITK_ask_cli_argument( "-g=");
initialize ( usr , upw , ugp );
ITK_set_journalling(TRUE);
export_bvr();
ITK_exit_module(TRUE);
return status;
}

/* Do BVR export */

static int export_bvr()
{

int max_char_size = 80;
char *xmlFileName;
char *logFileName = (char *) MEM_alloc(max_char_size * sizeof(char));
char *itemid;
char *rev;

int n_objects = 0;

tag_t *objects = NULL;

// Create PIE session
tag_t session;
ERROR_CHECK( PIE_create_session(&session) );

if ( (xmlFileName = ITK_ask_cli_argument( "-file=" )) == NULL )
{
display_help_message();

return ( SampleError );
}

// Set the name of the XML file to export
ERROR_CHECK( PIE_session_set_file(session, xmlFileName) );

// Set the name of the log file
sprintf(logFileName, "%s.log", xmlFileName);
ERROR_CHECK( PIE_session_set_log_file(session, logFileName) );

// get item id
if ( (itemid = ITK_ask_cli_argument( "-item=" )) == NULL )
{
display_help_message();

return ( SampleError );
}

// Get item revision
if ( (rev = ITK_ask_cli_argument( "-rev=" )) == NULL )
{
display_help_message();
return ( SampleError );
}

// Get tranfermode
int n_transfer_modes;
tag_t *transfer_modes;
ERROR_CHECK( PIE_find_transfer_mode("ConfiguredDataExportDefault", "", &n_transfer_modes,

&transfer_modes) );
if( n_transfer_modes == 0 )
{

printf("Error in finding default transfer mode\n");
return(SampleError);
}

// Set the transfermode on the sessionion
ERROR_CHECK( PIE_session_set_transfer_mode(session, transfer_modes[0]) );
//To export the translations of a localizable properties, call PIE__set_export_languages
// with the language codes that you are interested in to set the locales to PIE Session.
// Get the topline of this item to export
ERROR_CHECK( find_topline_of_itemid(itemid, rev, &n_objects, &objects) ) ;

// Pass in the list of objects and do the export
ERROR_CHECK( PIE_session_export_objects(session, n_objects, objects) );

// Delete the session
ERROR_CHECK( PIE_delete_session(session) );

MEM_free(logFileName);
return( ITK_ok );
}

/* Login */

static void initialize ( char *usr , char *upw , char *ugp)
{

int status = ITK_ok;
char *message;
ITK_initialize_text_services( 0 );
if ( ITK_ask_cli_argument( "-h" ) != 0 )
{
display_help_message();
exit( EXIT_FAILURE );
}

status = ITK_init_module ( usr , upw , ugp );
if (status != ITK_ok)
{
EMH_ask_error_text (status, &message);
printf("Error with ITK_auto_login: \"%d\", \"%s\"\n", status, message);
MEM_free(message);
return ;
}
else
{
printf ("login to database successful.\n");
}
}

/* Find topline for this item */

static int find_topline_of_itemid(char *itemid,char *rev, int *n_tags, tag_t **tags)
{
tag_t itemTag = NULLTAG;
tag_t rev_tag = NULLTAG;

int local_num_tags = 0;

tag_t *local_tags = NULL;

// Find the required item //API Deprecated
ERROR_CHECK( ITEM_find_item (itemid, &itemTag) );

if(itemTag == NULLTAG)
{
return(SampleError);
}

//Export only if rev is provided
if (rev != NULL)
{
// Get item revision tag//API Deprecated
ERROR_CHECK( ITEM_find_rev(itemid,rev, &rev_tag) );
tag_t window = NULLTAG;
tag_t top_line = NULLTAG;

// Create a window for BVR export of this item
ERROR_CHECK( BOM_create_window(&window) );

local_num_tags = 1;
local_tags = (tag_t *) MEM_alloc(sizeof(tag_t) * 1);

local_tags[0] = window;
// Set this item as topline to export
ERROR_CHECK( BOM_set_window_top_line(window,itemTag,rev_tag,NULLTAG,&top_line) );

local_num_tags++;

local_tags = (tag_t *) MEM_realloc (local_tags, sizeof(tag_t) * local_num_tags);

local_tags[local_num_tags-1] = top_line;

}

*n_tags = local_num_tags;
*tags = local_tags;
return ITK_ok;
}

/* Displays help messgage */

static void display_help_message(void)
{

printf( "\n\nsample_plmxml_itk_export: PLMXML export of a simple BVR" );
printf( "\n\nUSAGE: sample_plmxml_itk_export -u=username -p=password -g=groupname
-item=itemname -rev=itemrevision -file=filename");
printf( "\n -h displays this message");
}
Explanation : Important API
  PIE_create_session(&session)
 PIE_session_set_file(session, xmlFileName)
 PIE_session_set_log_file(session, logFileName)
 PIE_find_transfer_mode("ConfiguredDataExportDefault", "", &n_transfer_modes, &transfer_modes)
 PIE_session_set_revision_rule(tSession,tRevRule);
 PIE_session_set_transfer_mode(session, transfer_modes[0])
 PIE_session_export_objects(session, n_objects, objects)

3 comments:

  1. In this statement "transfer_modes[0]" why use zero

    ReplyDelete
  2. There is only one transfer mode in the array(transfer_modes[]), hence to select that one from the array it is written as transfer_modes[0].

    ReplyDelete