How to create elastic strain plot for each ply of a layered composite modeled by solids?

Javier Vique
Javier Vique Member, Employee Posts: 86
Second Anniversary 5 Answers 25 Likes 10 Comments
✭✭✭✭

The idea is to automate the creation of elastic strains result in Mechanical on X or Y direction for all plies of a solid model, grouping these objects and then printing the maximum or minimum values.

Tagged:

Answers

  • Javier Vique
    Javier Vique Member, Employee Posts: 86
    Second Anniversary 5 Answers 25 Likes 10 Comments
    ✭✭✭✭

    Creating elastic strain object is straightforward in Mechanical. The main issue might be understanding how plies are selected when Sub Scope By is set as Ply. Please see the code below which identifies the plies (they've got 3D in their names') and then these are used in the scope of each result.

    import os
    
    Solution = ExtAPI.DataModel.Project.Model.Analyses[0].Solution
    AllPlies = ExtAPI.DataModel.GetObjectsByType(DataModelObjectCategory.AnalysisPly)
    
    def PostCreation(direction):
        MaximumResults = []
        MinimumResults = []
        for Ply in AllPlies:
            if '3D' in Ply.Name:
                StrainElastic = Solution.AddNormalElasticStrain()
                StrainElastic.Ply = [Ply]
                if direction == 'YY':
                    StrainElastic.NormalOrientation = NormalOrientationType.YAxis
                StrainElastic.By = SetDriverStyle.MaximumOverTime
                StrainElastic.DisplayOption = ResultAveragingType.ElementalMean
                StrainElastic.RenameBasedOnDefinition()
                MaximumResults.append(StrainElastic)
                StrainElasticMin = StrainElastic.Duplicate()
                StrainElasticMin.By = SetDriverStyle.MinimumOverTime
                StrainElasticMin.RenameBasedOnDefinition()
                MinimumResults.append(StrainElasticMin)
        name_max = '3D_Plies' + direction + '_MaxOverTime'
        name_min = '3D_Plies' + direction + '_MinOverTime'
        Solution.EvaluateAllResults()
        for i in MaximumResults:
            print('Result:%s Ply:%s Strain:%.5f' % (name_max,i.Name,i.Maximum.Value))
        for i in MinimumResults:
            print('Result:%s Ply:%s Strain:%.5f' % (name_max,i.Name,i.Minimum.Value))
        Result_Group = Tree.Group(MaximumResults)
        Result_Group.Name = name_max
        Result_Group = Tree.Group(MinimumResults)
        Result_Group.Name = name_min
        return
    
    PostCreation('XX')
    PostCreation('YY')
    
  • choukri ELGHERYB
    choukri ELGHERYB Member Posts: 1
    First Comment
    **

    final version with extract csv and picture for each ply
    import csv
    import os

    Solution = ExtAPI.DataModel.Project.Model.Analyses[0].Solution
    AllPlies = ExtAPI.DataModel.GetObjectsByType(DataModelObjectCategory.AnalysisPly)

    def PostCreation(direction, csv_writer):
    MaximumResults = []
    MinimumResults = []
    for Ply in AllPlies:
    if 'TVF57' in Ply.Name:
    StrainElastic = Solution.AddNormalElasticStrain()
    StrainElastic.Ply = [Ply]
    if direction == 'YY':
    StrainElastic.NormalOrientation = NormalOrientationType.YAxis
    StrainElastic.By = SetDriverStyle.MaximumOverTime
    StrainElastic.DisplayOption = ResultAveragingType.ElementalMean
    StrainElastic.RenameBasedOnDefinition()
    MaximumResults.append(StrainElastic)
    StrainElasticMin = StrainElastic.Duplicate()
    StrainElasticMin.By = SetDriverStyle.MinimumOverTime
    StrainElasticMin.RenameBasedOnDefinition()
    MinimumResults.append(StrainElasticMin)
    name_max = 'TVF57_Plies' + direction + '_MaxOverTime'
    name_min = 'TVF57_Plies' + direction + '_MinOverTime'
    Solution.EvaluateAllResults()
    for i in MaximumResults:
    max_strain = i.Maximum.Value
    csv_writer.writerow([name_max, i.Name, max_strain])
    print('Result:%s Ply:%s Strain:%.5f' % (name_max, i.Name, max_strain))
    ExportImage(i, name_max)
    for i in MinimumResults:
    min_strain = i.Minimum.Value
    csv_writer.writerow([name_min, i.Name, min_strain])
    print('Result:%s Ply:%s Strain:%.5f' % (name_min, i.Name, min_strain))
    ExportImage(i, name_min)
    Result_Group = Tree.Group(MaximumResults)
    Result_Group.Name = name_max
    Result_Group = Tree.Group(MinimumResults)
    Result_Group.Name = name_min

    def ExportImage(result, result_name):
    Settings = Ansys.Mechanical.Graphics.GraphicsImageExportSettings()
    result.Activate()
    image_dir = r"D:\images"
    if not os.path.exists(image_dir):
    os.makedirs(image_dir)
    image_path = os.path.join(image_dir, result_name + '' + result.Name + '.png')
    try:
    ExtAPI.Graphics.ExportImage(image_path, GraphicsImageExportFormat.PNG, Settings)
    print('Exported image for result: {}
    {}'.format(result_name, result.Name))
    except Exception as ex:
    print('Failed to export image for result: {}_{} - {}'.format(result_name, result.Name, str(ex)))

    image_dir = r"D:\images"
    if not os.path.exists(image_dir):
    os.makedirs(image_dir)

    csv_file_path = os.path.join(image_dir, 'results_results_ID1_TVF57.csv')
    try:
    with open(csv_file_path, mode='w') as file:
    csv_writer = csv.writer(file)
    csv_writer.writerow(['Result Type', 'Ply Name', 'Strain Value']) # Header row
    PostCreation('XX', csv_writer)
    PostCreation('YY', csv_writer)
    print("CSV file saved: {}".format(csv_file_path))
    except Exception as ex:
    print('Failed to save CSV file: {}'.format(str(ex)))