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.
Function variables
james_sleeman
Member, Developers Posts: 21 ✭✭
It would be super useful to be able to define a variable as a function, so that we could do something like
#myFunctionVariable(5)
where
#myFunctionVariable(x) = [equation involving x]
use case for example would be in having some standard and centrally configurable adjustment for hole sizes, to achieve dimensionally accurate holes on 3d printers without having to hard-code in your printer/filament's peculiarities we could just dimension our 3mm holes to #hole(3) such that the #hole(x) variable was defined as an equation that produces the correctly adjusted dimension.
#myFunctionVariable(5)
where
#myFunctionVariable(x) = [equation involving x]
use case for example would be in having some standard and centrally configurable adjustment for hole sizes, to achieve dimensionally accurate holes on 3d printers without having to hard-code in your printer/filament's peculiarities we could just dimension our 3mm holes to #hole(3) such that the #hole(x) variable was defined as an equation that produces the correctly adjusted dimension.
1
Comments
Here's a variable that displays a decimal as a fraction (just paste this into the Value field of a variable feature).
function(x){var wholeNumber = floor((x + 1e-11 * meter) / inch);var remainder = x / inch - wholeNumber;var fraction = 1;for (var i = 0; i < 8; i += 1){if (abs(remainder - round(remainder)) < 1e-11) { break;}remainder *= 2;fraction *= 2;}if (fraction == 1){return wholeNumber ~ "\""; } var fractionalPart = round(remainder) ~ "/" ~ fraction;if (wholeNumber == 0){return fractionalPart ~ "\"";}return wholeNumber ~ " " ~ fractionalPart ~ "\""; }
This is definitely one of those "why would you ever do this" sort of situations, as it is way more efficient and simpler to just write a custom feature, but who knows there might be some cases where it's better to just use a variable. One could imagine a variable studio of functions that one might want to use often.
chadstoltzfus@premiercb.com
#pipe_width_at(#soundpipe_length+#soundhole_length)
Any ideas?
Here the design I am experimenting with: https://cad.onshape.com/documents/cace26656a717e831c8ca8ff/w/aa63b2eac792cde7392ff602/e/4c5477d04bbedb839dccd792?configuration=List_5jTWSIBKJ1DnCZ=Standard&renderMode=0&uiState=642c8d1bb05f15165c99ebca
So this works:
name: increment. value: function(x){return x + 1};
But this doesn't:
name: startValue. value: 42
name offsetFromStartValue. value: function(x){return startValue+x;)
^^these two can still be defined, but #offsetFromStartValue(2.0) will fail
Thinking now about building a bigger (Featurescript)-feature that creates essentialy the entire part programmatically - might be easier.
--- old posting ---
I cannot get this to work. Have defined a function as value of a parameter as shown by @chadstoltzfus. Now I try to call it, but it is always shown as invalid.
#pipe_width_at(#soundpipe_length+#soundhole_length)
Any ideas?
Removed link - not sure why my postings are not published when I post links to my public documents? Might have helped to understand the problem...
To my knowledge there isn't a way to reference Part Studio variables inside the scope of the lambda function, though it's worth noting you can send a Part Studio variable as an argument to the function.
Ex:
name: startValue. value: 42
name offsetFromStartValue. value: function(x, y){return y+x * in;)
Invoke #offsetFromStartValue(2in, #startValue)
Will return 44
Though you're probably right, it's going to be much more scalable and easy to manage to just make a custom feature.
chadstoltzfus@premiercb.com