趁着年前这几天,挖个深坑,搞一下java的gui脚本(陆续更新,欢迎加入)
主要用于汽车常用CFD分析的简化操作,脚本性质,陆续增加其他功能插件。
先上一个NETBEANS的简化教程(starccm+10.04为例)
1、首先搭建环境
a、netbeans软件安装(java1.7版本)
b、jdk 1.7安装
c、starccm+库导入
2、构思功能
实现一个连续体的脚本,空气动力粘度可填写,如图

3、建立文件结构
新建如下结构文件

4、编写test.java,蓝红绿可根据实际情况更改
package test;
import javax.swing.JFrame;
import star.common.Simulation;
import star.common.StarMacro;
public class test extends StarMacro{
public void execute() {
Simulation sim = this.getActiveSimulation();
gui mw = new gui(sim);
mw.setVisible(true);
mw.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
5、编辑jframe

设置变量名为jTextField_viscosity,红色为增加代码,包和变量名称可能会有问题,可根据实际情况更改
package test;
import star.common.Simulation;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Rectangle;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import star.turbulence.*;
import star.material.*;
import star.common.*;
import star.keturb.*;
import star.flow.*;
import star.segregatedflow.*;
import star.metrics.*;
import star.common.Simulation;
/**
*
* @author wangfc
*/
public class gui extends javax.swing.JFrame {
/**
* Creates new form gui
*/
public gui() {
initComponents();
}
private Simulation simulation_0 = null;
public gui(Simulation Simlocal){
initComponents();
this.simulation_0 = Simlocal;
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton_model = new javax.swing.JButton();
jTextField_viscosity = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton_model.setText("粘度");
jButton_model.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton_modelActionPerformed(evt);
}
});
jTextField_viscosity.setText("XXXXXXXXXX");
jTextField_viscosity.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField_viscosityActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(95, 95, 95)
.addComponent(jTextField_viscosity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 134, Short.MAX_VALUE)
.addComponent(jButton_model)
.addGap(48, 48, 48))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(96, 96, 96)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton_model)
.addComponent(jTextField_viscosity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(181, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton_modelActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println("actionPerformed()");
String txt = jTextField_viscosity.getText();
double air_viscosity;
try {
air_viscosity = Double.valueOf( txt );
} catch (Exception ex) {
air_viscosity = 0;
}
changeVelocity(simulation_0, air_viscosity);
// TODO add your handling code here:
}
private void jTextField_viscosityActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
//修改进口速度值方法
private void changeVelocity(Simulation simulation_0, double air_viscosity) {
//此处后为脚本
PhysicsContinuum physicsContinuum_0 =
simulation_0.getContinuumManager().createContinuum(PhysicsContinuum.class);
physicsContinuum_0.enable(ThreeDimensionalModel.class);
physicsContinuum_0.enable(SteadyModel.class);
physicsContinuum_0.enable(SolidModel.class);
SolidModel solidModel_0 =
physicsContinuum_0.getModelManager().getModel(SolidModel.class);
physicsContinuum_0.disableModel(solidModel_0);
physicsContinuum_0.enable(SingleComponentGasModel.class);
physicsContinuum_0.enable(SegregatedFlowModel.class);
physicsContinuum_0.enable(IdealGasModel.class);
physicsContinuum_0.enable(TurbulentModel.class);
physicsContinuum_0.enable(RansTurbulenceModel.class);
physicsContinuum_0.enable(KEpsilonTurbulence.class);
physicsContinuum_0.enable(RkeTwoLayerTurbModel.class);
physicsContinuum_0.enable(KeTwoLayerAllYplusWallTreatment.class);
physicsContinuum_0.enable(CellQualityRemediationModel.class);
IdealGasModel idealGasModel_0 =
physicsContinuum_0.getModelManager().getModel(IdealGasModel.class);
physicsContinuum_0.disableModel(idealGasModel_0);
physicsContinuum_0.enable(ConstantDensityModel.class);
SingleComponentGasModel singleComponentGasModel_0 =
physicsContinuum_0.getModelManager().getModel(SingleComponentGasModel.class);
Gas gas_0 =
((Gas) singleComponentGasModel_0.getMaterial());
ConstantMaterialPropertyMethod constantMaterialPropertyMethod_0 =
((ConstantMaterialPropertyMethod) gas_0.getMaterialProperties().getMaterialProperty(DynamicViscosityProperty.class).getMethod());
constantMaterialPropertyMethod_0.getQuantity().setValue(air_viscosity);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new gui().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton_model;
private javax.swing.JTextField jTextField_viscosity;
// End of variables declaration
}
6、在starccm+中用户宏路径添加*\dist,播放宏测试test.java
7、enjoy it!
//附上个人测试的源代码(类的名称可能与上面不同)
1 package test; 2 import javax.swing.JFrame; 3 import star.common.Simulation; 4 import star.common.StarMacro; 5 6 public class Testgui1 extends StarMacro{ 7 @Override 8 public void execute() { 9 Simulation sim = this.getActiveSimulation(); 10 gui mw = new gui(sim); 11 mw.setVisible(true); 12 mw.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 13 } 14 }
1 package test; 2 import star.common.Simulation; 3 import javax.swing.JPanel; 4 import javax.swing.JFrame; 5 import javax.swing.JLabel; 6 import java.awt.Rectangle; 7 import java.awt.Font; 8 import javax.swing.JTextField; 9 import javax.swing.JButton; 10 import star.turbulence.*; 11 import star.material.*; 12 import star.common.*; 13 import star.keturb.*; 14 import star.flow.*; 15 import star.segregatedflow.*; 16 import star.metrics.*; 17 import star.common.Simulation; 18 /** 19 * 20 * @author wangfc 21 */ 22 public class gui extends javax.swing.JFrame { 23 24 /** 25 * Creates new form gui 26 */ 27 public gui() { 28 initComponents(); 29 } 30 private Simulation simulation_0 = null; 31 public gui(Simulation Simlocal){ 32 initComponents(); 33 this.simulation_0 = Simlocal; 34 } 35 /** 36 * This method is called from within the constructor to initialize the form. 37 * WARNING: Do NOT modify this code. The content of this method is always 38 * regenerated by the Form Editor. 39 */ 40 @SuppressWarnings("unchecked") 41 // <editor-fold defaultstate="collapsed" desc="Generated Code"> 42 private void initComponents() { 43 44 jButton_model = new javax.swing.JButton(); 45 jTextField_viscosity = new javax.swing.JTextField(); 46 47 setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 48 49 jButton_model.setText("粘度"); 50 jButton_model.addActionListener(new java.awt.event.ActionListener() { 51 public void actionPerformed(java.awt.event.ActionEvent evt) { 52 jButton_modelActionPerformed(evt); 53 } 54 }); 55 56 jTextField_viscosity.setText("XXXXXXXXXX"); 57 jTextField_viscosity.addActionListener(new java.awt.event.ActionListener() { 58 public void actionPerformed(java.awt.event.ActionEvent evt) { 59 jTextField_viscosityActionPerformed(evt); 60 } 61 }); 62 63 javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 64 getContentPane().setLayout(layout); 65 layout.setHorizontalGroup( 66 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 67 .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 68 .addGap(95, 95, 95) 69 .addComponent(jTextField_viscosity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 70 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 134, Short.MAX_VALUE) 71 .addComponent(jButton_model) 72 .addGap(48, 48, 48)) 73 ); 74 layout.setVerticalGroup( 75 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 76 .addGroup(layout.createSequentialGroup() 77 .addGap(96, 96, 96) 78 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 79 .addComponent(jButton_model) 80 .addComponent(jTextField_viscosity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) 81 .addContainerGap(181, Short.MAX_VALUE)) 82 ); 83 84 pack(); 85 }// </editor-fold> 86 87 private void jButton_modelActionPerformed(java.awt.event.ActionEvent evt) { 88 System.out.println("actionPerformed()"); 89 String txt = jTextField_viscosity.getText(); 90 double air_viscosity; 91 try { 92 air_viscosity = Double.valueOf( txt ); 93 } catch (Exception ex) { 94 air_viscosity = 0; 95 } 96 changeVelocity(simulation_0, air_viscosity); 97 // TODO add your handling code here: 98 } 99 100 private void jTextField_viscosityActionPerformed(java.awt.event.ActionEvent evt) { 101 102 // TODO add your handling code here: 103 } 104 //修改进口速度值方法 105 private void changeVelocity(Simulation simulation_0, double air_viscosity) { 106 //此处后为脚本 107 108 PhysicsContinuum physicsContinuum_0 = 109 simulation_0.getContinuumManager().createContinuum(PhysicsContinuum.class); 110 111 physicsContinuum_0.enable(ThreeDimensionalModel.class); 112 113 physicsContinuum_0.enable(SteadyModel.class); 114 115 physicsContinuum_0.enable(SolidModel.class); 116 117 SolidModel solidModel_0 = 118 physicsContinuum_0.getModelManager().getModel(SolidModel.class); 119 120 physicsContinuum_0.disableModel(solidModel_0); 121 122 physicsContinuum_0.enable(SingleComponentGasModel.class); 123 124 physicsContinuum_0.enable(SegregatedFlowModel.class); 125 126 physicsContinuum_0.enable(IdealGasModel.class); 127 128 physicsContinuum_0.enable(TurbulentModel.class); 129 130 physicsContinuum_0.enable(RansTurbulenceModel.class); 131 132 physicsContinuum_0.enable(KEpsilonTurbulence.class); 133 134 physicsContinuum_0.enable(RkeTwoLayerTurbModel.class); 135 136 physicsContinuum_0.enable(KeTwoLayerAllYplusWallTreatment.class); 137 138 physicsContinuum_0.enable(CellQualityRemediationModel.class); 139 140 IdealGasModel idealGasModel_0 = 141 physicsContinuum_0.getModelManager().getModel(IdealGasModel.class); 142 143 physicsContinuum_0.disableModel(idealGasModel_0); 144 145 physicsContinuum_0.enable(ConstantDensityModel.class); 146 147 SingleComponentGasModel singleComponentGasModel_0 = 148 physicsContinuum_0.getModelManager().getModel(SingleComponentGasModel.class); 149 150 Gas gas_0 = 151 ((Gas) singleComponentGasModel_0.getMaterial()); 152 153 ConstantMaterialPropertyMethod constantMaterialPropertyMethod_0 = 154 ((ConstantMaterialPropertyMethod) gas_0.getMaterialProperties().getMaterialProperty(DynamicViscosityProperty.class).getMethod()); 155 156 constantMaterialPropertyMethod_0.getQuantity().setValue(air_viscosity); 157 } 158 /** 159 * @param args the command line arguments 160 */ 161 public static void main(String args[]) { 162 /* Set the Nimbus look and feel */ 163 //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 164 /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 165 * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 166 */ 167 try { 168 for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 169 if ("Nimbus".equals(info.getName())) { 170 javax.swing.UIManager.setLookAndFeel(info.getClassName()); 171 break; 172 } 173 } 174 } catch (ClassNotFoundException ex) { 175 java.util.logging.Logger.getLogger(gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 176 } catch (InstantiationException ex) { 177 java.util.logging.Logger.getLogger(gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 178 } catch (IllegalAccessException ex) { 179 java.util.logging.Logger.getLogger(gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 180 } catch (javax.swing.UnsupportedLookAndFeelException ex) { 181 java.util.logging.Logger.getLogger(gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 182 } 183 //</editor-fold> 184 185 /* Create and display the form */ 186 java.awt.EventQueue.invokeLater(new Runnable() { 187 public void run() { 188 new gui().setVisible(true); 189 } 190 }); 191 } 192 193 // Variables declaration - do not modify 194 private javax.swing.JButton jButton_model; 195 private javax.swing.JTextField jTextField_viscosity; 196 // End of variables declaration 197 }
后面会更新eclipse
感谢https://github.com/ytsxg123的技术支持!
浙公网安备 33010602011771号