ANSYS Mechanical Scripting: HOWTO Part 4

Categories:

Where we left Off Last Time…

In the third part of this series, I left you with an introduction to the ANSYS Selection Manager and Geometry B-Rep.  We also created a sample script that iterated over all of the faces in a selected body and created a named selection for each face.   We built on our technique described in Part 2 for discovering ANSYS functionality. 

Introducing the Microsoft Visual Studio Script Debugger

In this post I want to introduce you to the Visual Studio Script Debugger.  Running ANSYS Mechanical underneath the debugger is definitely the most powerful tool we have at our disposal when it comes to creating custom scripts.  If you can learn to use this tool effectively, you will be able to watch the inner workings of Mechanical interact with your scripts in a precisely controlled environment.  Furthermore, the rich debugger displays will allow you to see most all of the mechanical objects in suspended animation and will also allow you to investigate their properties and methods in real time.  Finally, the “Immediate Window” in the debugger will allow you to execute random pieces of code that you type in on the fly while Mechanical is suspended in the debugger.  This will allow you to test out various techniques and code sequences in one debugging session.   Simply put, you must become proficient at using the debugger to have any real hope of successfully scripting the ANSYS Mechanical tool.

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

 

image

 

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…image
  5. In the dialog that pops up, look for the AnsysWBU.exe executable in the list.  image
  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…

At this point you can set breakpoints, step into and out of functions, interrogate variable values, etc…  The debugger is truly a remarkable tool, as we shall briefly touch on next.

Whirlwind Tour of the MS Debugger

Below is a screen shot of our little script inside the visual studio debugger.

image

As you can see, the current execution line is located over the debugger statement.  Pressing F10 will advance one line at a time.  Also, you will notice that there is a window below the code window with a tab called “Locals”  This tab shows the local variables defined in this function.  At this point in the execution, they are not defined.  However, as we step the execution, these variables will come into play.  You can watch what happens to variables as you step through your code here.

Also, you will notice a second window called the “Immediate Window”.  This window may not be visible for your default debugging layout.  If you don’t see it, navigate to Debug->Windows->Immediate Window

image

In the immediate window you can enter in code that will be executed “immediately” while the program is stopped in the debugger.  So, for example, if you see a problem in your script that might be fixed with a code change you may be tempted to leave the debugger fix it and then re-run.  However, if you are unsure of your fix, or if you want to try a number of alternatives, you can just type them into the immediate window and see what happens.  It works as if you were executing new code at the current breakpoint location that isn’t in your script.  You can then step forward in your script to execute a few lines, enter something in the immediate window, and then continue on stepping.  It is a very powerful technique.

Lets step through a few lines.  I’m going to press F10 four or five times.  At that point I get the following screen.

image

You can see that my_array now has an ellipse next to it and I is equal to 1.  If I click on the plus symbol next to my_array in the locals pane, I get the following:

image

You can see that we’ve filled the zero-th item in the array with the value zero.  Lets hit F10 some more…  Note, you can also hover your mouse over the array in the code window.  You can see I’ve filled in a few more values

image

The print screen function doesn’t capture it well, but you can see the values in the array have changed.  Now, lets say I just realized that for some code below to work properly, the fifth value in this array needs to be less than 10.  (We don’t have any code below, and this is quite contrived, but bear with me and use your imagination…)  So, I just pop down into the immediate window and type:

my_array[5] = 8;

image

When I press return, this will execute that single line of code. You can see that my_array[5] now holds the value 8 instead of 10.

image

 

You can also set break points in your code.    Just right click on the line, and choose Breakpoint->Insert Breakpoint

image

Breakpoints work kind of like the debugger command, only they don’t litter your code with commands.  I usually insert one debugger command at the top of my script and then breakpoints where needed.   Breakpoints can also be conditional.  That is, they won’t break unless some condition is true.  This is great for executing most of a loop, but breaking on the last or next to last item.  After you have inserted a breakpoint, just right click on the line and choose Breakpoint->Condition…

image

You can insert a condition like:

image

From here, press F5 which is the Run command, and the code will execute inside the loop until I == 8.

Conclusion

