We have a named selection called here 'test_ns', that has either 2 nodes or 2 vertices. How can we then calculate the distance between 2 nodes or vertices using scripting?
#Below is one way of doing this: myNS = ExtAPI.DataModel.GetObjectsByName("test_ns")[0] #((we need 2 nodes or vertices in NS only)) allNodeIds = [] geomIds = myNS.Location.Ids Mesh = ExtAPI.DataModel.MeshDataByName("Global") n=[] veryes=0 nodyes=0 if myNS.Location.SelectionType == SelectionTypeEnum.GeometryEntities: veryes=1 for geomId in geomIds: #print('vertex scoping') mR = Mesh.MeshRegionById(geomId) n.append(mR) nodeIds = mR.NodeIds allNodeIds.extend(nodeIds) else: #print('node scoping') nodyes=1 MeshData=ExtAPI.DataModel.MeshDataByName("Global") allNodeIds = myNS.Location.Ids for node in allNodeIds: n.append(MeshData.NodeById(node)) #calculate distance for 2 nodes only if len(allNodeIds)==2 and veryes==1: print('calculate distance:') n1x=n[0].Nodes[0].X n1y=n[0].Nodes[0].Y n1z=n[0].Nodes[0].Z n2x=n[1].Nodes[0].X n2y=n[1].Nodes[0].Y n2z=n[1].Nodes[0].Z distc=sqrt((n2x-n1x)**2+(n2y-n1y)**2+(n2z-n1z)**2) print(distc) elif len(allNodeIds)==2 and nodyes==1: print('calculate distance:') n1x=n[0].X n1y=n[0].Y n1z=n[0].Z n2x=n[1].X n2y=n[1].Y n2z=n[1].Z distc=sqrt((n2x-n1x)**2+(n2y-n1y)**2+(n2z-n1z)**2) print(distc)