Custom, Lightweight, External Application Integration with Parameter Definition

The extension Squares demonstrates the use of parameter definitions to integrate an external application.

Driven by parameters defined in the XML extension definition file, the external application squares the value of an input number, which is displayed in the Parameter Set bar.

 

The external application updates the output parameter to the computed square value. The use of parameters in this example enables you to leverage DesignXplorer functionality.

 

XML Extension Definition File

The XML extension definition file Squares.xml performs the following actions:

  • References the IronPython script Squares.py.

  • Defines a single task in the <tasks> block.

  • Defines the inputs and outputs in the <inputs> and <outputs> blocks. Note that an empty input is defined.

  • Defines two parameters in the <parameters> block.

  • Defines a single task group with a single task in the <taskgroups> block.

The file Squares.xml contains the following code:

<extension version="1" name="Squares">
    <guid shortid="Squares">69d0095b-e138-4841-a13a-de12238c83f4</guid>
    <script src="squares.py" />
    <interface context="Project">
        <images>images</images>
    </interface>
      <workflow name="wf1" context="Project" version="1">
        <tasks>
            <task name="Squares" caption="Squares" icon="squares_component" version="1">
                <callbacks>
                    <onupdate>update</onupdate>
                </callbacks>
                <inputs>
                    <input/>
                </inputs>
                <outputs/>
                <parameters>
                    <parameter name="Input" caption="Input" usage="Input" control="Double" version="1"/>
                    <parameter name="Output" caption="Output" usage="Output" control="Double" version="1"/>
                </parameters>
            </task>
        </tasks>
        <taskgroups>
            <taskgroup name="Squares" caption="Squares" icon="squares" category="ACT Custom Workflows" 
abbreviation="SQRS" version="1">
                <includeTask name="Squares" caption="Squares"/>
            </taskgroup>
        </taskgroups>
      </workflow>
</extension>

IronPython Script

The IronPython script Squares.py performs the following actions:

  • Obtains the parameters.

  • Prepares the inputs.

  • Writes the input file.

  • Runs the external solver.

  • Reads the output file.

  • Sets the parameters to the calculated solver values.

Because the XML extension definition file has the callback <onupdate>, the method update is defined in the script. All ACT workflow callbacks get a container (for the task) and a context.

The script Squares.py contains the following code:

import System

import clr
clr.AddReference("Ans.Utilities")
import Ansys.Utilities

#convenience method to look up parameters based on a predetermined ID
def GetParameterByName(parameters, id):
    match = None
    for param in parameters:
        if param.ParameterName == id:
            match = param
            break
    return match

def update(task):
    container = task.InternalObject
    context = ExtAPI.DataModel.Context
    activeDir = container.GetActiveDirectory()
    extensionDir = ExtAPI.ExtensionManager.CurrentExtension.InstallDir
    exeName = "ExampleAddinExternalSolver.exe"
    solverPath = System.IO.Path.Combine(extensionDir, exeName)
    
    #get parameters owned by container
    
    params = None
    lock = context.ContainerReadLock(container)
    params = context.Project.GetDataReferencesByType(container, "ParameterAdapter")
    lock.Dispose()
    
    #isolate specific parameters
    
    inputParam = GetParameterByName(params, "Input")
    outputParam = GetParameterByName(params, "Output")
    
    #prep i/o file paths
    
    inputFileName = "input.txt"
    outputFileName = "output.txt"
    dpInputFile = System.IO.Path.Combine(activeDir, inputFileName)
    dpOutputFile = System.IO.Path.Combine(activeDir, outputFileName)
    
    #write input file
    
    if inputParam != None and outputParam != None:
        val = inputParam.Value    
        #write input file
        f = open(dpInputFile, "w")
        f.write('input='+val.ToString(System.Globalization.NumberFormatInfo.InvariantInfo))
        f.close()
    
    #run exe
    
    runInMono = Ansys.Utilities.ApplicationConfiguration.DefaultConfiguration.IsRuntimeMono
    monoPath = "mono"
    monoArgs = System.String.Format("{0} \"{1}\" \"{2}\"", solverPath, dpInputFile, dpOutputFile)
    info = None
    if runInMono:
        info = System.Diagnostics.ProcessStartInfo(monoPath, monoArgs)
    else:
        info = System.Diagnostics.ProcessStartInfo(solverPath, System.String.Format("\"{0}\" \"{1}\"", 
dpInputFile, dpOutputFile))
    info.CreateNoWindow = True
    info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Minimized
    p = System.Diagnostics.Process.Start(info)
    p.WaitForExit()
    
    #read output file
    
    outValue = None
    f = open(dpOutputFile, "r")
    currLine = f.readline()
    while currLine != "":
        valuePair = currLine.split('=')
        outValue = System.Int32.Parse(valuePair[1], System.Globalization.NumberFormatInfo.InvariantInfo)
        currLine = f.readline()
    f.close()
    
    #set output value
    
    if outValue == None:
        raise Exception("Error in update - no output value detected!")
    else:
        outputParam.Value = outValue

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