ANSYS Workbench with Excel and Python

If you are looking to drive ANSYS Workbench with Excel and Python, you may have come across the post by PADT here: http://www.padtinc.com/blog/the-focus/workbench-and-excel-part-2-driving-workbench-from-excel-with-python. This was a really useful introduction, but I had some issues with getting the examples to work.

Scripting ANSYS Workbench with Excel and Python

I didn’t have much luck getting the PADT examples to work without some modification, so if you’ve had the same issue then see below for the working files. The content consists of an ANSYS Workbench project (included in the Archived format below, so be sure to File>Restore Archive… to extract the project), an Excel sheet to run the analysis and extract the results, and a Python script to handle the interaction of Excel and ANSYS.

One of the improvements I made was to update the Excel sheet so that there should be no need to modify the VBA code – all the options can be configured via the orange input boxes, including running the analysis in Batch Mode for quicker and background solving.

The Workbench project includes the analysis and parameter variables that we want to vary. This will obviously depend on your own model and requirements. It also has the output parameters that we will read in to Excel once the analysis has completed.

The Python script is the interface between Excel and ANSYS and will need to be modified to suit your model, but it should be relatively self-explanatory. Essentially the Workbench input parameters are given values from the Excel sheet, then the analysis is solved, then the Workbench output parameters are given back to Excel.

The Excel sheet looks like this:

 

Excel worksheet to drive ANSYS and extract results

Files for Scripting ANSYS Workbench with Excel and Python

  • Excel Workbook: TowerTool1.xlsm
  • ANSYS Workbench Project: ExcelTower1.wbpz (remove the .zip file extension once you have downloaded it)
  • Python Script: updateWB2.py (remove the .txt file extension once you have downloaded it)

I hope these work for you, but if you have any issues or questions then get in touch or leave a comment below.

 

 

 

4 Comments:

  1. Hi,
    thank you for this guide.
    This connects Ansys WB parameters and Excel by using Python but what if I want to exchange outputs/inputs that I cannot set as a parameter in Workbench?

    For example multiple load steps:
    t=1 Force=2000N
    t=2 Force=3200N
    .
    .
    .
    or other Ansys WB outputs like the stresses on a node for every of those load steps?

    thank you very much

    • Hi,
      you could resort to writing out input or output snippets of code to a text file. For example, if you want to output nodal stresses over multiple load steps then you could write these to a text file (using a Command Snippet, commands like *VWRITE etc can write to files) and then read this file back into Excel (which can be accomplished with a VBA macro). You can read time-varying loads from a text file direct from ANSYS Workbench (i.e. without needing any novel Command Snippets), then all you would need is to write a VBA macro to export the time-varying load (in the correct format for ANSYS) to a file.
      Hope that gives you some idea – unfortunately the price of automating the process is getting into the nitty-gritty of Excel VBA and possibly ANSYS APDL. Let me know if you have any other questions and I’ll try and help.
      Nick

      • Hi Nick,
        thanks for your quick answer. I got it to work by using VWRITE command but I had another solution in mind which I could not get to work yet.
        I can access almost everything directly through python:

        – Parameters with Parameters.GetParameter(Name=””)
        – None parameterized data like material density with GetSystem(“”).GetContainer(ComponentName=”Engineering Data”).GetMaterial(Name=””).GetProperty(Name=”Elasticity”).SetData….

        Isn’t it possible to access the Force in a similar way?
        I can access the setup with .GetContainer but that’s it, I did not manage to access the force.

        I can also change the loads with an ACT-Snippet inside Mechanical. Is it possible to use an ACT-Snippet over a python command and transfer the loads as variables?

        A last question, ACT has the nice autocomplete that shows me all different attributes of objects. Is there no package for python that shows me all options after typing the dot?

        thank you

        • Great that the VWRITE method worked. There should be a way to use ACT but honestly I just haven’t used it enough to know how to do it. If you figure it out, let me know – I’m sure it’ll be of use to other engineers in the future. In terms of ACT and python to transfer the loads as variables – this should be possible as ACT uses Python as the language, but again I’ve not used ACT so can’t help I’m afraid.
          For plain Python, there are numerous text editors / IDEs where you can use autocomplete / intellisense. I use Visual Studio Code and PyCharm, but there are others too. It will only autocomplete for modules that you have imported though, but is a very useful feature.

Leave a Reply

Your email address will not be published. Required fields are marked *