Creating Probe Principal Stresses from a Node Selection

Goal

The following script inserts a Probe for Principal Stresses and specifies a node for solution.

Code

# Retrieve principal stresses from a selection of nodes and display in a message box

import math
import units
import mech_dpf
import Ans.DataProcessing as dpf

clr.AddReference("Ans.UI.Toolkit.Base")
clr.AddReference("Ans.UI.Toolkit")
from Ansys.UI.Toolkit import *

curSel=ExtAPI.SelectionManager.CurrentSelection

# Need to have at least one node selected, otherwise pass"
if (curSel.Ids.Count==0):
    mess_line1 = 'Please select at least one node'
    MessageBox.Show(mess_line1)

else: # assuming nodes are selected

    meshData=ExtAPI.DataModel.Project.Model.Analyses[0].MeshData
    
    scoping=dpf.Scoping()
    scoping.Ids = curSel.Ids
    scoping.Location=dpf.enums.location.nodal
    
    
    an1=ExtAPI.DataModel.AnalysisByName(ExtAPI.DataModel.AnalysisNames[0])
    rstFile=an1.WorkingDir+'file.rst'
    
    dataSource = dpf.DataSources(rstFile)
    
    stressOp1 = dpf.operators.result.stress_principal_1()
    stressOp2 = dpf.operators.result.stress_principal_2()
    stressOp3 = dpf.operators.result.stress_principal_3()
    
    stressOp1.inputs.data_sources.Connect(dataSource)
    stressOp2.inputs.data_sources.Connect(dataSource)
    stressOp3.inputs.data_sources.Connect(dataSource)
    
    stressOp1.inputs.mesh_scoping.Connect(scoping)
    stressOp2.inputs.mesh_scoping.Connect(scoping)
    stressOp3.inputs.mesh_scoping.Connect(scoping)
    
    stressData1=stressOp1.outputs.fields_container.GetData()[0]
    stressData2=stressOp2.outputs.fields_container.GetData()[0]
    stressData3=stressOp3.outputs.fields_container.GetData()[0]
    
    stressData=[]
    
    for nid in curSel.Ids:
        stressData.append([nid,stressData1.GetEntityDataById(nid)[0],stressData2.GetEntityDataById(nid)[0],stressData3.GetEntityDataById(nid)[0]])
    
    # Display result
    mess_line='Node\tS1\tS2\tS3\n'
    for sd in stressData:
        mess_line+=str(sd[0])+'\t'+str(round(sd[1],2))+'\t'+str(round(sd[2],2))+'\t'+str(round(sd[3],2))+'\n'
    MessageBox.Show(mess_line)