How to get the total length of an edge (from mesh) in Mechanical via Scripting?
Rohith Patchigolla
Member, Moderator, Employee Posts: 206
✭✭✭✭
in Structures
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.
Tagged:
0
Best Answer
-
Please use the below script to extract the total length of all element edges corresponding to edges in a selected named selection
import math ################################################################ #Input # Provide the name of the Edge Named Selection EdgeNamedSelection = DataModel.GetObjectsByName('Selection')[0] ################################################################ ##################################################### # Distance calculation function def CalculateDistance(i,j): Nodei_xyz = MeshDetails.NodeXYZById(i) Nodej_xyz = MeshDetails.NodeXYZById(j) dx = Nodei_xyz[0]-Nodej_xyz[0] dy = Nodei_xyz[1]-Nodej_xyz[1] dz = Nodei_xyz[2]-Nodej_xyz[2] d = dx **2 + dy ** 2 + dz**2 return math.sqrt(d)*1000 ##################################################### MeshDetails=DataModel.MeshDataByName(DataModel.MeshDataNames[0]) EdgeNodes=[] for edge in EdgeNamedSelection.Location: EdgeNodes.extend(MeshDetails.MeshRegionById(edge).NodeIds) EdgeElements = MeshDetails.ElementIdsFromNodeIds(EdgeNodes) EdgeElements = sorted(EdgeElements) EdgeLength = 0 DistancePairs = [] for E in EdgeElements: print('') print('') ElementNodes = sorted(MeshDetails.ElementById(E).NodeIds) NodesOnEdge = [N for N in ElementNodes if N in EdgeNodes] if NodesOnEdge in DistancePairs: # It means that this distance was calculated before .. continue if len(NodesOnEdge)==3: print('ELEMENT :',E) Node1 = NodesOnEdge[0] Node2 = NodesOnEdge[1] Node3= NodesOnEdge[2] D = [[CalculateDistance(Node1,Node2),Node1,Node2]] D += [[CalculateDistance(Node1,Node3),Node1,Node3]] D += [[CalculateDistance(Node2,Node3),Node2,Node3]] D = sorted(D) print(D) print(D[0][0]+D[1][0]) EdgeLength +=D[0][0]+D[1][0] print(' .. Distance from',D[0][1],'to',D[0][2],'is',D[0][0]) print(' .. Distance from',D[1][1],'to',D[1][2],'is',D[1][0]) print('.. Total edge distance :',EdgeLength) elif len(NodesOnEdge)==2: print('ELEMENT :',E) Node1 = NodesOnEdge[0] Node2 = NodesOnEdge[1] D = CalculateDistance(Node1,Node2) EdgeLength +=D print(' .. Distance from',Node1,'to',Node2,'is',D) else: continue DistancePairs +=[NodesOnEdge] print('TOTAL EDGE LENGTH :',EdgeLength)
1