Defining Toolbars and Toolbar Buttons

This section focuses on the customization of toolbars and toolbar buttons in ANSYS products that expose their own toolbars. In addition to the target products that support ACT direct customization (Workbench, AIM, Mechanical, DesignModeler, and DesignXplorer), toolbar customization is also available for all products that support ACT wizard extensions (Fluent and Electronics Desktop).

ANSYS Mechanical is used for demonstration purposes. It requires that you use BMP files for the images to display for toolbar buttons. ANSYS Workbench and all other supported ANSYS products require you to use PNG files.

A toolbar serves as a parent container for one or more toolbar buttons. Conceptually, the toolbar should address a specific feature, with each toolbar button being associated with a function that supports the feature. This relationship is reflected in the structure of the XML extension definition file that defines the toolbar and its buttons.

The XML file for the extension ExtToolbarSample follows.

<extension version="1" minorversion="0" name="ExtToolbarSample">  
  <guid shortid="ExtToolbarSample">ccdbbed4-9fdd-4157-acea-abccddbd62fc</guid>
  <script src="toolbarsample.py" />
  <interface context="Mechanical">
    <images>images</images>
    <callbacks>
      <oninit>init</oninit>
    </callbacks>
    <toolbar name="ToolBar1" caption="ToolBar1">
      <entry name="TB1Button1" icon="button1Red">
        <callbacks>
          <onclick>OnClickTB1Button1</onclick>
        </callbacks>
      </entry>
      <entry name="TB1Button2" icon="button2Red">
        <callbacks>
          <onclick>OnClickTB1Button2</onclick>
        </callbacks>
      </entry>
      <entry name="TB1Button3" icon="button3Red">
        <callbacks>
          <onclick>OnClickTB1Button3</onclick>
        </callbacks>
      </entry>
    </toolbar>  
    <toolbar name="Toolbar2" caption="Toolbar2">
      <entry name="TB2Button1" icon="button1Blue">
        <callbacks>
          <onclick>OnClickTB2Button1</onclick>
        </callbacks>
      </entry>
      <entry name="TB2Button2" icon="button2Blue">
        <callbacks>
          <onclick>OnClickTB2Button2</onclick>
        </callbacks>
      </entry>
      <entry name="TB2Button3" icon="button3Blue">
        <callbacks>
          <onclick>OnClickTB2Button3</onclick>
        </callbacks>
      </entry>
    </toolbar>
  </interface>
</extension>

The extension ExtToolbarSample adds two toolbars, Toolbar1 and Toolbar2. Each toolbar has three buttons.

 

This extension's XML file defines two <toolbar> blocks. Each <toolbar> block has two attributes, name and caption. The attribute name is required and is used for internal references. The attribute caption is the text displayed in Mechanical.

The <toolbar> block has a child <entry> block that defines the buttons in the toolbar. The <entry> block has two attributes, name and icon. The attribute name is required and is used for internal references. The name is also displayed as the tooltip. The attribute icon is the name of the image file to display as the button.

The <entry> block has a child <callbacks> block that defines callbacks to events. For instance, the callback onclick specifies the name of the IronPython function to invoke when the button is clicked.

Consider the second line in the previous XML code:

<script src="toolbarsample.py" /> 

This line defines toolbarsample.py as the IronPython script for the extension ExtToolbarSample. The code in this script follows. By default, ACT looks for the script in the directory of the extension. If the script is located in a different directory, an explicit path to the file must be inserted into the extension's XML file.

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

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

def OnClickTB1Button1(analysis):
    LogButtonClicked(1, 1, analysis)

def OnClickTB1Button2(analysis):
    LogButtonClicked(1, 2, analysis)

def OnClickTB1Button3(analysis):
    LogButtonClicked(1, 3, analysis) 

def OnClickTB2Button1(analysis):
    LogButtonClicked(2, 1, analysis)

def OnClickTB2Button2(analysis):
    LogButtonClicked(2, 2, analysis) 

def OnClickTB2Button3(analysis):
    LogButtonClicked(2, 3, analysis)     

def LogButtonClicked(toolbarId, buttonId, analysis):
    now = datetime.datetime.now()
    outFile = SetUserOutput("ExtToolbarSample.log", analysis)
    f = open(outFile,'a')    
    f.write("*.*.*.*.*.*.*.*\n")
    f.write(str(now)+"\n")
    f.write("Toolbar "+toolbarId.ToString()+" - Button "+buttonId.ToString()+" Clicked. \n")
    f.write("*.*.*.*.*.*.*.*\n")
    f.close()
    MessageBox.Show("Toolbar "+toolbarId.ToString()+" - Button "+buttonId.ToString()+" Clicked.") 
    
def SetUserOutput(filename, analysis):
    solverDir = analysis.WorkingDir
    return os.path.join(solverDir,filename)

Each button defined in the extension ExtToolbarSample has a unique callback function. Each callback function passes the toolbar ID and the ID of the button pressed to the function LogButtonClicked, which stores them in the variables toolbarId and buttonId. These variables are referenced within the function where their string values are written. The functions LogButtonClicked and SetUserOutput demonstrate how to reduce redundant code in callbacks using utility functions. The object Analysis is passed to each callback <entry> and then used in the funciton SetUserOutput to query for the working directory of the analysis. The script in toolbarsample.py makes use of the namespace datetime from the .NET framework. The namespace datetime exposes a class that is also called datetime. The function LogButtonClicked invokes datetime to query the current date and time and stores the result in the variable named now. The utility str() is used to extract the string definition of the variable now to write out the date and time.


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