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.

Example of FeatureScript functions in Variable features?

Evan_ReeseEvan_Reese Member Posts: 239 PRO
I'm looking at the help documentation for variables here and I see that custom functions can be written with Featurescript syntax inside of a Variable feature. I understand the example given, but I'm curious if anyone has any examples of a time they found this functionality particularly handy. It seems like a powerful capability, but I'm having a hard time imagining a use-case for myself. Does anyone have anything cool they want to show?
Evan Reese / Agency Owner and Industrial Designer
Website: fractalmade.com
Instagram: @evan.reese.designs

Comments

  • tim_hess427tim_hess427 Member Posts: 278 PRO
    That is cool! I didn't realize we could do that - thanks for pointing it out. 

    I'm trying to think of examples, but anything I can come up with involving calculations could just as easily be done with a combination of variables and configurations. So, I would think this would be most valuable in situations where you want to avoid typing the same expression multiple times or if there are things you could do in a function that go beyond the simple calculations?  Would you be able to do queries or evaluations within these functions?
  • Evan_ReeseEvan_Reese Member Posts: 239 PRO
    @tim_hess427
    That's kind of where I'm stuck. I can't think of a function I'd run more than once or twice, in which case I may as well just type it as an equation, but the fact that it's there has me very curious about the scenarios where it's indispensable. There's got to be a few, right?

    I can't say for sure, but I don't think queries or evals are possible in the functions.
    Evan Reese / Agency Owner and Industrial Designer
    Website: fractalmade.com
    Instagram: @evan.reese.designs
  • konstantin_shiriazdanovkonstantin_shiriazdanov Member Posts: 993 ✭✭✭✭✭
    I think we have this capability because there is a kind of input field which accepts any valid featurescript expression (isAnything() predicate in featurescript). So that lambda or ternary operator can be used as soon as they all are valid language constructions, and it was for free and they didn't requare any additional efforts to provide functionality for exporting lambda's into context variables, but if they wanted to restrict some type of expression as input it would requare additional job so it's just win-win.
  • Evan_ReeseEvan_Reese Member Posts: 239 PRO
    @konstantin_shiriazdanov
    That makes sense. I guess I'm just really hoping there's some perfect application for it because that would excite me.
    Evan Reese / Agency Owner and Industrial Designer
    Website: fractalmade.com
    Instagram: @evan.reese.designs
  • john_mcclaryjohn_mcclary Member, Developers Posts: 2,709 PRO
     setVariable(context, "sm", function(x) {  return x  + 1; } );                  // Works
     setVariable(context, "sm-guage", function(x) {  return SM_Table[x].Size; } );  // doesn't work
    It won't let me read from a map though :(

    Otherwise it would be handy to make a toolkit FS that holds a bunch of common functions as vars.

    for example, the pitch dia of a sprocket.
    I always just type it out long form in the dimension, but there are only two parameters, chain pitch and tooth count.
    so it would be cool to just type #Sprocket_Pitch_dia(.75,11)  and not have to remember if you need to multiply the pitch by sin() or divide.. (I get it wrong half the time)
    setVariable(context, "Sprocket_Pitch_Dia", function(x,y) {  return x/sin(180*degree/y); } );







  • Evan_ReeseEvan_Reese Member Posts: 239 PRO
    @john_mcclary
    now, this is an interesting idea! I like the toolkit concept. If someone works with a particular system that has common equations, then a feature could be written to turn each equation into a function to make downstream calculations way faster and less error-prone.

    Bummer that the map didn't work. Any ideas on a workaround?
    Evan Reese / Agency Owner and Industrial Designer
    Website: fractalmade.com
    Instagram: @evan.reese.designs
  • john_mcclaryjohn_mcclary Member, Developers Posts: 2,709 PRO
    Oh, actually it does work with a map.

    My issue was I had a "-" in the name, apparently that is not a valid  variable name. My bad
  • tim_hess427tim_hess427 Member Posts: 278 PRO
    @john_mcclary Are you defining the map in a separate featurescrip? 

    Sorry if its a dumb question - I'm just starting to dig into featurescript. 
  • john_mcclaryjohn_mcclary Member, Developers Posts: 2,709 PRO
    Yea, In this case I do have a featurescript that is basically a custom bend table where it gives a bend radius and stock size by inputing the Guage of the material

    But you could make your map in the same file if you want.
    export const SM_Table =
    { 
        .25 : { Name : "1/4\" (.25)", Size : .25, Bend : .262 },
        7 : { Name : "7 Gauge  (.1793)", Size : .1793, Bend : .262 },
        10 : { Name : "10 Gauge (.1345)", Size : .1345, Bend : .164 },
        11 : { Name : "11 Gauge (.1196)", Size : .1196, Bend : .164 },
        12 : { Name : "12 Gauge (.1046)", Size : .1046, Bend : .164 },
        14 : { Name : "14 Gauge (.0747)", Size : .0747, Bend : .079 },
        16 : { Name : "16 Gauge (.0598)", Size : .0598, Bend : .079 },
        18 : { Name : "18 Gauge (.0478)", Size : .0478, Bend : .079 }

Sign In or Register to comment.