Python Result shall wait until results evaluated

SchluppIng
SchluppIng Member Posts: 15
Name Dropper First Comment
**

Hi,

I have a question regarding Python Results in Mechanical. I am automatically generating Python results using a lot of code, structured as follows:

def createPicture(current_path):
import toolbar
toolbar.DoUndeformedWireFrameResultView(ExtAPI)
Graphics.ViewOptions.ResultPreference.ExtraModelDisplay =
MechanicalEnums.Graphics.ExtraModelDisplay.UndeformedWireframe
scfOut = ExtAPI.DataModel.GetObjectsByName('Outside SCF overview')[0]
imgPath = current_path + "\Results\SCFResOut.png"

cam = Graphics.Camera
cam.FocalPoint = Point([0, 7.5, 0], 'm')
cam.ViewVector = Vector3D(1, 0, 0)
cam.UpVector = Vector3D(0, 1, 0)
cam.SceneHeight = Quantity(15, 'm')
cam.SceneWidth = Quantity(botRad, 'm')
img = scfOut.AddImage()
img.SourceType = 1
img.ImagePath = imgPath
ExtAPI.Graphics.ExportScreenToImage(imgPath )

def post_started(sender, analysis): # Do not edit this line
define_dpf_workflow(analysis)
# Here comes my code, which is executed and performs some conversions, etc.

createPicture(path)

Now, the script should wait until the results are loaded and evaluated. Once they are created, the script should take a picture and save it. However, I can't find a function that waits and only creates the images after the evaluation is complete.

The issue is that if I let the script create the images while the evaluation is still in progress, it only produces a picture of the geometry without the results.

Answers

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

    Hi

    This example creates images when the results are done (after post ):

    https://discuss.ansys.com/discussion/4399/how-to-export-images-via-python-code-object-in-batch-run-with-graphics-camera-setfit

    All the best

    Erik

  • 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. :)

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

    Hi @SchluppIng - can you post your solution here as others might find this useful if they encounter a similar issue.
    Erik

  • SchluppIng
    SchluppIng Member Posts: 15
    Name Dropper First Comment
    **

    First of all I created an ACT which calls a function to create an event based python code.
    Example func:

    def addPythonCodeEvent(analysis, currentPath):
        import os
    
        #Add some path for self created libs/funcs.
        paths_to_append = [os.path.join(currentPath, folder) for folder in os.listdir(currentPath) if os.path.isdir(os.path.join(currentPath, folder))]
        paths_to_append.append(currentPath)
        for path in paths_to_append:
            if path not in sys.path:
                path = os.path.join(path)
                sys.path.append(path)
    
        import TextReader
        import re
        sol = analysis.Solution
        pyCodeEvent = sol.AddPythonCodeEventBased()
        pyCodeEvent.TargetCallback = PythonCodeTargetCallback.OnAfterPost
    
        pictureCreatorFile = currentPath + "\\MechPyRes\\PySCFPictureCreator.py"
        pictureCreatorContent = TextReader.read_text_file(pictureCreatorFile)
    
        new_path_out = str(currentPath + "\\Results\\SCF_Overview_Outside.png")
    
    
        pictureCreatorContent = re.sub(r'fPathOut\s*=\s*".*?"', 'fPathOut = r\'{}\''.format(new_path_out), pictureCreatorContent)
    
        #Here the py script from below is added the the python event base code
        pyCodeEvent.Text = pictureCreatorContent
        pyCodeEvent.Name = "SCF_Picture_Creator"
        pyCodeEvent.PropertyByName("Suppressed").InternalValue = True
    

    This is the python event based code:

    def after_post(this, solution):
        #Do some personal checks for whatever
        if ExtAPI.DataModel.GetObjectsByName('Outside SCF overview'):
            import toolbar
            toolbar.DoUndeformedWireFrameResultView(ExtAPI)
    
            #Disable ruler for output
            Graphics.ViewOptions.ShowRuler = False
    
            #Disable mesh etc.
            Graphics.ViewOptions.ResultPreference.ExtraModelDisplay = MechanicalEnums.Graphics.ExtraModelDisplay.UndeformedWireframe
    
            #Create empty variable for output pathes, will be overwritten
            fPathOut = "C:\\"
            fPathIn = "C:\\"
    
            #Get results from python code
            scf = ExtAPI.DataModel.GetObjectsByName('PyResults')[0]
    
    
            #Set output settings for image
            gset = Ansys.Mechanical.Graphics.GraphicsImageExportSettings()
            gset.CurrentGraphicsDisplay = False
            gset.Width = 750
            gset.Height = 1000
            gset.Background =  GraphicsBackgroundType.White
            gset.Capture = GraphicsCaptureType.ImageAndLegend
            gset.FontMagnification = 0.6
    
            #Active outside SCF, set camera and export image
            scfOut.Activate()
    
            #Find max value to display
            maxValOut = 0.0
            for prop in scfOut.Properties:
                if "SCF Bending FreeEdge" in prop.Name:
                    scf = scfOut.GetCustomPropertyByPath(prop.Name)
                    maxValOut = scf.Value
    
            #Setup legend
            lsOut = Ansys.Mechanical.Graphics.Tools.CurrentLegendSettings().MakeCopy()
            #ls = Ansys.Mechanical.Graphics.Tools.LegendSettings()
            lsUnitOut = lsOut.Unit
            lsOut.SetMinMax(Quantity(1.0, lsUnitOut), Quantity(maxValOut, lsUnitOut))
    
    
            #Set camera orientation
            ExtAPI.Graphics.Camera.SetSpecificViewOrientation(ViewOrientationType.Right)
            ExtAPI.Graphics.Camera.SetFit()
    
            #Export graphics, import graphics to the results and rename 
            Graphics.ExportImage(fPathOut, GraphicsImageExportFormat.PNG, gset)
            imgOut = scfOut.AddImage(fPathOut)
            imgOut.Name = "SCF Overview Outside"
    

    Probably not the best soultion, but it works