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

Member, Moderator, Employee Posts: 884
100 Answers 500 Comments 250 Likes Second Anniversary
✭✭✭✭
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

  • Member, Moderator, Employee Posts: 884
    100 Answers 500 Comments 250 Likes Second Anniversary
    ✭✭✭✭

    Below is an example script that will do exactly that:

    1. # Import necessary modules
    2. import os
    3. from os.path import exists
    4. import wbjn
    5. import clr
    6. clr.AddReference("Microsoft.Office.Interop.Excel") 
    7. import Microsoft.Office.Interop.Excel as Excel
    8. from System import Array
    9.  
    10.  
    11. # Open Excel instance
    12. excel = Excel.ApplicationClass()
    13. excel.Visible = True
    14.  
    15.  
    16. # Open pre-existing Excel file 
    17. user_files = wbjn.ExecuteCommand(ExtAPI, "returnValue(GetUserFilesDirectory())")
    18. filename = os.path.join(user_files,'LookUpTable3D.csv')
    19. workbook = excel.Workbooks.Open(filename)
    20. worksheet = workbook.Worksheets("LookUpTable3D")
    21.  
    22.  
    23. # Clear existing data
    24. worksheet.Range("A14:D10000").ClearContents()
    25.  
    26.  
    27. # Get list of named selection which name contains "nodalns_fvf"
    28. ns_fiberVolumeFraction = [ns for ns in ExtAPI.DataModel.GetObjectsByType(DataModelObjectCategory.NamedSelection) if 'nodalns_fvf' in ns.Name]
    29.  
    30.  
    31. # Extract data (from ns name and nodes in ns) 
    32. mesh_data = ExtAPI.DataModel.MeshDataByName('Global')
    33. X_pos = []
    34. Y_pos = []
    35. Z_pos = []
    36. fiber_vf = []
    37. for ns in ns_fiberVolumeFraction:
    38.     for node in ns.Location:
    39.         X_pos.append(mesh_data.NodeById(node).X)
    40.         Y_pos.append(mesh_data.NodeById(node).Y)
    41.         Z_pos.append(mesh_data.NodeById(node).Z)
    42.         fiber_vf.append("0."+ ns.Name.Split('_')[-1])
    43.  
    44.  
    45. # Write data to Excel file
    46. len_data = len(X_pos)
    47. range_data = "A14:D"+str(len_data)
    48. xlrange = worksheet.Range[range_data]
    49. data = Array.CreateInstance(object,len_data, 4) 
    50.  
    51.  
    52. for row in range(len_data): 
    53.     for column in range(4): 
    54.         data[row, 0] = X_pos[row] 
    55.         data[row, 1] = Y_pos[row]
    56.         data[row, 2] = Z_pos[row]
    57.         data[row, 3] = fiber_vf[row]
    58.  
    59.  
    60. xlrange.Value2 = data
    61.  
    62.  
    63. # Save file and exit
    64. workbook.Save()
    65. workbook.Close(True)
    66. 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:


  • Member, Moderator, Employee Posts: 884
    100 Answers 500 Comments 250 Likes Second Anniversary
    ✭✭✭✭

Welcome!

It looks like you're new here. Sign in or register to get started.