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.

Best way to use onshape via python

arpit_agarwal087arpit_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

Comments

  • billy2billy2 Member, OS Professional, Mentor, Developers, User Group Leader Posts: 2,014 PRO
    edited March 2023
    If you want to program a partstudio then you'll be using featurescript. If you want to access an assembly, you'll be using the API which works with any language. Bottom line, if you want to program inside OS, use featurescript.

    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:
    client-->socket-->server-->post-->API-->server-->client

    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.



  • arpit_agarwal087arpit_agarwal087 Member Posts: 9
    Thanks for your response. I am looking to talk to OS with python. However, I am looking for a beginner example on how to do it. You solutions look very good. Is it available for public use or could you share some snippets for somebody starting to use the API. 
  • eric_pestyeric_pesty Member Posts: 1,461 PRO
    Have you looked at this?
    https://www.youtube.com/watch?v=A1AOfzhy5XE
    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...)


  • billy2billy2 Member, OS Professional, Mentor, Developers, User Group Leader Posts: 2,014 PRO
    edited March 2023
    Go look at the link above supplied by @eric_pesty. Alnis gets things running with minimal effort.

    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.

    https://cad.onshape.com/glassworks/explorer/

    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.

    The reason I switched to programming in node is because of JSON. Love/hate javascript, my server code is javascript and the client code is javascript. The object that's passed around is native on my server & client. I can copy code from my server and paste it into the client and make your computer parse the data. My server is a traffic cop and directs the data to clients. Then, the work begins on your computer. I only have one server instance, so I make sure it's not parsing any data. I spreading the process across all those who come to my site. Maybe I should begin a visit to site with a warning "I don't use cookies but I do use your cpu".

    When you post, you'll be sending data back to the server in a JSON format that you converted from python back to JSON. Python objects are very similar to javascript objects and may not be an issue. But..... I would run your converted the python object through a JSON linter to insure compatibility, you don't want to debug this through the onshape server.

    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.

  • arpit_agarwal087arpit_agarwal087 Member Posts: 9
    I have looked at the google colab. However, it depends on onshape_client which is archived. That is why I am looking for another solution. 
  • arpit_agarwal087arpit_agarwal087 Member Posts: 9
    I did a stupid mistake of replacing access_key with secret_key while making request. I am able to create a new document using the onshape-client python example. However, I am wondering how to do it without the onshape-client python as it is archived project.

    from onshape_client import OnshapeElement, Client
    client = Client()
    new_doc = OnshapeElement.create("Engine")
  • liam_brennan625liam_brennan625 Member Posts: 5
    Are there any updates on this since March? The onshape-clients repo is still archived and I can't seem to find what the intended alternative is. For now, I guess just using the REST api with the python requests library works ok?
Sign In or Register to comment.