Exporting nodal locations and results with a python object

Options
M
M Member, Employee Posts: 230
First Comment First Anniversary Ansys Employee Solution Developer Community of Practice Member

Written for Ansys v23r1

Check that in Workbench - Tools - Options - Mechanical - Connect/Run Python Code Objects When Mechanical is Launched is checked (on).

Then in Mechanical - File - Options - Export - Text File Export - make sure Enclude Node Numbers is set to Yes **and **Include Node Location is set to yes.

Then in your Mechanical instance insert a Python Code Object in the Solution branch (Right click - Insert - Python Code Object) and set the Target Callback to After Post (after the results have been generated).

! Make sure to change the path to a directory (save_directory = ) where you want to save results. Or you can use the wbjn module to find the project directory.
! set the named selection to a **nodal **named selection in your model.

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 os, wbjn

    dp=wbjn.ExecuteCommand(ExtAPI,"returnValue(Parameters.GetCurrentDesignPoint().DisplayText)")

    named_selection_name = 'mirror1'
    result_name = 'Total Deformation' + '_' + named_selection_name
    save_directory = r'D:\\temp\b'

    node_file_name = named_selection_name + '_nodes.txt'
    result_save_name = dp + '_' + result_name + '.txt'

    named_selection = ExtAPI.DataModel.GetObjectsByName(named_selection_name)[0]
    named_selection.ExportToTextFile(os.path.join(save_directory,node_file_name))

    results = ExtAPI.DataModel.GetObjectsByType(DataModelObjectCategory.Result)
    result_names = [i.Name for i in results]

    if result_name not in result_names:
        result = ExtAPI.DataModel.Project.Model.Analyses[0].Solution.AddTotalDeformation()
        scope = ExtAPI.DataModel.GetObjectsByName(named_selection_name)[0]
        result.Name = result_name
        result.Location = scope
        # set time etc for your specific result here
        result.EvaluateAllResults()
    result = ExtAPI.DataModel.GetObjectsByName(result_name)[0]
    result.ExportToTextFile(os.path.join(save_directory,result_save_name))

    pass