How to export images via Python Code object (in batch run) with Graphics.Camera.SetFit()?

Rohith Patchigolla
Rohith Patchigolla Member, Moderator, Employee Posts: 212
100 Comments 25 Answers Second Anniversary 25 Likes
✭✭✭✭

I am exporting images via Python Code object under Solution with "After Post" callback. But, some of my graphics command such as Camera.SetFit() or Camera.Rotate() are not working correctly. How to get them working?

Answers

  • Rohith Patchigolla
    Rohith Patchigolla Member, Moderator, Employee Posts: 212
    100 Comments 25 Answers Second Anniversary 25 Likes
    ✭✭✭✭
    edited September 5

    The trick is to use any Graphics commands (such as Camera.SetFit() or Camera.Rotate() or changing certain Result Preferences such as changing Deformation Scale - Graphics.ViewOptions.ResultPreference.DeformationScaleMultiplier) only after the result object is activated using resultObj.Activate() command.

    An example is shown below to export all deformations and equivalent stress results of a particular analysis for each design point as pngs to the User Files Directory of the Project.

    Steps:
    1. RMB on Solution of an analysis --> Insert --> Python Code
    2. Change the "Target Callback" property to "After Post"
    3. Replace the existing script in the "Script" tab to the script below.
    4. RMB on Python Code --> Connect
    5. Before launching DP study, please go to WB --> Tools --> Options --> Mechanical --> "Reconnect Python Code Object when Mechanical is launched"

    def after_post(this, solution):# Do not edit this line
    
        #import some required modules
    
        import os
        import wbjn
    
        #Get Design Point Number as Text (to append it to the image name)
        dpn=wbjn.ExecuteCommand(ExtAPI,"returnValue(a+Parameters.GetActiveDesignPoint().Name)",a="DP")
        ExtAPI.Log.WriteMessage("DP " + str(dpn) + " Start")
        #Get the User Files Directory of the Project to save images. Ofcourse you can use any path you like.
        UserFilesDir = wbjn.ExecuteCommand(ExtAPI,"returnValue(GetUserFilesDirectory())")
    
        #Some Graphics Settings
        gset = Ansys.Mechanical.Graphics.GraphicsImageExportSettings()
        gset.CurrentGraphicsDisplay = False
        gset.Width = 1920
        gset.Height = 1080
    
        analysis = solution.Parent
        #Get All Equivalent Stress Objects
        EqvStressResults = [child for child in solution.Children if child.DataModelObjectCategory == DataModelObjectCategory.EquivalentStress]
        #Get All Total Deformation Objects 
        TotalDeformationResults = [child for child in solution.Children if child.DataModelObjectCategory == DataModelObjectCategory.TotalDeformation]
        AllResults = EqvStressResults + TotalDeformationResults
        for result in AllResults:
            result.Activate()
            Graphics.ViewOptions.ResultPreference.DeformationScaling = MechanicalEnums.Graphics.DeformationScaling.True
            Graphics.ViewOptions.ResultPreference.DeformationScaleMultiplier = 10
            ExtAPI.Graphics.Camera.SetSpecificViewOrientation(ViewOrientationType.Back)
            ExtAPI.Graphics.Camera.SetFit()
            fName=dpn+"_"+result.Name+"_" + analysis.Name+".png"
            fPath = os.path.join(UserFilesDir, fName)
            if os.path.exists(fPath):
                os.remove(fPath)
            Graphics.ExportImage(fPath, GraphicsImageExportFormat.PNG, gset)
    
        ExtAPI.Log.WriteMessage("DP " + str(dpn) + " Finished")
    
  • SchluppIng
    SchluppIng Member Posts: 15
    Name Dropper First Comment
    **

    Perhaps I didn't phrase my question entirely correctly, but the issue has been resolved as I found a solution. Nonetheless, thank you for your quick response. :)