How to get the total length of an edge (from mesh) in Mechanical via Scripting?

Member, Moderator, Employee Posts: 248
50 Answers 100 Comments Second Anniversary 25 Likes
✭✭✭✭

I would like to extract length of an edge. Not from Geometry but from Mesh, as geometric length and length based on summing up individual element edge lengths is different.

Best Answer

  • Member, Moderator, Employee Posts: 248
    50 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭
    Answer ✓

    Please use the below script to extract the total length of all element edges corresponding to edges in a selected named selection

    1. import math
    2.  
    3. ################################################################
    4. #Input
    5. # Provide the name of the Edge Named Selection
    6. EdgeNamedSelection = DataModel.GetObjectsByName('Selection')[0]
    7. ################################################################
    8.  
    9. #####################################################
    10. # Distance calculation function
    11. def CalculateDistance(i,j):
    12. Nodei_xyz = MeshDetails.NodeXYZById(i)
    13. Nodej_xyz = MeshDetails.NodeXYZById(j)
    14. dx = Nodei_xyz[0]-Nodej_xyz[0]
    15. dy = Nodei_xyz[1]-Nodej_xyz[1]
    16. dz = Nodei_xyz[2]-Nodej_xyz[2]
    17. d = dx **2 + dy ** 2 + dz**2
    18. return math.sqrt(d)*1000
    19. #####################################################
    20.  
    21.  
    22. MeshDetails=DataModel.MeshDataByName(DataModel.MeshDataNames[0])
    23.  
    24. EdgeNodes=[]
    25. for edge in EdgeNamedSelection.Location:
    26. EdgeNodes.extend(MeshDetails.MeshRegionById(edge).NodeIds)
    27.  
    28. EdgeElements = MeshDetails.ElementIdsFromNodeIds(EdgeNodes)
    29. EdgeElements = sorted(EdgeElements)
    30.  
    31. EdgeLength = 0
    32. DistancePairs = []
    33. for E in EdgeElements:
    34. print('')
    35. print('')
    36. ElementNodes = sorted(MeshDetails.ElementById(E).NodeIds)
    37. NodesOnEdge = [N for N in ElementNodes if N in EdgeNodes]
    38. if NodesOnEdge in DistancePairs:
    39. # It means that this distance was calculated before ..
    40. continue
    41. if len(NodesOnEdge)==3:
    42. print('ELEMENT :',E)
    43. Node1 = NodesOnEdge[0]
    44. Node2 = NodesOnEdge[1]
    45. Node3= NodesOnEdge[2]
    46. D = [[CalculateDistance(Node1,Node2),Node1,Node2]]
    47. D += [[CalculateDistance(Node1,Node3),Node1,Node3]]
    48. D += [[CalculateDistance(Node2,Node3),Node2,Node3]]
    49. D = sorted(D)
    50. print(D)
    51. print(D[0][0]+D[1][0])
    52. EdgeLength +=D[0][0]+D[1][0]
    53. print(' .. Distance from',D[0][1],'to',D[0][2],'is',D[0][0])
    54. print(' .. Distance from',D[1][1],'to',D[1][2],'is',D[1][0])
    55. print('.. Total edge distance :',EdgeLength)
    56. elif len(NodesOnEdge)==2:
    57. print('ELEMENT :',E)
    58. Node1 = NodesOnEdge[0]
    59. Node2 = NodesOnEdge[1]
    60. D = CalculateDistance(Node1,Node2)
    61. EdgeLength +=D
    62. print(' .. Distance from',Node1,'to',Node2,'is',D)
    63. else:
    64. continue
    65. DistancePairs +=[NodesOnEdge]
    66.  
    67. print('TOTAL EDGE LENGTH :',EdgeLength)

Welcome!

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