Find Node
This code takes a node number as user input, e.g.”nodeNumber = 47885”. Then the node is located using APDL (in batch) and the position is plotted and labeled in the Mechanical GUI. In the example figure below, node 47885 is a remote point! Use Clear Graphics to clear the graphics and Graphics.LabelManager.DeleteLabels(label) to delete the label.
import ansys
import wbjn
import re
############################ Node Number Input!##############################
nodeNumber = 47885
############################ COLOR SETTINGS
labelColor = "Red" # Green, Red, Blue
nodeColor = "Blue" # Green, Red, Blue
############################ COLOR SETTINGS
messages = ExtAPI.Application.Messages
for message in messages:
if 'A solver pivot' in message.DisplayString:
try: list_nodenumbers = re.findall(r"[-+]?(?:\d*\.*\d+)", str(message.DisplayString))
except: print "No node found in message " + str(message)
elif 'Element' in message.DisplayString and 'located in Body' in message.DisplayString:
try: list_nodenumbers = re.findall(r"[-+]?(?:\d*\.*\d+)", str(message.DisplayString))
except: print "No node found in message " + str(message)
runAPDL = False
mesh = Model.Analyses[0].MeshData
try: node =mesh.NodeById(nodeNumber)
except: runAPDL = True
analysisSettings = Model.Analyses[0].AnalysisSettings
unitsUsed = str(analysisSettings.SolverUnitSystem)
if unitsUsed == "ConsistentMKS": conv = 1
elif unitsUsed == "ConsistentNMM": conv = 1e-3
elif unitsUsed == "ConsistentMKS": conv = 1
Graphics.Scene.Clear()
try: Graphics.LabelManager.DeleteLabels(label)
except: pass
if runAPDL:
cmd = 'returnValue(GetUserFilesDirectory())'
UserFilesPath = wbjn.ExecuteCommand(ExtAPI,cmd)
wDir = Model.Analyses[0].WorkingDir
inputFile = UserFilesPath + "\\apdlInput.inp"
output_coordinates = UserFilesPath +"\\nodeCoordOutput"
with open(inputFile, 'w') as apdlinp:
apdlinp.write("/POST1\n")
apdlinp.write("SET,FIRST\n")
apdlinp.write("*GET,my_xloc,NODE," + str(nodeNumber) + ",LOC,X\n")
apdlinp.write("*GET,my_yloc,NODE," + str(nodeNumber) + ",LOC,Y\n")
apdlinp.write("*GET,my_zloc,NODE," + str(nodeNumber) + ",LOC,Z\n")
apdlinp.write("ALLSEL\n")
apdlinp.write("*CFOPEN," + output_coordinates + ",csv\n")
apdlinp.write("*VWRITE,my_xloc,my_yloc,my_zloc\n")
apdlinp.write("(F10.0,F10.0,F10.0)\n")
apdlinp.write("\n")
apdlinp.write("*CFCLOS\n")
commandlinestring = " -b nolist -i "+inputFile + " -o "+ "outputfile.out"
ansys.RunANSYS(ExtAPI,commandlinestring, wDir,minimized=True,hidden=True)
with open(output_coordinates+".csv",'r') as resCoords:
coordList=resCoords.readlines()
coordList_new = str(coordList[-1]).strip()
coordList_new = re.findall(r"[-+]?(?:\d*\.*\d+)", coordList_new)
coordinates = [float(index) for index in coordList_new]
conv = 1e-3
xFromAPDL = coordinates[0]*conv
yFromAPDL = coordinates[1]*conv
zFromAPDL = coordinates[2]*conv
point = Graphics.CreateWorldPoint(xFromAPDL,yFromAPDL,zFromAPDL)
p = Point([xFromAPDL,yFromAPDL,zFromAPDL],'m')
else:
point = Graphics.CreateWorldPoint(node.X,node.Y,node.Z)
p = Point([node.X,node.Y,node.Z],'m')
sphere = Graphics.Scene.Factory3D.CreateSphere(.01)
sphere.Transformation3D.Set(point)
if nodeColor == "Green": sphere.Color = 0x00FF00
elif nodeColor == "Red": sphere.Color = 0xFF0000
if nodeColor == "Blue": sphere.Color = 0x0000FF
label = Graphics.LabelManager.CreateLabel(DataModel.GetObjectById(1))
if labelColor == "Green": label.Color = Ansys.ACT.Common.Graphics.Color(0,180,0)
elif labelColor == "Red": label.Color = Ansys.ACT.Common.Graphics.Color(180,0,0)
elif labelColor == "Blue": label.Color = Ansys.ACT.Common.Graphics.Color(0,0,180)
label.Scoping.XYZ = p
label.ShowAlways = True
label.Note = str(nodeNumber)