Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.

First time visiting? Here are some places to start:
  1. Looking for a certain topic? Check out the categories filter or use Search (upper right).
  2. Need support? Ask a question to our Community Support category.
  3. Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
  4. Be respectful, on topic and if you see a problem, Flag it.

If you would like to contact our Community Manager personally, feel free to send a private message or an email.

Is there a way to change values of variables depending on an Enum selection?

I am relatively new to custom FeatureScripts and I am trying to create a list of variables. I would like the value of those variables to change depending on what choice I have made in the dropdown menu.

I want the choices inside the blue bracket to change the value of those inside of the grey bracket.

Best Answer

  • Evan_ReeseEvan_Reese Member Posts: 2,060 PRO
    edited November 2021 Answer ✓
    Actually, thinking more about it you could make each variable it's own map and just use the enum value as the key. It has to be made into a string to work, but I think I like this way a lot more. Here's an example.
    Evan Reese / Principal and Industrial Designer with Ovyl
    Website: ovyl.io

Answers

  • Evan_ReeseEvan_Reese Member Posts: 2,060 PRO
    edited November 2021
    There's probably a more elegant way, but you could do it with if statements.

    if (definition.myEnum == materialType.SHEET)
    {
    v.GA30 = 0.0120*inch;
    // and so on...
    }
    else if (definition.myEnum == materialType.STAINLESS)
    {
    v.GA30 = 0.0100*inch;
    // and so on...
    }

    Evan Reese / Principal and Industrial Designer with Ovyl
    Website: ovyl.io
  • Evan_ReeseEvan_Reese Member Posts: 2,060 PRO
    edited November 2021 Answer ✓
    Actually, thinking more about it you could make each variable it's own map and just use the enum value as the key. It has to be made into a string to work, but I think I like this way a lot more. Here's an example.
    Evan Reese / Principal and Industrial Designer with Ovyl
    Website: ovyl.io
  • david_dalton526david_dalton526 Member Posts: 3
    edited November 2021
    Actually, thinking more about it you could make each variable it's own map and just use the enum value as the key. It has to be made into a string to work, but I think I like this way a lot more. Here's an example.
    Thank you so much. I previously thought of using a Boolean expression but I knew there had to be a better way. I haven't messed with strings too much but, I can see that it is a very helpful tool to have.
Sign In or Register to comment.