COG (Deformed)
Calculate and create a Comment with COG coordinates + Coordinate Sytem at COG.
Scope to Solution and run the Script!
import os
import ansys
import re
import units
import csv
clr.AddReference("Ans.UI.Toolkit")
clr.AddReference("Ans.UI.Toolkit.Base")
from Ansys.UI.Toolkit import *
return_ = False
if str(Tree.FirstActiveObject.GetType()) != 'Ansys.ACT.Automation.Mechanical.Solution':
MessageBox.Show("Highlight Solution in the tree!")
return_ = True
if not return_:
solution = Tree.FirstActiveObject
inpName = "COG_.inp"
wDir = solution.WorkingDir
newdir = os.path.join(wDir,"COG_CALC")
if not os.path.exists(newdir):
os.mkdir(newdir)
with open(os.path.join(newdir, inpName),'w') as PostInp:
PostInp.write("/BATCH\n")
PostInp.write("/POST1\n")
PostInp.write("file," + str(wDir) + "file,rst \n")
PostInp.write("SET,LAST\n")
PostInp.write("/PREP7 \n")
PostInp.write("ALLSEL\n")
PostInp.write("ESLN\n")
PostInp.write("UPCOORD,1\n")
PostInp.write("/SOLU \n")
PostInp.write("ALLSEL\n")
PostInp.write("ESEL,S,ENAME,,184\n")
PostInp.write("NSLE,U\n")
PostInp.write("D,ALL,ALL \n")
PostInp.write("SOLVE \n")
PostInp.write("*GET,MY_X,ELEM,,MC,X \n")
PostInp.write("*GET,MY_Y,ELEM,,MC,Y \n")
PostInp.write("*GET,MY_Z,ELEM,,MC,Z \n")
PostInp.write("/POST1\n")
PostInp.write("*CFOPEN,COG_FINAL,csv \n")
PostInp.write("*VWRITE,MY_X,MY_Y,MY_Z \n")
PostInp.write("(F,';',F,';',F)\n")
PostInp.write("*CFCLOS \n")
PostInp.write("FINISH \n")
commandline = " -b nolist -i " + inpName + " -o out.lis"
ansys.RunANSYS(ExtAPI,commandline,runDir=newdir,exelocation=None,minimized=True,hidden=True,unlockLicense=True)
Tree.Refresh()
with open(str(newdir) + "\COG_FINAL.csv") as csv_file:
reader = csv.reader(csv_file, delimiter=';')
cog_line = list(reader)[0]
analysis = solution.Parent
reader = analysis.GetResultsData()
disp = reader.GetResult("U")
unit_disp = disp.GetComponentInfo("X").Unit
reader.Dispose()
toUnit = units.GetCurrentCompactUnitString("Length")
conv_fact= units.ConvertUnit(1,unit_disp,toUnit)
X = float(cog_line[0])*conv_fact
Y = float(cog_line[1])*conv_fact
Z = float(cog_line[2])*conv_fact
deformed_cog = Model.CoordinateSystems.AddCoordinateSystem()
deformed_cog.OriginX = Quantity(X,toUnit)
deformed_cog.OriginY = Quantity(Y,toUnit)
deformed_cog.OriginZ = Quantity(Z,toUnit)
deformed_cog.Name = "Deformed COG Coordinate System"
ExtAPI.Log.WriteMessage("COG_X= " + str(X))
ExtAPI.Log.WriteMessage("COG_Y= " + str(Y))
ExtAPI.Log.WriteMessage("COG_Z= " + str(Z))
comment = Model.Analyses[0].Solution.AddComment()
commentText = "COG X = " + str(X) + ' [' + toUnit + ']' + "\n"
commentText += "COG Y = " + str(Y) + ' [' + toUnit + ']' + "\n"
commentText += "COG Z = " + str(Z) + ' [' + toUnit + ']' + "\n"
comment.Text = commentText
comment.Name = "Deformed COG X,Y,Z"
ExtAPI.Graphics.ViewOptions.ShowCoordinateSystems = True