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 csv
import ansys
import wbjn
import units
############################ Node Number Input!##############################
nodeNumber = 15686
############################ 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)
mesh = ExtAPI.DataModel.MeshDataByName("Global")
reader = analysis.GetResultsData()
disp = reader.GetResult("U")
apdl_unit = disp.GetComponentInfo("X").Unit
reader.Dispose()
toUnit = units.GetCurrentCompactUnitString("Length")
conv_fact= units.ConvertUnit(1,apdl_unit,toUnit)
Graphics.Scene.Clear()
try: Graphics.LabelManager.DeleteLabels(label)
except: pass
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("(F,';',F,';',F)\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") as resCoords: coordList=list(csv.reader(resCoords, delimiter=';'))[0]
coordinates = [float(index) for index in coordList]
xFromAPDL = coordinates[0]*conv_fact
yFromAPDL = coordinates[1]*conv_fact
zFromAPDL = coordinates[2]*conv_fact
print ("Location of Node = (" + str(xFromAPDL) + ", " + str(yFromAPDL) + ", " + str(zFromAPDL) + ") " + str(toUnit))
fromUnit = units.GetCurrentCompactUnitString("Length")
conv_fact_graphics= units.ConvertUnit(1,fromUnit,"m")
x_graphics = xFromAPDL*conv_fact_graphics
y_graphics = yFromAPDL*conv_fact_graphics
z_graphics = zFromAPDL*conv_fact_graphics
print ("Graphical location of Node = (" + str(x_graphics) + ", " + str(y_graphics) + ", " + str(z_graphics) + ") m")
point = Graphics.CreateWorldPoint(xFromAPDL,yFromAPDL,zFromAPDL)
p = Point([x_graphics,y_graphics,z_graphics],"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)