Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations The Obturator on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using JLink (Java OTK Free) to Run Predefined FEA Simulation

lm222

Mechanical
May 21, 2025
2
Hello everyone! My company uses Creo 7.0.9. I am utilizing J-Link (Java Object Toolkit free) to create some programs to automate some repetitive tasks. I need your help in automating the running of predefined FEA simulations, or at least fixing my code so far...


_______________________________________________________________________________________________


Problem Background / Goal:





One of the features I want to have in my J-Link programs is the ability to rerun predefined simulations from code... meaning I as the engineer set up everything from conditions to loads, and get the simulation all ready to go, and my J-Link application really just has to hit the run button. The use case here is: I have other already functioning j-link code to change parameters of loads, so the idea is in the future, I can have some automated way of testing 15 different loads, running the FEA each time, saving results, and maybe even making me a graphic or something like that...
____________________________________________________________________________________________
Efforts so far:





I have attempted a few ways to do this.


1. I successfully created a map key that opens the simulate environment, opens the analysis and design study window and clicks run. This works perfectly. When I am in creo. I just hit F2, and then my simulation runs... I then attempted to use J-Link to run the mapkey, but Jlink does not do this. There are no errors no crashes, and nothing in the Trail.txt file, but also nothing happens when I run the program. I have attached code and my config.pro file with the mapkey below in case you want to see... Eventually I gave up on this.


2. I attempted directly running each of the macros contained within the mapkey file in JLink using session.RunMacro("MACRO TEXT HERE") and session.ExecuteMacro(). This ended up working (sort of)... I was able to run the first few macros to open up the simulation environment and to open the analysis and design studies window, but after that it just stops. Regardless of what windows I activate or what I try to do, it refuses to actually run the FEA. I basically want the program to hit the green flag button, but it will not.


3. I have tried to find a way to do this natively through the creo API, but so far I have not found anything promising. I tried to use pushbutton to simulate a button click, but I just get a bunch of errors. I am pretty sure the API isn't designed for me to use this to simulate clicking the run button, but correct me if there is a good way to do this...





_________________________________________________________________________________________________





What I am looking to hear from you





1. does anyone know if this is even allowed/possible? The thought occurred to me that perhaps this is intentionally blocked with J-Link and only possible with the full OTK. Do I need to buy something, so this gets enabled?


2. Is this some sort of permissions issue? Is the program not allowed to run simulations?


3. Is there any obvious error in my code? Do you have any fixes or suggestions for me?


4. Is there another/better way to do this?





I would greatly appreciate any concrete sample code you can provide to help me solve this issue.





_________________________________________________________________________________________________


config.pro file containing Mapkey for running my FEA





mapkey $F2 @MAPKEY_NAMEEnable Jlink to automate running FEA studies;\
mapkey(continued) @MAPKEY_LABELLinkRunFEA;\
mapkey(continued) ~ Activate `main_dlg_cur` `page_Applications_control_btn` 1;\
mapkey(continued) ~ Command `ProCmdAppMechanica` 1;~ Command `ProCmdSimMechSol` ;\
mapkey(continued) ~ Activate `run` `RunToolBtn`;~ Activate `UI Message Dialog` `yes`;\
mapkey(continued) ~ Activate `UI Message Dialog` `no`;~ Activate `run` `ClosePush`;\
mapkey(continued) ~ Command `ProCmdAppMechanicaExit`;


__________________________________________________________________________________________________


An attempt to run the FEA using directly(note: I have tried adding 5 second delays between all macros just to give creo a moment to enter the simulate environment properly before spamming another macro through J-Link, but it did not make a difference).

method call:


runFEAAnalysis(session);

method:

public void runFEAAnalysis(Session session) {
try {
System.out.println("Starting FEA analysis...");

// Step 1: Activate the Applications tab
session.RunMacro("~ Activate `main_dlg_cur` `page_Applications_control_btn` 1;");
System.out.println("Activated Applications tab.");

// Step 2: Enter Simulate mode
session.RunMacro("~ Command `ProCmdAppMechanica` 1;");
System.out.println("Entered Simulate mode.");

// Step 3: Open the Analysis and Design Studies window
session.RunMacro("~ Command `ProCmdSimMechSol`;");
System.out.println("Opened Analysis and Design Studies window.");

// Step 4: Click the green flag to run the simulation
session.RunMacro("~ Activate `run` `RunToolBtn`;");
System.out.println("Clicked the green flag to run the simulation.");

// Step 5: Handle file overwrite prompt
session.RunMacro("~ Activate `UI Message Dialog` `yes`;");
System.out.println("Handled file overwrite prompt.");

// Step 6: Handle diagnostics prompt
session.RunMacro("~ Activate `UI Message Dialog` `no`;");
System.out.println("Handled diagnostics prompt.");

// Step 7: Close the results window
session.RunMacro("~ Activate `run` `ClosePush`;");
System.out.println("Closed results window.");

// Step 8: Exit Simulate mode
session.RunMacro("~ Command `ProCmdAppMechanicaExit`;");
System.out.println("Exited Simulate mode.");

System.out.println("FEA analysis completed successfully.");
} catch (jxthrowable e) {
System.err.println("Error during FEA analysis: " + e.getMessage());
e.printStackTrace();
}
}
___________________________________________________________________________________________________
Attempt using MapKey...

