StarCCM - Save all Scenes/Plots via macro

Source: Steve-Portal

This macro applies to the following two cases:

  1. when you need to extract the images from a simulation file which is not open or running
  2. when the simulation file is open, but the number of scene is high or plotting| is slow due to model complexity.

In both cases, the usage of the following macro (see at the end of the page) will help you save time, as all the scene are automatically extracted and saved. Beware, all desired settings of the scenes and plots need to be set before saving - as you normally would do, too.

The macro below loops through all the scenes of the simulation by means of Java collections.

        Simulation simulation_0 = getActiveSimulation();
 
        String dir = simulation_0.getSessionDir(); //get the name of the simulation's directory
        String sep = System.getProperty("file.separator"); //get the right separator for your operative system
 
        for (Scene scene : simulation_0.getSceneManager().getScenes()) {
            scene.printAndWait(resolvePath(dir + sep + scene.getPresentationName() + ".jpg"), 1, 800, 600);
        }

The code loops through the collection of the Scenes of the simulation and for each one saves a .jpg image (with an 800×600 resolution) in the same directory of the sim file. The name of the file is the same as the name of the scene.

In the same fashion one can build a macro to hardcopy all the plots of the current simulation:

        Simulation simulation_0 = getActiveSimulation();
 
        String dir = simulation_0.getSessionDir(); //get the name of the simulation's directory
        String sep = System.getProperty("file.separator"); //get the right separator for your operative system
 
 
        for (StarPlot plot : simulation_0.getPlotManager().getObjects()) {
            plot.encode(resolvePath(dir + sep + plot.getPresentationName() + ".jpg"), "jpg", 800, 600);
        }

The attached macro are ready to run for the purpose.

Note: to execute the macro in batch use the following sintax from a shell:

  installation_path/starccm+ -batch HardcopyAllScenes.java your_filename.sim

If the simulation is already open, just execute the macro using the buttom “play macro” from the top menu bar in main GUI.

Valid from STAR-CCM+ 8.02.

Implementation into your macro

If you have a finished simulation and want to print out the graphs as a post step, then you'll find prepared macros at the end of the page.

On the other hand, if you have a macro which works for you, then you might not want to modify it very much. In that case you can extend your macro adding the following java method. It does all the work indepentend from other parts of code. All you have to do is run saveGraphs() somewhere in your execute() method tree. You'll find an example at the end of this page, and in the supplementry example files of this page: StarCCM - Use Try/catch Exception handler

 private void saveGraphs() {
        Simulation sim = getActiveSimulation();
 
        String dir = sim.getSessionDir(); //get the name of the simulation's directory
        String sep = System.getProperty("file.separator"); //get the right separator for your operative system
 
 
        for (Scene scn: sim.getSceneManager().getScenes()) {
            sim.println("Saving Scene: " + scn.getPresentationName());
            scn.printAndWait(resolvePath(dir + sep + scn.getPresentationName() + ".jpg"), 1, 1920, 1080);
        }
 
        for (StarPlot plt : sim.getPlotManager().getObjects()) {
            sim.println("Saving Plot: " + plt.getPresentationName());
            plt.encode(resolvePath(dir + sep + plt.getPresentationName() + ".jpg"), "jpg", 1920, 1080);
        }
 
        sim.println("Saving completed");
   }

Hardcopy all Scenes

HardcopyAllScenes.java
import star.common.*;
import star.vis.*;
 
public class HardcopyAllScenes extends StarMacro {
 
    public void execute() {
 
        Simulation sim = getActiveSimulation();
 
        String dir = sim.getSessionDir(); //get the name of the simulation's directory
        String sep = System.getProperty("file.separator"); //get the right separator for your operative system
 
 
        for (Scene scn: sim.getSceneManager().getScenes()) {
            sim.println("Saving Scene: " + scn.getPresentationName());
            scn.printAndWait(resolvePath(dir + sep + scn.getPresentationName() + ".jpg"), 1, 1920, 1080);
        }
    }
}

Hardcopy all Plots

HardcopyAllPlots.java
import star.common.*;
import star.base.neo.*;
 
