Providing an Interactive Dialog

You can edit a macro to provide an interactive dialog.

Simcenter STAR-CCM+ offers a great many visualization controls that let you customize the appearance of the displays in your simulation. Settings for these controls are captured in the recording of macros. For example, a simple macro was created to add a geometry scene to a simulation and change the Color Mode property of the geometry displayer to Constant.



The next step in editing the macro involves changing the constant color to red.



The macro, named partcolor.java, contains the following code:

// STAR-CCM+ macro: partcolor.java
package macro;
import java.util.*;
import star.common.*;
import star.base.neo.*;
import star.vis.*;
public class partcolor extends StarMacro {
  public void execute() {
    Simulation simulation_1 = 
      getActiveSimulation();
    simulation_1.getSceneManager().createGeometrySceneAndWait("Geometry Scene", "Outline", "Geometry", 1);
    Scene scene_1 = 
      simulation_1.getSceneManager().getScene("Geometry Scene 1");
    scene_1.initializeAndWait();
    PartDisplayer partDisplayer_3 = 
      ((PartDisplayer) scene_1.getDisplayerManager().getDisplayer("Outline 1"));
    partDisplayer_3.initialize();
    PartDisplayer partDisplayer_4 = 
      ((PartDisplayer) scene_1.getDisplayerManager().getDisplayer("Geometry 1"));
    partDisplayer_4.initialize();
    PartDisplayer partDisplayer_5 = 
      ((PartDisplayer) scene_1.getHighlightDisplayer());
    partDisplayer_5.initialize();
    CurrentView currentView_1 = 
      scene_1.getCurrentView();
    currentView_1.setInput(new DoubleVector(new double[] {-0.5333999395370483, 1.268258864962263, 0.0}), new DoubleVector(new double[] {-0.5333999395370483, 1.268258864962263, 14.471860805953908}), new DoubleVector(new double[] {0.0, 1.0, 0.0}), -1.0, 0);
    partDisplayer_4.setColorMode(1);
    partDisplayer_4.setDisplayerColor(new DoubleVector(new double[] {1.0, 0.0, 0.0}));
  }
}

This typical macro code contains the one color selection that is made during recording—it remains the same. However, when the macro contains code for user input, it pauses to activate a dialog in which you can make your own selection.



Once you click OK, the macro continues.

To make this work, the code of partcolor.java has extensive new and modified code, as follows:

// STAR-CCM+ macro: partcolor.java
package macro;
import java.util.*;
import star.common.*;
import star.base.neo.*;
import star.vis.*;
import java.io.File;
import java.awt.*;
import javax.swing.*;
import star.base.neo.NeoMacro;
import star.base.neo.NeoScriptFile;
import star.base.neo.NeoDoubleField;
import star.base.neo.NeoIntegerField;
import org.openide.DialogDescriptor;
import org.openide.NotifyDescriptor;
import org.openide.DialogDisplayer;
public class partcolor extends StarMacro {
    public boolean promptUserForInput(String title, JComponent panel) {
        if (System.getProperty("java.awt.headless") == null) {
            if (title == "") {
                title = "Input values";
            }
            DialogDescriptor dd = new DialogDescriptor(panel, title, true, NotifyDescriptor.OK_CANCEL_OPTION, NotifyDescriptor.OK_OPTION, null);
            return DialogDisplayer.getDefault().
                notify(dd) == NotifyDescriptor.OK_OPTION;
        }
        return false;
    }
    public class ColorChoice extends JPanel {
        ButtonGroup group;
        String[] labels = { "Red", "Green", "Blue", "Black", "White", "Gray", "Yellow" };
        Color[] colors = { Color.RED, Color.GREEN, Color.BLUE, Color.BLACK, Color.WHITE, Color.GRAY, Color.YELLOW };
        public ColorChoice() {
            super(new GridLayout(0, 1));
            setBackground(Color.lightGray);
            setBorder(BorderFactory.
                createTitledBorder("Colors"));
            group = new ButtonGroup();
            JRadioButton option;
            for (int i = 0; i < labels.length; i++) {
                option = new JRadioButton(labels[i]);
                if(i==0) option.setSelected(true);
                option.setForeground(colors[i]);
                group.add(option);
                add(option);
            }
        }
        public Color getColor() {
            Enumeration elements = group.getElements();
            while (elements.hasMoreElements()) {
                AbstractButton button = (AbstractButton)elements.nextElement();
                if (button.isSelected()) {
                    String btext=button.getText();
                    for (int i = 0;i < labels.length; i++) {
                        if(btext==labels[i]) return colors[i];
                    }
                }
            }
            return Color.RED;
        }
    }
    public void execute() {
        Simulation simulation_3 = 
          getActiveSimulation();
simulation_3.getSceneManager().createGeometrySceneAndWait("Geometry Scene", "Outline", "Geometry", 1);
        Scene scene_3 = 
simulation_3.getSceneManager().getScene("Geometry Scene 1");
        scene_3.initializeAndWait();
        PartDisplayer partDisplayer_9 = 
          ((PartDisplayer) scene_3.getDisplayerManager().getDisplayer("Outline 1"));
        partDisplayer_9.initialize();
        PartDisplayer partDisplayer_10 = 
          ((PartDisplayer) scene_3.getDisplayerManager().getDisplayer("Geometry 1"));
        partDisplayer_10.initialize();
        PartDisplayer partDisplayer_11 = 
          ((PartDisplayer) scene_3.getHighlightDisplayer());
        partDisplayer_11.initialize();
        CurrentView currentView_3 = 
          scene_3.getCurrentView();
        currentView_3.setInput(new DoubleVector(new double[] {-0.5333999395370483, 1.268258864962263, 0.0}), new DoubleVector(new double[] {-0.5333999395370483, 1.268258864962263, 14.471860805953908}), new DoubleVector(new double[] {0.0, 1.0, 0.0}), -1.0, 0);
        partDisplayer_10.setColorMode(1);
        ColorChoice p = new ColorChoice();
        if (promptUserForInput("Set Color", p)) {
        Color mycolor = p.getColor();
        partDisplayer_10.setDisplayerColorColor(mycolor);
        }
    }
}