How to retrieve MAPDL matids for each body in a custom result?
Customer wants to retrieve the MatID associated to selected bodies in a custom result in 2020R2. How can he do this?
Best Answers
-
the solver_data structure would be useful in such a case, as it now has a .GetMaterialSolverId(geoBodyId) function. But... this was not available in 2020R2.
I found out that the information is written to the ds.dat file in a comment block. So here is a sample code that will retrieve the information from the ds.dat file, write out a text file to user_files and display a message in Mechanical with the body ids corresponding to the ACT object selection. Not the most elegant solution, but it works...
# Custom Result Object Evaluation def EvaluateMatID(result,stepInfo,collector): import os # Reader initialization reader = result.Analysis.GetResultsData() reader.CurrentResultSet = stepInfo.Set solution=result.Parent.Solution matids_dict={} # Write a file with body names and corresponding APDL MatID f_out=open(os.path.join(result.Analysis.WorkingDir,'../../../user_files','matids.txt'),'w') # File to be written in user_files folder datfile=os.path.join(result.Analysis.WorkingDir,'ds.dat') # MAPDL input file to be parsed with open(datfile,'r') as f: # Parse MAPDL file line=f.readline() while line.find('!************************* Model Summary ********************')==-1: # This indicates where the correspondance between body names and matid are available line=f.readline() # read body names and matid line=f.readline() while line.find('!************************* End Model Summary ********************')==-1: # Read in the comments splitline=line.split(',') # Split with "," to separate information in the comments f_out.write(splitline[0].strip('!')+'\t'+splitline[-1]) # Write out first part of line (body name) and last part (matid) matids_dict[splitline[0].strip('!')]=int(splitline[-1]) # create dictionary with material ids line=f.readline() f_out.close() # display message with matid for selected body(ies) for the ACT object propGeo = result.Properties["Geometry"] refIds = propGeo.Value.Ids mat_ids=[] for bodyId in refIds: bodyName=ExtAPI.DataModel.GeoData.GeoEntityById(bodyId).Name mat_ids.append(matids_dict[bodyName]) msg = Ansys.Mechanical.Application.Message('Found the following material Ids: '+str(mat_ids), MessageSeverityType.Warning) ExtAPI.Application.Messages.Add(msg)
4 -
The above script only works if all bodies have different names. I had to adapt it so we don't rely on body names but just on their order in the tree
def EvaluateMatID(result,stepInfo,collector): '''Evaluates Normal Stress as a custom result, with different averaging options.''' import os # Reader initialization reader = result.Analysis.GetResultsData() reader.CurrentResultSet = stepInfo.Set solution=result.Parent.Solution matids_dict={} body_index=0 # Write a file with body names and corresponding APDL MatID f_out=open(os.path.join(result.Analysis.WorkingDir,'../../../user_files','matids.txt'),'w') # File to be written in user_files folder datfile=os.path.join(result.Analysis.WorkingDir,'ds.dat') # MAPDL input file to be parsed with open(datfile,'r') as f: # Parse MAPDL file line=f.readline() while line.find('!************************* Model Summary ********************')==-1: # This indicates where the correspondance between body names and matid are available line=f.readline() # read body names and matid line=f.readline() while line.find('!************************* End Model Summary ********************')==-1: # Read in the comments splitline=line.split(',') # Split with "," to separate information in the comments f_out.write(splitline[0].strip('!')+'\t'+splitline[-1]) # Write out first part of line (body name) and last part (matid) matids_dict[body_index]=[splitline[0].strip('!'),int(splitline[-1])] # create dictionary with material ids body_index+=1 line=f.readline() f_out.close() # display message with matid for selected body(ies) for the ACT object propGeo = result.Properties["Geometry"] refIds = propGeo.Value.Ids # as we can have duplicate body names, running through body Ids in order they appear # in the tree mat_ids=[] body_index=0 bodies=ExtAPI.DataModel.GetObjectsByType(DataModelObjectCategory.Body) # retrieve all bodies for body in bodies: geo_body_id=body.GetGeoBody().Id if geo_body_id in refIds: mat_ids.append(matids_dict[body_index][1]) body_index+=1 msg = Ansys.Mechanical.Application.Message('Found the following material Ids: '+str(mat_ids), MessageSeverityType.Warning) ExtAPI.Application.Messages.Add(msg)
6
Answers
-
@Pierre Thieffry
Thank you for these codeI wanted to ask about the
Ansys.ACT.Interfaces.Analysis.ISolverData.GetMaterialSolverId(geoID)
the documentation says only one parameter is required (int32)
But shell shows this
Ansys.ACT.Interfaces.Analysis.ISolverData.GetMaterialSolverId(26)
GetMaterialSolverId() takes exactly 2 arguments (1 given)And when I try to give a second parameter it shows this
Ansys.ACT.Interfaces.Analysis.ISolverData.GetMaterialSolverId(ISolverData,26)
expected ISolverData, got typeWhat is this second parameter (ISolerData) and how can I find it?
Thank you in advance
0 -
If you are trying to get this information for post processing after the solution is done, you should look at the solution object: Solution.SolverData
If you are trying to get this before the solve while, the input file is being written. The solver data is passed as an argument to the get solved commands Call Back.
0 -
@CollinsJnr to follow up on @Mike.Thompson ' s reply, you can use this in a Python Code with "Get Solve commands" callback:
geobody_id=741 matid_for_a_body=Ansys.ACT.Interfaces.Analysis.ISolverData.GetMaterialSolverId(solver_data,geobody_id)
As Mike mentioned, the 'solver_data' info is an argument passed to the callback as you can see from the comments in the Python Code:
solver_data -- data stucture that allows you to access information from the model such as current step, contact pair ids, etc.
Using the function as is in the console will lead to the message on the required argument. Nevertheless, the documentation is indeed misleading. I will report internally.
0