Dialog Box Creation

You can add custom dialog boxes to Ansys products. The extension ExtDialogSample defines a custom menu in Mechanical named DialogSample that has one menu item named GetFilename.This menu option opens a file selection dialog box and displays a custom message dialog box. The XML file follows.

extension version="1" name="ExtDialogSample">  
  <guid shortid="ExtDialogSample">25c00857-4e27-4b01-bd22-c52699ac39e9</guid>
  <script src="dialogsample.py" />
  <interface context="Mechanical">
    <images>images</images>
    <callbacks>
      <oninit>init</oninit>
    </callbacks>
    <toolbar name="DialogSample" caption="DialogSample">
      <entry name="DialogSample1" caption="GetFilename" icon="blank">
        <callbacks>
          <onclick>GUIMenuOpenFile</onclick>
        </callbacks>
      </entry>
    </toolbar>  
  </interface>
</extension>

The callback function specified for the GetFilename menu button is GUIMenuOpenFile. The IronPython script dialogsample.py defines this function.

clr.AddReference("Ans.UI.Toolkit")
clr.AddReference("Ans.UI.Toolkit.Base")
from Ansys.UI.Toolkit import *

def init(context):
    ExtAPI.Log.WriteMessage("Init ExtDialogSample ...") 

def GUIMenuOpenFile(analysis):
    filters = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
    dir = "c:\\"
    res = FileDialog.ShowOpenDialog(ExtAPI.UserInterface.MainWindow,dir,filters,2,"ExtDialogSample","")

    if res[0]==DialogResult.OK:  
        message = str("OPEN selected -> Filename is "+res[1])
    else:
        message = "CANCEL selected -> No file"
    MessageBox.Show(message)

When the function GUIMenuOpenFile is invoked, an instance of a modal file selection dialog box is created by calling FileDialog.ShowOpenDialog. The class FileDialog is provided by the UI Toolkit. When running the extension, the necessary information is supplied in the dialog box. When the Open or Cancel button is clicked, the file selection dialog box closes. The supplied information is returned to the function GUIMenuOpenFile, which uses it to create the message dialog box. The message shown in this dialog box validates the result that is returned from the file selection dialog box.