Write information to Excel, example on Mesh Quality
Pernelle Marone-Hitz
Member, Moderator, Employee Posts: 859
✭✭✭✭
In Mechanical and through ACT scripting, how can I extract the min, max, average mesh quality shown in the detail's view of each body (provided a mesh quality has been selected at the mesh level) and write this information to an Excel document?
Tagged:
0
Best Answers
-
The following code can be adapted:
import clr clr.AddReferenceByName('Microsoft.Office.Interop.Excel') from Microsoft.Office.Interop import Excel excel = Excel.ApplicationClass() excel.Visible = True # makes the Excel application visible to the user excel.ScreenUpdating = True # Enables screen refreshing # opening a workbook filename = r"D:\DATA\Book1.xlsx" #Specify existing Excel document workbook = excel.Workbooks.Open(filename) # adding a worksheet ws = workbook.Worksheets.Add() # Add a new worksheet and change its name ws.Name = "MyNewSheet" geometry = ExtAPI.DataModel.Project.Model.Geometry # Reference geometry bodies = geometry.GetChildren(DataModelObjectCategory.Body, True) # Get a list of all bodies ws.Cells(1,1).Value = "Body name" # Prepare name of columns in Excel file ws.Cells(1,2).Value = "Minimum" ws.Cells(1,3).Value = "Maximum" ws.Cells(1,4).Value = "Average" i = 2 for body in bodies: ws.Cells(i,1).Value = str(body.Name) # write body name ws.Cells(i,2).Value =body.Minimum # write body's minimum mesh quality value ws.Cells(i,3).Value =body.Maximum ws.Cells(i,4).Value =body.Average i = i+1
6 -
Users can also request to get elemental quality ratios, instead of maximum, minimum and average values. The code shown below allows you to print element id, mesh type (kTet10, kHex20...), body and element quality ratio. With this info, a customized report can be done by bodies, element type...
import clr clr.AddReferenceByName('Microsoft.Office.Interop.Excel') from Microsoft.Office.Interop import Excel excel = Excel.ApplicationClass() excel.Visible = True excel.ScreenUpdating = True # opening a workbook filename = r"D:\TEST\Maillage_Comptage.xlsx" workbook = excel.Workbooks.Open(filename) ws = workbook.Worksheets.Add() # adding a worksheet ws.Name = "MyNewSheet" analysis = ExtAPI.DataModel.Project.Model.Analyses[0] meshdata = analysis.MeshData results = analysis.GetResultsData() mesh_results = results.GetResult("MESH_") mesh_results.Components mesh_results.SelectComponents(['ELEMENT_QUALITY']) ws.Cells(1,1).Value = "Element ID" ws.Cells(1,2).Value = "Type" ws.Cells(1,3).Value = "Body" ws.Cells(1,4).Value = "Quality" i = 2 for j in range(meshdata.Elements.Count): my_body = ExtAPI.DataModel.GeoData.GeoEntityById(meshdata.Elements[j].TopoRefId) ws.Cells(i,1).Value = str(meshdata.Elements[j].Id) ws.Cells(i,2).Value = str(meshdata.ElementById(meshdata.Elements[j].Id).Type) ws.Cells(i,3).Value = str(my_body.Name) ws.Cells(i,4).Value = str(mesh_results.GetElementValues(meshdata.Elements[j].Id)[0]) i = i+1
6