/*---------------------------------------------------------------------------*\ 08-Jul-97 H-03-16 Pavel $$1 Created 11-Jul-97 H-03-17 Pavel $$2 Added possibility connect/disconnect to existing Pro/E session 01-Aug-97 H-03-17+ amin $$3 Rename to ProEngineerConnect and ProEngineerDisconnect 19-Jan-98 H-03-37 aab $$4 Fix some bugs for C++ compiler 22-Jan-98 H-03-37+ aab $$5 Fixed more bugs 09-Feb-98 H-03-38 ljl $$6 set hostname to empty string for Release 20, comments, fix seg. fault after retrieval in restart 11-Feb-98 H-03-38 ljl $$7 Fix err argument type for C++ compiler. 17-Mar-98 H-03-41 akh $$8 Call ProUtilCommandLineParse() to retrieve application parameters from command line 10-May-98 I-01-07 CHI $$9 remove call to obsolete function 20-Oct-98 I-01-23 Pavel $$10 Added ProAsynchronousMain, removed buttons from MAIN and MISC menus 21-Oct-98 I-01-23 AKH $$11 Included stdlib.h 28-Oct-98 I-01-25 akh $$12 added ProEngineerConnectWS 06-Nov-98 I-01-25 akh $$13 Fixed compilation error. 03-Dec-98 I-01-27 akh $$14 Correct TEST_CALL_REPORTs \*---------------------------------------------------------------------------*/ /*------------------------------------------------------------------*\ Pro/TOOLKIT includes \*------------------------------------------------------------------*/ #include "ProToolkit.h" #include "ProCore.h" #include "ProMenu.h" #include "ProMessage.h" #include "ProMenuBar.h" #include "ProMdl.h" #include "ProNotify.h" #include "ProUtil.h" /*------------------------------------------------------------------*\ Application includes \*------------------------------------------------------------------*/ #include "TestError.h" #include "UtilString.h" #include "UtilFiles.h" #include #include /* STATICS */ static int event = 0; static int Terminate = 0; static int Action_done = 1; static ProFileName msgfil; static ProProcessHandle proe_handle; static int connect_mode = 0; void UserTerminateAction PRO_PROTO ((ProeTerminationStatus term_type)); int UserActionFunc PRO_PROTO (( ProAppData ptr, int opt)); int UserRestartProeTest (char*, char*, int, char*); int UserTerm (); int ProTestShowMenu (); ProError UserNotificationFunc(); #define USER_RETRIEVE_ACTION 1 #define USER_MESSAGE_ACTION 2 #define USER_EXIT_ACTION 3 #define MSGFIL msgfil /*====================================================================*\ FUNCTION : TestAccessDefault() PURPOSE : enable access to the -Main button \*====================================================================*/ static uiCmdAccessState TestAccessDefault(uiCmdAccessMode access_mode) { return (ACCESS_AVAILABLE); } /*============================================================================*\ Function : main Purpose : Demonstrate the asynchronous mode of operations \*============================================================================*/ int main(int argc, char *argv[]) { int i, action, bad_status = 0; char *part_to_retrieve=NULL; char proe_command[PRO_PATH_SIZE], text_path[PRO_PATH_SIZE]; char ua_msg[20]; int counter = 0, timeout = 10; char *hostname ; /* hostname not supported in Release 20. */ char *display=NULL, *user=NULL; ProBoolean random, asy_flag = PRO_B_FALSE, random_choice; ProError err, last_err = PRO_TK_NO_ERROR; time_t action_time = 0, report_time = 0, cur_time; wchar_t workspace[PRO_NAME_SIZE]; ProProcessHandle p_handle; ProStringToWstring(MSGFIL, "usermsg.txt"); ProTestErrlogOpen("pt_toolkit", "", ""); if (argc >= 2) if (strncmp (argv[1], "-rpc", 4) == 0) asy_flag = PRO_B_TRUE; if (asy_flag == PRO_B_TRUE) /* Start application from Pro/E session */ { err = ProAsynchronousMain (argc, argv); TEST_CALL_REPORT ("ProAsynchronousMain()", "main()", err, err != PRO_TK_NO_ERROR); if (err != PRO_TK_NO_ERROR) { fprintf (stderr, "Could not connect to Pro/E\n"); exit (0); } else printf ("Connection may have succeeded!\n"); } else /* Start application from command line */ { /* Check command line args and make the proe command */ for(i=0; i or\n", argv[0]); fprintf(stderr, "\t%s or\n", argv[0]); fprintf(stderr, "\t%s " " \n", argv[0]); fprintf(stderr, "\t%s -connect [user [display ]]\n", argv[0]); exit(0); } /*------------------------------------------------------------------*\ Connect to a Pro/ENGINNER session. \*------------------------------------------------------------------*/ if (ProUtilStrcmp(argv[1], "-connect")==0) { connect_mode = 1; strcpy(text_path, argv[2]); hostname = "" ; if (argc>4) display = argv[4]; if (argc>3) user = argv[3]; printf("Attempt to connect ... user %s, display %s\n", user == NULL ? "any" : user, display == NULL ? "any" : display); printf("Text path %s\n", text_path); if (argc>5) { if (argc<7) { fprintf(stderr, "\t%s -connect [user [display [workspace timeout]] ]\n", argv[0]); } ProStringToWstring (workspace, argv[5]); timeout = atoi (argv[6]); err = ProEngineerConnectWS ("", display, user, workspace, text_path, PRO_B_TRUE, timeout, &random_choice, &p_handle); TEST_CALL_REPORT("ProEngineerConnectWS()", "main()", err, err != PRO_TK_NO_ERROR); } else{ err = ProEngineerConnect(hostname, display, user, text_path, PRO_B_TRUE, 20, &random, &proe_handle); TEST_CALL_REPORT("ProEngineerConnect()", "main()", err, err != PRO_TK_NO_ERROR); } if (err != PRO_TK_NO_ERROR) { printf("Attempt unsuccessfull, exiting\n"); return (0); } } /*------------------------------------------------------------------*\ Spawn a Pro/ENGINNER session. \*------------------------------------------------------------------*/ else { strcpy(proe_command, argv[1]); /* Pro/E command */ if(argc == 4) { strcat(proe_command, " "); strcat(proe_command, argv[argc-1]); /* model or trail file */ } else if (argc > 4) { strcat(proe_command, " "); strcat(proe_command, argv[3]); /* run mode */ strcat(proe_command, " "); strcat(proe_command, argv[4]); /* graphics mode */ strcat(proe_command, " "); strcat(proe_command, argv[5]); /* trail mode */ strcat(proe_command, " "); strcat(proe_command, argv[6]); /* trail file */ } strcpy(text_path, argv[2]); /* Start up Pro/ENGINEER */ fprintf(stderr, "\n*****\tStarting Pro/Engineer\t*****\n"); fprintf(stderr, "\tProe command: \"%s\"\t\n\tPlease wait...\n", proe_command); err = ProEngineerStart(proe_command, text_path); TEST_CALL_REPORT("ProEngineerStart()", "main()", err, err != PRO_TK_NO_ERROR); } } /* Interrupt handling */ while (!Terminate) { ProEventProcess(); time(&cur_time); if (!Terminate) { err = ProEngineerStatusGet(); } else err = PRO_TK_NO_ERROR; /* report only once in a sec */ if ((last_err != err) || (cur_time - 1 > report_time)) { report_time = cur_time; last_err = err; TEST_CALL_REPORT("ProEventProcess()", "main()", PRO_TK_NO_ERROR, 0); /* Check the status of Pro/E */ TEST_CALL_REPORT("ProEngineerStatusGet()", "main()", err, err != PRO_TK_NO_ERROR && err != PRO_TK_GENERAL_ERROR); } if (err != PRO_TK_NO_ERROR) { bad_status ++; if (bad_status > 3) { Terminate = 1; } continue; } bad_status = 0; if (!Terminate) { if (Action_done || (cur_time-5 > action_time)) { action_time = cur_time; action = USER_MESSAGE_ACTION; UserActionFunc( ua_msg, action); Action_done = 0; } } } ProEventProcess(); if (asy_flag == PRO_B_FALSE) { /*------------------------------------------------------------------*\ If the Pro/ENGINEER session was spawned by this application, restart Pro/ENGINEER after initial Pro/ENGINEER termination. (Restart is simple asynchronous model.) \*------------------------------------------------------------------*/ if (!connect_mode) { /* Now re-start Pro/E */ UserRestartProeTest(proe_command, text_path, argc, part_to_retrieve); } } ProTestErrlogClose(); return (0); } /*===========================================================================*\ Function : user_initialize() Purpost : demo another way to add user's menu item \*===========================================================================*/ extern "C" int user_initialize(int argc, char *argv[]) { char *pre = "Pre", *post = "Post"; ProError err; uiCmdCmdId cmd_id; /*------------------------------------------------------------------*\ Parse the command line \*------------------------------------------------------------------*/ ProUtilCommandLineParse( argc, argv ); /*------------------------------------------------------------------*\ Initialize termination notification function. \*------------------------------------------------------------------*/ err = ProTermFuncSet((ProTerminationAction)UserTerminateAction); TEST_CALL_REPORT("ProTermFuncSet()", "user_initialize()", err, err != PRO_TK_NO_ERROR); if( err != PRO_TK_NO_ERROR) fprintf(stderr, "\nTermination function is NOT SET !!!\n"); /*---------------------------------------------------------------------*\ Add new button to the menu bar \*---------------------------------------------------------------------*/ err = ProCmdActionAdd("-Pt Async", (uiCmdCmdActFn)ProTestShowMenu, uiProe2ndImmediate, TestAccessDefault, PRO_B_TRUE, PRO_B_TRUE, &cmd_id); TEST_CALL_REPORT("ProCmdActionAdd", "user_initialize", err, err != PRO_TK_NO_ERROR); err = ProMenubarmenuPushbuttonAdd( "Utilities", "-Pt Async", "-Pt Async", "Async mode test commands", "Utilities.psh_util_aux", PRO_B_TRUE, cmd_id, MSGFIL); TEST_CALL_REPORT("ProMenubarmenuPushbuttonAdd", "user_initialize", err, err != PRO_TK_NO_ERROR); err = ProCmdCmdIdFind("exit", &cmd_id); TEST_CALL_REPORT("ProCmdCmdIdFind()", "user_initialize()", err, err != PRO_TK_NO_ERROR && err != PRO_TK_GENERAL_ERROR); err = ProCmdBracketFuncAdd(cmd_id, (uiCmdCmdBktFn)UserTerm, "UserTerm", NULL); err = ProMenuFileRegister("Async Test", "tkasync.mnu", NULL); TEST_CALL_REPORT("ProMenuFileRegister()", "user_initialize()", err, err !=PRO_TK_NO_ERROR); err = ProMenubuttonActionSet("Async Test", "Async Test", (ProMenubuttonAction)ProMenuDelete, NULL, 0); TEST_CALL_REPORT("ProMenubuttonActionSet()", "user_initialize()", err, err !=PRO_TK_NO_ERROR); err = ProMenubuttonActionSet("Async Test", "Done/Return", (ProMenubuttonAction)ProMenuDelete, NULL, 0); TEST_CALL_REPORT("ProMenubuttonActionSet()", "user_initialize()", err, err !=PRO_TK_NO_ERROR); err = ProMenubuttonActionSet("Async Test", "-Retrieve Obj", (ProMenubuttonAction)UserActionFunc, NULL, USER_RETRIEVE_ACTION); TEST_CALL_REPORT("ProMenubuttonActionSet()", "user_initialize()", err, err !=PRO_TK_NO_ERROR); err = ProMenubuttonLocationSet("Async Test", "-Exit", -1); TEST_CALL_REPORT("ProMenubuttonLocationSet()", "user_initialize()", err, err !=PRO_TK_NO_ERROR); err = ProMenubuttonActionSet("Async Test", "-Exit", (ProMenubuttonAction)UserActionFunc, NULL, USER_EXIT_ACTION); TEST_CALL_REPORT("ProMenubuttonActionSet()", "user_initialize()", err, err !=PRO_TK_NO_ERROR); err = ProNotificationSet(PRO_MDL_DISPLAY_PRE, (ProFunction)UserNotificationFunc); TEST_CALL_REPORT("ProNotificationSet()", "user_initialize()", err, err !=PRO_TK_NO_ERROR); return(0); } /*===========================================================================*\ Function : user_terminate() Purpost : end Pro/T application \*===========================================================================*/ extern "C" void user_terminate() { ProError err; err = ProNotificationUnset(PRO_MDL_DISPLAY_PRE); TEST_CALL_REPORT("ProNotificationUnset()", "user_terminate()", err, err !=PRO_TK_NO_ERROR); TEST_CALL_REPORT("user_terminate()", "user_terminate()", PRO_TK_NO_ERROR, 0); printf("user terminate\n"); Terminate = 1; } /*===========================================================================*\ Function : UserTerm() Purpose : set global variable Terminate to 1 telling Pro/DEVELOP application that Pro/E terminated \*===========================================================================*/ int UserTerm() { ProLine wcmd; ProError err; Terminate = 1; ProStringToWstring(wcmd, "yes"); err = ProMacroLoad(wcmd); TEST_CALL_REPORT("ProMacroLoad()", "UserTerm()", err, err !=PRO_TK_NO_ERROR); return(0); } /*===========================================================================*\ Function : UserRestartProeTest() Purpose : test restarting Pro/E and simple async call \*===========================================================================*/ int UserRestartProeTest( char *pro_command, char *text_path, int arg_num, char *part_to_retrieve) { char name[PRO_NAME_SIZE]; ProType type; char name_type[PRO_FAMILY_NAME_SIZE + PRO_TYPE_SIZE + 2]; wchar_t w_name_type[PRO_FAMILY_NAME_SIZE + PRO_TYPE_SIZE + 2]; ProMdl model; char *proe; int w_id, ret = 0; ProError err; if(arg_num>3) if (arg_num==4) { proe=strrchr(pro_command, ' '); *proe='\0'; } else { return (0); } /* Start up Pro/ENGINEER */ fprintf(stderr, "\n*****\tRe-Starting Pro/Engineer\t*****\n"); fprintf(stderr, "\tProe command: \"%s\"\t\n", pro_command); fprintf(stderr, "\t\tPlease wait...\n"); err = ProEngineerStart(pro_command, text_path); TEST_CALL_REPORT("ProEngineerStart()", "UserRestartProeTest()", err, err !=PRO_TK_NO_ERROR); if (part_to_retrieve!=NULL) { fprintf(stderr, "\nRetrieving part %s\n", part_to_retrieve); /* Query for an object */ ret = ProUtilConfirmNameType(part_to_retrieve, name, &type); } else { /* Query for an object */ err = ProMessageDisplay(MSGFIL, "USER %0s", "Enter object name.type: "); TEST_CALL_REPORT("ProMessageDisplay()", "UserRestartProeTest()", err, err !=PRO_TK_NO_ERROR); err = ProMessageStringRead(PRO_NAME_SIZE+PRO_TYPE_SIZE+1, w_name_type); if (err == PRO_TK_NO_ERROR) { ProWstringToString(name_type, w_name_type); part_to_retrieve = name_type ; ret = ProUtilConfirmNameType(name_type, name, &type); } } /* Retrieve an object */ if (err == PRO_TK_NO_ERROR && ret > 0) { ProStringToWstring(w_name_type, name); err = ProMdlRetrieve(w_name_type, (ProMdlType)type, &model); TEST_CALL_REPORT("ProMdlRetrieve()", "UserRestartProeTest()", err, err !=PRO_TK_NO_ERROR); /* Display the object */ err = ProObjectwindowCreate(w_name_type, type, &w_id); TEST_CALL_REPORT("ProObjectwindowCreate()", "UserRestartProeTest()", err, err !=PRO_TK_NO_ERROR); err = ProMdlDisplay(model); TEST_CALL_REPORT("ProMdlDisplay()", "UserRestartProeTest()", err, err !=PRO_TK_NO_ERROR); fprintf(stderr, "After retrieving '%s' part\n", part_to_retrieve); fprintf(stderr, "Exit Pro/E\n"); } err = ProEngineerEnd(); TEST_CALL_REPORT("ProEngineerEnd()", "UserRestartProeTest()", err, err != PRO_TK_NO_ERROR); return (0); } /*---------------------------------------------------------------------*\ Function: UserTerminateAction() Purpose: called on Pro/E exit \*---------------------------------------------------------------------*/ void UserTerminateAction( ProeTerminationStatus term_type) { TEST_CALL_REPORT("ProTerminationAction()", "UserTerminateAction()", PRO_TK_NO_ERROR, 0); switch (term_type) { case PROTERM_EXIT: Terminate=1; fprintf(stderr, "\nMenu item 'EXIT' was picked\n"); break; } } /*============================================================================*\ Function : ProTestShowMenu() Purpose : Perform a Pro/E operation \*============================================================================*/ int ProTestShowMenu() { int action, menu_id; ProError err; err = ProMenuCreate(PROMENUTYPE_MAIN, "Async Test", &menu_id); TEST_CALL_REPORT("ProMenuCreate()", "ProTestShowMenu()", err, err != PRO_TK_NO_ERROR); err = ProMenuProcess("Async Test", &action); TEST_CALL_REPORT("ProMenuProcess()", "ProTestShowMenu()", err, err != PRO_TK_NO_ERROR); return(0); } /*============================================================================*\ Function : UserActionFunc() Purpose : Perform a Pro/E operation \*============================================================================*/ int UserActionFunc( ProAppData ptr, int opt) { char name[PRO_NAME_SIZE]; ProMdlType type; ProMdl model; ProCharLine msgbuf; char name_type[PRO_FAMILY_NAME_SIZE + PRO_TYPE_SIZE + 2]; wchar_t w_name_type[PRO_FAMILY_NAME_SIZE + PRO_TYPE_SIZE + 2]; ProError err; int w_id, ret=0; switch (opt) { case USER_RETRIEVE_ACTION : Action_done = 1; /* Query for an object */ err = ProMessageDisplay(MSGFIL, "USER %0s", "Enter object name.type: "); TEST_CALL_REPORT("ProMessageDisplay()", "UserActionFunc()", err, err !=PRO_TK_NO_ERROR); err = ProMessageStringRead(PRO_NAME_SIZE+PRO_TYPE_SIZE+1, w_name_type); if (err == PRO_TK_NO_ERROR) { ProWstringToString(name_type, w_name_type); ret = ProUtilConfirmNameType(name_type, name, (ProType*)&type); } /* Retrieve the object */ if (err == PRO_TK_NO_ERROR && ret > 0) { ProStringToWstring(w_name_type, name); err = ProMdlRetrieve(w_name_type, type, &model); TEST_CALL_REPORT("ProMdlRetrieve()", "UserActionFunc()", err, err !=PRO_TK_NO_ERROR); /* Display the object */ err = ProObjectwindowCreate(w_name_type, (ProType)type, &w_id); TEST_CALL_REPORT("ProObjectwindowCreate()", "UserActionFunc()", err, err !=PRO_TK_NO_ERROR); err = ProWindowActivate(w_id); TEST_CALL_REPORT("ProWindowActivate()", "UserActionFunc()", err, err !=PRO_TK_NO_ERROR); err = ProMdlDisplay(model); TEST_CALL_REPORT("ProMdlDisplay()", "UserActionFunc()", err, err !=PRO_TK_NO_ERROR); if (err == PRO_TK_NO_ERROR) { /* Confirm back to user */ sprintf(msgbuf, "Retrieved: %s", name_type); err = ProMessageDisplay(MSGFIL, "USER %0s", msgbuf); TEST_CALL_REPORT("ProMessageDisplay()", "UserActionFunc()", err, err !=PRO_TK_NO_ERROR); fprintf(stderr, "\t%s\n", msgbuf); } } break; case USER_MESSAGE_ACTION : sprintf(msgbuf, "In the loop. Terminate = %d", Terminate ); err = ProMessageDisplay(MSGFIL, "USER %0s", msgbuf); TEST_CALL_REPORT("ProMessageDisplay()", "UserActionFunc()", err, err !=PRO_TK_NO_ERROR); fprintf(stderr, "\t%s\n", msgbuf); break; case USER_EXIT_ACTION : if (!connect_mode) { err = ProEngineerEnd(); TEST_CALL_REPORT("ProEngineerEnd()", "UserActionFunc()", err, err != PRO_TK_NO_ERROR); fprintf(stderr, "\n\tHello World! After ProEngineerEnd()\n"); } else { err = ProEngineerDisconnect(&proe_handle, 20); TEST_CALL_REPORT("ProEngineerDisconnect()", "UserActionFunc()", err, err != PRO_TK_NO_ERROR); fprintf(stderr, "\n\tHello World! After ProDisconnectProe()\n"); } Terminate = 1; break; default : break; } return(0); } /*============================================================================*\ Function : UserNotificationFunc() Purpose : Test Notification function in async mode \*============================================================================*/ ProError UserNotificationFunc() { fprintf(stderr, "Notification function\n"); TEST_CALL_REPORT("ProMdlDisplayPre()", "UserNotificationFunc()", PRO_TK_NO_ERROR, 0); return (PRO_TK_NO_ERROR); }