Skip to content

Commit

Permalink
added copy workspace to client, made documentation video responsive a…
Browse files Browse the repository at this point in the history
…nd made assembly insert work
  • Loading branch information
eak24 committed Aug 20, 2018
1 parent 4ff6c71 commit 05e516d
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 21 deletions.
16 changes: 16 additions & 0 deletions docs/_static/css/youtube.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* Youtube Reflexive */
.vid-container {
position: relative;
padding-bottom: 50%;
padding-top: 35px; height: 0; overflow: hidden;
}

.vid-container iframe,
.vid-container object,
.vid-container embed {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
2 changes: 2 additions & 0 deletions docs/_templates/layout.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{% extends "!layout.html" %}
{% set css_files = css_files + [ "_static/css/youtube.css" ] %}
5 changes: 4 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,7 @@
# -- Options for todo extension ----------------------------------------------

# If true, `todo` and `todoList` produce output, else they produce nothing.
todo_include_todos = True
todo_include_todos = True

def setup(app):
app.add_stylesheet("css/youtube.css")
10 changes: 5 additions & 5 deletions docs/index.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Welcome to onshapepy's documentation!
Welcome to the OnShapePy Documentation!
=============================================

OnShapePy gives you the ability to control OnShape configurable parts with Python, effectively unlocking the vast
Expand All @@ -7,11 +7,11 @@ Python scientific libraries to OnShape parts.
Quick video demo
------------------

See how to connect to a PartStudio and adjust parameters from python below!

.. raw:: html

<iframe src="https://player.vimeo.com/video/285497632" width="700" height="460" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
<div class="vid-container">
<iframe width="700" height="394" src="https://www.youtube.com/embed/tpqhwz4zX9s" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
</div>

Straight to the code
---------------------
Expand All @@ -26,7 +26,7 @@ First specify your credentials in a file named: "~/.onshapepy". Get your access
**Note: For more complex configurations, see :ref:`OnShapePy Configuration <configuration_file>`.**

Then follow along with the code below:
Then follow along with the Python code below:

.. code-block:: Python
Expand Down
10 changes: 4 additions & 6 deletions onshapepy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'''
onshapepy
=========
Demonstrates usage of API keys for the Onshape REST API
'''
from onshapepy.part import Part
from onshapepy.core.client import Client
import pint
u=pint.UnitRegistry()
11 changes: 7 additions & 4 deletions onshapepy/assembly.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,33 @@
"""

from onshapepy.uri import Uri
from onshapepy.core.context import Context
import json


class Assembly:


def __init__(self, url):
""" Connect to an assembly that points to the assembly specified with did, wid and eid.
""" Connect to an assembly that points to the assembly specified with the url.
Args:
- url (str): The url of the onshape item
"""
self.uri = Uri(url)


def insert(self, part, client):
def insert(self, part):
""" Insert a part into this assembly.
Args:
- part (onshapepy.part.Part) A Part instance that will be inserted.
- client (onshapepy.core.client.Client) A Client instance that does the inserting.
Returns:
- requests.Response: Onshape response data
"""
pass
params = {k: str(v) for k,v in part.params.items()}
c = Context().client
res=c.create_assembly_instance(self.uri.as_dict(), part.uri.as_dict(), params)
return res
22 changes: 21 additions & 1 deletion onshapepy/core/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,25 @@ def list_documents(self):

return self._api.request('get', '/api/documents')

def copy_workspace(self, uri, new_name):
'''
Copy the current workspace.
Args:
- uri (dict): the uri of the workspace being copied. Needs to have a did and wid key.
- new_name (str): the new name of the copied workspace.
Returns:
- requests.Response: Onshape response data
'''

payload = {
'isPublic': True,
'newName': new_name
}

return self._api.request('post', '/api/documents/' + uri['did'] + '/workspaces/' + uri['wvm'] + '/copy', body=payload)

def create_assembly(self, did, wid, name='My Assembly'):
'''
Creates a new assembly element in the specified document / workspace.
Expand Down Expand Up @@ -284,7 +303,8 @@ def create_assembly_instance(self, assembly_uri, part_uri, configuration):
"isWholePartStudio": True,
"configuration": self.encode_configuration(part_uri["did"], part_uri["eid"], configuration)
}
return self._api.request('post', '/api/assemblies/d/' + assembly_uri["did"] + '/w/' + assembly_uri["wvm"] + '/e/' + assembly_uri["eid"] + '/instances', body=payload)
return self._api.request('post', '/api/assemblies/d/' + assembly_uri["did"] + '/' + assembly_uri["wvm_type"] +
'/' + assembly_uri["wvm"] + '/e/' + assembly_uri["eid"] + '/instances', body=payload)

def encode_configuration(self, did, eid, parameters):
'''
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name= 'onshapepy',
version= '0.0.13',
version= '0.0.14',
description= 'Drive part configurations from Python.',
long_description= long_description,
author= 'Ethan Keller',
Expand Down
4 changes: 4 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
def cube():
return Part("https://cad.onshape.com/documents/2d47b6abec9d1de1d2538372/w/39e483948767f72c97d2792f/e/0639ea3c439aa0947744d29a")

@pytest.fixture
def versioned_cube():
return Part("https://cad.onshape.com/documents/2d47b6abec9d1de1d2538372/v/460c8d693cb4140726eada1a/e/0639ea3c439aa0947744d29a")

@pytest.fixture
def assembly():
return Assembly("https://cad.onshape.com/documents/8ec353ba00f37f447b5a61f5/w/04c36c786829759832bd3d1a/e/6f1f9c485ff1e639f4db63c0")
Expand Down
6 changes: 3 additions & 3 deletions tests/test_assembly.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from onshapepy.core.client import Client

def test_insert(cube, assembly):

def test_insert(versioned_cube, assembly):

# Insert into the assembly
assembly.insert(cube, Client())
assembly.insert(versioned_cube)
4 changes: 4 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ def test_new_doc(client):
new_doc = client.new_document(public=True).json()
assert new_doc['name'] == 'Test Document'

def test_copy_workspace(client):
res = client.copy_workspace(uri.as_dict(), "Test 2")
assert res.status_code == 200

def test_encode_configuration(client):
s = client.encode_configuration(uri.did, uri.eid, {"Length": "5 meters", "height": "2 meters"})
assert s == 'Length=5+meters;height=2+meters'
Expand Down

0 comments on commit 05e516d

Please sign in to comment.