Appendices

Appendix A: A Sample Java Bean that Imports an HM Model

////////////////////////////////////////////////////////////////////////

package com.altair.hwpmtraining;

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.beans.*;
import java.util.Vector;

import com.altair.hwm.interfaces.*;
import com.altair.hwm.beans.utils.*;
import com.altair.hwm.beans.utils.events.*;
import com.altair.hwm.comm.*;
import com.altair.hwm.comm.hm.*;

/**
 * Title:        HMImport
 * Description:  Launch an HM session and load a model in it.
 * Copyright     Copyright (c) 2001
 * Company:      Altair Engineering, Inc.
 */
public class HMImport extends HWMBasePanel implements ActionListener

//The path to the HM executable
protected String m_strHMExecPath = "";

//Session name for communicating with HWMCommHM
protected String m_strSessionName = HWMBeanUtils.DEF_HW_SESSION;

 
//* UI components used in this bean
protected HWMTextField m_txtFile = null;
protected HWMButton m_btnImport = null;
protected HWMFileBrowser m_hwmFileBrowser = null;
 
/**
 * Constructor
 */
public HMImport()

try

jbInit();
}
catch(Exception exception)

exception.printStackTrace();
}
}

/**
 * Builds GUI
 * @throws Exception
 */
private void jbInit() throws Exception

//Set the layout of this panel as GridbagLayout
setLayout(new GridBagLayout());

//Create and add listeners to UI components
HWMLabel lblFile = new HWMLabel("File");
m_txtFile = new HWMTextField("");
m_btnImport  = new HWMButton("Import");
m_hwmFileBrowser = new HWMFileBrowser();
m_btnImport.addActionListener(this);
m_hwmFileBrowser.addHWMCmdEventListener(new HWMCmdEventListener()

public void OnCmdSuccess(HWMCmdEvent hwmCmdEvent)
{        
m_txtFile.setText(m_hwmFileBrowser.GetSelectedFilename());
}
public void OnCmdFailed(HWMCmdEvent hwmCmdEvent) {}
});

//Add tooltips
m_txtFile.setToolTipText("File to be imported into HM");
m_hwmFileBrowser.setToolTipText("File Browser");
m_btnImport.setToolTipText("Import the file in Hypermesh");

//Add the components to the panel
this.add(lblFile,  
new GridBagConstraints(0, 0, 1, 1, 0.0, 1.0,
GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,
new Insets(9, 9, 9, 3), 0, 0));
this.add(m_txtFile,  
new GridBagConstraints(1, 0, 1, 1, 1.0, 1.0,
GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL,
new Insets(9, 3, 9, 3), 0, 0));
this.add(m_hwmFileBrowser,
new GridBagConstraints(2, 0, 1, 1, 0.0, 1.0,
GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,
new Insets(9, 3, 9, 3), 0, 0));
this.add(m_btnImport, 
new GridBagConstraints(3, 0, 1, 1, 0.0, 1.0,
GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,
new Insets(9, 3, 9, 9), 0, 0));
}

/**
 * This method is called by the HWPM engine to set the interface with
 * which the bean can communicate directly with the HWPM classes.
 * @param hwmFrameWork The interface with which a bean can
 * communicate with HWPM
 */
public void SetFrameWork(IHWMFrameWork hwmFrameWork)

super.SetFrameWork( hwmFrameWork );
}

/**
 * The play method needs to execute the functionaliy. This may be 
 * invoked in the replay mode.
 */
public void Play()

this. ImportModel();
}

/**
 * Callback for import model button and file type combo box
 */
public void actionPerformed(ActionEvent event)

Object objSource = event.getSource();

try

if(objSource == m_btnImport)

ImportModel();
}
catch (Exception exception)

exception.printStackTrace();
}
}

/**
 * Imports the selected model into Hypermesh
 * @throws Exception
 */
public void ImportModel() throws Exception

//First launch HyperMesh, if it has not already been
HWMCommMgr hwmCommMgr = GetFrameWork().GetCommMgr();
HWMCommHM commHM = hwmCommMgr.StartHM(m_strHMExecPath, null,
null, m_strSessionName);

//Send the command through commHM to load the model
String strHMFile = this.m_txtFile.getText();
strHMFile = strHMFile.replace('\\', '/');
String strCommand = "*readfile " + '"' + strHMFile + '"';
commHM.SendCommand(strCommand);

//Fire command success event
fireOnCmdSuccess(new HWMCmdEvent(this, 1));
}

/**
 * Save the required props in the datamodel (so that they persist)
 */
public void SaveProperties()

//Get the datamodel
IHWMDataModel hwmDataModel = m_hwmFrameWork.GetDataModel();
super.SaveProperties();

//Session Name
hwmDataModel.SetProperty(GetBeanName()+"::HW_SESSION_NAME",
this.m_strSessionName);

