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.

Updating Variables through Python API

alex_kagayalex_kagay Member Posts: 2
Hi,

I have started a project where I would like to do a GET to get the variables of a part, do a mathematical operation, and then POST the updated variables. I have gotten the GET and the mathematical operation, and am struggling to actually POST. My document is linked here.

I have obtained the url from the Glassworks API Explorer, but I am not sure what to use for the Request body or how to pass that into the POST statement.

When I use just the Glassworks API explorer, I get an error 404, not found.

My code for posting is:

        fixed_url = '/api/v6/variables/d/did/w/wid/e/eid/variables'
        fixed_url = self.fix_url(fixed_url,self.element.did,self.element.wvmid,self.element.eid,999)
        params = {}
        headers = {'Accept': 'application/vnd.onshape.v1+json; charset=UTF-8;qs=0.1', 'Content-Type': 'application/json'}

        vars = json.loads(response.content)
        value1 = "1.5 meter"
        vars[0]["variables"][0]["value"] = value1
        payload = {"content":vars}


        response = requests.post(self.base+fixed_url,
                           params=params,
                           auth=self.api_keys,
                           headers=headers,
                           payload=payload)


I would appreciate any insight to what I am doing wrong, thank you!

Comments

  • Matt_ShieldsMatt_Shields Member, Onshape Employees Posts: 415
    I think using POST with the variables endpoint only works with Variable Studios.

    To update variable features, I think you need to use the features endpoint.

    https://cad.onshape.com/api/partstudios/d/{DID}/w/{WID}/e/{EID}/features/featureid/{FID}


  • philip_kristoffersenphilip_kristoffersen Member Posts: 5
    I have the exact same problem both when variables are in variables studio and not
  • Matt_ShieldsMatt_Shields Member, Onshape Employees Posts: 415
    this worked for me:

    import requests 
    
    # Get all of the variables in the Part Studio
    response = requests.get(
        "https://cad.onshape.com/api/variables/d/{}/{}/{}/e/{}/variables".format(
            DID, WVM, WID, EID
        ), 
        auth=(API_ACCESS, API_SECRET), 
        headers={
            "Accept": "application/json;charset=UTF-8; qs=0.09", 
            "Content-Type": "application/json"
        }
    ) 
    response = response.json()
    # Grab just the variables from the response
    variables = response[0]["variables"]
    
    # Update the angles, adding 5 degrees
    for var in variables:
      if var['type'] == "ANGLE":
              arr = var['expression'].split(" ")
              arr[0] = int(arr[0]) + 5
              var['expression'] = str(arr[0]) + " deg"
              print("Angle updated to " + var['expression'])
    
    payload = response[0]["variables"]
    
    # Post the variables back
    response = requests.post(
        "https://cad.onshape.com/api/variables/d/{}/w/{}/e/{}/variables".format(
            DID, WID, EID
        ), 
        auth=(API_ACCESS, API_SECRET), 
        headers={
            "Accept": "application/json;charset=UTF-8; qs=0.09", 
            "Content-Type": "application/json"
        },
        json=payload
    ) 
    # Response 204 is success with no content
    print<span>(response)</span><br><br>

Sign In or Register to comment.