Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations GregLocock on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Export from ansys to Excel with Python script (HELP) 1

Status
Not open for further replies.

numi

Computer
May 16, 2018
3
Hi there

I have to export some chart (table with force and moment reaction) from Mechanical / Ansys to an excel file with a python script!
I've never done something like this before and found nothing in the "workbench scripting guide".

The main problem I think is to try to get the values from the table and store them in variables in the script. And then put these variables in a function that create an Excel ?

Any sort of help from users who have some experience with this would be greatly appreciated as I struggle on it since a few days.

Many thx
 
Replies continue below

Recommended for you

In Ansys App store you can find an app called "Reaction forces". If you want your own modified version,
you should first go through ACT tutorial course in Customer Portal to learn the basics.

 
This is likely doable in APDL command snippet. Please see a previous thread for some pointers on force extraction.

To write text files to be read by Excel, here's a bit of sample code. You could look up help for these commands:
Code:
*cfopen, rboltf1.txt
*vwrite, rbolt1(1,1), rbolt1(1,2), rbolt1(1,3)
(E,' ',E,' ',E)
*cfclos


Kind regards,
Jason
 
Agree with L_K and sk_cheah.

Ansys Workbench is not script friendly to directly get the script working from Workbench project window. You have to use ACT(Ansys customization toolkit) to have this working. And to write ACT you have to juggle between two languages-APDL and Python/Jscript.

Simple way is to use APDL to get the reactions in file using command snippet.
 
Hi,

First of all thank you all for your help! L_K yes I know but indeed I wanted to do my own as training but actually I have to focus on my main project. As you told me I went through ACT these last days.

I managed to export data from mechanical to excel by putting them as parameter (by checking the box in mechanical).

Problem is that we can not set everything as parameter and even if we can, it won't be the best way to do..

So my actual problem is how to go through the mechanical tree to get the value I want using ACT ?

If I get it well it should be something like this :

GetTemplate().CreateSystem().GetContainer(ComponentName="Setup ?").GetProperty but then I'm lost..

If someone have some experience with ACT, any idea how to access data (equivalent stress for example) this way ? I think we have to put "setup" in the ComponentName Get Container but thats it.

The only exemple they give in act doc is with engineering data.

Thx
 
The tutorial name in customer portal is: Introduction to ANSYS ACT for ANSYS Mechanical 17.0.
You can start ACT Console in Mechanical to get a feel for different functions (adding load/result objects etc).


 
Hey L_K

I manage to do everything I wanted.

For those who had same problem easiest way is to use the act console and start with the ExtAPI tag. This would help you access evertything you want in mechanical and put them in a script.
To export all this to Excel, you have to use the link L_K send.

Thanks for your help [wavey2]
 
Hello,

i have the same Problem like numi.
I have a static analysis with multiple loadsteps and I want to export a deformation probe result table (Tabular data) for every loadstep as txtfile.

For total deformation i somehow managed it (see code below...not very elegant!), but for deformation probe the same is not possible (because CreateResultsAtAllSets does not exist)

analysis = ExtAPI.DataModel.Project.Model.Analyses[0]
solution = analysis.Solution
TotalDef = solution.Children[1]
TotalDef.CreateResultsAtAllSets()
TotalDef.EvaluateAllResults()
myfile = open('test.txt', 'w')
for k in range(3,solution.Children.Count-4):
TotalDef = solution.Children[k]
Maximum = TotalDef.Maximum
Minimum = TotalDef.Minimum
myfile.write("%s \t %s\n" % (Maximum, Minimum))
myfile.close()


Any suggestions? Thanks!
 
Hi,

Maybe this code will give you some ideas. It is not very optimized or neat but should work.

Python:
model = ExtAPI.DataModel.Project.Model
analysis = model.Analyses[0]
solution = analysis.Solution
analysis_settings = analysis.AnalysisSettings
number_of_steps = analysis_settings.NumberOfSteps
workingdir = analysis.WorkingDir

#Delete old results, for testing purposes

for num, child in enumerate(solution.Children):
	if num != 0:
		child.Delete()


#Create a total deformation probe for every coordinate system whose name contains "probe"


coordinate_systems = [cs for cs in model.CoordinateSystems.Children if "probe" in cs.Name]


probes = []

for cs in coordinate_systems:
	

	totaldef = solution.AddDeformationProbe()
	
	probes.append(totaldef)

	totaldef.LocationMethod = LocationDefinitionMethod.CoordinateSystem

	totaldef.CoordinateSystemSelection = cs

	totaldef.Orientation = cs

	totaldef.ResultSelection =ProbeDisplayFilter.Total

	solution.EvaluateAllResults()