//HM exectable Path
hwmDataModel.SetProperty(GetBeanName()+"::HM_EXEC_PATH",
this.m_strHMExecPath);

//The HM Filename
hwmDataModel.SetProperty(GetBeanName()+"::HM_FILENAME",
this.m_txtFile.getText());
}

/**
 * Get the required properties from the datamodel and initialize self
 */
public void ReadProperties()

String strTemp;
//Get the datamodel
IHWMDataModel hwmDataModel = m_hwmFrameWork.GetDataModel();
super.ReadProperties();

//Session Name
strTemp = (String)
hwmDataModel.GetProperty(GetBeanName()+"::HW_SESSION_NAME");
if (strTemp != null)  m_strSessionName = strTemp;

//HM executable Path
strTemp = (String)
hwmDataModel.GetProperty(GetBeanName()+"::HM_EXEC_PATH");
if (strTemp != null) m_strHMExecPath = strTemp;

//HM Filename
strTemp = (String)
hwmDataModel.GetProperty(GetBeanName()+"::HM_FILENAME");
if (strTemp != null) this.m_txtFile.setText(strTemp);
}

/**
 * Returns the session name for HM communication
 * @return String The session name for HM Comm
 */
public String GetSessionName()

return m_strSessionName;
}

/**
 * Sets the session name for HM communication
 * @param strSessionName The session name for HM Comm
 */
public void SetSessionName(String strSessionName)

m_strSessionName = strSessionName;
}

/**
 * Gets path to the HM execuatable.
 * @return String The full path to the HM executable
 */
public String GetHMExecPath()

return this.m_strHMExecPath;
}

/**
 * Sets path to the HM execuatable that needs to be launched
 * @param strHMPath The full path to the HM executable
 */
public void SetHMExecPath(String strHMPath)
m_strHMExecPath = strHMPath;
}

/**
 * Enables the UI
 */
public void Enable()

this.setEnabled(true);
}
 
/**
 * Disables the UI
 */
public void Disable()

this.setEnabled(false);
}

/**
 * Enables/disable the UI
 * @param bEnable If true the UI is enabled else disabled
 */
public void setEnabled( boolean bEnable )

this.m_btnImport.setEnabled(bEnable);
super.setEnabled(bEnable);
}
}

 
////////////////////////////////////////////////////////////////////////

package com.altair.hwpmtraining;

import java.beans.*;
import java.lang.reflect.*;
import com.altair.hwm.beans.utils.HWMSimpleBeanInfo;

/**
 * Title:        HMImportBeanInfo<p>
 * Description:  Beaninfo for HMImport<p>
 * Company:      Altair Engineering, Inc.<p>
 */
public class HMImportBeanInfo extends HWMSimpleBeanInfo

/**
 * Constructor
 */
public HMImportBeanInfo()

super(HMImport.class);
}

/**
 * Returns the Bean Descriptor for HMImport bean
 * @returns HMImportBean BeanDescriptor
 */
public BeanDescriptor getBeanDescriptor()

BeanDescriptor beanDesc = new BeanDescriptor( m_classBeanSrc );
beanDesc.setDisplayName( "Import File");
beanDesc.setShortDescription("Imports file into HM");
return beanDesc;
}

/**
 * Returns an array of PropertyDescriptors representing the 
 * properties of HMImport Bean.
 *
 * @returns A PropertyDescriptor array, representing the properties 
 * of the HMImport Bean and the properties of the base class
 */
public PropertyDescriptor[] getPropertyDescriptors()

try

//Session name
PropertyDescriptor propDescSessionName= new PropertyDescriptor(
"HW_SESSION_NAME", m_classBeanSrc,
"GetSessionName", "SetSessionName");
propDescSessionName.setDisplayName("HM Session Name");
propDescSessionName.setShortDescription(
"HM Session to which the commands are to be sent");
propDescSessionName.setBound(true);

//HM Executable Path
PropertyDescriptor propDescHMPath= new PropertyDescriptor(
"HM_EXEC_PATH", m_classBeanSrc,
"GetHMExecPath", "SetHMExecPath");
propDescHMPath.setDisplayName("HM Executable Path");
propDescHMPath.setShortDescription(
"The path to the HM executable to be launched");
propDescHMPath.setBound(true);
 

//Create an array of property descriptors
PropertyDescriptor[] arrPropDescs =
new PropertyDescriptor[] { propDescSessionName,
propDescHMPath };
return super.AppendPropertyDescriptors(arrPropDescs);
}
catch( IntrospectionException exceptionIntrospection)

exceptionIntrospection.printStackTrace();
return null;
}
}

/**
 * Returns an array of MethodDescriptors in the HMImport bean
 *
 * @returns A MethodDescriptor array, containing the methods of the
 * HMImport bean and the methods of the base class
 */
