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.
Best way to use onshape via python
arpit_agarwal087 Member Posts: 9 ✭
I am wondering what is the best way to work with onshape using python. I saw that the onshape-client is archived. Is there any other alternative? Any suggestions would be helpful
I looked though onshape-client which is a thorough python app that exercises many of the API's options.
The example index.html is larger than my entire website including client code, server code & all modules used to talk to OS. It's a large example.
Read the API and pick what you want todo, then do it. It doesn't require a mountain of code.
There's different ways to access the OS api:
-login into OS and use localhost to call the API. Logging into OS gives your computer rights to use the API. (I don't use this)
-register with OS API and get api keys, now you have 2 ways to get to the API
--you can easily get to your account and access your data
--you can register with the app store and request access to other users data. This requires oauth2.
The way I do it:
-I open a socket between the client & server
-My server makes connections to OS server using api keys to my data.
-I have registered in the app store and have had access to other OS data, but I don't do that any longer.
So, ideally here's the preferred path:
So I can ask OS for images of all documents and the API will start sending images asynchronously back to the client. The client then unpacks json for the images and I update the page.
This could take some time, creates a ton of internet traffic and it's fun. You could start responding to the rendered page by deleting stuff which would in turn overlay more internet traffic through the socket and you could delete based on the images OS sent to you. It's your responsibility to up date your local page and remove the images of the documents you've deleted.
If you want to use python with OS, then you'll be using the API. If you want to program inside OS use featurescript, it's much better than python for programming inside OS.
There are some links in the description to some colab notebooks.
I was able to get some basic stuff through the API thanks to these examples (with no prior knowledge of how any of it worked...)
My code, won't help you. I run node and I'm running off a server, the login is totally different.
Just keep in mind, that when you login into onshape, your computer has access to onshape's api. For me, my server never logs into onshape so I use a different method to access onshape.
Here's a listing of the current API calls.
Not all calls are listed though, in many cases, you inspect the network traffic for onshape and capture the call that the onshape client makes to the servers. Let's say you want gltf data. It's not documented. So you open the debug console in your browser, then click on the gltf command inside onshape and capture the call the onshape client is making to the onshape server.
The API documentation is not complete by any means, but you can get by.
Any cloud application, you can hijack the commands they're using.
As an easy goal, try and retrieve a listing of your documents. Alnis is listing a public document so I'm not sure you have access to your own stuff. I've never run the API from localhost. You should have access to your documents because your client has access.
REST is a JSON file and there should be a function call in python to convert JSON into a python object. Then you'll need to create HTML tags and display the results in a browser or just print it out to a console.
This forum is the best place to ask questions but most here write featurescripts. Tech support has a lot guys now that can help with basic API questions.
Eventually you'll want a server so others can experience your API magic. Servers are cheap and will make your magic accessible to all.