Using a solved project, this example examines APIs for result post processing and graphical manipulation actions. This example requires you to download the following project and python files.
Mechanical_Post_Example.wbpz
Mechanical_Post_Example.py
These files are available here.
Procedure
Open the project file Mechanical_Post_Example.wbpz in the Workbench application.
Right-click the Model cell in the Project Schematic and the Edit option. This opens the project in Mechanical.
Select the Automation tab and select the Scripting option to open the Mechanical Scripting pane.
Select the Open Script option (
) from the Editor toolbar. Navigate to the proper folder location and select Mechanical_Post_Graphics_Example_001.py.
Select the Run Script option (
) from the Editor toolbar.
Scripts Illustrated
In this example, the python file automatically performs the following actions:
# -*- coding: UTF-8 -*- #Scenario 1: Set up the Tree objects items and API base definitions. This step is necessary for all the API functions that follow. These definitions will be used throughout the current example. Definitions for Geometry, Mesh, Connections, Solution Object, Legend etc., are covered here. GEOM=Model.Geometry MESH=Model.Mesh STATIC_STRUCTURAL=Model.Analyses[0] SOLUTION=STATIC_STRUCTURAL.Solution CAMERA=Graphics.Camera GRAPHICS_SETTINGS= Ansys.Mechanical.Graphics.GraphicsImageExportSettings() LEGEND_SETTINGS=Ansys.Mechanical.Graphics.Tools.CurrentLegendSettings() GLOBAL_SETTINGS=Graphics.GlobalLegendSettings VIEW_OPTIONS=Graphics.ViewOptions FOLDER_PATH='E:\\temp' #Scenario 2: Insert results. This step would insert the results that we would use to demonstrate the different API actions. TOTAL_DEFORMATION_RESULT=SOLUTION.AddTotalDeformation() #Scenario 3: Setup the units system. ExtAPI.Application.ActiveUnitSystem=MechanicalUnitSystem.StandardMKS #Scenario 3: Setup the units system. Testing.TBScenario('Setup the unit system') ExtAPI.Application.ActiveUnitSystem=MechanicalUnitSystem.StandardMKS #Scenario 4: Setup proper contact settings. Testing.TBScenario("Set previous default contact formulation as per 2023 R2") CONN_GRP = ExtAPI.DataModel.Project.Model.Connections CONTS = CONN_GRP.Children[0] CONT01 = CONTS.Children[0] CONT02 = CONTS.Children[1] CONT01.ContactFormulation=ContactFormulation.AugmentedLagrange #Scenario 5: Clear and Solve Testing.TBScenario('Clear and Solve') SOLUTION.ClearGeneratedData() SOLUTION.Solve() #Scenario 6: Change the Camera angles, Orientation and Export images. Change the export settings and repeat for a few different options. #To fit the model to screen. TOTAL_DEFORMATION_RESULT.Activate() CAMERA.SetFit() #To change the view to Front, back, Top etc., and export images with default settings. The images can be saved at a location of your choice. In this example it is shown as FOLDER_PATH CAMERA.SetSpecificViewOrientation(ViewOrientationType.Front) Graphics.ExportImage(FOLDER_PATH +'\\ DEF1.png', GraphicsImageExportFormat.PNG, GRAPHICS_SETTINGS) CAMERA.SetSpecificViewOrientation(ViewOrientationType.Back) Graphics.ExportImage(FOLDER_PATH+'\\DEF2.png', GraphicsImageExportFormat.PNG, GRAPHICS_SETTINGS) CAMERA.SetSpecificViewOrientation(ViewOrientationType.Right) Graphics.ExportImage(FOLDER_PATH+'\\DEF3.png', GraphicsImageExportFormat.PNG, GRAPHICS_SETTINGS) CAMERA.SetSpecificViewOrientation(ViewOrientationType.Left) Graphics.ExportImage(FOLDER_PATH+'\\DEF4.png', GraphicsImageExportFormat.PNG, GRAPHICS_SETTINGS) CAMERA.SetSpecificViewOrientation(ViewOrientationType.Top) Graphics.ExportImage(FOLDER_PATH+'\\DEF5.png', GraphicsImageExportFormat.PNG, GRAPHICS_SETTINGS) CAMERA.SetSpecificViewOrientation(ViewOrientationType.Bottom) Graphics.ExportImage(FOLDER_PATH+'\\DEF6.png', GraphicsImageExportFormat.PNG, GRAPHICS_SETTINGS) CAMERA.SetSpecificViewOrientation(ViewOrientationType.Iso) Graphics.ExportImage(FOLDER_PATH+'\\DEF7.png', GraphicsImageExportFormat.PNG, GRAPHICS_SETTINGS) #Orient the model to non-standard orientation #Change the focal point (Works like Pan) CAMERA.FocalPoint = Point([0.050000, 0.150000, 0.180000], 'm') #Change the camera angle (Works like Rotate) CAMERA.ViewVector = Vector3D(0.75331, 0.58096, -0.308235) CAMERA.UpVector = Vector3D(-0.523344, 0.813377, 0.254025) #Change the height and width of Scene (Works like Zoom) CAMERA.SceneHeight = Quantity(0.477099, 'm') CAMERA.SceneWidth = Quantity(0.358609, 'm') #Define the Image export settings. First define Image resolutions GRAPHICS_SETTINGS.Width = 1920 GRAPHICS_SETTINGS.Height = 1080 #Define whether image would contain legend or Image only GRAPHICS_SETTINGS.Capture=GraphicsCaptureType.ImageOnly #Remove the defaults and set the background to White GRAPHICS_SETTINGS.CurrentGraphicsDisplay = False GRAPHICS_SETTINGS.Background=GraphicsBackgroundType.White Graphics.ExportImage(FOLDER_PATH+'\\DEF8.png', GraphicsImageExportFormat.PNG, GRAPHICS_SETTINGS) #Orient the legend horizontally, hide ruler, date and Time and triads. GLOBAL_SETTINGS.LegendOrientation=LegendOrientationType.Horizontal GLOBAL_SETTINGS.ShowDateAndTime=False VIEW_OPTIONS.ShowTriad=False VIEW_OPTIONS.ShowRuler=False #Scenario 7: Change the Legend to display with different bands, scale, Color Scheme etc., and export those images as well. #Change the number of bands in the legend to 4, color scale to Greyscale and change upper bound value of band 1 to a different value than default. LEGEND_SETTINGS.NumberOfBands=4 LEGEND_SETTINGS.ColorScheme=LegendColorSchemeType.GrayScale LEGEND_SETTINGS.SetUpperBound(1,Quantity(0.4,'mm')) #Scenario 8: Create section plane CAMERA.SetSpecificViewOrientation(ViewOrientationType.Right) section_plane = Ansys.Mechanical.Graphics.SectionPlane(Point([0.050000, 0.160203, 0.180000], Graphics.Unit), Vector3D(-2.68596e-18, 0.849285, 0.527934), 'Section Plane1', SectionPlaneType.AlongDirection, True) Graphics.SectionPlanes.Add(section_plane) #Scenario 9: Export results as Text file/csv to User defined location. TOTAL_DEFORMATION_RESULT.Activate() TOTAL_DEFORMATION_RESULT.ExportToTextFile(FOLDER_PATH+'\\Txt1.txt') #Scenario 10: Resetting to defaults. Execute this to unmake all the changes made in previous scenarios LEGEND_SETTINGS.Reset() Graphics.SectionPlanes.RemoveAt(0) GLOBAL_SETTINGS.ShowDateAndTime=True VIEW_OPTIONS.ShowTriad=True VIEW_OPTIONS.ShowRuler=True
Summary
This example demonstrates how scripting in Mechanical can be used to automate your actions.