The user wants to automatically change the colour of probe labels to match the colour of the corresponding value of the legend for a result plot. Like in the image below:
The following snippet contains a function that identifies the colour of the corresponding value based on the currently active legend (result object):
def compute_legend_color(value): legend = Ansys.Mechanical.Graphics.Tools.CurrentLegendSettings() value_band_index = 0 for band_index in range(legend.NumberOfBands): if legend.GetLowerBound(band_index) <= value: value_band_index = band_index color = legend.GetBandColor(value_band_index) # BGR-encoded uint red = (color & 0xFF) >> 0 green = (color & 0xFF00) >> 8 blue = (color & 0xFF0000) >> 16 # each channel is an 8-bit unsigned integer (range is [0, 255]) return Ansys.ACT.Common.Graphics.Color(red = red, green = green, blue = blue, alpha = 255)
The user needs to retrieve the target result object in the res object and iterate over the probes of interest. Assign the output of the compute_legend_color() function to the label.Colour property. Here's an example:
res
probes
compute_legend_color()
label.Colour
res = ExtAPI.DataModel.GetObjectsByName('Equivalent Stress')[0] # your result object of interest res.Activate() # CurrentLegendSettings() is only available when a result or varying load object is active probes = Graphics.LabelManager.GetObjectLabels(res) for label in probes: # Graphics.LabelManager.GetObjectLabels(res): if label.GetType() != Ansys.Mechanical.Graphics.Labels.ProbeLabel: continue # avoids general user annotations (LabelManager.CreateLabel(...) or Home > Insert > Annotation) label.Color = compute_legend_color(label.Value)