all_results = []

for probe in probes:

	res = []
	res.append(probe.Name)
	
	for step in range(number_of_steps):
		probe.DisplayTime = Quantity("{} [s]".format(step+1))
		probe.EvaluateAllResults()
		res.append(probe.TotalDeformation)
	
	

	all_results.append(res)	


with open(workingdir + 'test.txt', 'w') as myfile:

	for result in all_results:
		for value in result:
			myfile.write(str(value))
			myfile.write(",")
		myfile.write("\n")
 
Thank you LK!!

Do i really have to evaluate my results for every loadstep?
I have quite a lot of loadsteps, so its very time consuming.

Still thank you for your quick help!
 
I will have a deeper look into it as soon as i can.
Thank you LK
 
I have the same problem. But I am very new to Workbench. I have already setup some solutions manually (average displacement at some surfaces along their normal direction over different frequencies). What should I write to export those tabular data in a simple way?

Please help!
 
Use the previous example as a template. Use auto-complete. For example after you have defined
variable called solution, type "solution." and the console will list all the functions and properties
that are possible. If you want to share your Ansys project, I can take a closer look.

You could create a named selection which includes all the surfaces you want the average for. Then find
out the normals of the surfaces. Maybe create new coordinate systems that are normal to surfaces if they are not
aligned with the global coordinates.

How many surfaces and frequencies are we talking about?
 
Hi, Guys!
i have similar problem about IronPython interaction with excel.

I am working on xml+python script development, ACT.
I am doing development in AIM. when I assign a material to a body, the scrip record is like this:

>>>materialAssignment2 = study1.CreateEntity(
>>> Type="MaterialAssignment",
>>>Association=physicsDefinition1,
>>> Location=["BODY8"])
>>>material1 = study1.CreateEntity(
>>> Type="Material",
>>> Association=physicsDefinition1)
>>>material1.ImportEngineeringData(Name="Aluminum Alloy")
>>>materialAssignment2.Material = material1

the problem is i do not know "BODY8", but only know its part name "1.par"
HOW to get the "BODY8" of "1.par", through script? thank you very much!
 
L_K

Thank you.

I wrote the following step:

analysis = ExtAPI.DataModel.Project.Model.Analyses[0]
solution = analysis.Solution
Fq1 = solution.Children[1]

Does it mean I create an object that contains my first set of solution? If it is so. How shall I export the data from Fq1? I did not find any method that allows me to export data.

Besides, I don't know why my console always freezes up (mouse becomes spinning circle when it is in the console area). Do you have similar problem?
 
I hope this code will get you started. Before running the code in console you
have to select the surfaces for which you want directional deformation objects.
Python:
model = ExtAPI.DataModel.Project.Model
analysis = model.Analyses[0]
solution = analysis.Solution
analysis_settings = analysis.AnalysisSettings
number_of_steps = analysis_settings.NumberOfSteps
workingdir = analysis.WorkingDir

#Clear all solution objects, named selections and coordinate system. For testing purposes.

for num,obj in enumerate(solution.Children):
	if num !=0:
		if obj.GetType().ToString() == 'Ansys.ACT.Automation.Mechanical.TreeGroupingFolder':
			obj.DeleteTreeGroupAndChildren()
		else: obj.Delete()

for nsel in model.NamedSelections.Children:
	nsel.Delete()

for num,cs in enumerate(model.CoordinateSystems.Children):
	if num != 0:
		cs.Delete()



#select faces for which you want to create directional deformation results

face_ids = ExtAPI.SelectionManager.CurrentSelection.Ids


for num,face_id in enumerate(face_ids,1):
	selection = ExtAPI.SelectionManager.CreateSelectionInfo(SelectionTypeEnum.GeometryEntities)
	selection.Ids = [face_id]
	
	cs = model.CoordinateSystems.AddCoordinateSystem()
	cs.Name = "face{}_cs".format(num)
	cs.OriginLocation = selection
	cs.PrimaryAxisDefineBy=CoordinateSystemAlignmentType.Associative
	cs.PrimaryAxisLocation = selection

	dirdef = solution.AddDirectionalDeformation()
	dirdef.Name = "face{}_directional_deformation".format(num)
	dirdef.Location = selection
	dirdef.CoordinateSystem = cs

This only works for planar surfaces.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor