用java搞一下STARCCM带gui的脚本 |
您所在的位置:网站首页 › solidmodel › 用java搞一下STARCCM带gui的脚本 |
趁着年前这几天,挖个深坑,搞一下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") // 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(); }// 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 */ // /* 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); } // /* 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! //附上个人测试的源代码(类的名称可能与上面不同) ![]() ![]()
![]() ![]() 后面会更新eclipse 感谢https://github.com/ytsxg123的技术支持! |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |