Creating the IronPython Script

The IronPython script defines functions that respond to user and interface interactions and implements the behavior of the extensions. Typically, functions are invoked through the different events or callbacks in the XML file.

The script sample1.py for the extension ExtSample1 follows:

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

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

def HighFiveOut(analysis_obj):
    MessageBox.Show("High five! ExtSample1 is a success!")

This script contains two functions:

  • init(): Called when the Ansys product is opened. In the XML file, the argument context for the element <interface> contains the name of the product ("Mechanical").

  • HighFiveOut(): Called when the toolbar button HighFive is clicked. The callback <onclick> passes an instance of the active object Analysis as an argument.

For any function, the global variable ExtAPI represents the main entry point to the ACT API. In this example, the function init() uses the ACT interface ILog to write a message in the log file. For comprehensive information on available interfaces, see the Ansys ACT API Reference Guide.