Simple Guide to IronPython GUI Creation and Debugging

1 © 2011 ANSYS, Inc. September 8, 2020 Outline – IronPython GUI Creation and Debugging

Simple GUI Creation Example project showing creation of a basic GUI to perform an action in HFSS Debugging Troubleshooting scripts using Visual Studio Shell

2 © 2011 ANSYS, Inc. September 8, 2020 IronPython Graphical User Interfaces

A major advantage of IronPython is the ability to create modern GUI elements Scripts created can now be more intuitive and interactive to common users Creating a GUI can be simplified through the use of an Integrated Development Environment (IDE) This will serve as a simple guide to creating GUI for IronPython

3 © 2011 ANSYS, Inc. September 8, 2020 SharpDevelop IDE for GUI Creation

Free open-source IDE for development on Microsoft’s .NET platform Download at - http://www.icsharpcode.net/opensource/sd/download/

SharpDevlop is an easy to use IDE that we will use only for the design and automatic code generation of simple graphical user interfaces Although this can be used for debugging, I would recommend using Visual Studio as discussed in the debugging section of this document

4 © 2011 ANSYS, Inc. September 8, 2020 Example Project

We will create a simple GUI that takes a single text input from a user and based on this input, draws a box in HFSS

5 © 2011 ANSYS, Inc. September 8, 2020 Creating a New Solution

Open SharpDevelop 4.1 and create a New Solution Choose: Categories → Python Templates → Windows Application Set name and location

6 © 2011 ANSYS, Inc. September 8, 2020 Blank Project

Toggle between source code and form design

7 © 2011 ANSYS, Inc. September 8, 2020 Create GUI

Properties Drag tools onto form to create GUI

Highlight objects to view and modify properties 8 © 2011 ANSYS, Inc. September 8, 2020 Changing Properties Highlight the Label Object in the GUI Designer The properties will now be displayed in the Properties Windows Change the Text Property of lable1 to Box Size

Notice the Name of this object is still referred to as label1, this is how the object will be referenced in the code, likely this should be changed to have a more meaningful name but for this example it will be left as default Change Text Property of Button1 to Draw Box Change Text Property of textBox1 to 1, this will be the default value entered into the text box The GUI should now appear as it is shown here 9 © 2011 ANSYS, Inc. September 8, 2020 Automatically Generated Code

Press “Play” button to preview GUI Toggle between GUI designer and source code with tabs at bottom of window

You can see the code that was automatically generated, reflecting the properties of each button, text box and label

10 © 2011 ANSYS, Inc. September 8, 2020 Create Event for Button Click

Select Events Icon in Properties window while button1 is selected

In form designer, double click on button to insert code for click event

Function Code Generated by double clicking on button1

Action of Clicking button will call the function Button1Click 11 © 2011 ANSYS, Inc. September 8, 2020 Save code for Completed GUI Code for GUI is now completed The MainForm.py code can be copied and pasted into new .py file or File→Save As… If Save As is used, there is no need to save a resource file for the simple GUI, so press cancel when prompted For this example save file as GUI_Tutorial.py, we will use this file later to add functionality and make this GUI perform an action in HFSS We also have the option to write the additional code directly in SharpDevelop but I prefer to use either Visual Studio or a text editor

12 © 2011 ANSYS, Inc. September 8, 2020 Add Operations to Button Click Function

• Function automatically generated in SharpDevelop does not do anything Button click will result in nothing happening • Lets add a simple function so that when the script is launched from within HFSS HFSS will display the GUI Read the value entered into the text box Draw a box in HFSS based on the text box entry when the button is pressed

13 © 2011 ANSYS, Inc. September 8, 2020 Edit Script Open script file saved from SharpDevelop, GUI_Tutorial.py Any text editor will work, but be mindful of how tabs and whitespaces are treated in different text editors. IronPython requires consistent indentation I use Visual Studio 2010 as an editor, just go to menu File → Open → File

We need to add a few additional lines that were left out when the GUI_Tutorial.py file was saved from SharpDevelop

14 © 2011 ANSYS, Inc. September 8, 2020 Modify Code

Code copied or saved from SharpDevelop will be missing two sections of code from the start and end of the .py file SharpDevelop creates 2 .py files for each project, a Program.py and Mainform.py, when we did a Save As we only saved the MainForm.py and did not include some code included Program.py. We must add this code to run our GUI from the single file that we saved. After GUI is created we will no longer be using SharpDevelop Add this code to the very start of GUI_Tutorial.py code (remove any indentation when copied and pasted) import clr clr.AddReference("System.Drawing") clr.AddReference("System.Windows.Forms") And this to the very end (remove any indentation when copied and pasted) Application.EnableVisualStyles() form = MainForm() Application.Run(form) The GUI will now launch if the GUI_Tutorial code is run from within HFSS

15 © 2011 ANSYS, Inc. September 8, 2020 Edit Script

Under the Button1Click function We will enter code to draw a box Notice the Xsize, Ysize and Zsize have value of textBox1.Text with the “mm” string concatenated. When Button1 is clicked, the script will get the current active project and design and draw a box with the size specified in textbox1.

16 © 2011 ANSYS, Inc. September 8, 2020 Button 1 Copy and Paste

