Example of resolving UIThread error message - take screenshot in Mechanical

Pernelle Marone-Hitz
Pernelle Marone-Hitz Member, Moderator, Employee Posts: 871
100 Answers 500 Comments 250 Likes First Anniversary
✭✭✭✭
edited June 2023 in Structures

Do we have an example of resolving a UIThread error message for Mechanical scripting?

Tagged:

Answers

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

    Below is an example. If ExtAPI.Graphics.ExportImage() is called directly from OnButtonClickScreenshot function, the screenshot is not taken and the UIThread error message is returned. To resolve this, ExtAPI.Graphics.ExportImage() is called in another function, thanks to ExtAPI.Application.InvokeUIThread().

        def OnButtonClickScreenshot(s, e):
            '''
            Action when button is clicked (need to separate process in UIThread)
            '''        
            
            imagePath = GetUserFileFolder()
            imageName = "DummyImage" + str(imageNumber)
            ExtAPI.Application.InvokeUIThread(TakeScreenshot, [imagePath, imageName])
                 
                 
        def TakeScreenshot(args):
            '''
            Take screenshot of object
            '''
            imagePath = args[0]
            imageName = args[1]
            setting2d = Ansys.Mechanical.Graphics.GraphicsImageExportSettings()
            setting2d.Resolution = GraphicsResolutionType.HighResolution
            setting2d.Width = 838
            setting2d.Height = 392
            setting2d.Background = GraphicsBackgroundType.White
            setting2d.CurrentGraphicsDisplay = False
            totalPath = os.path.join(imagePath, imageName) + '.png'
            ExtAPI.Graphics.ExportImage(totalPath, GraphicsImageExportFormat.PNG,
                                        setting2d)