The debugger is your friend; plain and simple.  Use it wisely and use it often.  In fact, if you want to script in Mechanical, you are going to have to become intimately familiar with the debugger.  In a future article, I will show you more powerful debugging tips when the debugger by itself lets you down. Stay tuned…

Categories

Get Your Ansys Products & Support from the Engineers who Contribute to this Blog.

Technical Expertise to Enable your Addictive Manufacturing Success.

PADT Pulse Newsletter Screen Grab from March 2023

PADT’s Pulse Newsletter

Keep up to date on what is going on at PADT by subscribing to our newsletter.


By submitting this form, you are consenting to receive marketing emails from: . You can revoke your consent to receive emails at any time by using the SafeUnsubscribe® link, found at the bottom of every email. Emails are serviced by Constant Contact

Share this post:

Upcoming Events

05/31/2023

Driving Automotive Innovation with Additive - Webinar

05/24/2023

Hill Air Force Base Tech Expo

05/24/2023

Structural Updates in Ansys 2023 R1 (3) – Structural Optimization & Ex

05/23/2023

CROSSTALK 2023: Emerging Opportunities for Advanced Manufacturing Smal

05/10/2023

Signal & Power Integrity Updates in Ansys 2023 R1 - Webinar

04/26/2023

Additive Manufacturing Updates in Ansys 2023 R1 - Webinar

04/20/2023

38th Space Symposium Arizona Space Industry

More Info

04/19/2023

38th Space Symposium
Arizona Space Industry

04/19/2023

Additive Aids for Manufacturing - Webinar

04/18/2023

38th Space Symposium
Arizona Space Industry

04/17/2023

38th Space Symposium

04/13/2023

Venture Madness 2023

04/12/2023

Fluid Meshing & GPU-Solver Updates in Ansys 2023 R1 - Webinar

03/29/2023

8th Thermal and Fluids Engineering Conference

03/29/2023

Structural Updates in Ansys 2023 R1 - Composites, Fracture & MAPDL

03/28/2023

8th Thermal and Fluids Engineering Conference

03/27/2023

8th Thermal and Fluids Engineering Conference

03/26/2023

8TH Thermal and Fluids Engineering Conference

03/24/2023

Arizona BioPreneur Conference | Spring 2023

03/22/2023

2023 Arizona MedTech Conference

03/22/2023

Optimize Jigs & Fixtures with Additive - Webinar

03/15/2023

3D Design Updates in Ansys 2023 R1 - Webinar

03/08/2023

Competitive Advantages of 1D/3D Coupled Simulation - Webinar

03/01/2023

High Frequency Updates in Ansys 2023 R1 - Webinar

02/22/2023

Additive Advantages in Aerospace - Webinar

02/15/2023

Structural Updates in Ansys 2023 R1 (1) - Webinar

02/09/2023

IME 2023: MD&M | WestPack | ATX | D&M | Plastek

02/08/2023

IME 2023 MD&M | WestPack | ATX | D&M | Plastek

02/07/2023

IME 2023 MD&M | WestPack | ATX | D&M | Plastek

01/27/2023

Arizona Photonics Days, 2023

01/26/2023

Arizona Photonics Days, 2023

01/26/2023

TIPE 3D Printing | 2023

01/26/2023

Venture Cafe Phoenix Talent Night - Job Fari

01/26/2023

VFS 2023 Autonomous/Electric VTOL Symposium

01/25/2023

Arizona Photonics Days, 2023

01/25/2023

Building A.M.- Utah: Kickoff!

01/25/2023

TIPE 3D Printing | 2023

01/25/2023

VFS 2023 Autonomous/Electric VTOL Symposium

01/24/2023

VFS 2023 Autonomous/Electric VTOL Symposium

01/24/2023

TIPE 3D Printing | 2023

01/18/2023

2023 AZ Tech Council Golf Tournament

12/21/2022

Simulation Best Practices for 5G Technology - Webinar

12/14/2022

Digital Twins Updates in Ansys 2022 R2 - Webinar

12/08/2022

Tech the Halls - AZ Tech Council Holiday Mixer

12/07/2022

Electric Vehicle and Other Infrastructure Update Panel

11/30/2022

