Standalone Legend Settings

You can create an object for standalone legend settings, modify its properties, and then copy this object onto an existing result object.

To create the object with the standalone legend settings, you use the command LegendSettings:

legendSettings = Ansys.Mechanical.Graphics.Tools.LegendSettings()

If you want to assign a default unit system, in the command, you would enter the unit system inside the parentheses:

legendSettings = Ansys.Mechanical.Graphics.Tools.LegendSettings("mm")

The object for the standalone legend settings has a default set of band values (from 0 to 9) and a default color scheme (Rainbow). You can set or modify any other properties that you care about. It is also possible to set the minimum and maximum for the standalone legend to mimic the minimum and maximum of a result:

legendSettings.SetMinMax(Quantity(1,'m'),Quantity(100,'m'))

If result-specific legend settings have been exported to an XML file, you can import these settings to create standalone legend settings:

legendSettings = Graphics.ImportLegend("E://file.xml",'m')

Copying Legends

You can copy the legend settings from the current result onto an object for standalone legend settings. Conversely, you can create an object for standalone legend settings and then copy it onto a current result. These capabilities allow you to reuse a set of legend settings in various places.

These methods are available for copy operations:

MethodDescription
MakeCopy()Returns a standalone LegendSettings object that is a copy of the current result's legend.
CopyTo()Copies a standalone LegendSettings object onto the current result's legend.
SetMinMaxGets or sets whether the minimum and maximum values are shown.

Several examples demonstrate how to use these methods.

Example 1

This code creates a standalone LegendSettings object using the current result's legend:

# Navigate to a Total deformation object
totalDeform = DataModel.GetObjectsByName("Total Deformation")[0]
# Make a copy of its legend settings
totDefLegend = Ansys.Mechanical.Graphics.Tools.CurrentLegendSettings().MakeCopy()

Example 2

This code copies a standalone LegendSettings object onto the current result’s legend:

# Create a standalone legend settings object 
legendSetting = Ansys.Mechanical.Graphics.Tools.LegendSettings()
legendSetting.NumberOfBands = 3
legendSetting.SetBandColor(1, Ansys.Mechanical.DataModel.Constants.Colors.Red)

# Navigate to a result object
totalDeform = DataModel.GetObjectsByName("Total Deformation")[0]
totalDeform.Activate()

# Copy the standalone legend to this result
legendSetting.CopyTo(Ansys.Mechanical.Graphics.Tools.CurrentLegendSettings())

These copy operations can also be used for copying legends between different results.

Example 3

This code copies the legend from one result object onto another:

# Navigate to a result object
eqvStress = DataModel.GetObjectsByName("Equivalent Stress")[0]
eqvStress.Activate()

# Make a copy of its legend settings
eqvStressLegend = Ansys.Mechanical.Graphics.Tools.CurrentLegendSettings().MakeCopy()

# Navigate to a different result object
principalStress = DataModel.GetObjectsByName("Maximum Principal Stress")[0]
principalStress.Activate()

# Copy the previous legend to this result
eqvStressLegend.CopyTo(Ansys.Mechanical.Graphics.Tools.CurrentLegendSettings())