├── images ├── btn.PNG └── console.PNG ├── LICENSE ├── README.md ├── Export Beams Probes from Connections.py └── Export Weld Force-Moment Probes from Solution.py /images/btn.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/robsiegwart/ANSYS-ACT-snippets/HEAD/images/btn.PNG -------------------------------------------------------------------------------- /images/console.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/robsiegwart/ANSYS-ACT-snippets/HEAD/images/console.PNG -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Rob Siegwart 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ANSYS-ACT-snippets 2 | 3 | Collection of some files for performing scripting in ANSYS Mechanical via the ACT API. 4 | 5 | Note that scripting in ANSYS Mechanical uses Python 2. 6 | 7 | ## Contents 8 | 9 | ### `Export Beams Probes from Connections.py` 10 | 11 | This file loops through beam objects in the Connections folder and searches for 12 | the corresponding beam probes in the Solution. If a beam probe is not already 13 | in the Solution for that beam then a new one is created. Beam results are then 14 | saved out to a text file in the `user_files` directory of the project. 15 | 16 | The output text file is tab-delimited with the following contents/columns: 17 | 18 | Beam Name Axial Force Torque Shear Force at I Shear Force at J Moment at I Moment at J 19 | 20 | ### `Export Weld Force-Moment Probes from Solution.py` 21 | 22 | This script exports force and moment result probes associated with structural 23 | welds. This is applicable when Bonded contact is used to represent welded 24 | connections. Usage of the script is performed by first creating a force and 25 | moment probe for each contact pair that represents a welded connection. These 26 | are then all placed within a grouping folder in the Solution portion of the tree 27 | having the word "weld" in it (case insensitive). This is used to designate those 28 | result objects that are considered welds. Force and moment results are they 29 | saved to a text file in the `user_files` directory of the project. 30 | 31 | The output text file is tab-delimited with the following contents/columns: 32 | 33 | Probe Name FX FY FZ MX MY MZ 34 | 35 | ## Usage 36 | 37 | Open the ACT pane with the button (pre-tabbed application shown): 38 | ![ANSYS ACT button](images/btn.PNG) 39 | 40 | Copy and paste script contents into the console of the ANSYS ACT pane: 41 | ![ANSYS ACT console](images/console.PNG) -------------------------------------------------------------------------------- /Export Beams Probes from Connections.py: -------------------------------------------------------------------------------- 1 | # MIT License 2 | 3 | # Copyright (c) 2020 Rob Siegwart 4 | 5 | # Permission is hereby granted, free of charge, to any person obtaining a copy 6 | # of this software and associated documentation files (the "Software"), to deal 7 | # in the Software without restriction, including without limitation the rights 8 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | # copies of the Software, and to permit persons to whom the Software is 10 | # furnished to do so, subject to the following conditions: 11 | 12 | # The above copyright notice and this permission notice shall be included in all 13 | # copies or substantial portions of the Software. 14 | 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | # SOFTWARE. 22 | 23 | ''' 24 | Beam Probe Exporting Script for ANSYS Mechanical 25 | ================================================ 26 | 27 | This is a Mechanical ACT script to save out beam result probes to a text file. 28 | 29 | This file loops through the beams in the Connections folder and searches for 30 | the corresponding beam probes in the Solution. If a beam probe is not already 31 | in the Solution for that beam then a new one is created. Therefore you do not 32 | need to already have created the result probes in the Solution. 33 | 34 | The output text file is tab-delimited with the following contents/columns: 35 | 36 | Beam Name Axial Force Torque Shear Force at I Shear Force at J Moment at I Moment at J 37 | 38 | 39 | Remember that ANSYS uses Python 2.x. 40 | 41 | ANSYS versions tested with: 42 | 17.1, 2019R1 43 | 44 | 45 | VERSION 0.1.0 46 | ''' 47 | 48 | # Set the working directory to the user_files directory of the project 49 | from os import chdir 50 | MECH_dir = ExtAPI.DataModel.Project.Model.Analyses[0].WorkingDir 51 | user_dir = MECH_dir.split('_files')[0] + r'_files\user_files' 52 | chdir(user_dir) 53 | 54 | # Create variables for connections and solution objects 55 | connections = ExtAPI.DataModel.Project.Model.Connections 56 | solution = ExtAPI.DataModel.Project.Model.Analyses[0].Solution 57 | 58 | # Filter the connections and solution items for beams and beam probes, respectively 59 | cont_beams = filter(lambda item: item.GetType() == Ansys.ACT.Automation.Mechanical.Connections.Beam and item.Suppressed == False, connections.Children) 60 | sol_beam_probes = filter(lambda item: item.GetType() == Ansys.ACT.Automation.Mechanical.Results.ProbeResults.BeamProbe, solution.Children) 61 | 62 | # Create a list of beams from the solution beam probes 63 | sol_beams = map(lambda bp: bp.BoundaryConditionSelection, sol_beam_probes) 64 | 65 | # Convert the connections and solution beams lists to sets and compare to find differences 66 | beams_to_create = set(cont_beams) - set(sol_beams) 67 | 68 | # Create a beam probe for each beam not already having a result probe 69 | if beams_to_create: 70 | for beam in beams_to_create: 71 | bp = solution.AddBeamProbe() 72 | bp.BoundaryConditionSelection = beam 73 | sol_beam_probes.append(bp) 74 | 75 | solution.EvaluateAllResults() 76 | 77 | # Open an output file to write results to 78 | f = open('Beam results.txt','w') 79 | f.write('Beam Probes\n\n') 80 | f.write('Name\tAxial Force\tTorque\tShear Force at I\tShear Force at J\tMoment at I\tMoment at J\n\n') 81 | 82 | # Write out beam probes to a file 83 | # the unit segment (e.g. [lbf]) is stripped from the result 84 | for br in sol_beam_probes: 85 | name = str(br.BoundaryConditionSelection.Name).strip() 86 | f.write( name + '\t' + 87 | str(br.AxialForce).split('[')[0] + '\t' + 88 | str(br.Torque).split('[')[0] + '\t' + 89 | str(br.ShearForceAtI).split('[')[0] + '\t' + 90 | str(br.ShearForceAtJ).split('[')[0] + '\t' + 91 | str(br.MomentAtI).split('[')[0] + '\t' + 92 | str(br.MomentAtJ).split('[')[0] + '\n' ) 93 | 94 | f.close() 95 | -------------------------------------------------------------------------------- /Export Weld Force-Moment Probes from Solution.py: -------------------------------------------------------------------------------- 1 | # MIT License 2 | 3 | # Copyright (c) 2020 Rob Siegwart 4 | 5 | # Permission is hereby granted, free of charge, to any person obtaining a copy 6 | # of this software and associated documentation files (the "Software"), to deal 7 | # in the Software without restriction, including without limitation the rights 8 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | # copies of the Software, and to permit persons to whom the Software is 10 | # furnished to do so, subject to the following conditions: 11 | 12 | # The above copyright notice and this permission notice shall be included in all 13 | # copies or substantial portions of the Software. 14 | 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | # SOFTWARE. 22 | 23 | ''' 24 | Weld Exporting Script for ANSYS Mechanical 25 | ========================================== 26 | 27 | This is a Mechanical ACT script to save out Force and Moment probes defined in 28 | the Solution portion of the tree, associated with Bonded contact representing 29 | welds. 30 | 31 | To mark Force/Moment probes as welds, they must be in a group with the word 32 | 'weld' in it (case insensitive). Then, all force and moment probes contained 33 | in it are exported. 34 | 35 | The file is saved to the 'user_files' directory associated with the project 36 | and is called 'Weld Results.txt'. 37 | 38 | Force probes are used as the master list and so the program loops through the 39 | forces and finds the corresponding moment probe for the same contact item. 40 | Stray moment probes will not be discovered. If no moment probe exists, a new 41 | line is added and the data for the moment fields are blank. 42 | 43 | The output text file is tab-delimited with the following contents/columns: 44 | 45 | Probe Name FX FY FZ MX MY MZ 46 | 47 | 48 | To Use 49 | ------ 50 | Put a Force and Moment probe for each desired contact item in the Solution, 51 | within a folder with the word 'weld' in it. This can be sped up by 52 | right-clicking on the Contact item in the Connections folder and choosing 53 | "Create > Force Reaction" and "Create > Moment Reaction". 54 | 55 | For example: 56 | 57 | [-] Solution (A6) 58 | |-- Solution Information 59 | |-- [-] Welds 1 60 | |-- [-] Box 1 61 | | |-- Force Reaction 1 62 | | |-- Moment Reaction 1 63 | |-- [-] Box 2 64 | | |-- Force Reaction 3 65 | | |-- Moment Reaction 3 66 | |-- Force Reaction 4 67 | |-- Moment Reaction 4 68 | 69 | 70 | Notes 71 | ----- 72 | For moment summation the default is to use the centroid of the contact element 73 | set. If your contact geometry accurately represents the weld geometry then this 74 | will be ok. Otherwise you will likely need some small value of trim set in the 75 | contact definition, so that the moment summation point is approximately at the 76 | actual centroid of the weld group. Alternatively, a custom coordinate system 77 | could be used but this is not supported in this script yet. 78 | 79 | Remember that ANSYS uses Python 2.x. 80 | 81 | Limitations: 82 | Does not support custom coordinate systems for the moment summation. Could be 83 | a future feature. 84 | 85 | ANSYS versions tested with: 86 | 17.1 87 | 88 | A nice future feature for this script would be to grab the contacts 89 | marked as welds in the **connections** folder and then find the 90 | corresponding force and moment probes in the solution object if 91 | they exist or create new ones if they don't already exist. 92 | 93 | 94 | VERSION 0.2.0 95 | ''' 96 | 97 | # Set the working directory to the user_files directory of the project 98 | from os import chdir 99 | MECH_dir = ExtAPI.DataModel.Project.Model.Analyses[0].WorkingDir 100 | user_dir = MECH_dir.split('_files')[0] + r'_files\user_files' 101 | chdir(user_dir) 102 | 103 | # Create variables for connections and solution objects 104 | connections = ExtAPI.DataModel.Project.Model.Connections 105 | solution = ExtAPI.DataModel.Project.Model.Analyses[0].Solution 106 | 107 | # The unit portion (e.g. [lbf] and [lbf-in]) is stripped from the result so that it may be imported into Excel and used as is 108 | solution_weld_groups = filter(lambda item: item.GetType() == Ansys.ACT.Automation.Mechanical.TreeGroupingFolder, solution.Children) 109 | solution_weld_groups = filter(lambda item: 'weld' in item.Name.lower(), solution_weld_groups) 110 | 111 | 112 | def cleanup_name(string): 113 | ''' 114 | Clean up the default ANSYS names after you right-click and select 115 | "Rename Based on Definition". Removes common elements that show up in the 116 | name. 117 | ''' 118 | to_remove = ['All - ', ' (Underlying Element)', 'End Time', ' - ', 'Force Reaction', 'Contact', '1. s' ] 119 | name = string 120 | for each in to_remove: 121 | name.replace(each,'') 122 | return name.strip() 123 | 124 | 125 | def save_forces_moments(items_list, folder_name=None): 126 | ''' 127 | Loop through a list of ANSYS items and save force and moment probes. The 128 | forces are used as the master - moments are looked up based on the force. 129 | Stray moment probes won't be discovered. 130 | 131 | Set a string to folder_name to have it print the folder name in the output 132 | file. 133 | 134 | Parameters 135 | ---------- 136 | items_list a list of ANSYS objects to filter for force, moments 137 | folder_name an optional name to include in the output text name description 138 | ''' 139 | forces = filter(lambda x: x.GetType() == Ansys.ACT.Automation.Mechanical.Results.ProbeResults.ForceReaction, items_list) 140 | moments = filter(lambda x: x.GetType() == Ansys.ACT.Automation.Mechanical.Results.ProbeResults.MomentReaction, items_list) 141 | 142 | if not forces: 143 | return 144 | 145 | if folder_name: 146 | f.write(' {} '.format(folder_name).center(20,'-') + '\n') 147 | 148 | for force in forces: 149 | bc = force.ContactRegionSelection 150 | name = cleanup_name(bc.Name) 151 | f.write( name + '\t' + 152 | str(force.XAxis).split('[')[0] + '\t' + 153 | str(force.YAxis).split('[')[0] + '\t' + 154 | str(force.ZAxis).split('[')[0] + '\t' ) 155 | 156 | # look for the moment load that is of the same contact item 157 | moment_sel = filter(lambda mr: mr.ContactRegionSelection == bc, moments) 158 | if len(moment_sel) > 1: 159 | f.write('\n') 160 | continue 161 | try: 162 | moment = moment_sel[0] 163 | f.write( str(moment.XAxis).split('[')[0] + '\t' + 164 | str(moment.YAxis).split('[')[0] + '\t' + 165 | str(moment.ZAxis).split('[')[0] + '\n' ) 166 | except IndexError: 167 | f.write('\n') 168 | print 'Cannot find corresponding Moment Probe' 169 | 170 | 171 | def loop(items, name=None): 172 | ''' 173 | Loop function to recursively save out force/moments from nested folders. 174 | 175 | Parameters 176 | ---------- 177 | items a list of ANSYS solution objects 178 | ''' 179 | save_forces_moments(items, name) 180 | sub_folders = filter(lambda x: x.GetType() == Ansys.ACT.Automation.Mechanical.TreeGroupingFolder, items) 181 | for group in sub_folders: 182 | loop(group.Children, group.Name) 183 | 184 | 185 | # Write out force/moment probes to a file 186 | f = open('Weld results.txt','w') 187 | f.write('Welds\n\n') 188 | f.write('Name\tFX\tFY\tFZ\tMX\tMY\tMZ\n\n') 189 | loop(solution_weld_groups) 190 | f.close() --------------------------------------------------------------------------------