Text for the function is placed here for your convenience to copy and paste into GUI_Tutorial.py under the function Button1Click. Make sure to correctly indent the pasted code to match what is shown on previous slide

oProject = oDesktop.GetActiveProject() oDesign = oProject.GetActiveDesign() oEditor = oDesign.SetActiveEditor("3D Modeler")

oEditor.CreateBox( { "NAME" : "BoxParameters", "XPosition": "0mm", "YPosition": "0mm", "ZPosition": "0mm", "Xsize": self._textBox1.Text + "mm", "Ysize": self._textBox1.Text + "mm", "Zsize": self._textBox1.Text + "mm" }, { "NAME": "Attributes", "Name": "Box1", "Flags": "", "Color": "(132 132 193)", "Transparency": 0, "PartCoordinateSystem": "Global", "UDMId": "", "MaterialValue" : "\"vacuum\"", "SolveInside": True } ) 17 © 2011 ANSYS, Inc. September 8, 2020 Translation of VBscript Command to IronPython

Vbscript can easily be converted to IronPython

The Vbscript command Set oProject = oDesktop.GetActiveProject() Would be changed to oProject = oDesktop.GetActiveProject() in IronPython

Change Array( to [ and close with a ] instead of the )

Remove the line continuation symbols

Change named arrays from "NAME:BoxParameters", to "NAME" : "BoxParameters",

*See Desktop Scripting with IronPython by Vamsidhar Juvvigunta for more complete guide 18 © 2011 ANSYS, Inc. September 8, 2020 Create Box: IronPython vs. VBscript Commands Alternative dictionary syntax IronPython VBscript oEditor.CreateBox( oEditor.CreateBox Array("NAME:BoxParameters", _ { "XPosition:=", “0mm", "YPosition:=", _ "NAME" : "BoxParameters", “0mm", "ZPosition:=", "0mm", "XSize:=", “1mm", _ "XPosition" : "0mm", "YSize:=", “1mm", "ZSize:=", _ "YPosition" : "0mm", “1mm"), Array("NAME:Attributes", "Name:=", "Box1",_ "ZPosition" : "0mm", "Flags:=", "", "Color:=", _ "Xsize" : "1mm", "(132 132 193)", "Transparency:=", 0, _ "Ysize" : "1mm", "PartCoordinateSystem:=", _ "Zsize" : "1mm" "Global", "UDMId:=", "", "MaterialValue:=", "" & Chr(34) }, & "vacuum“ _ { & Chr(34) & "", "SolveInside:=", _ "NAME" : "Attributes", true) "Name" : "Box1", "Flags" : "", "Color" : "(132 132 193)", "Transparency" : 0, "PartCoordinateSystem": "Global", "UDMId" : "", "MaterialValue" : "\"vacuum\"", "SolveInside" : True } )

19 © 2011 ANSYS, Inc. September 8, 2020 Running Script Open HFSS v14 Insert a blank project with a blank HFSS design In HFSS, menu Tools → Run Script Set File type to IronPython Script Files and browse and open to .py file saved

20 © 2011 ANSYS, Inc. September 8, 2020 Adding Script as an External Tool Creating an external tool for a script will eliminate the need to browse and find a script when it needs to be ran, instead it is located under the tool menu To Add an External Tool - Tools → External Tools… Enter GUI Tutorial under Menu Text: Enter path to GUI_Tutorial.py in the Command line Select Add

21 © 2011 ANSYS, Inc. September 8, 2020 Debugging IronPython

For debugging I would recommend to use the Python Tools for Visual Studio 2010 and 2010 Shell. Both tools can be freely downloaded http://pytools.codeplex.com/

http://www.microsoft.com/download/en/details.aspx?id=115

With these tools installed, you are ready to start debugging

22 © 2011 ANSYS, Inc. September 8, 2020 Steps to Debugging

Set Environment Variable ANSOFT_SCRIPT_DEBUG = 1 Insert Breakpoint _ipybreak() Run script and attach the debugger Tools → Run Script, Debug

23 © 2011 ANSYS, Inc. September 8, 2020 Set Environment Variable

• In order to debug a script triggered by a breakpoint you must set an environment variable • ANSOFT_SCRIPT_DEBUG = 1

24 © 2011 ANSYS, Inc. September 8, 2020 Insert BreakPoint

• To start debugging, a breakpoint must be inserted into the script to trigger the debugger • If the script is being run from within the HFSS desktop, add the line _ipybreak() to your script at the point you wish to start debugging

25 © 2011 ANSYS, Inc. September 8, 2020 Run script and attach the debugger

• From within HFSS, launch the script you wish to debug – Tools → Run Script • The user-defined breakpoint will trigger the message and give the option to Debug the script • Select Microsoft Visual Studio 2010 from the list of possible debuggers and select Yes to begin debugging – If this window does not appear, you may need to first launch Visual Studio and attach to process (hfss.exe) before hitting the Debug button

26 © 2011 ANSYS, Inc. September 8, 2020 Debugging

You should now be in Visual Studio ready to start stepping through the code The breakpoint inserted into the code may result in this block of code initially being displayed

Pressing F10 will step to the next line in the code which should be the line in your script where you inserted the breakpoint F10 and F11 can be used to step through script in order to debug

27 © 2011 ANSYS, Inc. September 8, 2020