public MethodDescriptor[] getMethodDescriptors()

try

//ImportModel
Method methodImportModel = m_classBeanSrc.getMethod(
"ImportModel", null);
MethodDescriptor methDescImportModel = new
MethodDescriptor( methodImportModel );

//Create the array of method descriptors
MethodDescriptor[] arrmethDescriptors = { methDescImportModel};
return super.AppendMethodDescriptors(arrmethDescriptors);
}
catch( NoSuchMethodException exceptionNoSuchMethod )

exceptionNoSuchMethod.printStackTrace();
return null;
}
}

/**
 * Show only the HWMCmdEvent
 * @return EventSetDescriptor[] The array of events that the bean 
 * exposes
 */
public EventSetDescriptor[] getEventSetDescriptors()

return super.GetEventSetDescriptors();
}
}

Appendix B: A Sample Tcl Module that Imports an HM Model

#######################################################################

# Title :        import_model.tcl                                     #

# Description :  Load a HyperMesh model in the session called         #

#                HW_DEF_SESSION                                       #

# Description :  Altair Engineering, Inc.                             #

#######################################################################

 

########################################################################
#  Create the UI components in the window
########################################################################
proc ::hw::pmgr::${::hw::pmgr::namespace}::DisplayWnd {} 
variable mainWnd;
variable id;
variable m_strFileName;   
set mynamespace ::hw::pmgr::${::hw::pmgr::namespace};

if {[WndExists] == 0} 
CreateWnd "HWPM -- Load Template" "Import"

# Create all the UI components
set lblFile [label $mainWnd.lblFile -text "HM Filename:" \
-font [hwt::AppFont] -anchor w];
set txtFileName [entry $mainWnd.txtFileName -font [hwt::AppFont] \
-textvariable ${mynamespace}::m_strFileName -width 60]
set btnFileBrowser [button $mainWnd.btnFB -text " Browse... " \
-command "${mynamespace}::OnBrowse" \
-font [hwt::AppFont]];
pack $lblFile $txtFileName $btnFileBrowser \
-side left -padx 5 -pady 5;

#Get the data from the DataModel and update the UI
GetDataFromDataModel;    
}
return 0;
}

########################################################################
# The heart of the module.  The actual action performed by the module 
# will be done here
########################################################################
proc ::hw::pmgr::${::hw::pmgr::namespace}::Exec {} 
## ----------------------------------------------------------------
## NOTE: 
## 1.  THIS PROCEDURE SHOULD RETURN 0 IF NO ERROR
##     ANY OTHER RETURN VALUE IMPLIES ERROR !!!
## 2.  DO NOT TRY TO GET VALUES FROM THE UI COMPONENTS -- THIS FUNC 
##     IS ALSO CALLED IN PLAY MODE (NO WINDOW & UI COMPONENTS WILL 
##     BE CREATED IN THAT MODE).  RELY ON DATA FROM THE DATAMODEL 
## ----------------------------------------------------------------

variable id;
variable m_strFileName;

if { $m_strFileName == "" } 
tk_messageBox -message "Please select a HyperMesh file to load"
return -1;
}

*readfile \"$m_strFileName\";
return 0;
}

########################################################################
# This method will be called by the OnExecute method.  All data will be 
# retrieved from the UI components and set in the datamodel
########################################################################
proc ::hw::pmgr::${::hw::pmgr::namespace}::SetDataInDataModel {} 
variable mainWnd;
variable id;
variable m_strFileName;

set nDataChanged 0;

# Set all the data from the UI into the data model.
# As we set, check if the data has changed

#Set the HM filename in the data model
if {$m_strFileName != \
[::hw::pmgr::PmgrGetData $id ${id}::HM_FILENAME]} 
::hw::pmgr::PmgrSetData $id ${id}::HM_FILENAME $m_strFileName;
set nDataChanged 1;
}

if {$nDataChanged == 0} 
RestoreOriginalState;
};
}

######################################################################### This method will be called in "Play" mode, before the Exec method is 
# called.  Retrieve any data from the datamodel required and populate 
# global/namespace variables (if required)
#
#  This method is optional, and you may delete this function, 
#  if not required
########################################################################proc ::hw::pmgr::${::hw::pmgr::namespace}::GetDataFromDataModel {} 
variable mainWnd;
variable id;
variable m_strFileName;

#Get the HM filename in the data model
set m_strFileName [::hw::pmgr::PmgrGetData $id ${id}::HM_FILENAME];
}

### --------------   END STANDARD TEMPLATE   ------------------------###

proc ::hw::pmgr::${::hw::pmgr::namespace}::OnBrowse { } 
variable mainWnd;
variable id;
variable m_strFileName;

set m_strFileName [tk_getOpenFile -title "Select HM File"];
}