You are on page 1of 6

ANSYS JS

Background

• ANSYS frame work:

o
o The outer shell (aka the impenetrable black box) is the GUI (how the user typically
interacts with the program)
o However, the GUI itself is not a statically compiled piece of executable code, it is more
like an interpreter that builds itself every time you launch ANSYS Mechanical
o The structure of the ANSYS Mechanical GUI is described in a handful of XML files
▪ the text associated with all of the menus and buttons in the GUI; whether a GUI
entity is a toolbar, or a toolbar button or a menu item; all of that is described
textually inside these XML files
o The interactive part of the ANSYS Mechanical UI is handled primarily through the use of
javascript (an interpreted scripting language)
▪ The way it works is that there is a javascript interpeter built into the ANSYS
Mechanical executable
• Almost all of the core functionality of ANSYS Mechanical is implemented
as a set of C++ libraries
o C++ libraries are big pieces of code like meshing, or virtual
topology, or graphics or geometry selection that expose a set of
routines that hook into the javascript interpreter
• All of these functional pieces are then glued together with javascript
o Thus, it turns out that the UI part of the ANSYS Mechanical GUI is just a whole bunch of
javascript code juggling all of these big pieces of functionality that are implemented in
C++

Great blog posts:

http://www.padtinc.com/blog/the-focus/ansys-mechanical-scripting-howto-part-1

http://www.padtinc.com/blog/the-focus/ansys-mechanical-scripting-howto-part-2

http://www.padtinc.com/blog/the-focus/ansys-mechanical-scripting-howto-part-3

http://www.padtinc.com/blog/the-focus/ansys-mechanical-scripting-howto-part-4

http://www.padtinc.com/blog/the-focus/ansys-mechanical-scripting-howto-part-5
To look for tree / menu item function and call it

• Look for menu item’s corresponding string for it’s ID in


o C:\Program Files\ANSYS Inc\v172\aisol\DesignSpace\DSPages\Language\en-
us\xml\dsstringtable.xml
o Get the string id
▪ E.g. look up “Export Text File”
• String id = “ID_ExportTextFile”
• Look up ID for its function calls in
o C:\Program Files\ANSYS Inc\v172\aisol\DesignSpace\DSPages\xml\dscontextmenu.xml
o Get the Callback Methodnames
▪ This is calling a function defined in a different file – there may be multiple,
choose one that sounds most descriptive
▪ E.g. look up “ID_ExportTextFile”
• methodName – “doExportToTextFile”
• Look up method name for function in
o C:\Program Files\ANSYS Inc\v172\aisol\DesignSpace\DSPages\scripts\DSMenuScript.js
• You can now call this function by:
o DS.Script.methodName(inputs)
▪ E.g. DS.Script.doExportToTextFile()

Some common functions

• Print out in a dialog box


o WBScript.Out("message here", true);
▪ “Message here” can be a concatenated string
• E.g. WBScript.Out(“File name preface “ + var I = 1, true)

Grabbing things from ANSYS GUI

http://www.padtinc.com/blog/the-focus/ansys-mechanical-scripting-howto-part-4

Some Preliminaries
There are a number of steps you need to take to actually get the debugger environment
set up properly. Here are a list of steps you need to take:

1. You obviously need to install Visual Studio. I use Visual Studio Professional 2010, but I
think any version including 2005 and above will probably be sufficient. I cannot speak
to whether or not the Express Editions will work for this task, so if someone has
experience with one of these editions and can shed some light, please post a comment.
2. In the Internet Explorer “Internet Options” dialog (arrived at through Tools->Internet
Options…) you need to make sure that “Disable Script Debugging (Other)” is
unchecked. See image below.
3. Finally, you need to adjust the registry setting
HKEY_CURRENT_USER\Software\Microsoft\Windows Script\Settings\JITDebug = 1

Debugging Code
The easiest way to stop Mechanical in the debugger is to insert the javascript “debugger” command
wherever you would like to stop execution and look around. So, for example, lets assume we have the
following script:

function my_func() {
debugger;
var my_array = new Array();
for(var i = 0; i < 10; i++) {
my_array[i] = 2*i;
}
}
my_func();

