I have several .csv files that define pressure boundary conditions. I would like to use those files in Mechanical to define imported pressure, each file defining the pressure for one specific load step.
Let's assume the files have been saved inside a folder named "PressuresToImport" and that this folder is placed in the user files directory of the project:
The code below will parse this folder, define and imported load group and an imported pressure, scoped to a named selection called "Pressure_NS":
import wbjn import os # Get pressures file from a specific folder in the User Files directory userfilesdir = wbjn.ExecuteCommand(ExtAPI,"""returnValue(GetUserFilesDirectory())""") pressure_folder = os.path.join(userfilesdir, 'PressuresToImport') pressure_files = [] for root, dirs, files in os.walk(pressure_folder): for name in files: pressure_files.append(os.path.join(root, name)) pressure_files.sort() for file_path in pressure_files: print(file_path) # Full path if not pressure_files: raise Exception("No pressure files found in: {}".format(pressure_folder)) # Add imported load group analysis = Model.Analyses[0] imported_load_group = analysis.AddImportedLoadExternalData() external_data_files = Ansys.Mechanical.ExternalData.ExternalDataFileCollection() external_data_files.SaveFilesWithProject = False file_identifiers = [] for i, file_path in enumerate(pressure_files, start=1): external_data_file = Ansys.Mechanical.ExternalData.ExternalDataFile() external_data_files.Add(external_data_file) external_data_file.Identifier = "File{}".format(i) external_data_file.Description = "" external_data_file.IsMainFile = False external_data_file.FilePath = file_path external_data_file.ImportSettings = Ansys.Mechanical.ExternalData.ImportSettingsFactory.GetSettingsForFormat( MechanicalEnums.ExternalData.ImportFormat.Delimited ) import_settings = external_data_file.ImportSettings import_settings.SkipRows = 0 import_settings.SkipFooter = 0 import_settings.Delimiter = "\t" import_settings.AverageCornerNodesToMidsideNodes = False import_settings.UseColumn(0, MechanicalEnums.ExternalData.VariableType.XCoordinate, "mm", "X Coordinate@A") import_settings.UseColumn(1, MechanicalEnums.ExternalData.VariableType.YCoordinate, "mm", "Y Coordinate@B") import_settings.UseColumn(2, MechanicalEnums.ExternalData.VariableType.ZCoordinate, "mm", "Z Coordinate@C") import_settings.UseColumn(3, MechanicalEnums.ExternalData.VariableType.Pressure, "MPa", "Pressure@D") file_identifiers.append("File{}".format(i)+":Pressure@D") imported_load_group.ImportExternalDataFiles(external_data_files) # Add imported pressure imported_pressure = imported_load_group.AddImportedPressure() # Scope pressure to "Pressure_NS" named selection imported_pressure.Location = DataModel.GetObjectsByName('Pressure_NS')[0] # Modify imported pressure Data magnitude_col_idx = 0 analysis_time_col_idx = 1 table=imported_pressure.GetTableByName("") table.Clear() with Transaction(): row_index=0 for i, fi in enumerate(file_identifiers, start=1): table=imported_pressure.GetTableByName("") # get the worksheet table. Must be done at every step to grab the full table row =table[row_index] # get row to be defined row[magnitude_col_idx] = fi row[analysis_time_col_idx] = i if row_index<len(file_identifiers)-1: table.Add(None) # add a new row to the table row_index+=1 Tree.Refresh()
Inside the Imported Load group, we can check that different files have been imported:
For each file, we can verify how the data has been defined:
And in the imported pressure, we can verify that each file has been used for a specific step:
Please note that this code currently assumes that the number of steps in the analysis have already been defined and that the number of files match the number of steps. For better robustness, these verifications could be added to the script.