Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.
First time visiting? Here are some places to start:- Looking for a certain topic? Check out the categories filter or use Search (upper right).
- Need support? Ask a question to our Community Support category.
- Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
- 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_fenoglietto
Member Posts: 36 PRO
in General
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
He uses the following functions to connect to the onshape document
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
<div>part_URL = "https://cad.onshape.com/documents/{}/w/{}/e/{}".format( self.did, self.wid, self.eid )</div><div>self.myPart = Part( part_URL ) </div><div>self.c = Client()</div>
To update the configurations, we use:
But this does not seem to work anymore.
Can I get some guidance on how to properly update the value of the configurations?
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?
Tagged:
0
Comments
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.
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?
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.
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...
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
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:
Then, I modify "res" and send it as a 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?
Use something like this (html img tag) to show it:
Where ${image.images} is the data you have posted.
You should see the configuration you've asked for.
I have a function that builds the url and I use it for debugging purposes. Here's an example I'm using:
Look at the inputs & the configuration variables, they should match. In this case I make 3 async calls to get 3 images.
Here's the syntax (give me a shaded view made with this configuration):
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
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.
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...
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.
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):
Once again, I would still like to know if I can reply on onshapepy or if it will be removed at some point.
Having multiple people accessing the configuration at the same time, this way is best.