Last week we had a very well attended Webinar on using Scripting with Workbench. We talked about how you can control projects, parameters, and material properties with python. We also talked about how you can use python to drive data compliant applications by sending commands and/or parameters to it. You can watch the webinar at:
Click on PADT ANSYS Webinar Series and then find the recording from August 11, 2011.
Several attendees asked if we had an example of using python to bring up a GUI on top of workbench. We did not. This is how The Focus articles are born.
In case you missed the webinar or didn’t follow some of it, here are the important points:
- Workbench has IronPython built into it from the ground up
- You can record whatever you do in the GUI to create scripts
- You can read those scripts in to control Workbench
- ANSYS uses IronPython because it talks to Windows programs
- Get detailed info at www.python.org, www.ironpython.net, and www.ironpython.info
Most of the examples below are derived from information I got at: http://www.ironpython.info/index.php/Contents#Windows_Forms
We will be making a GUI for the famous Tower of Test application. We will need a place to enter in the size of the tower, a pressure load. Then a simple table showing the results when we get them. And of course, buttons to cancel and execute.
If you are already an experienced Windows programmer, this will be a slam dunk. If you are not, then you will need to make sure you learn your way around the MSDN .Net Class Library Reference:
That is where you find properties and methods for the GUI. We will be spending most of our time in System.Windows.Form.
Right now, some of you might be asking “Why .NET?” Python supports probably over a dozen different GUI’s. I am using .NET for a few reasons:
- It is built into IronPython. No need to download a library and install it right
- It will have a consistent look and feel to Workbench
- More people know .NET than the other GUI libraries.
If you are anit-microsoft, you can poke around and find a library that fits your needs. The process will be the same.
Crawl: Always Start with Hello World
Anytime I try and learn a new language, I usually find a “Hello World” Example. And, thanks to the internet I found one that only needed slight modification:
So first, we access the huge number of Windows tools by importing clr. Then, get a reference to Windows Forms. Once that is done we need to import more from Windows Forms. This is a big tool box so it has lots of levels you have to dig through. For this guy we need to be able to do the form itself (the window) and a label (text in the window).
Now that we have our objects, we create a form with the title “Hello World Form” and a label with the text “Hello World!” We then use the Add method to add the label control to the form. Last, we use the Show Dialog method to show the form. TADA.
To give it a try, copy and paste this into your own text editor and then go into Workbench and choose:
File->Scripting->Open Command Window…
Then paste in the script. Or you can save it to a file and read it in. I like to paste so I can see my error messages as it goes.
Now you are a Workbench GUI programmer. Put it on your resume!
Walk: Add Some Controls to the Window
Next we want to do a bit more than saying hello.
The important thing I learned that I just didn’t’ get before is that you have to make a class out of your form and any functions that get called when the user clicks on a button or interacts in any way with the form. You do this because Windows will then pass information about the form back to the event function.
In the example below is like “Hello World” to start, then we make 4 text boxes with labels for the user to enter data into. We then add an OK and a Cancel button. The code is commented with an explanation of what is going on. If something does not make sense, look it up in a python reference or ask someone who is a python programmer.
If you remember the previous article on Driving Workbench from Excel this will all look familiar. If you don’t review the article, especially the part about getting parameters in and out of Workbench with Python. To finish our example we will use the same basic code to:
- Get the current values from Workbench and set our dialog to show those values
- When the user hits OK:
- Set the Workbench parameters to the current dialog box parameters
- Update the model
- Get the resulting deflection and display it.
The code is shown below, with the bits added for the Workbench interaction highlighted. Note that we also introduced the specification of font properties on a label. Making it even more fancy.
Next Steps and Suggestions
And that in a nutshell is the basics of putting a GUI on Workbench. By using the calls to Workbench, .NET or any other complaint application, you can script away and do what you want.
Much more can be done. You could have the solver you are running in update and create some plots, and then use python to find those plots (say search for any *.jpg files in the project director created after the OK button was pushed) and insert them into PowerPoint with text that you also gather.
There really are no limitations.
To learn more, try stuff. Get to know the MSDN documentation to know what you can do to make a more professional GUI. The Windows Forms part of ironpython.info is a great resource for this and points to MSDN as needed.
There is a lot more you can do with ANSYS Workbench and the various solvers. And with each release, workbench will add and document more.
If you write a cool application you can share, make sure you put it on XANSYS or send it to Sheldon at ANSYS.net.