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.

Alternative to lots of hidden feature params?

EvanReeseEvanReese Member, Mentor Posts: 2,135 ✭✭✭✭✭
I'm doing a lot of UI logic that changes based on user selections. I'm doing it by using hidden boolean parameters and toggling them with editing logic. For example, the UI might look different depending on whether someone picks a vertex, edge, non-planar face, planar face, or nothing. My issue, is that there are getting to be quite a few of booleans. I also have some hidden integers for point indexes. I'd really love to be able to just have one hidden parameter that's a map of all of my booleans, then address it in the UI. I'd fill it with something like

{"pointIndex" : 1, "noSelection" : false, "isVertex" : false, "isEdge" : true, "isFace" : false, "isPlanarFace" : false}

Is there a way to do something like that? I can get by without it, but it just gets clickity-clackity to write, and fluffs up the code making it harder to read.
Evan Reese

Best Answers

  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,683
    Answer ✓
    You have to remember that the UI is a predicate so it cannot react to any logic created in the body of the script (apart from Editing Logic). 
    Senior Director, Technical Services, EMEAI
  • kevin_o_toole_1kevin_o_toole_1 Onshape Employees, Developers, HDM Posts: 565
    Answer ✓
    if (definition.UI.boolean1)
    {
        *Other parameters to show*
    }
    Yes, in order to get conditional visibility you need the booleans listed out explicitly. Our UI only has logic for dealing with those explicitly listed parameters. It sounds like you've made the simplest functional solution.

Answers

  • EvanReeseEvanReese Member, Mentor Posts: 2,135 ✭✭✭✭✭
    I just realized I can probably use a hidden enum instead of a bunch of booleans for isVertex, isEdge etc. It would still be nice to have a single place to put all of the info that the UI references though.
    Evan Reese
  • caden_armstrong2caden_armstrong2 Member, User Group Leader Posts: 127 ✭✭✭
    Closest solution you'd have would be to put all of the hidden parameters into a single predicate which gets called in your precondition.
    Functionally, I don't think it would really differ too much from having a hidden parameter map as you are suggesting,
    as in both cases you still need to have an if statement checking a map variable.

    If your biggest gripe is a messy precondition, moving it away into a precondition would at least organize things.
  • EvanReeseEvanReese Member, Mentor Posts: 2,135 ✭✭✭✭✭
    That's a good idea, Caden. Thanks!
    Evan Reese
  • kevin_o_toole_1kevin_o_toole_1 Onshape Employees, Developers, HDM Posts: 565
    You can also have a single "isAnything" field which contains a map/array/whatever data structure you want. That way you don't need to keep modifying the precondition to add new non-user-visible fields.
  • EvanReeseEvanReese Member, Mentor Posts: 2,135 ✭✭✭✭✭
    edited June 2022
    You can also have a single "isAnything" field which contains a map/array/whatever data structure you want. That way you don't need to keep modifying the precondition to add new non-user-visible fields.
    Thanks, Kevin. I'd hoped that would work, but I'm not sure it's what I'm looking for. I'm wanting to hide and show UI parameters based on the contents, but it seems like I'm not able to access anything in the map. What I'd love to do is something like this:

    annotation { "Name" : "UI settings"}
    isAnything(definition.UI);

    then in my precondition something like

    if (definition.UI.boolean1)
    {
    *Other parameters to show*
    }

    I struggle with code formatting on the forums but here's an example script
    https://cad.onshape.com/documents/74b868c81ffd51b8b4a9c6a5/w/facc72e9362395db8a26dc6b/e/3cbb4cc8c851cbb31b6aa7b0
    Evan Reese
  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,683
    Answer ✓
    You have to remember that the UI is a predicate so it cannot react to any logic created in the body of the script (apart from Editing Logic). 
    Senior Director, Technical Services, EMEAI
  • EvanReeseEvanReese Member, Mentor Posts: 2,135 ✭✭✭✭✭
    NeilCooke said:
    You have to remember that the UI is a predicate so it cannot react to any logic created in the body of the script (apart from Editing Logic). 
    Yeah, I get that. I'd just hoped there would be a way to store a lot of booleans or enums inside of one isAnything param and access them, but I think I can't get inside of an array or map within the predicate. It seems like only booleans, enums, and lookup tables can drive visibility of other parameters. Is that right?
    Evan Reese
  • kevin_o_toole_1kevin_o_toole_1 Onshape Employees, Developers, HDM Posts: 565
    Answer ✓
    if (definition.UI.boolean1)
    {
        *Other parameters to show*
    }
    Yes, in order to get conditional visibility you need the booleans listed out explicitly. Our UI only has logic for dealing with those explicitly listed parameters. It sounds like you've made the simplest functional solution.
  • EvanReeseEvanReese Member, Mentor Posts: 2,135 ✭✭✭✭✭
    Thanks folks. For future readership, if you have a number of independent things to control in the UI via editing logic, use booleans. If you have a lot of mutually exclusive options, you can use an enum. You can't access anything in a container (i.e. map, array etc) within the precondition, so creating a map of booleans won't work.
    Evan Reese
Sign In or Register to comment.