attempted method calls:
executeMacroAsync(session, "F2");
executeMacroAsync(session, "$F2");
executeMacroAsync(session, "LinkRunFEA");
executeMacroAsync(session, "Enable Jlink to automate running FEA studies");

method (2 different approaches):
public void executeMacroAsync(Session session, String macroCommand) {
try {
System.out.println("Running macro: " + macroCommand);

// Construct the mapkey command string
String macroString = "~ Activate `proe_win`; %" + macroCommand + ";";

try {
Thread.sleep(5000); // Add a delay
} catch (InterruptedException e) {
System.err.println("Thread was interrupted: " + e.getMessage());
Thread.currentThread().interrupt(); // Restore the interrupted status
}

// Run the macro
session.RunMacro(macroString);
System.out.println("RunMacro executed successfully for mapkey: " + macroCommand);

try {
Thread.sleep(5000); // Add another delay
} catch (InterruptedException e) {
System.err.println("Thread was interrupted: " + e.getMessage());
Thread.currentThread().interrupt(); // Restore the interrupted status
}

// Step 2: Cast Session to WSession and execute the macro explicitly
if (session instanceof WSession) {
WSession wSession = (WSession) session;
wSession.ExecuteMacro();
System.out.println("ExecuteMacro executed successfully.");
//JOptionPane.showMessageDialog(null, "Executing Macro Command!");
} else {
System.err.println("Session is not an instance of WSession. ExecuteMacro cannot be called.");
//JOptionPane.showMessageDialog(null, "Unable to connect to Wsession");
}

// Flush the current window to process UI updates
//session.FlushCurrentWindow();
System.out.println("FlushCurrentWindow executed successfully.");

} catch (jxthrowable e) {
System.err.println("Error executing macro: " + e.getMessage());
e.printStackTrace();
}
}



public void executeMacroAsync(Session session, String macroCommand) {
try {
System.out.println("Running macro: " + macroCommand);

// Step 1: Activate the Applications tab
String activateApplicationsTab = "~ Activate `main_dlg_cur` `page_Applications_control_btn` 1;";
session.RunMacro(activateApplicationsTab);
System.out.println("Activated Applications tab.");

// Step 2: Construct the mapkey command string
// Escape special characters if necessary
String macroString = "~ Command `$" + macroCommand.replace("`", "\\`") + "`;";
System.out.println("Constructed macro string: " + macroString);

// Step 3: Run the macro
session.RunMacro(macroString);
System.out.println("RunMacro executed successfully for mapkey: " + macroCommand);

// Step 4: Flush the current window to process UI updates
session.FlushCurrentWindow();
System.out.println("FlushCurrentWindow executed successfully.");
} catch (jxthrowable e) {
System.err.println("Error executing macro: " + e.getMessage());
e.printStackTrace();
}
}
 
Replies continue below

Recommended for you

For similar tasks I used a combination of mapkeys and AutoIt.

More of a "me" problem, I never could get a grip on J-link, but had no trouble getting AutoIt to get Pro/E/Wildfire/Creo to do what I wanted. And, yes, the bit about figuring out if the mapkeys and Pro/E had finished doing what they were supposed to do was a challenge.

A weirder part was this: AutoIt has a function that can detect that a window with some "name" exists. At first the software would just blow by those checks, but I could not see the target window. Turned out the clever tykes at PTC made all the windows exist at some point, just with negative screen coordinates and inactive. They also weren't using Windows UI functions for menus and other UI features.

At least with AutoIt I could see on-screen what was happening.

I wish I could help more, but there were too many hurdles that I could not overcome with J-Link.
 
I appreciate the suggestion. I will look into it for the future. For this use case, ideally I would love to stick with J-Link and JAVA in general because of some of the other applications I am connecting to my code.
 
AutoIt was originally created to do any job an admin might do (provided they have rights to do so,) so if you have Java components I am sure it can communicate with them as well.

As I said, I wish I'd had more luck with J-Link. At the time PTC appeared to have added it as a marketing check mark rather than a first order tool. The fact they had no examples and did not revise their Pro/E code to use it was a huge impediment. This from having used the C API until I found that the way Pro/E passed arguments was controlled by the order in which parameters/dimensions were created in the model and were not passed with the names.

As a temp fix, AutoIt can certainly monitor for almost any visible characteristic and would be trivial to have it just waiting for the Run button to show up and push it for you. Just keep all the rest of your work as-is and expand the J-Link/Java part as you wish. It can also monitor processes to see what is running and let you make use of that information.
 
As to the particulars:

2. I attempted directly running each of the macros contained within the mapkey file in JLink using session.RunMacro("MACRO TEXT HERE") and session.ExecuteMacro(). This ended up working (sort of)... I was able to run the first few macros to open up the simulation environment and to open the analysis and design studies window, but after that it just stops. Regardless of what windows I activate or what I try to do, it refuses to actually run the FEA. I basically want the program to hit the green flag button, but it will not.

This seems to me like the J-Link application is waiting for the mapkey macro to end, but it cannot get that info because transfer of control is made to the FEA app. When you run it by hand you are still operating within the application environment, but J-Link is running outside of it.

This is a discussion of mapkeys via J-Link: https://community.ptc.com/t5/3D-Part-Assembly-Design/J-Link-running-mapkey-recursively/td-p/18656

You can search the Community for posts mentioning jlink and j-link.
 

Part and Inventory Search

Sponsor