General Mechanical

General Mechanical

Topics related to Mechanical Enterprise, Motion, Additive Print and more.

Python-Script to Export all Children of a Solution Tree

    • FS
      Subscriber

      I have a transient thermal model which solves at a number of times steps, the number may vary from simulation conditions. The results at each time step are stored in a solution folder.

      For postprocessing I want to export the (in this case) temperature at each time step. I have created the following script. However it requires to enter the number of children manually. How can I determine the number of children, such as to remain the script untouched independent of the solution. BTW:

      r1.Children 

      does not work.

      Script:

      '''
      Export all children in solution folder 'Temperature'
      '''

      import os

      analysis=ExtAPI.DataModel.Project.Model.Analyses[0]
      locationToExport = analysis.WorkingDir
      # Enter number time steps
      nsteps = 20
      steps = list(range(2,nsteps+2))

      for i in steps:
          resultsName = 'Temperature '+ str(i)
          fileName = resultsName + '.txt'
          fileToExport = os.path.join(locationToExport,fileName)
          r1=ExtAPI.DataModel.GetObjectsByName(resultsName)[0]
          r1.ExportToTextFile(fileToExport)
    • ErKo
      Ansys Employee

       

       

       

       

       

      Hi

      It is because you are selecting on names (Getobjectbyname) and the Temperature 1 or 2 names are likely used by a temperature bc also.

      To resolve this, change the names (temp. bc) to say Mytemp1,…, instead and it all works.

      Or select all temperature results and loop through them and export:made this example: https://discuss.ansys.com/discussion/9888/how-to-export-temperature-results-to-text-files-using-mechanical-scripting

      All the best

      Erik

       

       

       

       

       

    • Rohith Patchigolla
      Ansys Employee

      Hi, 

      If you want to be sure that you are searching by name only items of the type Grouping Folder and only those under Solution of a particular analysis, please try the below script. 

      #Assuming its the first analysis in the tree
      Analysis = DataModel.AnalysisList[0]
      solution = Analysis.Solution
      GroupName = "Temperature"
      myGroup = [child for child in solution.Children if child.Name ==GroupName and child.DataModelObjectCategory == DataModelObjectCategory.TreeGroupingFolder][0]
      allChildren = myGroup.Children
      print(len(allChildren))

       

Viewing 2 reply threads
  • You must be logged in to reply to this topic.