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.

Modifying Configuration (Variable) using Python and Onshape API

fluvio_lobo_fenogliettofluvio_lobo_fenoglietto Member Posts: 36 PRO
Hello,

A student in my lab had built a python script that modified configuration/variables on a part.
He was using this github repo.

Having imported the library
from    onshapepy.play              import  *                           # Onshape API
He uses the following functions to connect to the onshape document
<div>part_URL&nbsp; &nbsp; = "https://cad.onshape.com/documents/{}/w/{}/e/{}".format( self.did, self.wid, self.eid )</div><div>self.myPart = Part( part_URL )&nbsp;</div><div>self.c&nbsp; &nbsp; &nbsp; = Client()</div>
To update the configurations, we use:
self.myPart.params == { self.keys[j]:new_val[j]*u.mm }
Note that "key" is the name of the configuration variable
But this does not seem to work anymore.

Can I get some guidance on how to properly update the value of the configurations?

Comments

  • billy2billy2 Member, OS Professional, Mentor, Developers, User Group Leader Posts: 2,068 PRO
    It's all been switched up.

    Configurations aren't updated any longer, instead, you send query parameters for part studios or assemblies.

    It works better this way.

    I think you'll have to re-write your code.


  • fluvio_lobo_fenogliettofluvio_lobo_fenoglietto Member Posts: 36 PRO
    @billy2

    Thank you!
    That was my fear.
    I am in a good spot for re-writing.

    Could I get an example from you?
    Can I still directly affect variables?
  • billy2billy2 Member, OS Professional, Mentor, Developers, User Group Leader Posts: 2,068 PRO
    here's an example of a call to get an image & set a configuration:

    https://cad.onshape.com/api/partstudios/d/9fcf3164704365d01771f942/v/bf27dccad59772f34b3c34a9/e/aaae3de87dcf62d3ea140ba2/shadedviews?outputWidth=900&outputHeight=900&pixelSize=0&viewMatrix=0.612%2C0.612%2C0%2C0%2C-0.354%2C0.354%2C0.707%2C0%2C0.707%2C-0.707%2C0.707%2C0.0&configuration=List_SiRnPmmz6O52QH%3Dlarge 

    the cool thing is that if 5 people are banging on your site, you're not updating your model each time, each call is it's separate request and managing all this is much better than in the past.

    Put the variables after "configuration" in the query string and you'll get what you want.


  • fluvio_lobo_fenogliettofluvio_lobo_fenoglietto Member Posts: 36 PRO
    @billy2

    Thanks!

    I have been reading a little through the day and I think I can change the program myself.
    I do wish the documentation was a little more standard, but that may be just me.

    I could not really see anything on your example tho. I got this:

    Unless I am doing something wrong...
  • Jake_RosenfeldJake_Rosenfeld Moderator, Onshape Employees, Developers Posts: 1,646
    @fluvio_lobo_fenoglietto

    I think Billy's point was that that is how a URL is formed with a configuration as one of the parameters.  See "configuration=" section
    Jake Rosenfeld - Modeling Team
  • fluvio_lobo_fenogliettofluvio_lobo_fenoglietto Member Posts: 36 PRO
    @billy2, @Jake_Rosenfeld,

    I think I figured it our between your answers and the github repo.
    To retrieve the configurations from my python script/module, I am using:
    res = c._api.request('get', '/api/partstudios/d/' + did + '/w/' + wid + '/e/' + eid + '/configuration')

    Then, I modify "res" and send it as a payload:
    res = c._api.request('post', '/api/partstudios/d/' + did + '/w/' + wid + '/e/' + eid + '/configuration', body=payload)
    My modified payload is essential the entire res.text retrieved initially.
    Then I will write a script that finds the lines associate with the "parameterId" and value I wish to update.

    I am not familiar with these request data structures, is there a more efficient way of updating a section of the payload rather than the entire text?
    <div>"currentConfiguration" : [ {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "units" : "millimeter",</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "value" : 65.0</div><div>&nbsp; &nbsp; &nbsp; },</div>



  • billy2billy2 Member, OS Professional, Mentor, Developers, User Group Leader Posts: 2,068 PRO
    Yeap, the good news is that it's working. What you have is an image. 

    Use something like this (html img tag) to show it:
    `<img alt="Rendered Image" src="data:image/png;charset=UTF-8;base64,${image.images}">`

    Where 
    ${image.images} is the data you have posted.

    You should see the configuration you've asked for.


  • billy2billy2 Member, OS Professional, Mentor, Developers, User Group Leader Posts: 2,068 PRO
    edited May 2019
    I believe all your requests will be get requests from now on. You get the configuration table & then build a request so you can get something with a particular configuration. In this case, I'm asking for an image with a configuration.

    I have a function that builds the url and I use it for debugging purposes. Here's an example I'm using:

    makeURL https://cad.onshape.com/api/partstudios/d/7a91a7e7fdac227ae58f29bb/w/9678a49a38974f64811c6754/e/5c0b6c8bc4f7726d29e3d40e/shadedviews?includeSurfaces=false&useAntiAliasing=true&outputWidth=400&outputHeight=400&pixelSize=1&viewMatrix=top&configuration=Diameter%3D10%20mm%3BSquare%3D10%20mm%3BCorner_Radius%3D5%20mm%3BTop_Radius%3D1%20mm%3BFillet_Corners%3Dtrue%3BFillet_Top%3Dtrue%3BList_C4mF1zZQmzWyHr%3DDefault%3BBlock_Draft%3Dtrue%3BList_dGypK79Vvq08vL%3DDefault%3BPocket_Draft%3Dtrue%3B
    
    <br>




    Look at the inputs & the configuration variables, they should match. In this case I make 3 async calls to get 3 images.





  • billy2billy2 Member, OS Professional, Mentor, Developers, User Group Leader Posts: 2,068 PRO
    edited May 2019
    So after getting the configuration data, you have to build the query.

    Here's the syntax (give me a shaded view made with this configuration):
    https://cad.onshape.com/api/partstudios/DWE?shadowedview=....&configuration=......
    
    
    
  • Ethan_KEthan_K Member, Onshape Employees Posts: 57
    You can also use these instructions to generate a url-specific configuration https://onshape-public.github.io/onshape-clients/python_html/make_configuration_url.html and the accompanying package - https://pypi.org/project/onshape-client/ has a lot of those endpoints built in. It is still in the experimental phase, but that will be the officially supported python package for interacting with the Onshape API. 
  • fluvio_lobo_fenogliettofluvio_lobo_fenoglietto Member Posts: 36 PRO
    @billy, @ethan_keller769,

    Thank you both, I am getting the hang of it.
    I am also learning the structure of the json() body so that I can make the changes directly
    I will make sure to post the program here after making improvements

  • billy2billy2 Member, OS Professional, Mentor, Developers, User Group Leader Posts: 2,068 PRO
    edited May 2019
    For me, this new pattern works really well. I'm wanting something, mainly an image and it's working well.

    One tip about json, it's best to modify the leaf and put it back vs. trying to send bits & pieces back. When you ask for something, modify it and then put the whole thing back. I was trying to upload only the parts that changed and they didn't work well.


  • fluvio_lobo_fenogliettofluvio_lobo_fenoglietto Member Posts: 36 PRO
    @billy2,

    How do I get changes to regenerate without refreshing the browser manually?
    I am trying to export the file as an .STL after updating the configuration.
    I thought this happened automatically...but I keep exporting the default model...
  • billy2billy2 Member, OS Professional, Mentor, Developers, User Group Leader Posts: 2,068 PRO
    edited May 2019
    I'm not sure how you have things set up, your site's architecture and what you are doing?

    For me, I'm using sockets. So when you connect to my server https://www.rustyshed.com, there's an open pipeline between client & server. 

    Sequence between client, server & OS
    -client    onclick event sends a json packet to server
    -server    sees packet and parses packet
    -server    sees an OS requests and loads onshape handler (this code is at git)
    -server    uses async await and puts out get to OS
    -server     on return sends back to client
    -client     then parses, updates <div id="changeMe">typically</div>

    I think you need a server. I'm not a python guy so I don't know how you're programming this.

    The basic circle is called AJAX and you shouldn't be sending requests by refreshing your browser. You lose state. What sent the request will be gone when the message is sent back. Go look into XMLHttpRequest.

    When I run local, it's still a client/server relationship. The server is just local host.

    To get an stl, you ask another server @ onshape by a redirect. 

    I'm not sure this is an answer. This is the cloud and we're typically making a bunch of promises.




  • Ethan_KEthan_K Member, Onshape Employees Posts: 57
    @fluvio_lobo_fenoglietto Can you post the exact URL you are calling to get the STL?
  • fluvio_lobo_fenogliettofluvio_lobo_fenoglietto Member Posts: 36 PRO
    edited May 2019
    @ethan_keller769, @billy2

    Quick update... I think I got it to work...

    The solution was changing the default parameters value instead of the current configuration value and expression. (duh moment!) This may have been a huge oversight on my part... >.>

    Note that I still do not get any "live rendering" or "regeneration" but the exported .STL matches the changes

    Overall, my python script goes as follows (simplified version):
    from onshapepy.play import *<div>import json</div><div><br></div><div>did = "04b732c124cfa152cf7c07f3"</div><div>wid = "c4358308cbf0c97a44d8a71a"</div><div>eid = "a23208c314d70c14da7071e6"</div><div><br></div><div>part_URL&nbsp; &nbsp; = "https://cad.onshape.com/documents/{}/w/{}/e/{}".format(did,wid,eid)</div><div>myPart = Part( part_URL )</div><div>c&nbsp; &nbsp; &nbsp; = Client()
    
    # get configuration structure...</div><div>res = c._api.request('get','/api/partstudios/d/{}/w/{}/e/{}/configuration'.format(self.did, self.wid, self.eid))
    
    # translate said response to a json body...
    r = json.loads(res)
    
    # modify the json structure...
    r['configurationParameters'][0]['message']['rangeAndDefault']['message']['defaultValue'] = 65
    
    # update configuration...
    c._api.request('post','/api/partstudios/d/{}/w/{}/e/{}/configuration'.format(self.did, self.wid, self.eid),body=json.dumps(payload))
    
    # export stl...
    stl = c._api.request('get','/api/partstudios/d/{}/w/{}/e/{}/stl'.format(self.did, self.wid, self.eid))
    
    </div>

    Once again, I would still like to know if I can reply on onshapepy or if it will be removed at some point.

    Thanks for all the help!
  • Ethan_KEthan_K Member, Onshape Employees Posts: 57
    There is no way to now get a `live rendering` change from changing the current configuration. This is because changing the current configuration is done on a per-client basis, and so there is no single source of truth about which configuration the partStudio is in. This is intentional because our conceptual model of a configuration is that it is a 'lens' through which you view the part and therefore using a different lens should not change the 'part recipe' at all. If you change the default, this will be recorded in a new microversion, and will behave a bit more like what we had before with the current configuration. onshapepy will remain on Pypi, but no subsequent updates will be made to that package. It is deprecated and there are no guarantees moving forward that it will be updated.
  • billy2billy2 Member, OS Professional, Mentor, Developers, User Group Leader Posts: 2,068 PRO
    I prefer the new way, at least for a website. 

    Having multiple people accessing the configuration at the same time, this way is best.



Sign In or Register to comment.