How can I insert an image or a figure in the Mechanical Tree, through ACT?

Options
Pernelle Marone-Hitz
Pernelle Marone-Hitz Member, Moderator, Employee Posts: 813
First Comment First Anniversary Ansys Employee Solution Developer Community of Practice Member
edited June 2023 in Structures

How can I insert an image or a figure in the Mechanical Tree, through ACT ?

Answers

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 813
    First Comment First Anniversary Ansys Employee Solution Developer Community of Practice Member
    Answer ✓
    Options

    To create a figure object (that can be further manipulated, rotated for example), one can use AddFigure() method, for example :

        ExtAPI.DataModel.Tree.ActiveObjects[0].AddFigure()
    

    There is not similar function to create an image, but one can use the following workaround :

    1. Create a screenshot through ExportScreenToImage() method
    2. Insert this image in the Mechanical tree through AddImage() method.

    Here is a script example :

        ImgPath= r"E:\test.png"
        ExtAPI.Graphics.ExportScreenToImage(ImgPath)  
        IntObj = ExtAPI.DataModel.Tree.ActiveObjects[0].InternalObject
        Img = IntObj.AddImage()
        Img.SourceType = 1
        Img.ImagePath = ImgPath
    
  • M
    M Member, Employee Posts: 230
    First Comment First Anniversary Ansys Employee Solution Developer Community of Practice Member
    Answer ✓
    Options

    You can also create and add a figure to the tree (solution) from a Python Code (object) if you use the Cpython engine. You can create a Python Code object and set it to after Post, set the Engine Type to Cpython and paste the script below to generate a figure in the tree.

    def after_post(this, solution):# Do not edit this line
    
        import matplotlib.pyplot as plt
        import os
        
        plt.plot([1,2,3,4,5],[5,10,15,20,25],color='red')
        plt.ylabel('hello [unit]')
        plt.xlabel('kitty [unit]')
        plt.grid()
        wd = solution.Parent.WorkingDir
        myPath = os.path.join(wd,'myFig.png')
        plt.savefig(myPath)
        image = solution.AddImage(myPath)
    
        pass
    
    
    

    enter image description here