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.

Options

Update a configuration variable or a normal variable with the API?

schepmanschepman 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 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

Answers

  • Options
    caden_armstrongcaden_armstrong Member, User Group Leader Posts: 127 ✭✭✭
    I am actually working on a product that does exactly this, if you are interested in trying out the closed beta, send me a PM.

    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.
  • Options
    _anton_anton Member, Onshape Employees Posts: 292
    Check it out:

    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


  • Options
    Matt_ShieldsMatt_Shields Member Posts: 231 PRO
    Just to clarify: if you want to actually watch the model update on your screen, I don't think you can use configurations.  As Anton pointed out, you can make an API call and pass configuration variables in the query string.  You'll get back a response, based on the configuration, but you won't see the model update on your screen.  If you want to do that (which I get), then I think you'll need to use the API to modify dimensions.  Also, doable.

    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:


  • Options
    schepmanschepman Member Posts: 3
    Sorry for the late reply-- This is truly SO helpful, thank you!

    I was able to get this working for modifying constraints of a sketch.. my user made it clear they want to be able to update a variable. Are variables features too? There is a variable API but.. it doesn't seem to want to update variables.

    Thanks again for all the help. It seems like it should be easy, but software is crazy sometimes.


  • Options
    _anton_anton Member, Onshape Employees Posts: 292
    Here are the docs for our /variables APIs: https://cad.onshape.com/glassworks/explorer/#/Variables

    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.
  • Options
    Jonathan_HutchinsonJonathan_Hutchinson Member Posts: 65 PRO
    Hi Anton, I was trying to receive variables but I haven't been able to do so. I'm not finding it to be so simple. Could some specific steps be made available to achieve changing a part studio variable?

    So far I have tried even just posting the example cube from the doumentation, but this didn't get a response.
  • Options
    Jonathan_HutchinsonJonathan_Hutchinson Member Posts: 65 PRO
    Hi Anton, I was trying to receive variables but I haven't been able to do so. I'm not finding it to be so simple. Could some specific steps be made available to achieve changing a part studio variable?

    So far I have tried even just posting the example cube from the doumentation, but this didn't get a response from the API.
  • Options
    Matt_ShieldsMatt_Shields Member Posts: 231 PRO
Sign In or Register to comment.