How can I get a user button to create a Python results with its script and properties?

Pierre Thieffry
Pierre Thieffry Member, Moderator, Employee Posts: 95
First Anniversary Ansys Employee Solution Developer Community of Practice Member Photogenic
edited June 2023 in Structures

A power user has created a Python result and would like to share it with other people. How can he do this?

Tagged:

Answers

  • Pierre Thieffry
    Pierre Thieffry Member, Moderator, Employee Posts: 95
    First Anniversary Ansys Employee Solution Developer Community of Practice Member Photogenic
    Answer ✓

    Well, here's one potential solution, as there is no "export button" or "generate user button" option.

    First run this script that will store the Python results script and property provider:

    pr=ExtAPI.DataModel.GetObjectsByName('Python Result')[0]
    
    script=pr.Text 
    provider_text=pr.PropertyProviderText
    
    f=open(r'wherever\result_script.txt','w') 
    f.write(script)
    f.write('\n***** delimiter\n') # use whatever delimiter 
    f.write(provider_text) 
    f.close()
    

    Then use this script to create the user button:

    current_object=ExtAPI.DataModel.Tree.FirstActiveObject
    
    if current_object.GetType()==Ansys.ACT.Automation.Mechanical.Solution: # user has selected a 'solution' under which the python result will be inserted
        new_presult=current_object.AddPythonResult()
        f=open(r'wherever\result_script.txt','r') # script content stored somewhere
        script=''
        provider_text=''
        line=f.readline()
        # read script text
        while line.find('***** delimiter')==-1: # same delimiter as above 
            script+=line
            line=f.readline()
        line=f.readline()
        # read provider text
        while line:
            provider_text+=line
            line=f.readline()
            
        
        f.close()
        new_presult.PropertyProviderText=provider_text
        new_presult.Text=script
        new_presult.Connect()
        new_presult.ReloadProperties()