General Mechanical

General Mechanical

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

How to automatically export .csv or .xls after solving each design point?

    • Chan-Hua Yeh
      Subscriber

      I have a static structural analysis in Workbench, and I would like Ansys Mechanical to automatically export the tabular data after solving a design point. Does anybody know how to do this?

       

      Problem description:

      Currently, for every design point, I have to right click "set as current", and wait for opening Mechanical, export the data manually, wait for closing Mechanical, which is quite time-consuming and laborious. It usually takes 1 munites to open or close the Mechanical. Here is how I manually export the .xls file now:

      Attempts:

      I have tried to put some Python code in "Python Code" under solution, and successfully export a blank csv file after solution. However, I couldn't find an API to retrieve the result data to put in my csv and I don't know if this way is the right way.

      I am using Workbench 2022 R1 Enterprise.

      Thank you in advance!

    • Avnish Pandey
      Ansys Employee

      Hi Chan,

      To automatically export the tabular data after solving a design point in Ansys Mechanical, you can utilize an Ansys ACT extension. This extension can automate the export of various data during the update process for a Component/System/Project and Design Point. It streamlines the process, especially when dealing with a large number of design points.

      Custom Update V3

      Additionally, you can group many result items into the same table and export them. This option becomes available when you first right-click on "Solution" and choose "Results Summary." Then, right-click on "Solution" again and select "Export"

      -Best

       

    • Erik Kostson
      Ansys Employee

       

       

      Hi

       

      See here for some examples:

      Export to file

      All the best

      Erik

       

       

      • Chan-Hua Yeh
        Subscriber

         

         

         

         

        Thank you, Mr. Pandey, but I am not allowed to install additional extensions at the moment.

        Thank you, Mr. Kostson. Your information did help! Now I can export a text file after solving using “result.ExportToTextFile(export_name+’.txt’)” in the postprocessing section:

        Q1:
        However, the exported txt files for each design point (dp) are the same. 

        For example, if I have 3 design points, it can output 3 txt files after solving (the txt file names are different by adding system time), but all 3 txt files contain the same data, which belongs to the current design point. That is, no matter how many design points it solves, Ansys Mechanical only exports one design point’s data.

        Q2:

        May I ask if the design point parameters can be retrieved in the “Python Code” object?

        Since there are many design points, it would be nice if Mechanical could export results with parameters in the file names. For example:

        ”force_1N_pressure_1Pa_stress.txt” or “force_5N_length_1m_directional deformation.txt”

        I have searched and got this information, which says we can access model settings using Python APIs:

        So I have tried many attributes, but couldn’t find anyone that worked. The closest attribute I’ve found is “GetParameter”, but it still didn’t help.

        Thank you very much for any help!

         

         

         

    • Erik Kostson
      Ansys Employee

      Hi

       

      I have added a comment in my post :

      Export to file

      Just search and look up wbjn and dpn (design point number).

      Erik

    • Chan-Hua Yeh
      Subscriber

      Thank you a lot for editing your post! Now I can add the “dpn” in the file names!

      There is only one thing: the content of each txt file is yet still the same as the current design point, not from each design point’s data.

      For example, there are four design points, and after one of them is updated, a txt file would be generated. There are four txt files with “dpn” in their file names in the end, but the data in each file is exactly the same as the current one (DP 1).

      If I change the current design point to DP2, all four txt files would contain the same data as DP2.

      Here is my code. I have added “Tree.Activate(result)” as you suggested in your post, hoping that Ansys refreshes results before exporting txt files, but it seems it’s not working. Do you know how to deal with this issue? And may I ask if “Python Code” is the correct place to put my code for automation?

      import csv, os
      import tempfile
      import mech_dpf, wbjn
      import Ans.DataProcessing as dpf
      fres = []
      fres.Add('Ansys.ACT.Automation.Mechanical.Results.DeformationResults.DirectionalDeformation')

      analysis = ExtAPI.DataModel.Project.Model.Analyses[0]
      solution = analysis.Solution
      all_results = solution.Children                                                                                                        
      dpn = wbjn.ExecuteCommand(ExtAPI,"returnValue(a+Parameters.GetActiveDesignPoint().Name)",a="DP")

      target_folder = r'test'                                                                                          
      for result in all_results:
          restype = str(result.GetType())
          if restype==fres[0] and result.Name == "Directional Deformation 2":
              Tree.Activate(result)
              export_name = os.path.join(target_folder, result.Name)
              export_name = export_name + "-" + str(dpn) + "-" + str(round(os.times()[1],3))
              result.ExportToTextFile(export_name+'.txt')
    • Erik Kostson
      Ansys Employee

       

      Hi

      See the post below (see all steps, e.g, after post callback, and especially the last part in that post).

      https://developer.ansys.com/blog/script-tip-friday-python-code-object-mechanical

      All the best

      Erik

       

    • Chan-Hua Yeh
      Subscriber

      It works! Thank you very much!

      My fault. I didn't notice that "callback" is a terminology.

      Your help will save me a lot of time!

       

      Summary:

      1. What's the goal? 

      Exporting raw data after solving each design point

      2. Where is the information about this?

      Export to file, callback after post

      3. Where to put the code for automation?

      "Python Code" under "Solution", not anywhere else (ex. not "Mechanical Scripting" nor "Python Result")

      In Mechanical, right-click on "Solution"->insert->"Python Code"

      In Details of "Python Code", select "After Post" in "Target Callback"->right-click on "Python Code"->connect

      Now the environment is ready.

      4. What is the code?

      def after_post(this, solution):# Do not edit this line

        """
        Called after post processing.
        Keyword Arguments : 
            this -- the datamodel object instance of the python code object you are currently editing in the tree
            solution -- Solution
          """


        # To access properties created using the Property Provider, please use the following command.
        # this.GetCustomPropertyByPath("your_property_group_name/your_property_name")
        # To access scoping properties use the following to access geometry scoping and named selection respectively:
        # this.GetCustomPropertyByPath("your_property_group_name/your_property_name/Geometry Selection")
          # this.GetCustomPropertyByPath("your_property_group_name/your_property_name/Named Selection")

        import csv, os
        import tempfile
        import mech_dpf, wbjn
        import Ans.DataProcessing as dpf

        fres = []
          fres.Add('Ansys.ACT.Automation.Mechanical.Results.DeformationResults.DirectionalDeformation')

        analysis = ExtAPI.DataModel.Project.Model.Analyses[0]
        solution = analysis.Solution
        all_results = solution.Children                                                                                                   
        dpn = wbjn.ExecuteCommand(ExtAPI,"returnValue(a+Parameters.GetActiveDesignPoint().Name)",a="DP")
        
        # write results to txt file         
      target_folder = r'test'   
        for result in all_results:
            restype = str(result.GetType())
            if restype==fres[0] and result.Name == "Directional Deformation 2":
                Tree.Activate(result)
                export_name = os.path.join(target_folder, result.Name)
                export_name = export_name + "-" + str(dpn) + "-" + str(round(os.times()[1],3)) #include dp number and time in the file names
                print(export_name)
                  result.ExportToTextFile(export_name+'.txt')

          pass

       

       

    • Erik Kostson
      Ansys Employee

      That is great - many thanks for your post - as this might be useful to others we are closing it.

      All the best

      Erik

Viewing 7 reply threads
  • The topic ‘How to automatically export .csv or .xls after solving each design point?’ is closed to new replies.