How to access Tabular data using ACT?

Chemsdine CHEMAI
Chemsdine CHEMAI Member, Employee Posts: 201
First Anniversary Ansys Employee Solution Developer Community of Practice Member 5 Likes
edited December 2023 in Structures

How to access Tabular data using ACT?

Tagged:

Best Answers

  • Chemsdine CHEMAI
    Chemsdine CHEMAI Member, Employee Posts: 201
    First Anniversary Ansys Employee Solution Developer Community of Practice Member 5 Likes
    Answer ✓

    It's possible using the following unsupported code :

    paneTabular=ExtAPI.UserInterface.GetPane(MechanicalPanelEnum.TabularData)
    control = paneTabular.ControlUnknown
    for col in range(1,control.ColumnsCount+1):
        for row in range(1,control.RowsCount+1):
            cellText= control.cell(row ,col ).Text
            if cellText!=None:
                print cellText
    
  • M
    M Member, Employee Posts: 199
    First Comment First Anniversary Ansys Employee Solution Developer Community of Practice Member
    Answer ✓

    I enhanced your solution to write the table to a csv:

    import os
    import csv
    directory = 'D:/'
    
    for analysis in Model.Analyses:
        for solution in analysis.Solution.Children:
            if solution.Name != 'Solution Information':
                solution.Activate()
                paneTabular=ExtAPI.UserInterface.GetPane(MechanicalPanelEnum.TabularData)
                control = paneTabular.ControlUnknown    
                num_columns = control.ColumnsCount+1
                num_rows = control.RowsCount+1
                result = []
                for row in range(1,num_rows):
                    r = []
                    for col in range(1,num_columns):
                        r.append(control.cell(row ,col ).Text)
                    result.append(r)
                save_name = os.path.join(directory, analysis.Name + ' '+ solution.Name+'.csv')
                with open(save_name, 'w') as f:
                    writer = csv.writer(f)
                    writer.writerows(result)
    

    This will write all tabular solution data to a csv. Please tell me there is a better way.

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 715
    First Comment First Anniversary Ansys Employee Solution Developer Community of Practice Member
    Answer ✓

    It seems there is now (2021R2) a better way to access Tabular Data:

    TOTAL_DEFORMATION_1 = SOLUTION.AddTotalDeformation()  
    FREQ1 = TOTAL_DEFORMATION_1.TabularData["Frequency"][0]   
    

    See https://ansyshelp.ansys.com/account/secured?returnurl=/Views/Secured/corp/v212/en/act_script/act_script_demo_modal_acoustic.html for further reference.

  • M
    M Member, Employee Posts: 199
    First Comment First Anniversary Ansys Employee Solution Developer Community of Practice Member
    Answer ✓

    Here is a way to fill in a tabular data from an imported csv. The example here is a heat flow import (so the quantity units are sec and W).

    At the end, the time for the analysis settings is set.

    Thanks to Chems.

    import csv
    myData = r"D:\my\path\heatFlow.csv"
    rows = []
    
    with open(myData, 'r') as file:
        csvreader = csv.reader(file)
        header = next(csvreader)
        for row in csvreader:
            rows.append(row)
    
    timeSteps = len(rows)
    analysis = ExtAPI.DataModel.Project.Model.Analyses[0]
    
    myTimes = [Quantity(float(i[0]), "sec") for i in rows] # create quantity lists for Discrete input
    myFlow = [Quantity(float(i[1]), "W") for i in rows]
    
    myHeatFlow = analysis.AddHeatFlow()
    myMag = myHeatFlow.Magnitude
    myMag.Inputs[0].DiscreteValues = myTimes
    myMag.Output.DiscreteValues = myFlow
    
    ## You might want to check this, it seems to work and avoid the duplication of the steps but might add 1 extra (+1) !!
    analysis.AnalysisSettings.NumberOfSteps = timeSteps
    with Transaction(suspendClicks=True):
        for t0 in range(timeSteps):
            analysis.AnalysisSettings.CurrentStepNumber= t0+1
            analysis.AnalysisSettings.StepEndTime = myTimes[t0+1]
    

Answers