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.
Update a configuration variable or a normal variable with the API?
schepman
Member Posts: 3 ✭
Hi there! New to CAD, programmer by trade. Trying to help a friend automate some things using the OnShape API and could really use some guidance.
I've got a very simple document: https://cad.onshape.com/documents/9572b13e2c65ea469fb4ed48/w/17787c7fb46478b1f546c9ae/e/9b59ef337a4bba9c765a1cfd
I want to be able to use the API to update either a variable in a part studio (possible?) or a configuration variable (seems possible?) and then once either one of those is updated, the model will react to that (I'm sure I'm getting the jargon wrong here..) .. then I want to be able to read out the values of a bunch of variables.
This is a simple model but my friend essentially wants to be able to run design studies where a variable that drives the model gets put through a range of values and then at each stop on the range I can read out all the values of the other variables and we can graph them.
I am currently able to read variables out of the API, which seems simple enough! But I don't understand how to update them. The variables API doesn't have anything like that available. So.. seems like maybe configurations, but.. when I use that API the configuration value I have setup called "configvar" isn't read back.. it's value in "current configuration" is always default. I wonder if that is because configurations don't actually update the UI? If I programmatically update the current configuration and then programmatically read the variables that change... maybe? Would be nice to see the model update.
Is there a supported path for what I'm trying to do? Thanks for ANY guidance!
Chris
0
Answers
To answer the actual question, when you run API calls against a part studio, the configuration is an input parameter., If you do an evaluate feature script to get the value of a variable (or get features), the call is done against a specific config. Configurations aren't like a permanent state in the studio, everything is a bit more 'on the fly'.
This means to achieve your goal, you need to build the configuration string with the configuration definition from /elements/.../configuration, for each configuration and then feed it into either the get features or evaluatefeaturescript end point to get the values.
I copied the doc (here) and configured the two dimensions in the sketch to length_top and length_bottom, as Caden says. Then a call to the /variables API, with the appropriate config, gets you the two measured variables, like so:
https://cad.onshape.com/api/v5/variables/d/7befb16f1f14ebf6af945aab/w/6ae5b044be077ba2a2107f9c/e/42553b807581681133075c75/variables?includeValuesAndReferencedVariables=true&configuration=length_top=160+millimeter;length_bottom=240+millimeter
I just got it working. I called:
https://cad.onshape.com/api/partstudios/d/{DID}/{WVM}/{WID}/e/{EID}/features
Found the right feature. Updated its expression. Then sent it back to:
In a Part Studio, variables are explicitly defined by features. Variable Studios are very similar to Part Studios underneath (at the time of writing), but we treat those variables differently from a UI and API perspective.
There's a simple POST for assigning variables in a Variable Studio. If you want to use the API to set variables in a Part Studio, you'll need to use the /features endpoint.
The /variables GET will retrieve variables from a Part Studio or Variable Studio.
So far I have tried even just posting the example cube from the doumentation, but this didn't get a response.
So far I have tried even just posting the example cube from the doumentation, but this didn't get a response from the API.
Try it here: https://cad.onshape.com/glassworks/explorer/#/Variables/setVariables