Synchronous Application

In the last blog entry we saw a skeleton program which starts as soon as Pro/ENGINEER starts. The two functions user_initialize () and user_terminate () are called in succession. In a synchronous application the user_initialize () function is the first function to be which gets called and once it is complete, the user_terminate () function is called. We may put all our functions under user_initialize (). Please look at the API wizard for more details about the user_initialize () and the user_terminate () functions. API wizard is the best friend that any Pro/Toolkit developer can have.

We will build upon a skeleton model to craft our next application. The next application is going to write the mass properties of any model into the current working directory. The API we are going to use will be ProSolidMassPropertyGet (). For this application we will also create a menu which is going to invoke the Pro/Toolkit application. For creating the menu button we will be using the API’s ProCmdActionAdd () and ProMenubarmenuPushbuttonAdd (). Refer to the API wizard for more details about the API’s.

In this code we will be extracting the mass property information of any model. The source file should be named as “tkapp.c” and the same process of compiling the code and executing the application as mentioned in my
previous post. In the application there will be the #TKButton on the #File menu to execute the application.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/********************************
Pro/Toolkit Includes files
*********************************/

#include "ProToolkit.h"
#include "ProFeature.h"
#include "ProElemId.h"
#include "ProExtrude.h"
#include "ProModFeat.h"
#include "ProStdSection.h"
#include "ProElement.h"
#include "ProElempath.h"
#include "ProFeatType.h"
#include "ProFeatForm.h"
#include "ProSelection.h"
#include "ProSection.h"
#include "ProMenu.h"
#include "ProMenuBar.h"
#include "ProUtil.h"
#include "ProSolid.h"
#include "ProDtmCsys.h"

/********************************
Global definitions
*********************************/

static ProError status;
static wchar_t MSGFIL[] = {'f','i','l','e','.','t','x','t','\0'};

/********************************
Constructors
*********************************/

int call_back ();
int write_display(ProMassProperty );

/********************************
Access function Start - This function determines whether the button created in the application should be visible in Pro/ENGINEER or not
*********************************/


static uiCmdAccessState TestAccessDefault (uiCmdAccessMode access_mode)
{
ProMode mode;
ProModeCurrentGet( &mode );

if ( mode == PRO_MODE_PART
mode == PRO_MODE_COMPOSITE
mode == PRO_MODE_SHEET_METAL
mode == PRO_MODE_ASSEMBLY
)
return ACCESS_AVAILABLE;
return ACCESS_INVISIBLE;
}

/********************************
user_initialize Function - In this section a menu button is created and which has an call back action
*********************************/

int user_initialize()
{
uiCmdCmdId cmd_id;

status=ProCmdActionAdd("TKButton", (uiCmdCmdActFn) call_back, uiProe2ndImmediate,TestAccessDefault, PRO_B_TRUE, PRO_B_TRUE, &cmd_id);
status = ProMenubarmenuPushbuttonAdd("File", "TKButton", "TKButton", "TKButton","File.psh_rename", PRO_B_TRUE, cmd_id,MSGFIL);

return (0);
}

/********************************
call_back Function Start - This is the call back function which is getting called when the TKButton menu is pressed. All the action happend here.
*********************************/

int call_back ()
{
ProMdl model;
ProSolid solid;
ProName name;
ProCharName cname;
ProSelection *sel;
int n_sel;
ProModelitem modelitem;
ProMassProperty mass_prop;
ProCsys p_csys;
ProName csys_name;

status = ProMdlCurrentGet((ProMdl)&solid);
if(status)
{
print_s("The model could not be retrieved");
}
status = ProSelect("csys",1,NULL,NULL,NULL,NULL,&sel,&n_sel);
if(status)
{
return 0;
}
status = ProSelectionModelitemGet(sel[0], &modelitem);

status = ProModelitemNameGet(&modelitem,csys_name);

status = ProSolidMassPropertyGet(solid,csys_name, &mass_prop);

write_display(mass_prop);// File output Function called

return(0);
}

/********************************
File write and display Function Start
*********************************/

int write_display(ProMassProperty massprop)
{
FILE *fp;
ProName w_filename;
ProInfoWindowLocation win_location = { 0.5, 0.2 };
ProInfoWindowSize win_size = { 15, 80 };
char *fname = "output.txt";
fp = fopen (fname, "w");
/* Any command in between fopen and fprintf results in compiler error*/

fprintf(fp, "\nThe mass and volume of the part");
fprintf(fp, "\nMass = %f and Volume = %f ", massprop.mass, massprop.volume);
fprintf(fp, "\nCenter of gravity with respect to the selected CSYS");
fprintf(fp, "\nX Y Z %f %f %f \n", massprop.center_of_gravity[0], massprop.center_of_gravity[1], massprop.center_of_gravity[2]);
fclose(fp);
/* important Point, close the file before using "ProInfoWindowDisplay", or it will print nothing*/


// Displaying the information window in Pro/ENGINEER
ProStringToWstring(w_filename,"output.txt");
status = ProInfoWindowDisplay(w_filename,&win_location, &win_size );
return (0);
}
void user_terminate()
{

}
/* The text editor of blogger is very bad, please execuse my poor formatting of the code*/
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Handles: Handles as the name suggest is the way data is handled in Pro/Toolkit. There are two types of handled in Pro/Toolkit - Opaque handle and Data Handle. The Data Handle is defined as structure and access structure members we can use dot operator (.) or pointer (->). Examples of DH are ProModelitem, ProGeomitem, ProExtobj, ProFeature and ProProcstep. For more details about Data Structure in C please refer to the following link.
The second type of Handled is called Opaque handles. Opaque handles are the black box from which the data can be extracted by other supporting API’s. Examples of Opaque handles are ProSurface, ProEdge and ProCsys.

At this point I want to reiterate that the API Wizard will be the “Best Friend” of any Pro/Toolkit developer. Please make sure that you go through the API Wizard whenever you want to write any code. My next application will be an Asynchronous application in the next post. I will be posting the entries on regular intervals, do leave me a comment; I will be happy to interact with you. In case you require the source code and other files from this application, feel free to write to me at
Alxn.Page@gmail.com. Thanks for your valuable time in reading through. Ciao...
_________________________________________________________
TRADEMARKS: Pro/ENGINEER, Wildfire, Pro/Toolkit, Pro/Develop, J-Link, Pro/Web.Link and VB API are the registered trademarks of PTC (http://www.ptc.com/). All other products or services mentioned in this blog are the trademarks or service marks of their respective companies or organizations.

5 comments:

  1. Replies
    1. Sarika you may help me for learning Pro/Toolkit

      Delete
  2. Dear Alxn I want to learn Pro/Toolkit from scratch kindly help me with example of Pro/toolkit application

    ReplyDelete
  3. Your blog are very enlightening. Thank you again.

    ReplyDelete
  4. Thank you for the tutorial. Its was very enlightening.

    ReplyDelete