SPEOS Updates in Ansys 2022 R2 - Webinar

11/23/2022

Simulation Best Practices for Electronics Reliability - Webinar

11/16/2022

Discovery Updates in Ansys 2022 R2

11/10/2022

VentureCafe Phoenix Panel: Venture Capital in AZ

11/08/2022

2022 GOVERNOR’S CELEBRATION OF INNOVATION AWARDS + TECH SHOWCASE

11/03/2022

VentureCafe Phoenix Panel: Angel Investment in AZ

11/02/2022

High & Low Frequency Electromagnetics Updates in Ansys 2022 R2

10/26/2022

Simulation Best Practices For Chip-Package-System Design & Development

10/20/2022

Nerdtoberfest 2022

10/19/2022

2022 Southern Arizona Tech + Business Expo

10/19/2022

LS-DYNA Updates in Ansys 2022 R2 - Webinar

10/17/2022

Experience Stratasys Truck Tour - Clearfield Utah

10/14/2022

ASU School of Manufacturing Systems and Networks - Formal Opening Cele

10/14/2022

Experience Stratasys Truck Tour - Midvale Utah

10/12/2022

Experience Stratasys Truck Tour - Littleton Colorado

10/06/2022

Fluids Updates in Ansys 2022 R2 - Webinar

10/05/2022

Experience Stratasys Truck Tour - Colorado Springs

09/29/2022

White Hat Life Science Investor Conference - 2022

09/28/2022

2022 AZBio Awards

09/28/2022

Simulation Best Practices for Rotating Machinery Design & Development

09/21/2022

ExperienceIT NM 2022

09/21/2022

Additive Updates in Ansys 2022 R2 - Webinar

09/14/2022

Rocky Mountain Life Sciences Investor & Partnering Conference

09/08/2022

Ansys Optics Simulation User Group Meeting - Virtual

09/08/2022

Ansys Optics Simulation User Group Meeting

09/07/2022

SI & PI Updates in Ansys 2022 R2 - Webinar

08/31/2022

Simulation Best Practices for Developing Medical Devices - Webinar

08/24/2022

Mechanical Updates in Ansys 2022 R2 - Webinar

08/10/2022

Tucson after5 Tech Mixer: Ruda-Cardinal

08/05/2022

Flagstaff Tech Tour, 2022

08/02/2022

2022 CEO Leadership Retreat

08/01/2022

2022 CEO Leadership Retreat

07/27/2022

Thermal Integrity Updates in Ansys 2022 R1 - Webinar

07/20/2022

Simulation Best Practices for the Pharmaceutical Industry - Webinar

07/14/2022

NCMS Technology Showcase: Corpus Christi Army Depot

07/13/2022

NCMS Technology Showcase: Corpus Christi Army Depot

07/13/2022

Additive & Structural Optimization Updates in Ansys 2022 R1 - Webinar

07/07/2022

Arizona AADM Conference, 2022

06/29/2022

LS-DYNA Updates & Advancements in Ansys 2022 R1 - Webinar

06/23/2022

Simulation Best Practices for Wind Turbine Design - Webinar

06/15/2022

MAPDL Updates & Advancements in Ansys 2022 R1 - Webinar

06/01/2022

Mechanical Updates in Ansys 2022 R1 - pt. 2 Webinar

05/26/2022

Modelling liquid cryogenic rocket engines in Flownex - Webinar

05/25/2022

SMR & Advanced Reactor 2022

05/25/2022

05/24/2022

SMR & Advanced Reactor 2022

05/19/2022

RAPID + tct 2022

05/19/2022

Venture Cafe Roundtable: AI & Healthcare

05/18/2022

Tucson after5 Tech Mixer: World View

05/18/2022

RAPID + tct 2022

More Info

05/18/2022

Signal & Power Integrity Updates in Ansys 2022 R1 - Webinar

05/18/2022

Simulation World 2022

05/17/2022

RAPID + tct 2022

05/11/2022

Experience Stratasys Manufacturing Virtual Event

Search in PADT site

Contact Us

Most of our customers receive their support over the phone or via email. Customers who are close by can also set up a face-to-face appointment with one of our engineers.

For most locations, simply contact us: