How can I modify a Chart in Mechanical?

oliadag
oliadag Member Posts: 4
First Comment
**

Hello everyone,

I want to modify a chart using Mechanical Scripting, and I managed to get the settings I wanted with this function:

def postprocessing():

    charts = ExtAPI.DataModel.GetObjectsByName("Chart")

    chart = charts[0]  # Assuming there's only one chart

    chart.VisibleProperties[1].InternalValue = 5
    chart.VisibleProperties[5].InternalValue = 'Deflection'
    chart.VisibleProperties[6].InternalValue = 'Force'
    chart.VisibleProperties[9].InternalValue = 'Omit'
    chart.VisibleProperties[10].InternalValue = 'Omit'
    chart.VisibleProperties[11].InternalValue = 'Display'
    chart.VisibleProperties[12].InternalValue = 'Omit'

postprocessing()

While this script works, I'm looking for a way to modify the chart properties more clearly without having to rely on Visible Properties. I think it'd be easier for someone else to follow the script if it didn't rely so much on indexing.

Any ideas?

Thank you!

Olia

Comments

  • Erik Kostson
    Erik Kostson Member, Moderator, Employee Posts: 275
    50 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭

    Hi - that looks pretty good - we did not have an example of that (chart mod.), so thank you.

    Erik

    PS: I am not sure if there is a better way than this to set these fields :)

  • Landon Mitchell Kanner
    Landon Mitchell Kanner Member, Employee, GitHub-issue-creator Posts: 316
    50 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭
    edited November 4

    One option for better readability is to use the PropertyByName method to select the properties. You can first print the VisibleProperties to see a list of the property names,:

    But this may have robustness issues, depending on your workflow.

    Note that the axis labels have direct APIs:
    chart.XAxisLabel = 'Deflection'

  • oliadag
    oliadag Member Posts: 4
    First Comment
    **

    Thank you both for your replies!

    Today I noticed something strange with the script, though. When I create a chart, change the scoping manually, and run the script below, the script doesn’t seem to work at all. Interestingly, if I make a manual adjustment in the chart's details (e.g., modifying X Axis-Chart Controls), the script then functions correctly.

    charts = ExtAPI.DataModel.GetObjectsByName("Chart")
    chart = charts[0]
    
    chart.PropertyByName("IndependentAxisKey").InternalValue = 5
    chart.XAxisLabel = 'Deflection'
    chart.YAxisLabel = 'Force'`
    

    It seems like manually changing something in the chart’s details triggers a refresh that allows the script to work as expected. Does anyone know why this might be happening?

  • Landon Mitchell Kanner
    Landon Mitchell Kanner Member, Employee, GitHub-issue-creator Posts: 316
    50 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭

    It seems like manually changing something in the chart’s details triggers a refresh that allows the script to work as expected. Does anyone know why this might be happening?

    I couldn't reproduce this behavior (running 2024R2). But perhaps adding Tree.Refresh() will help?