Showing posts with label user_initialize(). Show all posts
Showing posts with label user_initialize(). Show all posts

Getting Started

In this section we will build a skeleton synchronous Pro/Toolkit application from the scratch. You should have a compiler to compile code. In this example I will be using the Microsoft Visual C++ 6 and Pro/ENGINEER Wildfire 2.0. I will use the command prompt for compiling the code with the help of PTC provided standard make files. Create a folder and copy the "make_basic" file from the "[TK_LOADPOINT]/ [MACHINE]/obj" folder. Now open the file with any text editor and edit the text in the file as shown below:
~~~~~~~~~~~~~~~~~~~~
Line#23 -->> EXE = pt_basic.exe change it to EXE = tkapp.exe ( we will call our application "tkapp")
Line#24 -->> EXE_DLL = pt_basic.dll change it to EXE_DLL = tkapp.dll
Line#27 -->> PROTOOL_SRC = ../../../protoolkit change it to PROTOOL_SRC = [PROE_LOADPOINT]/protoolkit (this is required for the compiler to know the location of the library file and the include files of Pro/Toolkit)
Line#29 -->> PRODEV_SRC = ../../../prodevelop change it to PRODEV_SRC = [PROE_LOADPOINT]/prodevelop (this is required for the compiler to know the location of the library file and the include files of Pro/Develop)
Line#73 -->> OBJS = pt_basic_src.obj pt_utils.obj change it to OBJS = tkapp.obj (we indicate that there will be an intermediate file with the name tkapp.obj created)
Line#94 -->> pt_basic_src.obj: $(PROTK_APPLS)/pt_basic/pt_basic_src/pt_basic_src.c
$(CC) $(CFLAGS) $(PROTK_APPLS)/pt_basic/pt_basic_src/pt_basic_src.c change it to tkapp_src.obj: tkapp.c
Remove the line starting with "pt_utils.obj" completely.
~~~~~~~~~~~~~~~~~~~~
Save the file and exit the editor. Now the second task is to get the source code going. To start with, we will write a small program in Pro/Toolkit. As mentioned in my previous blog, for a synchronous application the requirement is to have a user_initialize() and a user_terminate() function. We will open a text editor again and write the code as shown below (The name of the code file should be tkapp.c):
~~~~~~~~~~~~~~~~~~~~
#include "ProToolkit.h" // This include file is a must
/********************************/
int user_initialize() // This is a must for sync applications
{
printf("\nHello Pro/Toolkit World !!\n");
return (0);
}
/********************************/
void user_terminate() // This is a must for sync applications
{
printf("\nGoodbye !!\n");
}
~~~~~~~~~~~~~~~~~~~~
Save the file as tkapp.c in the same directory which was created in step 1. Open a command prompt and then go to the directory which was created in step 1. At this point you will have to set the environment for using Microsoft Visual C++ tools on the command window. You will type something like this on the command prompt "C:\Program Files\Microsoft Visual Studio\VC98\Bin\VCVARS32.bat" (The path could vary if you have the "Microsoft Visual Studio", installed on a different folder). After running this batch file (VCVARS32.bat), you will get a system message as “Setting environment for using Microsoft Visual C++ tools.”
Once the environment is set in the command prompt, we will compile the program “tkapp.c” by using the namke function, we will type “nmake -f make_basic”. This is going to compile the program and give us the “tkapp.exe” which is our first executable Pro/Toolkit application. Please refer to the command line snapshot below for more details.

Once the application is ready we will have to just make a protk.dat file to register the application in Pro/ENGINEER. The protk.dat file is a text file which has .dat extension for Pro/ENGINEER to recognize and register the Pro/Toolkit application. I am pasting the contents of the protk.dat file for the above application below:
~~~~~~~~~~~~~~~~~~~~
name tkapp
Startup exe
Allow_stop True
Delay_start False
exec_file tkapp.exe
text_dir .
END
~~~~~~~~~~~~~~~~~~~~
For details about the contents of the protk.dat file please refer to PTC guide for Pro/Toolkit (tkuse.pdf). After having the protk.dat file you may launch Pro/ENGINEER from the same directory and get the application running as shown in the snapshot below:
___________________________________________________________________
This is a skeleton application to have us up and running. The subsequent blog entries will deal with more complex features of Pro/Toolkit. I hope that all the readers will be able to follow the steps and complete the application as suggested in this blog. Please feel free to write to me and I will be glad to answer. Do subscribe to this blog and get updates as and when it happens. 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…. Bye for now, God Bless!!
_________________________________________________________
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.

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.