public class HardcopyAllPlots extends StarMacro {
 
  public void execute() {
 
	Simulation sim = getActiveSimulation();
 
        String dir = sim.getSessionDir(); //get the name of the simulation's directory
        String sep = System.getProperty("file.separator"); //get the right separator for your operative system
 
 
	for (StarPlot plt : sim.getPlotManager().getObjects()) {
	    sim.println("Saving Plot: " + plt.getPresentationName());
	    plt.encode(resolvePath(dir + sep + plt.getPresentationName() + ".jpg"), "jpg", 1920, 1080);
	}
 
  }
}

Just Run and Save all Graphs

runAndSaveGraphs.java
import star.common.*; 
import star.vis.*;   
 
public class runAndSaveGraphs extends StarMacro {  
 
  public void execute() {           
    Simulation sim = getActiveSimulation();
 
    // run until stop crits are met
    sim.getSimulationIterator().run();
 
    saveGraphs();
  }
 
 
  private void saveGraphs() {
    Simulation sim = getActiveSimulation();
 
    String dir = sim.getSessionDir(); //get the name of the simulation's directory
    String sep = System.getProperty("file.separator"); //get the right separator for your operative system
 
    for (Scene scn: sim.getSceneManager().getScenes()) {
      sim.println("Saving Scene: " + scn.getPresentationName());
      scn.printAndWait(resolvePath(dir + sep + scn.getPresentationName() + ".jpg"), 1, 1920, 1080);
    }
 
    for (StarPlot plt : sim.getPlotManager().getObjects()) {
      sim.println("Saving Plot: " + plt.getPresentationName());
      plt.encode(resolvePath(dir + sep + plt.getPresentationName() + ".jpg"), "jpg", 1920, 1080);
    }
 
    sim.println("Saving completed");
  }  
}

Export Report Values to CSV

This script tries to read out the value of a report and write it to a csv file with the same name as the report.

Possible Issues:

  • Report is not readable, such as incomplete setup
  • Report name is not a suitable file name (avoid special characters)
exportReports.java
import star.common.*; 
import star.vis.*;   
import star.base.report.Report;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
 
public class exportReports extends StarMacro {  
  public void execute() {           
    Simulation sim = getActiveSimulation();
 
    // run until stop crits are met
    sim.getSimulationIterator().run();
 
    saveReports();
  }
 
  private void saveReports() {
    Simulation sim = getActiveSimulation();
 
    String dir = sim.getSessionDir(); //get the name of the simulation's directory
    String sep = System.getProperty("file.separator"); //get the right separator for your operative system
 
    Collection<Report> reports = sim.getReportManager().getObjects();
 
    for (Report rep : reports) {
      String repName = rep.getPresentationName();
      sim.println("Exporting Report: " + repName);
 
      try (PrintWriter pw = new PrintWriter(dir + sep + repName + ".csv")) {
        pw.write(String.valueOf(rep.monitoredValue()) + "\n");
      } catch (Exception e) {
        sim.println(e.toString());
      }
 
  }
}  

Export the Values from a Plot

exportReports.java
import star.common.*; 
import star.vis.*;   
import java.util.*;
 
public class plotDataExporter extends StarMacro {  
  public void execute() {           
    Simulation sim = getActiveSimulation();
 
    // run until stop crits are met
    sim.getSimulationIterator().run();
 
    exportPlotData();
  }
 
  private void exportPlotData() {
    Simulation sim = getActiveSimulation();
 
    String dir = sim.getSessionDir(); //get the name of the simulation's directory
    String sep = System.getProperty("file.separator"); //get the right separator for your operative system
 
    for (StarPlot plt : sim.getPlotManager().getObjects()) {
      String pltName = plt.getPresentationName();
 
      sim.println("Exporting Data of Plot: " + pltName);
      plt.export(dir + sep + pltName + ".csv", ",");                  
    }
  }  
}
guide/starccm/save_all_scenesplots.txt · Last modified: 2019/02/27 13:57 by savoss
Back to top
CC Attribution-Share Alike 3.0 Unported
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0