Simcenter STAR-CCM+ Macro that writes the sim file details to a file

2024-08-01T14:25:43.000-0400
Simcenter STAR-CCM+ CAD Clients Simcenter STAR-CCM+ Simcenter STAR-CCM+ Virtual Reality Teamcenter Share Simcenter STAR-CCM+ Viewer Simcenter STAR-CCM+ Application Specific Solutions

Summary

If you have the need to know how to write details like the name of a Simcenter STAR-CCM+ simulation to a text file, through a JAVA macro, then the following article will explain how to do this. We'll take the name of the simulations where this macro was run and we'll print that to a text file, but be aware that we can also export things like physics models, mesh size, etc.


Details

As the summary mentions, if you’d like to know in which STAR-CCM+ simulation has your JAVA Macro been used/opened in, then we can create a text file with the names of the simulation where this has happened. In this article, we’ll explain how to do this and, if needed, you can just add these lines of code to your own macro.

With the macro attached to this article, we’ll be able to check if this text file has been created. If it hasn’t, then we’ll create one. If it has been created, then we’ll just add the name of the .sim file where the macro was opened from.

To start, we will focus on briefly explaining the different parts of the code from the JAVA macro and what they do, so that you have a better understanding of it and modify it to your needs. We can encapsulate almost the entire macro in one try-catch statement. Here’s the code:

String mySimName = getSimulation().getPresentationName();
File simFilesOpened = new File("C:\\Users\\user_name\\Desktop\\simFilesOpened.txt");
                             
//Create the file

//First we'll check if a file called 'simFilesOpened.txt' already exists. If it doesn't, we'll create it here.
    try {
        if (simFilesOpened.createNewFile()) {
            simulation_0.println("'simFilesOpened' text file not found. Creating it now...\n Created File 'simFilesOpened.txt'");
        } else {
            simulation_0.println("'simFilesOpened' text file found. Writing data...");
        }

//Write Content

//In the file we created above, we'll write "This JAVA macro has been opened in simulation  ->" and the name of the simulation where

//this JAVA macro has been run.

//If there's an error, like the JAVA macro doesn't have write permission/rights, then we'll output to the terminal "An error occurred while //writing data to text file", so that we can isolate where the problem comes from.
FileWriter writer = new FileWriter("C:\\Users\\user_name\\Desktop\\simFilesOpened.txt",true);
BufferedWriter out = new BufferedWriter(writer);
out.write("This JAVA macro has been opened in simulation -> " + mySimName + "\n");
out.close();

          } catch (IOException e) {
simulation_0.println("An error occurred while writing data to text file");
               e.printStackTrace();
    }


We have created some comments on each part of the JAVA macro, but let's dissect it a little bit more. The first part of the macro will declare the variables “mySimName” and “simFilesOpened”. For the latter, the path where the new file will get created has to be the complete path, starting from the Drive. This gives us the liberty of saving the macro at a certain location that may or may not be the same location where our .sim files exist. In this case, we are saving the file in the C drive.

The second part of the macro will create the file. The if-else statement is there because, if we already have the file called “simFilesOpened.txt” on our computer, on the location specified beforehand, then it will say that the file was found and that it’s writing data. If the text file hasn’t been found, then it’ll say that the text file wasn’t found, and it’s being created.

Last part will write the data into the file. We’re using the FileWriter class of the java.io package. The following line will be the part of the code that appends text to the file:

out.write("This JAVA macro has been opened in simulation -> " + mySimName + "\n");

 

As a final comment, after we include these lines of code to other JAVA macros, we could modify the code to write to a text file things like what version of STAR was used with the macro, physics models, mesh size, etc. This could be a tool to make statistics about your simulations a more automated process, as you could not only write to a text file, but you could also create a .csv with these details.

We'll attach the JAVA Macro and a dummy simulation to this Knowledge Base Articles, however we highly encourage the users to modify the macro to fit their needs or copy the code into their own JAVA macros (with a few modifications, of course) and to try this out on your end. We just need to open different .sim files and run the macro (or run on our dummy .sim file and then rename that .sim file), so that we see how it writes the name of the simulations in which it was run.

If you have any questions about the code or something else, please reach out to your Dedicated Support Engineer.
 

KB Article ID# KB000112646_EN_US

Contents

SummaryDetails

Associated Components

Simcenter STAR-CCM+ Clients