As you can see above, this simple script consists of one function called my_func that just populates an
array. The first statement in this function is the keyword debugger. If all works according to plan, when
we execute this script, we’ll end up in the Microsoft Debugger. To make this all happen, you need to
follow a ten step program:
1. Start MS Visual Studio
2. Start ANSYS Mechanical
3. Copy the above script to a file named debug_test.js anywhere on your harddrive.
4. Inside MS Visual Studio, navigate to Debug->Attach to Process…

5. In the dialog that pops up, look for the AnsysWBU.exe executable in the

list.
6. Select AnsysWBU.exe and press the Attach button.
7. Switch to the Mechanical Application
8. From the tools Menu, choose Tools->Run Macro…
9. Navigate to the location of debug_test.js and click open.
10. Start debugging…

http://xansys.org/forum/viewtopic.php?p=91258
The following is an attempt to explain how you can learn all about these things down in the list view of
Mechanical if you are interested. I'm totally aware that it ain't straightforward, but it works if you really need
it. Also, Public Service Announcement for the XANSYS community at large, you should take vitamins and use
ACT to customize WB if you want to do that kind of thing. However, if you want to continue down this path,
this is what you will need. You need a debugger like visual studio and you need to know how to "attach" the
debugger to ANSYS mechanical. (http://www.padtinc.com/blog/the-focus/ansys-mechanical-scripting-
howto-part-4) You also need to create two files, one an XML file and one an HTML file. They need to be in
the same directory. The XML file can be named whatever, but the HTML file needs to be named test.htm.

The steps you take to use this are the following:


1. Attach the VS debugger to Mechanical
2. Create these two files using the contents below in the same directory.
3. Click on the Simulation wizard button (Green Check Mark) and choose to navigate to a wizard definition
4. Navigate to the XML file.
5. This will create a wizard panel in Mechanical with a Debug button.
6. Select the field in the List View for whatever object you are interested in in the tree.
7. Open the test.htm file in Visual Studio (make you have the debugger attached to Mechanical at this point)
8. Insert a break point at the end of the function my_test()
9. Press the "Debug" button in the Mechanical Wizard.
10. You should be thrown into the debugger at this point and you should have a local variable named
selected_item
11. If you type that variable into the watch window and expand it out, you will see a field called "PropName"
12. The value of this field is the name of the property associated with the particular object you are currently
operating on.

So, this will let you select any object in the tree and then select any item in the details pane and figure out
what the property name is. To determine what the "values" are make some changes to the values in the GUI
and then look at the selected_item "Value" and "ValueWithoutUnits" in the debugger. This will tell you what
value you need to set the property to to replicate what you see in the GUI. Clear as mud, right? This was my
primary technique for figuring things out back in the day.

The contents of the XML file are:

+++++++++++++++++++++++++++++++++++++++

< ?xml version="1.0" encoding="utf-8"?>


< simulation-wizard version="1.0">
< object-groups>
< object-group name="component">
< object-type class="id_Component" />
< /object-group>
< /object-groups>
< strings>
< language xml:lang="en-us">
< string id="title_string">
Test code
< /string>
< string id="description_string">
The following is used to get at the contents of the list view.
< /string>
< /language>
< /strings>
< body>
< group id="Title" caption="title_string" description="description_string">
< /group>
< group id="GeometryUpdate" caption="Geometry Update">
< iframe src=".\test.htm"/>
< /group>
< /body>
< /simulation-wizard>

++++++++++++++++++++++++++++++++++++++
The contents of the HTML file are:
++++++++++++++++++++++++++++++++++++++

< html>
< head>
< /head>
< script type="text/javascript" id="XMLUtilitiesScript"></script>
< script type="text/javascript" id="IFrameScript"></script>
< script type="text/javascript">
var DS;
var SC;
var LV;
var folder = window.external.ScriptEngine.Engine.installDir + "SimulationWizard";
XMLUtilitiesScript.src = folder + "/System/XMLUtilities.js";
IFrameScript.src = folder + "/System/IFrame.js";

function OnInit() {
DS = window.external.AppletList.Applet("DSApplet").App;
SC = DS.Script;
LV = DS.Script.lv;

function IFrame_onload() {
OnInit();
}

function my_test() {
// Put a break point at the line below and then
// look at the item in the debugger.
var selected_item = LV.SelectedItem;
}

< /script>

< body scroll="no" onload="IFrame_onload()">


< table>
< tr>
< td>
< input id="debug_button" type="button" value="Debug" onclick="my_test()" />
< /td>
< /tr>
< /table>
< /body>

You might also like