Saturday 24 December 2016

Teamcenter ITK : Register Method on post action of Item Creation -> Project Assignment

Problem Statement :
 Assign the created object to Project depending on m5_ProjectCode  attribute value.
 This should be called at post action of creation.

(Once you register your customization in Teamcenter preference TC_Customization_Libraries this customization will get called on post action of creation of item)

Snapshot provides the details about Item message parameters .


 #include <tc/tc.h>
 #include <custom.h>
 #include <dataset.h>
 #include <epm/epm.h>
 #include <ict_userservice.h>
 #include <method.h>
 #include <property/prop.h>
 #include <property/prop_errors.h>
 #include <property/prop_msg.h>
 #include <reservation.h>
 #include <stdarg.h>
 #include <string.h>
 #include <tc.h>
 #include <tc/emh_const.h>
 #include <tc/envelope.h>
 #include <tc/emh.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/item_msg.h>
 #include <tccore/method.h>
 #include <tccore/tctype.h>
 #include <tccore/workspaceobject.h>
 #include <tcfile.h>
 #include <tctype.h>
 #include <item_errors.h>
 #include <user_exits/user_exits.h>
 #include <workspaceobject.h>
 #include <project.h>
 
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;
}
extern DLLAPI int createpost_method_1(METHOD_message_t *m, va_list args)
{

const char* item_id;
const char* item_name;
const char* type_name;
const char* rev_id;
tag_t* new_item_tag;
tag_t* new_rev_tag;
tag_t item_master_form_tag;
tag_t item_rev_master_form_tag;
tag_t tagItem=NULLTAG;
tag_t tagItemRev=NULLTAG;
tag_t objTypeTag;
tag_t projobj;
char *ObjProject = NULL;
char* itemTagString=NULL;
char* itemRevTagString=NULL;
char type_name1[TCTYPE_name_size_c+1];

//Refer snapshot for how do we know about this parameters
item_id = va_arg(args, const char*);
item_name = va_arg(args, const char*);
type_name = va_arg(args, const char*);
rev_id = va_arg(args, const char*);
new_item_tag = va_arg(args, tag_t *);
new_rev_tag = va_arg(args, tag_t *);
item_master_form_tag = va_arg(args, tag_t);
item_rev_master_form_tag= va_arg(args, tag_t);
tagItem=*new_item_tag;
tagItemRev=*new_rev_tag; 

//Find the object type tag for input tagItemRev
if (TCTYPE_ask_object_type(tagItemRev,&objTypeTag)!=ITK_ok)PrintErrorStack(); 

//Get the name of object
if (TCTYPE_ask_name(objTypeTag,type_name1)!=ITK_ok)PrintErrorStack();
printf("\n type_name = %s\n", type_name1);

if (AOM_get_value_string(tagItemRev,"m5_ProjectCode",&ObjProject))PrintErrorStack();
printf("\nProject from Object : %s\n",ObjProject);

if(strcmp(ObjProject,""))
{

//Find the project in Teamcenter with ObjProject
if (PROJ_find(ObjProject,&projobj) !=ITK_ok)PrintErrorStack();

if(projobj !=NULLTAG)
{
//Assign the project to an object
if (PROJ_assign_objects(1 ,&projobj ,1 ,&tagItem )!=ITK_ok)PrintErrorStack();
printf("\nAssigned to Project\n");
}
else
{
printf("\nProject not found\n");
}

}
else
{
printf("\nProject Code attribute found NULL on object. Project will not be assigned\n");
}
printf("\nPost Creation.....done\n");
return ITK_ok;
}
extern int createpost_register_method(int *decision, va_list args)
{
METHOD_id_t method;
*decision = ALL_CUSTOMIZATIONS;
//Find the method to which we extend to add post action
if ( METHOD_find_method("Item", ITEM_create_msg, &method) !=ITK_ok)
PrintErrorStack();
if (method.id != NULLTAG)
{
//Register the method to be called on post action of ITEM_create_msg
if( METHOD_add_action(method, METHOD_post_action_type, (METHOD_function_t) createpost_method_1, NULL)!=ITK_ok)
PrintErrorStack();
printf("Registered as Post-Action for Creation\n");
}
else
printf("Method not found!\n");
return ITK_ok;
} 
//Function get loaded when TC_Customization_Libraries preference are loaded by teamcenter
//This library must be added in TC_Customization_Libraries preference as createpost
extern DLLAPI int createpost_register_callbacks ()
{
printf("\n\ncreatepost_register_callbacks changed\n\n");
//Once this function get loaded createpost_register_method will be called
if(CUSTOM_register_exit ( "createpost", "USER_init_module", (CUSTOM_EXIT_ftn_t) createpost_register_method) !=ITK_ok) 
PrintErrorStack();
return ( ITK_ok );
}
Explanation : Important API 
METHOD_find_method("Item", ITEM_create_msg, &method) 
METHOD_add_action(method, METHOD_post_action_type, (METHOD_function_t) createpost_method_1, NULL) 
PROJ_find(ObjProject,&projobj) 
PROJ_assign_objects(1 ,&projobj ,1 ,&tagItem )

4 comments:

  1. Can you share an example to create a post-action in BMIDE itself?

    ReplyDelete
  2. Sure, I will definitely provide the example soon

    ReplyDelete
  3. Error 13 error LNK2019: unresolved external symbol TYPE_ask_type referenced in function A5_new_post_action c:\Users\administrator\documents\visual studio 2013\Projects\Win32Project2\Win32Project2\Extension3.obj Win32Project2


    can you please suggest for this error?

    ReplyDelete