Sunday 25 December 2016

Teamcenter ITK : Dataset Upload

Problem statement: Upload dataset in Teamcenter using ITK APIs.

 Here we will learn ITK APIs which will be very helpful in data migration projects.
#include <ae/ae.h>
#include <ae/datasettype.h>
#include <ae/nxsm.h>
#include <custom.h>
#include <dataset.h>
#include <ecm/ecm.h>
#include <epm/epm.h>
#include <folder.h>
#include <form/form.h>
#include <ict_userservice.h>
#include <item_errors.h>
#include <itk/mem.h>
#include <property/prop.h>
#include <property/prop_errors.h>
#include <property/prop_msg.h>
#include <stdarg.h>
#include <string.h>
#include <tc.h>
#include <tc/emh.h>
#include <tc/emh_const.h>
#include <tc/tc.h>
#include <tccore/aom.h>
#include <tccore/aom_prop.h>
#include <tccore/custom.h>
#include <tccore/item.h>
#include <tccore/item_errors.h>
#include <tccore/method.h>
#include <tccore/tctype.h>
#include <tccore/workspaceobject.h>
#include <tcinit/tcinit.h>
#include <time.h>

#define CALLAPI(expr)ITKCALL(ifail = expr); if(ifail != ITK_ok)return ifail;
static int PrintErrorStack( void )
{
 int iNumErrs = 0;
 int *pSevLst = NULL;
 int *pErrCdeLst = NULL;
 char **pMsgLst = NULL;
 register int i = 0;
 
 EMH_ask_errors( &iNumErrs, &pSevLst, &pErrCdeLst, &pMsgLst );
 fprintf( stderr, "Error(s): \n");
 for ( i = 0; i < iNumErrs; i++ )
 {
 fprintf( stderr, "\t%6d: %s\n", pErrCdeLst[i], pMsgLst[i] );
 }
 return ITK_ok;
 }
 int save_object (tag_t object_tag)
 {
    int ReturnCode;
   /* Save the object. */
    ReturnCode = AOM_save (object_tag);
    if (ReturnCode != ITK_ok)
    {
     //output_error ("ERROR %d save object.\n", ReturnCode);
      return (ReturnCode);
    }
 /* Unlock the object. */
 
 ReturnCode = AOM_unlock (object_tag);
     return (ReturnCode);
 }
 int trim(char *str)
 {
 char *ptr = NULL;
 int i,j=0;
 ptr = (char*)malloc((strlen(str)+1) * 2);
 for(i=0;str[i]!='\0';i++)
 {
  if (str[i] != ' ' && str[i] != '\t')
  ptr[j++]=str[i];
 }
 ptr[j]='\0';
 str=ptr;
 free(ptr);
 return 0;
 }
 extern int ITK_user_main( int argc, char **argv )
 {
 
 char *file1 = NULL;
 char *LineRead1 = NULL;
 FILE *fp1;
 char mystring1 [10000];
 char *RqmtID = NULL;
 tag_t my_dataset = NULLTAG;
 char * new_name1 = NULL;
 char * new_name2 = NULL;
 char **list;
 int ifail;
 int n_items =0;
 int backVal = 0;
 int count= 0;
 
 IMF_file_t fileDescriptor ;
 tag_t datasettype_tag= NULLTAG;
 tag_t default_tool_tag;
 tag_t newfile = NULLTAG;
 tag_t newFileTag = NULLTAG;
 tag_t imannewFileTag = NULLTAG;
 AE_reference_type_t reference_type;
 IMF_file_t filedescriptor;

//ITK_initialize_text_services : Initializes the Teamcenter text services. This is necessary to get the correct 
//error strings when you use EMH_get_error_string, or by certain other Teamcenter functions that make use of text services, like 
//forms
//Paramter The value is not used anymore 
 ITK_initialize_text_services( ITK_BATCH_TEXT_MODE );
 ifail = ITK_auto_login ();
 
 new_name1=(char*)MEM_alloc(sizeof(char)*120);
 new_name2=(char*)MEM_alloc(sizeof(char)*120);
 
 LineRead1=malloc(30000);
 
 file1=ITK_ask_cli_argument("-i=");
 printf("\n Input file1 is--> %s \n ",file1);
 fp1=fopen(file1,"r");
 
 //Find the dataset type object for specified type->MSExcel
 AE_find_datasettype ("MSExcel",&datasettype_tag);
 
 //Get the defualt tool object for the dataset type
 AE_ask_datasettype_def_tool(datasettype_tag,&default_tool_tag);
 
 if(fp1==NULL)
 {
 printf("\n Error in opening the file1 \n");
 }
 else
 {
 printf("\n File1 is successfully opened \n");
 while(fgets(mystring1,200000,fp1) !=NULL)
 {
 if(mystring1)
 {
 strcpy(LineRead1,mystring1);
 printf("\n mystring1 %s ",mystring1);
 printf("\n LineRead1 %s ",LineRead1);
 }
 
 RqmtID=strtok(LineRead1,"\n");
 printf("\n RqmtID %s ",RqmtID );
 printf("\n Req item%s\n",RqmtID);fflush(stdout);
 strcpy(new_name1,RqmtID);
 printf("\n new_name1 %s\n",new_name1);fflush(stdout);
 backVal = trim(new_name1);
 printf("\n new_name1: %s\n",new_name1);fflush(stdout);
 strcpy(new_name2,new_name1);
 strcat(new_name2,".xls");
 printf("\n new_name2: %s\n",new_name2);fflush(stdout);
    //Get the dataset with defined criteria
 CALLAPI(AE_find_all_datasets(new_name1,&n_items,&item_tags));
 if (n_items >= 1)
 {
 printf("\nDataset is already avaialbe\n");fflush(stdout);
 }
 else 
 {
 
 printf("\n 44444444444444444444444 \n");fflush(stdout);
 //Create Dataset of specified type(datasettype_tag) and with other attibutes
 CALLAPI(AE_create_dataset_with_id(datasettype_tag, new_name1,"Loading Bulk Dataset", 0, 0, &tDataset));
 printf("\n 5555555555555555 \n");fflush(stdout);
 
 //Set dataset format
 CALLAPI(AE_set_dataset_format(tDataset,"BINARY_REF"));
 
 //Set the tool(tool with which this dataset to be open) for Specified dataset
 CALLAPI(AE_set_dataset_tool(tDataset,default_tool_tag));
 
 //Get the references for the dataset
 //In which we insert the actual datset file
 CALLAPI(AE_ask_datasettype_refs(datasettype_tag,&count,&list));
 
 //Import the file in Teamcenter
 if ( IMF_import_file ("/home/req83/Deliverable.xls",NULL,SS_BINARY,&imannewFileTag,&filedescriptor) !=ITK_ok ) PrintErrorStack();
 
 //Set the original name property for file
 if ( IMF_set_original_file_name(imannewFileTag,new_name2) !=ITK_ok ) PrintErrorStack();
 
 //Save the uploaded file
 save_object(imannewFileTag);
 
 //Add the imported file into dataset as named reference
 if (AE_add_dataset_named_ref(tDataset,list[0],SS_BINARY,imannewFileTag) !=ITK_ok ) PrintErrorStack();
 //if ( AE_import_named_ref  (tDataset,"excel","/home/req83/Deliverable.xls",new_name2,SS_BINARY) !=ITK_ok ) PrintErrorStack();

 //Save the dataset
 save_object(tDataset);
 printf("\nDataset is created\n");fflush(stdout);
 }
 }
 fclose (fp1);
 }
 return ( ITK_ok );
 }
Explanation : Important API 
AE_find_datasettype ("MSExcel",&datasettype_tag); AE_ask_datasettype_def_tool(datasettype_tag,&default_tool_tag); AE_create_dataset_with_id(datasettype_tag, new_name1,"Loading Bulk Dataset", 0, 0, &tDataset) AE_set_dataset_format(tDataset,"BINARY_REF") 
AE_set_dataset_tool(tDataset,default_tool_tag) AE_ask_datasettype_refs(datasettype_tag,&count,&list) 
IMF_import_file ("/home/req83/Deliverable.xls",NULL,SS_BINARY,&imannewFileTag,&filedescriptor) 
IMF_set_original_file_name(imannewFileTag,new_name2) AE_add_dataset_named_ref(tDataset,list[0],SS_BINARY,imannewFileTag)

No comments:

Post a Comment