提供互动对话框
可以编辑用于提供互动对话框的宏。
Simcenter STAR-CCM+ 提供很多可视化控制,可用于自定义模拟中显示窗口的外观。 在录制宏时将捕捉这些控制的设置。 例如,创建了简单的宏以将几何场景添加到模拟,并将几何显示器的颜色模式属性更改为 Constant(恒定)。
宏编辑过程的下一步涉及将恒定的颜色更改为红色。
名为 partcolor.java 的宏包含以下代码:
// 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}));
}
}
此典型宏代码包含在录制过程中进行的一种颜色选择 — 它保持不变。 但是,当宏包含用户输入的代码时,它将暂停以激活用户可在其中做出选择的对话框。
单击确定时,宏将继续。
为了使其发挥作用,partcolor.jav 的代码具有全新且详细的已修改代码,如下所示:
// 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);
}
}
}