How to export results (SMISC - axial forces and moments) for all the beam connections in Mechanical?

Options
Rohith Patchigolla
Rohith Patchigolla Member, Moderator, Employee Posts: 115
First Answer First Anniversary Name Dropper Solution Developer Community of Practice Member

I have lots of beam connections in the model. I want to export the data in the below format into a csv.

beam element type id, Beam name in Mechanical, Fx, My_i, My_j, Mz_i , Mz_j

Comments

  • Rohith Patchigolla
    Rohith Patchigolla Member, Moderator, Employee Posts: 115
    First Answer First Anniversary Name Dropper Solution Developer Community of Practice Member
    edited October 2023
    Options

    This script (can be used in Mechanical Scripting console or Python Code object with After Post callback) which is a combination of Mechanical scripting and DPF will export the needed data into a csv file saved in User Files Directory of the Project.

    The results will correspond to the first analysis in the list. If a different analysis is desired, please change the index on the command "analysis = DataModel.AnalysisList[0]".

    Also, if the beam connection is scoped to bolt pretension, please note that there will be two beams created and the code needs to be tweaked to get the information we need. Please have a look at the second answer to this post

    import os
    import wbjn
    
    #Function to write a CSV file from the gathered data
    def writeCSV(t1,fileName):
        ExtAPI.Log.WriteMessage('my File name ' + fileName)
        with open(fileName , 'w') as f:
            for line in t1:
                for col in line:
                    #For Non-German
                    f.write(str(col) + ', ')
                    #For German OS 
                    #f.write(str(col).replace('.',',') + '; ')
                f.write('\n')
    
    #Find the user files directory for saving the data
    UserFilesDir = wbjn.ExecuteCommand(ExtAPI,"returnValue(GetUserFilesDirectory())")
    #Provide the name of the CSV file
    saveName = os.path.join(UserFilesDir,'BeamMoments_My_Mz' + '.csv')
    
    analysis = DataModel.AnalysisList[0]
    first_solution = analysis.Solution
    solver_data = first_solution.SolverData    
    allBeams = DataModel.GetObjectsByType(DataModelObjectCategory.Beam)
    allData = []
    for beam in allBeams:
        beamData = solver_data.GetObjectData(beam)
        beamMatid = beamData.MaterialId
        beamElem = solver_data.ElementIdsByMaterialId(str(beamMatid))
    
        import mech_dpf
        import Ans.DataProcessing as dpf
        #Result Data    
        dataSource = dpf.DataSources(analysis.Solution.ResultFilePath)
    
        # Read mesh in results file
        mesh_op = dpf.operators.mesh.mesh_provider() 
        mesh_op.inputs.data_sources.Connect(dataSource)
        my_mesh = mesh_op.outputs.mesh.GetData()
    
        #scoping
        my_scoping = dpf.Scoping()
        my_scoping.Location = "Elemental"
        my_scoping.Ids = beamElem
        Myi = dpf.operators.result.mapdl.smisc(mesh=my_mesh,data_sources=dataSource,item_index = 2,mesh_scoping=my_scoping).outputs.fields_container.GetData()
        Myj = dpf.operators.result.mapdl.smisc(mesh=my_mesh,data_sources=dataSource,item_index = 15,mesh_scoping=my_scoping).outputs.fields_container.GetData()
        Mzi = dpf.operators.result.mapdl.smisc(mesh=my_mesh,data_sources=dataSource,item_index = 3,mesh_scoping=my_scoping).outputs.fields_container.GetData()
        Mzj = dpf.operators.result.mapdl.smisc(mesh=my_mesh,data_sources=dataSource,item_index = 16,mesh_scoping=my_scoping).outputs.fields_container.GetData()
    
        allData.append([beamMatid, beam.Name,Myi[0].Data[0], Myj[0].Data[0], Mzi[0].Data[0], Mzj[0].Data[0]])
    #CSV file will be written in the User Files Directory
    writeCSV(allData,saveName)