You can add custom toolbars to Ansys products that expose their own toolbars. A toolbar is 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 extension's XML file, which defines the toolbar and its buttons.
In the introductory example,
the extension ExtSample1 shows how to create a custom Mechanical
toolbar with a single button that displays this message when clicked: High
five! ExtSample1 is a success!.
Here, the extension ExtToolbarSample shows how to create two custom Mechanical toolbars, each with multiple button that call IronPython functions. This extension's XML file follows.
<extension version="1" 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>
When the extension ExtToolbarSample is loaded in Mechanical, to the right of the ribbon's Automation tab, two additional tabs display: ToolBar1 and Toolbar 2. When the Toolbar1 tab is selected, you see three red buttons:
When the Toolbar2 tab is selected, you see three blue buttons:
To define each of these toolbars, the XML file uses the element
<toolbar>. Each of these elements has two attributes,
name and caption. The attribute
name is required and is used for internal references. The
attribute caption is the text to display in Mechanical.
The element <toolbar> has a child element
<entry> that defines the buttons in the toolbar. The
element <entry> has two attributes,
name and icon. The attribute
name is required and is used for internal references. It also
displays as the tooltip for the button. The attribute icon is the
name of the image file to display as the button.
The element <entry> has a child element
<callbacks> 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 third line in the XML file:
<script src="toolbarsample.py" />
This line defines toolbarsample.py as the IronPython script for the extension. This script follows.
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)