In this example, using the support files, you perform a Structural Optimization analysis in Mechanical using a sequence of python journal commands that will define and solve the analysis. This analysis uses the Topology Optimization - Level Set Based method.
This example begins in the Mechanical application. It requires you to download the following Ansys DesignModeler and python files.
Structural_Optimization_Level_Set.agdb
Structural_Optimization_Level_Set.py
These files are available here.
Procedure
The following procedure assumes you understand how to add a system in Workbench and make changes to system cells and properties as well as saving the support files to a known location.
Add a Static Structural analysis to the Project Schematic. Drag and drop a Structural Optimization system onto the static system as shown.

Right-click the Geometry cell of the static system and select > and select the Structural_Optimization_Level_Set.agdb file.

Right-click the Model cell and select . This action launches the Mechanical application.
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
Structural_Optimization_Level_Set.py.Select the Run Script option (
) from the Editor toolbar.
Scripts Illustrated
In this example, the python file automatically performs the following actions:
#Scenario 1 Store all main tree nodes as variables
GEOMETRY = Model.Geometry
MESH = Model.Mesh
NAMED_SELECTIONS = Model.NamedSelections
CONNECTIONS = Model.Connections
COORDINATE_SYSTEMS = Model.CoordinateSystems
GLOBAL_COORDINATE_SYSTEM = COORDINATE_SYSTEMS.Children[0]
#Scenario 2 Select Unit System
ExtAPI.Application.ActiveUnitSystem = MechanicalUnitSystem.StandardMKS
#Scenario 3 Store Named selections as variable
Fixed_Support_1 = [i for i in NAMED_SELECTIONS.GetChildren[Ansys.ACT.Automation.Mechanical.NamedSelection](True) if i.Name == 'Fixed_Support_1'][0]
Fixed_Support_2 = [i for i in NAMED_SELECTIONS.GetChildren[Ansys.ACT.Automation.Mechanical.NamedSelection](True) if i.Name == 'Fixed_Support_2'][0]
Force = [i for i in NAMED_SELECTIONS.GetChildren[Ansys.ACT.Automation.Mechanical.NamedSelection](True) if i.Name == 'Force'][0]
#Scenario 4 Add mesh method
MESH.ElementSize = Quantity('0.005 [m]')
MESH.UseAdaptiveSizing = False
#Scenario 5 Setup Static Structural analysis
STATIC_STRUCTURAL = Model.Analyses[0]
FIXED_SUPPORT1 = STATIC_STRUCTURAL.AddFixedSupport()
FIXED_SUPPORT1.Location = Fixed_Support_1
FIXED_SUPPORT2 = STATIC_STRUCTURAL.AddFixedSupport()
FIXED_SUPPORT2.Location = Fixed_Support_2
FORCE1 = STATIC_STRUCTURAL.AddForce()
FORCE1.Location = Force
FORCE1.DefineBy = LoadDefineBy.Components
FORCE1.XComponent.Output.DiscreteValues = [Quantity("-1000 [N]")]
SOLUTION1 = STATIC_STRUCTURAL.Solution
TOTAL_DEFORMATION = SOLUTION1.AddTotalDeformation()
EQUIVALENT_STRESS = SOLUTION1.AddEquivalentStress()
#Scenario 6 Setup Topology Opimization analysis
TOPOLOGY_OPTIMIZATION = Model.Analyses[1]
OPTIMIZATION_REGION1 = TOPOLOGY_OPTIMIZATION.Children[1]
OPTIMIZATION_REGION1.BoundaryCondition = BoundaryConditionType.AllLoadsAndSupports
OPTIMIZATION_REGION1.OptimizationType = OptimizationType.TopologyLevelSet
# Mass Constraint
RESPONSE_CONSTRAINT1 = TOPOLOGY_OPTIMIZATION.Children[3]
RESPONSE_CONSTRAINT1.DefineBy = ResponseConstraintDefineBy.Range
RESPONSE_CONSTRAINT1.PercentageToRetainMin = 50
RESPONSE_CONSTRAINT1.PercentageToRetainMax = 70
# Insert Reaction Force Constraint
RESPONSE_CONSTRAINT2 = TOPOLOGY_OPTIMIZATION.AddReactionForceConstraint()
RESPONSE_CONSTRAINT2.Location = Fixed_Support_1
RESPONSE_CONSTRAINT2.AxisSelection = AxisSelectionType.XAxis
RESPONSE_CONSTRAINT2.ReactionForceCriteria = ReactionForceCriteriaType.Sum
RESPONSE_CONSTRAINT2.BoundType = TopoBoundType.LowerBound
RESPONSE_CONSTRAINT2.XComponentMax.Output.DiscreteValues=[Quantity ("500 [N]")]
# Insert Reaction Force Constraint
RESPONSE_CONSTRAINT3 = TOPOLOGY_OPTIMIZATION.AddReactionForceConstraint()
RESPONSE_CONSTRAINT3.Location = Fixed_Support_2
RESPONSE_CONSTRAINT3.AxisSelection = AxisSelectionType.XAxis
RESPONSE_CONSTRAINT3.ReactionForceCriteria = ReactionForceCriteriaType.Sum
RESPONSE_CONSTRAINT3.BoundType = TopoBoundType.LowerBound
RESPONSE_CONSTRAINT3.XComponentMax.Output.DiscreteValues=[Quantity ("100 [N]")]
# Get Tracker and Result
SOLUTION2 = TOPOLOGY_OPTIMIZATION.Solution
SOLUTION_INFORMATION2 = SOLUTION2.SolutionInformation
TOPOLOGY_DENSITY_TRACKER1 = SOLUTION_INFORMATION2.Children[0]
TOPOLOGY_DENSITY1 = SOLUTION2.Children[1]
# Insert Smoothing
SMOOTHING1 = TOPOLOGY_DENSITY1.AddSmoothing()
#Scenario 7 Solve and Review Results
SOLUTION2.Solve(True)
TOPO_DENS1_OV = TOPOLOGY_DENSITY1.OriginalVolume.Value
TOPO_DENS1_FV = TOPOLOGY_DENSITY1.FinalVolume.Value
TOPO_DENS1_PVO = TOPOLOGY_DENSITY1.PercentVolumeOfOriginal
TOPO_DENS1_OM = TOPOLOGY_DENSITY1.OriginalMass.Value
TOPO_DENS1_FM = TOPOLOGY_DENSITY1.FinalMass.Value
TOPO_DENS1_PMO = TOPOLOGY_DENSITY1.PercentMassOfOriginalSummary
This example demonstrates how scripting in Mechanical can be used to automate your actions.