Mechanical Python Code example - write nodal information to .csv file

Pernelle Marone-Hitz
Pernelle Marone-Hitz Member, Moderator, Employee Posts: 732
First Comment First Anniversary Ansys Employee Solution Developer Community of Practice Member
edited June 2023 in Structures

I have a model where several nodal named selections are defined:

etc. These named selection are named with the format "nodalns_fvf_XX".

I also have a .csv file, saved in the user_files directory of the project. This .csv file will be used to define a 3D look-up table in ACP. The "XX" value in the named selection's name will define the fiber volume fraction to be used on that node ("XX" in the NS name defines a FVF of 0.XX, ie "nodalns_fvf_50" should define a fiber volume fraction of 0.50 for all nodes in this named selection).

I want a script that will:

  • loop through all named selections
  • identify the named selections with the specific names (in this case, that their name contains "nodalns_fvf"
  • for each node in these named selection, write to the csv file the (X,Y,Z) position of the node, and the fiber volume fraction to be used on that node.


Answers

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 732
    First Comment First Anniversary Ansys Employee Solution Developer Community of Practice Member

    Below is an example script that will do exactly that:

    # Import necessary modules
    import os
    from os.path import exists
    import wbjn
    import clr
    clr.AddReference("Microsoft.Office.Interop.Excel") 
    import Microsoft.Office.Interop.Excel as Excel
    from System import Array
    
    
    # Open Excel instance
    excel = Excel.ApplicationClass()
    excel.Visible = True
    
    
    # Open pre-existing Excel file 
    user_files = wbjn.ExecuteCommand(ExtAPI, "returnValue(GetUserFilesDirectory())")
    filename = os.path.join(user_files,'LookUpTable3D.csv')
    workbook = excel.Workbooks.Open(filename)
    worksheet = workbook.Worksheets("LookUpTable3D")
    
    
    # Clear existing data
    worksheet.Range("A14:D10000").ClearContents()
    
    
    # Get list of named selection which name contains "nodalns_fvf"
    ns_fiberVolumeFraction = [ns for ns in ExtAPI.DataModel.GetObjectsByType(DataModelObjectCategory.NamedSelection) if 'nodalns_fvf' in ns.Name]
    
    
    # Extract data (from ns name and nodes in ns) 
    mesh_data = ExtAPI.DataModel.MeshDataByName('Global')
    X_pos = []
    Y_pos = []
    Z_pos = []
    fiber_vf = []
    for ns in ns_fiberVolumeFraction:
        for node in ns.Location:
            X_pos.append(mesh_data.NodeById(node).X)
            Y_pos.append(mesh_data.NodeById(node).Y)
            Z_pos.append(mesh_data.NodeById(node).Z)
            fiber_vf.append("0."+ ns.Name.Split('_')[-1])
    
    
    # Write data to Excel file
    len_data = len(X_pos)
    range_data = "A14:D"+str(len_data)
    xlrange = worksheet.Range[range_data]
    data = Array.CreateInstance(object,len_data, 4) 
    
    
    for row in range(len_data): 
        for column in range(4): 
            data[row, 0] = X_pos[row] 
            data[row, 1] = Y_pos[row]
            data[row, 2] = Z_pos[row]
            data[row, 3] = fiber_vf[row]
    
    
    xlrange.Value2 = data
    
    
    # Save file and exit
    workbook.Save()
    workbook.Close(True)
    excel.Quit()
    

    For the script to be executed each time the mesh is generated, simply place it in a Python Code object and use the "After Mesh Generated" callback:

    The returned .csv file is as follows:


  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 732
    First Comment First Anniversary Ansys Employee Solution Developer Community of Practice Member