Defining Pop-up Dialog Boxes

This section discusses message dialog boxes using the extension ExtDialogSample as an example.This extension defines a menu labeled DialogSample with one menu item labeled GetFilename. It demonstrates how to open a file selection dialog box and display a message dialog box in the user interface.

The following XML extension definition file for the extension ExtDialogSample is similar to the one for the extension ExtToolbarSample.

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 in the XML file for the GetFilename menu button is GUIMenuOpenFile. The IronPython script dialogsample.py follows.

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 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. The user inputs the necessary information in the dialog box. When the user clicks the Open or Cancel button, the file selection dialog box closes and this information is returned to GUIMenuOpenFile. The returned information is then used to create a message dialog box. The message shown in the message dialog box validates the result returned from the file selection dialog box.


Release 18.2 - © ANSYS, Inc. All rights reserved.