Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.
First time visiting? Here are some places to start:- Looking for a certain topic? Check out the categories filter or use Search (upper right).
- Need support? Ask a question to our Community Support category.
- Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
- 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?
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?
0

Comments
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?
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.
The Onsherpa | Reach peak Onshape productivity
www.theonsherpa.com
That makes sense. I guess I'm just really hoping there's some perfect application for it because that would excite me.
The Onsherpa | Reach peak Onshape productivity
www.theonsherpa.com
setVariable(context, "sm", function(x) { return x + 1; } ); // Works setVariable(context, "sm-guage", function(x) { return SM_Table[x].Size; } ); // doesn't workIt won't let me read from a map thoughOtherwise 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); } );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?
The Onsherpa | Reach peak Onshape productivity
www.theonsherpa.com
My issue was I had a "-" in the name, apparently that is not a valid variable name. My bad
Sorry if its a dumb question - I'm just starting to dig into featurescript.
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 }