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.

Update feature using API and Python

anna_bengtsson419anna_bengtsson419 Member Posts: 3
edited October 2018 in Data management

Hi!

I’m new to use Onshape REST API, or API overall. I’m trying to modify a feature (a feature I’ve created with Feature Script) by calling the update feature API with python. But I don’t understand what my functions input/ouput should be, how do I understand that from the documentation? My feature have different parameters I want to change, see picture attached. This is my function in python so far:

def update_feature(self, did, wid ??):

return self.__api.request('post', '/api/partstudios/d/' + did + '/w/' + wid + '/e/' + eid + '/features' + fid ??)

I’ve downloaded Onshapepy so I’m just adding the function to the client module.

Thank you in advance!


Tagged:

Comments

  • Jake_RosenfeldJake_Rosenfeld Moderator, Onshape Employees, Developers Posts: 1,646
    @anna_bengtsson419

    @ethan_keller924 will be able to help! I've sent him a message for you.
    Jake Rosenfeld - Modeling Team
  • ethan_keller924ethan_keller924 Member, csevp Posts: 42 PRO
    Hi @anna_bengtsson419,

    The best way to do what you're asking for is to build the feature you want to add in the UI, and then use "get feature list" to see the structure of your built feature. Then, if you can call "add feature" using that structure in the API. This works for even custom features. Try that out, and let me know if you run into issues!

  • anna_bengtsson419anna_bengtsson419 Member Posts: 3
    @ethan_keller924

    Thank you for your answer, i appreciate it! I think I understand that from the developer portal and I've been able to get the feature list from my part studio with your Onshapepy.

    My problem is that I don't understand how to write my own function and how to define it when I call it. I can read about the structure in the developer portal, but I don't really understand it. As I said, new to API and this kind of programming. 

    This i what I have so far. 

    Function: 

    def update_feature(self, did, wid, eid):
            return self.__api.request('post', '/api/partstudios/d/' + did + '/w/' + wid + '/e/' + eid + '/features')

    And when i call it:

    from onshapepy.core.client import c
    update_feature = c.update_feature(did, wid, eid)

    I understand I've missed some, but I do not really understand what. Or where I can learn. 

    Thank you in advance!

  • ethan_keller924ethan_keller924 Member, csevp Posts: 42 PRO
    Hi @anna_bengtsson419,

    The update feature endpoint is POST request that takes a body. Please look in the API explorer to experiment and see how to format it correctly. Right now you're not passing in a body to the rest call. 

    Hope that helps!
    -Ethan
  • billy2billy2 Member, OS Professional, Mentor, Developers, User Group Leader Posts: 2,068 PRO
    edited October 2018
    @anna_bengtsson419 & @ethan_keller924

    I do what ethan does. It's important to replace the entire leaf vs. one small part of the leaf. It may seem like more data traffic, but in the end it's easier to manage. Download it, modify it and then put it back (or parts of it).

    I usually don't send the whole object back and tend to test and pick a leaf that's 1 or 2 levels from the top. I don't think this is considered good practice though.

    I use api explorer a lot to test what works and what won't. When my process is working, I hard code it. But, I always start in the api explorer.


  • billy2billy2 Member, OS Professional, Mentor, Developers, User Group Leader Posts: 2,068 PRO
    edited November 2018
    @anna_bengtsson419 I'm not using python, but here's a post showing how to use the api explorer.

    So I want to run 'update configuration' from api explorer:


    Click on 'try it' and below will show up:


    The  'update configuration'  is a post and has a json request body that needs the correct information. What's shown is the format and if you click on the 'make api call' button you'll get a 404 error because this isn't the correct data. So, how do you get the right data? Go get it.

    I run the get configuration api call and print it out on my webpage:


    I copy this response into my copy buffer using command-c and paste it into the request body:




    With the proper request body, you'll get a status 200 and you're talking to the server. The server likes you now.

    You can use the api to get and copy the response, then try update and paste in the response. Me, I print the object out on my web page while developing because I'm constantly pick stuff out of it. Before deploying, shut it off.

    You can now change values in the request body and see what comes back. You just bang on the server until you get it to give you what you want. In my case I have to change 'value' and 'expression'. Why? this stuff isn't documented and I doubt it ever will be. You have to test until it works. I've never broken a server and you won't get a status 200 with something that's wrong.

    The get response object returns both "configurationParameters" & "currentConfiguration" and I wanted to omitted the "configurationParameters" in the update configuration request body. This "configurationParameters" leaf isn't really required for the api call update configuration. It stopped working. So I tried to omit it and save on transmission overhead but couldn't.

    Send what you get. Bang on it and make the server work for you. Once you figure it out, hard code it, in your case, in python.



  • Matteo_GazzinMatteo_Gazzin Member Posts: 4
    Totally agree with billy, why nothing is documented about that? 

    Anyway, back in November I was able to change the configuration using the same endpoint you used.
    That API is now deprecated and the new one is reachable at:
    /api/elements/d/../w/.../e... 

    If you try to use it, the configuration is changed correctly but the geometry is not updated lively as it was before (meaning that the visualization in onshape is not updated at all). If you refresh the page, the geometry is updated correctly. 

    Anyway, I don't care about visualization update but I want to export the updated STL or PARASOLID and that does not work correctly. 
    If I try to export the STL or PARASOLID through the respective API calls, the result does not correspond with the current geometry visualized in OnShape.

    On the other end, the Export Part feature (right click on part, export, select STL or parasolid) works correctly and the geometry corresponds to the one visualized in OnShape.

    So it looks like changing the configuration is not enough to update the visualized geometry and retrieve it(while it was back in november). 

    Am I missing something?

    Thanks, 
    Matteo


  • Ethan_KEthan_K Member, Onshape Employees Posts: 57
    Hi @Matteo_Gazzin

    A few weeks ago we changed how we handle current configurations. Instead of storing it with a new microversion, we now allow multiple users (and even the same user with multiple tabs open) to view different configurations applied to the part. A side effect of this change is that the currentConfiguration key within the getConfiguration response no longer makes sense - because a single microversion no longer has one, set current config. Therefore, to maintain backwards compatibility, we return the default configuration (specified within the configurationParameters key) as the currentConfiguration, and we added an optional configuration query parameter to all endpoints that need to have the ability to specify both microversion and configuration. Therefore, to get an stl (or any other resource, for that matter) with a particular configuration applied, you need to create a URL like this one: 

    https://cad.onshape.com/api/partstudios/d/cca81d10f239db0db9481e6f/v/aaa25d18038e06f0b2964e2d/e/69c9eedda86512966b20bc90/stl?configuration=List_UKkGODiz574chc%3Dfileted%3Bshow_cube%3Dtrue%3Bsize%3D0.0762%2Bmeter

    Please note that you should always call our encode Configuration endpoint to generate the query param string. If you attempt to encode it directly, your code will fail for documents with many configuration values. For those, we store the configuration on our side and return the query parameter as a key that points to the stored current configuration state.

    Does that clear this up?
  • Matteo_GazzinMatteo_Gazzin Member Posts: 4
    edited April 2019
    Yes, this clarifies a lot. Thanks for the quick answer!
    Anyway, I still have some issues downloading the stl...

    This is what I've done so far:
    - using get versions I get the version ID I'm interested in 
    - using  decode configuration I get the configuration corresponding to that version ID (so I know how to format the configuration properly for the next command)
    - using encode configuration I get the encoded version of my configuration. 
    - I pass this as configuration query param of the exportSTL endpoint, as you said: 
    /api/partstudios/d/.../v/.../e/..../stl?configuration=height%253D0.155%2Bmeter%253Blength%253D1.0%2Bmeter%253Bthickness%253D0.007%2Bmeter%253Bwidth%253D0.101%2Bmeter
    I tried both from a python client and from the API Explorer and I always get Unauthenticated API request but of course, I am authenticated. 
    If I try the same call without the configuration query param, the call goes through and I can download the stl which is of course not updated according to what you explained in your post.   

    Thanks, 
    Matteo
  • Ethan_KEthan_K Member, Onshape Employees Posts: 57
    edited April 2019
    Hi @Matteo_Gazzin
    My guess is that this is a valid issue with the authentication headers you are sending. When requesting an STL, you submit a GET request to a url like: https://cad.onshape.com/api/partstudios/d/cca81d10f239db0db9481e6f/v/aaa25d18038e06f0b2964e2d/e/69c9eedda86512966b20bc90/stl?configuration=List_UKkGODiz574chc%3Dchamfered%3Bshow_cube%3Dfalse%3Bsize%3D0.254%2Bmeter and then you get redirected with a 307 to a url that looks like: https://cad-usw2.onshape.com/modelexport?workspaceId=80887f5994121967bf4d59a6&microversion=54a117438e58ff1e34ac8476&format=STL&mode=text&grouping=true&scale=1.0&units=inch&configuration=List_UKkGODiz574chc%3Dchamfered%3Bshow_cube%3Dfalse%3Bsize%3D0.254%2Bmeter&documentVersionId=aaa25d18038e06f0b2964e2d&_m=6edfc476&documentId=cca81d10f239db0db9481e6f&elementId=69c9eedda86512966b20bc90 . If you are using apiKeys, your client has to recalculate the authentication headers included to reflect the new query params. In our Python client (still in development) we do this automatically. See here for the relevant logic to process all redirect requests (status 300 codes): https://github.com/ethan92429/onshape-clients/blob/master/python/onshape_client/rest.py#L252 . Are you using one of the Onshape provided clients at https://github.com/onshape-public ? 

    Cheers,
    -Ethan
  • Matteo_GazzinMatteo_Gazzin Member Posts: 4
    Hi @ethan_keller924
    you're right, that was the problem. I fixed my code and now it works fine! Thanks. 
    Looking forward to using your new python client. 

    Matteo
Sign In or Register to comment.