How can we export results to text file and then combine all files into one file

Erik Kostson
Erik Kostson Member, Moderator, Employee Posts: 331
50 Answers 100 Comments Second Anniversary 25 Likes
✭✭✭✭
edited April 16 in Structures

How can we export results to a text file and then combine all files into one file automatically?

Tagged:

Answers

  • Erik Kostson
    Erik Kostson Member, Moderator, Employee Posts: 331
    50 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭
    edited April 16 Answer ✓

    Similar question has been asked in the Ansys learning forum, and the answer to this is:

    global files
    files=[]
    anls=ExtAPI.DataModel.Project.Model.Analyses
    
    maxnls=len(anls)
    
    for j in range(0,maxnls):
        res=anls[j].Solution.Children
        maxres=len(res)
        for i in range(1,maxres):
            res[i].ExportToTextFile(bool,'''D:\'''+str(res[i].Name)+'''_nls_'''+str(j)+'''_'''+str(i)+'''.txt''')
            files.append('''D:\'''+str(res[i].Name)+'''_nls_'''+str(j)+'''_'''+str(i)+'''.txt''')
    
    
    with open("D:\output_file.txt", "w") as outfile:
        for filename in files:
            with open(filename) as infile:
                contents = infile.read()
                outfile.write(contents)
    

    ANd:

    import os
    global files
    files=[]
    anls=ExtAPI.DataModel.Project.Model.Analyses
    
    maxnls=len(anls)
    
    for j in range(0,maxnls):
        res=anls[j].Solution.Children
        maxres=len(res)
        for myres in res:
            if str(myres.GetType())=='Ansys.ACT.Automation.Mechanical.Results.UserDefinedResult':
                print(myres.Name)
                myres.Activate()
                dir = "D:/"
                path = os.path.join(dir,str(myres.Name.Split('-')[0])+".txt")#change split and file type to .xls if needed
                myres.ExportToTextFile(path)
                files.append("D:/"+str(myres.Name.Split('-')[0])+".txt")
    with open("D:/output_file.txt", "w") as outfile:
        for filename in files:
            with open(filename) as infile:
                contents = infile.read()
                outfile.write(contents)