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

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

  • caden_armstrong2
    caden_armstrong2 Member, User Group Leader Posts: 121 ✭✭✭
    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.
  • _anton
    _anton Member, Onshape Employees Posts: 555 image
    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


  • Matt_Shields
    Matt_Shields Member, Onshape Employees Posts: 1,007 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:


  • schepman
    schepman 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.


  • _anton
    _anton Member, Onshape Employees Posts: 555 image
    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.
  • Jonathan_Hutchinson
    Jonathan_Hutchinson Member Posts: 162 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.
  • Jonathan_Hutchinson
    Jonathan_Hutchinson Member Posts: 162 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.
  • Matt_Shields
    Matt_Shields Member, Onshape Employees Posts: 1,007 PRO