Insert results and export image and animation

Pernelle Marone-Hitz
Pernelle Marone-Hitz Member, Moderator, Employee Posts: 871
100 Answers 500 Comments 250 Likes First Anniversary
✭✭✭✭
edited June 2023 in Structures
  • In Mechanical scripting, I would like to add a total deformation result and an equivalent stress result, evaluate those results and export an image (.png) and an animation (.avi) for each result.

Best Answer

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 871
    100 Answers 500 Comments 250 Likes First Anniversary
    ✭✭✭✭
    Answer ✓

    Here is an example:

    import os
    export_folder = r'D:\Data'
    
    analysis = ExtAPI.DataModel.Project.Model.Analyses[0]
    solution = analysis.Solution
    solution.AddTotalDeformation()
    solution.AddEquivalentStress()
    solution.EvaluateAllResults()
    
    all_results = solution.GetChildren(DataModelObjectCategory.Result,True)
    for result in all_results:
        export_name = os.path.join(export_folder,result.Name)
        ExtAPI.Graphics.ExportImage(export_name +'.png')
        result.ExportAnimation(export_name + '.avi')
    

Answers

  • Erik Kostson
    Erik Kostson Member, Moderator, Employee Posts: 276
    50 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭
    edited April 29

    Thank you - this is really helpful. If we already have a set of existing results and we want to save an image for every result in a separate file then the for loop really helps and with the additional line (Tree.Activate) - add import wbjn and include the design-point number (dpn = wbjn.ExecuteCommand(ExtAPI,"returnValue(a+Parameters.GetActiveDesignPoint().Name)",a="DP")) in the file name so when running design point studies so the files are not overwritten:

    import os
    export_folder = r'D:\Data'
    analysis = ExtAPI.DataModel.Project.Model.Analyses[0]
    solution = analysis.Solution
    
    all_results = solution.GetChildren(DataModelObjectCategory.Result,True)
    for result in all_results:
        Tree.Activate(result)
        export_name = os.path.join(export_folder,result.Name)
        ExtAPI.Graphics.ExportImage(export_name +'.png')
    

    We can also add some filters (fres below) to get specific type of results (reaction or user defined say)

    import os
    fres=[]
    fres.Add('Ansys.ACT.Automation.Mechanical.Results.ProbeResults.ForceReaction')
    fres.Add('Ansys.ACT.Automation.Mechanical.Results.DeformationResults.TotalDeformation')
    fres.Add('Ansys.ACT.Automation.Mechanical.Results.UserDefinedResult')
    fres.Add('Ansys.ACT.Automation.Mechanical.Results.StressResults.EquivalentStress')
    fres.Add('Ansys.ACT.Automation.Mechanical.Results.DeformationResults.DirectionalDeformation')
    export_folder = r'D:\Data\test2'
    analysis = ExtAPI.DataModel.Project.Model.Analyses[0]
    solution = analysis.Solution
    
    all_results = solution.Children
    
    for result in all_results:
        restype=str(result.GetType())
        if  (restype == fres[0] or restype == fres[1] 
            or restype == fres[2] or restype == fres[3]
            or restype == fres[4]):
            Tree.Activate(result)
            export_name = os.path.join(export_folder,result.Name)
            ExtAPI.Graphics.ExportImage(export_name +'.png')
    
    

    Finally we can use the below script in a Python Code (Insert under solution and with a After Post type target callback) to export some results to file :

    def after_post(this, analysis):# Do not edit this line
        import os
        fres=[]
        fres.Add('Ansys.ACT.Automation.Mechanical.Results.DeformationResults.TotalDeformation')#Ansys.ACT.Automation.Mechanical.Results.ThermalResults.TemperatureResult for Temp.
        fres.Add('Ansys.ACT.Automation.Mechanical.Results.UserDefinedResult')
        fres.Add('Ansys.ACT.Automation.Mechanical.Results.StressResults.EquivalentStress')
        fres.Add('Ansys.ACT.Automation.Mechanical.Results.DeformationResults.DirectionalDeformation')
        export_folder = r'D:\Data\test2'
        analysis = ExtAPI.DataModel.Project.Model.Analyses[0]
        solution = analysis.Solution
    
        all_results = solution.Children
    
        for result in all_results:
            restype=str(result.GetType())
            if  (restype == fres[0] or restype == fres[1] 
                or restype == fres[2] or restype == fres[3]):
                Tree.Activate(result)
                export_name = os.path.join(export_folder,result.Name)
                result.ExportToTextFile(export_name +'.txt')
        pass
    
  • sombodyfromtheworld
    sombodyfromtheworld Member Posts: 32
    10 Comments First Anniversary Name Dropper
    **
    edited December 2023

    Hi
    I would like to AppendGraph to my image as well. How can I do that?

    GRAPHICS = ExtAPI.Graphics
    GRAPHICS.ExportViewports('C:/.../image.png', GraphicsImageExportFormat.PNG, GraphicsViewportsExportSettings().AppendGraph = True)
    

    like this:

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 871
    100 Answers 500 Comments 250 Likes First Anniversary
    ✭✭✭✭

    Hi @sombodyfromtheworld , exporting the graph view cannot be done through the supported APIs. I'd recommend grabbing the values of the plot (result.PlotData) and reconstructing the graph elsewhere if needed. There are some unsupported/undocumented methods to export the graph, but there is no guarantee they'll work: https://discuss.ansys.com/discussion/1214/how-to-take-a-screenshot-of-your-mechanical-application

  • sombodyfromtheworld
    sombodyfromtheworld Member Posts: 32
    10 Comments First Anniversary Name Dropper
    **
    edited December 2023

    @Pernelle Marone-Hitz you mean that I can access X,Y coordinates of the plot and build the custom plot for instance in matplotlib?

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 871
    100 Answers 500 Comments 250 Likes First Anniversary
    ✭✭✭✭

    You can't access the values of the graph directly, but you can access the values of the 3D plot, and rebuild the graph (I acknowledge it is not straightforward).

  • sombodyfromtheworld
    sombodyfromtheworld Member Posts: 32
    10 Comments First Anniversary Name Dropper
    **

    @Pernelle Marone-Hitz could you please post the command to access the values of the 3D plot.

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 871
    100 Answers 500 Comments 250 Likes First Anniversary
    ✭✭✭✭

    @sombodyfromtheworld There are already a couple of examples of using PlotData on the forum. For example, this one: https://discuss.ansys.com/discussion/comment/1511#Comment_1511

  • sombodyfromtheworld
    sombodyfromtheworld Member Posts: 32
    10 Comments First Anniversary Name Dropper
    **
    edited December 2023

    @Pernelle Marone-Hitz I see. PlotData returs only last substep values (at 1s in my case). In order for me to build the plot I need to EvaluateAllResults() at each substep and retrive max value from PlotData.

    something laike this, right?

    time_X_coord = []
    values_Y_coord = []
    for substep in np.arange(0, 1.1, 0.1):        
        new_stress.DisplayTime = (Quantity, substep)
        new_stress.EvaluateAllResults()
        max_stress_value = max(new_stress.PlotData['Values'])
        time_X_coord.append(substep)
        values_Y_coord.append(max_stress_value)
    

    or can I just return the TabularData from this table?

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 871
    100 Answers 500 Comments 250 Likes First Anniversary
    ✭✭✭✭

    Indeed, for PlotData, you would need to change the time step and re-evaluate again. You can also grab the tabular data, but it also uses unsupported/undocumented commands: https://discuss.ansys.com/discussion/255/how-to-access-tabular-data-using-act

  • Mike.Thompson
    Mike.Thompson Member, Employee Posts: 361
    25 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭
    edited December 2023

    @sombodyfromtheworld ,
    If you would like time-history, data from the results, most often the best way to get this is through the data processing framework (DPF). Create your own python code, using DPF to extract the results for all-time points.
    There are a number of examples on this forum of how to extract different results using DPF. It is a bit more work, but provides much more control on what data you want, and how to export it. Also, once you have set up the code for one type of result, it is often just a couple lines to change for a different result. For example, changing from equivalent stress to principal, stress, or a stress tensor to a strain tensor.